Debian Codegraph @

src/Gui/MainWindow.h

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
24 #ifndef GUI_MAINWINDOW_H
25 #define GUI_MAINWINDOW_H
26
27 #include <QEvent>
28 #include <QMainWindow>
29 #include <QMdiArea>
30
31 #include "Window.h"
32 #include "InputHint.h"
33
34 class QMimeData;
35 class QUrl;
36 class QMdiSubWindow;
37
38 namespace App
39 {
40 class Document;
41 }
42
43 namespace Gui
44 {
45
46 class BaseView;
47 class CommandManager;
48 class Document;
49 class MacroManager;
50 class MDIView;
51
52 namespace DockWnd
53 {
54 class HelpView;
55 } // namespace DockWnd
56
57 class GuiExport UrlHandler: public QObject
58 {
59 Q_OBJECT
60
61 public:
62 explicit UrlHandler(QObject* parent = nullptr)
63 : QObject(parent)
64 {}
65 ~UrlHandler() override = default;
66 virtual void openUrl(App::Document*, const QUrl&)
67 {}
68 };
69
70 /**
71 * The MainWindow class provides a main window with menu bar, toolbars, dockable windows,
72 * a status bar and mainly a workspace for the MDI windows.
73 * @author Werner Mayer
74 */
75 class GuiExport MainWindow: public QMainWindow
76 {
77 Q_OBJECT
78
79 public:
80 enum ConfirmSaveResult
81 {
82 Cancel = 0,
83 Save,
84 SaveAll,
85 Discard,
86 DiscardAll
87 };
88 /**
89 * Constructs an empty main window. For default \a parent is 0, as there usually is
90 * no toplevel window there.
91 */
92 explicit MainWindow(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::Window);
93 /** Destroys the object and frees any allocated resources. */
94 ~MainWindow() override;
95 /**
96 * Filters events if this object has been installed as an event filter for the watched object.
97 */
98 bool eventFilter(QObject* o, QEvent* e) override;
99 /**
100 * Adds an MDI window \a view to the main window's workspace and adds a new tab
101 * to the tab bar.
102 */
103 void addWindow(MDIView* view);
104 /**
105 * Removes an MDI window from the main window's workspace and its associated tab without
106 * deleting the widget. If the main windows does not have such a window nothing happens.
107 */
108 void removeWindow(MDIView* view, bool close = true);
109 /**
110 * Returns a list of all MDI windows in the worpspace.
111 */
112 QList<QWidget*> windows(QMdiArea::WindowOrder order = QMdiArea::CreationOrder) const;
113 /**
114 * Returns the internal QMdiArea instance.
115 */
116 QMdiArea* getMdiArea() const;
117 /**
118 * Can be called after the caption of an MDIView has changed to update the tab's caption.
119 */
120 void tabChanged(MDIView* view);
121 /**
122 * Returns the active MDI window or 0 if there is none.
123 */
124 MDIView* activeWindow() const;
125 /**
126 * Sets the active window to \a view.
127 */
128 void setActiveWindow(MDIView* view);
129 /**
130 * MRU: Appends \a file to the list of recent files.
131 */
132 void appendRecentFile(const QString& filename);
133 /**
134 * MRU: Appends \a macro to the list of recent macros.
135 */
136 void appendRecentMacro(const QString& filename);
137 /**
138 * Returns true that the context menu contains the 'Customize...' menu item.
139 */
140 QMenu* createPopupMenu() override;
141
142 /** @name Splasher and access methods */
143 //@{
144 /** Gets the one and only instance. */
145 static MainWindow* getInstance();
146 /** Starts the splasher at startup. */
147 void startSplasher();
148 /** Stops the splasher after startup. */
149 void stopSplasher();
150 /** Shows the online documentation. */
151 void showDocumentation(const QString& help);
152 //@}
153
154 /** @name Layout Methods
155 */
156 //@{
157 /// Loads the main window settings.
158 void loadWindowSettings();
159 /// Saves the main window settings.
160 void saveWindowSettings(bool canDelay = false);
161 //@}
162
163 /** @name Menu
164 */
165 //@{
166 /// Set menu for dock windows.
167 void setDockWindowMenu(QMenu*);
168 /// Set menu for toolbars.
169 void setToolBarMenu(QMenu*);
170 /// Set menu for sub-windows
171 void setWindowsMenu(QMenu*);
172 //@}
173
174 /** @name MIME data handling
175 */
176 //@{
177 /** Create mime data from selected objects */
178 QMimeData* createMimeDataFromSelection() const;
179 /** Check if mime data contains object data */
180 bool canInsertFromMimeData(const QMimeData* source) const;
181 /** Insert the objects into the active document. If no document exists
182 * one gets created.
183 */
184 void insertFromMimeData(const QMimeData* source);
185 /**
186 * Load files from the given URLs into the given document. If the document is 0
187 * one gets created automatically if needed.
188 *
189 * If a url handler is registered that supports its scheme it will be delegated
190 * to this handler. This mechanism allows one to change the default behaviour.
191 */
192 void loadUrls(App::Document*, const QList<QUrl>&);
193 /**
194 * Sets the \a handler for the given \a scheme.
195 * If setUrlHandler() is used to set a new handler for a scheme which already has a handler,
196 * the existing handler is simply replaced with the new one. Since MainWindow does not take
197 * ownership of handlers, no objects are deleted when a handler is replaced.
198 */
199 void setUrlHandler(const QString& scheme, UrlHandler* handler);
200 /**
201 * Removes a previously set URL handler for the specified \a scheme.
202 */
203 void unsetUrlHandler(const QString& scheme);
204 //@}
205
206 void updateActions(bool delay = false);
207
208 enum StatusType
209 {
210 None,
211 Err,
212 Wrn,
213 Pane,
214 Msg,
215 Log,
216 Tmp,
217 Critical
218 };
219 void showStatus(int type, const QString& message);
220
221 void showHints(const std::list<InputHint>& hints = {});
222 void hideHints();
223
224 void initDockWindows(bool show);
225
226 bool isRestoringWindowState() const;
227
228 public Q_SLOTS:
229 /**
230 * Updates the standard actions of a text editor such as Cut, Copy, Paste, Undo and Redo.
231 */
232 void updateEditorActions();
233 /**
234 * Sets text to the pane in the status bar.
235 */
236 void setPaneText(int i, QString text);
237 /**
238 * Sets the userschema in the status bar
239 */
240 void setUserSchema(int userSchema);
241 /**
242 * Arranges all child windows in a tile pattern.
243 */
244 void tile();
245 /**
246 * Arranges all the child windows in a cascade pattern.
247 */
248 void cascade();
249 /**
250 * Closes the child window that is currently active.
251 */
252 void closeActiveWindow();
253 /**
254 * Closes all document window.
255 */
256 bool closeAllDocuments(bool close = true);
257 /** Pop up a message box asking for saving document
258 */
259 int confirmSave(App::Document* doc, QWidget* parent = nullptr, bool addCheckBox = false);
260 /**
261 * Activates the next window in the child window chain.
262 */
263 void activateNextWindow();
264 /**
265 * Activates the previous window in the child window chain.
266 */
267 void activatePreviousWindow();
268 /**
269 * Just emits the workbenchActivated() signal to notify all receivers.
270 */
271 void activateWorkbench(const QString&);
272 /**
273 * Starts the what's this mode.
274 */
275 void whatsThis();
276 void switchToTopLevelMode();
277 void switchToDockedMode();
278
279 void statusMessageChanged();
280
281 void showMessage(const QString& message, int timeout = 0);
282 void setRightSideMessage(const QString& message);
283 bool isRightSideMessageVisible() const;
284
285 // Set main window title
286 void setWindowTitle(const QString& string);
287
288 protected:
289 /**
290 * This method checks if the main window can be closed by checking all open documents and views.
291 */
292 void closeEvent(QCloseEvent* e) override;
293 void showEvent(QShowEvent* e) override;
294 void hideEvent(QHideEvent* e) override;
295 void timerEvent(QTimerEvent*) override
296 {
297 Q_EMIT timeEvent();
298 }
299 void customEvent(QEvent* e) override;
300 bool event(QEvent* e) override;
301 /**
302 * Try to interpret dropped elements.
303 */
304 void dropEvent(QDropEvent* e) override;
305 /**
306 * Checks if a mime source object can be interpreted.
307 */
308 void dragEnterEvent(QDragEnterEvent* e) override;
309 /**
310 * This method is called from the Qt framework automatically whenever a
311 * QTranslator object has been installed. This allows one to translate all
312 * relevant user visible text.
313 */
314 void changeEvent(QEvent* e) override;
315
316 private:
317 void setupDockWindows();
318 bool setupTaskView();
319 bool setupSelectionView();
320 bool setupReportView();
321 bool setupPythonConsole();
322 bool updateTreeView(bool show);
323 bool updatePropertyView(bool show);
324 bool updateTaskView(bool show);
325 bool updateComboView(bool show);
326 bool updateDAGView(bool show);
327
328 void populateToolBarMenu(QMenu*);
329 void populateDockWindowMenu(QMenu*);
330
331 static void renderDevBuildWarning(
332 QPainter& painter,
333 const QPoint startPosition,
334 const QSize maxSize,
335 QColor color
336 );
337
338 private Q_SLOTS:
339 /**
340 * \internal
341 */
342 void setActiveSubWindow(QWidget*);
343 /**
344 * Activates the associated tab to this widget.
345 */
346 void onWindowActivated(QMdiSubWindow*);
347 /**
348 * Close tab at position index.
349 */
350 void tabCloseRequested(int index);
351 /**
352 * Fills up the menu with the current windows in the workspace.
353 */
354 void onWindowsMenuAboutToShow();
355 /**
356 * Fills up the menu with the current toolbars.
357 */
358 void onToolBarMenuAboutToShow();
359 /**
360 * Fills up the menu with the current dock windows.
361 */
362 void onDockWindowMenuAboutToShow();
363 /**
364 * This method gets frequently activated and test the commands if they are still active.
365 */
366 void _updateActions();
367 /**
368 * \internal
369 */
370 void delayedStartup();
371 /**
372 * \internal
373 */
374 void processMessages(const QList<QString>&);
375 /**
376 * \internal
377 */
378 void clearStatus();
379
380 Q_SIGNALS:
381 void timeEvent();
382 void windowStateChanged(QWidget*);
383 void workbenchActivated(const QString&);
384 void mainWindowClosed();
385
386 private:
387 /// some kind of singleton
388 static MainWindow* instance;
389 struct MainWindowP* d;
390 };
391
392 inline MainWindow* getMainWindow()
393 {
394 return MainWindow::getInstance();
395 }
396
397 // -------------------------------------------------------------
398
399 /** The status bar observer displays the text on the status bar of the main window
400 * in an appropriate color. Normal text messages are black, warnings are orange and
401 * error messages are in red. Log messages are completely ignored.
402 * The class is implemented to be thread-safe.
403 * @see Console
404 * @see ILogger
405 * @author Werner Mayer
406 */
407 class StatusBarObserver: public WindowParameter, public Base::ILogger
408 {
409 public:
410 StatusBarObserver();
411 ~StatusBarObserver() override;
412
413 /** Observes its parameter group. */
414 void OnChange(Base::Subject<const char*>& rCaller, const char* sReason) override;
415
416 void sendLog(
417 const std::string& notifiername,
418 const std::string& msg,
419 Base::LogStyle level,
420 Base::IntendedRecipient recipient,
421 Base::ContentType content
422 ) override;
423
424 /// name of the observer
425 const char* name() override
426 {
427 return "StatusBar";
428 }
429
430 friend class MainWindow;
431
432 private:
433 QString msg, wrn, err, critical;
434 };
435
436 // -------------------------------------------------------------
437
438 /** This is a helper class needed when a style sheet is restored or cleared.
439 * @author Werner Mayer
440 */
441 class ActionStyleEvent: public QEvent
442 {
443 public:
444 static int EventType;
445 enum Style
446 {
447 Restore,
448 Clear
449 };
450
451 explicit ActionStyleEvent(Style type);
452 Style getType() const;
453
454 private:
455 Style type;
456 };
457
458 } // namespace Gui
459
460 #endif // GUI_MAINWINDOW_H