1 /*
2  *    Copyright 2012, 2013 Thomas Schöps
3  *    Copyright 2014 Thomas Schöps, Kai Pastor
4  *
5  *    This file is part of OpenOrienteering.
6  *
7  *    OpenOrienteering is free software: you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation, either version 3 of the License, or
10  *    (at your option) any later version.
11  *
12  *    OpenOrienteering is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #ifndef OPENORIENTEERING_MAIN_WINDOW_CONTROLLER_H
23 #define OPENORIENTEERING_MAIN_WINDOW_CONTROLLER_H
24 
25 #include <QObject>
26 #include <QString>
27 
28 class QKeyEvent;
29 class QWidget;
30 
31 namespace OpenOrienteering {
32 
33 class MainWindow;
34 class FileFormat;
35 
36 
37 /** A MainWindowController provides the specific content and
38  *  behaviours for a main window, for example map drawing or
39  *  course setting functions.
40  */
41 class MainWindowController : public QObject
42 {
43 Q_OBJECT
44 public:
45 
46 	~MainWindowController() override;
47 
48 
49 	/**
50 	 * Returns true when the controller wants a menu bar to be shown.
51 	 *
52 	 * The default implementation returns false for mobile mode,
53 	 * true otherwise.
54 	 */
55 	virtual bool menuBarVisible();
56 
57 	/**
58 	 * Returns true when the controller wants a status bar to be shown.
59 	 *
60 	 * The default implementation returns false for mobile mode,
61 	 * true otherwise.
62 	 */
63 	virtual bool statusBarVisible();
64 
65 
66 	/** Save to a file.
67 	 *  @param path the path to save to
68 	 *  @param format the file format
69 	 *  @return true if saving was successful, false on errors
70 	 */
71 	virtual bool saveTo(const QString& path, const FileFormat& format);
72 
73 	/** Export to a file, but don't change modified state
74 	 *  with regard to the original file.
75 	 *  @param path the path to export to
76 	 *  @return true if saving was successful, false on errors
77 	 */
78 	bool exportTo(const QString& path);
79 
80 	/** Export to a file, but don't change modified state
81 	 *  with regard to the original file.
82 	 *  @param path the path to export to
83 	 *  @param format the file format
84 	 *  @return true if saving was successful, false on errors
85 	 */
86 	virtual bool exportTo(const QString& path, const FileFormat& format);
87 
88 	/** Load from a file.
89 	 *  @param path the path to load from
90 	 *  @param dialog_parent Alternative parent widget for all dialogs.
91 	 *      If nullptr, implementations should use MainWindowController::window.
92 	 *  @return true if loading was successful, false on errors
93 	 */
94 	virtual bool loadFrom(const QString& path, const FileFormat& format, QWidget* dialog_parent = nullptr);
95 
96 	/** Attach the controller to a main window.
97 	 *  The controller should create its user interface here.
98 	 */
99 	virtual void attach(MainWindow* window) = 0;
100 
101 	/** Detach the controller from a main window.
102 	 *  The controller should delete its user interface here.
103 	 */
104 	virtual void detach();
105 
106 	/**
107 	 * Returns true when editing is in progress.
108 	 *
109 	 * "Editing in progress" means the file is an "unstable" state where no
110 	 * global operations like save, undo, redo shall not be applied.
111 	 */
112 	virtual bool isEditingInProgress() const;
113 
114 	/**
115 	 * @brief Receives key press events from the main window.
116 	 *
117 	 * QKeyEvent starts with isAccepted() == true, so the return value of this
118 	 * function decides if the event shall be stopped from being handled further.
119 	 *
120 	 * The default implementation simply returns false.
121 	 *
122 	 * @return True if the event shall be stopped from being handled further, false otherwise.
123 	 */
124 	virtual bool keyPressEventFilter(QKeyEvent* event);
125 
126 	/**
127 	 * @brief Receives key release events from the main window.
128 	 *
129 	 * QKeyEvent starts with isAccepted() == true, so the return value of this
130 	 * function decides if the event shall be stopped from being handled further.
131 	 *
132 	 * The default implementation simply returns false.
133 	 *
134 	 * @return True if the event shall be stopped from being handled further, false otherwise.
135 	 */
136 	virtual bool keyReleaseEventFilter(QKeyEvent* event);
137 
138 	/** Get the main window this controller is attached to.
139 	 */
140 	inline MainWindow* getWindow() const;
141 
142 	/** Get a controller suitable for a particular file.
143 	 *  @param filename the name of the file
144 	 *  @return a MainWindowController that is able to load the file
145 	 */
146 	static MainWindowController* controllerForFile(const QString& filename);
147 
148 protected:
149 	MainWindow* window;
150 };
151 
152 
153 
154 //### MainWindowController inline code ###
155 
156 inline
getWindow()157 MainWindow* MainWindowController::getWindow() const
158 {
159 	return window;
160 }
161 
162 
163 }  // namespace OpenOrienteering
164 
165 #endif
166