Debian Codegraph @

src/Gui/DockWindowManager.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <array>
24 #include <QAction>
25 #include <QApplication>
26 #include <QDockWidget>
27 #include <QMap>
28 #include <QMouseEvent>
29 #include <QPointer>
30 #include <QTimer>
31
32 #include <boost/algorithm/string/predicate.hpp>
33
34 #include <App/Application.h>
35 #include <Base/Tools.h>
36
37 #include "DockWindowManager.h"
38 #include "MainWindow.h"
39 #include "OverlayManager.h"
40
41
42 using namespace Gui;
43
44 DockWindowItems::DockWindowItems() = default;
45
46 DockWindowItems::~DockWindowItems() = default;
47
48 void DockWindowItems::addDockWidget(const char* name, Qt::DockWidgetArea pos, DockWindowOptions option)
49 {
50 DockWindowItem item;
51 item.name = QString::fromUtf8(name);
52 item.pos = pos;
53 item.visibility = option.testFlag(DockWindowOption::Visible);
54 item.tabbed = option.testFlag(DockWindowOption::HiddenTabbed);
55 _items << item;
56 }
57
58 void DockWindowItems::setDockingArea(const char* name, Qt::DockWidgetArea pos)
59 {
60 for (QList<DockWindowItem>::iterator it = _items.begin(); it != _items.end(); ++it) {
61 if (it->name == QString::fromUtf8(name)) {
62 it->pos = pos;
63 break;
64 }
65 }
66 }
67
68 void DockWindowItems::setVisibility(const char* name, bool v)
69 {
70 for (QList<DockWindowItem>::iterator it = _items.begin(); it != _items.end(); ++it) {
71 if (it->name == QString::fromUtf8(name)) {
72 it->visibility = v;
73 break;
74 }
75 }
76 }
77
78 void DockWindowItems::setVisibility(bool v)
79 {
80 for (QList<DockWindowItem>::iterator it = _items.begin(); it != _items.end(); ++it) {
81 it->visibility = v;
82 }
83 }
84
85 const QList<DockWindowItem>& DockWindowItems::dockWidgets() const
86 {
87 return this->_items;
88 }
89
90 // -----------------------------------------------------------
91
92 namespace Gui
93 {
94
95 class DockWidgetEventFilter: public QObject
96 {
97 public:
98 bool eventFilter(QObject* o, QEvent* e)
99 {
100 if (!o->isWidgetType() || e->type() != QEvent::MouseMove) {
101 return false;
102 }
103 auto widget = qobject_cast<QDockWidget*>(o);
104 if (!widget || !widget->isFloating()) {
105 if (overridden) {
106 overridden = false;
107 QApplication::restoreOverrideCursor();
108 }
109 return false;
110 }
111 if (static_cast<QMouseEvent*>(e)->buttons() != Qt::NoButton) {
112 return false;
113 }
114 auto pos = QCursor::pos();
115 QPoint topLeft = widget->mapToGlobal(QPoint(cursorMargin, cursorMargin));
116 int h = widget->frameGeometry().height();
117 int w = widget->frameGeometry().width();
118 QPoint bottomRight = widget->mapToGlobal(QPoint(w - cursorMargin, h - cursorMargin));
119 bool left = QRect(topLeft - QPoint(cursorMargin, cursorMargin), QSize(cursorMargin, h))
120 .contains(pos);
121 bool right = QRect(bottomRight.x(), topLeft.y(), cursorMargin, h).contains(pos);
122 bool bottom = QRect(topLeft.x() - cursorMargin, bottomRight.y(), w, cursorMargin).contains(pos);
123 auto cursor = Qt::ArrowCursor;
124 if (left && bottom) {
125 cursor = Qt::SizeBDiagCursor;
126 }
127 else if (right && bottom) {
128 cursor = Qt::SizeFDiagCursor;
129 }
130 else if (bottom) {
131 cursor = Qt::SizeVerCursor;
132 }
133 else if (left || right) {
134 cursor = Qt::SizeHorCursor;
135 }
136 else if (overridden) {
137 overridden = false;
138 QApplication::restoreOverrideCursor();
139 return false;
140 }
141 if (overridden) {
142 QApplication::changeOverrideCursor(cursor);
143 }
144 else {
145 overridden = true;
146 QApplication::setOverrideCursor(cursor);
147 }
148 return false;
149 }
150
151 bool overridden = false;
152 int cursorMargin = 5;
153 };
154
155 struct DockWindowManagerP
156 {
157 QList<QDockWidget*> _dockedWindows;
158 QMap<QString, QPointer<QWidget>> _dockWindows;
159 DockWindowItems _dockWindowItems;
160 ParameterGrp::handle _hPref;
161 boost::signals2::scoped_connection _connParam;
162 QTimer _timer;
163 DockWidgetEventFilter _dockWidgetEventFilter;
164 QPointer<OverlayManager> overlayManager;
165 };
166 } // namespace Gui
167
168 DockWindowManager* DockWindowManager::_instance = nullptr;
169
170 DockWindowManager* DockWindowManager::instance()
171 {
172 if (!_instance) {
173 _instance = new DockWindowManager;
174 }
175 return _instance;
176 }
177
178 void DockWindowManager::destruct()
179 {
180 delete _instance;
181 _instance = nullptr;
182 }
183
184 DockWindowManager::DockWindowManager()
185 {
186 d = new DockWindowManagerP;
187 d->_hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp/MainWindow/DockWindows");
188
189 auto grp = App::GetApplication().GetUserParameter().GetGroup("BaseApp/Preferences/DockWindows");
190 if (grp->GetBool("ActivateOverlay", true)) {
191 setupOverlayManagement();
192 }
193 }
194
195 DockWindowManager::~DockWindowManager()
196 {
197 d->_dockedWindows.clear();
198 delete d;
199 }
200
201 bool DockWindowManager::isOverlayActivated() const
202 {
203 return (d->overlayManager != nullptr);
204 }
205
206 void DockWindowManager::setupOverlayManagement()
207 {
208 d->overlayManager = OverlayManager::instance();
209
210 qApp->installEventFilter(&d->_dockWidgetEventFilter);
211
212 d->_dockWidgetEventFilter.cursorMargin = d->_hPref->GetInt("CursorMargin", 5);
213 d->_connParam = d->_hPref->Manager()->signalParamChanged.connect(
214 [this](ParameterGrp* Param, ParameterGrp::ParamType Type, const char* name, const char*) {
215 if (Param == d->_hPref) {
216 switch (Type) {
217 case ParameterGrp::ParamType::FCBool:
218 // For batch process UI setting changes, e.g. loading new preferences
219 d->_timer.start(100);
220 break;
221 case ParameterGrp::ParamType::FCInt:
222 if (name && boost::equals(name, "CursorMargin")) {
223 d->_dockWidgetEventFilter.cursorMargin
224 = d->_hPref->GetInt("CursorMargin", 5);
225 }
226 break;
227 default:
228 break;
229 }
230 }
231 }
232 );
233
234 d->_timer.setSingleShot(true);
235
236 connect(&d->_timer, &QTimer::timeout, [this]() {
237 for (auto w : this->getDockWindows()) {
238 if (auto dw = qobject_cast<QDockWidget*>(w)) {
239 QSignalBlocker blocker(dw);
240 QByteArray dockName = dw->toggleViewAction()->data().toByteArray();
241 dw->setVisible(d->_hPref->GetBool(dockName, dw->isVisible()));
242 }
243 }
244 });
245 }
246
247 /**
248 * Adds a new dock window to the main window and embeds the given \a widget.
249 */
250 QDockWidget* DockWindowManager::addDockWindow(const char* name, QWidget* widget, Qt::DockWidgetArea pos)
251 {
252 if (!widget) {
253 return nullptr;
254 }
255 QDockWidget* dw = qobject_cast<QDockWidget*>(widget->parentWidget());
256 if (dw) {
257 return dw;
258 }
259
260 // creates the dock widget as container to embed this widget
261 MainWindow* mw = getMainWindow();
262 dw = new QDockWidget(mw);
263
264 if (d->overlayManager) {
265 d->overlayManager->setupTitleBar(dw);
266 }
267
268 // Note: By default all dock widgets are hidden but the user can show them manually in the view
269 // menu. First, hide immediately the dock widget to avoid flickering, after setting up the dock
270 // widgets MainWindow::loadLayoutSettings() is called to restore the layout.
271 dw->hide();
272 switch (pos) {
273 case Qt::LeftDockWidgetArea:
274 case Qt::RightDockWidgetArea:
275 case Qt::TopDockWidgetArea:
276 case Qt::BottomDockWidgetArea:
277 mw->addDockWidget(pos, dw);
278 default:
279 break;
280 }
281 connect(dw, &QObject::destroyed, this, &DockWindowManager::onDockWidgetDestroyed);
282 connect(widget, &QObject::destroyed, this, &DockWindowManager::onWidgetDestroyed);
283
284 // add the widget to the dock widget
285 widget->setParent(dw);
286 dw->setWidget(widget);
287
288 // set object name and window title needed for i18n stuff
289 dw->setObjectName(QString::fromUtf8(name));
290 dw->setWindowTitle(widget->windowTitle());
291 dw->setFeatures(
292 QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable
293 | QDockWidget::DockWidgetFloatable
294 );
295
296 d->_dockedWindows.push_back(dw);
297
298 if (d->overlayManager) {
299 d->overlayManager->initDockWidget(dw);
300 }
301
302 connect(dw->toggleViewAction(), &QAction::triggered, [this, dw]() {
303 Base::ConnectionBlocker block(d->_connParam);
304 QByteArray dockName = dw->toggleViewAction()->data().toByteArray();
305 d->_hPref->SetBool(dockName.constData(), dw->isVisible());
306 });
307
308 auto cb = []() {
309 getMainWindow()->saveWindowSettings(true);
310 };
311 connect(dw, &QDockWidget::topLevelChanged, cb);
312 connect(dw, &QDockWidget::dockLocationChanged, cb);
313 return dw;
314 }
315
316 /**
317 * Returns the widget inside the dock window by name.
318 * If it does not exist 0 is returned.
319 */
320 QWidget* DockWindowManager::getDockWindow(const char* name) const
321 {
322 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
323 ++it) {
324 if ((*it)->objectName() == QString::fromUtf8(name)) {
325 return (*it)->widget();
326 }
327 }
328
329 return nullptr;
330 }
331
332 /**
333 * Returns the dock widget by name.
334 * If it does not exist 0 is returned.
335 */
336 QDockWidget* DockWindowManager::getDockContainer(const char* name) const
337 {
338 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
339 ++it) {
340 if ((*it)->objectName() == QLatin1String(name)) {
341 return (*it);
342 }
343 }
344
345 return nullptr;
346 }
347
348 /**
349 * Returns a list of all widgets inside the dock windows.
350 */
351 QList<QWidget*> DockWindowManager::getDockWindows() const
352 {
353 QList<QWidget*> docked;
354 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
355 ++it) {
356 docked.push_back((*it)->widget());
357 }
358 return docked;
359 }
360
361 /**
362 * Removes the specified dock window with name \name without deleting it.
363 */
364 QWidget* DockWindowManager::removeDockWindow(const char* name)
365 {
366 QWidget* widget = nullptr;
367 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
368 ++it) {
369 if ((*it)->objectName() == QString::fromUtf8(name)) {
370 QDockWidget* dw = *it;
371 d->_dockedWindows.erase(it);
372
373 if (d->overlayManager) {
374 d->overlayManager->unsetupDockWidget(dw);
375 }
376
377 getMainWindow()->removeDockWidget(dw);
378 // avoid to destruct the embedded widget
379 widget = dw->widget();
380 widget->setParent(nullptr);
381 dw->setWidget(nullptr);
382 disconnect(dw, &QObject::destroyed, this, &DockWindowManager::onDockWidgetDestroyed);
383 disconnect(widget, &QObject::destroyed, this, &DockWindowManager::onWidgetDestroyed);
384 delete dw; // destruct the QDockWidget, i.e. the parent of the widget
385 break;
386 }
387 }
388
389 return widget;
390 }
391
392 /**
393 * Method provided for convenience. Does basically the same as the method above unless that
394 * it accepts a pointer.
395 */
396 void DockWindowManager::removeDockWindow(QWidget* widget)
397 {
398 if (!widget) {
399 return;
400 }
401 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
402 ++it) {
403 if ((*it)->widget() == widget) {
404 QDockWidget* dw = *it;
405 d->_dockedWindows.erase(it);
406 if (d->overlayManager) {
407 d->overlayManager->unsetupDockWidget(dw);
408 }
409 getMainWindow()->removeDockWidget(dw);
410 // avoid to destruct the embedded widget
411 widget->setParent(nullptr);
412 dw->setWidget(nullptr);
413 disconnect(dw, &QObject::destroyed, this, &DockWindowManager::onDockWidgetDestroyed);
414 disconnect(widget, &QObject::destroyed, this, &DockWindowManager::onWidgetDestroyed);
415 delete dw; // destruct the QDockWidget, i.e. the parent of the widget
416 break;
417 }
418 }
419 }
420
421 /**
422 * If the corresponding dock widget isn't visible then activate it.
423 */
424 void DockWindowManager::activate(QWidget* widget)
425 {
426 QDockWidget* dw = nullptr;
427 QWidget* par = widget->parentWidget();
428 while (par) {
429 dw = qobject_cast<QDockWidget*>(par);
430 if (dw) {
431 break;
432 }
433 par = par->parentWidget();
434 }
435
436 if (!dw) {
437 return;
438 }
439
440 if (!dw->toggleViewAction()->isChecked()) {
441 dw->toggleViewAction()->activate(QAction::Trigger);
442 }
443
444 dw->raise();
445 }
446
447 /**
448 * Sets the window title for the dockable windows.
449 */
450 void DockWindowManager::retranslate()
451 {
452 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
453 ++it) {
454 QString title = (*it)->windowTitle();
455 if (title.isEmpty()) {
456 (*it)->setWindowTitle(QDockWidget::tr((*it)->objectName().toUtf8()));
457 }
458 else {
459 (*it)->setWindowTitle(title);
460 }
461 }
462 }
463
464 /**
465 * Appends a new \a widget with \a name to the list of available dock widgets. The caller must make
466 * sure that the name is unique. If a widget with this name is already registered nothing is done
467 * but false is returned, otherwise it is appended and true is returned.
468 *
469 * As default the following widgets are already registered:
470 * \li Std_TreeView
471 * \li Std_PropertyView
472 * \li Std_ReportView
473 * \li Std_ToolBox
474 * \li Std_ComboView
475 * \li Std_SelectionView
476 *
477 * To avoid name clashes the caller should use names of the form \a module_widgettype, i. e. if a
478 * analyse dialog for the mesh module is added the name must then be Mesh_AnalyzeDialog.
479 *
480 * To make use of dock windows when a workbench gets loaded the method setupDockWindows() must
481 * reimplemented in a subclass of Gui::Workbench.
482 */
483 bool DockWindowManager::registerDockWindow(const char* name, QWidget* widget)
484 {
485 QMap<QString, QPointer<QWidget>>::Iterator it = d->_dockWindows.find(QString::fromUtf8(name));
486 if (it != d->_dockWindows.end() || !widget) {
487 return false;
488 }
489 d->_dockWindows[QString::fromUtf8(name)] = widget;
490 widget->hide(); // hide the widget if not used
491 return true;
492 }
493
494 QWidget* DockWindowManager::unregisterDockWindow(const char* name)
495 {
496 QWidget* widget = nullptr;
497 QMap<QString, QPointer<QWidget>>::Iterator it = d->_dockWindows.find(QString::fromUtf8(name));
498 if (it != d->_dockWindows.end()) {
499 widget = d->_dockWindows.take(QString::fromUtf8(name));
500 }
501
502 return widget;
503 }
504
505 QWidget* DockWindowManager::findRegisteredDockWindow(const char* name)
506 {
507 QMap<QString, QPointer<QWidget>>::Iterator it = d->_dockWindows.find(QString::fromUtf8(name));
508 if (it != d->_dockWindows.end()) {
509 return it.value();
510 }
511 return nullptr;
512 }
513
514 /** Sets up the dock windows of the activated workbench. */
515 void DockWindowManager::setup(DockWindowItems* items)
516 {
517 // save state of current dock windows
518 saveState();
519 d->_dockWindowItems = *items;
520
521 QList<QDockWidget*> docked = d->_dockedWindows;
522 const QList<DockWindowItem>& dws = items->dockWidgets();
523 for (const auto& it : dws) {
524 QDockWidget* dw = findDockWidget(docked, it.name);
525 QByteArray dockName = it.name.toLatin1();
526 bool visible = d->_hPref->GetBool(dockName.constData(), it.visibility);
527
528 if (!dw) {
529 QMap<QString, QPointer<QWidget>>::Iterator jt = d->_dockWindows.find(it.name);
530 if (jt != d->_dockWindows.end()) {
531 dw = addDockWindow(jt.value()->objectName().toUtf8(), jt.value(), it.pos);
532 jt.value()->show();
533 dw->toggleViewAction()->setData(it.name);
534 dw->setVisible(visible);
535 }
536 }
537 else {
538 dw->setVisible(visible);
539 dw->toggleViewAction()->setVisible(true);
540 int index = docked.indexOf(dw);
541 docked.removeAt(index);
542 }
543
544 if (d->overlayManager && dw && visible && !dw->titleBarWidget()) {
545 d->overlayManager->setupDockWidget(dw);
546 }
547 }
548
549 tabifyDockWidgets(items);
550 }
551
552 void DockWindowManager::tabifyDockWidgets(DockWindowItems* items)
553 {
554 // Tabify dock widgets only once to avoid to override the current layout
555 // in case it was modified by the user. The user shouldn't be forced to
556 // restore a possibly changed layout after switching to another workbench.
557 static bool tabify = false;
558 if (tabify) {
559 return;
560 }
561
562 std::array<QList<QDockWidget*>, 4> areas;
563 const QList<DockWindowItem>& dws = items->dockWidgets();
564 QList<QDockWidget*> docked = d->_dockedWindows;
565 for (const auto& it : dws) {
566 QDockWidget* dw = findDockWidget(docked, it.name);
567 if (it.tabbed && dw) {
568 Qt::DockWidgetArea pos = getMainWindow()->dockWidgetArea(dw);
569 switch (pos) {
570 case Qt::LeftDockWidgetArea:
571 areas[0] << dw;
572 break;
573 case Qt::RightDockWidgetArea:
574 areas[1] << dw;
575 break;
576 case Qt::TopDockWidgetArea:
577 areas[2] << dw;
578 break;
579 case Qt::BottomDockWidgetArea:
580 areas[3] << dw;
581 break;
582 default:
583 break;
584 }
585 }
586 }
587
588 // tabify dock widgets for which "tabbed" is true and which have the same position
589 for (auto& area : areas) {
590 for (auto it : area) {
591 if (it != area.front()) {
592 getMainWindow()->tabifyDockWidget(area.front(), it);
593 tabify = true;
594 }
595 }
596
597 // activate the first of the tabbed dock widgets
598 if (area.size() > 1) {
599 area.front()->raise();
600 }
601 }
602 }
603
604 void DockWindowManager::saveState()
605 {
606 const QList<DockWindowItem>& dockItems = d->_dockWindowItems.dockWidgets();
607 for (QList<DockWindowItem>::ConstIterator it = dockItems.begin(); it != dockItems.end(); ++it) {
608 QDockWidget* dw = findDockWidget(d->_dockedWindows, it->name);
609 if (dw) {
610 QByteArray dockName = dw->toggleViewAction()->data().toByteArray();
611 d->_hPref->SetBool(dockName.constData(), dw->isVisible());
612 }
613 }
614 }
615
616 void DockWindowManager::loadState()
617 {
618 ParameterGrp::handle hPref = App::GetApplication()
619 .GetUserParameter()
620 .GetGroup("BaseApp")
621 ->GetGroup("MainWindow")
622 ->GetGroup("DockWindows");
623 const QList<DockWindowItem>& dockItems = d->_dockWindowItems.dockWidgets();
624 for (QList<DockWindowItem>::ConstIterator it = dockItems.begin(); it != dockItems.end(); ++it) {
625 QDockWidget* dw = findDockWidget(d->_dockedWindows, it->name);
626 if (dw) {
627 QByteArray dockName = it->name.toUtf8();
628 bool visible = hPref->GetBool(dockName.constData(), it->visibility);
629 dw->setVisible(visible);
630 }
631 }
632 }
633
634 QDockWidget* DockWindowManager::findDockWidget(const QList<QDockWidget*>& dw, const QString& name) const
635 {
636 for (QList<QDockWidget*>::ConstIterator it = dw.begin(); it != dw.end(); ++it) {
637 if ((*it)->toggleViewAction()->data().toString() == name) {
638 return *it;
639 }
640 }
641
642 return nullptr;
643 }
644
645 void DockWindowManager::onDockWidgetDestroyed(QObject* dw)
646 {
647 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
648 ++it) {
649 if (*it == dw) {
650 d->_dockedWindows.erase(it);
651 break;
652 }
653 }
654 }
655
656 void DockWindowManager::onWidgetDestroyed(QObject* widget)
657 {
658 for (QList<QDockWidget*>::Iterator it = d->_dockedWindows.begin(); it != d->_dockedWindows.end();
659 ++it) {
660 // make sure that the dock widget is not about to being deleted
661 if ((*it)->metaObject() != &QDockWidget::staticMetaObject) {
662 disconnect(*it, &QObject::destroyed, this, &DockWindowManager::onDockWidgetDestroyed);
663 d->_dockedWindows.erase(it);
664 break;
665 }
666
667 if ((*it)->widget() == widget) {
668 // Delete the widget if not used anymore
669 QDockWidget* dw = *it;
670 dw->deleteLater();
671 break;
672 }
673 }
674 }
675
676 #include "moc_DockWindowManager.cpp"