CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxConsoleWidgetCollection.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include <QDockWidget>
15 #include <QMenu>
16 #include <QTimer>
17 //#include <QApplication>
18 #include <QDebug>
19 //#include <QDesktopWidget>
20 #include <QBuffer>
21 #include <QDataStream>
22 #include "cxProfile.h"
23 #include "cxConsoleWidget.h"
24 #include "cxNullDeleter.h"
25 
26 namespace cx
27 {
28 
29 ConsoleWidgetCollection::ConsoleWidgetCollection(QWidget* parent, QString objectName, QString windowTitle, XmlOptionFile options, LogPtr log) :
30  QMainWindow(parent), mObjectName(objectName), mWindowTitle(windowTitle)
31 {
32  this->setObjectName(mObjectName);
33  this->setWindowTitle(mWindowTitle);
34  mOptions = options;
35  mLog = log;
36 
37  this->setupUI();
38 }
39 
40 ConsoleWidgetCollection::ConsoleWidgetCollection(QWidget* parent, QString objectName, QString windowTitle) :
41  QMainWindow(parent), mObjectName(objectName), mWindowTitle(windowTitle)
42 {
43  this->setObjectName(mObjectName);
44  this->setWindowTitle(mWindowTitle);
45  mOptions = profile()->getXmlSettings().descend(this->objectName());
46  mLog = reporter();
47 
48  this->setupUI();
49 }
50 
51 void ConsoleWidgetCollection::setupUI()
52 {
53  this->setFocusPolicy(Qt::StrongFocus); // needed for help system: focus is used to display help text
54 
55  this->setDockNestingEnabled(true);
56  this->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North);
57 
58  int consoleCount = this->option("consoleCount").readVariant(-1).toInt();
59 
60  for (int i=0; i<consoleCount; ++i)
61  {
62  this->addConsole();
63  }
64 
65  // Restore saved window states
66  // Must be done after all DockWidgets are created
67 
68  this->restoreState(this->option("windowState").readVariant().toByteArray());
69 
70  // default: add two consoles
71  if (mDockWidgets.empty())
72  {
73  this->onNewConsole();
74  this->onNewConsole();
75  }
76 }
77 
78 XmlOptionItem ConsoleWidgetCollection::option(QString name)
79 {
80  return XmlOptionItem(name, mOptions.getElement());
81 }
82 
84 {
85  this->option("windowState").writeVariant(this->saveState());
86  this->option("consoleCount").writeVariant(QVariant::fromValue<int>(mDockWidgets.size()));
87 }
88 
89 void ConsoleWidgetCollection::onDockWidgetVisibilityChanged(bool val)
90 {
91  if (!this->isVisible())
92  {
93 // std::cout << "ignore remove - parent hidden" << std::endl;
94  return;
95  }
96 
97  QTimer::singleShot(0, this, SLOT(checkVisibility()));
98 }
99 
100 void ConsoleWidgetCollection::checkVisibility()
101 {
102  this->removeHiddenConsoles();
103 }
104 
105 void ConsoleWidgetCollection::removeHiddenConsoles()
106 {
107  for (int i=0; i<mDockWidgets.size(); )
108  {
109  if (!mDockWidgets[i]->isVisible())
110  this->deleteDockWidget(mDockWidgets[i]);
111  else
112  ++i;
113  }
114 }
115 
116 QDockWidget* ConsoleWidgetCollection::addAsDockWidget(QWidget* widget)
117 {
118  QDockWidget* dockWidget = this->createDockWidget(widget);
119 
120  mDockWidgets.push_back(dockWidget);
121 
122  connect(dockWidget, &QDockWidget::visibilityChanged,
123  this, &ConsoleWidgetCollection::onDockWidgetVisibilityChanged);
124 
125  this->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
126  return dockWidget;
127 }
128 
129 QDockWidget* ConsoleWidgetCollection::createDockWidget(QWidget* widget)
130 {
131  QDockWidget* dockWidget = new QDockWidget(widget->windowTitle(), this);
132  connect(widget, &QWidget::windowTitleChanged, this, &ConsoleWidgetCollection::onConsoleWindowTitleChanged);
133  dockWidget->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);
134  dockWidget->setObjectName(widget->objectName() + "DockWidget");
135  dockWidget->setWidget(widget);
136  return dockWidget;
137 }
138 
139 void ConsoleWidgetCollection::onConsoleWindowTitleChanged(const QString & title)
140 {
141  QWidget* widget = dynamic_cast<QWidget*>(sender());
142  QDockWidget* dockWidget = dynamic_cast<QDockWidget*>(sender()->parent());
143  if (!widget || !dockWidget)
144  return;
145  dockWidget->setWindowTitle(widget->windowTitle());
146 }
147 
148 void ConsoleWidgetCollection::deleteDockWidget(QDockWidget* dockWidget)
149 {
150  disconnect(dockWidget, &QDockWidget::visibilityChanged,
151  this, &ConsoleWidgetCollection::onDockWidgetVisibilityChanged);
152 
153  this->removeDockWidget(dockWidget);
154  mDockWidgets.removeAll(dockWidget);
155  dockWidget->deleteLater();
156 
157  for (int i=0; i<mDockWidgets.size(); ++i)
158  {
159  QString uid = QString("ConsoleWidget%1").arg(i);
160  QString dock_uid = uid + "DockWidget";
161  mDockWidgets[i]->setObjectName(dock_uid);
162  mDockWidgets[i]->widget()->setObjectName(uid);
163  }
164 }
165 
166 ConsoleWidget* ConsoleWidgetCollection::addConsole()
167 {
168  QString uid = QString("ConsoleWidget%1").arg(mDockWidgets.size());
169  ConsoleWidget* console = new ConsoleWidget(this, uid, "Console", mOptions.descend(uid), mLog);
170  this->addAsDockWidget(console);
171  return console;
172 }
173 
174 void ConsoleWidgetCollection::onNewConsole()
175 {
176  ConsoleWidget* console = this->addConsole();
177  console->setDetails(true);
178 }
179 
181 {
182  this->removeHiddenConsoles();
183 
184  QMenu* popupMenu = new QMenu(this);
185 
186  QAction* addConsoleAction = new QAction("New Console", this);
187  connect(addConsoleAction, &QAction::triggered, this, &ConsoleWidgetCollection::onNewConsole);
188  popupMenu->addAction(addConsoleAction);
189 
190  for (int i=0; i<mDockWidgets.size(); ++i)
191  {
192  popupMenu->addAction(mDockWidgets[i]->toggleViewAction());
193  }
194 
195  return popupMenu;
196 }
197 
198 
199 
200 } //namespace cx
201 
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
ConsoleWidgetCollection(QWidget *parent, QString objectName, QString windowTitle, XmlOptionFile options, LogPtr log)
ReporterPtr reporter()
Definition: cxReporter.cpp:36
void writeVariant(const QVariant &val)
Widget for displaying status messages.
QDomElement getElement()
return the current element
Helper class for storing one string value in an xml document.
boost::shared_ptr< class Log > LogPtr
Definition: cxLog.h:48
void setDetails(bool on)
QVariant readVariant(const QVariant &defval=QVariant()) const
Helper class for xml files used to store ssc/cx data.
XmlOptionFile descend(QString element) const
step one level down in the xml tree
Namespace for all CustusX production code.