Skip to content

Commit 0c5ef5e

Browse files
Added support for new configuration flag RetainTabSizeWhenCloseButtonHidden
1 parent b776fc2 commit 0c5ef5e

File tree

7 files changed

+76
-29
lines changed

7 files changed

+76
-29
lines changed

demo/MainWindow.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,18 @@ CMainWindow::CMainWindow(QWidget *parent) :
301301

302302
// uncomment the following line if the tab close button should be
303303
// a QToolButton instead of a QPushButton
304-
//CDockManager::setConfigFlags(CDockManager::configFlags() | CDockManager::TabCloseButtonIsToolButton);
304+
// CDockManager::setConfigFlags(CDockManager::configFlags() | CDockManager::TabCloseButtonIsToolButton);
305+
306+
// uncomment the following line if you wand a fixed tab width that does
307+
// not change if the visibility of the close button changes
308+
// CDockManager::setConfigFlag(CDockManager::RetainTabSizeWhenCloseButtonHidden, true);
305309

306310
// Now create the dock manager and its content
307311
d->DockManager = new CDockManager(this);
308312

309313
// Uncomment the following line to have the old style where the dock
310314
// area close button closes the active tab
311-
//CDockManager::setConfigFlags({CDockManager::DockAreaHasCloseButton
315+
// CDockManager::setConfigFlags({CDockManager::DockAreaHasCloseButton
312316
// | CDockManager::DockAreaCloseButtonClosesTab});
313317
connect(d->PerspectiveComboBox, SIGNAL(activated(const QString&)),
314318
d->DockManager, SLOT(openPerspective(const QString&)));

src/DockManager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,13 @@ void CDockManager::setConfigFlags(const ConfigFlags Flags)
778778
}
779779

780780

781+
//===========================================================================
782+
void CDockManager::setConfigFlag(eConfigFlag Flag, bool On)
783+
{
784+
internal::setFlag(StaticConfigFlags, Flag, On);
785+
}
786+
787+
781788
} // namespace ads
782789

783790
//---------------------------------------------------------------------------

src/DockManager.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,15 @@ class ADS_EXPORT CDockManager : public CDockContainerWidget
123123
*/
124124
enum eConfigFlag
125125
{
126-
ActiveTabHasCloseButton = 0x01, //!< If this flag is set, the active tab in a tab area has a close button
127-
DockAreaHasCloseButton = 0x02, //!< If the flag is set each dock area has a close button
128-
DockAreaCloseButtonClosesTab = 0x04,//!< If the flag is set, the dock area close button closes the active tab, if not set, it closes the complete cock area
129-
OpaqueSplitterResize = 0x08, //!< See QSplitter::setOpaqueResize() documentation
130-
XmlAutoFormattingEnabled = 0x10,//!< If enabled, the XML writer automatically adds line-breaks and indentation to empty sections between elements (ignorable whitespace).
131-
XmlCompressionEnabled = 0x20,//!< If enabled, the XML output will be compressed and is not human readable anymore
132-
TabCloseButtonIsToolButton = 0x40,//! If enabled the tab close buttons will be QToolButtons instead of QPushButtons - disabled by default
126+
ActiveTabHasCloseButton = 0x0001, //!< If this flag is set, the active tab in a tab area has a close button
127+
DockAreaHasCloseButton = 0x0002, //!< If the flag is set each dock area has a close button
128+
DockAreaCloseButtonClosesTab = 0x0004,//!< If the flag is set, the dock area close button closes the active tab, if not set, it closes the complete cock area
129+
OpaqueSplitterResize = 0x0008, //!< See QSplitter::setOpaqueResize() documentation
130+
XmlAutoFormattingEnabled = 0x0010,//!< If enabled, the XML writer automatically adds line-breaks and indentation to empty sections between elements (ignorable whitespace).
131+
XmlCompressionEnabled = 0x0020,//!< If enabled, the XML output will be compressed and is not human readable anymore
132+
TabCloseButtonIsToolButton = 0x0040,//! If enabled the tab close buttons will be QToolButtons instead of QPushButtons - disabled by default
133+
AllTabsHaveCloseButton = 0x0080, //!< if this flag is set, then all tabs that are closable show a close button
134+
RetainTabSizeWhenCloseButtonHidden = 0x0100, //!< if this flag is set, the space for the close button is reserved even if the close button is not visible
133135
DefaultConfig = ActiveTabHasCloseButton | DockAreaHasCloseButton | OpaqueSplitterResize | XmlCompressionEnabled, ///< the default configuration
134136
};
135137
Q_DECLARE_FLAGS(ConfigFlags, eConfigFlag)
@@ -159,6 +161,11 @@ class ADS_EXPORT CDockManager : public CDockContainerWidget
159161
*/
160162
static void setConfigFlags(const ConfigFlags Flags);
161163

