Debian Codegraph @

src/Gui/TaskElementColors.cpp

raw
1 /****************************************************************************
2 * Copyright (c) 2018 Zheng Lei (realthunder) <[email protected]> *
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
24 #include <boost/algorithm/string/predicate.hpp>
25 #include <QColorDialog>
26 #include <sstream>
27
28
29 #include <App/ElementNamingUtils.h>
30 #include <App/Document.h>
31
32 #include "TaskElementColors.h"
33 #include "ui_TaskElementColors.h"
34 #include "Application.h"
35 #include "BitmapFactory.h"
36 #include "Command.h"
37 #include "Control.h"
38 #include "Document.h"
39 #include "FileDialog.h"
40 #include "Selection.h"
41 #include "SelectionObject.h"
42 #include "ViewProviderLink.h"
43
44
45 FC_LOG_LEVEL_INIT("Gui", true, true)
46
47 using namespace Gui;
48 namespace sp = std::placeholders;
49
50 class ElementColors::Private: public Gui::SelectionGate
51 {
52 public:
53 using Connection = boost::signals2::connection;
54 std::unique_ptr<Ui_TaskElementColors> ui;
55 ViewProviderDocumentObject* vp;
56 ViewProviderDocumentObject* vpParent;
57 Document* vpDoc;
58 std::map<std::string, QListWidgetItem*> elements;
59 std::vector<QListWidgetItem*> items;
60 std::string hiddenSub;
61 Connection connectDelDoc;
62 Connection connectDelObj;
63 QPixmap px;
64 bool busy;
65 long onTopMode;
66 bool touched;
67
68 std::string editDoc;
69 std::string editObj;
70 std::string editSub;
71 std::string editElement;
72
73 explicit Private(ViewProviderDocumentObject* vp, const char* element = "")
74 : ui(new Ui_TaskElementColors())
75 , vp(vp)
76 , vpParent(vp)
77 , vpDoc(vp->getDocument())
78 , editElement(element)
79 {
80 auto doc = Application::Instance->editDocument();
81 if (doc) {
82 auto editVp = doc->getInEdit(&vpParent, &editSub);
83 if (editVp == vp) {
84 auto obj = vpParent->getObject();
85 editDoc = obj->getDocument()->getName();
86 editObj = obj->getNameInDocument();
87 editSub = Data::noElementName(editSub.c_str());
88 }
89 }
90 if (editDoc.empty()) {
91 vpParent = vp;
92 editDoc = vp->getObject()->getDocument()->getName();
93 editObj = vp->getObject()->getNameInDocument();
94 editSub.clear();
95 }
96 onTopMode = vpParent->OnTopWhenSelected.getValue();
97 busy = false;
98 touched = false;
99 int w = QApplication::style()->standardPixmap(QStyle::SP_DirClosedIcon).width();
100 px = QPixmap(w, w);
101 }
102
103 ~Private() override
104 {
105 try {
106 vpParent->OnTopWhenSelected.setValue(onTopMode);
107 }
108 catch (const Base::Exception& e) {
109 e.reportException();
110 }
111 }
112
113 bool allow(App::Document* doc, App::DocumentObject* obj, const char* subname) override
114 {
115 if (editDoc != doc->getName() || editObj != obj->getNameInDocument()
116 || !boost::starts_with(subname, editSub)) {
117 return false;
118 }
119 if (editElement.empty()) {
120 return true;
121 }
122 const char* dot = strrchr(subname, '.');
123 if (!dot) {
124 dot = subname;
125 }
126 else {
127 ++dot;
128 }
129 return *dot == 0 || boost::starts_with(dot, editElement);
130 }
131
132 void populate()
133 {
134 int i = 0;
135 for (auto& v : vp->getElementColors()) {
136 addItem(i++, v.first.c_str());
137 }
138 apply();
139 }
140
141 void addItem(int i, const char* sub, bool push = false)
142 {
143 auto itE = elements.find(sub);
144 if (i < 0 && itE != elements.end()) {
145 if (push && !ViewProvider::hasHiddenMarker(sub)) {
146 items.push_back(itE->second);
147 }
148 return;
149 }
150
151 const char* marker = ViewProvider::hasHiddenMarker(sub);
152 if (marker) {
153 auto icon = BitmapFactory().pixmap("Invisible");
154 auto item = new QListWidgetItem(
155 icon,
156 QString::fromLatin1(std::string(sub, marker - sub).c_str()),
157 ui->elementList
158 );
159 item->setData(Qt::UserRole, QColor());
160 item->setData(Qt::UserRole + 1, QString::fromLatin1(sub));
161 elements.emplace(sub, item);
162 return;
163 }
164
165 for (auto& v : vp->getElementColors(sub)) {
166 auto it = elements.find(v.first);
167 if (it != elements.end()) {
168 if (push) {
169 items.push_back(it->second);
170 }
171 continue;
172 }
173 auto color = v.second;
174 QColor c;
175 c.setRgbF(color.r, color.g, color.b, color.a);
176 px.fill(c);
177 auto item = new QListWidgetItem(
178 QIcon(px),
179 QString::fromLatin1(Data::oldElementName(v.first.c_str()).c_str()),
180 ui->elementList
181 );
182 item->setData(Qt::UserRole, c);
183 item->setData(Qt::UserRole + 1, QString::fromLatin1(v.first.c_str()));
184 if (push) {
185 items.push_back(item);
186 }
187 elements.emplace(v.first, item);
188 }
189 }
190
191 void apply()
192 {
193 std::map<std::string, Base::Color> info;
194 int count = ui->elementList->count();
195 for (int i = 0; i < count; ++i) {
196 auto item = ui->elementList->item(i);
197 auto col = item->data(Qt::UserRole).value<QColor>();
198 std::string sub = qPrintable(item->data(Qt::UserRole + 1).value<QString>());
199 info.emplace(sub, Base::Color::fromValue<QColor>(col));
200 }
201 if (!App::GetApplication().getActiveTransaction()) {
202 App::GetApplication().setActiveTransaction("Set colors");
203 }
204 vp->setElementColors(info);
205 touched = true;
206 Selection().clearSelection();
207 }
208
209 void reset()
210 {
211 touched = false;
212 App::GetApplication().closeActiveTransaction(true);
213 Selection().clearSelection();
214 }
215
216 void accept()
217 {
218 if (touched && ui->recompute->isChecked()) {
219 auto obj = vp->getObject();
220 obj->touch();
221 obj->getDocument()->recompute(obj->getInListRecursive());
222 touched = false;
223 }
224 App::GetApplication().closeActiveTransaction();
225 }
226
227 void removeAll()
228 {
229 if (!elements.empty()) {
230 hiddenSub.clear();
231 ui->elementList->clear();
232 elements.clear();
233 apply();
234 }
235 }
236
237 void removeItems()
238 {
239 const auto items = ui->elementList->selectedItems();
240 for (auto item : items) {
241 std::string sub = qPrintable(item->data(Qt::UserRole + 1).value<QString>());
242 if (sub == hiddenSub) {
243 hiddenSub.clear();
244 }
245 elements.erase(sub);
246 delete item;
247 }
248 apply();
249 }
250
251 void editItem(QWidget* parent, QListWidgetItem* item)
252 {
253 std::string sub = qPrintable(item->data(Qt::UserRole + 1).value<QString>());
254 if (ViewProvider::hasHiddenMarker(sub.c_str())) {
255 return;
256 }
257 auto color = item->data(Qt::UserRole).value<QColor>();
258 QColorDialog cd(color, parent);
259 cd.setOption(QColorDialog::ShowAlphaChannel);
260 if (DialogOptions::dontUseNativeColorDialog()) {
261 cd.setOption(QColorDialog::DontUseNativeDialog);
262 }
263 if (cd.exec() != QDialog::Accepted || color == cd.selectedColor()) {
264 return;
265 }
266 color = cd.selectedColor();
267 item->setData(Qt::UserRole, color);
268 px.fill(color);
269 item->setIcon(QIcon(px));
270 apply();
271 }
272
273 void onSelectionChanged(const SelectionChanges& msg)
274 {
275 // no object selected in the combobox or no sub-element was selected
276 if (busy) {
277 return;
278 }
279 busy = true;
280 switch (msg.Type) {
281 case SelectionChanges::ClrSelection:
282 ui->elementList->clearSelection();
283 break;
284 case SelectionChanges::AddSelection:
285 case SelectionChanges::RmvSelection:
286 if (msg.pDocName && msg.pObjectName && msg.pSubName && msg.pSubName[0]) {
287 if (editDoc == msg.pDocName && editObj == msg.pObjectName
288 && boost::starts_with(msg.pSubName, editSub)) {
289 const auto items = ui->elementList->findItems(
290 QString::fromLatin1(msg.pSubName - editSub.size()),
291 Qt::MatchExactly
292 );
293 for (auto item : items) {
294 item->setSelected(msg.Type == SelectionChanges::AddSelection);
295 }
296 }
297 }
298 default:
299 break;
300 }
301 busy = false;
302 }
303
304 void onSelectionChanged()
305 {
306 if (busy) {
307 return;
308 }
309 busy = true;
310 std::map<std::string, int> sels;
311 for (auto& sel : Selection().getSelectionEx(
312 editDoc.c_str(),
313 App::DocumentObject::getClassTypeId(),
314 ResolveMode::NoResolve
315 )) {
316 if (sel.getFeatName() != editObj) {
317 continue;
318 }
319 for (auto& sub : sel.getSubNames()) {
320 if (boost::starts_with(sub, editSub)) {
321 sels[sub.c_str() + editSub.size()] = 1;
322 }
323 }
324 break;
325 }
326 const auto items = ui->elementList->selectedItems();
327 for (auto item : items) {
328 std::string name(qPrintable(item->data(Qt::UserRole + 1).value<QString>()));
329 if (ViewProvider::hasHiddenMarker(name.c_str())) {
330 continue;
331 }
332 auto& v = sels[name];
333 if (!v) {
334 Selection().addSelection(editDoc.c_str(), editObj.c_str(), (editSub + name).c_str());
335 }
336 v = 2;
337 }
338 for (auto& v : sels) {
339 if (v.second != 2) {
340 Selection().rmvSelection(editDoc.c_str(), editObj.c_str(), (editSub + v.first).c_str());
341 }
342 }
343 busy = false;
344 }
345 };
346
347 /* TRANSLATOR Gui::TaskElementColors */
348
349 ElementColors::ElementColors(ViewProviderDocumentObject* vp, bool noHide)
350 : d(new Private(vp))
351 {
352 d->ui->setupUi(this);
353 setupConnections();
354
355 d->ui->objectLabel->setText(QString::fromUtf8(vp->getObject()->Label.getValue()));
356 d->ui->elementList->setMouseTracking(true); // needed for itemEntered() to work
357
358 if (noHide) {
359 d->ui->hideSelection->setVisible(false);
360 }
361
362 ParameterGrp::handle hPart = App::GetApplication().GetParameterGroupByPath(
363 "User parameter:BaseApp/Preferences/View"
364 );
365 d->ui->recompute->setChecked(hPart->GetBool("ColorRecompute", true));
366 d->ui->onTop->setChecked(hPart->GetBool("ColorOnTop", true));
367 if (d->ui->onTop->isChecked()) {
368 d->vpParent->OnTopWhenSelected.setValue(3);
369 }
370
371 Selection().addSelectionGate(d, ResolveMode::NoResolve);
372
373 // NOLINTBEGIN
374 // clang-format off
375 d->connectDelDoc = Application::Instance->signalDeleteDocument.connect(std::bind
376 (&ElementColors::slotDeleteDocument, this, sp::_1));
377 d->connectDelObj = Application::Instance->signalDeletedObject.connect(std::bind
378 (&ElementColors::slotDeleteObject, this, sp::_1));
379 // clang-format on
380 // NOLINTEND
381
382 d->populate();
383 }
384
385 ElementColors::~ElementColors()
386 {
387 d->connectDelDoc.disconnect();
388 d->connectDelObj.disconnect();
389 Selection().rmvSelectionGate();
390 }
391
392 void ElementColors::setupConnections()
393 {
394 // clang-format off
395 connect(d->ui->removeSelection, &QPushButton::clicked,
396 this, &ElementColors::onRemoveSelectionClicked);
397 connect(d->ui->addSelection, &QPushButton::clicked,
398 this, &ElementColors::onAddSelectionClicked);
399 connect(d->ui->removeAll, &QPushButton::clicked,
400 this, &ElementColors::onRemoveAllClicked);
401 connect(d->ui->elementList, &QListWidget::itemDoubleClicked,
402 this, &ElementColors::onElementListItemDoubleClicked);
403 connect(d->ui->elementList, &QListWidget::itemSelectionChanged,
404 this, &ElementColors::onElementListItemSelectionChanged);
405 connect(d->ui->elementList, &QListWidget::itemEntered,
406 this, &ElementColors::onElementListItemEntered);
407 connect(d->ui->recompute, &QCheckBox::clicked,
408 this, &ElementColors::onRecomputeClicked);
409 connect(d->ui->onTop, &QCheckBox::clicked,
410 this, &ElementColors::onTopClicked);
411 connect(d->ui->hideSelection, &QPushButton::clicked,
412 this, &ElementColors::onHideSelectionClicked);
413 connect(d->ui->boxSelect, &QPushButton::clicked,
414 this, &ElementColors::onBoxSelectClicked);
415 // clang-format on
416 }
417
418 void ElementColors::onRecomputeClicked(bool checked)
419 {
420 ParameterGrp::handle hPart = App::GetApplication().GetParameterGroupByPath(
421 "User parameter:BaseApp/Preferences/View"
422 );
423 hPart->SetBool("ColorRecompute", checked);
424 }
425
426 void ElementColors::onTopClicked(bool checked)
427 {
428 ParameterGrp::handle hPart = App::GetApplication().GetParameterGroupByPath(
429 "User parameter:BaseApp/Preferences/View"
430 );
431 hPart->SetBool("ColorOnTop", checked);
432 d->vpParent->OnTopWhenSelected.setValue(checked ? 3 : d->onTopMode);
433 }
434
435 void ElementColors::slotDeleteDocument(const Document& Doc)
436 {
437 if (d->vpDoc == &Doc || d->editDoc == Doc.getDocument()->getName()) {
438 Control().closeDialog();
439 }
440 }
441
442 void ElementColors::slotDeleteObject(const ViewProvider& obj)
443 {
444 if (d->vp == &obj) {
445 Control().closeDialog();
446 }
447 }
448
449 void ElementColors::onRemoveSelectionClicked()
450 {
451 d->removeItems();
452 }
453
454 void ElementColors::onBoxSelectClicked()
455 {
456 auto cmd = Application::Instance->commandManager().getCommandByName("Std_BoxElementSelection");
457 if (cmd) {
458 cmd->invoke(0);
459 }
460 }
461
462 void ElementColors::onHideSelectionClicked()
463 {
464 auto sels = Selection().getSelectionEx(
465 d->editDoc.c_str(),
466 App::DocumentObject::getClassTypeId(),
467 ResolveMode::NoResolve
468 );
469 for (auto& sel : sels) {
470 if (d->editObj != sel.getFeatName()) {
471 continue;
472 }
473 const auto& subs = sel.getSubNames();
474 if (!subs.empty()) {
475 for (auto& sub : subs) {
476 if (boost::starts_with(sub, d->editSub)) {
477 auto name = Data::noElementName(sub.c_str() + d->editSub.size());
478 name += ViewProvider::hiddenMarker();
479 d->addItem(-1, name.c_str());
480 }
481 }
482 d->apply();
483 }
484 return;
485 }
486 }
487
488 void ElementColors::onAddSelectionClicked()
489 {
490 auto sels = Selection().getSelectionEx(
491 d->editDoc.c_str(),
492 App::DocumentObject::getClassTypeId(),
493 ResolveMode::NoResolve
494 );
495 d->items.clear();
496 if (sels.empty()) {
497 d->addItem(-1, "Face", true);
498 }
499 else {
500 for (auto& sel : sels) {
501 if (d->editObj != sel.getFeatName()) {
502 continue;
503 }
504 const auto& subs = sel.getSubNames();
505 if (subs.empty()) {
506 d->addItem(-1, "Face", true);
507 break;
508 }
509 for (auto& sub : subs) {
510 if (boost::starts_with(sub, d->editSub)) {
511 d->addItem(-1, sub.c_str() + d->editSub.size(), true);
512 }
513 }
514 break;
515 }
516 }
517
518 if (!d->items.empty()) {
519 auto color = d->items.front()->data(Qt::UserRole).value<QColor>();
520 QColorDialog cd(color, this);
521 cd.setOption(QColorDialog::ShowAlphaChannel);
522 if (DialogOptions::dontUseNativeColorDialog()) {
523 cd.setOption(QColorDialog::DontUseNativeDialog);
524 }
525 if (cd.exec() != QDialog::Accepted) {
526 return;
527 }
528 color = cd.selectedColor();
529 for (auto item : d->items) {
530 item->setData(Qt::UserRole, color);
531 d->px.fill(color);
532 item->setIcon(QIcon(d->px));
533 }
534 d->apply();
535 }
536 }
537
538 void ElementColors::onRemoveAllClicked()
539 {
540 d->removeAll();
541 }
542
543 bool ElementColors::accept()
544 {
545 d->accept();
546 Application::Instance->setEditDocument(nullptr);
547 return true;
548 }
549
550 bool ElementColors::reject()
551 {
552 d->reset();
553 Application::Instance->setEditDocument(nullptr);
554 return true;
555 }
556
557 void ElementColors::changeEvent(QEvent* e)
558 {
559 QWidget::changeEvent(e);
560 if (e->type() == QEvent::LanguageChange) {
561 d->ui->retranslateUi(this);
562 }
563 }
564
565 void ElementColors::leaveEvent(QEvent* e)
566 {
567 QWidget::leaveEvent(e);
568 Selection().rmvPreselect();
569
570 if (!d->hiddenSub.empty()) {
571 d->vp->partialRender({d->hiddenSub}, false);
572 d->hiddenSub.clear();
573 }
574 }
575
576 void ElementColors::onElementListItemEntered(QListWidgetItem* item)
577 {
578 std::string name(qPrintable(item->data(Qt::UserRole + 1).value<QString>()));
579 if (!d->hiddenSub.empty()) {
580 d->vp->partialRender({d->hiddenSub}, false);
581 d->hiddenSub.clear();
582 }
583
584 if (ViewProvider::hasHiddenMarker(name.c_str())) {
585 d->hiddenSub = name;
586 d->vp->partialRender({name}, true);
587 name.resize(name.size() - ViewProvider::hiddenMarker().size());
588 }
589
590 Selection().setPreselect(
591 d->editDoc.c_str(),
592 d->editObj.c_str(),
593 (d->editSub + name).c_str(),
594 0,
595 0,
596 0,
597 d->ui->onTop->isChecked() ? Gui::SelectionChanges::MsgSource::TreeView
598 : Gui::SelectionChanges::MsgSource::Internal
599 );
600 }
601
602 void ElementColors::onElementListItemSelectionChanged()
603 {
604 d->onSelectionChanged();
605 }
606
607 void ElementColors::onSelectionChanged(const SelectionChanges& msg)
608 {
609 d->onSelectionChanged(msg);
610 }
611
612 void ElementColors::onElementListItemDoubleClicked(QListWidgetItem* item)
613 {
614 d->editItem(this, item);
615 }
616
617 /* TRANSLATOR Gui::TaskElementColors */
618
619 TaskElementColors::TaskElementColors(ViewProviderDocumentObject* vp, bool noHide)
620 {
621 widget = new ElementColors(vp, noHide);
622 addTaskBox(widget);
623 }
624
625 TaskElementColors::~TaskElementColors() = default;
626
627 void TaskElementColors::open()
628 {}
629
630 void TaskElementColors::clicked(int id)
631 {
632 Q_UNUSED(id)
633 }
634
635 bool TaskElementColors::accept()
636 {
637 return widget->accept();
638 }
639
640 bool TaskElementColors::reject()
641 {
642 return widget->reject();
643 }
644
645 #include "moc_TaskElementColors.cpp"