]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSearch.cpp
merge ButtonController and its view (Qt2BC in this case)
[lyx.git] / src / frontends / qt4 / GuiSearch.cpp
1 /**
2  * \file GuiSearch.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Edwin Leuven
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiSearch.h"
15 #include "qt_helpers.h"
16
17 #include <QLineEdit>
18 #include <QCloseEvent>
19
20 using std::string;
21
22 namespace lyx {
23 namespace frontend {
24
25
26 /////////////////////////////////////////////////////////////////////
27 //
28 // GuiSearchDialog
29 //
30 /////////////////////////////////////////////////////////////////////
31
32
33 static void uniqueInsert(QComboBox * box, QString const & text)
34 {
35         for (int i = 0; i < box->count(); ++i) {
36                 if (box->itemText(i) == text)
37                         return;
38         }
39
40         box->addItem(text);
41 }
42
43
44 GuiSearchDialog::GuiSearchDialog(GuiSearch * form)
45         : form_(form)
46 {
47         setupUi(this);
48
49         connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
50         connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
51         connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
52         connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
53         connect(findCO, SIGNAL(editTextChanged(const QString &)),
54                 this, SLOT(findChanged()));
55
56         setFocusProxy(findCO);
57 }
58
59
60 void GuiSearchDialog::show()
61 {
62         QDialog::show();
63         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
64 }
65
66
67 void GuiSearchDialog::closeEvent(QCloseEvent * e)
68 {
69         form_->slotWMHide();
70         e->accept();
71 }
72
73
74 void GuiSearchDialog::findChanged()
75 {
76         if (findCO->currentText().isEmpty()) {
77                 findPB->setEnabled(false);
78                 replacePB->setEnabled(false);
79                 replaceallPB->setEnabled(false);
80         } else {
81                 findPB->setEnabled(true);
82                 replacePB->setEnabled(!form_->readOnly());
83                 replaceallPB->setEnabled(!form_->readOnly());
84         }
85 }
86
87
88 void GuiSearchDialog::findClicked()
89 {
90         docstring const find = qstring_to_ucs4(findCO->currentText());
91         form_->find(find,
92                 caseCB->isChecked(),
93                 wordsCB->isChecked(),
94                 backwardsCB->isChecked());
95         uniqueInsert(findCO, findCO->currentText());
96         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
97 }
98
99
100 void GuiSearchDialog::replaceClicked()
101 {
102         docstring const find = qstring_to_ucs4(findCO->currentText());
103         docstring const replace = qstring_to_ucs4(replaceCO->currentText());
104         form_->replace(find, replace,
105                 caseCB->isChecked(),
106                 wordsCB->isChecked(),
107                 backwardsCB->isChecked(), false);
108         uniqueInsert(findCO, findCO->currentText());
109         uniqueInsert(replaceCO, replaceCO->currentText());
110 }
111
112
113 void GuiSearchDialog::replaceallClicked()
114 {
115         form_->replace(qstring_to_ucs4(findCO->currentText()),
116                 qstring_to_ucs4(replaceCO->currentText()),
117                 caseCB->isChecked(),
118                 wordsCB->isChecked(),
119                 false, true);
120         uniqueInsert(findCO, findCO->currentText());
121         uniqueInsert(replaceCO, replaceCO->currentText());
122 }
123
124
125 /////////////////////////////////////////////////////////////////////
126 //
127 // GuiSearch
128 //
129 /////////////////////////////////////////////////////////////////////
130
131
132 GuiSearch::GuiSearch(GuiDialog & parent)
133         : GuiView<GuiSearchDialog>(parent, _("Find and Replace"))
134 {
135 }
136
137
138 void GuiSearch::build_dialog()
139 {
140         dialog_.reset(new GuiSearchDialog(this));
141
142         bc().setCancel(dialog_->closePB);
143         bc().addReadOnly(dialog_->replaceCO);
144         bc().addReadOnly(dialog_->replacePB);
145         bc().addReadOnly(dialog_->replaceallPB);
146
147         dialog_->replacePB->setEnabled(false);
148         dialog_->replaceallPB->setEnabled(false);
149 }
150
151
152 void GuiSearch::find(docstring const & str, bool casesens,
153                    bool words, bool backwards)
154 {
155         controller().find(str, casesens, words, !backwards);
156 }
157
158
159 void GuiSearch::replace(docstring const & findstr, docstring const & replacestr,
160         bool casesens, bool words, bool backwards, bool all)
161 {
162         controller().replace(findstr, replacestr, casesens, words,
163                              !backwards, all);
164 }
165
166 } // namespace frontend
167 } // namespace lyx
168
169
170 #include "GuiSearch_moc.cpp"