164+
/**
165+
* Set a certain config flag
166+
*/
167+
static void setConfigFlag(eConfigFlag Flag, bool On = true);
168+
162169
/**
163170
* Adds dockwidget into the given area.
164171
* If DockAreaWidget is not null, then the area parameter indicates the area

src/DockWidget.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,25 +289,21 @@ CDockWidgetTab* CDockWidget::tabWidget() const
289289
//============================================================================
290290
void CDockWidget::setFeatures(DockWidgetFeatures features)
291291
{
292+
if (d->Features == features)
293+
{
294+
return;
295+
}
292296
d->Features = features;
297+
d->TabWidget->onDockWidgetFeaturesChanged();
293298
}
294299

295300

296301
//============================================================================
297302
void CDockWidget::setFeature(DockWidgetFeature flag, bool on)
298303
{
299-
#if QT_VERSION >= 0x050700
300-
d->Features.setFlag(flag, on);
301-
#else
302-
if(on)
303-
{
304-
d->Features |= flag;
305-
}
306-
else
307-
{
308-
d->Features &= ~flag;
309-
}
310-
#endif
304+
auto Features = features();
305+
internal::setFlag(Features, flag, on);
306+
setFeatures(Features);
311307
}
312308

313309

src/DockWidgetTab.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,8 @@ void DockWidgetTabPrivate::createLayout()
167167
CloseIcon.addPixmap(normalPixmap, QIcon::Normal);
168168
CloseIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
169169
CloseButton->setIcon(CloseIcon);
170-
171170
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
172-
CloseButton->setVisible(false);
171+
_this->onDockWidgetFeaturesChanged();
173172
#ifndef QT_NO_TOOLTIP
174173
CloseButton->setToolTip(QObject::tr("Close Tab"));
175174
#endif
@@ -389,8 +388,10 @@ bool CDockWidgetTab::isActiveTab() const
389388
void CDockWidgetTab::setActiveTab(bool active)
390389
{
391390
bool DockWidgetClosable = d->DockWidget->features().testFlag(CDockWidget::DockWidgetClosable);
392-
bool TabHasCloseButton = d->testConfigFlag(CDockManager::ActiveTabHasCloseButton);
393-
d->CloseButton->setVisible(active && DockWidgetClosable && TabHasCloseButton);
391+
bool ActiveTabHasCloseButton = d->testConfigFlag(CDockManager::ActiveTabHasCloseButton);
392+
bool AllTabsHaveCloseButton = d->testConfigFlag(CDockManager::AllTabsHaveCloseButton);
393+
bool TabHasCloseButton = (ActiveTabHasCloseButton && active) | AllTabsHaveCloseButton;
394+
d->CloseButton->setVisible(DockWidgetClosable && TabHasCloseButton);
394395
if (d->IsActiveTab == active)
395396
{
396397
return;
@@ -546,6 +547,17 @@ bool CDockWidgetTab::event(QEvent *e)
546547
}
547548

548549

550+
//============================================================================
551+
void CDockWidgetTab::onDockWidgetFeaturesChanged()
552+
{
553+
auto Features = d->DockWidget->features();
554+
auto SizePolicy = d->CloseButton->sizePolicy();
555+
SizePolicy.setRetainSizeWhenHidden(Features.testFlag(CDockWidget::DockWidgetClosable)
556+
&& d->testConfigFlag(CDockManager::RetainTabSizeWhenCloseButtonHidden));
557+
d->CloseButton->setSizePolicy(SizePolicy);
558+
}
559+
560+
549561

550562

551563
} // namespace ads

src/DockWidgetTab.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class ADS_EXPORT CDockWidgetTab : public QFrame
5353
private:
5454
DockWidgetTabPrivate* d; ///< private data (pimpl)
5555
friend struct DockWidgetTabPrivate;
56+
friend class CDockWidget;
57+
void onDockWidgetFeaturesChanged();
5658

5759
private slots:
5860
void onDetachActionTriggered();
@@ -125,7 +127,6 @@ private slots:
125127
*/
126128
QString text() const;
127129

128-
129130
/**
130131
* Sets the tab text
131132
*/
@@ -136,15 +137,14 @@ private slots:
136137
*/
137138
bool isClosable() const;
138139

139-
140140
/**
141141
* Track event ToolTipChange and set child ToolTip
142142
*/
143143
virtual bool event(QEvent *e) override;
144144

145-
public slots:
146-
virtual void setVisible(bool visible) override;
147145

146+
public slots:
147+
virtual void setVisible(bool visible) override;
148148

149149
signals:
150150
void activeTabChanged();

src/ads_globals.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,27 @@ T findParent(const QWidget* w)
162162
*/
163163
QPixmap createTransparentPixmap(const QPixmap& Source, qreal Opacity);
164164

165+
166+
/**
167+
* Helper function for settings flags in a QFlags instance.
168+
*/
169+
template <class T>
170+
void setFlag(T& Flags, typename T::enum_type flag, bool on = true)
171+
{
172+
#if QT_VERSION >= 0x050700
173+
Flags.setFlag(flag, on);
174+
#else
175+
if(on)
176+
{
177+
d->Features |= flag;
178+
}
179+
else
180+
{
181+
d->Features &= ~flag;
182+
}
183+
#endif
184+
}
185+
165186
} // namespace internal
166187
} // namespace ads
167188

0 commit comments

Comments
 (0)