Debian Codegraph @

src/Gui/TaskView/TaskAppearance.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2009 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 <Base/Console.h>
26 #include <Gui/Application.h>
27 #include <Gui/BitmapFactory.h>
28 #include <Gui/Document.h>
29 #include <Gui/ViewProvider.h>
30 #include <Gui/WaitCursor.h>
31
32 #include "ui_TaskAppearance.h"
33 #include "TaskAppearance.h"
34
35
36 using namespace Gui::TaskView;
37 namespace sp = std::placeholders;
38
39 /* TRANSLATOR Gui::TaskView::TaskAppearance */
40
41 TaskAppearance::TaskAppearance(QWidget* parent)
42 : TaskBox(Gui::BitmapFactory().pixmap("document-new"), tr("Appearance"), true, parent)
43 {
44 // we need a separate container widget to add all controls to
45 proxy = new QWidget(this);
46 ui = new Ui_TaskAppearance();
47 ui->setupUi(proxy);
48 setupConnections();
49
50 ui->textLabel1_3->hide();
51 ui->changePlot->hide();
52 QMetaObject::connectSlotsByName(this);
53
54 this->groupLayout()->addWidget(proxy);
55 Gui::Selection().Attach(this);
56
57 // NOLINTBEGIN
58 this->connectChangedObject = Gui::Application::Instance->signalChangedObject.connect(
59 std::bind(&TaskAppearance::slotChangedObject, this, sp::_1, sp::_2)
60 );
61 // NOLINTEND
62 }
63
64 TaskAppearance::~TaskAppearance()
65 {
66 delete ui;
67 this->connectChangedObject.disconnect();
68 Gui::Selection().Detach(this);
69 }
70
71 void TaskAppearance::setupConnections()
72 {
73 // clang-format off
74 connect(ui->changeMode, &QComboBox::textActivated,
75 this, &TaskAppearance::onChangeModeActivated);
76 connect(ui->changePlot, &QComboBox::textActivated,
77 this, &TaskAppearance::onChangePlotActivated);
78 connect(ui->spinTransparency, qOverload<int>(&QSpinBox::valueChanged),
79 this, &TaskAppearance::onTransparencyValueChanged);
80 connect(ui->spinPointSize, qOverload<int>(&QSpinBox::valueChanged),
81 this, &TaskAppearance::onPointSizeValueChanged);
82 connect(ui->spinLineWidth, qOverload<int>(&QSpinBox::valueChanged),
83 this, &TaskAppearance::onLineWidthValueChanged);
84 // clang-format on
85 }
86
87 void TaskAppearance::changeEvent(QEvent* e)
88 {
89 TaskBox::changeEvent(e);
90 if (e->type() == QEvent::LanguageChange) {
91 ui->retranslateUi(proxy);
92 }
93 }
94
95 /// @cond DOXERR
96 void TaskAppearance::OnChange(
97 Gui::SelectionSingleton::SubjectType& rCaller,
98 Gui::SelectionSingleton::MessageType Reason
99 )
100 {
101 Q_UNUSED(rCaller);
102 if (Reason.Type == SelectionChanges::AddSelection || Reason.Type == SelectionChanges::RmvSelection
103 || Reason.Type == SelectionChanges::SetSelection
104 || Reason.Type == SelectionChanges::ClrSelection) {
105 std::vector<Gui::ViewProvider*> views = getSelection();
106 setDisplayModes(views);
107 setPointSize(views);
108 setLineWidth(views);
109 setTransparency(views);
110 }
111 }
112 /// @endcond
113
114 void TaskAppearance::slotChangedObject(const Gui::ViewProvider& obj, const App::Property& prop)
115 {
116 // This method gets called if a property of any view provider is changed.
117 // We pick out all the properties for which we need to update this dialog.
118 const std::vector<Gui::ViewProvider*> Provider = getSelection();
119 auto vp = std::find_if(Provider.begin(), Provider.end(), [&obj](Gui::ViewProvider* v) {
120 return v == &obj;
121 });
122
123 if (vp != Provider.end()) {
124 std::string prop_name = obj.getPropertyName(&prop);
125 if (prop.isDerivedFrom<App::PropertyInteger>()) {
126 long value = static_cast<const App::PropertyInteger&>(prop).getValue();
127 if (prop_name == "Transparency") {
128 bool blocked = ui->spinTransparency->blockSignals(true);
129 ui->spinTransparency->setValue(value);
130 ui->spinTransparency->blockSignals(blocked);
131 blocked = ui->horizontalSlider->blockSignals(true);
132 ui->horizontalSlider->setValue(value);
133 ui->horizontalSlider->blockSignals(blocked);
134 }
135 }
136 else if (prop.isDerivedFrom<App::PropertyFloat>()) {
137 float value = static_cast<const App::PropertyFloat&>(prop).getValue();
138 if (prop_name == "PointSize") {
139 bool blocked = ui->spinPointSize->blockSignals(true);
140 ui->spinPointSize->setValue((int)value);
141 ui->spinPointSize->blockSignals(blocked);
142 }
143 else if (prop_name == "LineWidth") {
144 bool blocked = ui->spinLineWidth->blockSignals(true);
145 ui->spinLineWidth->setValue((int)value);
146 ui->spinLineWidth->blockSignals(blocked);
147 }
148 }
149 }
150 }
151
152 /**
153 * Sets the 'Display' property of all selected view providers.
154 */
155 void TaskAppearance::onChangeModeActivated(const QString& s)
156 {
157 Gui::WaitCursor wc;
158 std::vector<Gui::ViewProvider*> Provider = getSelection();
159 for (const auto& It : Provider) {
160 App::Property* prop = It->getPropertyByName("DisplayMode");
161 if (prop && prop->is<App::PropertyEnumeration>()) {
162 auto Display = static_cast<App::PropertyEnumeration*>(prop);
163 Display->setValue((const char*)s.toLatin1());
164 }
165 }
166 }
167
168 void TaskAppearance::onChangePlotActivated(const QString& s)
169 {
170 Base::Console().log("Plot = %s\n", (const char*)s.toLatin1());
171 }
172
173 /**
174 * Sets the 'Transparency' property of all selected view providers.
175 */
176 void TaskAppearance::onTransparencyValueChanged(int transparency)
177 {
178 std::vector<Gui::ViewProvider*> Provider = getSelection();
179 for (const auto& It : Provider) {
180 App::Property* prop = It->getPropertyByName("Transparency");
181 if (prop && prop->isDerivedFrom<App::PropertyInteger>()) {
182 auto Transparency = static_cast<App::PropertyInteger*>(prop);
183 Transparency->setValue(transparency);
184 }
185 }
186 }
187
188 /**
189 * Sets the 'PointSize' property of all selected view providers.
190 */
191 void TaskAppearance::onPointSizeValueChanged(int pointsize)
192 {
193 std::vector<Gui::ViewProvider*> Provider = getSelection();
194 for (const auto& It : Provider) {
195 App::Property* prop = It->getPropertyByName("PointSize");
196 if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
197 auto PointSize = static_cast<App::PropertyFloat*>(prop);
198 PointSize->setValue(static_cast<float>(pointsize));
199 }
200 }
201 }
202
203 /**
204 * Sets the 'LineWidth' property of all selected view providers.
205 */
206 void TaskAppearance::onLineWidthValueChanged(int linewidth)
207 {
208 std::vector<Gui::ViewProvider*> Provider = getSelection();
209 for (const auto& It : Provider) {
210 App::Property* prop = It->getPropertyByName("LineWidth");
211 if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
212 auto LineWidth = static_cast<App::PropertyFloat*>(prop);
213 LineWidth->setValue(static_cast<float>(linewidth));
214 }
215 }
216 }
217
218 void TaskAppearance::setDisplayModes(const std::vector<Gui::ViewProvider*>& views)
219 {
220 QStringList commonModes, modes;
221 for (auto it = views.begin(); it != views.end(); ++it) {
222 App::Property* prop = (*it)->getPropertyByName("DisplayMode");
223 if (prop && prop->is<App::PropertyEnumeration>()) {
224 auto display = static_cast<App::PropertyEnumeration*>(prop);
225 if (!display->hasEnums()) {
226 return;
227 }
228 std::vector<std::string> value = display->getEnumVector();
229 if (it == views.begin()) {
230 for (const auto& jt : value) {
231 commonModes << QLatin1String(jt.c_str());
232 }
233 }
234 else {
235 for (const auto& jt : value) {
236 if (commonModes.contains(QLatin1String(jt.c_str()))) {
237 modes << QLatin1String(jt.c_str());
238 }
239 }
240
241 commonModes = modes;
242 modes.clear();
243 }
244 }
245 }
246
247 ui->changeMode->clear();
248 ui->changeMode->addItems(commonModes);
249 ui->changeMode->setDisabled(commonModes.isEmpty());
250
251 // find the display mode to activate
252 for (const auto view : views) {
253 App::Property* prop = view->getPropertyByName("DisplayMode");
254 if (prop && prop->is<App::PropertyEnumeration>()) {
255 auto display = static_cast<App::PropertyEnumeration*>(prop);
256 QString activeMode = QString::fromLatin1(display->getValueAsString());
257 int index = ui->changeMode->findText(activeMode);
258 if (index != -1) {
259 ui->changeMode->setCurrentIndex(index);
260 break;
261 }
262 }
263 }
264 }
265
266 void TaskAppearance::setPointSize(const std::vector<Gui::ViewProvider*>& views)
267 {
268 bool pointSize = false;
269 for (const auto& view : views) {
270 App::Property* prop = view->getPropertyByName("PointSize");
271 if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
272 bool blocked = ui->spinPointSize->blockSignals(true);
273 ui->spinPointSize->setValue((int)static_cast<App::PropertyFloat*>(prop)->getValue());
274 ui->spinPointSize->blockSignals(blocked);
275 pointSize = true;
276 break;
277 }
278 }
279
280 ui->spinPointSize->setEnabled(pointSize);
281 }
282
283 void TaskAppearance::setLineWidth(const std::vector<Gui::ViewProvider*>& views)
284 {
285 bool lineWidth = false;
286 for (const auto& view : views) {
287 App::Property* prop = view->getPropertyByName("LineWidth");
288 if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
289 bool blocked = ui->spinLineWidth->blockSignals(true);
290 ui->spinLineWidth->setValue((int)static_cast<App::PropertyFloat*>(prop)->getValue());
291 ui->spinLineWidth->blockSignals(blocked);
292 lineWidth = true;
293 break;
294 }
295 }
296
297 ui->spinLineWidth->setEnabled(lineWidth);
298 }
299
300 void TaskAppearance::setTransparency(const std::vector<Gui::ViewProvider*>& views)
301 {
302 bool transparency = false;
303 for (const auto& view : views) {
304 App::Property* prop = view->getPropertyByName("Transparency");
305 if (prop && prop->isDerivedFrom<App::PropertyInteger>()) {
306 bool blocked = ui->spinTransparency->blockSignals(true);
307 ui->spinTransparency->setValue(static_cast<App::PropertyInteger*>(prop)->getValue());
308 ui->spinTransparency->blockSignals(blocked);
309 transparency = true;
310 break;
311 }
312 }
313
314 ui->spinTransparency->setEnabled(transparency);
315 ui->horizontalSlider->setEnabled(transparency);
316 }
317
318 std::vector<Gui::ViewProvider*> TaskAppearance::getSelection() const
319 {
320 std::vector<Gui::ViewProvider*> views;
321
322 // get the complete selection
323 std::vector<SelectionSingleton::SelObj> sel = Selection().getCompleteSelection();
324 for (const auto& it : sel) {
325 Gui::ViewProvider* view = Application::Instance->getDocument(it.pDoc)->getViewProvider(
326 it.pObject
327 );
328 if (view) {
329 views.push_back(view);
330 }
331 }
332
333 return views;
334 }
335
336
337 #include "moc_TaskAppearance.cpp"