15 mMainWindow(mainWindow)
18 mFirstDummyToolbar =
new QToolBar;
19 mFirstDummyToolbar->setObjectName(
"dummy_toolbar");
21 mMainWindow->addToolBar(mFirstDummyToolbar);
26 QDockWidget* dockWidget = this->createDockWidget(widget);
27 mMainWindow->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
29 this->tabifyWidget(dockWidget);
32 item.mWidget = dockWidget;
33 dockWidget->setVisible(
false);
35 item.mGroup = groupname;
36 item.mName = widget->windowTitle();
37 mMainWindow->restoreDockWidget(dockWidget);
39 item.mOwningService = owningPlugin;
41 connect(dockWidget->toggleViewAction(), &QAction::triggered,
this, &DynamicMainWindowWidgets::onWidgetActionTriggered);
43 mItems.push_back(item);
50 item.mWidget = toolbar;
51 item.mGroup = groupname;
52 item.mName = toolbar->windowTitle();
53 mItems.push_back(item);
55 mMainWindow->addToolBar(toolbar);
61 QDockWidget* DynamicMainWindowWidgets::createDockWidget(QWidget* widget)
63 QScrollArea* scroller = this->addVerticalScroller(widget);
64 QDockWidget* dockWidget =
new QDockWidget(widget->windowTitle(), mMainWindow);
65 connect(widget, &QWidget::windowTitleChanged,
this, &DynamicMainWindowWidgets::onConsoleWindowTitleChanged);
66 dockWidget->setObjectName(widget->objectName() +
"DockWidget");
67 dockWidget->setFocusPolicy(Qt::StrongFocus);
68 dockWidget->setWidget(scroller);
74 for (
unsigned i=0; i<mItems.size(); ++i)
76 DynamicWidget item = mItems[i];
78 if (item.mName != name)
83 QDockWidget* dockWidget =
dynamic_cast<QDockWidget*
>(item.mWidget);
97 for (
unsigned i=0; i<desktop.
mPresets.size(); ++i)
98 this->restorePreset(desktop.
mPresets[i]);
101 void DynamicMainWindowWidgets::restorePreset(
const Desktop::Preset& preset)
103 QToolBar* tb = mMainWindow->findChild<QToolBar*>(preset.
name);
107 mMainWindow->removeToolBar(tb);
108 mMainWindow->insertToolBar(mFirstDummyToolbar, tb);
113 QDockWidget* dw = mMainWindow->findChild<QDockWidget*>(preset.
name+
"DockWidget");
119 QDockWidget* first = this->getFirstDockWidget(Qt::DockWidgetArea(preset.
position));
121 mMainWindow->tabifyDockWidget(first, dw);
123 mMainWindow->addDockWidget(Qt::DockWidgetArea(preset.
position), dw);
127 mMainWindow->addDockWidget(Qt::DockWidgetArea(preset.
position), dw);
132 CX_LOG_WARNING() << QString(
"Attempted to restore a nonexitent preset widget: [%1]").arg(preset.
name);
135 void DynamicMainWindowWidgets::onWidgetActionTriggered(
bool checked)
137 QAction* action =
dynamic_cast<QAction*
>(sender());
140 QWidget* widget =
dynamic_cast<QWidget*
>(action->parent());
152 for (
unsigned i=0; i<mItems.size(); )
154 DynamicWidget item = mItems[i];
156 if (item.mOwningService != service)
163 mItems.erase(mItems.begin()+i);
165 QDockWidget* dockWidget =
dynamic_cast<QDockWidget*
>(item.mWidget);
167 mMainWindow->removeDockWidget(dockWidget);
172 void DynamicMainWindowWidgets::onConsoleWindowTitleChanged(
const QString & title)
174 QWidget* widget =
dynamic_cast<QWidget*
>(sender());
176 for (
int i=0; i<3; ++i)
180 QDockWidget* dockWidget =
dynamic_cast<QDockWidget*
>(widget);
183 dockWidget->setWindowTitle(title);
186 widget = widget->parentWidget();
190 QScrollArea* DynamicMainWindowWidgets::addVerticalScroller(QWidget *widget)
192 QScrollArea* scroller =
new QScrollArea(NULL);
193 scroller->setWidget(widget);
194 widget->setParent(scroller);
195 scroller->setWidgetResizable(
true);
196 scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
197 QSizePolicy policy = scroller->sizePolicy();
198 policy.setHorizontalPolicy(QSizePolicy::Minimum);
199 scroller->setSizePolicy(policy);
203 void DynamicMainWindowWidgets::tabifyWidget(QDockWidget* dockWidget)
205 QDockWidget* widget = this->getFirstDockWidget(Qt::LeftDockWidgetArea);
208 mMainWindow->tabifyDockWidget(widget, dockWidget);
211 QDockWidget* DynamicMainWindowWidgets::getFirstDockWidget(Qt::DockWidgetArea area)
213 for (
unsigned i=0; i<mItems.size(); ++i)
215 QDockWidget* widget =
dynamic_cast<QDockWidget*
>(mItems[i].mWidget);
218 if (mMainWindow->dockWidgetArea(widget) == Qt::LeftDockWidgetArea)
226 for (
unsigned i=0; i<mItems.size(); ++i)
228 mItems[i].mWidget->hide();
230 QToolBar* toolbar =
dynamic_cast<QToolBar*
>(mItems[i].mWidget);
234 mMainWindow->removeToolBar(toolbar);
235 mMainWindow->addToolBar(toolbar);
241 void DynamicMainWindowWidgets::addToWidgetGroupMap(ActionGroupMap& groups, QAction* action, QString groupname)
243 action->setMenuRole(QAction::NoRole);
245 if (!groups.count(groupname))
247 QActionGroup* group =
new QActionGroup(
this);
248 group->setExclusive(
false);
249 groups[groupname] = group;
251 QAction* heading =
new QAction(groupname,
this);
252 heading->setDisabled(
true);
253 groups[groupname]->addAction(heading);
256 groups[groupname]->addAction(action);
263 QMenu* popupMenu =
new QMenu;
265 ActionGroupMap groups;
266 ActionGroupMap tgroups;
267 for (
unsigned i=0; i<mItems.size(); ++i)
269 QDockWidget* widget =
dynamic_cast<QDockWidget*
>(mItems[i].mWidget);
271 this->addToWidgetGroupMap(groups, widget->toggleViewAction(), mItems[i].mGroup);
272 QToolBar* toolbar =
dynamic_cast<QToolBar*
>(mItems[i].mWidget);
274 this->addToWidgetGroupMap(tgroups, toolbar->toggleViewAction(), mItems[i].mGroup);
277 QMenu* widgets = popupMenu->addMenu(
"Widgets");
279 for(std::map<QString, QActionGroup*>::iterator it = groups.begin(); it != groups.end(); ++it)
281 widgets->addSeparator();
282 widgets->addActions(it->second->actions());
285 QMenu* toolbars = popupMenu->addMenu(
"Toolbars");
287 for(std::map<QString, QActionGroup*>::iterator it = tgroups.begin(); it != tgroups.end(); ++it)
289 toolbars->addSeparator();
290 toolbars->addActions(it->second->actions());
std::vector< Preset > mPresets
void registerToolBar(QToolBar *toolbar, QString groupname="Toolbars")
DynamicMainWindowWidgets(QMainWindow *mainWindow)
void restoreFrom(const Desktop &desktop)
QMenu * createPopupMenu()
QByteArray mMainWindowState
void showWidget(QString name)
Data class for CustusX desktop.
void owningServiceRemoved(QObject *service)
QDockWidget * addAsDockWidget(QWidget *widget, QString groupname, QObject *owningPlugin=NULL)