Debian Codegraph @

src/Gui/OverlayWidgets.h

raw
1 /****************************************************************************
2 * Copyright (c) 2020 Zheng Lei (realthunder) <[email protected]> *
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 FC_OVERLAYWIDGETS_H
24 #define FC_OVERLAYWIDGETS_H
25
26 #include <QTabWidget>
27 #include <QTimer>
28 #include <QTime>
29 #include <QAction>
30 #include <QToolButton>
31 #include <QGraphicsEffect>
32 #include <QImage>
33 #include <QDockWidget>
34 #include <QSplitter>
35 #include <QMenu>
36
37 #include <Base/Parameter.h>
38
39 #include "OverlayManager.h"
40
41 class QPropertyAnimation;
42 class QLayoutItem;
43
44 namespace Gui
45 {
46
47 class OverlayTabWidget;
48 class OverlayTitleBar;
49 class OverlaySplitterHandle;
50 class OverlaySizeGrip;
51 class OverlayProxyWidget;
52 class OverlayGraphicsEffect;
53 class OverlayDragFrame;
54
55 /// Tab widget to contain dock widgets in overlay mode
56 class OverlayTabWidget: public QTabWidget
57 {
58 Q_OBJECT
59
60 /** @name Graphics effect properties for customization through stylesheet */
61 //@{
62 Q_PROPERTY(
63 QColor effectColor READ effectColor WRITE setEffectColor
64 ) // clazy:exclude=qproperty-without-notify
65 Q_PROPERTY(
66 int effectWidth READ effectWidth WRITE setEffectWidth
67 ) // clazy:exclude=qproperty-without-notify
68 Q_PROPERTY(
69 int effectHeight READ effectHeight WRITE setEffectHeight
70 ) // clazy:exclude=qproperty-without-notify
71 Q_PROPERTY(
72 qreal effectOffsetX READ effectOffsetX WRITE setEffectOffsetX
73 ) // clazy:exclude=qproperty-without-notify
74 Q_PROPERTY(
75 qreal effectOffsetY READ effectOffsetY WRITE setEffectOffsetY
76 ) // clazy:exclude=qproperty-without-notify
77 Q_PROPERTY(
78 qreal effectBlurRadius READ effectBlurRadius WRITE setEffectBlurRadius
79 ) // clazy:exclude=qproperty-without-notify
80 Q_PROPERTY(
81 bool enableEffect READ effectEnabled WRITE setEffectEnabled
82 ) // clazy:exclude=qproperty-without-notify
83 Q_PROPERTY(qreal animation READ animation WRITE setAnimation) // clazy:exclude=qproperty-without-notify
84 //@}
85
86 public:
87 /** Constructor
88 * @param parent: parent widget
89 * @param pos: docking position
90 */
91 OverlayTabWidget(QWidget* parent, Qt::DockWidgetArea pos);
92
93 /// Enable/disable overlay mode for this tab widget
94 void setOverlayMode(bool enable);
95
96 /// Update splitter handle according to overlay status
97 void updateSplitterHandles();
98
99 /** Add a dock widget
100 * @param widget: dock widget to be added
101 * @param title: title for the dock widget
102 */
103 void addWidget(QDockWidget* widget, const QString& title);
104
105 /** Remove a dock widget
106 * @param widget: dock widget to be removed
107 * @param last: optional non overlaid widget. If given, then the removed
108 * dock widget will be tabified together with this one.
109 */
110 void removeWidget(QDockWidget* widget, QDockWidget* last = nullptr);
111
112 /** Set the current dock widget
113 * @param widget: an overlay dock widget
114 *
115 * All other dock widget in the same tab widget will be collapsed, and the
116 * give widget will take the whole space. This is actually handled inside
117 * onCurrentChanged().
118 */
119 void setCurrent(QDockWidget* widget);
120
121 /** Handle ESC key press
122 *
123 * If the this overlay tab widget is hidden and its hint/tabs are visible,
124 * pressing ESC will hide the hint and tabs.
125 *
126 * If this overlay tab widget is visible and the current mouse cursor is on
127 * top of the tab widget title bar or any of its split handler, then
128 * pressing ESC will hide the title bar and split handler.
129 */
130 bool onEscape();
131
132 /// Enable/disable transparent background mode
133 void setTransparent(bool enable);
134
135 /// Check if transparent background mode is active
136 bool isTransparent() const;
137
138 /// Auto mode to show or hide the tab widget
139 enum class AutoMode
140 {
141 /// No auto show or hide
142 NoAutoMode,
143 /// Auto hide tab widget on lost of focus
144 AutoHide,
145 /// Auto show tab widget on any new editing task
146 EditShow,
147 /// Auto hide tab widget on any new editing task
148 EditHide,
149 /// Auto show on any task panel e.g. suggestive task panel in PartDesign
150 TaskShow,
151 };
152 /// Set auto mode to show or hide the tab widget
153 void setAutoMode(AutoMode mode);
154 /// Get current auto mode
155 AutoMode getAutoMode() const
156 {
157 return autoMode;
158 }
159
160 /// Touch the tab widget to trigger saving of settings
161 void touch()
162 {
163 touched = true;
164 }
165 /// Check if the tab widget settings need to be saved
166 bool isTouched() const
167 {
168 return touched;
169 }
170
171 /// Set geometry of this tab widget
172 void setRect(QRect rect);
173
174 /// Get the geometry of this tab widget
175 const QRect& getRect();
176
177 /// Overlay query option
178 enum class QueryOption
179 {
180 /// Report the current overlay status
181 QueryOverlay,
182 /// Report true if transparency status has been changed
183 TransparencyChanged,
184 /// Report true if transparency status has not been changed
185 TransparencyNotChanged,
186 };
187 /// Query overlay status
188 bool isOverlaid(QueryOption option = QueryOption::QueryOverlay) const;
189 /** Check if needs to auto hide this tab widget
190 *
191 * Besides when auto hide mode is activated by user, the tab widget will
192 * also auto hide if the current view does not have panning capability
193 * (queried through MdiView::hasMsg("CanPan")). The user can explicitly
194 * disable this auto hide if no pan by setting user parameter
195 * View/DockOverlayAutoView, or preference option Display -> UI -> Auto
196 * hide in non 3D view.
197 */
198 bool checkAutoHide() const;
199 /** Obtain geometry of auto hiding tab widget
200 * @param rect: output geometry of the tab widget
201 * @return Return true if the tab widget should be auto hiding
202 */
203 bool getAutoHideRect(QRect& rect) const;
204 /// Handler of various actions exposed as buttons on title bar
205 void onAction(QAction*);
206 /// Sync relevant actions status with the current auto mode
207 void syncAutoMode();
208 // Establish if stylesheet is dark
209 static bool isStyleSheetDark(std::string curStyleSheet);
210 // Rotate the AutoHide icon according to the dock area
211 static QPixmap rotateAutoHideIcon(QPixmap pxAutoHide, Qt::DockWidgetArea dockArea);
212
213 /** Set tab widget position offset
214 * @param ofs: the offset size. Width is the x offset for top and bottom
215 * docking tab widget, and y offset for left and right ones. Height is the y
216 * offset for top and bottom, and x offset for left and right ones.
217 */
218 void setOffset(const QSize& ofs);
219 /// Get the tab widget position offset
220 const QSize& getOffset() const
221 {
222 return offset;
223 }
224
225 /** Set tab widget size delta
226 * @param delta: the size delta. For left and right widget, the delta is
227 * added to the height of the tab widget. For top and bottom widget, it is
228 * added to the width.
229 */
230 void setSizeDelta(int delta);
231 /// Get the tab widget size delta
232 int getSizeDelta() const
233 {
234 return sizeDelta;
235 }
236
237 /// Obtain the proxy widget
238 OverlayProxyWidget* getProxyWidget()
239 {
240 return proxyWidget;
241 }
242
243 /// Obtain the current dock widget
244 QDockWidget* currentDockWidget() const;
245 /// Obtain the dock widget by index
246 QDockWidget* dockWidget(int index) const;
247 /// Obtain the index of a given dock widget
248 int dockWidgetIndex(QDockWidget*) const;
249
250 /// Set the title bar for this tab widget
251 void setTitleBar(QWidget*);
252
253 /// Get the splitter
254 QSplitter* getSplitter() const
255 {
256 return splitter;
257 }
258 /// Get the title bar
259 QWidget* getTitleBar() const
260 {
261 return titleBar;
262 }
263
264 /// Get the docking position of this tab widget
265 Qt::DockWidgetArea getDockArea() const
266 {
267 return dockArea;
268 }
269
270 /// Get delay time for animated reveal
271 const QTime& getRevealTime() const
272 {
273 return revealTime;
274 }
275 /// Set delay time for animated reveal
276 void setRevealTime(const QTime& time);
277
278 /// Restore state
279 void restore(ParameterGrp::handle handle);
280 /// Save tab orders and positions
281 void saveTabs();
282
283 /** @name Graphics effect properties setters and getters */
284 //@{
285 QColor effectColor() const;
286 void setEffectColor(const QColor&);
287 int effectWidth() const;
288 void setEffectWidth(int);
289 int effectHeight() const;
290 void setEffectHeight(int);
291 qreal effectOffsetX() const;
292 void setEffectOffsetX(qreal);
293 qreal effectOffsetY() const;
294 void setEffectOffsetY(qreal);
295 qreal effectBlurRadius() const;
296 void setEffectBlurRadius(qreal);
297 bool effectEnabled() const;
298 void setEffectEnabled(bool);
299 qreal animation() const
300 {
301 return _animation;
302 }
303 void setAnimation(qreal);
304 //@}
305
306 /// Schedule for repaint
307 void scheduleRepaint();
308
309 /** Return the pixel alpha value at the give position
310 * @param pos: position
311 * @param radiusScale: scale of the radius to check for alpha.
312 *
313 * @return Returns the largest alpha value of a circular area of 'pos' as
314 * center and radius as defined by user parameter
315 * View/DockOverlayAlphaRadius. May return -1 if out side of the widget, or
316 * zero if transparent.
317 */
318 int testAlpha(const QPoint& pos, int radiusScale);
319
320 /// Start animated showing
321 void startShow();
322 /// Start animated hiding
323 void startHide();
324
325 /// Internal state of the tab widget
326 enum class State
327 {
328 /// The tab widget is showing
329 Showing,
330 /// Normal visible state
331 Normal,
332 /// Visual hint is visible
333 Hint,
334 /// Hint is hidden by user after pressing ESC
335 HintHidden,
336 /// The tab widget is explicitly hidden by user
337 Hidden,
338 };
339 /// Set state of the tab widget
340 void setState(State);
341 /// Get the state of the widget
342 State getState() const
343 {
344 return _state;
345 }
346
347 /// Handle splitter resize
348 void onSplitterResize(int index);
349
350 /// Check if the tab widget is saving its state
351 bool isSaving() const
352 {
353 return _saving;
354 }
355
356 /// Helper function to create title bar for a dock widget
357 static QWidget* createTitleButton(QAction* action, int size);
358 /// Helper function to prepare a widget as a title widget
359 static QLayoutItem* prepareTitleWidget(QWidget* widget, const QList<QAction*>& actions);
360
361 protected:
362 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
363 void enterEvent(QEvent* ev) override;
364 #else
365 void enterEvent(QEnterEvent* ev) override;
366 #endif
367 void leaveEvent(QEvent* ev) override;
368 void changeEvent(QEvent* ev) override;
369 void resizeEvent(QResizeEvent* ev) override;
370 void paintEvent(QPaintEvent* ev) override;
371 bool event(QEvent* ev) override;
372 bool eventFilter(QObject* obj, QEvent* ev) override;
373
374 void retranslate();
375 void refreshIcons();
376
377 /// Overlay mode options
378 enum class OverlayOption
379 {
380 /// Enable overlay
381 Enable,
382 /// Disable overlay
383 Disable,
384 /// Enable overlay and show tab bar
385 ShowTab,
386 };
387 /// Toggle overlay mode for a given widget
388 void setOverlayMode(QWidget* widget, OverlayOption option);
389 /// Helper function to set overlay mode for a give widget
390 static void _setOverlayMode(QWidget* widget, OverlayOption option);
391
392 protected:
393 void onCurrentChanged(int index);
394 void onTabMoved(int from, int to);
395 void onRepaint();
396 void onAnimationStateChanged();
397 void setupLayout();
398 void onSizeGripMove(const QPoint&);
399
400 private:
401 friend class OverlayProxyWidget;
402 friend class OverlayTitleBar;
403 friend class OverlayManager;
404 friend class OverlayManager::Private;
405 friend class OverlaySplitterHandle;
406 friend class OverlaySizeGrip;
407
408 QSize offset;
409 int sizeDelta = 0;
410 QRect rectOverlay;
411 OverlayProxyWidget* proxyWidget;
412 QSplitter* splitter = nullptr;
413 QWidget* titleBar = nullptr;
414 QAction actNoAutoMode;
415 QAction actAutoHide;
416 QAction actEditHide;
417 QAction actEditShow;
418 QAction actTaskShow;
419 QAction actAutoMode;
420 QMenu autoModeMenu;
421 QAction actTransparent;
422 QAction actIncrease;
423 QAction actDecrease;
424 QAction actOverlay;
425 QTimer timer;
426 QTimer repaintTimer;
427 AutoMode autoMode = AutoMode::NoAutoMode;
428 bool repainting = false;
429 bool overlaid = false;
430 bool currentTransparent = false;
431 bool touched = false;
432 bool busy = false;
433 Qt::DockWidgetArea dockArea;
434 int tabSize = 0;
435 QTime revealTime;
436 ParameterGrp::handle hGrp;
437
438 OverlayGraphicsEffect* _graphicsEffect = nullptr;
439 OverlayGraphicsEffect* _graphicsEffectTab = nullptr;
440 bool _effectEnabled = false;
441
442 QImage _image;
443 qreal _imageScale;
444
445 qreal _animation = 0;
446 QPropertyAnimation* _animator = nullptr;
447
448 State _state = State::Normal;
449
450 std::map<QDockWidget*, int> _sizemap;
451 bool _saving = false;
452
453 // NOLINTBEGIN
454 static OverlayDragFrame* _DragFrame;
455 static QDockWidget* _DragFloating;
456 static QWidget* _Dragging;
457 static OverlayTabWidget* _LeftOverlay;
458 static OverlayTabWidget* _RightOverlay;
459 static OverlayTabWidget* _TopOverlay;
460 static OverlayTabWidget* _BottomOverlay;
461 // NOLINTEND
462 };
463
464 /// A translucent frame as a visual indicator when dragging a dock widget
465 class OverlayDragFrame: public QWidget
466 {
467 Q_OBJECT
468 public:
469 explicit OverlayDragFrame(QWidget* parent);
470 QSize sizeHint() const override;
471 QSize minimumSizeHint() const override;
472
473 protected:
474 void paintEvent(QPaintEvent* ev) override;
475 };
476
477 /// Title bar for OverlayTabWidget
478 class OverlayTitleBar: public QWidget
479 {
480 Q_OBJECT
481 public:
482 explicit OverlayTitleBar(QWidget* parent);
483 void setTitleItem(QLayoutItem*);
484 void endDrag();
485
486 protected:
487 void mouseMoveEvent(QMouseEvent* ev) override;
488 void mousePressEvent(QMouseEvent* ev) override;
489 void mouseReleaseEvent(QMouseEvent* ev) override;
490 void paintEvent(QPaintEvent* ev) override;
491 void keyPressEvent(QKeyEvent* ke) override;
492 void timerEvent(QTimerEvent* te) override;
493
494 private:
495 QPoint dragOffset;
496 QSize dragSize;
497 QLayoutItem* titleItem = nullptr;
498 QColor textcolor;
499 int timerId = 0;
500 bool blink = false;
501 bool mouseMovePending = false;
502 bool ignoreMouse = false;
503 };
504
505 /// Size grip for title bar and split handler of OverlayTabWidget
506 class OverlaySizeGrip: public QWidget
507 {
508 Q_OBJECT
509 public:
510 OverlaySizeGrip(QWidget* parent, bool vertical);
511
512 Q_SIGNALS:
513 void dragMove(const QPoint& globalPos);
514
515 protected:
516 void paintEvent(QPaintEvent* ev) override;
517 void mouseMoveEvent(QMouseEvent* ev) override;
518 void mousePressEvent(QMouseEvent* ev) override;
519 void mouseReleaseEvent(QMouseEvent* ev) override;
520 const QPixmap& pixmap() const;
521
522 private:
523 bool vertical;
524 };
525
526 /// Splitter for OverlayTabWidget
527 class OverlaySplitter: public QSplitter
528 {
529 Q_OBJECT
530 public:
531 explicit OverlaySplitter(QWidget* parent);
532 void retranslate();
533
534 protected:
535 QSplitterHandle* createHandle() override;
536 };
537
538
539 /// Splitter handle for dragging the splitter
540 class OverlaySplitterHandle: public QSplitterHandle
541 {
542 Q_OBJECT
543 public:
544 friend class OverlaySplitter;
545
546 OverlaySplitterHandle(Qt::Orientation, QSplitter* parent);
547 void setTitleItem(QLayoutItem*);
548 void retranslate();
549 void refreshIcons();
550 QDockWidget* dockWidget();
551
552 void showTitle(bool enable);
553 bool isShowing() const
554 {
555 return _showTitle;
556 }
557 void endDrag();
558
559 protected:
560 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
561 void enterEvent(QEvent* ev) override;
562 #else
563 void enterEvent(QEnterEvent* ev) override;
564 #endif
565 void showEvent(QShowEvent* ev) override;
566 void leaveEvent(QEvent* ev) override;
567 void paintEvent(QPaintEvent* ev) override;
568 void changeEvent(QEvent* ev) override;
569 void mouseMoveEvent(QMouseEvent* ev) override;
570 void mousePressEvent(QMouseEvent* ev) override;
571 void mouseReleaseEvent(QMouseEvent* ev) override;
572 void keyPressEvent(QKeyEvent* ev) override;
573 QSize sizeHint() const override;
574
575 protected:
576 void onAction();
577 void onTimer();
578
579 private:
580 QLayoutItem* titleItem = nullptr;
581 int idx = -1;
582 QAction actFloat;
583 bool _showTitle = true;
584 int dragging = 0;
585 QPoint dragOffset;
586 QSize dragSize;
587 QTimer timer;
588 };
589
590
591 /// Tool button for the title bar of the OverlayTabWidget
592 class OverlayToolButton: public QToolButton
593 {
594 Q_OBJECT
595 public:
596 explicit OverlayToolButton(QWidget* parent);
597 };
598
599 /** Class for handling visual hint for bringing back hidden overlay dock widget
600 *
601 * The proxy widget is transparent except a customizable rectangle area with a
602 * selectable color shown as the visual hint. The hint is normally hidden, and
603 * is shown only if the mouse hovers within the widget. When the hint area is
604 * clicked, it will bring back hidden overlay dock panel. Note that the proxy
605 * widget itself is mouse transparent as well, meaning that it will not receive
606 * any mouse event. It is handled in the OverlayManager event filter.
607 */
608 class OverlayProxyWidget: public QWidget
609 {
610 Q_OBJECT
611
612 Q_PROPERTY(QBrush hintColor READ hintColor WRITE setHintColor) // clazy:exclude=qproperty-without-notify
613
614 public:
615 explicit OverlayProxyWidget(OverlayTabWidget*);
616
617 OverlayTabWidget* getOwner() const
618 {
619 return owner;
620 }
621
622 /// For representing hit region
623 enum class HitTest
624 {
625 /// Not hitting
626 HitNone = 0,
627 /// Hitting the proxy widget size but not within the visible hint area.
628 HitOuter = 1,
629 /// Hitting the visible hint area.
630 HitInner = 2,
631 };
632 /** Mouse cursor hit test
633 * @param pos: cursor position
634 * @param delay: Whether to delay showing hint on mouse hit
635 */
636 HitTest hitTest(const QPoint& pos, bool delay = true);
637
638 /// Check if the visual hint is showing
639 bool isActivated() const;
640
641 QBrush hintColor() const;
642 void setHintColor(const QBrush&);
643
644 QRect getRect() const;
645
646 void onMousePress();
647
648 protected:
649 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
650 void enterEvent(QEvent* ev) override;
651 #else
652 void enterEvent(QEnterEvent* ev) override;
653 #endif
654 void hideEvent(QHideEvent* ev) override;
655 void paintEvent(QPaintEvent* ev) override;
656
657 protected:
658 void onTimer();
659
660 private:
661 OverlayTabWidget* owner;
662 bool drawLine = false;
663 int dockArea;
664 QTimer timer;
665 QBrush _hintColor;
666 };
667
668 /** Graphic effects for drawing shadow and outline of text on transparent background
669 *
670 * Modified from https://stackoverflow.com/a/23752747
671 */
672 class OverlayGraphicsEffect: public QGraphicsEffect
673 {
674 Q_OBJECT
675 public:
676 explicit OverlayGraphicsEffect(QObject* parent);
677
678 void draw(QPainter* painter) override;
679 QRectF boundingRectFor(const QRectF& rect) const override;
680
681 inline void setSize(const QSize& size)
682 {
683 if (_size != size) {
684 _size = size;
685 updateBoundingRect();
686 }
687 }
688
689 inline QSize size() const
690 {
691 return _size;
692 }
693
694 inline void setOffset(const QPointF& offset)
695 {
696 if (_offset != offset) {
697 _offset = offset;
698 updateBoundingRect();
699 }
700 }
701
702 inline QPointF offset() const
703 {
704 return _offset;
705 }
706
707 inline void setBlurRadius(qreal blurRadius)
708 {
709 if (_blurRadius != blurRadius) {
710 _blurRadius = blurRadius;
711 updateBoundingRect();
712 }
713 }
714
715 inline qreal blurRadius() const
716 {
717 return _blurRadius;
718 }
719
720 inline void setColor(const QColor& color)
721 {
722 _color = color;
723 }
724 inline QColor color() const
725 {
726 return _color;
727 }
728
729 inline bool enabled() const
730 {
731 return _enabled;
732 }
733 inline void setEnabled(bool enabled)
734 {
735 if (_enabled != enabled) {
736 _enabled = enabled;
737 updateBoundingRect();
738 }
739 }
740
741 private:
742 bool _enabled;
743 QSize _size;
744 qreal _blurRadius;
745 QColor _color;
746 QPointF _offset;
747 };
748
749 } // namespace Gui
750
751 #endif // FC_OVERLAYWIDGETS_H