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

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.

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. 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. More...
 

Public Member Functions

 DocksController (QMainWindow *mainWindow)
 
void addDockActionsToMenu (QMenu *menu)
 
void addWidget (int id, QWidget *widget, Qt::DockWidgetArea area)
 
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 setDockVisible (int id, bool visible=true)
 
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)
 
bool eventFilter (QObject *, QEvent *event) override
 
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
 

Constructor & Destructor Documentation

◆ DocksController()

DocksController::DocksController ( QMainWindow *  mainWindow)

Definition at line 43 of file DocksController.cpp.

44  : QObject(mainWindow)
46 {
47  m_mainWindow->setDocumentMode(true);
48  m_mainWindow->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::South);
49  m_mainWindow->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
50  m_mainWindow->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
51  m_mainWindow->installEventFilter(this);
52 }
QMainWindow * m_mainWindow
static QMainWindow * mainWindow
Definition: Globals.h:22

References m_mainWindow.

Member Function Documentation

◆ addDockActionsToMenu()

void DocksController::addDockActionsToMenu ( QMenu *  menu)

Definition at line 234 of file DocksController.cpp.

235 {
236  QList<QAction*> actions;
237  for (auto* dockWidget : dockWidgets()) {
238  if (dockWidget->parentWidget() == m_mainWindow) {
239  QAction* action = dockWidget->toggleViewAction();
240  action->setText(action->property("original_title").toString());
241  actions.append(action);
242  }
243  }
244  std::sort(actions.begin(), actions.end(), [](const QAction* action1, const QAction* action2) {
245  return stripAccelerator(action1->text()).toLower()
246  < stripAccelerator(action2->text()).toLower();
247  });
248 
249  foreach (QAction* action, actions)
250  menu->addAction(action);
251 }
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 54 of file DocksController.cpp.

55 {
56  auto* dockWidget = new QDockWidget(m_mainWindow);
57  dockWidget->setWidget(widget);
58  dockWidget->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable
59  | QDockWidget::DockWidgetFloatable);
60  dockWidget->setObjectName(widget->objectName() + QLatin1String("DockWidget"));
61 
62  QString title = widget->windowTitle();
63  dockWidget->toggleViewAction()->setProperty("original_title", title);
64  title = stripAccelerator(title);
65  dockWidget->setWindowTitle(title);
66 
67  connect(dockWidget->toggleViewAction(), &QAction::triggered, [=]() {
68  if (dockWidget->isVisible())
69  dockWidget->raise();
70  });
71 
72  connect(dockWidget, &QDockWidget::visibilityChanged, [this, dockWidget](bool visible) {
74  dockWidget->setProperty(dockWidgetActiveState, visible);
75  });
76 
77  dockWidget->setProperty(dockWidgetActiveState, true);
78 
79  return dockWidget;
80 }
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 82 of file DocksController.cpp.

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

References addDockForWidget(), Error, and m_docks.

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

Here is the call graph for this function:

◆ dockToMinMaxSizes

void DocksController::dockToMinMaxSizes ( )
slot

◆ dockWidgets()

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

Definition at line 148 of file DocksController.cpp.

149 {
150  return m_mainWindow->findChildren<QDockWidget*>();
151 }

References m_mainWindow.

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

◆ eventFilter()

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

Definition at line 224 of file DocksController.cpp.

225 {
226  if (event->type() == QEvent::Show)
228  else if (event->type() == QEvent::Hide)
230 
231  return QObject::eventFilter(obj, event);
232 }
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 131 of file DocksController.cpp.

132 {
133  if (m_docks.find(id) == m_docks.end())
134  return nullptr;
135 
136  return m_docks[id].dock();
137 }

References m_docks.

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

◆ findDock() [2/2]

QDockWidget * DocksController::findDock ( QWidget *  widget)

Definition at line 139 of file DocksController.cpp.

140 {
141  for (auto& it : m_docks)
142  if (it.second.widget() == widget)
143  return it.second.dock();
144 
145  return nullptr;
146 }

References m_docks.

◆ handleWindowVisibilityChanged()

void DocksController::handleWindowVisibilityChanged ( bool  visible)
private

Definition at line 213 of file DocksController.cpp.

214 {
216  for (auto* dockWidget : dockWidgets()) {
217  if (dockWidget->isFloating())
218  dockWidget->setVisible(visible && dockWidget->property(dockWidgetActiveState).toBool());
219  }
220  if (visible)
222 }

References dockWidgets(), and m_handleDockVisibilityChanges.

Referenced by eventFilter().

Here is the call graph for this function:

◆ resetLayout()

void DocksController::resetLayout ( )

Definition at line 96 of file DocksController.cpp.

97 {
98  setTrackingEnabled(false);
99  for (auto* dockWidget : dockWidgets()) {
100  dockWidget->setFloating(false);
101  m_mainWindow->removeDockWidget(dockWidget);
102  }
103 
104  for (auto& it : m_docks)
105  m_mainWindow->addDockWidget(it.second.area(), it.second.dock());
106 
107  // Fixes issue: https://bugreports.qt.io/browse/QTBUG-65592
108 #if QT_VERSION >= 0x050600
109  if (!dockWidgets().empty())
110  m_mainWindow->resizeDocks({dockWidgets().first()}, {10}, Qt::Horizontal);
111 #endif
112 
113  for (auto* dockWidget : dockWidgets())
114  dockWidget->show();
115 
116  setTrackingEnabled(true);
117 }
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 281 of file DocksController.cpp.

282 {
283  QByteArray ba = settings.value(QLatin1String(StateKey), QByteArray()).toByteArray();
284  if (!ba.isEmpty())
285  m_mainWindow->restoreState(ba, settingsVersion);
286  for (auto* widget : dockWidgets())
287  widget->setProperty(dockWidgetActiveState, settings.value(widget->objectName(), false));
288 }

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 263 of file DocksController.cpp.

264 {
265  QHash<QString, QVariant> hash;
266  foreach (const QString& key, settings->childKeys()) {
267  hash.insert(key, settings->value(key));
268  }
269  restoreSettings(hash);
270 }
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 272 of file DocksController.cpp.

273 {
274  QHash<QString, QVariant> settings;
275  settings.insert(QLatin1String(StateKey), m_mainWindow->saveState(settingsVersion));
276  for (auto* dockWidget : dockWidgets())
277  settings.insert(dockWidget->objectName(), dockWidget->property(dockWidgetActiveState));
278  return settings;
279 }

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 253 of file DocksController.cpp.

254 {
255  QHash<QString, QVariant> hash = saveSettings();
256  QHashIterator<QString, QVariant> it(hash);
257  while (it.hasNext()) {
258  it.next();
259  settings->setValue(it.key(), it.value());
260  }
261 }
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 173 of file DocksController.cpp.

174 {
175  QWidget* widget = qobject_cast<QWidget*>(sender());
176  ASSERT(widget);
177  QDockWidget* dock = findDock(widget);
178  ASSERT(dock);
179 
180  m_dock_info.m_dock = dock;
181  m_dock_info.m_min_size = dock->minimumSize();
182  m_dock_info.m_max_size = dock->maximumSize();
183 
184  if (height > 0) {
185  if (dock->height() < height)
186  dock->setMinimumHeight(height);
187  else
188  dock->setMaximumHeight(height);
189  }
190 
191  QTimer::singleShot(1, this, &DocksController::dockToMinMaxSizes);
192 }
QDockWidget * findDock(int id)

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

Here is the call graph for this function:

◆ setDockVisible()

void DocksController::setDockVisible ( int  id,
bool  visible = true 
)

Definition at line 125 of file DocksController.cpp.

126 {
127  if (auto* dock = findDock(id))
128  dock->setHidden(!visible);
129 }

References findDock().

Referenced by SampleView::onRequestViewInRealSpace(), and SampleView::updateSingleSampleMode().

Here is the call graph for this function:

◆ setTrackingEnabled()

void DocksController::setTrackingEnabled ( bool  enabled)
private

Definition at line 202 of file DocksController.cpp.

203 {
204  if (enabled) {
206  for (auto* dockWidget : dockWidgets())
207  dockWidget->setProperty(dockWidgetActiveState, dockWidget->isVisible());
208  } else {
210  }
211 }

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 155 of file DocksController.cpp.

156 {
157  for (auto& it : m_docks) {
158  if (std::find(visibleDocks.begin(), visibleDocks.end(), it.first) != visibleDocks.end())
159  it.second.dock()->show();
160  else
161  it.second.dock()->hide();
162  }
163 }

References m_docks.

Referenced by JobView::setActivity().

◆ toggleDock()

void DocksController::toggleDock ( int  id)

Definition at line 119 of file DocksController.cpp.

120 {
121  if (auto* dock = findDock(id))
122  dock->setHidden(!dock->isHidden());
123 }

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 72 of file DocksController.h.

Referenced by dockToMinMaxSizes(), and setDockHeightForWidget().

◆ m_docks

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

Definition at line 71 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: