1
/***************************************************************************
2
* Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
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"
29
#include <QListWidgetItem>
36
#include <App/ComplexGeoData.h>
37
#include <App/Document.h>
38
#include <App/GeoFeature.h>
40
#include "SelectionView.h"
41
#include "Application.h"
42
#include "BitmapFactory.h"
47
FC_LOG_LEVEL_INIT("Selection", true, true, true)
50
using namespace Gui::DockWnd;
53
/* TRANSLATOR Gui::DockWnd::SelectionView */
55
SelectionView::SelectionView(Gui::Document* pcDocument, QWidget* parent)
56
: DockWindow(pcDocument, parent)
57
, SelectionObserver(true, ResolveMode::NoResolve)
61
, openedAutomatically(false)
63
setWindowTitle(tr("Selection View"));
65
QVBoxLayout* vLayout = new QVBoxLayout(this);
66
vLayout->setSpacing(0);
67
vLayout->setContentsMargins(0, 0, 0, 0);
69
QLineEdit* searchBox = new QLineEdit(this);
70
searchBox->setPlaceholderText(tr("Search"));
71
searchBox->setToolTip(tr("Searches object labels"));
72
QHBoxLayout* hLayout = new QHBoxLayout();
73
hLayout->setSpacing(2);
74
QToolButton* clearButton = new QToolButton(this);
75
clearButton->setFixedSize(18, 21);
76
clearButton->setCursor(Qt::ArrowCursor);
77
clearButton->setStyleSheet(QString::fromUtf8("QToolButton {margin-bottom:1px}"));
78
clearButton->setIcon(BitmapFactory().pixmap(":/icons/edit-cleartext.svg"));
79
clearButton->setToolTip(tr("Clears the search field"));
80
clearButton->setAutoRaise(true);
81
countLabel = new QLabel(this);
82
countLabel->setText(QString::fromUtf8("0"));
83
countLabel->setToolTip(tr("The number of selected items"));
84
hLayout->addWidget(searchBox);
85
hLayout->addWidget(clearButton, 0, Qt::AlignRight);
86
hLayout->addWidget(countLabel, 0, Qt::AlignRight);
87
vLayout->addLayout(hLayout);
89
selectionView = new QListWidget(this);
90
selectionView->setContextMenuPolicy(Qt::CustomContextMenu);
91
vLayout->addWidget(selectionView);
93
enablePickList = new QCheckBox(this);
94
enablePickList->setText(tr("Picked object list"));
95
vLayout->addWidget(enablePickList);
96
pickList = new QListWidget(this);
97
pickList->setVisible(false);
98
vLayout->addWidget(pickList);
100
selectionView->setMouseTracking(true); // needed for itemEntered() to work
101
pickList->setMouseTracking(true);
106
connect(clearButton, &QToolButton::clicked, searchBox, &QLineEdit::clear);
107
connect(searchBox, &QLineEdit::textChanged, this, &SelectionView::search);
108
connect(searchBox, &QLineEdit::editingFinished, this, &SelectionView::validateSearch);
109
connect(selectionView, &QListWidget::itemDoubleClicked, this, &SelectionView::toggleSelect);
110
connect(selectionView, &QListWidget::itemEntered, this, &SelectionView::preselect);
111
connect(pickList, &QListWidget::itemDoubleClicked, this, &SelectionView::toggleSelect);
112
connect(pickList, &QListWidget::itemEntered, this, &SelectionView::preselect);
113
connect(selectionView, &QListWidget::customContextMenuRequested, this, &SelectionView::onItemContextMenu);
114
connect(enablePickList, &QCheckBox::stateChanged, this, &SelectionView::onEnablePickList);
118
SelectionView::~SelectionView() = default;
120
void SelectionView::leaveEvent(QEvent*)
122
Selection().rmvPreselect();
126
void SelectionView::onSelectionChanged(const SelectionChanges& Reason)
128
ParameterGrp::handle hGrp = App::GetApplication()
131
->GetGroup("Preferences")
132
->GetGroup("Selection");
133
bool autoShow = hGrp->GetBool("AutoShowSelectionView", false);
134
hGrp->SetBool("AutoShowSelectionView",
135
autoShow); // Remove this line once the preferences window item is implemented
138
if (!parentWidget()->isVisible() && Selection().hasSelection()) {
139
parentWidget()->show();
140
openedAutomatically = true;
142
else if (openedAutomatically && !Selection().hasSelection()) {
143
parentWidget()->hide();
144
openedAutomatically = false;
149
QTextStream str(&selObject);
151
auto getSelectionName = [](QTextStream& str,
155
App::DocumentObject* obj) {
159
if (subName != 0 && subName[0] != 0) {
161
/* Original code doesn't take account of histories in subelement names and displays
162
* them inadvertently. Let's not do that.
165
/* Remove the history from the displayed subelement name */
166
App::ElementNamePair elementName;
167
App::GeoFeature::resolveElement(obj, subName, elementName);
168
str << elementName.oldName.c_str(); // Use the shortened element name not the full one.
169
/* Mark it visually if there was a history as a "tell" for if a given selection has TNP
171
if (elementName.newName.size() > 0) {
174
auto subObj = obj->getSubObject(subName);
180
str << QString::fromUtf8(obj->Label.getValue());
184
if (Reason.Type == SelectionChanges::AddSelection) {
187
list << QString::fromLatin1(Reason.pDocName);
188
list << QString::fromLatin1(Reason.pObjectName);
189
App::Document* doc = App::GetApplication().getDocument(Reason.pDocName);
190
App::DocumentObject* obj = doc->getObject(Reason.pObjectName);
191
getSelectionName(str, Reason.pDocName, Reason.pObjectName, Reason.pSubName, obj);
193
// insert the selection as item
194
QListWidgetItem* item = new QListWidgetItem(selObject, selectionView);
195
item->setData(Qt::UserRole, list);
197
else if (Reason.Type == SelectionChanges::ClrSelection) {
198
if (!Reason.pDocName[0]) {
200
selectionView->clear();
204
str << Reason.pDocName;
207
const auto items = selectionView->findItems(selObject, Qt::MatchStartsWith);
208
for (auto item : items) {
213
else if (Reason.Type == SelectionChanges::RmvSelection) {
214
App::Document* doc = App::GetApplication().getDocument(Reason.pDocName);
215
App::DocumentObject* obj = doc->getObject(Reason.pObjectName);
216
getSelectionName(str, Reason.pDocName, Reason.pObjectName, Reason.pSubName, obj);
218
QList<QListWidgetItem*> l = selectionView->findItems(selObject, Qt::MatchStartsWith);
223
else if (Reason.Type == SelectionChanges::SetSelection) {
225
selectionView->clear();
226
std::vector<SelectionSingleton::SelObj> objs =
227
Gui::Selection().getSelection(Reason.pDocName, ResolveMode::NoResolve);
228
for (const auto& it : objs) {
231
list << QString::fromLatin1(it.DocName);
232
list << QString::fromLatin1(it.FeatName);
234
App::Document* doc = App::GetApplication().getDocument(it.DocName);
235
App::DocumentObject* obj = doc->getObject(it.FeatName);
236
getSelectionName(str, it.DocName, it.FeatName, it.SubName, obj);
237
QListWidgetItem* item = new QListWidgetItem(selObject, selectionView);
238
item->setData(Qt::UserRole, list);
242
else if (Reason.Type == SelectionChanges::PickedListChanged) {
243
bool picking = Selection().needPickedList();
244
enablePickList->setChecked(picking);
245
pickList->setVisible(picking);
248
const auto& sels = Selection().getPickedList(Reason.pDocName);
249
for (const auto& sel : sels) {
250
App::Document* doc = App::GetApplication().getDocument(sel.DocName);
254
App::DocumentObject* obj = doc->getObject(sel.FeatName);
260
QTextStream str(&selObject);
261
getSelectionName(str, sel.DocName, sel.FeatName, sel.SubName, obj);
267
new QListWidgetItem(selObject, pickList);
272
countLabel->setText(QString::number(selectionView->count()));
275
void SelectionView::search(const QString& text)
277
if (!text.isEmpty()) {
279
App::Document* doc = App::GetApplication().getActiveDocument();
280
std::vector<App::DocumentObject*> objects;
282
objects = doc->getObjects();
283
selectionView->clear();
284
for (auto it : objects) {
285
QString label = QString::fromUtf8(it->Label.getValue());
286
if (label.contains(text, Qt::CaseInsensitive)) {
287
searchList.push_back(it);
290
QTextStream str(&selObject);
292
list << QString::fromLatin1(doc->getName());
293
list << QString::fromLatin1(it->getNameInDocument());
295
str << QString::fromUtf8(doc->Label.getValue());
297
str << it->getNameInDocument();
301
QListWidgetItem* item = new QListWidgetItem(selObject, selectionView);
302
item->setData(Qt::UserRole, list);
305
countLabel->setText(QString::number(selectionView->count()));
310
void SelectionView::validateSearch()
312
if (!searchList.empty()) {
313
App::Document* doc = App::GetApplication().getActiveDocument();
315
Gui::Selection().clearSelection();
316
for (auto it : searchList) {
317
Gui::Selection().addSelection(doc->getName(), it->getNameInDocument(), nullptr);
323
void SelectionView::select(QListWidgetItem* item)
326
item = selectionView->currentItem();
331
QStringList elements = item->data(Qt::UserRole).toStringList();
332
if (elements.size() < 2) {
337
// Gui::Selection().clearSelection();
338
Gui::Command::runCommand(Gui::Command::Gui, "Gui.Selection.clearSelection()");
339
// Gui::Selection().addSelection(elements[0].toLatin1(),elements[1].toLatin1(),0);
340
QString cmd = QString::fromLatin1(
341
R"(Gui.Selection.addSelection(App.getDocument("%1").getObject("%2")))")
342
.arg(elements[0], elements[1]);
343
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
345
catch (Base::Exception& e) {
350
void SelectionView::deselect()
352
QListWidgetItem* item = selectionView->currentItem();
356
QStringList elements = item->data(Qt::UserRole).toStringList();
357
if (elements.size() < 2) {
361
// Gui::Selection().rmvSelection(elements[0].toLatin1(),elements[1].toLatin1(),0);
362
QString cmd = QString::fromLatin1(
363
R"(Gui.Selection.removeSelection(App.getDocument("%1").getObject("%2")))")
364
.arg(elements[0], elements[1]);
366
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
368
catch (Base::Exception& e) {
373
void SelectionView::toggleSelect(QListWidgetItem* item)
378
std::string name = item->text().toLatin1().constData();
379
char* docname = &name.at(0);
380
char* objname = std::strchr(docname, '#');
385
char* subname = std::strchr(objname, '.');
388
char* end = std::strchr(subname, ' ');
394
char* end = std::strchr(objname, ' ');
400
if (Gui::Selection().isSelected(docname, objname, subname)) {
401
cmd = QString::fromLatin1("Gui.Selection.removeSelection("
402
"App.getDocument('%1').getObject('%2'),'%3')")
403
.arg(QString::fromLatin1(docname),
404
QString::fromLatin1(objname),
405
QString::fromLatin1(subname));
408
cmd = QString::fromLatin1("Gui.Selection.addSelection("
409
"App.getDocument('%1').getObject('%2'),'%3',%4,%5,%6)")
410
.arg(QString::fromLatin1(docname),
411
QString::fromLatin1(objname),
412
QString::fromLatin1(subname))
418
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
420
catch (Base::Exception& e) {
425
void SelectionView::preselect(QListWidgetItem* item)
430
std::string name = item->text().toLatin1().constData();
431
char* docname = &name.at(0);
432
char* objname = std::strchr(docname, '#');
437
char* subname = std::strchr(objname, '.');
440
char* end = std::strchr(subname, ' ');
446
char* end = std::strchr(objname, ' ');
451
QString cmd = QString::fromLatin1("Gui.Selection.setPreselection("
452
"App.getDocument('%1').getObject('%2'),'%3',tp=2)")
453
.arg(QString::fromLatin1(docname),
454
QString::fromLatin1(objname),
455
QString::fromLatin1(subname));
457
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
459
catch (Base::Exception& e) {
464
void SelectionView::zoom()
468
Gui::Command::runCommand(Gui::Command::Gui, "Gui.SendMsgToActiveView(\"ViewSelection\")");
470
catch (Base::Exception& e) {
475
void SelectionView::treeSelect()
479
Gui::Command::runCommand(Gui::Command::Gui, "Gui.runCommand(\"Std_TreeSelection\")");
481
catch (Base::Exception& e) {
486
void SelectionView::touch()
488
QListWidgetItem* item = selectionView->currentItem();
492
QStringList elements = item->data(Qt::UserRole).toStringList();
493
if (elements.size() < 2) {
496
QString cmd = QString::fromLatin1(R"(App.getDocument("%1").getObject("%2").touch())")
497
.arg(elements[0], elements[1]);
499
Gui::Command::runCommand(Gui::Command::Doc, cmd.toLatin1());
501
catch (Base::Exception& e) {
506
void SelectionView::toPython()
508
QListWidgetItem* item = selectionView->currentItem();
512
QStringList elements = item->data(Qt::UserRole).toStringList();
513
if (elements.size() < 2) {
518
QString cmd = QString::fromLatin1(R"(obj = App.getDocument("%1").getObject("%2"))")
519
.arg(elements[0], elements[1]);
520
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
521
if (elements.length() > 2) {
522
App::Document* doc = App::GetApplication().getDocument(elements[0].toLatin1());
523
App::DocumentObject* obj = doc->getObject(elements[1].toLatin1());
524
QString property = getProperty(obj);
526
cmd = QString::fromLatin1(R"(shp = App.getDocument("%1").getObject("%2").%3)")
527
.arg(elements[0], elements[1], property);
528
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
530
if (supportPart(obj, elements[2])) {
531
cmd = QString::fromLatin1(R"(elt = App.getDocument("%1").getObject("%2").%3.%4)")
532
.arg(elements[0], elements[1], property, elements[2]);
533
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
537
catch (const Base::Exception& e) {
542
void SelectionView::showPart()
544
QListWidgetItem* item = selectionView->currentItem();
548
QStringList elements = item->data(Qt::UserRole).toStringList();
549
if (elements.length() > 2) {
550
App::Document* doc = App::GetApplication().getDocument(elements[0].toLatin1());
551
App::DocumentObject* obj = doc->getObject(elements[1].toLatin1());
552
QString module = getModule(obj->getTypeId().getName());
553
QString property = getProperty(obj);
554
if (!module.isEmpty() && !property.isEmpty() && supportPart(obj, elements[2])) {
556
Gui::Command::addModule(Gui::Command::Gui, module.toLatin1());
558
QString::fromLatin1(R"(%1.show(App.getDocument("%2").getObject("%3").%4.%5))")
559
.arg(module, elements[0], elements[1], property, elements[2]);
560
Gui::Command::runCommand(Gui::Command::Gui, cmd.toLatin1());
562
catch (const Base::Exception& e) {
569
QString SelectionView::getModule(const char* type) const
571
// go up the inheritance tree and find the module name of the first
572
// sub-class that has not the prefix "App::"
574
Base::Type typeId = Base::Type::fromName(type);
576
while (!typeId.isBad()) {
577
std::string temp(typeId.getName());
578
std::string::size_type pos = temp.find_first_of("::");
581
if (pos != std::string::npos) {
582
module = std::string(temp, 0, pos);
584
if (module != "App") {
590
typeId = typeId.getParent();
593
return QString::fromStdString(prefix);
596
QString SelectionView::getProperty(App::DocumentObject* obj) const
599
if (obj->isDerivedFrom<App::GeoFeature>()) {
600
App::GeoFeature* geo = static_cast<App::GeoFeature*>(obj);
601
const App::PropertyComplexGeoData* data = geo->getPropertyOfGeometry();
602
const char* name = data ? data->getName() : nullptr;
603
if (App::Property::isValidName(name)) {
604
property = QString::fromLatin1(name);
611
bool SelectionView::supportPart(App::DocumentObject* obj, const QString& part) const
613
if (obj->isDerivedFrom<App::GeoFeature>()) {
614
App::GeoFeature* geo = static_cast<App::GeoFeature*>(obj);
615
const App::PropertyComplexGeoData* data = geo->getPropertyOfGeometry();
617
const Data::ComplexGeoData* geometry = data->getComplexData();
618
std::vector<const char*> types = geometry->getElementTypes();
619
for (auto it : types) {
620
if (part.startsWith(QString::fromLatin1(it))) {
630
void SelectionView::onItemContextMenu(const QPoint& point)
632
QListWidgetItem* item = selectionView->itemAt(point);
637
QAction* selectAction = menu.addAction(tr("Select only"), this, [&] {
638
this->select(nullptr);
640
selectAction->setIcon(QIcon::fromTheme(QString::fromLatin1("view-select")));
641
selectAction->setToolTip(tr("Selects only this object"));
643
QAction* deselectAction = menu.addAction(tr("Deselect"), this, &SelectionView::deselect);
644
deselectAction->setIcon(QIcon::fromTheme(QString::fromLatin1("view-unselectable")));
645
deselectAction->setToolTip(tr("Deselects this object"));
647
QAction* zoomAction = menu.addAction(tr("Zoom fit"), this, &SelectionView::zoom);
648
zoomAction->setIcon(QIcon::fromTheme(QString::fromLatin1("zoom-fit-best")));
649
zoomAction->setToolTip(tr("Selects and fits this object in the 3D window"));
651
QAction* gotoAction = menu.addAction(tr("Go to selection"), this, &SelectionView::treeSelect);
652
gotoAction->setToolTip(tr("Selects and locates this object in the tree view"));
654
QAction* touchAction = menu.addAction(tr("Mark to recompute"), this, &SelectionView::touch);
655
touchAction->setIcon(QIcon::fromTheme(QString::fromLatin1("view-refresh")));
656
touchAction->setToolTip(tr("Mark this object to be recomputed"));
658
QAction* toPythonAction =
659
menu.addAction(tr("To python console"), this, &SelectionView::toPython);
660
toPythonAction->setIcon(QIcon::fromTheme(QString::fromLatin1("applications-python")));
661
toPythonAction->setToolTip(
662
tr("Reveals this object and its subelements in the python console."));
664
QStringList elements = item->data(Qt::UserRole).toStringList();
665
if (elements.length() > 2) {
666
// subshape-specific entries
668
menu.addAction(tr("Duplicate subshape"), this, &SelectionView::showPart);
669
showPart->setIcon(QIcon(QString::fromLatin1(":/icons/ClassBrowser/member.svg")));
670
showPart->setToolTip(tr("Creates a standalone copy of this subshape in the document"));
672
menu.exec(selectionView->mapToGlobal(point));
675
void SelectionView::onUpdate()
678
bool SelectionView::onMsg(const char* /*pMsg*/, const char** /*ppReturn*/)
683
void SelectionView::hideEvent(QHideEvent* ev)
685
DockWindow::hideEvent(ev);
688
void SelectionView::showEvent(QShowEvent* ev)
690
enablePickList->setChecked(Selection().needPickedList());
691
Gui::DockWindow::showEvent(ev);
694
void SelectionView::onEnablePickList()
696
bool enabled = enablePickList->isChecked();
697
Selection().enablePickedList(enabled);
698
pickList->setVisible(enabled);
703
#include "moc_SelectionView.cpp"