BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DocksController Class Reference

Handles creation and appearance of docked widgets in the context of QMainWindow. More...

Inheritance diagram for DocksController:
[legend]
Collaboration diagram for DocksController:
[legend]

Classes

struct  DockSizeInfo
 

Public Slots

void dockToMinMaxSizes ()
 
void setDockHeightForWidget (int height)
 A hack to request update of QDockWidget size if its child (e.g. More...
 

Public Member Functions

 DocksController (QMainWindow *mainWindow)
 
void addDockActionsToMenu (QMenu *menu)
 
void addWidget (int id, QWidget *widget, Qt::DockWidgetArea area)
 
const QList< QDockWidget * > dockWidgets () const
 
QDockWidget * findDock (int id)
 
QDockWidget * findDock (QWidget *widget)
 
void resetLayout ()
 
void restoreSettings (const QHash< QString, QVariant > &settings)
 
void restoreSettings (const QSettings *settings)
 
QHash< QString, QVariant > saveSettings () const
 
void saveSettings (QSettings *settings) const
 
void setVisibleDocks (const std::vector< int > &visibleDocks)
 Show docks with id's from the list. Other docks will be hidden. More...
 
void toggleDock (int id)
 

Private Member Functions

QDockWidget * addDockForWidget (QWidget *widget)
 
virtual bool eventFilter (QObject *, QEvent *event)
 
void handleWindowVisibilityChanged (bool visible)
 
void setTrackingEnabled (bool enabled)
 

Private Attributes

DockSizeInfo m_dock_info
 
std::map< int, DockWidgetInfom_docks
 
bool m_handleDockVisibilityChanges = true
 
QMainWindow * m_mainWindow
 

Detailed Description

Handles creation and appearance of docked widgets in the context of QMainWindow.

It is used for SampleView and JobView which are based on QMainWindow.

Definition at line 30 of file DocksController.h.

Constructor & Destructor Documentation

◆ DocksController()

DocksController::DocksController ( QMainWindow *  mainWindow)

Definition at line 42 of file DocksController.cpp.

43  : QObject(mainWindow), m_mainWindow(mainWindow)
44 {
45  m_mainWindow->setDocumentMode(true);
46  m_mainWindow->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::South);
47  m_mainWindow->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
48  m_mainWindow->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
49  m_mainWindow->installEventFilter(this);
50 }
QMainWindow * m_mainWindow

References m_mainWindow.

Member Function Documentation

◆ addDockActionsToMenu()

void DocksController::addDockActionsToMenu ( QMenu *  menu)

Definition at line 225 of file DocksController.cpp.

226 {
227  QList<QAction*> actions;
228  for (auto dockWidget : dockWidgets()) {
229  if (dockWidget->property("managed_dockwidget").isNull()
230  && dockWidget->parentWidget() == m_mainWindow) {
231  QAction* action = dockWidget->toggleViewAction();
232  action->setText(action->property("original_title").toString());
233  actions.append(action);
234  }
235  }
236  std::sort(actions.begin(), actions.end(), [](const QAction* action1, const QAction* action2) {
237  return stripAccelerator(action1->text()).toLower()
238  < stripAccelerator(action2->text()).toLower();
239  });
240 
241  foreach (QAction* action, actions)
242  menu->addAction(action);
243 }
const QList< QDockWidget * > dockWidgets() const

References dockWidgets(), and m_mainWindow.

Referenced by JobView::fillViewMenu(), and SampleView::fillViewMenu().

Here is the call graph for this function:

◆ addDockForWidget()

QDockWidget * DocksController::addDockForWidget ( QWidget *  widget)
private

Definition at line 52 of file DocksController.cpp.

53 {
54  auto dockWidget = new QDockWidget(m_mainWindow);
55  dockWidget->setWidget(widget);
56  dockWidget->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable
57  | QDockWidget::DockWidgetFloatable);
58  dockWidget->setObjectName(widget->objectName() + QLatin1String("DockWidget"));
59 
60  QString title = widget->windowTitle();
61  dockWidget->toggleViewAction()->setProperty("original_title", title);
62  title = stripAccelerator(title);
63  dockWidget->setWindowTitle(title);
64 
65  connect(dockWidget->toggleViewAction(), &QAction::triggered, [=]() {
66  if (dockWidget->isVisible())
67  dockWidget->raise();
68  });
69 
70  connect(dockWidget, &QDockWidget::visibilityChanged, [this, dockWidget](bool visible) {
72  dockWidget->setProperty(dockWidgetActiveState, visible);
73  });
74 
75  dockWidget->setProperty(dockWidgetActiveState, true);
76 
77  return dockWidget;
78 }
bool m_handleDockVisibilityChanges

References m_handleDockVisibilityChanges, and m_mainWindow.

Referenced by addWidget().

◆ addWidget()

void DocksController::addWidget ( int  id,
QWidget *  widget,
Qt::DockWidgetArea  area 
)

Definition at line 80 of file DocksController.cpp.

