Debian Codegraph @

src/Gui/Dialogs/DlgParameterFind.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QMessageBox>
24 #include <QPushButton>
25
26 #include "Dialogs/DlgParameterFind.h"
27 #include "ui_DlgParameterFind.h"
28 #include "Dialogs/DlgParameterImp.h"
29
30
31 using namespace Gui::Dialog;
32
33 /* TRANSLATOR Gui::Dialog::DlgParameterFind */
34
35 DlgParameterFind::DlgParameterFind(DlgParameterImp* parent)
36 : QDialog(parent)
37 , ui(new Ui_DlgParameterFind)
38 , dialog(parent)
39 {
40 ui->setupUi(this);
41 setupConnections();
42
43 QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
44 if (btn) {
45 btn->setText(tr("Find Next"));
46 btn->setDisabled(true);
47 }
48 }
49
50 /**
51 * Destroys the object and frees any allocated resources
52 */
53 DlgParameterFind::~DlgParameterFind()
54 {
55 // no need to delete child widgets, Qt does it all for us
56 delete ui;
57 }
58
59 void DlgParameterFind::setupConnections()
60 {
61 // clang-format off
62 connect(ui->lineEdit, &QLineEdit::textChanged,
63 this, &DlgParameterFind::onLineEditTextChanged);
64 connect(ui->checkGroups, &QCheckBox::toggled,
65 this, &DlgParameterFind::onCheckGroupsToggled);
66 connect(ui->checkNames, &QCheckBox::toggled,
67 this, &DlgParameterFind::onCheckNamesToggled);
68 connect(ui->checkValues, &QCheckBox::toggled,
69 this, &DlgParameterFind::onCheckValuesToggled);
70 // clang-format on
71 }
72
73 void DlgParameterFind::onLineEditTextChanged(const QString& text)
74 {
75 QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
76 if (btn) {
77 bool ok = ui->checkGroups->isChecked() || ui->checkNames->isChecked()
78 || ui->checkValues->isChecked();
79 btn->setDisabled(!ok || text.isEmpty());
80 }
81 }
82
83 void DlgParameterFind::onCheckGroupsToggled(bool)
84 {
85 QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
86 if (btn) {
87 bool ok = ui->checkGroups->isChecked() || ui->checkNames->isChecked()
88 || ui->checkValues->isChecked();
89 QString text = ui->lineEdit->text();
90 btn->setDisabled(!ok || text.isEmpty());
91 }
92 }
93
94 void DlgParameterFind::onCheckNamesToggled(bool)
95 {
96 QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
97 if (btn) {
98 bool ok = ui->checkGroups->isChecked() || ui->checkNames->isChecked()
99 || ui->checkValues->isChecked();
100 QString text = ui->lineEdit->text();
101 btn->setDisabled(!ok || text.isEmpty());
102 }
103 }
104
105 void DlgParameterFind::onCheckValuesToggled(bool)
106 {
107 QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
108 if (btn) {
109 bool ok = ui->checkGroups->isChecked() || ui->checkNames->isChecked()
110 || ui->checkValues->isChecked();
111 QString text = ui->lineEdit->text();
112 btn->setDisabled(!ok || text.isEmpty());
113 }
114 }
115
116 bool DlgParameterFind::matches(QTreeWidgetItem* item, const Options& opt) const
117 {
118 // check the group name
119 if (opt.group) {
120 // whole word matches
121 if (opt.match) {
122 if (item->text(0).compare(opt.text, Qt::CaseInsensitive) == 0) {
123 return true;
124 }
125 }
126 else {
127 if (item->text(0).indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0) {
128 return true;
129 }
130 }
131 }
132
133 if (opt.name || opt.value) {
134 auto group = static_cast<ParameterGroupItem*>(item);
135 Base::Reference<ParameterGrp> hGrp = group->_hcGrp;
136
137 auto boolMap = hGrp->GetBoolMap();
138 auto intMap = hGrp->GetIntMap();
139 auto uintMap = hGrp->GetUnsignedMap();
140 auto floatMap = hGrp->GetFloatMap();
141 auto asciiMap = hGrp->GetASCIIMap();
142
143 // check the name of an entry in the group
144 if (opt.name) {
145 if (opt.match) {
146 for (const auto& it : boolMap) {
147 QString text = QString::fromUtf8(it.first.c_str());
148 if (text.compare(opt.text, Qt::CaseInsensitive) == 0) {
149 return true;
150 }
151 }
152 for (const auto& it : intMap) {
153 QString text = QString::fromUtf8(it.first.c_str());
154 if (text.compare(opt.text, Qt::CaseInsensitive) == 0) {
155 return true;
156 }
157 }
158 for (const auto& it : uintMap) {
159 QString text = QString::fromUtf8(it.first.c_str());
160 if (text.compare(opt.text, Qt::CaseInsensitive) == 0) {
161 return true;
162 }
163 }
164 for (const auto& it : floatMap) {
165 QString text = QString::fromUtf8(it.first.c_str());
166 if (text.compare(opt.text, Qt::CaseInsensitive) == 0) {
167 return true;
168 }
169 }
170 for (const auto& it : asciiMap) {
171 QString text = QString::fromUtf8(it.first.c_str());
172 if (text.compare(opt.text, Qt::CaseInsensitive) == 0) {
173 return true;
174 }
175 }
176 }
177 else {
178 for (const auto& it : boolMap) {
179 QString text = QString::fromUtf8(it.first.c_str());
180 if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0) {
181 return true;
182 }
183 }
184 for (const auto& it : intMap) {
185 QString text = QString::fromUtf8(it.first.c_str());
186 if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0) {
187 return true;
188 }
189 }
190 for (const auto& it : uintMap) {
191 QString text = QString::fromUtf8(it.first.c_str());
192 if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0) {
193 return true;
194 }
195 }
196 for (const auto& it : floatMap) {
197 QString text = QString::fromUtf8(it.first.c_str());
198 if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0) {
199 return true;
200 }
201 }
202 for (const auto& it : asciiMap) {
203 QString text = QString::fromUtf8(it.first.c_str());
204 if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0) {
205 return true;
206 }
207 }
208 }
209 }
210
211 // check the value of an entry in the group
212 if (opt.value) {
213 if (opt.match) {
214 for (const auto& it : asciiMap) {
215 QString text = QString::fromUtf8(it.second.c_str());
216 if (text.compare(opt.text, Qt::CaseInsensitive) == 0) {
217 return true;
218 }
219 }
220 }
221 else {
222 for (const auto& it : asciiMap) {
223 QString text = QString::fromUtf8(it.second.c_str());
224 if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0) {
225 return true;
226 }
227 }
228 }
229 }
230 }
231
232 return false;
233 }
234
235 QTreeWidgetItem* DlgParameterFind::findItem(QTreeWidgetItem* root, const Options& opt) const
236 {
237 if (!root) {
238 return nullptr;
239 }
240
241 if (matches(root, opt)) {
242 // if the root matches then only return if it's not the current element
243 // as otherwise it would never move forward
244 if (root->treeWidget()->currentItem() != root) {
245 return root;
246 }
247 }
248
249 for (int i = 0; i < root->childCount(); i++) {
250 QTreeWidgetItem* item = root->child(i);
251 if (matches(item, opt)) {
252 return item;
253 }
254 item = findItem(item, opt);
255 if (item) {
256 return item;
257 }
258 }
259
260 return nullptr;
261 }
262
263 void DlgParameterFind::accept()
264 {
265 auto groupTree = dialog->findChild<ParameterGroup*>();
266 if (groupTree) {
267 Options opt;
268 opt.text = ui->lineEdit->text();
269 opt.group = ui->checkGroups->isChecked();
270 opt.name = ui->checkNames->isChecked();
271 opt.value = ui->checkValues->isChecked();
272 opt.match = ui->checkMatch->isChecked();
273
274 QTreeWidgetItem* current = groupTree->currentItem();
275 QTreeWidgetItem* next = findItem(current, opt);
276 while (!next && current) {
277 // go to the parent item and try again for each sibling after the current item
278 QTreeWidgetItem* parent = current->parent();
279 if (!parent) {
280 // switch from one top-level group to the next
281 QTreeWidgetItem* root = groupTree->invisibleRootItem();
282 if (root->indexOfChild(current) >= 0) {
283 parent = root;
284 }
285 }
286 if (parent) {
287 int index = parent->indexOfChild(current);
288 for (int i = index + 1; i < parent->childCount(); i++) {
289 next = findItem(parent->child(i), opt);
290 if (next) {
291 break;
292 }
293 }
294 }
295
296 if (!next) {
297 current = parent;
298 }
299 }
300
301 // if search was successful then make it the current item
302 if (next) {
303 groupTree->setCurrentItem(next);
304 }
305 else {
306 QMessageBox::warning(this, tr("Not found"), tr("Cannot find the text: %1").arg(opt.text));
307 }
308 }
309 }
310
311 void DlgParameterFind::reject()
312 {
313 QDialog::reject();
314 }
315
316 #include "moc_DlgParameterFind.cpp"