1
/***************************************************************************
2
* Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
# include <QMessageBox>
26
# include <QPushButton>
29
#include "DlgParameterFind.h"
30
#include "ui_DlgParameterFind.h"
31
#include "DlgParameterImp.h"
34
using namespace Gui::Dialog;
36
/* TRANSLATOR Gui::Dialog::DlgParameterFind */
38
DlgParameterFind::DlgParameterFind(DlgParameterImp* parent)
40
, ui(new Ui_DlgParameterFind)
46
QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
48
btn->setText(tr("Find Next"));
49
btn->setDisabled(true);
54
* Destroys the object and frees any allocated resources
56
DlgParameterFind::~DlgParameterFind()
58
// no need to delete child widgets, Qt does it all for us
62
void DlgParameterFind::setupConnections()
64
connect(ui->lineEdit, &QLineEdit::textChanged,
65
this, &DlgParameterFind::onLineEditTextChanged);
66
connect(ui->checkGroups, &QCheckBox::toggled,
67
this, &DlgParameterFind::onCheckGroupsToggled);
68
connect(ui->checkNames, &QCheckBox::toggled,
69
this, &DlgParameterFind::onCheckNamesToggled);
70
connect(ui->checkValues, &QCheckBox::toggled,
71
this, &DlgParameterFind::onCheckValuesToggled);
74
void DlgParameterFind::onLineEditTextChanged(const QString& text)
76
QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
78
bool ok = ui->checkGroups->isChecked() ||
79
ui->checkNames->isChecked() ||
80
ui->checkValues->isChecked();
81
btn->setDisabled(!ok || text.isEmpty());
85
void DlgParameterFind::onCheckGroupsToggled(bool)
87
QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
89
bool ok = ui->checkGroups->isChecked() ||
90
ui->checkNames->isChecked() ||
91
ui->checkValues->isChecked();
92
QString text = ui->lineEdit->text();
93
btn->setDisabled(!ok || text.isEmpty());
97
void DlgParameterFind::onCheckNamesToggled(bool)
99
QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
101
bool ok = ui->checkGroups->isChecked() ||
102
ui->checkNames->isChecked() ||
103
ui->checkValues->isChecked();
104
QString text = ui->lineEdit->text();
105
btn->setDisabled(!ok || text.isEmpty());
109
void DlgParameterFind::onCheckValuesToggled(bool)
111
QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
113
bool ok = ui->checkGroups->isChecked() ||
114
ui->checkNames->isChecked() ||
115
ui->checkValues->isChecked();
116
QString text = ui->lineEdit->text();
117
btn->setDisabled(!ok || text.isEmpty());
121
bool DlgParameterFind::matches(QTreeWidgetItem* item, const Options& opt) const
123
// check the group name
125
// whole word matches
127
if (item->text(0).compare(opt.text, Qt::CaseInsensitive) == 0)
131
if (item->text(0).indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
136
if (opt.name || opt.value) {
137
auto group = static_cast<ParameterGroupItem*>(item);
138
Base::Reference<ParameterGrp> hGrp = group->_hcGrp;
140
auto boolMap = hGrp->GetBoolMap();
141
auto intMap = hGrp->GetIntMap();
142
auto uintMap = hGrp->GetUnsignedMap();
143
auto floatMap = hGrp->GetFloatMap();
144
auto asciiMap = hGrp->GetASCIIMap();
146
// check the name of an entry in the group
149
for (const auto& it : boolMap) {
150
QString text = QString::fromUtf8(it.first.c_str());
151
if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
154
for (const auto& it : intMap) {
155
QString text = QString::fromUtf8(it.first.c_str());
156
if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
159
for (const auto& it : uintMap) {
160
QString text = QString::fromUtf8(it.first.c_str());
161
if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
164
for (const auto& it : floatMap) {
165
QString text = QString::fromUtf8(it.first.c_str());
166
if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
169
for (const auto& it : asciiMap) {
170
QString text = QString::fromUtf8(it.first.c_str());
171
if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
176
for (const auto& it : boolMap) {
177
QString text = QString::fromUtf8(it.first.c_str());
178
if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
181
for (const auto& it : intMap) {
182
QString text = QString::fromUtf8(it.first.c_str());
183
if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
186
for (const auto& it : uintMap) {
187
QString text = QString::fromUtf8(it.first.c_str());
188
if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
191
for (const auto& it : floatMap) {
192
QString text = QString::fromUtf8(it.first.c_str());
193
if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
196
for (const auto& it : asciiMap) {
197
QString text = QString::fromUtf8(it.first.c_str());
198
if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
204
// check the value of an entry in the group
207
for (const auto& it : asciiMap) {
208
QString text = QString::fromUtf8(it.second.c_str());
209
if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
214
for (const auto& it : asciiMap) {
215
QString text = QString::fromUtf8(it.second.c_str());
216
if (text.indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
226
QTreeWidgetItem* DlgParameterFind::findItem(QTreeWidgetItem* root, const Options& opt) const
231
if (matches(root, opt)) {
232
// if the root matches then only return if it's not the current element
233
// as otherwise it would never move forward
234
if (root->treeWidget()->currentItem() != root)
238
for (int i=0; i<root->childCount(); i++) {
239
QTreeWidgetItem* item = root->child(i);
240
if (matches(item, opt))
242
item = findItem(item, opt);
250
void DlgParameterFind::accept()
252
auto groupTree = dialog->findChild<ParameterGroup*>();
255
opt.text = ui->lineEdit->text();
256
opt.group = ui->checkGroups->isChecked();
257
opt.name = ui->checkNames->isChecked();
258
opt.value = ui->checkValues->isChecked();
259
opt.match = ui->checkMatch->isChecked();
261
QTreeWidgetItem* current = groupTree->currentItem();
262
QTreeWidgetItem* next = findItem(current, opt);
263
while (!next && current) {
264
// go to the parent item and try again for each sibling after the current item
265
QTreeWidgetItem* parent = current->parent();
267
// switch from one top-level group to the next
268
QTreeWidgetItem* root = groupTree->invisibleRootItem();
269
if (root->indexOfChild(current) >= 0) {
274
int index = parent->indexOfChild(current);
275
for (int i=index+1; i<parent->childCount(); i++) {
276
next = findItem(parent->child(i), opt);
287
// if search was successful then make it the current item
289
groupTree->setCurrentItem(next);
291
QMessageBox::warning(this, tr("Not found"), tr("Can't find the text: %1").arg(opt.text));
295
void DlgParameterFind::reject()
300
#include "moc_DlgParameterFind.cpp"