Debian Codegraph @

src/Gui/Widgets.h

raw
1 /***************************************************************************
2 * Copyright (c) 2004 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
24 #ifndef GUI_WIDGETS_H
25 #define GUI_WIDGETS_H
26
27 #include <memory>
28 #include <FCGlobal.h>
29
30 #include <QBasicTimer>
31 #include <QButtonGroup>
32 #include <QDialog>
33 #include <QElapsedTimer>
34 #include <QKeySequenceEdit>
35 #include <QLabel>
36 #include <QLineEdit>
37 #include <QListWidget>
38 #include <QPlainTextEdit>
39 #include <QPointer>
40 #include <QPushButton>
41 #include <QToolButton>
42
43 #include <Base/Parameter.h>
44 #include "ExpressionBinding.h"
45
46 class QGridLayout;
47 class QVBoxLayout;
48 class QTreeWidget;
49 class QTreeWidgetItem;
50 class QSpacerItem;
51
52 namespace Gui
53 {
54 class PrefCheckBox;
55 class CommandViewItemPrivate;
56
57 /**
58 * This class allows one to drag one or more items which correspond to a Command object.
59 * The dragged items can be dropped onto the @ref Gui::PythonConsole.
60 * @see CommandViewItem, Command
61 * @author Werner Mayer
62 */
63 class CommandIconView: public QListWidget
64 {
65 Q_OBJECT
66
67 public:
68 CommandIconView(QWidget* parent = nullptr);
69 ~CommandIconView() override;
70
71 protected:
72 void startDrag(Qt::DropActions supportedActions) override;
73
74 protected Q_SLOTS:
75 void onSelectionChanged(QListWidgetItem* item, QListWidgetItem*);
76
77 Q_SIGNALS:
78 /** Emits this signal if selection has changed. */
79 void emitSelectionChanged(const QString&);
80 };
81
82 // ------------------------------------------------------------------------------
83
84 class GuiExport ActionSelector: public QWidget
85 {
86 Q_OBJECT
87
88 public:
89 ActionSelector(QWidget* parent = nullptr);
90 ~ActionSelector() override;
91
92 QTreeWidget* availableTreeWidget() const
93 {
94 return availableWidget;
95 }
96 QTreeWidget* selectedTreeWidget() const
97 {
98 return selectedWidget;
99 }
100 void setSelectedLabel(const QString&);
101 QString selectedLabel() const;
102 void setAvailableLabel(const QString&);
103 QString availableLabel() const;
104
105 private:
106 void keyPressEvent(QKeyEvent*) override;
107 void changeEvent(QEvent*) override;
108 void retranslateUi();
109 void setButtonsEnabled();
110
111 private Q_SLOTS:
112 void onAddButtonClicked();
113 void onRemoveButtonClicked();
114 void onUpButtonClicked();
115 void onDownButtonClicked();
116 void onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*);
117 void onItemDoubleClicked(QTreeWidgetItem* item, int column);
118
119 private:
120 QGridLayout* gridLayout;
121 QVBoxLayout* vboxLayout;
122 QVBoxLayout* vboxLayout1;
123 QPushButton* addButton;
124 QPushButton* removeButton;
125 QPushButton* upButton;
126 QPushButton* downButton;
127 QLabel* labelAvailable;
128 QLabel* labelSelected;
129 QTreeWidget* availableWidget;
130 QTreeWidget* selectedWidget;
131 QSpacerItem* spacerItem;
132 QSpacerItem* spacerItem1;
133 };
134
135 // ------------------------------------------------------------------------------
136
137 /**
138 * The AccelLineEdit class provides a lineedit to specify shortcuts.
139 * \author Werner Mayer
140 */
141 class GuiExport AccelLineEdit: public QKeySequenceEdit
142 {
143 Q_OBJECT
144
145 public:
146 explicit AccelLineEdit(QWidget* parent = nullptr);
147 explicit AccelLineEdit(const QKeySequence& keySequence, QWidget* parent = nullptr);
148 void setReadOnly(bool value);
149 bool isEmpty() const;
150 QString text() const;
151 };
152
153 // ------------------------------------------------------------------------------
154
155 /**
156 * The ModifierLineEdit class provides a lineedit to specify modifiers.
157 */
158 class GuiExport ModifierLineEdit: public QLineEdit
159 {
160 Q_OBJECT
161
162 public:
163 ModifierLineEdit(QWidget* parent = nullptr);
164
165 protected:
166 void keyPressEvent(QKeyEvent* e) override;
167 };
168
169 // ------------------------------------------------------------------------------
170
171 /**
172 * The ClearLineEdit class adds a clear button at the right side.
173 * http://stackoverflow.com/questions/21232224/qlineedit-with-custom-button
174 */
175 class GuiExport ClearLineEdit: public QLineEdit
176 {
177 Q_OBJECT
178
179 public:
180 ClearLineEdit(QWidget* parent = nullptr);
181
182 protected:
183 void resizeEvent(QResizeEvent*) override;
184
185 private Q_SLOTS:
186 void updateClearButton(const QString& text);
187
188 private:
189 QAction* clearAction;
190 };
191
192 // ------------------------------------------------------------------------------
193
194 using CheckListItem = QPair<QString, bool>;
195 class Ui_DlgTreeWidget;
196
197 /**
198 * The CheckListDialog class provides a dialog with a QListView with
199 * checkable items inside.
200 * \author Werner Mayer
201 */
202 class GuiExport CheckListDialog: public QDialog
203 {
204
205 Q_OBJECT
206
207 public:
208 CheckListDialog(QWidget* parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags());
209 ~CheckListDialog() override;
210
211 void setCheckableItems(const QStringList& items);
212 void setCheckableItems(const QList<CheckListItem>& items);
213 QStringList getCheckedItems() const;
214
215 void accept() override;
216
217 private:
218 QStringList checked;
219 std::unique_ptr<Ui_DlgTreeWidget> ui;
220 };
221
222 // ------------------------------------------------------------------------------
223
224 /**
225 * Implementation of a color button.
226 * \author Werner Mayer
227 */
228 class GuiExport ColorButton: public QPushButton
229 {
230 Q_OBJECT
231
232 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed)
233 Q_PROPERTY(
234 bool allowChangeColor READ allowChangeColor WRITE setAllowChangeColor
235 ) // clazy:exclude=qproperty-without-notify
236 Q_PROPERTY(bool drawFrame READ drawFrame WRITE setDrawFrame) // clazy:exclude=qproperty-without-notify
237 Q_PROPERTY(
238 bool allowTransparency READ allowTransparency WRITE setAllowTransparency
239 ) // clazy:exclude=qproperty-without-notify
240
241 public:
242 ColorButton(QWidget* parent = nullptr);
243 ~ColorButton() override;
244
245 void setColor(const QColor&);
246 QColor color() const;
247
248 void setPackedColor(uint32_t);
249 uint32_t packedColor() const;
250
251 void setAllowChangeColor(bool);
252 bool allowChangeColor() const;
253
254 void setDrawFrame(bool);
255 bool drawFrame() const;
256
257 void setAllowTransparency(bool);
258 bool allowTransparency() const;
259
260 void setModal(bool);
261 bool isModal() const;
262
263 void setAutoChangeColor(bool);
264 bool autoChangeColor() const;
265
266 public Q_SLOTS:
267 void onChooseColor();
268
269 private Q_SLOTS:
270 void onColorChosen(const QColor&);
271 void onRejected();
272
273 Q_SIGNALS:
274 /** Emits this signal when color has changed */
275 void changed();
276
277 protected:
278 void paintEvent(QPaintEvent*) override;
279
280 private:
281 void showModeless();
282 void showModal();
283
284 private:
285 struct ColorButtonP* d;
286 };
287
288 // ------------------------------------------------------------------------------
289
290 /**
291 * A text label where a url can specified. When the user clicks on the text label the system browser
292 * gets opened with the specified url.
293 *
294 * This can be used for e.g. in the about dialog where the url of the maintainer of an application
295 * can be specified.
296 * @author Werner Mayer
297 */
298 class GuiExport UrlLabel: public QLabel
299 {
300 Q_OBJECT
301 Q_PROPERTY(QString url READ url WRITE setUrl) // clazy:exclude=qproperty-without-notify
302 Q_PROPERTY(
303 bool launchExternal READ launchExternal WRITE setLaunchExternal
304 ) // clazy:exclude=qproperty-without-notify
305
306 public:
307 UrlLabel(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
308 ~UrlLabel() override;
309
310 QString url() const;
311 bool launchExternal() const;
312
313 Q_SIGNALS:
314 void linkClicked(QString url);
315
316 public Q_SLOTS:
317 void setUrl(const QString& u);
318 void setLaunchExternal(bool l);
319
320 protected:
321 void mouseReleaseEvent(QMouseEvent*) override;
322
323 private:
324 QString _url;
325 bool _launchExternal;
326 };
327
328
329 /**
330 * A text label whose appearance can change based on a specified state.
331 *
332 * The state is an arbitrary string exposed as a Qt Property (and thus available for selection via
333 * a stylesheet). This is intended for things like messages to the user, where a message that is an
334 * "error" might be colored differently than one that is a "warning" or a "message".
335 *
336 * In order of style precedence for a given state: User preference > Stylesheet > Default
337 * unless the stylesheet sets the overridePreference, in which case the stylesheet will
338 * take precedence. If a stylesheet sets styles for this widgets states, it should also
339 * set the "handledByStyle" property to ensure the style values are used, rather than the
340 * defaults.
341 *
342 * For example, the .qss might contain:
343 * Gui--StatefulLabel {
344 * qproperty-overridePreference: true;
345 * }
346 * Gui--StatefulLabel[state="special_state"] {
347 * color: red;
348 * }
349 * In this case, StatefulLabels with state "special_state" will be colored red, regardless of any
350 * entry in preferences. Use the "overridePreference" stylesheet option with care!
351 *
352 * @author Chris Hennes
353 */
354 class GuiExport StatefulLabel: public QLabel, public Base::Observer<const char*>
355 {
356 Q_OBJECT
357 Q_PROPERTY(bool overridePreference MEMBER _overridePreference WRITE setOverridePreference)
358 Q_PROPERTY(QString state MEMBER _state WRITE setState)
359
360 public:
361 StatefulLabel(QWidget* parent = nullptr);
362 ~StatefulLabel() override;
363
364 /** If an unrecognized state is set, use this style */
365 void setDefaultStyle(const QString& defaultStyle);
366
367 /** If any of the states have user preferences associated with them, this sets the parameter
368 group that stores those preferences. All states must be in the same parameter group, but
369 the group does not have to have entries for all of them. */
370 void setParameterGroup(const std::string& groupName);
371
372 /** Register a state and its corresponding style (optionally attached to a user preference) */
373 void registerState(
374 const QString& state,
375 const QString& styleCSS,
376 const std::string& preferenceName = std::string()
377 );
378
379 /** For convenience, allow simple color-only states via QColor (optionally attached to a user
380 * preference) */
381 void registerState(
382 const QString& state,
383 const QColor& color,
384 const std::string& preferenceName = std::string()
385 );
386
387 /** For convenience, allow simple color-only states via QColor (optionally attached to a user
388 * preference) */
389 void registerState(
390 const QString& state,
391 const QColor& foregroundColor,
392 const QColor& backgroundColor,
393 const std::string& preferenceName = std::string()
394 );
395
396 /** Observes the parameter group and clears the cache if it changes */
397 void OnChange(Base::Subject<const char*>& rCaller, const char* rcReason) override;
398
399 public Q_SLOTS:
400 void setState(QString state);
401 void setOverridePreference(bool overridePreference);
402
403 private:
404 QString _state;
405 bool _overridePreference;
406 ParameterGrp::handle _parameterGroup;
407 ParameterGrp::handle _stylesheetGroup;
408
409 struct StateData
410 {
411 QString defaultCSS;
412 std::string preferenceString;
413 };
414
415 std::map<QString, StateData> _availableStates;
416 std::map<QString, QString> _styleCache;
417 QString _defaultStyle;
418 };
419
420 // ----------------------------------------------------------------------
421
422 /**
423 * The LabelButton class provides a label with a button on the right side.
424 * @author Werner Mayer
425 */
426 class GuiExport LabelButton: public QWidget
427 {
428 Q_OBJECT
429
430 Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
431
432 public:
433 LabelButton(QWidget* parent = nullptr);
434 ~LabelButton() override;
435
436 QVariant value() const;
437
438 QLabel* getLabel() const;
439 QPushButton* getButton() const;
440
441 public Q_SLOTS:
442 void setValue(const QVariant&);
443
444 protected:
445 virtual void showValue(const QVariant& data);
446 void resizeEvent(QResizeEvent*) override;
447
448 protected Q_SLOTS:
449 virtual void browse();
450
451 Q_SIGNALS:
452 void valueChanged(const QVariant&);
453 void buttonClicked();
454
455 private:
456 QLabel* label;
457 QPushButton* button;
458 QVariant _val;
459 };
460
461 // ----------------------------------------------------------------------
462
463 /**
464 * Qt's tooltip does not work as expected with some classes, e.g. when showing
465 * it in the 3d view it immediately receives a timer event to destroy itself.
466 * This class is thought to circumvent this behaviour by filtering the internal
467 * timer events.
468 * @author Werner Mayer
469 */
470 class GuiExport ToolTip: public QObject
471 {
472 public:
473 static void showText(const QPoint& pos, const QString& text, QWidget* w = nullptr);
474 static void hideText();
475
476 protected:
477 static ToolTip* instance();
478
479 ToolTip();
480 ~ToolTip() override;
481
482 void timerEvent(QTimerEvent* e) override;
483 bool eventFilter(QObject* o, QEvent* e) override;
484
485 void installEventFilter();
486 void removeEventFilter();
487
488 private:
489 bool installed, hidden;
490 static ToolTip* inst;
491 QString text;
492 QPoint pos;
493 QPointer<QWidget> w; // need guard in case widget gets destroyed
494 QBasicTimer tooltipTimer;
495 QElapsedTimer displayTime;
496 };
497
498 // ----------------------------------------------------------------------
499
500 class GuiExport StatusWidget: public QDialog
501 {
502 Q_OBJECT
503
504 public:
505 StatusWidget(QWidget* parent);
506 ~StatusWidget() override;
507 void setStatusText(const QString&);
508 QSize sizeHint() const override;
509 void showText(int ms);
510
511 protected:
512 void showEvent(QShowEvent*) override;
513 void hideEvent(QHideEvent*) override;
514
515 private:
516 QLabel* label;
517 };
518
519 // ----------------------------------------------------------------------
520
521 class GuiExport PropertyListEditor: public QPlainTextEdit
522 {
523 Q_OBJECT
524
525 public:
526 PropertyListEditor(QWidget* parent = nullptr);
527
528 void lineNumberAreaPaintEvent(QPaintEvent* event);
529 int lineNumberAreaWidth();
530
531 protected:
532 void resizeEvent(QResizeEvent* event) override;
533
534 private Q_SLOTS:
535 void updateLineNumberAreaWidth(int newBlockCount);
536 void highlightCurrentLine();
537 void updateLineNumberArea(const QRect&, int);
538
539 private:
540 QWidget* lineNumberArea;
541 };
542
543 // ----------------------------------------------------------------------
544
545 class GuiExport LabelEditor: public QWidget
546 {
547 Q_OBJECT
548
549 Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
550 Q_PROPERTY(
551 QString buttonText READ buttonText WRITE setButtonText
552 ) // clazy:exclude=qproperty-without-notify
553
554 public:
555 enum InputType
556 {
557 String,
558 Float,
559 Integer
560 };
561
562 LabelEditor(QWidget* parent = nullptr);
563 ~LabelEditor() override;
564
565 /**
566 * Returns the text.
567 */
568 QString text() const;
569
570 /**
571 * Returns the button's text.
572 */
573 QString buttonText() const;
574
575 /**
576 * Set the input type.
577 */
578 void setInputType(InputType);
579
580 public Q_SLOTS:
581 virtual void setText(const QString&);
582 virtual void setButtonText(const QString&);
583 virtual void validateText(const QString&);
584
585 Q_SIGNALS:
586 void textChanged(const QString&);
587
588 private Q_SLOTS:
589 void changeText();
590
591 protected:
592 void resizeEvent(QResizeEvent*) override;
593
594 private:
595 InputType type;
596 QString plainText;
597 QLineEdit* lineEdit;
598 QPushButton* button;
599 };
600
601 /**
602 * The ExpLineEdit class provides a lineedit that support expressing binding.
603 * \author realthunder
604 */
605 class GuiExport ExpLineEdit: public QLineEdit, public ExpressionWidget
606 {
607 Q_OBJECT
608
609 public:
610 ExpLineEdit(QWidget* parent = nullptr, bool expressionOnly = false);
611
612 void setExpression(std::shared_ptr<App::Expression> expr) override;
613 void bind(const App::ObjectIdentifier& _path) override;
614 bool apply(const std::string& propName) override;
615
616 void keyPressEvent(QKeyEvent* event) override;
617 void resizeEvent(QResizeEvent* event) override;
618
619 private Q_SLOTS:
620 void finishFormulaDialog();
621 void openFormulaDialog();
622 void onChange() override;
623
624 private:
625 bool autoClose;
626 };
627
628 /*!
629 * \brief The ButtonGroup class
630 * Unlike Qt's QButtonGroup this class allows it that in exclusive mode
631 * all buttons can be unchecked.
632 */
633 class GuiExport ButtonGroup: public QButtonGroup
634 {
635 Q_OBJECT
636
637 public:
638 ButtonGroup(QObject* parent = nullptr);
639
640 void setExclusive(bool on);
641 bool exclusive() const;
642
643 private:
644 bool _exclusive;
645 };
646
647 /**
648 * Adjusts the position of the given dialog to ensure it remains within the bounds of the main
649 * window. This helps prevent dialogs from appearing partially or fully off-screen relative to the
650 * main application window.
651 *
652 * This function shall be invoked after the dialog has been shown (using show()) or its position or
653 * size has changed. Exception: If the dialog is modal, this function should be called before the
654 * dialog is called with exec().
655 */
656 void adjustDialogPosition(QDialog* dialog);
657
658 } // namespace Gui
659
660 #endif // GUI_WIDGETS_H