FreeCAD

Форк
0
/
DlgParameterFind.cpp 
300 строк · 10.7 Кб
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 "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <QMessageBox>
26
# include <QPushButton>
27
#endif
28

29
#include "DlgParameterFind.h"
30
#include "ui_DlgParameterFind.h"
31
#include "DlgParameterImp.h"
32

33

34
using namespace Gui::Dialog;
35

36
/* TRANSLATOR Gui::Dialog::DlgParameterFind */
37

38
DlgParameterFind::DlgParameterFind(DlgParameterImp* parent)
39
  : QDialog(parent)
40
  , ui(new Ui_DlgParameterFind)
41
  , dialog(parent)
42
{
43
    ui->setupUi(this);
44
    setupConnections();
45

46
    QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
47
    if (btn) {
48
        btn->setText(tr("Find Next"));
49
        btn->setDisabled(true);
50
    }
51
}
52

53
/**
54
 *  Destroys the object and frees any allocated resources
55
 */
56
DlgParameterFind::~DlgParameterFind()
57
{
58
    // no need to delete child widgets, Qt does it all for us
59
    delete ui;
60
}
61

62
void DlgParameterFind::setupConnections()
63
{
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);
72
}
73

74
void DlgParameterFind::onLineEditTextChanged(const QString& text)
75
{
76
    QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
77
    if (btn) {
78
        bool ok = ui->checkGroups->isChecked() ||
79
                  ui->checkNames->isChecked() ||
80
                  ui->checkValues->isChecked();
81
        btn->setDisabled(!ok || text.isEmpty());
82
    }
83
}
84

85
void DlgParameterFind::onCheckGroupsToggled(bool)
86
{
87
    QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
88
    if (btn) {
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());
94
    }
95
}
96

97
void DlgParameterFind::onCheckNamesToggled(bool)
98
{
99
    QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
100
    if (btn) {
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());
106
    }
107
}
108

109
void DlgParameterFind::onCheckValuesToggled(bool)
110
{
111
    QPushButton* btn = ui->buttonBox->button(QDialogButtonBox::Ok);
112
    if (btn) {
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());
118
    }
119
}
120

121
bool DlgParameterFind::matches(QTreeWidgetItem* item, const Options& opt) const
122
{
123
    // check the group name
124
    if (opt.group) {
125
        // whole word matches
126
        if (opt.match) {
127
            if (item->text(0).compare(opt.text, Qt::CaseInsensitive) == 0)
128
                return true;
129
        }
130
        else {
131
            if (item->text(0).indexOf(opt.text, 0, Qt::CaseInsensitive) >= 0)
132
                return true;
133
        }
134
    }
135

136
    if (opt.name || opt.value) {
137
        auto group = static_cast<ParameterGroupItem*>(item);
138
        Base::Reference<ParameterGrp> hGrp = group->_hcGrp;
139

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();
145

146
        // check the name of an entry in the group
147
        if (opt.name) {
148
            if (opt.match) {
149
                for (const auto& it : boolMap) {
150
                    QString text = QString::fromUtf8(it.first.c_str());
151
                    if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
152
                        return true;
153
                }
154
                for (const auto& it : intMap) {
155
                    QString text = QString::fromUtf8(it.first.c_str());
156
                    if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
157
                        return true;
158
                }
159
                for (const auto& it : uintMap) {
160
                    QString text = QString::fromUtf8(it.first.c_str());
161
                    if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
162
                        return true;
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
                for (const auto& it : asciiMap) {
170
                    QString text = QString::fromUtf8(it.first.c_str());
171
                    if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
172
                        return true;
173
                }
174
            }
175
            else {
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)
179
                        return true;
180
                }
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)
184
                        return true;
185
                }
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)
189
                        return true;
190
                }
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)
194
                        return true;
195
                }
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)
199
                        return true;
200
                }
201
            }
202
        }
203

204
        // check the value of an entry in the group
205
        if (opt.value) {
206
            if (opt.match) {
207
                for (const auto& it : asciiMap) {
208
                    QString text = QString::fromUtf8(it.second.c_str());
209
                    if (text.compare(opt.text, Qt::CaseInsensitive) == 0)
210
                        return true;
211
                }
212
            }
213
            else {
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)
217
                        return true;
218
                }
219
            }
220
        }
221
    }
222

223
    return false;
224
}
225

226
QTreeWidgetItem* DlgParameterFind::findItem(QTreeWidgetItem* root, const Options& opt) const
227
{
228
    if (!root)
229
        return nullptr;
230

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)
235
            return root;
236
    }
237

238
    for (int i=0; i<root->childCount(); i++) {
239
        QTreeWidgetItem* item = root->child(i);
240
        if (matches(item, opt))
241
            return item;
242
        item = findItem(item, opt);
243
        if (item)
244
            return item;
245
    }
246

247
    return nullptr;
248
}
249

250
void DlgParameterFind::accept()
251
{
252
    auto groupTree = dialog->findChild<ParameterGroup*>();
253
    if (groupTree) {
254
        Options opt;
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();
260

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();
266
            if (!parent) {
267
                // switch from one top-level group to the next
268
                QTreeWidgetItem* root = groupTree->invisibleRootItem();
269
                if (root->indexOfChild(current) >= 0) {
270
                    parent = root;
271
                }
272
            }
273
            if (parent) {
274
                int index = parent->indexOfChild(current);
275
                for (int i=index+1; i<parent->childCount(); i++) {
276
                    next = findItem(parent->child(i), opt);
277
                    if (next)
278
                        break;
279
                }
280
            }
281

282
            if (!next) {
283
                current = parent;
284
            }
285
        }
286

287
        // if search was successful then make it the current item
288
        if (next)
289
            groupTree->setCurrentItem(next);
290
        else
291
            QMessageBox::warning(this, tr("Not found"), tr("Can't find the text: %1").arg(opt.text));
292
    }
293
}
294

295
void DlgParameterFind::reject()
296
{
297
    QDialog::reject();
298
}
299

300
#include "moc_DlgParameterFind.cpp"
301

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.