Commit 1541fbd2 authored by hjk's avatar hjk

FancyMainWindow: Use auto-hiding dock title bars

Instead of the manual hard-to-discover "Locked" action

Change-Id: I08e3b65d3e620adfa1439a8de6e028ecfbdec16d
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 08f12e95
......@@ -32,17 +32,199 @@
#include "qtcassert.h"
#include <QContextMenuEvent>
#include <QMenu>
#include <QDebug>
#include <QDockWidget>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QMenu>
#include <QPainter>
#include <QSettings>
#include <QStyle>
#include <QStyleOption>
#include <QToolButton>
static const char lockedKeyC[] = "Locked";
static const char stateKeyC[] = "State";
static const int settingsVersion = 2;
static const char dockWidgetActiveState[] = "DockWidgetActiveState";
namespace Utils {
// Stolen from QDockWidgetTitleButton
class DockWidgetTitleButton : public QAbstractButton
{
public:
DockWidgetTitleButton(QWidget *parent)
: QAbstractButton(parent)
{
setFocusPolicy(Qt::NoFocus);
}
QSize sizeHint() const
{
ensurePolished();
int size = 2*style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin, 0, this);
if (!icon().isNull()) {
int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this);
QSize sz = icon().actualSize(QSize(iconSize, iconSize));
size += qMax(sz.width(), sz.height());
}
return QSize(size, size);
}
QSize minimumSizeHint() const { return sizeHint(); }
void enterEvent(QEvent *event)
{
if (isEnabled()) update();
QAbstractButton::enterEvent(event);
}
void leaveEvent(QEvent *event)
{
if (isEnabled()) update();
QAbstractButton::leaveEvent(event);
}
void paintEvent(QPaintEvent *event);
};
void DockWidgetTitleButton::paintEvent(QPaintEvent *)
{
QPainter p(this);
QStyleOptionToolButton opt;
opt.init(this);
opt.state |= QStyle::State_AutoRaise;
opt.icon = icon();
opt.subControls = 0;
opt.activeSubControls = 0;
opt.features = QStyleOptionToolButton::None;
opt.arrowType = Qt::NoArrow;
int size = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this);
opt.iconSize = QSize(size, size);
style()->drawComplexControl(QStyle::CC_ToolButton, &opt, &p, this);
}
class TitleBarWidget : public QWidget
{
public:
TitleBarWidget(QDockWidget *parent, const QStyleOptionDockWidget &opt)
: QWidget(parent), q(parent), m_active(true)
{
m_titleLabel = new QLabel(this);
m_floatButton = new DockWidgetTitleButton(this);
m_floatButton->setIcon(q->style()->standardIcon(QStyle::SP_TitleBarNormalButton, &opt, q));
m_floatButton->setAccessibleName(QDockWidget::tr("Float"));
m_floatButton->setAccessibleDescription(QDockWidget::tr("Undocks and re-attaches the dock widget"));
m_closeButton = new DockWidgetTitleButton(this);
m_closeButton->setIcon(q->style()->standardIcon(QStyle::SP_TitleBarCloseButton, &opt, q));
m_closeButton->setAccessibleName(QDockWidget::tr("Close"));
m_closeButton->setAccessibleDescription(QDockWidget::tr("Closes the dock widget"));
setActive(false);
const int minWidth = 10;
const int maxWidth = 10000;
const int inactiveHeight = 3;
const int activeHeight = m_closeButton->sizeHint().height() + 2;
m_minimumInactiveSize = QSize(minWidth, inactiveHeight);
m_maximumInactiveSize = QSize(maxWidth, inactiveHeight);
m_minimumActiveSize = QSize(minWidth, activeHeight);
m_maximumActiveSize = QSize(maxWidth, activeHeight);
auto layout = new QHBoxLayout(this);
layout->setMargin(0);
layout->setSpacing(0);
layout->setContentsMargins(4, 0, 0, 0);
layout->addWidget(m_titleLabel);
layout->addStretch();
layout->addWidget(m_floatButton);
layout->addWidget(m_closeButton);
setLayout(layout);
}
void enterEvent(QEvent *event)
{
setActive(true);
QWidget::enterEvent(event);
}
void leaveEvent(QEvent *event)
{
if (!q->isFloating())
setActive(false);
QWidget::leaveEvent(event);
}
void setActive(bool on)
{
if (m_active == on)
return;
m_active = on;
m_titleLabel->setVisible(on);
m_floatButton->setVisible(on);
m_closeButton->setVisible(on);
update();
}
QSize sizeHint() const
{
ensurePolished();
return m_active ? m_maximumActiveSize : m_maximumInactiveSize;
}
QSize minimumSizeHint() const
{
ensurePolished();
return m_active ? m_minimumActiveSize : m_minimumInactiveSize;
}
public:
QDockWidget *q;
bool m_active;
QSize m_minimumActiveSize;
QSize m_maximumActiveSize;
QSize m_minimumInactiveSize;
QSize m_maximumInactiveSize;
QLabel *m_titleLabel;
DockWidgetTitleButton *m_floatButton;
DockWidgetTitleButton *m_closeButton;
};
class DockWidget : public QDockWidget
{
public:
DockWidget(QWidget *inner, QWidget *parent)
: QDockWidget(parent)
{
setWidget(inner);
setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable);
setObjectName(inner->objectName() + QLatin1String("DockWidget"));
setWindowTitle(inner->windowTitle());
QStyleOptionDockWidget opt;
initStyleOption(&opt);
auto titleBar = new TitleBarWidget(this, opt);
titleBar->m_titleLabel->setText(inner->windowTitle());
setTitleBarWidget(titleBar);
auto origFloatButton = findChild<QAbstractButton *>(QLatin1String("qt_dockwidget_floatbutton"));
connect(titleBar->m_floatButton, SIGNAL(clicked()), origFloatButton, SIGNAL(clicked()));
auto origCloseButton = findChild<QAbstractButton *>(QLatin1String("qt_dockwidget_closebutton"));
connect(titleBar->m_closeButton, SIGNAL(clicked()), origCloseButton, SIGNAL(clicked()));
}
};
/*! \class Utils::FancyMainWindow
\brief The FancyMainWindow class is a MainWindow with dock widgets and
......@@ -53,40 +235,30 @@ namespace Utils {
in a Window-menu.
*/
struct FancyMainWindowPrivate
class FancyMainWindowPrivate
{
public:
FancyMainWindowPrivate();
bool m_locked;
bool m_handleDockVisibilityChanges;
QAction m_menuSeparator1;
QAction m_toggleLockedAction;
QAction m_menuSeparator2;
QAction m_menuSeparator;
QAction m_resetLayoutAction;
QDockWidget *m_toolBarDockWidget;
};
FancyMainWindowPrivate::FancyMainWindowPrivate() :
m_locked(true),
m_handleDockVisibilityChanges(true),
m_menuSeparator1(0),
m_toggleLockedAction(FancyMainWindow::tr("Locked"), 0),
m_menuSeparator2(0),
m_menuSeparator(0),
m_resetLayoutAction(FancyMainWindow::tr("Reset to Default Layout"), 0),
m_toolBarDockWidget(0)
{
m_toggleLockedAction.setCheckable(true);
m_toggleLockedAction.setChecked(m_locked);
m_menuSeparator1.setSeparator(true);
m_menuSeparator2.setSeparator(true);
m_menuSeparator.setSeparator(true);
}
FancyMainWindow::FancyMainWindow(QWidget *parent) :
QMainWindow(parent), d(new FancyMainWindowPrivate)
{
connect(&d->m_toggleLockedAction, SIGNAL(toggled(bool)),
this, SLOT(setLocked(bool)));
connect(&d->m_resetLayoutAction, SIGNAL(triggered()),
this, SIGNAL(resetLayout()));
}
......@@ -98,41 +270,18 @@ FancyMainWindow::~FancyMainWindow()
QDockWidget *FancyMainWindow::addDockForWidget(QWidget *widget)
{
QDockWidget *dockWidget = new QDockWidget(widget->windowTitle(), this);
dockWidget->setWidget(widget);
// Set an object name to be used in settings, derive from widget name
const QString objectName = widget->objectName();
if (objectName.isEmpty())
dockWidget->setObjectName(QLatin1String("dockWidget") + QString::number(dockWidgets().size() + 1));
else
dockWidget->setObjectName(objectName + QLatin1String("DockWidget"));
QTC_ASSERT(widget, return 0);
QTC_CHECK(widget->objectName().size());
QTC_CHECK(widget->windowTitle().size());
auto dockWidget = new DockWidget(widget, this);
connect(dockWidget->toggleViewAction(), SIGNAL(triggered()),
this, SLOT(onDockActionTriggered()), Qt::QueuedConnection);
connect(dockWidget, SIGNAL(visibilityChanged(bool)),
this, SLOT(onDockVisibilityChange(bool)));
connect(dockWidget, SIGNAL(topLevelChanged(bool)),
this, SLOT(onTopLevelChanged()));
dockWidget->setProperty(dockWidgetActiveState, true);
updateDockWidget(dockWidget);
return dockWidget;
}
void FancyMainWindow::updateDockWidget(QDockWidget *dockWidget)
{
const QDockWidget::DockWidgetFeatures features =
(d->m_locked) ? QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable
: QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable;
if (dockWidget->property("managed_dockwidget").isNull()) { // for the debugger tool bar
QWidget *titleBarWidget = dockWidget->titleBarWidget();
if (d->m_locked && !titleBarWidget && !dockWidget->isFloating()) {
titleBarWidget = new QWidget(dockWidget);
} else if ((!d->m_locked || dockWidget->isFloating()) && titleBarWidget) {
delete titleBarWidget;
titleBarWidget = 0;
}
dockWidget->setTitleBarWidget(titleBarWidget);
}
dockWidget->setFeatures(features);
return dockWidget;
}
void FancyMainWindow::onDockActionTriggered()
......@@ -150,11 +299,6 @@ void FancyMainWindow::onDockVisibilityChange(bool visible)
sender()->setProperty(dockWidgetActiveState, visible);
}
void FancyMainWindow::onTopLevelChanged()
{
updateDockWidget(qobject_cast<QDockWidget*>(sender()));
}
void FancyMainWindow::setTrackingEnabled(bool enabled)
{
if (enabled) {
......@@ -166,14 +310,6 @@ void FancyMainWindow::setTrackingEnabled(bool enabled)
}
}
void FancyMainWindow::setLocked(bool locked)
{
d->m_locked = locked;
foreach (QDockWidget *dockWidget, dockWidgets()) {
updateDockWidget(dockWidget);
}
}
void FancyMainWindow::hideEvent(QHideEvent *event)
{
Q_UNUSED(event)
......@@ -229,7 +365,6 @@ QHash<QString, QVariant> FancyMainWindow::saveSettings() const
{
QHash<QString, QVariant> settings;
settings.insert(QLatin1String(stateKeyC), saveState(settingsVersion));
settings.insert(QLatin1String(lockedKeyC), d->m_locked);
foreach (QDockWidget *dockWidget, dockWidgets()) {
settings.insert(dockWidget->objectName(),
dockWidget->property(dockWidgetActiveState));
......@@ -242,8 +377,6 @@ void FancyMainWindow::restoreSettings(const QHash<QString, QVariant> &settings)
QByteArray ba = settings.value(QLatin1String(stateKeyC), QByteArray()).toByteArray();
if (!ba.isEmpty())
restoreState(ba, settingsVersion);
d->m_locked = settings.value(QLatin1String("Locked"), true).toBool();
d->m_toggleLockedAction.setChecked(d->m_locked);
foreach (QDockWidget *widget, dockWidgets()) {
widget->setProperty(dockWidgetActiveState,
settings.value(widget->objectName(), false));
......@@ -255,11 +388,6 @@ QList<QDockWidget *> FancyMainWindow::dockWidgets() const
return findChildren<QDockWidget *>();
}
bool FancyMainWindow::isLocked() const
{
return d->m_locked;
}
static bool actionLessThan(const QAction *action1, const QAction *action2)
{
QTC_ASSERT(action1, return true);
......@@ -282,26 +410,14 @@ QMenu *FancyMainWindow::createPopupMenu()
QMenu *menu = new QMenu(this);
foreach (QAction *action, actions)
menu->addAction(action);
menu->addAction(&d->m_menuSeparator1);
menu->addAction(&d->m_toggleLockedAction);
menu->addAction(&d->m_menuSeparator2);
menu->addAction(&d->m_menuSeparator);
menu->addAction(&d->m_resetLayoutAction);
return menu;
}
QAction *FancyMainWindow::menuSeparator1() const
{
return &d->m_menuSeparator1;
}
QAction *FancyMainWindow::toggleLockedAction() const
{
return &d->m_toggleLockedAction;
}
QAction *FancyMainWindow::menuSeparator2() const
QAction *FancyMainWindow::menuSeparator() const
{
return &d->m_menuSeparator2;
return &d->m_menuSeparator;
}
QAction *FancyMainWindow::resetLayoutAction() const
......@@ -313,9 +429,7 @@ void FancyMainWindow::setDockActionsVisible(bool v)
{
foreach (const QDockWidget *dockWidget, dockWidgets())
dockWidget->toggleViewAction()->setVisible(v);
d->m_toggleLockedAction.setVisible(v);
d->m_menuSeparator1.setVisible(v);
d->m_menuSeparator2.setVisible(v);
d->m_menuSeparator.setVisible(v);
d->m_resetLayoutAction.setVisible(v);
}
......
......@@ -40,7 +40,7 @@ QT_END_NAMESPACE
namespace Utils {
struct FancyMainWindowPrivate;
class FancyMainWindowPrivate;
class QTCREATOR_UTILS_EXPORT FancyMainWindow : public QMainWindow
{
......@@ -56,7 +56,6 @@ public:
QList<QDockWidget *> dockWidgets() const;
void setTrackingEnabled(bool enabled);
bool isLocked() const;
void saveSettings(QSettings *settings) const;
void restoreSettings(const QSettings *settings);
......@@ -64,15 +63,12 @@ public:
void restoreSettings(const QHash<QString, QVariant> &settings);
// Additional context menu actions
QAction *menuSeparator1() const;
QAction *toggleLockedAction() const;
QAction *menuSeparator2() const;
QAction *menuSeparator() const;
QAction *resetLayoutAction() const;
// Overwritten to add locked/reset.
virtual QMenu *createPopupMenu();
QDockWidget *toolBarDockWidget() const;
void setToolBarDockWidget(QDockWidget *dock);
......@@ -82,20 +78,18 @@ signals:
void resetLayout();
public slots:
void setLocked(bool locked);
void setDockActionsVisible(bool v);
protected:
void hideEvent(QHideEvent *event);
void showEvent(QShowEvent *event);
void contextMenuEvent(QContextMenuEvent *event);
private slots:
void onDockActionTriggered();
void onDockVisibilityChange(bool);
void onTopLevelChanged();
private:
void updateDockWidget(QDockWidget *dockWidget);
void handleVisibilityChanged(bool visible);
FancyMainWindowPrivate *d;
......
......@@ -290,18 +290,10 @@ void AnalyzerManagerPrivate::delayedInit()
// Populate Windows->Views menu with standard actions.
Context analyzerContext(C_ANALYZEMODE);
ActionContainer *viewsMenu = Core::ActionManager::actionContainer(Id(M_WINDOW_VIEWS));
Command *cmd = Core::ActionManager::registerAction(m_mainWindow->menuSeparator1(),
Command *cmd = Core::ActionManager::registerAction(m_mainWindow->menuSeparator(),
"Analyzer.Views.Separator1", analyzerContext);
cmd->setAttribute(Command::CA_Hide);
viewsMenu->addAction(cmd, G_DEFAULT_THREE);
cmd = Core::ActionManager::registerAction(m_mainWindow->toggleLockedAction(),
"Analyzer.Views.ToggleLocked", analyzerContext);
cmd->setAttribute(Command::CA_Hide);
viewsMenu->addAction(cmd, G_DEFAULT_THREE);
cmd = Core::ActionManager::registerAction(m_mainWindow->menuSeparator2(),
"Analyzer.Views.Separator2", analyzerContext);
cmd->setAttribute(Command::CA_Hide);
viewsMenu->addAction(cmd, G_DEFAULT_THREE);
cmd = Core::ActionManager::registerAction(m_mainWindow->resetLayoutAction(),
"Analyzer.Views.ResetSimple", analyzerContext);
cmd->setAttribute(Command::CA_Hide);
......
......@@ -313,16 +313,8 @@ void DebuggerMainWindowPrivate::createViewsMenuItems()
cmd->setAttribute(Command::CA_Hide);
m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
cmd = Core::ActionManager::registerAction(q->menuSeparator1(),
"Debugger.Views.Separator1", debugcontext);
cmd->setAttribute(Command::CA_Hide);
m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
cmd = Core::ActionManager::registerAction(q->toggleLockedAction(),
"Debugger.Views.ToggleLocked", debugcontext);
cmd->setAttribute(Command::CA_Hide);
m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
cmd = Core::ActionManager::registerAction(q->menuSeparator2(),
"Debugger.Views.Separator2", debugcontext);
cmd = Core::ActionManager::registerAction(q->menuSeparator(),
"Debugger.Views.Separator", debugcontext);
cmd->setAttribute(Command::CA_Hide);
m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
cmd = Core::ActionManager::registerAction(q->resetLayoutAction(),
......@@ -464,8 +456,6 @@ QWidget *DebuggerMainWindow::createContents(IMode *mode)
setDockNestingEnabled(true);
connect(this, SIGNAL(resetLayout()),
d, SLOT(resetDebuggerLayout()));
connect(toggleLockedAction(), SIGNAL(triggered()),
d, SLOT(updateDockWidgetSettings()));
QBoxLayout *editorHolderLayout = new QVBoxLayout;
editorHolderLayout->setMargin(0);
......
......@@ -2626,7 +2626,6 @@ void DebuggerPluginPrivate::createNewDock(QWidget *widget)
{
QDockWidget *dockWidget =
m_mainWindow->createDockWidget(CppLanguage, widget);
dockWidget->setWindowTitle(widget->windowTitle());
dockWidget->setFeatures(QDockWidget::DockWidgetClosable);
dockWidget->show();
}
......
......@@ -230,14 +230,8 @@ void FormEditorW::setupViewActions()
addDockViewAction(viewMenu, ActionEditorSubWindow, m_contexts,
tr("Action Editor"), "FormEditor.ActionEditor");
// Lock/Reset
Command *cmd = addToolAction(m_editorWidget->menuSeparator1(), m_contexts, "FormEditor.SeparatorLock", viewMenu);
cmd->setAttribute(Command::CA_Hide);
cmd = addToolAction(m_editorWidget->toggleLockedAction(), m_contexts, "FormEditor.Locked", viewMenu);
cmd->setAttribute(Command::CA_Hide);
cmd = addToolAction(m_editorWidget->menuSeparator2(), m_contexts, "FormEditor.SeparatorReset", viewMenu);
// Reset
Command *cmd = addToolAction(m_editorWidget->menuSeparator(), m_contexts, "FormEditor.SeparatorReset", viewMenu);
cmd->setAttribute(Command::CA_Hide);
cmd = addToolAction(m_editorWidget->resetLayoutAction(), m_contexts, "FormEditor.ResetToDefaultLayout", viewMenu);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment