1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 Uwe Kindler
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 or (at your option) any later version.
19 ** The licenses are as published by the Free Software Foundation
20 ** and appearing in the file LICENSE.LGPLv21 included in the packaging
21 ** of this file. Please review the following information to ensure
22 ** the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 3 or (at your option) any later version
28 ** approved by the KDE Free Qt Foundation. The licenses are as published by
29 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
30 ** included in the packaging of this file. Please review the following
31 ** information to ensure the GNU General Public License requirements will
32 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
33 **
34 ****************************************************************************/
35 
36 #pragma once
37 
38 #include <QDebug>
39 #include <QPair>
40 #include <QPixmap>
41 #include <QStyle>
42 #include <QWidget>
43 #include <QtCore/QtGlobal>
44 
45 QT_BEGIN_NAMESPACE
46 class QAbstractButton;
47 class QSplitter;
48 QT_END_NAMESPACE
49 
50 #ifndef ADS_STATIC
51 #ifdef ADVANCEDDOCKINGSYSTEM_LIBRARY
52 #define ADS_EXPORT Q_DECL_EXPORT
53 #else
54 #define ADS_EXPORT Q_DECL_IMPORT
55 #endif
56 #else
57 #define ADS_EXPORT
58 #endif
59 
60 //#define ADS_DEBUG_PRINT
61 
62 // Define ADS_DEBUG_PRINT to enable a lot of debug output
63 #ifdef ADS_DEBUG_PRINT
64 #define ADS_PRINT(s) qDebug() << s
65 #else
66 #define ADS_PRINT(s)
67 #endif
68 
69 // Set ADS_DEBUG_LEVEL to enable additional debug output and to enable layout
70 // dumps to qDebug and std::cout after layout changes
71 #define ADS_DEBUG_LEVEL 0
72 
73 namespace ADS {
74 
75 class DockSplitter;
76 
77 enum DockWidgetArea {
78     NoDockWidgetArea = 0x00,
79     LeftDockWidgetArea = 0x01,
80     RightDockWidgetArea = 0x02,
81     TopDockWidgetArea = 0x04,
82     BottomDockWidgetArea = 0x08,
83     CenterDockWidgetArea = 0x10,
84 
85     InvalidDockWidgetArea = NoDockWidgetArea,
86     OuterDockAreas = TopDockWidgetArea | LeftDockWidgetArea | RightDockWidgetArea
87                      | BottomDockWidgetArea,
88     AllDockAreas = OuterDockAreas | CenterDockWidgetArea
89 };
90 Q_DECLARE_FLAGS(DockWidgetAreas, DockWidgetArea)
91 
92 enum eTitleBarButton { TitleBarButtonTabsMenu, TitleBarButtonUndock, TitleBarButtonClose };
93 
94 /**
95  * The different dragging states
96  */
97 enum eDragState {
98     DraggingInactive,      //!< DraggingInactive
99     DraggingMousePressed,  //!< DraggingMousePressed
100     DraggingTab,           //!< DraggingTab
101     DraggingFloatingWidget //!< DraggingFloatingWidget
102 };
103 
104 /**
105  * The different icons used in the UI
106  */
107 enum eIcon {
108     TabCloseIcon,            //!< TabCloseIcon
109     DockAreaMenuIcon,        //!< DockAreaMenuIcon
110     DockAreaUndockIcon,      //!< DockAreaUndockIcon
111     DockAreaCloseIcon,       //!< DockAreaCloseIcon
112     FloatingWidgetCloseIcon, //!< FloatingWidgetCloseIcon
113 
114     IconCount, //!< just a delimiter for range checks
115 };
116 
117 /**
118  * For bitwise combination of dock wdget features
119  */
120 enum eBitwiseOperator
121 {
122     BitwiseAnd,
123     BitwiseOr
124 };
125 
126 namespace internal {
127 const char *const closedProperty = "close";
128 const char *const dirtyProperty = "dirty";
129 
130 /**
131  * Replace the from widget in the given splitter with the To widget
132  */
133 void replaceSplitterWidget(QSplitter *splitter, QWidget *from, QWidget *to);
134 
135 /**
136  * This function walks the splitter tree upwards to hides all splitters
137  * that do not have visible content
138  */
139 void hideEmptyParentSplitters(DockSplitter *firstParentSplitter);
140 
141 /**
142  * Convenience class for QPair to provide better naming than first and
143  * second
144  */
145 class DockInsertParam : public QPair<Qt::Orientation, bool>
146 {
147 public:
DockInsertParam(Qt::Orientation orientation,bool append)148     DockInsertParam(Qt::Orientation orientation, bool append)
149         : QPair<Qt::Orientation, bool>(orientation, append)
150     {}
151 
orientation()152     Qt::Orientation orientation() const { return this->first; }
append()153     bool append() const { return this->second; }
insertOffset()154     int insertOffset() const { return append() ? 1 : 0; }
155 };
156 
157 /**
158  * Returns the insertion parameters for the given dock area
159  */
160 DockInsertParam dockAreaInsertParameters(DockWidgetArea area);
161 
162 /**
163  * Searches for the parent widget of the given type.
164  * Returns the parent widget of the given widget or 0 if the widget is not
165  * child of any widget of type T
166  *
167  * It is not safe to use this function in in DockWidget because only
168  * the current dock widget has a parent. All dock widgets that are not the
169  * current dock widget in a dock area have no parent.
170  */
171 template<class T>
findParent(const QWidget * widget)172 T findParent(const QWidget *widget)
173 {
174     QWidget *parentWidget = widget->parentWidget();
175     while (parentWidget) {
176         T parentImpl = qobject_cast<T>(parentWidget);
177         if (parentImpl) {
178             return parentImpl;
179         }
180         parentWidget = parentWidget->parentWidget();
181     }
182     return 0;
183 }
184 
185 /**
186  * Creates a semi transparent pixmap from the given pixmap Source.
187  * The Opacity parameter defines the opacity from completely transparent (0.0)
188  * to completely opaque (1.0)
189  */
190 QPixmap createTransparentPixmap(const QPixmap &source, qreal opacity);
191 
192 /**
193  * Helper function for settings flags in a QFlags instance.
194  */
195 template<class T>
196 void setFlag(T &flags, typename T::enum_type flag, bool on = true)
197 {
198     flags.setFlag(flag, on);
199 }
200 
201 /**
202  * Helper function for settings tooltips without cluttering the code with
203  * tests for preprocessor macros
204  */
205 template <class QObjectPtr>
setToolTip(QObjectPtr obj,const QString & tip)206 void setToolTip(QObjectPtr obj, const QString &tip)
207 {
208 #ifndef QT_NO_TOOLTIP
209     obj->setToolTip(tip);
210 #else
211     Q_UNUSED(obj);
212     Q_UNUSED(tip);
213 #endif
214 }
215 
216 /**
217  * Helper function to set the icon of a certain button.
218  * Use this function to set the icons for the dock area and dock widget buttons.
219  * The function first uses the CustomIconId to get an icon from the
220  * IconProvider. You can register your custom icons with the icon provider, if
221  * you do not want to use the default buttons and if you do not want to use
222  * stylesheets.
223  * If the IconProvider does not return a valid icon (icon is null), the function
224  * fetches the given standard pixmap from the QStyle.
225  * param[in] Button The button whose icons are to be set
226  * param[in] StandardPixmap The standard pixmap to be used for the button
227  * param[in] CustomIconId The identifier for the custom icon.
228  */
229 void setButtonIcon(QAbstractButton *button, QStyle::StandardPixmap standarPixmap,
230     ADS::eIcon CustomIconId);
231 
232 enum eRepolishChildOptions
233 {
234     RepolishIgnoreChildren,
235     RepolishDirectChildren,
236     RepolishChildrenRecursively
237 };
238 
239 /**
240  * Calls unpolish() / polish for the style of the given widget to update
241  * stylesheet if a property changes
242  */
243 void repolishStyle(QWidget *widget, eRepolishChildOptions options = RepolishIgnoreChildren);
244 
245 } // namespace internal
246 } // namespace ADS
247