~ubuntu-branches/ubuntu/maverick/juffed/maverick-proposed

« back to all changes in this revision

Viewing changes to src/gui/FindDlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Maia Kozheva
  • Date: 2010-07-22 15:01:58 UTC
  • Revision ID: james.westby@ubuntu.com-20100722150158-m3792gi6tj0y8zl6
Tags: upstream-0.8.1
ImportĀ upstreamĀ versionĀ 0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
JuffEd - An advanced text editor
 
3
Copyright 2007-2009 Mikhail Murzin
 
4
 
 
5
This program is free software; you can redistribute it and/or
 
6
modify it under the terms of the GNU General Public License 
 
7
version 2 as published by the Free Software Foundation.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
GNU General Public License for more details.
 
13
 
 
14
You should have received a copy of the GNU General Public License
 
15
along with this program; if not, write to the Free Software
 
16
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
17
*/
 
18
 
 
19
#include "FindDlg.h"
 
20
 
 
21
//      Qt headers
 
22
#include <QtGui/QKeyEvent>
 
23
#include <QtGui/QLineEdit>
 
24
 
 
25
#include "IconManager.h"
 
26
#include "MainSettings.h"
 
27
 
 
28
namespace Juff {
 
29
namespace GUI {
 
30
 
 
31
QString FindDlg::lastString_ = "";
 
32
QString FindDlg::lastReplaceText_ = "";
 
33
bool FindDlg::matchCase_ = true;
 
34
bool FindDlg::backward_ = false;
 
35
bool FindDlg::regExpMode_ = false;
 
36
bool FindDlg::wholeWords_ = false;
 
37
bool FindDlg::multiLine_ = false;
 
38
QStringList FindDlg::strings_;
 
39
QStringList FindDlg::replaces_;
 
40
 
 
41
FindDlg::FindDlg(QWidget* parent, bool repl) : 
 
42
        QDialog(parent) {
 
43
 
 
44
        uiFind.setupUi(this);
 
45
 
 
46
        foreach (QString str, strings_)
 
47
                uiFind.findCmb->addItem(str);
 
48
        foreach (QString repl, replaces_)
 
49
                uiFind.replaceCmb->addItem(repl);
 
50
 
 
51
        uiFind.findCmb->setCompleter(0);
 
52
        uiFind.replaceCmb->setCompleter(0);
 
53
 
 
54
        connect(uiFind.findBtn, SIGNAL(clicked()), SLOT(accept()));
 
55
        connect(uiFind.cancelBtn, SIGNAL(clicked()), SLOT(reject()));
 
56
        connect(uiFind.replaceChk, SIGNAL(toggled(bool)), SLOT(setReplaceMode(bool)));
 
57
        connect(uiFind.multiLineChk, SIGNAL(toggled(bool)), SLOT(multiLineChecked(bool)));
 
58
        connect(uiFind.regexpChk, SIGNAL(toggled(bool)), SLOT(regExpChecked(bool)));
 
59
 
 
60
        uiFind.findCmb->setEditText(lastString_);
 
61
        uiFind.mlEd->setText(lastString_);
 
62
        uiFind.replaceCmb->setEditText(lastReplaceText_);
 
63
        uiFind.matchCaseChk->setChecked(matchCase_);
 
64
        uiFind.backwardChk->setChecked(backward_);
 
65
        uiFind.replaceChk->setChecked(false);
 
66
        uiFind.regexpChk->setChecked(regExpMode_);
 
67
        uiFind.multiLineChk->setChecked(multiLine_);
 
68
        uiFind.wholeWordsChk->setChecked(wholeWords_);
 
69
 
 
70
        setReplaceMode(repl);
 
71
        if ( !multiLine_ ) {
 
72
                uiFind.findCmb->setFocus();
 
73
                uiFind.findCmb->lineEdit()->selectAll();
 
74
        }
 
75
        else {
 
76
                uiFind.mlEd->setFocus();
 
77
                uiFind.mlEd->selectAll();
 
78
        }
 
79
        uiFind.mlEd->setVisible(multiLine_);
 
80
        uiFind.findCmb->setVisible(!multiLine_);
 
81
 
 
82
        resize(MainSettings::findDlgRect().size());
 
83
}
 
84
 
 
85
FindDlg::~FindDlg() {
 
86
        lastReplaceText_ = uiFind.replaceCmb->currentText();
 
87
        matchCase_ = uiFind.matchCaseChk->isChecked();
 
88
        backward_ = uiFind.backwardChk->isChecked();
 
89
        regExpMode_ = uiFind.regexpChk->isChecked();
 
90
        wholeWords_ = uiFind.wholeWordsChk->isChecked();
 
91
        multiLine_ = uiFind.multiLineChk->isChecked();
 
92
        if ( multiLine_ ) {
 
93
                lastString_ = uiFind.mlEd->toPlainText();
 
94
        }
 
95
        else {
 
96
                lastString_ = uiFind.findCmb->currentText();
 
97
        }
 
98
 
 
99
        MainSettings::setFindDlgRect(rect());
 
100
        
 
101
        if ( strings_.contains(lastString_) )
 
102
                strings_.removeAll(lastString_);
 
103
        if ( replaces_.contains(lastReplaceText_) )
 
104
                replaces_.removeAll(lastReplaceText_);
 
105
        if ( !lastString_.isEmpty() )
 
106
                strings_.prepend(lastString_);
 
107
        if ( !lastReplaceText_.isEmpty() )
 
108
                replaces_.prepend(lastReplaceText_);
 
109
}
 
110
 
 
111
void FindDlg::setText(const QString& t) {
 
112
        if ( uiFind.multiLineChk->isChecked() ) {
 
113
                uiFind.mlEd->setText(t); 
 
114
                uiFind.mlEd->selectAll();
 
115
        }
 
116
        else {
 
117
                if ( !t.isEmpty() ) {
 
118
                        uiFind.findCmb->insertItem(0, t);
 
119
                        uiFind.findCmb->setCurrentIndex(0); 
 
120
                        uiFind.findCmb->lineEdit()->selectAll();
 
121
                }
 
122
        }
 
123
}
 
124
 
 
125
QString FindDlg::text() const {
 
126
        if ( uiFind.multiLineChk->isChecked() )
 
127
                return uiFind.mlEd->toPlainText();
 
128
        else
 
129
                return uiFind.findCmb->currentText();
 
130
}
 
131
 
 
132
QString FindDlg::replaceTo() const {
 
133
        QString replStr = uiFind.replaceCmb->currentText();
 
134
        replStr.replace("\\n", "\n");
 
135
        replStr.replace("\\r", "\r");
 
136
        replStr.replace("\\t", "\t");
 
137
        return replStr;
 
138
}
 
139
 
 
140
DocFindFlags FindDlg::flags() const {
 
141
        return DocFindFlags(uiFind.replaceChk->isChecked(), uiFind.matchCaseChk->isChecked(), 
 
142
                        uiFind.backwardChk->isChecked(), uiFind.regexpChk->isChecked(), 
 
143
                        uiFind.wholeWordsChk->isChecked(), uiFind.multiLineChk->isChecked());
 
144
}
 
145
 
 
146
void FindDlg::keyPressEvent(QKeyEvent* e) {
 
147
        if ( ( e->key() == Qt::Key_F || e->key() == Qt::Key_R ) && e->modifiers() & Qt::ControlModifier) {
 
148
                uiFind.replaceChk->toggle();
 
149
        }
 
150
        QDialog::keyPressEvent(e);
 
151
}
 
152
 
 
153
void FindDlg::setReplaceMode(bool replaceMode) {
 
154
        if (replaceMode) {
 
155
                uiFind.findBtn->setText(tr("Replace"));
 
156
                uiFind.findBtn->setIcon(IconManager::instance()->getIcon(ID_REPLACE));
 
157
                setWindowTitle(tr("Replace"));
 
158
                uiFind.replaceCmb->setFocus();
 
159
                uiFind.replaceCmb->lineEdit()->selectAll();
 
160
        }
 
161
        else {
 
162
                uiFind.findBtn->setText(tr("Find"));
 
163
                uiFind.findBtn->setIcon(IconManager::instance()->getIcon(ID_FIND));
 
164
                setWindowTitle(tr("Find"));
 
165
        }
 
166
        if (uiFind.replaceChk->isChecked() != replaceMode)
 
167
                uiFind.replaceChk->setChecked(replaceMode);
 
168
}
 
169
 
 
170
void FindDlg::multiLineChecked(bool chk) {
 
171
        if ( chk ) {
 
172
                uiFind.lowerSpacer->changeSize(0, 0, QSizePolicy::Minimum, QSizePolicy::Fixed);
 
173
                uiFind.mlEd->setText(uiFind.findCmb->currentText());
 
174
        }
 
175
        else {
 
176
                uiFind.findCmb->setEditText(uiFind.mlEd->toPlainText());
 
177
                uiFind.lowerSpacer->changeSize(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
 
178
        }
 
179
}
 
180
 
 
181
void FindDlg::regExpChecked(bool chk) {
 
182
        if ( !chk ) {
 
183
                if ( uiFind.multiLineChk->isChecked() ) {
 
184
                        multiLine_ = true;
 
185
                        uiFind.multiLineChk->setChecked(false);
 
186
                }
 
187
        }
 
188
        else {
 
189
                uiFind.multiLineChk->setChecked(multiLine_);
 
190
        }
 
191
}
 
192
 
 
193
}       //      namespace GUI
 
194
}       //      namespace Juff