]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QSearch.cpp
delete unneeded Menubar virtual interface.
[lyx.git] / src / frontends / qt4 / QSearch.cpp
1 /**
2  * \file QSearch.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 "QSearch.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 // QSearchDialog
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 QSearchDialog::QSearchDialog(QSearch * 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 QSearchDialog::show()
64 {
65         QDialog::show();
66         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
67 }
68
69
70 void QSearchDialog::closeEvent(QCloseEvent * e)
71 {
72         form_->slotWMHide();
73         e->accept();
74 }
75
76
77 void QSearchDialog::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 QSearchDialog::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 }
100
101
102 void QSearchDialog::replaceClicked()
103 {
104         docstring const find = qstring_to_ucs4(findCO->currentText());
105         docstring const replace = qstring_to_ucs4(replaceCO->currentText());
106         form_->replace(find, replace,
107                 caseCB->isChecked(),
108                 wordsCB->isChecked(),
109                 backwardsCB->isChecked(), false);
110         uniqueInsert(findCO, findCO->currentText());
111         uniqueInsert(replaceCO, replaceCO->currentText());
112 }
113
114
115 void QSearchDialog::replaceallClicked()
116 {
117         form_->replace(qstring_to_ucs4(findCO->currentText()),
118                 qstring_to_ucs4(replaceCO->currentText()),
119                 caseCB->isChecked(),
120                 wordsCB->isChecked(),
121                 false, true);
122         uniqueInsert(findCO, findCO->currentText());
123         uniqueInsert(replaceCO, replaceCO->currentText());
124 }
125
126
127 /////////////////////////////////////////////////////////////////////
128 //
129 // QSearch
130 //
131 /////////////////////////////////////////////////////////////////////
132
133
134 typedef QController<ControlSearch, QView<QSearchDialog> > SearchBase;
135
136
137 QSearch::QSearch(Dialog & parent)
138         : SearchBase(parent, _("Find and Replace"))
139 {
140 }
141
142
143 void QSearch::build_dialog()
144 {
145         dialog_.reset(new QSearchDialog(this));
146
147         bcview().setCancel(dialog_->closePB);
148         bcview().addReadOnly(dialog_->replaceCO);
149         bcview().addReadOnly(dialog_->replacePB);
150         bcview().addReadOnly(dialog_->replaceallPB);
151
152         dialog_->replacePB->setEnabled(false);
153         dialog_->replaceallPB->setEnabled(false);
154 }
155
156
157 void QSearch::find(docstring const & str, bool casesens,
158                    bool words, bool backwards)
159 {
160         controller().find(str, casesens, words, !backwards);
161 }
162
163
164 void QSearch::replace(docstring const & findstr, docstring const & replacestr,
165         bool casesens, bool words, bool backwards, bool all)
166 {
167         controller().replace(findstr, replacestr, casesens, words,
168                              !backwards, all);
169 }
170
171 } // namespace frontend
172 } // namespace lyx
173
174
175 #include "QSearch_moc.cpp"