Debian Codegraph @

src/Gui/Control.cpp

raw
1 /***************************************************************************
2 * Copyright 2011 (c) Jürgen Riegel <[email protected]> *
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 <QAction>
24 #include <QApplication>
25 #include <QDebug>
26 #include <QDockWidget>
27 #include <QPointer>
28
29 #include <App/AutoTransaction.h>
30 #include <Gui/ComboView.h>
31 #include <Gui/DockWindowManager.h>
32 #include <Gui/MainWindow.h>
33
34 #include "Control.h"
35 #include "BitmapFactory.h"
36 #include "Tree.h"
37 #include "TaskView/TaskView.h"
38
39
40 using namespace Gui;
41 using namespace std;
42
43 /* TRANSLATOR Gui::ControlSingleton */
44
45 ControlSingleton* ControlSingleton::_pcSingleton = nullptr;
46
47 ControlSingleton::ControlSingleton()
48 : ActiveDialog(nullptr)
49 , oldTabIndex(-1)
50 {}
51
52 ControlSingleton::~ControlSingleton() = default;
53
54 Gui::TaskView::TaskView* ControlSingleton::taskPanel() const
55 {
56 auto taskView = qobject_cast<Gui::TaskView::TaskView*>(
57 Gui::DockWindowManager::instance()->getDockWindow("Tasks")
58 );
59 return taskView;
60 }
61
62 void ControlSingleton::showDockWidget(QWidget* widget)
63 {
64 QWidget* parent = widget->parentWidget();
65 if (parent) {
66 parent->show();
67 parent->raise();
68 }
69 }
70
71 QTabBar* ControlSingleton::findTabBar(QDockWidget* widget) const
72 {
73 int count = getMainWindow()->tabifiedDockWidgets(widget).size() + 1;
74 if (count > 1) {
75 QList<QTabBar*> bars = getMainWindow()->findChildren<QTabBar*>();
76 for (auto it : bars) {
77 if (it->count() <= count) {
78 for (int i = 0; i < count; i++) {
79 if (it->tabText(i) == widget->windowTitle()) {
80 return it;
81 }
82 }
83 }
84 }
85 }
86
87 return nullptr;
88 }
89
90 void ControlSingleton::aboutToShowDialog(QDockWidget* widget)
91 {
92 static QIcon icon = Gui::BitmapFactory().pixmap("edit-edit.svg");
93 QTabBar* bar = findTabBar(widget);
94 if (bar) {
95 oldTabIndex = bar->currentIndex();
96 for (int i = 0; i < bar->count(); i++) {
97 if (bar->tabText(i) == widget->windowTitle()) {
98 bar->setTabIcon(i, icon);
99 break;
100 }
101 }
102 }
103
104 widget->show();
105 widget->raise();
106 }
107
108 void ControlSingleton::aboutToHideDialog(QDockWidget* widget)
109 {
110 QTabBar* bar = findTabBar(widget);
111 if (bar) {
112 bar->setCurrentIndex(oldTabIndex);
113 for (int i = 0; i < bar->count(); i++) {
114 if (bar->tabText(i) == widget->windowTitle()) {
115 bar->setTabIcon(i, QIcon());
116 break;
117 }
118 }
119 }
120 }
121
122 void ControlSingleton::showTaskView()
123 {
124 Gui::TaskView::TaskView* taskView = taskPanel();
125 if (taskView) {
126 showDockWidget(taskView);
127 }
128 }
129
130 void ControlSingleton::showModelView()
131 {
132 auto treeView = qobject_cast<Gui::TreeDockWidget*>(
133 Gui::DockWindowManager::instance()->getDockWindow("Tree view")
134 );
135 if (treeView) {
136 showDockWidget(treeView);
137 }
138 else {
139 auto comboView = qobject_cast<Gui::DockWnd::ComboView*>(
140 Gui::DockWindowManager::instance()->getDockWindow("Model")
141 );
142 if (comboView) {
143 showDockWidget(comboView);
144 }
145 }
146 }
147
148 void ControlSingleton::showDialog(Gui::TaskView::TaskDialog* dlg)
149 {
150 // only one dialog at a time, print a warning instead of raising an assert
151 if (ActiveDialog && ActiveDialog != dlg) {
152 if (dlg) {
153 qWarning() << "ControlSingleton::showDialog: Can't show "
154 << dlg->metaObject()->className()
155 << " since there is already an active task dialog";
156 }
157 else {
158 qWarning() << "ControlSingleton::showDialog: Task dialog is null";
159 }
160 return;
161 }
162
163 // Since the caller sets up a modeless task panel, it indicates intention
164 // for prolonged editing. So disable auto transaction in the current call
165 // stack.
166 // Do this before showing the dialog because its open() function is called
167 // which may open a transaction but fails when auto transaction is still active.
168 App::AutoTransaction::setEnable(false);
169
170 Gui::TaskView::TaskView* taskView = taskPanel();
171 // should return the pointer to combo view
172 if (taskView) {
173 taskView->showDialog(dlg);
174
175 // make sure that the combo view is shown
176 auto dw = qobject_cast<QDockWidget*>(taskView->parentWidget());
177 if (dw) {
178 aboutToShowDialog(dw);
179 dw->setVisible(true);
180 dw->toggleViewAction()->setVisible(true);
181 dw->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
182 }
183
184 if (ActiveDialog == dlg) {
185 return; // dialog is already defined
186 }
187 ActiveDialog = dlg;
188 connect(dlg, &TaskView::TaskDialog::aboutToBeDestroyed, this, &ControlSingleton::closedDialog);
189 }
190 }
191
192 Gui::TaskView::TaskDialog* ControlSingleton::activeDialog() const
193 {
194 return ActiveDialog;
195 }
196
197 void ControlSingleton::accept()
198 {
199 Gui::TaskView::TaskView* taskView = taskPanel();
200 if (taskView) {
201 taskView->accept();
202 qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
203 }
204 }
205
206 void ControlSingleton::reject()
207 {
208 Gui::TaskView::TaskView* taskView = taskPanel();
209 if (taskView) {
210 taskView->reject();
211 qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
212 }
213 }
214
215 void ControlSingleton::closeDialog()
216 {
217 Gui::TaskView::TaskView* taskView = taskPanel();
218 if (taskView) {
219 taskView->removeDialog();
220 }
221 }
222
223 void ControlSingleton::closedDialog()
224 {
225 ActiveDialog = nullptr;
226 Gui::TaskView::TaskView* taskView = taskPanel();
227 assert(taskView);
228
229 // make sure that the combo view is shown
230 auto dw = qobject_cast<QDockWidget*>(taskView->parentWidget());
231 if (dw) {
232 aboutToHideDialog(dw);
233 dw->setFeatures(
234 QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable
235 | QDockWidget::DockWidgetFloatable
236 );
237 }
238 }
239
240 bool ControlSingleton::isAllowedAlterDocument() const
241 {
242 if (ActiveDialog) {
243 return ActiveDialog->isAllowedAlterDocument();
244 }
245 return true;
246 }
247
248
249 bool ControlSingleton::isAllowedAlterView() const
250 {
251 if (ActiveDialog) {
252 return ActiveDialog->isAllowedAlterView();
253 }
254 return true;
255 }
256
257 bool ControlSingleton::isAllowedAlterSelection() const
258 {
259 if (ActiveDialog) {
260 return ActiveDialog->isAllowedAlterSelection();
261 }
262 return true;
263 }
264
265 // -------------------------------------------
266
267 ControlSingleton& ControlSingleton::instance()
268 {
269 if (!_pcSingleton) {
270 _pcSingleton = new ControlSingleton;
271 }
272 return *_pcSingleton;
273 }
274
275 void ControlSingleton::destruct()
276 {
277 if (_pcSingleton) {
278 delete _pcSingleton;
279 }
280 _pcSingleton = nullptr;
281 }
282
283
284 // -------------------------------------------
285
286
287 #include "moc_Control.cpp"