Skip to content

Commit b3a2721

Browse files
author
Uwe Kindler
committed
Some changes for dockwidget tab close button, some refactorings to make insertion of widgets into dock widget easier, added createDefaultToolBar function for creation of toolbar and removed layout flags from CDockWidget
1 parent 316e532 commit b3a2721

15 files changed

+187
-93
lines changed

demo/MainWindow.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ static ads::CDockWidget* createCalendarDockWidget(QMenu* ViewMenu)
9494
{
9595
static int CalendarCount = 0;
9696
QCalendarWidget* w = new QCalendarWidget();
97-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Calendar %1").arg(CalendarCount++),
98-
nullptr, ads::CDockWidget::WithScrollArea);
97+
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Calendar %1").arg(CalendarCount++));
9998
DockWidget->setWidget(w);
10099
DockWidget->setToggleViewActionMode(ads::CDockWidget::ActionModeShow);
101100
ViewMenu->addAction(DockWidget->toggleViewAction());
@@ -112,8 +111,7 @@ static ads::CDockWidget* createFileSystemTreeDockWidget(QMenu* ViewMenu)
112111
QFileSystemModel* m = new QFileSystemModel(w);
113112
m->setRootPath(QDir::currentPath());
114113
w->setModel(m);
115-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Filesystem %1").arg(FileSystemCount++),
116-
nullptr, ads::CDockWidget::WithTopToolBar);
114+
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Filesystem %1").arg(FileSystemCount++));
117115
DockWidget->setWidget(w);
118116
ViewMenu->addAction(DockWidget->toggleViewAction());
119117
return DockWidget;
@@ -178,13 +176,13 @@ void MainWindowPrivate::createContent()
178176
DockManager->addDockWidget(ads::LeftDockWidgetArea, DockWidget);
179177
DockManager->addDockWidget(ads::LeftDockWidgetArea, createLongTextLabelDockWidget(ViewMenu));
180178
auto FileSystemWidget = createFileSystemTreeDockWidget(ViewMenu);
181-
auto ToolBar = FileSystemWidget->toolBar();
179+
auto ToolBar = FileSystemWidget->createDefaultToolBar();
182180
ToolBar->addAction(ui.actionSaveState);
183181
ToolBar->addAction(ui.actionRestoreState);
184182
DockManager->addDockWidget(ads::BottomDockWidgetArea, FileSystemWidget);
185183

186184
FileSystemWidget = createFileSystemTreeDockWidget(ViewMenu);
187-
ToolBar = FileSystemWidget->toolBar();
185+
ToolBar = FileSystemWidget->createDefaultToolBar();
188186
ToolBar->addAction(ui.actionSaveState);
189187
ToolBar->addAction(ui.actionRestoreState);
190188
FileSystemWidget->setFeature(ads::CDockWidget::DockWidgetMovable, false);

src/DockAreaTabBar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ void CDockAreaTabBar::insertTab(int Index, CDockWidgetTab* Tab)
288288
{
289289
d->TabsLayout->insertWidget(Index, Tab);
290290
connect(Tab, SIGNAL(clicked()), this, SLOT(onTabClicked()));
291+
connect(Tab, SIGNAL(closeButtonClicked()), this, SLOT(onTabCloseButtonClicked()));
291292
connect(Tab, SIGNAL(moved(const QPoint&)), this, SLOT(onTabWidgetMoved(const QPoint&)));
292293
Tab->installEventFilter(this);
293294
emit tabInserted(Index);
@@ -400,6 +401,13 @@ void CDockAreaTabBar::onTabClicked()
400401
}
401402

402403

404+
//===========================================================================
405+
void CDockAreaTabBar::onTabCloseButtonClicked()
406+
{
407+
closeTab(currentIndex());
408+
}
409+
410+
403411
//===========================================================================
404412
CDockWidgetTab* CDockAreaTabBar::tab(int Index) const
405413
{

src/DockAreaTabBar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class CDockAreaTabBar : public QScrollArea
5454

5555
private slots:
5656
void onTabClicked();
57+
void onTabCloseButtonClicked();
5758
void onTabWidgetMoved(const QPoint& GlobalPos);
5859

5960
protected:

src/DockAreaTitleBar.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@
3737
#include <QScrollArea>
3838
#include <QMouseEvent>
3939
#include <QDebug>
40-
#include <QStyleOptionButton>
41-
#include <QPainter>
4240

41+
#include "ads_globals.h"
4342
#include "FloatingDockContainer.h"
4443
#include "DockAreaWidget.h"
4544
#include "DockOverlay.h"
@@ -81,8 +80,6 @@ struct DockAreaTitleBarPrivate
8180
* Creates the internal TabBar
8281
*/
8382
void createTabBar();
84-
85-
QPixmap createTransparentPixmap(const QPixmap& Source);
8683
};// struct DockAreaTitleBarPrivate
8784

8885

@@ -95,18 +92,6 @@ DockAreaTitleBarPrivate::DockAreaTitleBarPrivate(CDockAreaTitleBar* _public) :
9592
}
9693

9794

98-
//============================================================================
99-
QPixmap DockAreaTitleBarPrivate::createTransparentPixmap(const QPixmap& Source)
100-
{
101-
QPixmap disabledPixmap(Source.size());
102-
disabledPixmap.fill(Qt::transparent);
103-
QPainter p(&disabledPixmap);
104-
p.setOpacity(0.25);
105-
p.drawPixmap(0, 0, Source);
106-
return disabledPixmap;
107-
}
108-
109-
11095
//============================================================================
11196
void DockAreaTitleBarPrivate::createButtons()
11297
{
@@ -137,14 +122,14 @@ void DockAreaTitleBarPrivate::createButtons()
137122
CloseButton->setObjectName("closeButton");
138123
CloseButton->setAutoRaise(true);
139124

140-
// The standard icons do does not look good on high DPI screens
125+
// The standard icons do not look good on high DPI screens
141126
QIcon CloseIcon = _this->style()->standardIcon(QStyle::SP_TitleBarCloseButton);
142127
QPixmap normalPixmap = _this->style()->standardPixmap(QStyle::SP_TitleBarCloseButton, 0, CloseButton);
143-
QPixmap disabledPixmap = createTransparentPixmap(normalPixmap);
128+
QPixmap disabledPixmap = internal::createTransparentPixmap(normalPixmap, 0.25);
144129
CloseIcon.addPixmap(disabledPixmap, QIcon::Disabled);
145130

146131
CloseButton->setIcon(CloseIcon);
147-
CloseButton->setToolTip(QObject::tr("Close"));
132+
CloseButton->setToolTip(QObject::tr("Close all tabs"));
148133
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
149134
TopLayout->addWidget(CloseButton, 0);
150135
_this->connect(CloseButton, SIGNAL(clicked()), SLOT(onCloseButtonClicked()));
@@ -236,7 +221,8 @@ void CDockAreaTitleBar::onTabsMenuAboutToShow()
236221
void CDockAreaTitleBar::onCloseButtonClicked()
237222
{
238223
qDebug() << "CDockAreaTitleBar::onCloseButtonClicked";
239-
d->TabBar->closeTab(d->TabBar->currentIndex());
224+
//d->TabBar->closeTab(d->TabBar->currentIndex());
225+
d->DockArea->closeArea();
240226
}
241227

242228

@@ -263,8 +249,8 @@ void CDockAreaTitleBar::onCurrentTabChanged(int Index)
263249
{
264250
return;
265251
}
266-
CDockWidget* DockWidget = d->TabBar->tab(Index)->dockWidget();
267-
d->CloseButton->setEnabled(DockWidget->features().testFlag(CDockWidget::DockWidgetClosable));
252+
/*CDockWidget* DockWidget = d->TabBar->tab(Index)->dockWidget();
253+
d->CloseButton->setEnabled(DockWidget->features().testFlag(CDockWidget::DockWidgetClosable));*/
268254
}
269255

270256

src/DockAreaTitleBar.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ private slots:
8282
*/
8383
QAbstractButton* button(TitleBarButton which) const;
8484

85+
/**
86+
* This function is here for debug reasons
87+
*/
8588
virtual void setVisible(bool Visible) override;
8689

8790
signals:

src/DockAreaWidget.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,16 @@ QAbstractButton* CDockAreaWidget::titleBarButton(TitleBarButton which) const
739739
{
740740
return d->TitleBar->button(which);
741741
}
742+
743+
744+
//============================================================================
745+
void CDockAreaWidget::closeArea()
746+
{
747+
for (auto DockWidget : openedDockWidgets())
748+
{
749+
DockWidget->toggleView(false);
750+
}
751+
}
742752
} // namespace ads
743753

744754
//---------------------------------------------------------------------------

src/DockAreaWidget.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ private slots:
130130
*/
131131
void internalSetCurrentDockWidget(CDockWidget* DockWidget);
132132

133+
protected slots:
134+
void toggleView(bool Open);
135+
133136
public:
134137
using Super = QFrame;
135138

@@ -247,10 +250,9 @@ public slots:
247250
void setCurrentIndex(int index);
248251

249252
/**
250-
* This property controls whether the dock area is open or closed.
251-
* The toogleViewAction triggers this slot.
253+
* Closes the dock area and all dock widgets in this area
252254
*/
253-
void toggleView(bool Open);
255+
void closeArea();
254256

255257
signals:
256258
/**

src/DockWidget.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ struct DockWidgetPrivate
6868
CDockAreaWidget* DockArea = nullptr;
6969
QAction* ToggleViewAction;
7070
bool Closed = false;
71-
CDockWidget::LayoutFlags LayoutFlags;
7271
QScrollArea* ScrollArea = nullptr;
7372
QToolBar* ToolBar = nullptr;
7473
Qt::ToolButtonStyle ToolBarStyleDocked = Qt::ToolButtonIconOnly;
@@ -182,11 +181,6 @@ void DockWidgetPrivate::updateParentDockArea()
182181
//============================================================================
183182
void DockWidgetPrivate::setupToolBar()
184183
{
185-
if (!LayoutFlags.testFlag(CDockWidget::WithTopToolBar))
186-
{
187-
return;
188-
}
189-
190184
ToolBar = new QToolBar(_this);
191185
ToolBar->setObjectName("dockWidgetToolBar");
192186
Layout->addWidget(ToolBar);
@@ -201,11 +195,6 @@ void DockWidgetPrivate::setupToolBar()
201195
//============================================================================
202196
void DockWidgetPrivate::setupScrollArea()
203197
{
204-
if (!LayoutFlags.testFlag(CDockWidget::WithScrollArea))
205-
{
206-
return;
207-
}
208-
209198
ScrollArea = new QScrollArea(_this);
210199
ScrollArea->setObjectName("dockWidgetScrollArea");
211200
ScrollArea->setWidgetResizable(true);
@@ -214,20 +203,16 @@ void DockWidgetPrivate::setupScrollArea()
214203

215204

216205
//============================================================================
217-
CDockWidget::CDockWidget(const QString &title, QWidget *parent,
218-
LayoutFlags layoutFlags) :
206+
CDockWidget::CDockWidget(const QString &title, QWidget *parent) :
219207
QFrame(parent),
220208
d(new DockWidgetPrivate(this))
221209
{
222-
d->LayoutFlags = layoutFlags;
223210
d->Layout = new QBoxLayout(QBoxLayout::TopToBottom);
224211
d->Layout->setContentsMargins(0, 0, 0, 0);
225212
d->Layout->setSpacing(0);
226213
setLayout(d->Layout);
227214
setWindowTitle(title);
228215
setObjectName(title);
229-
d->setupToolBar();
230-
d->setupScrollArea();
231216

232217
d->TabWidget = new CDockWidgetTab(this);
233218
d->ToggleViewAction = new QAction(title);
@@ -256,16 +241,14 @@ void CDockWidget::setToggleViewActionChecked(bool Checked)
256241

257242

258243
//============================================================================
259-
void CDockWidget::setWidget(QWidget* widget)
244+
void CDockWidget::setWidget(QWidget* widget, eInsertMode InsertMode)
260245
{
261-
if (d->LayoutFlags.testFlag(WithScrollArea))
246+
QScrollArea* ScrollAreaWidget = qobject_cast<QScrollArea*>(widget);
247+
if (ScrollAreaWidget || ForceNoScrollArea != InsertMode)
262248
{
249+
d->setupScrollArea();
263250
d->ScrollArea->setWidget(widget);
264251
}
265-
else if (d->Widget)
266-
{
267-
d->Layout->replaceWidget(d->Widget, widget);
268-
}
269252
else
270253
{
271254
d->Layout->addWidget(widget);
@@ -545,6 +528,18 @@ QToolBar* CDockWidget::toolBar() const
545528
}
546529

547530

531+
//============================================================================
532+
QToolBar* CDockWidget::createDefaultToolBar()
533+
{
534+
if (!d->ToolBar)
535+
{
536+
d->setupToolBar();
537+
}
538+
539+
return d->ToolBar;
540+
}
541+
542+
548543
//============================================================================
549544
void CDockWidget::setToolBar(QToolBar* ToolBar)
550545
{

src/DockWidget.h

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,12 @@ private slots:
152152
StateFloating
153153
};
154154

155-
/**
156-
* Use the layout flags to configure the layout of the dock widget.
157-
* The content of a dock widget should be resizable do a very small size to
158-
* prevent the dock widget from blocking the resizing. To ensure, that a
159-
* dock widget can be resized very well, it is better to insert the content+
160-
* widget into a scroll area. Enable the WithScrollArea
161-
* feature to use this feature. If your content widget is already in a
162-
* scroll area or if it is a derived class like QTableView, the you should
163-
* disable the WithScrollArea flag.
164-
* Often dock widgets need a ToolBar for control of operations in the dock
165-
* widget. Use the WithToolBar feature to enable a tool bar that is placed
166-
* on top of the dock widget content. If this flag is disabled, the toolBar()
167-
* function returns a nullptr.
168-
*/
169-
enum LayoutFlag
155+
enum eInsertMode
170156
{
171-
WithScrollArea = 0x01,
172-
WithTopToolBar = 0x02
157+
AutoScrollArea,
158+
ForceScrollArea,
159+
ForceNoScrollArea
173160
};
174-
Q_DECLARE_FLAGS(LayoutFlags, LayoutFlag)
175161

176162
/**
177163
* This mode configures the behavior of the toggle view action.
@@ -201,8 +187,7 @@ private slots:
201187
* by calling setObjectName() after construction.
202188
* Use the layoutFlags to configure the layout of the dock widget.
203189
*/
204-
CDockWidget(const QString &title, QWidget* parent = 0,
205-
LayoutFlags layoutFlags = 0);
190+
CDockWidget(const QString &title, QWidget* parent = 0);
206191

207192
/**
208193
* Virtual Destructor
@@ -216,8 +201,22 @@ private slots:
216201

217202
/**
218203
* Sets the widget for the dock widget to widget.
204+
* The InsertMode defines how the widget is inserted into the dock widget.
205+
* The content of a dock widget should be resizable do a very small size to
206+
* prevent the dock widget from blocking the resizing. To ensure, that a
207+
* dock widget can be resized very well, it is better to insert the content+
208+
* widget into a scroll area or to provide a widget that is already a scroll
209+
* area or that contains a scroll area.
210+
* If the InsertMode is AutoScrollArea, the DockWidget tries to automatically
211+
* detect how to insert the given widget. If the widget is derived from
212+
* QScrollArea (i.e. an QAbstractItemView), then the widget is inserted
213+
* directly. If the given widget is not a scroll area, the widget will be
214+
* inserted into a scroll area.
215+
* To force insertion into a scroll area, you can also provide the InsertMode
216+
* ForceScrollArea. To prevent insertion into a scroll area, you can
217+
* provide the InsertMode ForceNoScrollArea
219218
*/
220-
void setWidget(QWidget* widget);
219+
void setWidget(QWidget* widget, eInsertMode InsertMode = AutoScrollArea);
221220

222221
/**
223222
* Returns the widget for the dock widget. This function returns zero if
@@ -306,17 +305,30 @@ private slots:
306305
void setIcon(const QIcon& Icon);
307306

308307
/**
309-
* Returns tzhe icon that has been assigned to the dock widget
308+
* Returns the icon that has been assigned to the dock widget
310309
*/
311310
QIcon icon() const;
312311

313312
/**
314313
* If the WithToolBar layout flag is enabled, then this function returns
315314
* the dock widget toolbar. If the flag is disabled, the function returns
316315
* a nullptr.
316+
* This function returns the dock widget top tool bar.
317+
* If no toolbar is assigned, this function returns nullptr. To get a vaild
318+
* toolbar you either need to create a default empty toolbar via
319+
* createDefaultToolBar() function or you need to assign you custom
320+
* toolbar via setToolBar().
317321
*/
318322
QToolBar* toolBar() const;
319323

324+
/**
325+
* If you would like to use the default top tool bar, then call this
326+
* function to create the default tool bar.
327+
* After this function the toolBar() function will return a valid toolBar()
328+
* object.
329+
*/
330+
QToolBar* createDefaultToolBar();
331+
320332
/**
321333
* Assign a new tool bar that is shown above the content widget.
322334
* The dock widget will become the owner of the tool bar and deletes it

0 commit comments

Comments
 (0)