Debian Codegraph @

src/Gui/TaskView/TaskSelectLinkProperty.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2010 Jürgen Riegel <[email protected]> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24
25 #include <App/Document.h>
26 #include <App/DocumentObject.h>
27 #include <Base/Console.h>
28 #include <Gui/BitmapFactory.h>
29 #include <Gui/Selection/SelectionFilter.h>
30 #include <Gui/Selection/SelectionObject.h>
31
32 #include "ui_TaskSelectLinkProperty.h"
33 #include "TaskSelectLinkProperty.h"
34
35
36 using namespace Gui::TaskView;
37
38 /* TRANSLATOR Gui::TaskView::TaskSelectLinkProperty */
39
40 TaskSelectLinkProperty::TaskSelectLinkProperty(const char* sFilter, App::Property* prop, QWidget* parent)
41 : TaskBox(Gui::BitmapFactory().pixmap("mouse_pointer"), tr("edit selection"), true, parent)
42 , Filter(nullptr)
43 , LinkSub(nullptr)
44 , LinkList(nullptr)
45 {
46 // we need a separate container widget to add all controls to
47 proxy = new QWidget(this);
48 ui = new Ui_TaskSelectLinkProperty();
49 ui->setupUi(proxy);
50 setupConnections();
51
52 this->groupLayout()->addWidget(proxy);
53 Gui::Selection().Attach(this);
54
55 ui->Remove->setIcon(Gui::BitmapFactory().iconFromTheme("delete"));
56 ui->Add->setIcon(Gui::BitmapFactory().iconFromTheme("list-add"));
57 ui->Invert->setIcon(Gui::BitmapFactory().iconFromTheme("list-remove"));
58 ui->Help->setIcon(Gui::BitmapFactory().iconFromTheme("help-browser"));
59
60 // deactivate not implemented stuff
61 ui->Remove->setDisabled(true);
62 ui->Add->setDisabled(true);
63 ui->Invert->setDisabled(true);
64 ui->Help->setDisabled(true);
65
66 // property have to be set!
67 assert(prop);
68 StartObject = nullptr;
69 if (prop->isDerivedFrom<App::PropertyLinkSub>()) {
70 LinkSub = dynamic_cast<App::PropertyLinkSub*>(prop);
71 }
72 else if (prop->isDerivedFrom<App::PropertyLinkList>()) {
73 LinkList = dynamic_cast<App::PropertyLinkList*>(prop);
74 }
75 else {
76 Base::Console().warning(
77 "Unknown Link property type in "
78 "Gui::TaskView::TaskSelectLinkProperty::TaskSelectLinkProperty()"
79 );
80 }
81
82 setFilter(sFilter);
83 }
84
85 TaskSelectLinkProperty::~TaskSelectLinkProperty()
86 {
87 delete ui;
88 Gui::Selection().Detach(this);
89 }
90
91 void TaskSelectLinkProperty::setupConnections()
92 {
93 // clang-format off
94 connect(ui->Remove, &QToolButton::clicked,
95 this, &TaskSelectLinkProperty::onRemoveClicked);
96 connect(ui->Add, &QToolButton::clicked,
97 this, &TaskSelectLinkProperty::onAddClicked);
98 connect(ui->Invert, &QToolButton::clicked,
99 this, &TaskSelectLinkProperty::onInvertClicked);
100 connect(ui->Help, &QToolButton::clicked,
101 this, &TaskSelectLinkProperty::onHelpClicked);
102 // clang-format on
103 }
104
105 void TaskSelectLinkProperty::changeEvent(QEvent* e)
106 {
107 TaskBox::changeEvent(e);
108 if (e->type() == QEvent::LanguageChange) {
109 ui->retranslateUi(proxy);
110 }
111 }
112
113 /// @cond DOXERR
114
115
116 bool TaskSelectLinkProperty::setFilter(const char* sFilter)
117 {
118 Filter = new SelectionFilter(sFilter);
119 return Filter->isValid();
120 }
121
122
123 void TaskSelectLinkProperty::activate()
124 {
125 // first clear the selection
126 Gui::Selection().clearSelection();
127 // set the gate for the filter
128 Gui::Selection().addSelectionGate(new SelectionFilterGate(Filter));
129
130 // In case of LinkSub property
131 if (LinkSub) {
132 // save the start values for a cnacel operation (reject())
133 StartValueBuffer = LinkSub->getSubValues();
134 StartObject = LinkSub->getValue();
135 if (StartObject) {
136 std::string ObjName = StartObject->getNameInDocument();
137 std::string DocName = StartObject->getDocument()->getName();
138
139 for (const auto& it : StartValueBuffer) {
140 Gui::Selection().addSelection(DocName.c_str(), ObjName.c_str(), it.c_str());
141 }
142 }
143 }
144 // In case of LinkList property
145 else if (LinkList) {
146 // save the start values for a cnacel operation (reject())
147 const std::vector<App::DocumentObject*>& Values = LinkList->getValues();
148 for (const auto& Value : Values) {
149 std::string ObjName = Value->getNameInDocument();
150 std::string DocName = Value->getDocument()->getName();
151 Gui::Selection().addSelection(DocName.c_str(), ObjName.c_str());
152 }
153 }
154
155 checkSelectionStatus();
156 }
157
158 bool TaskSelectLinkProperty::accept()
159 {
160 // set the property with the selection
161 sendSelection2Property();
162
163 // clear selection and remove gate (return to normal operation)
164 Gui::Selection().clearSelection();
165 Gui::Selection().rmvSelectionGate();
166 return true;
167 }
168
169 bool TaskSelectLinkProperty::reject()
170 {
171 if (LinkSub) {
172 // restore the old values
173 LinkSub->setValue(StartObject, StartValueBuffer);
174 }
175
176 // clear selection and remove gate (return to normal operation)
177 Gui::Selection().clearSelection();
178 Gui::Selection().rmvSelectionGate();
179 return true;
180 }
181
182 void TaskSelectLinkProperty::sendSelection2Property()
183 {
184 if (LinkSub) {
185 std::vector<Gui::SelectionObject> temp = Gui::Selection().getSelectionEx();
186 assert(temp.size() >= 1);
187 LinkSub->setValue(temp[0].getObject(), temp[0].getSubNames());
188 }
189 else if (LinkList) {
190 std::vector<Gui::SelectionObject> sel = Gui::Selection().getSelectionEx();
191 std::vector<App::DocumentObject*> temp;
192 for (auto& it : sel) {
193 temp.push_back(it.getObject());
194 }
195
196 LinkList->setValues(temp);
197 }
198 }
199
200 void TaskSelectLinkProperty::checkSelectionStatus()
201 {
202 QPalette palette(QApplication::palette());
203
204 if (Filter->match()) {
205 palette.setBrush(QPalette::Base, QColor(200, 250, 200));
206 Q_EMIT emitSelectionFit();
207 }
208 else {
209 palette.setBrush(QPalette::Base, QColor(250, 200, 200));
210 Q_EMIT emitSelectionMisfit();
211 }
212 // ui->listWidget->setAutoFillBackground(true);
213 ui->listWidget->setPalette(palette);
214 }
215
216 void TaskSelectLinkProperty::OnChange(
217 Gui::SelectionSingleton::SubjectType& rCaller,
218 Gui::SelectionSingleton::MessageType Reason
219 )
220 {
221 Q_UNUSED(rCaller);
222 if (Reason.Type == SelectionChanges::AddSelection || Reason.Type == SelectionChanges::RmvSelection
223 || Reason.Type == SelectionChanges::SetSelection
224 || Reason.Type == SelectionChanges::ClrSelection) {
225 ui->listWidget->clear();
226 std::vector<Gui::SelectionSingleton::SelObj> sel = Gui::Selection().getSelection();
227 for (const auto& it : sel) {
228 std::string temp;
229 temp += it.FeatName;
230 if (strcmp(it.SubName, "") != 0) {
231 temp += "::";
232 temp += it.SubName;
233 }
234 new QListWidgetItem(QString::fromLatin1(temp.c_str()), ui->listWidget);
235 }
236 checkSelectionStatus();
237 }
238 }
239 /// @endcond
240
241 void TaskSelectLinkProperty::onRemoveClicked(bool)
242 {}
243
244 void TaskSelectLinkProperty::onAddClicked(bool)
245 {}
246
247 void TaskSelectLinkProperty::onInvertClicked(bool)
248 {}
249
250 void TaskSelectLinkProperty::onHelpClicked(bool)
251 {}
252
253
254 #include "moc_TaskSelectLinkProperty.cpp"