Debian Codegraph @

src/Gui/CommandWindow.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2005 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 <QCoreApplication>
24 #include <QStatusBar>
25 #include <QEvent>
26
27 #include "Command.h"
28 #include "Action.h"
29 #include "Application.h"
30 #include "MainWindow.h"
31 #include "View.h"
32 #include "Document.h"
33 #include "Dialogs/DlgActivateWindowImp.h"
34 #include "DockWindowManager.h"
35 #include "ToolBarManager.h"
36
37 #include <Base/Exception.h>
38 #include <App/Document.h>
39
40 using namespace Gui;
41
42
43 //===========================================================================
44 // Std_TileWindows
45 //===========================================================================
46 DEF_STD_CMD_A(StdCmdTileWindows)
47
48 StdCmdTileWindows::StdCmdTileWindows()
49 : Command("Std_TileWindows")
50 {
51 sGroup = "Window";
52 sMenuText = QT_TR_NOOP("&Tile");
53 sToolTipText = QT_TR_NOOP("Tiles the windows");
54 sWhatsThis = "Std_TileWindows";
55 sStatusTip = sToolTipText;
56 sPixmap = "Std_WindowTileVer";
57 eType = 0;
58 }
59
60 void StdCmdTileWindows::activated(int iMsg)
61 {
62 Q_UNUSED(iMsg);
63 getMainWindow()->tile();
64 }
65
66 bool StdCmdTileWindows::isActive()
67 {
68 return !(getMainWindow()->windows().isEmpty());
69 }
70
71 //===========================================================================
72 // Std_CascadeWindows
73 //===========================================================================
74 DEF_STD_CMD_A(StdCmdCascadeWindows)
75
76 StdCmdCascadeWindows::StdCmdCascadeWindows()
77 : Command("Std_CascadeWindows")
78 {
79 sGroup = "Window";
80 sMenuText = QT_TR_NOOP("&Cascade");
81 sToolTipText = QT_TR_NOOP("Tiles pragmatic");
82 sWhatsThis = "Std_CascadeWindows";
83 sStatusTip = sToolTipText;
84 sPixmap = "Std_WindowCascade";
85 eType = 0;
86 }
87
88 void StdCmdCascadeWindows::activated(int iMsg)
89 {
90 Q_UNUSED(iMsg);
91 getMainWindow()->cascade();
92 }
93
94 bool StdCmdCascadeWindows::isActive()
95 {
96 return !(getMainWindow()->windows().isEmpty());
97 }
98
99 //===========================================================================
100 // Std_CloseActiveWindow
101 //===========================================================================
102 DEF_STD_CMD_A(StdCmdCloseActiveWindow)
103
104 StdCmdCloseActiveWindow::StdCmdCloseActiveWindow()
105 : Command("Std_CloseActiveWindow")
106 {
107 sGroup = "Window";
108 sMenuText = QT_TR_NOOP("&Close");
109 sToolTipText = QT_TR_NOOP("Closes the active window");
110 sWhatsThis = "Std_CloseActiveWindow";
111 sStatusTip = sToolTipText;
112 // In QMdiSubWindow the 'QKeySequence::Close' shortcut is set which will
113 // collide with this shortcut. Thus the shortcut of QMdiSubWindow will be
114 // reset in MainWindow::addWindow() (#0002631)
115 sAccel = keySequenceToAccel(QKeySequence::Close);
116 sPixmap = "Std_CloseActiveWindow";
117 eType = NoTransaction;
118 }
119
120 void StdCmdCloseActiveWindow::activated(int iMsg)
121 {
122 Q_UNUSED(iMsg);
123 getMainWindow()->closeActiveWindow();
124 }
125
126 bool StdCmdCloseActiveWindow::isActive()
127 {
128 return !(getMainWindow()->windows().isEmpty());
129 }
130
131 //===========================================================================
132 // Std_CloseAllWindows
133 //===========================================================================
134 DEF_STD_CMD_A(StdCmdCloseAllWindows)
135
136 StdCmdCloseAllWindows::StdCmdCloseAllWindows()
137 : Command("Std_CloseAllWindows")
138 {
139 sGroup = "Window";
140 sMenuText = QT_TR_NOOP("Close A&ll");
141 sToolTipText = QT_TR_NOOP("Closes all windows");
142 sWhatsThis = "Std_CloseAllWindows";
143 sStatusTip = sToolTipText;
144 sPixmap = "Std_CloseAllWindows";
145 eType = NoTransaction;
146 }
147
148 void StdCmdCloseAllWindows::activated(int iMsg)
149 {
150 Q_UNUSED(iMsg);
151 getMainWindow()->closeAllDocuments();
152 }
153
154 bool StdCmdCloseAllWindows::isActive()
155 {
156 return !(getMainWindow()->windows().isEmpty()) || !App::GetApplication().getDocuments().empty();
157 }
158
159 //===========================================================================
160 // Std_ActivateNextWindow
161 //===========================================================================
162 DEF_STD_CMD_A(StdCmdActivateNextWindow)
163
164 StdCmdActivateNextWindow::StdCmdActivateNextWindow()
165 : Command("Std_ActivateNextWindow")
166 {
167 sGroup = "Window";
168 sMenuText = QT_TR_NOOP("&Next");
169 sToolTipText = QT_TR_NOOP("Activates the next window");
170 sWhatsThis = "Std_ActivateNextWindow";
171 sStatusTip = sToolTipText;
172 sPixmap = "Std_WindowNext";
173 sAccel = keySequenceToAccel(QKeySequence::NextChild);
174 eType = 0;
175 }
176
177 void StdCmdActivateNextWindow::activated(int iMsg)
178 {
179 Q_UNUSED(iMsg);
180 getMainWindow()->activateNextWindow();
181 }
182
183 bool StdCmdActivateNextWindow::isActive()
184 {
185 return !(getMainWindow()->windows().isEmpty());
186 }
187
188 //===========================================================================
189 // Std_ActivatePrevWindow
190 //===========================================================================
191 DEF_STD_CMD_A(StdCmdActivatePrevWindow)
192
193 StdCmdActivatePrevWindow::StdCmdActivatePrevWindow()
194 : Command("Std_ActivatePrevWindow")
195 {
196 sGroup = "Window";
197 sMenuText = QT_TR_NOOP("&Previous");
198 sToolTipText = QT_TR_NOOP("Switches to the previously active window");
199 sWhatsThis = "Std_ActivatePrevWindow";
200 sStatusTip = sToolTipText;
201 sPixmap = "Std_WindowPrev";
202 // Depending on the OS 'QKeySequence::PreviousChild' gives
203 // Ctrl+Shift+Backtab instead of Ctrl+Shift+Tab which leads
204 // to a strange behaviour when using it.
205 // A workaround is to create a shortcut as Shift + QKeySequence::NextChild
206 static std::string previousChild = std::string("Shift+")
207 + keySequenceToAccel(QKeySequence::NextChild);
208 sAccel = previousChild.c_str();
209 eType = 0;
210 }
211
212 void StdCmdActivatePrevWindow::activated(int iMsg)
213 {
214 Q_UNUSED(iMsg);
215 getMainWindow()->activatePreviousWindow();
216 }
217
218 bool StdCmdActivatePrevWindow::isActive()
219 {
220 return !(getMainWindow()->windows().isEmpty());
221 }
222
223 //===========================================================================
224 // Std_Windows
225 //===========================================================================
226 DEF_STD_CMD(StdCmdWindows)
227
228 StdCmdWindows::StdCmdWindows()
229 : Command("Std_Windows")
230 {
231 sGroup = "Window";
232 sMenuText = QT_TR_NOOP("Choose Open &Window");
233
234 sToolTipText = QT_TR_NOOP("Displays the open windows");
235 sWhatsThis = "Std_Windows";
236 sStatusTip = sToolTipText;
237 sPixmap = "Std_Windows";
238 eType = 0;
239 }
240
241 void StdCmdWindows::activated(int iMsg)
242 {
243 Q_UNUSED(iMsg);
244 Gui::Dialog::DlgActivateWindowImp dlg(getMainWindow());
245 dlg.exec();
246 }
247
248 //===========================================================================
249 // Std_UserInterface
250 //===========================================================================
251 DEF_STD_CMD(StdCmdUserInterface)
252
253 StdCmdUserInterface::StdCmdUserInterface()
254 : Command("Std_UserInterface")
255 {
256 sGroup = "View";
257 sMenuText = QT_TR_NOOP("Dock Views");
258 sToolTipText = QT_TR_NOOP("Docks all top-level views");
259 sWhatsThis = "Std_UserInterface";
260 sStatusTip = sToolTipText;
261 eType = 0;
262 }
263
264 void StdCmdUserInterface::activated(int)
265 {
266 getMainWindow()->switchToDockedMode();
267 }
268
269 //===========================================================================
270 // Std_DockWindowMenu
271 //===========================================================================
272
273 DEF_STD_CMD_AC(StdCmdDockViewMenu)
274
275 StdCmdDockViewMenu::StdCmdDockViewMenu()
276 : Command("Std_DockViewMenu")
277 {
278 sGroup = "View";
279 sMenuText = QT_TR_NOOP("&Panels");
280 sToolTipText = QT_TR_NOOP("Lists available dock panels");
281 sWhatsThis = "Std_DockViewMenu";
282 sStatusTip = sToolTipText;
283 eType = 0;
284 }
285
286 void StdCmdDockViewMenu::activated(int iMsg)
287 {
288 // Handled by the related QAction objects
289 Q_UNUSED(iMsg);
290 }
291
292 bool StdCmdDockViewMenu::isActive()
293 {
294 return true;
295 }
296
297 Action* StdCmdDockViewMenu::createAction()
298 {
299 Action* pcAction;
300 pcAction = new DockWidgetAction(this, getMainWindow());
301 applyCommandData(this->className(), pcAction);
302 return pcAction;
303 }
304
305 //===========================================================================
306 // Std_ToolBarMenu
307 //===========================================================================
308
309 DEF_STD_CMD_AC(StdCmdToolBarMenu)
310
311 StdCmdToolBarMenu::StdCmdToolBarMenu()
312 : Command("Std_ToolBarMenu")
313 {
314 sGroup = "View";
315 sMenuText = QT_TR_NOOP("&Toolbars");
316 sToolTipText = QT_TR_NOOP("Toggles this window");
317 sWhatsThis = "Std_ToolBarMenu";
318 sStatusTip = sToolTipText;
319 eType = 0;
320 }
321
322 void StdCmdToolBarMenu::activated(int iMsg)
323 {
324 // Handled by the related QAction objects
325 Q_UNUSED(iMsg);
326 }
327
328 bool StdCmdToolBarMenu::isActive()
329 {
330 return true;
331 }
332
333 Action* StdCmdToolBarMenu::createAction()
334 {
335 Action* pcAction;
336 pcAction = new ToolBarAction(this, getMainWindow());
337 applyCommandData(this->className(), pcAction);
338 return pcAction;
339 }
340
341 //===========================================================================
342 // Std_DlgToggleToolBarLock
343 //===========================================================================
344 DEF_STD_CMD_C(StdCmdToggleToolBarLock)
345
346 StdCmdToggleToolBarLock::StdCmdToggleToolBarLock()
347 : Command("Std_ToggleToolBarLock")
348 {
349 sGroup = "Tools";
350 sMenuText = QT_TR_NOOP("Lock Toolbars");
351 sToolTipText = QT_TR_NOOP("Locks toolbars so they are no longer moveable");
352 sWhatsThis = "Std_ToggleToolBarLock";
353 sStatusTip = sToolTipText;
354 eType = 0;
355 }
356
357
358 Action* StdCmdToggleToolBarLock::createAction()
359 {
360 Action* action = Command::createAction();
361
362 action->setCheckable(true);
363 action->setBlockedChecked(ToolBarManager::getInstance()->areToolBarsLocked());
364
365 return action;
366 }
367
368 void StdCmdToggleToolBarLock::activated(int iMsg)
369 {
370 Q_UNUSED(iMsg);
371
372 auto manager = ToolBarManager::getInstance();
373 auto toggled = !manager->areToolBarsLocked();
374
375 manager->setToolBarsLocked(toggled);
376
377 getAction()->setChecked(toggled);
378 }
379
380
381 //===========================================================================
382 // Std_ViewStatusBar
383 //===========================================================================
384
385 class FilterStatusBar: public QObject
386 {
387 // Q_OBJECT
388
389 public:
390 FilterStatusBar(Action* action)
391 : QObject(action)
392 {
393 this->action = action;
394 }
395 // virtual ~FilterStatusBar() {}
396 protected:
397 Action* action;
398 bool eventFilter(QObject* obj, QEvent* event) override
399 {
400 if (getMainWindow() && getMainWindow()->findChild<QStatusBar*>()
401 && obj == getMainWindow()->statusBar()
402 && ((event->type() == QEvent::Hide) || (event->type() == QEvent::Show))) {
403 this->action->setChecked(getMainWindow()->statusBar()->isVisible());
404 }
405 return false;
406 }
407 };
408
409 DEF_STD_CMD_AC(StdCmdStatusBar)
410
411 StdCmdStatusBar::StdCmdStatusBar()
412 : Command("Std_ViewStatusBar")
413 {
414 sGroup = "View";
415 sMenuText = QT_TR_NOOP("Status Bar");
416 sToolTipText = QT_TR_NOOP("Toggles the status bar");
417 sWhatsThis = "Std_ViewStatusBar";
418 sStatusTip = sToolTipText;
419 eType = 0;
420 }
421
422 Action* StdCmdStatusBar::createAction()
423 {
424 Action* pcAction = Command::createAction();
425 pcAction->setCheckable(true);
426 pcAction->setBlockedChecked(false);
427 auto fsb = new FilterStatusBar(pcAction);
428 getMainWindow()->statusBar()->installEventFilter(fsb);
429
430 return pcAction;
431 }
432
433 void StdCmdStatusBar::activated(int iMsg)
434 {
435 getMainWindow()->statusBar()->setVisible(iMsg != 0);
436 }
437
438 bool StdCmdStatusBar::isActive()
439 {
440 static bool checked = false;
441 if (!checked) {
442 Action* act = this->getAction();
443 if (act) {
444 act->setChecked(getMainWindow()->statusBar()->isVisible());
445 checked = true;
446 }
447 }
448 return true;
449 }
450
451 //===========================================================================
452 // Std_WindowsMenu
453 //===========================================================================
454
455 DEF_STD_CMD_AC(StdCmdWindowsMenu)
456
457 StdCmdWindowsMenu::StdCmdWindowsMenu()
458 : Command("Std_WindowsMenu")
459 {
460 sGroup = "Window";
461 sMenuText = QT_TR_NOOP("Activate Window"); // Replaced with the name of the window
462 sToolTipText = QT_TR_NOOP("Activates this window");
463 sWhatsThis = "Std_WindowsMenu";
464 sStatusTip = sToolTipText;
465 eType = 0;
466 }
467
468 void StdCmdWindowsMenu::activated(int iMsg)
469 {
470 // already handled by the main window
471 Q_UNUSED(iMsg);
472 }
473
474 bool StdCmdWindowsMenu::isActive()
475 {
476 return true;
477 }
478
479 Action* StdCmdWindowsMenu::createAction()
480 {
481 // Allow one to show 10 menu items in the 'Window' menu and one separator.
482 // If we have more windows then the user can use the 'Windows...' item.
483 WindowAction* pcAction;
484 pcAction = new WindowAction(this, getMainWindow());
485 for (int i = 0; i < 10; i++) {
486 QAction* window = pcAction->addAction(QObject::tr(getToolTipText()));
487 window->setCheckable(true);
488 window->setToolTip(QCoreApplication::translate(this->className(), getToolTipText()));
489 window->setStatusTip(QCoreApplication::translate(this->className(), getStatusTip()));
490 window->setWhatsThis(QCoreApplication::translate(this->className(), getWhatsThis()));
491 }
492
493 QAction* sep = pcAction->addAction(QLatin1String(""));
494 sep->setSeparator(true);
495
496 return pcAction;
497 }
498
499 //===========================================================================
500 // Instantiation
501 //===========================================================================
502
503
504 namespace Gui
505 {
506
507 void CreateWindowStdCommands()
508 {
509 CommandManager& rcCmdMgr = Application::Instance->commandManager();
510
511 rcCmdMgr.addCommand(new StdCmdTileWindows());
512 rcCmdMgr.addCommand(new StdCmdCascadeWindows());
513 rcCmdMgr.addCommand(new StdCmdCloseActiveWindow());
514 rcCmdMgr.addCommand(new StdCmdCloseAllWindows());
515 rcCmdMgr.addCommand(new StdCmdActivateNextWindow());
516 rcCmdMgr.addCommand(new StdCmdActivatePrevWindow());
517 rcCmdMgr.addCommand(new StdCmdWindows());
518 rcCmdMgr.addCommand(new StdCmdDockViewMenu());
519 rcCmdMgr.addCommand(new StdCmdToolBarMenu());
520 rcCmdMgr.addCommand(new StdCmdToggleToolBarLock());
521 rcCmdMgr.addCommand(new StdCmdWindowsMenu());
522 rcCmdMgr.addCommand(new StdCmdStatusBar());
523 rcCmdMgr.addCommand(new StdCmdUserInterface());
524 }
525
526 } // namespace Gui