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