16 #include "Base/Util/Assert.h"
18 #include <QAbstractItemView>
20 #include <QDockWidget>
22 #include <QMainWindow>
29 const char dockWidgetActiveState[] =
"DockWidgetActiveState";
30 const char StateKey[] =
"State";
31 const int settingsVersion = 2;
33 QString stripAccelerator(
const QString& text)
36 for (
int index = res.indexOf(
'&'); index != -1; index = res.indexOf(
'&', index + 1))
48 m_mainWindow->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::South);
49 m_mainWindow->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
50 m_mainWindow->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
57 dockWidget->setWidget(widget);
58 dockWidget->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable
59 | QDockWidget::DockWidgetFloatable);
60 dockWidget->setObjectName(widget->objectName() + QLatin1String(
"DockWidget"));
62 QString title = widget->windowTitle();
63 dockWidget->toggleViewAction()->setProperty(
"original_title", title);
64 title = stripAccelerator(title);
65 dockWidget->setWindowTitle(title);
67 connect(dockWidget->toggleViewAction(), &QAction::triggered, [=]() {
68 if (dockWidget->isVisible())
72 connect(dockWidget, &QDockWidget::visibilityChanged, [
this, dockWidget](
bool visible) {
74 dockWidget->setProperty(dockWidgetActiveState, visible);
77 dockWidget->setProperty(dockWidgetActiveState,
true);
85 throw Error(
"DocksController::addWidget() -> Error. "
86 "Attempt to add widget id twice");
91 QList<QAbstractItemView*> frames = widget->findChildren<QAbstractItemView*>();
92 for (
int i = 0; i < frames.count(); ++i)
93 frames[i]->setFrameStyle(QFrame::NoFrame);
100 dockWidget->setFloating(
false);
105 m_mainWindow->addDockWidget(it.second.area(), it.second.dock());
108 #if QT_VERSION >= 0x050600
122 dock->setHidden(!dock->isHidden());
128 dock->setHidden(!visible);
142 if (it.second.widget() == widget)
143 return it.second.dock();
158 if (std::find(visibleDocks.begin(), visibleDocks.end(), it.first) != visibleDocks.end())
159 it.second.dock()->show();
161 it.second.dock()->hide();
175 QWidget* widget = qobject_cast<QWidget*>(sender());
177 QDockWidget* dock =
findDock(widget);
185 if (dock->height() < height)
186 dock->setMinimumHeight(height);
188 dock->setMaximumHeight(height);
207 dockWidget->setProperty(dockWidgetActiveState, dockWidget->isVisible());
217 if (dockWidget->isFloating())
218 dockWidget->setVisible(visible && dockWidget->property(dockWidgetActiveState).toBool());
226 if (event->type() == QEvent::Show)
228 else if (event->type() == QEvent::Hide)
231 return QObject::eventFilter(obj, event);
236 QList<QAction*> actions;
239 QAction* action = dockWidget->toggleViewAction();
240 action->setText(action->property(
"original_title").toString());
241 actions.append(action);
244 std::sort(actions.begin(), actions.end(), [](
const QAction* action1,
const QAction* action2) {
245 return stripAccelerator(action1->text()).toLower()
246 < stripAccelerator(action2->text()).toLower();
249 foreach (QAction* action, actions)
250 menu->addAction(action);
256 QHashIterator<QString, QVariant> it(hash);
257 while (it.hasNext()) {
259 settings->setValue(it.key(), it.value());
265 QHash<QString, QVariant> hash;
266 foreach (
const QString& key, settings->childKeys()) {
267 hash.insert(key, settings->value(key));
274 QHash<QString, QVariant> settings;
275 settings.insert(QLatin1String(StateKey),
m_mainWindow->saveState(settingsVersion));
277 settings.insert(dockWidget->objectName(), dockWidget->property(dockWidgetActiveState));
283 QByteArray ba = settings.value(QLatin1String(StateKey), QByteArray()).toByteArray();
287 widget->setProperty(dockWidgetActiveState, settings.value(widget->objectName(),
false));
Defines class DocksController.
void setDockVisible(int id, bool visible=true)
bool m_handleDockVisibilityChanges
void restoreSettings(const QHash< QString, QVariant > &settings)
QDockWidget * addDockForWidget(QWidget *widget)
QHash< QString, QVariant > saveSettings() const
void setDockHeightForWidget(int height)
A hack to request update of QDockWidget size if its child (e.g. InfoWidget) wants it....
void addDockActionsToMenu(QMenu *menu)
void addWidget(int id, QWidget *widget, Qt::DockWidgetArea area)
QDockWidget * findDock(int id)
bool eventFilter(QObject *, QEvent *event) override
void handleWindowVisibilityChanged(bool visible)
void setVisibleDocks(const std::vector< int > &visibleDocks)
Show docks with id's from the list. Other docks will be hidden.
DocksController(QMainWindow *mainWindow)
QMainWindow * m_mainWindow
void setTrackingEnabled(bool enabled)
QList< QDockWidget * > dockWidgets() const
std::map< int, DockWidgetInfo > m_docks
static QMainWindow * mainWindow