81 {
82  if (m_docks.find(id) != m_docks.end())
83  throw GUIHelpers::Error("DocksController::addWidget() -> Error. "
84  "Attempt to add widget id twice");
85 
86  auto dock = addDockForWidget(widget);
87  m_docks[id] = DockWidgetInfo(dock, widget, area);
88 
89  QList<QAbstractItemView*> frames = widget->findChildren<QAbstractItemView*>();
90  for (int i = 0; i < frames.count(); ++i)
91  frames[i]->setFrameStyle(QFrame::NoFrame);
92 }
Holds information about the widget and its dock.
QDockWidget * addDockForWidget(QWidget *widget)
std::map< int, DockWidgetInfo > m_docks

References addDockForWidget(), and m_docks.

Referenced by JobView::createSubWindows(), and SampleView::createSubWindows().

Here is the call graph for this function:

◆ dockToMinMaxSizes

void DocksController::dockToMinMaxSizes ( )
slot

◆ dockWidgets()

const QList< QDockWidget * > DocksController::dockWidgets ( ) const

Definition at line 138 of file DocksController.cpp.

139 {
140  return m_mainWindow->findChildren<QDockWidget*>();
141 }

References m_mainWindow.

Referenced by addDockActionsToMenu(), handleWindowVisibilityChanged(), resetLayout(), restoreSettings(), saveSettings(), and setTrackingEnabled().

◆ eventFilter()

bool DocksController::eventFilter ( QObject *  obj,
QEvent *  event 
)
privatevirtual

Definition at line 215 of file DocksController.cpp.

216 {
217  if (event->type() == QEvent::Show)
219  else if (event->type() == QEvent::Hide)
221 
222  return QObject::eventFilter(obj, event);
223 }
void handleWindowVisibilityChanged(bool visible)

References handleWindowVisibilityChanged().

Here is the call graph for this function:

◆ findDock() [1/2]

QDockWidget * DocksController::findDock ( int  id)

Definition at line 123 of file DocksController.cpp.

124 {
125  ASSERT(m_docks.find(id) != m_docks.end());
126  return m_docks[id].dock();
127 }

References ASSERT, and m_docks.

Referenced by SampleView::resetLayout(), setDockHeightForWidget(), and toggleDock().

◆ findDock() [2/2]

QDockWidget * DocksController::findDock ( QWidget *  widget)

Definition at line 129 of file DocksController.cpp.

130 {
131  for (auto& it : m_docks)
132  if (it.second.widget() == widget)
133  return it.second.dock();
134 
135  throw GUIHelpers::Error("DocksController::findDock() -> Can't find dock for widget");
136 }

References m_docks.

◆ handleWindowVisibilityChanged()

void DocksController::handleWindowVisibilityChanged ( bool  visible)
private

Definition at line 203 of file DocksController.cpp.

204 {
206  for (auto dockWidget : dockWidgets()) {
207  if (dockWidget->isFloating()) {
208  dockWidget->setVisible(visible && dockWidget->property(dockWidgetActiveState).toBool());
209  }
210  }
211  if (visible)
213 }

References dockWidgets(), and m_handleDockVisibilityChanges.

Referenced by eventFilter().

Here is the call graph for this function:

◆ resetLayout()

void DocksController::resetLayout ( )

Definition at line 94 of file DocksController.cpp.

95 {
96  setTrackingEnabled(false);
97  for (auto dockWidget : dockWidgets()) {
98  dockWidget->setFloating(false);
99  m_mainWindow->removeDockWidget(dockWidget);
100  }
101 
102  for (auto& it : m_docks)
103  m_mainWindow->addDockWidget(it.second.area(), it.second.dock());
104 
105  // Fixes issue: https://bugreports.qt.io/browse/QTBUG-65592
106 #if QT_VERSION >= 0x050600
107  if (dockWidgets().size() > 0)
108  m_mainWindow->resizeDocks({dockWidgets().first()}, {10}, Qt::Horizontal);
109 #endif
110 
111  for (auto dockWidget : dockWidgets())
112  dockWidget->show();
113 
114  setTrackingEnabled(true);
115 }
void setTrackingEnabled(bool enabled)

References dockWidgets(), m_docks, m_mainWindow, and setTrackingEnabled().

Referenced by JobView::resetLayout(), and SampleView::resetLayout().

Here is the call graph for this function:

◆ restoreSettings() [1/2]

void DocksController::restoreSettings ( const QHash< QString, QVariant > &  settings)

Definition at line 274 of file DocksController.cpp.

275 {
276  QByteArray ba = settings.value(QLatin1String(StateKey), QByteArray()).toByteArray();
277  if (!ba.isEmpty())
278  m_mainWindow->restoreState(ba, settingsVersion);
279  for (auto widget : dockWidgets()) {
280  widget->setProperty(dockWidgetActiveState, settings.value(widget->objectName(), false));
281  }
282 }

References dockWidgets(), and m_mainWindow.

Referenced by restoreSettings().

Here is the call graph for this function:

◆ restoreSettings() [2/2]

void DocksController::restoreSettings ( const QSettings *  settings)

Definition at line 255 of file DocksController.cpp.

256 {
257  QHash<QString, QVariant> hash;
258  foreach (const QString& key, settings->childKeys()) {
259  hash.insert(key, settings->value(key));
260  }
261  restoreSettings(hash);
262 }
void restoreSettings(const QHash< QString, QVariant > &settings)

References restoreSettings().

Here is the call graph for this function:

◆ saveSettings() [1/2]

QHash< QString, QVariant > DocksController::saveSettings ( ) const

Definition at line 264 of file DocksController.cpp.

265 {
266  QHash<QString, QVariant> settings;
267  settings.insert(QLatin1String(StateKey), m_mainWindow->saveState(settingsVersion));
268  for (auto dockWidget : dockWidgets()) {
269  settings.insert(dockWidget->objectName(), dockWidget->property(dockWidgetActiveState));
270  }
271  return settings;
272 }

References dockWidgets(), and m_mainWindow.

Referenced by saveSettings().

Here is the call graph for this function:

◆ saveSettings() [2/2]

void DocksController::saveSettings ( QSettings *  settings) const

Definition at line 245 of file DocksController.cpp.

246 {
247  QHash<QString, QVariant> hash = saveSettings();
248  QHashIterator<QString, QVariant> it(hash);
249  while (it.hasNext()) {
250  it.next();
251  settings->setValue(it.key(), it.value());
252  }
253 }
QHash< QString, QVariant > saveSettings() const

References saveSettings().

Here is the call graph for this function:

◆ setDockHeightForWidget

void DocksController::setDockHeightForWidget ( int  height)
slot

A hack to request update of QDockWidget size if its child (e.g.

InfoWidget) wants it. The problem bypassed here is that there is no direct method to QMainWindow to recalculate position of splitters surrounding given QDockWidget. So our child QWidget requests here the change of Min/Max size of QDockWidget, this will trigger recalculation of QDockWidget layout and will force QDockWidget to respect sizeHints provided by ChildWidget. Later (in one single timer shot) we return min/max sizes of QDockWidget back to re-enable splitters functionality.

Definition at line 163 of file DocksController.cpp.

164 {
165  QWidget* widget = qobject_cast<QWidget*>(sender());
166  ASSERT(widget);
167  QDockWidget* dock = findDock(widget);
168  ASSERT(dock);
169 
170  m_dock_info.m_dock = dock;
171  m_dock_info.m_min_size = dock->minimumSize();
172  m_dock_info.m_max_size = dock->maximumSize();
173 
174  if (height > 0) {
175  if (dock->height() < height)
176  dock->setMinimumHeight(height);
177  else
178  dock->setMaximumHeight(height);
179  }
180 
181  QTimer::singleShot(1, this, &DocksController::dockToMinMaxSizes);
182 }
QDockWidget * findDock(int id)

References ASSERT, dockToMinMaxSizes(), findDock(), DocksController::DockSizeInfo::m_dock, m_dock_info, DocksController::DockSizeInfo::m_max_size, and DocksController::DockSizeInfo::m_min_size.

Referenced by JobView::createSubWindows(), and SampleView::createSubWindows().

Here is the call graph for this function:

◆ setTrackingEnabled()

void DocksController::setTrackingEnabled ( bool  enabled)
private

Definition at line 192 of file DocksController.cpp.

193 {
194  if (enabled) {
196  for (auto dockWidget : dockWidgets())
197  dockWidget->setProperty(dockWidgetActiveState, dockWidget->isVisible());
198  } else {
200  }
201 }

References dockWidgets(), and m_handleDockVisibilityChanges.

Referenced by resetLayout().

Here is the call graph for this function:

◆ setVisibleDocks()

void DocksController::setVisibleDocks ( const std::vector< int > &  visibleDocks)

Show docks with id's from the list. Other docks will be hidden.

Definition at line 145 of file DocksController.cpp.

146 {
147  for (auto& it : m_docks) {
148  if (std::find(visibleDocks.begin(), visibleDocks.end(), it.first) != visibleDocks.end())
149  it.second.dock()->show();
150  else
151  it.second.dock()->hide();
152  }
153 }

References m_docks.

Referenced by JobView::setActivity().

◆ toggleDock()

void DocksController::toggleDock ( int  id)

Definition at line 117 of file DocksController.cpp.

118 {
119  auto dock = findDock(id);
120  dock->setHidden(!dock->isHidden());
121 }

References findDock().

Referenced by SampleView::toggleRealSpaceView().

Here is the call graph for this function:

Member Data Documentation

◆ m_dock_info

DockSizeInfo DocksController::m_dock_info
private

Definition at line 71 of file DocksController.h.

Referenced by dockToMinMaxSizes(), and setDockHeightForWidget().

◆ m_docks

std::map<int, DockWidgetInfo> DocksController::m_docks
private

Definition at line 70 of file DocksController.h.

Referenced by addWidget(), findDock(), resetLayout(), and setVisibleDocks().

◆ m_handleDockVisibilityChanges

bool DocksController::m_handleDockVisibilityChanges = true
private

◆ m_mainWindow

QMainWindow* DocksController::m_mainWindow
private

The documentation for this class was generated from the following files: