]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSearch.cpp
move our stuff off the Q* namespace
[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 #include "Qt2BC.h"
17
18 #include "controllers/ControlSearch.h"
19
20 #include <QLineEdit>
21 #include <QCloseEvent>
22
23 using std::string;
24
25 namespace lyx {
26 namespace frontend {
27
28
29 /////////////////////////////////////////////////////////////////////
30 //
31 // GuiSearchDialog
32 //
33 /////////////////////////////////////////////////////////////////////
34
35
36 static void uniqueInsert(QComboBox * box, QString const & text)
37 {
38         for (int i = 0; i < box->count(); ++i) {
39                 if (box->itemText(i) == text)
40                         return;
41         }
42
43         box->addItem(text);
44 }
45
46
47 GuiSearchDialog::GuiSearchDialog(GuiSearch * form)
48         : form_(form)
49 {
50         setupUi(this);
51
52         connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
53         connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
54         connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
55         connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
56         connect(findCO, SIGNAL(editTextChanged(const QString &)),
57                 this, SLOT(findChanged()));
58
59         setFocusProxy(findCO);
60 }
61
62
63 void GuiSearchDialog::show()
64 {
65         QDialog::show();
66         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
67 }
68
69
70 void GuiSearchDialog::closeEvent(QCloseEvent * e)
71 {
72         form_->slotWMHide();
73         e->accept();
74 }
75
76
77 void GuiSearchDialog::findChanged()
78 {
79         if (findCO->currentText().isEmpty()) {
80                 findPB->setEnabled(false);
81                 replacePB->setEnabled(false);
82                 replaceallPB->setEnabled(false);
83         } else {
84                 findPB->setEnabled(true);
85                 replacePB->setEnabled(!form_->readOnly());
86                 replaceallPB->setEnabled(!form_->readOnly());
87         }
88 }
89
90
91 void GuiSearchDialog::findClicked()
92 {
93         docstring const find = qstring_to_ucs4(findCO->currentText());
94         form_->find(find,
95                 caseCB->isChecked(),
96                 wordsCB->isChecked(),
97                 backwardsCB->isChecked());
98         uniqueInsert(findCO, findCO->currentText());
99         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
100 }
101
102
103 void GuiSearchDialog::replaceClicked()
104 {
105         docstring const find = qstring_to_ucs4(findCO->currentText());
106         docstring const replace = qstring_to_ucs4(replaceCO->currentText());
107         form_->replace(find, replace,
108                 caseCB->isChecked(),
109                 wordsCB->isChecked(),
110                 backwardsCB->isChecked(), false);
111         uniqueInsert(findCO, findCO->currentText());
112         uniqueInsert(replaceCO, replaceCO->currentText());
113 }
114
115
116 void GuiSearchDialog::replaceallClicked()
117 {
118         form_->replace(qstring_to_ucs4(findCO->currentText()),
119                 qstring_to_ucs4(replaceCO->currentText()),
120                 caseCB->isChecked(),
121                 wordsCB->isChecked(),
122                 false, true);
123         uniqueInsert(findCO, findCO->currentText());
124         uniqueInsert(replaceCO, replaceCO->currentText());
125 }
126
127
128 /////////////////////////////////////////////////////////////////////
129 //
130 // GuiSearch
131 //
132 /////////////////////////////////////////////////////////////////////
133
134
135 typedef QController<ControlSearch, GuiView<GuiSearchDialog> > SearchBase;
136
137
138 GuiSearch::GuiSearch(Dialog & parent)
139         : SearchBase(parent, _("Find and Replace"))
140 {
141 }
142
143
144 void GuiSearch::build_dialog()
145 {
146         dialog_.reset(new GuiSearchDialog(this));
147
148         bcview().setCancel(dialog_->closePB);
149         bcview().addReadOnly(dialog_->replaceCO);
150         bcview().addReadOnly(dialog_->replacePB);
151         bcview().addReadOnly(dialog_->replaceallPB);
152
153         dialog_->replacePB->setEnabled(false);
154         dialog_->replaceallPB->setEnabled(false);
155 }
156
157
158 void GuiSearch::find(docstring const & str, bool casesens,
159                    bool words, bool backwards)
160 {
161         controller().find(str, casesens, words, !backwards);
162 }
163
164
165 void GuiSearch::replace(docstring const & findstr, docstring const & replacestr,
166         bool casesens, bool words, bool backwards, bool all)
167 {
168         controller().replace(findstr, replacestr, casesens, words,
169                              !backwards, all);
170 }
171
172 } // namespace frontend
173 } // namespace lyx
174
175
176 #include "GuiSearch_moc.cpp"