Implemented custom close handling

This commit is contained in:
Uwe Kindler 2020-01-06 11:42:36 +01:00
parent 0305d8a221
commit a5e8011222
10 changed files with 449 additions and 376 deletions

View File

@ -53,6 +53,7 @@
#include <QRubberBand> #include <QRubberBand>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QTableWidget> #include <QTableWidget>
#include <QMessageBox>
#include <QMap> #include <QMap>
#include <QElapsedTimer> #include <QElapsedTimer>
@ -171,6 +172,7 @@ static ads::CDockWidget* createEditorWidget(QMenu* ViewMenu)
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Editor %1").arg(EditorCount++)); ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Editor %1").arg(EditorCount++));
DockWidget->setWidget(w); DockWidget->setWidget(w);
DockWidget->setIcon(svgIcon(":/adsdemo/images/edit.svg")); DockWidget->setIcon(svgIcon(":/adsdemo/images/edit.svg"));
DockWidget->setFeature(ads::CDockWidget::CustomCloseHandling, true);
ViewMenu->addAction(DockWidget->toggleViewAction()); ViewMenu->addAction(DockWidget->toggleViewAction());
return DockWidget; return DockWidget;
} }
@ -475,6 +477,21 @@ void CMainWindow::createEditor()
DockWidget->setFeature(ads::CDockWidget::DockWidgetDeleteOnClose, true); DockWidget->setFeature(ads::CDockWidget::DockWidgetDeleteOnClose, true);
auto FloatingWidget = d->DockManager->addDockWidgetFloating(DockWidget); auto FloatingWidget = d->DockManager->addDockWidgetFloating(DockWidget);
FloatingWidget->move(QPoint(20, 20)); FloatingWidget->move(QPoint(20, 20));
connect(DockWidget, SIGNAL(closeRequested()), SLOT(onEditorCloseRequested()));
}
//============================================================================
void CMainWindow::onEditorCloseRequested()
{
auto DockWidget = qobject_cast<ads::CDockWidget*>(sender());
int Result = QMessageBox::question(this, "Close Editor", QString("Editor %1 "
"contains unsaved changes? Would you like to close it?")
.arg(DockWidget->windowTitle()));
if (QMessageBox::Yes == Result)
{
DockWidget->closeDockWidget();
}
} }

View File

@ -61,6 +61,7 @@ private slots:
void onViewToggled(bool Open); void onViewToggled(bool Open);
void createEditor(); void createEditor();
void createTable(); void createTable();
void onEditorCloseRequested();
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -478,7 +478,14 @@ void CDockAreaTabBar::onCloseOtherTabsRequested()
int Offset = Tab->dockWidget()->features().testFlag( int Offset = Tab->dockWidget()->features().testFlag(
CDockWidget::DockWidgetDeleteOnClose) ? 1 : 0; CDockWidget::DockWidgetDeleteOnClose) ? 1 : 0;
closeTab(i); closeTab(i);
i -= Offset;
// If the the dock widget blocks closing, i.e. if the flag
// CustomCloseHandling is set, and the dock widget is still open,
// then we do not need to correct the index
if (Tab->dockWidget()->isClosed())
{
i -= Offset;
}
} }
} }
} }
@ -569,7 +576,7 @@ void CDockAreaTabBar::closeTab(int Index)
{ {
return; return;
} }
Tab->hide(); //Tab->hide();
emit tabCloseRequested(Index); emit tabCloseRequested(Index);
} }

View File

@ -495,7 +495,7 @@ void CDockAreaWidget::onTabCloseRequested(int Index)
if (DockWidget->features().testFlag(CDockWidget::DockWidgetDeleteOnClose)) if (DockWidget->features().testFlag(CDockWidget::DockWidgetDeleteOnClose))
{ {
//DockWidget->deleteDockWidget(); //DockWidget->deleteDockWidget();
DockWidget->closeDockWidget(); DockWidget->closeDockWidgetInternal();
} }
else else
{ {
@ -758,15 +758,26 @@ CDockWidget* CDockAreaWidget::nextOpenDockWidget(CDockWidget* DockWidget) const
//============================================================================ //============================================================================
CDockWidget::DockWidgetFeatures CDockAreaWidget::features() const CDockWidget::DockWidgetFeatures CDockAreaWidget::features(eBitwiseOperator Mode) const
{ {
CDockWidget::DockWidgetFeatures Features(CDockWidget::AllDockWidgetFeatures); if (BitwiseAnd == Mode)
for (const auto DockWidget : dockWidgets())
{ {
Features &= DockWidget->features(); CDockWidget::DockWidgetFeatures Features(CDockWidget::AllDockWidgetFeatures);
for (const auto DockWidget : dockWidgets())
{
Features &= DockWidget->features();
}
return Features;
}
else
{
CDockWidget::DockWidgetFeatures Features(CDockWidget::NoDockWidgetFeatures);
for (const auto DockWidget : dockWidgets())
{
Features |= DockWidget->features();
}
return Features;
} }
return Features;
} }
@ -805,7 +816,7 @@ void CDockAreaWidget::closeArea()
auto OpenDockWidgets = openedDockWidgets(); auto OpenDockWidgets = openedDockWidgets();
if (OpenDockWidgets.count() == 1 && OpenDockWidgets[0]->features().testFlag(CDockWidget::DockWidgetDeleteOnClose)) if (OpenDockWidgets.count() == 1 && OpenDockWidgets[0]->features().testFlag(CDockWidget::DockWidgetDeleteOnClose))
{ {
OpenDockWidgets[0]->deleteDockWidget(); OpenDockWidgets[0]->closeDockWidgetInternal();
} }
else else
{ {

View File

@ -238,10 +238,10 @@ public:
* A bitwise and is used to combine the flags of all dock widgets. That * A bitwise and is used to combine the flags of all dock widgets. That
* means, if only one single dock widget does not support a certain flag, * means, if only one single dock widget does not support a certain flag,
* the whole dock are does not support the flag. I.e. if one single * the whole dock are does not support the flag. I.e. if one single
* dock widget in this area is not closabe, the whole dock are is not * dock widget in this area is not closable, the whole dock are is not
* closable. * closable.
*/ */
CDockWidget::DockWidgetFeatures features() const; CDockWidget::DockWidgetFeatures features(eBitwiseOperator Mode = BitwiseAnd) const;
/** /**
* Returns the title bar button corresponding to the given title bar * Returns the title bar button corresponding to the given title bar

View File

@ -1713,10 +1713,24 @@ void CDockContainerWidget::closeOtherAreas(CDockAreaWidget* KeepOpenArea)
{ {
for (const auto DockArea : d->DockAreas) for (const auto DockArea : d->DockAreas)
{ {
if (DockArea != KeepOpenArea && DockArea->features().testFlag(CDockWidget::DockWidgetClosable)) if (DockArea == KeepOpenArea)
{ {
DockArea->closeArea(); continue;
} }
if (!DockArea->features(BitwiseAnd).testFlag(CDockWidget::DockWidgetClosable))
{
continue;
}
// We do not close areas with widgets with custom close handling
if (DockArea->features(BitwiseOr).testFlag(CDockWidget::CustomCloseHandling))
{
std::cout << "CDockWidget::CustomCloseHandling" << std::endl;
continue;
}
DockArea->closeArea();
} }
} }

View File

@ -64,7 +64,7 @@ struct DockWidgetPrivate
QBoxLayout* Layout = nullptr; QBoxLayout* Layout = nullptr;
QWidget* Widget = nullptr; QWidget* Widget = nullptr;
CDockWidgetTab* TabWidget = nullptr; CDockWidgetTab* TabWidget = nullptr;
CDockWidget::DockWidgetFeatures Features = CDockWidget::AllDockWidgetFeatures; CDockWidget::DockWidgetFeatures Features = CDockWidget::DefaultDockWidgetFeatures;
CDockManager* DockManager = nullptr; CDockManager* DockManager = nullptr;
CDockAreaWidget* DockArea = nullptr; CDockAreaWidget* DockArea = nullptr;
QAction* ToggleViewAction = nullptr; QAction* ToggleViewAction = nullptr;
@ -744,42 +744,40 @@ void CDockWidget::deleteDockWidget()
{ {
dockManager()->removeDockWidget(this); dockManager()->removeDockWidget(this);
deleteLater(); deleteLater();
} d->Closed = true;
//============================================================================
bool CDockWidget::handleCloseRequest()
{
std::cout << "CDockWidget::handleCloseRequest()" << std::endl;
return true;
} }
//============================================================================ //============================================================================
void CDockWidget::closeDockWidget() void CDockWidget::closeDockWidget()
{ {
closeDockWidgetInternal(); closeDockWidgetInternal(true);
} }
//============================================================================ //============================================================================
bool CDockWidget::closeDockWidgetInternal() bool CDockWidget::closeDockWidgetInternal(bool ForceClose)
{ {
if (!ForceClose)
{
emit closeRequested();
}
if (!ForceClose && features().testFlag(CDockWidget::CustomCloseHandling))
{
return false;
}
if (features().testFlag(CDockWidget::DockWidgetDeleteOnClose)) if (features().testFlag(CDockWidget::DockWidgetDeleteOnClose))
{ {
if (handleCloseRequest()) deleteDockWidget();
{
deleteDockWidget();
return true;
}
} }
else else
{ {
toggleView(false); toggleView(false);
return true;
} }
return false; return true;
} }

View File

@ -53,414 +53,421 @@ class CFloatingDockContainer;
*/ */
class ADS_EXPORT CDockWidget : public QFrame class ADS_EXPORT CDockWidget : public QFrame
{ {
Q_OBJECT Q_OBJECT
private: private:
DockWidgetPrivate* d; ///< private data (pimpl) DockWidgetPrivate* d; ///< private data (pimpl)
friend struct DockWidgetPrivate; friend struct DockWidgetPrivate;
private slots: private slots:
/** /**
* Adjusts the toolbar icon sizes according to the floating state * Adjusts the toolbar icon sizes according to the floating state
*/ */
void setToolbarFloatingStyle(bool topLevel); void setToolbarFloatingStyle(bool topLevel);
protected: protected:
friend class CDockContainerWidget; friend class CDockContainerWidget;
friend class CDockAreaWidget; friend class CDockAreaWidget;
friend class CFloatingDockContainer; friend class CFloatingDockContainer;
friend class CDockManager; friend class CDockManager;
friend struct DockManagerPrivate; friend struct DockManagerPrivate;
friend class DockContainerWidgetPrivate; friend class DockContainerWidgetPrivate;
friend class CDockAreaTabBar; friend class CDockAreaTabBar;
friend class CDockWidgetTab; friend class CDockWidgetTab;
friend struct DockWidgetTabPrivate; friend struct DockWidgetTabPrivate;
/** /**
* Assigns the dock manager that manages this dock widget * Assigns the dock manager that manages this dock widget
*/ */
void setDockManager(CDockManager* DockManager); void setDockManager(CDockManager* DockManager);
/** /**
* If this dock widget is inserted into a dock area, the dock area will * If this dock widget is inserted into a dock area, the dock area will
* be registered on this widget via this function. If a dock widget is * be registered on this widget via this function. If a dock widget is
* removed from a dock area, this function will be called with nullptr * removed from a dock area, this function will be called with nullptr
* value. * value.
*/ */
void setDockArea(CDockAreaWidget* DockArea); void setDockArea(CDockAreaWidget* DockArea);
/** /**
* This function changes the toggle view action without emitting any * This function changes the toggle view action without emitting any
* signal * signal
*/ */
void setToggleViewActionChecked(bool Checked); void setToggleViewActionChecked(bool Checked);
/** /**
* Saves the state into the given stream * Saves the state into the given stream
*/ */
void saveState(QXmlStreamWriter& Stream) const; void saveState(QXmlStreamWriter& Stream) const;
/** /**
* This is a helper function for the dock manager to flag this widget * This is a helper function for the dock manager to flag this widget
* as unassigned. * as unassigned.
* When calling the restore function, it may happen, that the saved state * When calling the restore function, it may happen, that the saved state
* contains less dock widgets then currently available. All widgets whose * contains less dock widgets then currently available. All widgets whose
* data is not contained in the saved state, are flagged as unassigned * data is not contained in the saved state, are flagged as unassigned
* after the restore process. If the user shows an unassigned dock widget, * after the restore process. If the user shows an unassigned dock widget,
* a floating widget will be created to take up the dock widget. * a floating widget will be created to take up the dock widget.
*/ */
void flagAsUnassigned(); void flagAsUnassigned();
/** /**
* Call this function to emit a topLevelChanged() signal and to update * Call this function to emit a topLevelChanged() signal and to update
* the dock area tool bar visibility * the dock area tool bar visibility
*/ */
static void emitTopLevelEventForWidget(CDockWidget* TopLevelDockWidget, bool Floating); static void emitTopLevelEventForWidget(CDockWidget* TopLevelDockWidget, bool Floating);
/** /**
* Use this function to emit a top level changed event. * Use this function to emit a top level changed event.
* Do never use emit topLevelChanged(). Always use this function because * Do never use emit topLevelChanged(). Always use this function because
* it only emits a signal if the floating state has really changed * it only emits a signal if the floating state has really changed
*/ */
void emitTopLevelChanged(bool Floating); void emitTopLevelChanged(bool Floating);
/** /**
* Internal function for modifying the closed state when restoring * Internal function for modifying the closed state when restoring
* a saved docking state * a saved docking state
*/ */
void setClosedState(bool Closed); void setClosedState(bool Closed);
/** /**
* Internal toggle view function that does not check if the widget * Internal toggle view function that does not check if the widget
* already is in the given state * already is in the given state
*/ */
void toggleViewInternal(bool Open); void toggleViewInternal(bool Open);
/** /**
* Internal close dock widget implementation. * Internal close dock widget implementation.
* The function returns true if the dock widget has been closed or hidden * The function returns true if the dock widget has been closed or hidden
*/ */
bool closeDockWidgetInternal(); bool closeDockWidgetInternal(bool ForceClose = false);
public: public:
using Super = QFrame; using Super = QFrame;
enum DockWidgetFeature enum DockWidgetFeature
{ {
DockWidgetClosable = 0x01, DockWidgetClosable = 0x01,
DockWidgetMovable = 0x02,///< this feature is not properly implemented yet and is ignored DockWidgetMovable = 0x02,///< this feature is not properly implemented yet and is ignored
DockWidgetFloatable = 0x04, DockWidgetFloatable = 0x04,
DockWidgetDeleteOnClose = 0x08, ///< deletes the dock widget when it is closed DockWidgetDeleteOnClose = 0x08, ///< deletes the dock widget when it is closed
AllDockWidgetFeatures = DockWidgetClosable | DockWidgetMovable | DockWidgetFloatable, CustomCloseHandling = 0x10,
NoDockWidgetFeatures = 0x00 DefaultDockWidgetFeatures = DockWidgetClosable | DockWidgetMovable | DockWidgetFloatable,
}; AllDockWidgetFeatures = DefaultDockWidgetFeatures | DockWidgetDeleteOnClose | CustomCloseHandling,
Q_DECLARE_FLAGS(DockWidgetFeatures, DockWidgetFeature) NoDockWidgetFeatures = 0x00
};
Q_DECLARE_FLAGS(DockWidgetFeatures, DockWidgetFeature)
enum eState enum eState
{ {
StateHidden, StateHidden,
StateDocked, StateDocked,
StateFloating StateFloating
}; };
/** /**
* Sets the widget for the dock widget to widget. * Sets the widget for the dock widget to widget.
* The InsertMode defines how the widget is inserted into the dock widget. * The InsertMode defines how the widget is inserted into the dock widget.
* The content of a dock widget should be resizable do a very small size to * The content of a dock widget should be resizable do a very small size to
* prevent the dock widget from blocking the resizing. To ensure, that a * prevent the dock widget from blocking the resizing. To ensure, that a
* dock widget can be resized very well, it is better to insert the content+ * dock widget can be resized very well, it is better to insert the content+
* widget into a scroll area or to provide a widget that is already a scroll * widget into a scroll area or to provide a widget that is already a scroll
* area or that contains a scroll area. * area or that contains a scroll area.
* If the InsertMode is AutoScrollArea, the DockWidget tries to automatically * If the InsertMode is AutoScrollArea, the DockWidget tries to automatically
* detect how to insert the given widget. If the widget is derived from * detect how to insert the given widget. If the widget is derived from
* QScrollArea (i.e. an QAbstractItemView), then the widget is inserted * QScrollArea (i.e. an QAbstractItemView), then the widget is inserted
* directly. If the given widget is not a scroll area, the widget will be * directly. If the given widget is not a scroll area, the widget will be
* inserted into a scroll area. * inserted into a scroll area.
* To force insertion into a scroll area, you can also provide the InsertMode * To force insertion into a scroll area, you can also provide the InsertMode
* ForceScrollArea. To prevent insertion into a scroll area, you can * ForceScrollArea. To prevent insertion into a scroll area, you can
* provide the InsertMode ForceNoScrollArea * provide the InsertMode ForceNoScrollArea
*/ */
enum eInsertMode enum eInsertMode
{ {
AutoScrollArea, AutoScrollArea,
ForceScrollArea, ForceScrollArea,
ForceNoScrollArea ForceNoScrollArea
}; };
/** /**
* This mode configures the behavior of the toggle view action. * This mode configures the behavior of the toggle view action.
* If the mode if ActionModeToggle, then the toggle view action is * If the mode if ActionModeToggle, then the toggle view action is
* a checkable action to show / hide the dock widget. If the mode * a checkable action to show / hide the dock widget. If the mode
* is ActionModeShow, then the action is not checkable an it will * is ActionModeShow, then the action is not checkable an it will
* always show the dock widget if clicked. If the mode is ActionModeShow, * always show the dock widget if clicked. If the mode is ActionModeShow,
* the user can only close the DockWidget with the close button. * the user can only close the DockWidget with the close button.
*/ */
enum eToggleViewActionMode enum eToggleViewActionMode
{ {
ActionModeToggle,//!< ActionModeToggle ActionModeToggle,//!< ActionModeToggle
ActionModeShow //!< ActionModeShow ActionModeShow //!< ActionModeShow
}; };
/** /**
* This constructor creates a dock widget with the given title. * This constructor creates a dock widget with the given title.
* The title is the text that is shown in the window title when the dock * The title is the text that is shown in the window title when the dock
* widget is floating and it is the title that is shown in the titlebar * widget is floating and it is the title that is shown in the titlebar
* or the tab of this dock widget if it is tabified. * or the tab of this dock widget if it is tabified.
* The object name of the dock widget is also set to the title. The * The object name of the dock widget is also set to the title. The
* object name is required by the dock manager to properly save and restore * object name is required by the dock manager to properly save and restore
* the state of the dock widget. That means, the title needs to be unique. * the state of the dock widget. That means, the title needs to be unique.
* If your title is not unique or if you would like to change the title * If your title is not unique or if you would like to change the title
* during runtime, you need to set a unique object name explicitely * during runtime, you need to set a unique object name explicitely
* by calling setObjectName() after construction. * by calling setObjectName() after construction.
* Use the layoutFlags to configure the layout of the dock widget. * Use the layoutFlags to configure the layout of the dock widget.
*/ */
CDockWidget(const QString &title, QWidget* parent = 0); CDockWidget(const QString &title, QWidget* parent = 0);
/** /**
* Virtual Destructor * Virtual Destructor
*/ */
virtual ~CDockWidget(); virtual ~CDockWidget();
/** /**
* We return a fixed minimum size hint for all dock widgets * We return a fixed minimum size hint for all dock widgets
*/ */
virtual QSize minimumSizeHint() const override; virtual QSize minimumSizeHint() const override;
/** /**
* Sets the widget for the dock widget to widget. * Sets the widget for the dock widget to widget.
* The InsertMode defines how the widget is inserted into the dock widget. * The InsertMode defines how the widget is inserted into the dock widget.
* The content of a dock widget should be resizable do a very small size to * The content of a dock widget should be resizable do a very small size to
* prevent the dock widget from blocking the resizing. To ensure, that a * prevent the dock widget from blocking the resizing. To ensure, that a
* dock widget can be resized very well, it is better to insert the content+ * dock widget can be resized very well, it is better to insert the content+
* widget into a scroll area or to provide a widget that is already a scroll * widget into a scroll area or to provide a widget that is already a scroll
* area or that contains a scroll area. * area or that contains a scroll area.
* If the InsertMode is AutoScrollArea, the DockWidget tries to automatically * If the InsertMode is AutoScrollArea, the DockWidget tries to automatically
* detect how to insert the given widget. If the widget is derived from * detect how to insert the given widget. If the widget is derived from
* QScrollArea (i.e. an QAbstractItemView), then the widget is inserted * QScrollArea (i.e. an QAbstractItemView), then the widget is inserted
* directly. If the given widget is not a scroll area, the widget will be * directly. If the given widget is not a scroll area, the widget will be
* inserted into a scroll area. * inserted into a scroll area.
* To force insertion into a scroll area, you can also provide the InsertMode * To force insertion into a scroll area, you can also provide the InsertMode
* ForceScrollArea. To prevent insertion into a scroll area, you can * ForceScrollArea. To prevent insertion into a scroll area, you can
* provide the InsertMode ForceNoScrollArea * provide the InsertMode ForceNoScrollArea
*/ */
void setWidget(QWidget* widget, eInsertMode InsertMode = AutoScrollArea); void setWidget(QWidget* widget, eInsertMode InsertMode = AutoScrollArea);
/** /**
* Remove the widget from the dock and give ownership back to the caller * Remove the widget from the dock and give ownership back to the caller
*/ */
QWidget* takeWidget(); QWidget* takeWidget();
/** /**
* Returns the widget for the dock widget. This function returns zero if * Returns the widget for the dock widget. This function returns zero if
* the widget has not been set. * the widget has not been set.
*/ */
QWidget* widget() const; QWidget* widget() const;
/** /**
* Returns the tab widget of this dock widget that is shown in the dock * Returns the tab widget of this dock widget that is shown in the dock
* area title bar * area title bar
*/ */
CDockWidgetTab* tabWidget() const; CDockWidgetTab* tabWidget() const;
/** /**
* Sets, whether the dock widget is movable, closable, and floatable. * Sets, whether the dock widget is movable, closable, and floatable.
*/ */
void setFeatures(DockWidgetFeatures features); void setFeatures(DockWidgetFeatures features);
/** /**
* Sets the feature flag for this dock widget if on is true; otherwise * Sets the feature flag for this dock widget if on is true; otherwise
* clears the flag. * clears the flag.
*/ */
void setFeature(DockWidgetFeature flag, bool on); void setFeature(DockWidgetFeature flag, bool on);
/** /**
* This property holds whether the dock widget is movable, closable, and * This property holds whether the dock widget is movable, closable, and
* floatable. * floatable.
* By default, this property is set to a combination of DockWidgetClosable, * By default, this property is set to a combination of DockWidgetClosable,
* DockWidgetMovable and DockWidgetFloatable. * DockWidgetMovable and DockWidgetFloatable.
*/ */
DockWidgetFeatures features() const; DockWidgetFeatures features() const;
/** /**
* Returns the dock manager that manages the dock widget or 0 if the widget * Returns the dock manager that manages the dock widget or 0 if the widget
* has not been assigned to any dock manager yet * has not been assigned to any dock manager yet
*/ */
CDockManager* dockManager() const; CDockManager* dockManager() const;
/** /**
* Returns the dock container widget this dock area widget belongs to or 0 * Returns the dock container widget this dock area widget belongs to or 0
* if this dock widget has not been docked yet * if this dock widget has not been docked yet
*/ */
CDockContainerWidget* dockContainer() const; CDockContainerWidget* dockContainer() const;
/** /**
* Returns the dock area widget this dock widget belongs to or 0 * Returns the dock area widget this dock widget belongs to or 0
* if this dock widget has not been docked yet * if this dock widget has not been docked yet
*/ */
CDockAreaWidget* dockAreaWidget() const; CDockAreaWidget* dockAreaWidget() const;
/** /**
* This property holds whether the dock widget is floating. * This property holds whether the dock widget is floating.
* A dock widget is only floating, if it is the one and only widget inside * A dock widget is only floating, if it is the one and only widget inside
* of a floating container. If there are more than one dock widget in a * of a floating container. If there are more than one dock widget in a
* floating container, the all dock widgets are docked and not floating. * floating container, the all dock widgets are docked and not floating.
*/ */
bool isFloating() const; bool isFloating() const;
/** /**
* This function returns true, if this dock widget is in a floating. * This function returns true, if this dock widget is in a floating.
* The function returns true, if the dock widget is floating and it also * The function returns true, if the dock widget is floating and it also
* returns true if it is docked inside of a floating container. * returns true if it is docked inside of a floating container.
*/ */
bool isInFloatingContainer() const; bool isInFloatingContainer() const;
/** /**
* Returns true, if this dock widget is closed. * Returns true, if this dock widget is closed.
*/ */
bool isClosed() const; bool isClosed() const;
/** /**
* Returns a checkable action that can be used to show or close this dock widget. * Returns a checkable action that can be used to show or close this dock widget.
* The action's text is set to the dock widget's window title. * The action's text is set to the dock widget's window title.
*/ */
QAction* toggleViewAction() const; QAction* toggleViewAction() const;
/** /**
* Configures the behavior of the toggle view action. * Configures the behavior of the toggle view action.
* \see eToggleViewActionMode for a detailed description * \see eToggleViewActionMode for a detailed description
*/ */
void setToggleViewActionMode(eToggleViewActionMode Mode); void setToggleViewActionMode(eToggleViewActionMode Mode);
/** /**
* Sets the dock widget icon that is shown in tabs and in toggle view * Sets the dock widget icon that is shown in tabs and in toggle view
* actions * actions
*/ */
void setIcon(const QIcon& Icon); void setIcon(const QIcon& Icon);
/** /**
* Returns the icon that has been assigned to the dock widget * Returns the icon that has been assigned to the dock widget
*/ */
QIcon icon() const; QIcon icon() const;
/** /**
* If the WithToolBar layout flag is enabled, then this function returns * If the WithToolBar layout flag is enabled, then this function returns
* the dock widget toolbar. If the flag is disabled, the function returns * the dock widget toolbar. If the flag is disabled, the function returns
* a nullptr. * a nullptr.
* This function returns the dock widget top tool bar. * This function returns the dock widget top tool bar.
* If no toolbar is assigned, this function returns nullptr. To get a vaild * If no toolbar is assigned, this function returns nullptr. To get a vaild
* toolbar you either need to create a default empty toolbar via * toolbar you either need to create a default empty toolbar via
* createDefaultToolBar() function or you need to assign you custom * createDefaultToolBar() function or you need to assign you custom
* toolbar via setToolBar(). * toolbar via setToolBar().
*/ */
QToolBar* toolBar() const; QToolBar* toolBar() const;
/** /**
* If you would like to use the default top tool bar, then call this * If you would like to use the default top tool bar, then call this
* function to create the default tool bar. * function to create the default tool bar.
* After this function the toolBar() function will return a valid toolBar() * After this function the toolBar() function will return a valid toolBar()
* object. * object.
*/ */
QToolBar* createDefaultToolBar(); QToolBar* createDefaultToolBar();
/** /**
* Assign a new tool bar that is shown above the content widget. * Assign a new tool bar that is shown above the content widget.
* The dock widget will become the owner of the tool bar and deletes it * The dock widget will become the owner of the tool bar and deletes it
* on destruction * on destruction
*/ */
void setToolBar(QToolBar* ToolBar); void setToolBar(QToolBar* ToolBar);
/** /**
* This function sets the tool button style for the given dock widget state. * This function sets the tool button style for the given dock widget state.
* It is possible to switch the tool button style depending on the state. * It is possible to switch the tool button style depending on the state.
* If a dock widget is floating, then here are more space and it is * If a dock widget is floating, then here are more space and it is
* possible to select a style that requires more space like * possible to select a style that requires more space like
* Qt::ToolButtonTextUnderIcon. For the docked state Qt::ToolButtonIconOnly * Qt::ToolButtonTextUnderIcon. For the docked state Qt::ToolButtonIconOnly
* might be better. * might be better.
*/ */
void setToolBarStyle(Qt::ToolButtonStyle Style, eState State); void setToolBarStyle(Qt::ToolButtonStyle Style, eState State);
/** /**
* Returns the tool button style for the given docking state. * Returns the tool button style for the given docking state.
* \see setToolBarStyle() * \see setToolBarStyle()
*/ */
Qt::ToolButtonStyle toolBarStyle(eState State) const; Qt::ToolButtonStyle toolBarStyle(eState State) const;
/** /**
* This function sets the tool button icon size for the given state. * This function sets the tool button icon size for the given state.
* If a dock widget is floating, there is more space an increasing the * If a dock widget is floating, there is more space an increasing the
* icon size is possible. For docked widgets, small icon sizes, eg. 16 x 16 * icon size is possible. For docked widgets, small icon sizes, eg. 16 x 16
* might be better. * might be better.
*/ */
void setToolBarIconSize(const QSize& IconSize, eState State); void setToolBarIconSize(const QSize& IconSize, eState State);
/** /**
* Returns the icon size for a given docking state. * Returns the icon size for a given docking state.
* \see setToolBarIconSize() * \see setToolBarIconSize()
*/ */
QSize toolBarIconSize(eState State) const; QSize toolBarIconSize(eState State) const;
#ifndef QT_NO_TOOLTIP #ifndef QT_NO_TOOLTIP
/** /**
* This is function sets text tooltip for title bar widget * This is function sets text tooltip for title bar widget
* and tooltip for toggle view action * and tooltip for toggle view action
*/ */
void setTabToolTip(const QString &text); void setTabToolTip(const QString &text);
#endif #endif
public: // reimplements QFrame ----------------------------------------------- public: // reimplements QFrame -----------------------------------------------
/** /**
* Emits titleChanged signal if title change event occurs * Emits titleChanged signal if title change event occurs
*/ */
virtual bool event(QEvent *e) override; virtual bool event(QEvent *e) override;
public slots: public slots:
/** /**
* This property controls whether the dock widget is open or closed. * This property controls whether the dock widget is open or closed.
* The toogleViewAction triggers this slot * The toogleViewAction triggers this slot
*/ */
void toggleView(bool Open = true); void toggleView(bool Open = true);
/** /**
* This function will make a docked widget floating * This function will make a docked widget floating
*/ */
void setFloating(); void setFloating();
/** /**
* This function will delete the dock widget and its content from the * This function will delete the dock widget and its content from the
* docking system * docking system
*/ */
void deleteDockWidget(); void deleteDockWidget();
/** /**
* Closes the dock widget * Closes the dock widget
*/ */
void closeDockWidget(); void closeDockWidget();
signals: signals:
/** /**
* This signal is emitted if the dock widget is opened or closed * This signal is emitted if the dock widget is opened or closed
*/ */
void viewToggled(bool Open); void viewToggled(bool Open);
/** /**
* This signal is emitted if the dock widget is closed * This signal is emitted if the dock widget is closed
*/ */
void closed(); void closed();
/** /**
* This signal is emitted if the window title of this dock widget * This signal is emitted if the window title of this dock widget
* changed * changed
*/ */
void titleChanged(const QString& Title); void titleChanged(const QString& Title);
/** /**
* This signal is emitted when the floating property changes. * This signal is emitted when the floating property changes.
* The topLevel parameter is true if the dock widget is now floating; * The topLevel parameter is true if the dock widget is now floating;
* otherwise it is false. * otherwise it is false.
*/ */
void topLevelChanged(bool topLevel); void topLevelChanged(bool topLevel);
/**
* This signal is emitted, if close is requested
*/
void closeRequested();
}; // class DockWidget }; // class DockWidget
} }
// namespace ads // namespace ads

View File

@ -28,6 +28,8 @@
//============================================================================ //============================================================================
#include "FloatingDockContainer.h" #include "FloatingDockContainer.h"
#include <iostream>
#include <QBoxLayout> #include <QBoxLayout>
#include <QApplication> #include <QApplication>
#include <QMouseEvent> #include <QMouseEvent>
@ -358,8 +360,15 @@ void CFloatingDockContainer::closeEvent(QCloseEvent *event)
auto TopLevelDockWidget = topLevelDockWidget(); auto TopLevelDockWidget = topLevelDockWidget();
if (TopLevelDockWidget && TopLevelDockWidget->features().testFlag(CDockWidget::DockWidgetDeleteOnClose)) if (TopLevelDockWidget && TopLevelDockWidget->features().testFlag(CDockWidget::DockWidgetDeleteOnClose))
{ {
TopLevelDockWidget->deleteDockWidget(); if (TopLevelDockWidget->closeDockWidgetInternal())
this->deleteLater(); {
this->deleteLater();
}
else
{
event->ignore();
return;
}
} }
// In Qt version after 5.9.2 there seems to be a bug that causes the // In Qt version after 5.9.2 there seems to be a bug that causes the

View File

@ -117,6 +117,15 @@ enum eIcon
IconCount, //!< just a delimiter for range checks IconCount, //!< just a delimiter for range checks
}; };
/**
* For bitwise combination of dock wdget features
*/
enum eBitwiseOperator
{
BitwiseAnd,
BitwiseOr
};
namespace internal namespace internal
{ {
static const bool RestoreTesting = true; static const bool RestoreTesting = true;