Debian Codegraph @

src/Gui/InputVector.h

raw
1 /***************************************************************************
2 * Copyright (c) 2008 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 #ifndef GUI_INPUTVECTOR_H
24 #define GUI_INPUTVECTOR_H
25
26 #include <memory>
27 #include <QApplication>
28 #include <QDialog>
29 #include <QMessageBox>
30 #include <Gui/propertyeditor/PropertyItem.h>
31
32 class QGridLayout;
33 class QLabel;
34 class QDoubleSpinBox;
35 class QComboBox;
36
37 namespace Gui
38 {
39 class QuantitySpinBox;
40
41 class GuiExport LocationWidget: public QWidget
42 {
43 Q_OBJECT
44
45 public:
46 LocationWidget(QWidget* parent = nullptr);
47 ~LocationWidget() override;
48 QSize sizeHint() const override;
49
50 Base::Vector3d getPosition() const;
51 void setPosition(const Base::Vector3d&);
52 void setDirection(const Base::Vector3d& dir);
53 Base::Vector3d getDirection() const;
54 Base::Vector3d getUserDirection(bool* ok = nullptr) const;
55
56 private:
57 void onDirectionActivated(int);
58
59 private:
60 void changeEvent(QEvent*) override;
61 void retranslateUi();
62
63 private:
64 QGridLayout* box;
65 QLabel* xLabel;
66 QLabel* yLabel;
67 QLabel* zLabel;
68 QLabel* dLabel;
69 QuantitySpinBox* xValue;
70 QuantitySpinBox* yValue;
71 QuantitySpinBox* zValue;
72 QComboBox* dValue;
73 };
74
75 /** This is the abstract base dialog class that defines the interface for
76 * specifying a direction vector by the user.
77 * @author Werner Mayer
78 */
79 class GuiExport LocationDialog: public QDialog
80 {
81 Q_OBJECT
82
83 protected:
84 LocationDialog(QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags());
85 ~LocationDialog() override;
86
87 protected:
88 void changeEvent(QEvent* e) override = 0;
89
90 private:
91 void onDirectionActivated(int);
92
93 public:
94 virtual Base::Vector3d getDirection() const = 0;
95 Base::Vector3d getUserDirection(bool* ok = nullptr) const;
96
97 private:
98 virtual void directionActivated(int) = 0;
99 };
100
101 /* TRANSLATOR Gui::LocationDialog */
102
103 /** This is the template class that implements the interface of LocationDialog.
104 * The template argument can be the Ui interface class built by uic out of a
105 * .ui file.
106 * This class might be very useful for dialogs where a combo box is used to
107 * define a direction vector by the user. For such classes the programmer doesn't
108 * have to write a subclass to implement the appropriate signals/slots. Instead it's
109 * possible to omit this further class and use LocationDialogUi parametrized
110 * with the generated Ui class.
111 * @author Werner Mayer
112 */
113 template<class Ui>
114 class LocationDialogUi: public LocationDialog, public Ui
115 {
116 public:
117 LocationDialogUi(QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags())
118 : LocationDialog(parent, fl)
119 {
120 this->setupUi(this);
121 this->retranslate();
122 }
123 ~LocationDialogUi() override = default;
124
125 void retranslate()
126 {
127 Ui::retranslateUi(this);
128
129 if (this->direction->count() == 0) {
130 this->direction->insertItems(
131 0,
132 QStringList() << QApplication::translate("Gui::LocationDialog", "X")
133 << QApplication::translate("Gui::LocationDialog", "Y")
134 << QApplication::translate("Gui::LocationDialog", "Z")
135 << QApplication::translate("Gui::LocationDialog", "User defined…")
136 );
137
138 this->direction->setCurrentIndex(2);
139
140 // Vector3d declared to use with QVariant see Gui/propertyeditor/PropertyItem.h
141 this->direction->setItemData(
142 0,
143 QVariant::fromValue<Base::Vector3d>(Base::Vector3d(1, 0, 0))
144 );
145 this->direction->setItemData(
146 1,
147 QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 1, 0))
148 );
149 this->direction->setItemData(
150 2,
151 QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 0, 1))
152 );
153 }
154 else {
155 this->direction->setItemText(0, QApplication::translate("Gui::LocationDialog", "X"));
156 this->direction->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y"));
157 this->direction->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z"));
158 this->direction->setItemText(
159 this->direction->count() - 1,
160 QApplication::translate("Gui::LocationDialog", "User defined…")
161 );
162 }
163 }
164
165 void setPosition(const Base::Vector3d& v)
166 {
167 this->xPos->setValue(v.x);
168 this->yPos->setValue(v.y);
169 this->zPos->setValue(v.z);
170 }
171
172 Base::Vector3d getPosition() const
173 {
174 return Base::Vector3d(
175 this->xPos->value().getValue(),
176 this->yPos->value().getValue(),
177 this->zPos->value().getValue()
178 );
179 }
180
181 Base::Vector3d getDirection() const override
182 {
183 QVariant data = this->direction->itemData(this->direction->currentIndex());
184 if (data.canConvert<Base::Vector3d>()) {
185 return data.value<Base::Vector3d>();
186 }
187 else {
188 return Base::Vector3d(0, 0, 1);
189 }
190 }
191
192 protected:
193 void changeEvent(QEvent* e) override
194 {
195 if (e->type() == QEvent::LanguageChange) {
196 this->retranslate();
197 }
198 else {
199 QDialog::changeEvent(e);
200 }
201 }
202
203 private:
204 void setDirection(const Base::Vector3d& dir)
205 {
206 if (dir.Length() < Base::Vector3d::epsilon()) {
207 return;
208 }
209
210 // check if the user-defined direction is already there
211 for (int i = 0; i < this->direction->count() - 1; i++) {
212 QVariant data = this->direction->itemData(i);
213 if (data.canConvert<Base::Vector3d>()) {
214 const auto val = data.value<Base::Vector3d>();
215 if (val == dir) {
216 this->direction->setCurrentIndex(i);
217 return;
218 }
219 }
220 }
221
222 // add a new item before the very last item
223 QString display = QStringLiteral("(%1,%2,%3)").arg(dir.x).arg(dir.y).arg(dir.z);
224 this->direction->insertItem(
225 this->direction->count() - 1,
226 display,
227 QVariant::fromValue<Base::Vector3d>(dir)
228 );
229 this->direction->setCurrentIndex(this->direction->count() - 2);
230 }
231 void directionActivated(int index) override
232 {
233 // last item is selected to define direction by user
234 if (index + 1 == this->direction->count()) {
235 bool ok;
236 Base::Vector3d dir = this->getUserDirection(&ok);
237 if (ok) {
238 if (dir.Length() < Base::Vector3d::epsilon()) {
239 QMessageBox::critical(
240 this,
241 LocationDialog::tr("Wrong direction"),
242 LocationDialog::tr("Direction must not be the null vector")
243 );
244 return;
245 }
246
247 setDirection(dir);
248 }
249 }
250 }
251 };
252
253 /** This template class does basically the same as LocationDialogUi unless
254 * that it doesn inherit from a widget but only from the UI_-generated class.
255 * Thus, this class can be used as composition in dialog-based classes without
256 * including the ui_-generated header file. The Ui_-class can simply be forward
257 * declared, then.
258 * @author Werner Mayer
259 */
260 template<class Ui>
261 class LocationUi: public Ui
262 {
263 public:
264 LocationUi(QDialog* dlg)
265 {
266 this->setupUi(dlg);
267 this->retranslate(dlg);
268 }
269 ~LocationUi() = default;
270
271 void retranslate(QDialog* dlg)
272 {
273 Ui::retranslateUi(dlg);
274
275 if (this->direction->count() == 0) {
276 this->direction->insertItems(
277 0,
278 QStringList() << QApplication::translate("Gui::LocationDialog", "X")
279 << QApplication::translate("Gui::LocationDialog", "Y")
280 << QApplication::translate("Gui::LocationDialog", "Z")
281 << QApplication::translate("Gui::LocationDialog", "User defined…")
282 );
283
284 this->direction->setCurrentIndex(2);
285
286 // Vector3d declared to use with QVariant see Gui/propertyeditor/PropertyItem.h
287 this->direction->setItemData(
288 0,
289 QVariant::fromValue<Base::Vector3d>(Base::Vector3d(1, 0, 0))
290 );
291 this->direction->setItemData(
292 1,
293 QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 1, 0))
294 );
295 this->direction->setItemData(
296 2,
297 QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 0, 1))
298 );
299 }
300 else {
301 this->direction->setItemText(0, QApplication::translate("Gui::LocationDialog", "X"));
302 this->direction->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y"));
303 this->direction->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z"));
304 this->direction->setItemText(
305 this->direction->count() - 1,
306 QApplication::translate("Gui::LocationDialog", "User defined…")
307 );
308 }
309 }
310
311 void setPosition(const Base::Vector3d& v)
312 {
313 this->xPos->setValue(v.x);
314 this->yPos->setValue(v.y);
315 this->zPos->setValue(v.z);
316 }
317
318 Base::Vector3d getPosition() const
319 {
320 return Base::Vector3d(
321 this->xPos->value().getValue(),
322 this->yPos->value().getValue(),
323 this->zPos->value().getValue()
324 );
325 }
326
327 Base::Vector3d getDirection() const
328 {
329 QVariant data = this->direction->itemData(this->direction->currentIndex());
330 if (data.canConvert<Base::Vector3d>()) {
331 return data.value<Base::Vector3d>();
332 }
333 else {
334 return Base::Vector3d(0, 0, 1);
335 }
336 }
337
338 public:
339 void setDirection(const Base::Vector3d& dir)
340 {
341 if (dir.Length() < Base::Vector3d::epsilon()) {
342 return;
343 }
344
345 // check if the user-defined direction is already there
346 for (int i = 0; i < this->direction->count() - 1; i++) {
347 QVariant data = this->direction->itemData(i);
348 if (data.canConvert<Base::Vector3d>()) {
349 const auto val = data.value<Base::Vector3d>();
350 if (val == dir) {
351 this->direction->setCurrentIndex(i);
352 return;
353 }
354 }
355 }
356
357 // add a new item before the very last item
358 QString display = QStringLiteral("(%1,%2,%3)").arg(dir.x).arg(dir.y).arg(dir.z);
359 this->direction->insertItem(
360 this->direction->count() - 1,
361 display,
362 QVariant::fromValue<Base::Vector3d>(dir)
363 );
364 this->direction->setCurrentIndex(this->direction->count() - 2);
365 }
366 bool directionActivated(LocationDialog* dlg, int index)
367 {
368 // last item is selected to define direction by user
369 if (index + 1 == this->direction->count()) {
370 bool ok;
371 Base::Vector3d dir = dlg->getUserDirection(&ok);
372 if (ok) {
373 if (dir.Length() < Base::Vector3d::epsilon()) {
374 QMessageBox::critical(
375 dlg,
376 LocationDialog::tr("Wrong direction"),
377 LocationDialog::tr("Direction must not be the null vector")
378 );
379 return false;
380 }
381 setDirection(dir);
382 }
383 }
384 return true;
385 }
386 };
387
388 /** This template class is a subclass of LocationDialog using LocationUi
389 * and implements the pure virtual methods of its base class.
390 * Other dialog-based classes can directly inherit from this class if the
391 * location-interface is required. But note, in this case the ui_-header file
392 * needs to be included. If this should be avoided the class LocationUi
393 * must be used instead of whereas the Ui_-class can be forward declared.
394 * @author Werner Mayer
395 */
396 template<class Ui>
397 class LocationDialogImp: public LocationDialog
398 {
399 public:
400 LocationDialogImp(QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags())
401 : LocationDialog(parent, fl)
402 , ui(this)
403 {}
404 ~LocationDialogImp() override = default;
405
406 Base::Vector3d getDirection() const override
407 {
408 return ui.getDirection();
409 }
410
411 protected:
412 void changeEvent(QEvent* e) override
413 {
414 if (e->type() == QEvent::LanguageChange) {
415 ui.retranslate(this);
416 }
417 else {
418 QDialog::changeEvent(e);
419 }
420 }
421
422 private:
423 void directionActivated(int index) override
424 {
425 ui.directionActivated(this, index);
426 }
427
428 protected:
429 LocationUi<Ui> ui;
430 };
431
432 /**
433 * @brief The AbstractUi class
434 * Abstract base class the defines the class interface.
435 * @author Werner Mayer
436 */
437 class AbstractUi
438 {
439 public:
440 virtual ~AbstractUi() = default;
441 virtual void retranslate(QDialog* dlg) = 0;
442 virtual void setPosition(const Base::Vector3d& v) = 0;
443 virtual Base::Vector3d getPosition() const = 0;
444 virtual Base::Vector3d getDirection() const = 0;
445 virtual void setDirection(const Base::Vector3d& dir) = 0;
446 virtual bool directionActivated(LocationDialog* dlg, int index) = 0;
447 virtual boost::any get() = 0;
448 };
449
450 /** This is the template class that implements the interface of AbstractUi.
451 * The template argument is the Ui interface class built by uic out of a
452 * .ui file.
453 * @author Werner Mayer
454 */
455 template<class Ui>
456 class LocationImpUi: public AbstractUi
457 {
458 public:
459 LocationImpUi(Ui* ui)
460 : ui(ui)
461 {}
462 ~LocationImpUi() override = default;
463
464 boost::any get() override
465 {
466 return ui;
467 }
468
469 void retranslate(QDialog* dlg) override
470 {
471 ui->retranslateUi(dlg);
472
473 if (ui->direction->count() == 0) {
474 ui->direction->insertItems(
475 0,
476 QStringList() << QApplication::translate("Gui::LocationDialog", "X")
477 << QApplication::translate("Gui::LocationDialog", "Y")
478 << QApplication::translate("Gui::LocationDialog", "Z")
479 << QApplication::translate("Gui::LocationDialog", "User defined…")
480 );
481
482 ui->direction->setCurrentIndex(2);
483
484 // Vector3d declared to use with QVariant see Gui/propertyeditor/PropertyItem.h
485 ui->direction->setItemData(0, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(1, 0, 0)));
486 ui->direction->setItemData(1, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 1, 0)));
487 ui->direction->setItemData(2, QVariant::fromValue<Base::Vector3d>(Base::Vector3d(0, 0, 1)));
488 }
489 else {
490 ui->direction->setItemText(0, QApplication::translate("Gui::LocationDialog", "X"));
491 ui->direction->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y"));
492 ui->direction->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z"));
493 ui->direction->setItemText(
494 ui->direction->count() - 1,
495 QApplication::translate("Gui::LocationDialog", "User defined…")
496 );
497 }
498 }
499
500 void setPosition(const Base::Vector3d& v) override
501 {
502 ui->xPos->setValue(v.x);
503 ui->yPos->setValue(v.y);
504 ui->zPos->setValue(v.z);
505 }
506
507 Base::Vector3d getPosition() const override
508 {
509 return Base::Vector3d(
510 ui->xPos->value().getValue(),
511 ui->yPos->value().getValue(),
512 ui->zPos->value().getValue()
513 );
514 }
515
516 Base::Vector3d getDirection() const override
517 {
518 QVariant data = ui->direction->itemData(ui->direction->currentIndex());
519 if (data.canConvert<Base::Vector3d>()) {
520 return data.value<Base::Vector3d>();
521 }
522 else {
523 return Base::Vector3d(0, 0, 1);
524 }
525 }
526
527 public:
528 void setDirection(const Base::Vector3d& dir) override
529 {
530 if (dir.Length() < Base::Vector3d::epsilon()) {
531 return;
532 }
533
534 // check if the user-defined direction is already there
535 for (int i = 0; i < ui->direction->count() - 1; i++) {
536 QVariant data = ui->direction->itemData(i);
537 if (data.canConvert<Base::Vector3d>()) {
538 const auto val = data.value<Base::Vector3d>();
539 if (val == dir) {
540 ui->direction->setCurrentIndex(i);
541 return;
542 }
543 }
544 }
545
546 // add a new item before the very last item
547 QString display = QStringLiteral("(%1,%2,%3)").arg(dir.x).arg(dir.y).arg(dir.z);
548 ui->direction->insertItem(
549 ui->direction->count() - 1,
550 display,
551 QVariant::fromValue<Base::Vector3d>(dir)
552 );
553 ui->direction->setCurrentIndex(ui->direction->count() - 2);
554 }
555 bool directionActivated(LocationDialog* dlg, int index) override
556 {
557 // last item is selected to define direction by user
558 if (index + 1 == ui->direction->count()) {
559 bool ok;
560 Base::Vector3d dir = dlg->getUserDirection(&ok);
561 if (ok) {
562 if (dir.Length() < Base::Vector3d::epsilon()) {
563 QMessageBox::critical(
564 dlg,
565 LocationDialog::tr("Wrong direction"),
566 LocationDialog::tr("Direction must not be the null vector")
567 );
568 return false;
569 }
570 setDirection(dir);
571 }
572 }
573 return true;
574 }
575
576 private:
577 std::shared_ptr<Ui> ui;
578 };
579
580 /** This is a subclass of LocationDialog using AbstractUi that implements
581 * the pure virtual methods of its base class.
582 * Other dialog-based classes can directly inherit from this class if the
583 * location-interface is required.
584 * The advantage of this class compared to LocationDialogImp is that the
585 * ui_-header file doesn't need to be included in the header file of its
586 * sub-classes because it uses "type erasure with templates".
587 * @author Werner Mayer
588 */
589 class GuiExport LocationDialogUiImp: public LocationDialog
590 {
591 public:
592 template<class T>
593 LocationDialogUiImp(T* t, QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags())
594 : LocationDialog(parent, fl)
595 , ui(new LocationImpUi<T>(t))
596 {
597 std::shared_ptr<T> uit = boost::any_cast<std::shared_ptr<T>>(ui->get());
598 uit->setupUi(this);
599 ui->retranslate(this);
600 }
601 ~LocationDialogUiImp() override;
602
603 Base::Vector3d getDirection() const override;
604
605 Base::Vector3d getPosition() const;
606
607 protected:
608 void changeEvent(QEvent* e) override;
609
610 private:
611 void directionActivated(int index) override;
612
613 protected:
614 std::unique_ptr<AbstractUi> ui;
615 };
616
617 } // namespace Gui
618
619 #endif // GUI_INPUTVECTOR_H