1 | #include "ProjectionModel.h" |
---|
2 | #include "Channel.h" |
---|
3 | #include "RSync.h" |
---|
4 | #include "gui/QDesignViewWindow.h" |
---|
5 | #include "gui/QDesignViewWidget.h" |
---|
6 | #include "gui/QPanelWidget.h" |
---|
7 | #include "gui/QClientWindow.h" |
---|
8 | #include "gui/QProjectorWindow.h" |
---|
9 | #include "gui/QSceneViewerWindow.h" |
---|
10 | #include "gui/QPreferenceDialog.h" |
---|
11 | |
---|
12 | #include "GUIControler.h" |
---|
13 | |
---|
14 | using namespace projection; |
---|
15 | |
---|
16 | /** |
---|
17 | * Constructor. |
---|
18 | * |
---|
19 | * @param pModel Projection model. |
---|
20 | */ |
---|
21 | GUIControler::GUIControler(ProjectionModel* pModel) |
---|
22 | { |
---|
23 | m_pModel = pModel; |
---|
24 | |
---|
25 | m_pDesignViewWindow = NULL; |
---|
26 | m_pClientWindow = NULL; |
---|
27 | m_pSceneViewerWindow = NULL; |
---|
28 | m_pPreferenceDialog = NULL; |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * Destructor. |
---|
33 | */ |
---|
34 | GUIControler::~GUIControler() |
---|
35 | { |
---|
36 | delete m_pDesignViewWindow; |
---|
37 | delete m_pClientWindow; |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Initialize GUIs. |
---|
42 | * |
---|
43 | * @param bServer True if this program is a server, false if it is a client. |
---|
44 | */ |
---|
45 | void GUIControler::init(bool bServer) |
---|
46 | { |
---|
47 | if (bServer) |
---|
48 | { |
---|
49 | m_pDesignViewWindow = new QDesignViewWindow; |
---|
50 | m_pDesignViewWindow->setModel(m_pModel); |
---|
51 | m_pDesignViewWindow->showMaximized(); |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | m_pClientWindow = new QClientWindow(NULL); |
---|
56 | m_pClientWindow->show(); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Load projection settings from a file. |
---|
62 | * |
---|
63 | * @param fileName File name to load. |
---|
64 | * @return True if successfully loaded. |
---|
65 | */ |
---|
66 | bool GUIControler::loadFile(const QString& fileName) |
---|
67 | { |
---|
68 | if (m_pDesignViewWindow) |
---|
69 | return m_pDesignViewWindow->loadFile(fileName); |
---|
70 | return false; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Save the current projection settings to a file. |
---|
75 | * |
---|
76 | * @param fileName File name to save. |
---|
77 | * @return True if successfully saved. |
---|
78 | */ |
---|
79 | bool GUIControler::saveFile(const QString& fileName) |
---|
80 | { |
---|
81 | if (m_pDesignViewWindow) |
---|
82 | return m_pDesignViewWindow->saveFile(fileName); |
---|
83 | return false; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Retrieve main window. |
---|
88 | * |
---|
89 | * @return DesignViewWindow in server, ClientWindow in Client. |
---|
90 | */ |
---|
91 | QWidget* GUIControler::getMainWindow() |
---|
92 | { |
---|
93 | if (m_pDesignViewWindow) |
---|
94 | return m_pDesignViewWindow; |
---|
95 | if (m_pClientWindow) |
---|
96 | return m_pClientWindow; |
---|
97 | return NULL; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Retrieve QGLWidget for sharing OpenGL context. |
---|
102 | * |
---|
103 | * @return QGLWidget for sharing OpenGL context. |
---|
104 | */ |
---|
105 | QGLWidget* GUIControler::getGLWidget() |
---|
106 | { |
---|
107 | if (m_pDesignViewWindow) { |
---|
108 | return m_pDesignViewWindow->getGLWidget(); |
---|
109 | } |
---|
110 | /* else if (m_pProjectorWindows.size() > 0) { |
---|
111 | return m_pProjectorWindows[0]; |
---|
112 | }*/ |
---|
113 | static QGLWidget* g_pGLWidget = NULL; |
---|
114 | if (!g_pGLWidget) |
---|
115 | g_pGLWidget = new QGLWidget(getMainWindow()); |
---|
116 | return g_pGLWidget; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Select a channel. |
---|
121 | * |
---|
122 | * @param pChannel Channel object to select. |
---|
123 | */ |
---|
124 | void GUIControler::selectChannel(Channel* pChannel) |
---|
125 | { |
---|
126 | if (m_pDesignViewWindow) { |
---|
127 | m_pDesignViewWindow->getPanel()->selectChannel(pChannel); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Remove the specified channel. |
---|
133 | * |
---|
134 | * @param pChannel Channel object to remove. |
---|
135 | */ |
---|
136 | void GUIControler::removeChannel(Channel* pChannel) |
---|
137 | { |
---|
138 | for (int i=0; i<m_pProjectorWindows.size(); ++i) { |
---|
139 | if (m_pProjectorWindows.at(i)->getChannel() == pChannel) { |
---|
140 | QProjectorWindow* pProjectorWindow = m_pProjectorWindows.takeAt(i); |
---|
141 | delete pProjectorWindow; |
---|
142 | return; |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Process key event for channel. |
---|
149 | * |
---|
150 | * @param pChannel Channel to process key event. |
---|
151 | * @param pEvent Key event. |
---|
152 | */ |
---|
153 | void GUIControler::processChannelKeyEvent(Channel* pChannel, QKeyEvent* pEvent) |
---|
154 | { |
---|
155 | m_pDesignViewWindow->getDesignView()->processChannelKeyEvent(pChannel, pEvent); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Update the channel list in GUI. |
---|
160 | */ |
---|
161 | void GUIControler::updateChannelsGUI() |
---|
162 | { |
---|
163 | if (m_pDesignViewWindow) |
---|
164 | m_pDesignViewWindow->getPanel()->updateChannels(); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Update the name of channels in GUI. |
---|
169 | */ |
---|
170 | void GUIControler::updateChannelNamesGUI() |
---|
171 | { |
---|
172 | if (m_pDesignViewWindow) |
---|
173 | m_pDesignViewWindow->getPanel()->updateChannelNames(); |
---|
174 | |
---|
175 | for (int i=0; i<m_pProjectorWindows.size(); ++i) |
---|
176 | m_pProjectorWindows[i]->updateTitle(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Update the channel in GUI. |
---|
181 | */ |
---|
182 | void GUIControler::updateChannelGUI() |
---|
183 | { |
---|
184 | if (m_pDesignViewWindow) |
---|
185 | m_pDesignViewWindow->getPanel()->updateCurrentChannel(); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Update GUI. |
---|
190 | */ |
---|
191 | void GUIControler::updateGUI() |
---|
192 | { |
---|
193 | if (m_pDesignViewWindow) |
---|
194 | m_pDesignViewWindow->getPanel()->updateGUI(); |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Redraw SceneViewer. |
---|
199 | */ |
---|
200 | void GUIControler::updateSceneViewer() |
---|
201 | { |
---|
202 | if (m_pSceneViewerWindow) |
---|
203 | { |
---|
204 | m_pSceneViewerWindow->blockSignals(true); |
---|
205 | m_pSceneViewerWindow->updateGL(); |
---|
206 | m_pSceneViewerWindow->blockSignals(false); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * Put the SceneViewer camera at the center of the scene. |
---|
212 | */ |
---|
213 | void GUIControler::sceneCenterView() |
---|
214 | { |
---|
215 | if (m_pSceneViewerWindow) |
---|
216 | m_pSceneViewerWindow->centerView(); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Set the SceneViewer camera to view all the scene. |
---|
221 | */ |
---|
222 | void GUIControler::sceneViewAll() |
---|
223 | { |
---|
224 | if (m_pSceneViewerWindow) |
---|
225 | m_pSceneViewerWindow->viewAll(); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Redraw DesignViewWindow and ProjectorWindows. |
---|
230 | */ |
---|
231 | void GUIControler::updateViews() |
---|
232 | { |
---|
233 | if (m_pDesignViewWindow) |
---|
234 | m_pDesignViewWindow->getDesignView()->updateGL(); |
---|
235 | |
---|
236 | for (int i=0; i<m_pProjectorWindows.size(); ++i) |
---|
237 | if (m_pProjectorWindows[i]->isVisible()) |
---|
238 | m_pProjectorWindows[i]->updateGL(); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Set scene size for SceneViewer. |
---|
243 | * |
---|
244 | * @param radius Radius of the scene. |
---|
245 | */ |
---|
246 | void GUIControler::setSceneSize(float radius) |
---|
247 | { |
---|
248 | if (m_pSceneViewerWindow) { |
---|
249 | m_pSceneViewerWindow->setSceneSize(radius * 4.0f); |
---|
250 | m_pSceneViewerWindow->setZFar(radius * 4.0f); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Create (if not exist) and show a ProjectorWindow for the specified channel. |
---|
256 | * |
---|
257 | * @param pChannel Channel object for the ProjectorWindow. |
---|
258 | */ |
---|
259 | void GUIControler::showProjectorWindow(Channel* pChannel) |
---|
260 | { |
---|
261 | if (!pChannel) |
---|
262 | return; |
---|
263 | QProjectorWindow* pProjectorWindow = NULL; |
---|
264 | for (int i=0; i<m_pProjectorWindows.size(); ++i) |
---|
265 | if (m_pProjectorWindows.at(i)->getChannel() == pChannel) |
---|
266 | pProjectorWindow = m_pProjectorWindows[i]; |
---|
267 | if (!pProjectorWindow) |
---|
268 | { |
---|
269 | pProjectorWindow = new QProjectorWindow (0, getGLWidget(), Qt::Dialog); |
---|
270 | pProjectorWindow->setModel(m_pModel); |
---|
271 | pProjectorWindow->setChannel(pChannel); |
---|
272 | |
---|
273 | m_pProjectorWindows.append(pProjectorWindow); |
---|
274 | } |
---|
275 | |
---|
276 | if (!m_pModel->getRSync()->isServer() && pChannel->getRemoteFullScreen()) |
---|
277 | { |
---|
278 | QDesktopWidget desktop; |
---|
279 | if (!pProjectorWindow->isFullScreen() || |
---|
280 | desktop.availableGeometry(pChannel->getRemoteScreen()) != desktop.availableGeometry(pProjectorWindow)) |
---|
281 | { |
---|
282 | QRect rect = desktop.availableGeometry(pChannel->getRemoteScreen()); |
---|
283 | pProjectorWindow->move(rect.x() + 10, rect.y() + 10); |
---|
284 | pProjectorWindow->showFullScreen(); |
---|
285 | } |
---|
286 | } else |
---|
287 | pProjectorWindow->showNormal(); |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * Create (if not exist) and show a SceneViewerWindow. |
---|
292 | */ |
---|
293 | void GUIControler::showSceneViewerWindow() |
---|
294 | { |
---|
295 | if (!m_pSceneViewerWindow) |
---|
296 | { |
---|
297 | m_pSceneViewerWindow = new QSceneViewerWindow(0); |
---|
298 | m_pSceneViewerWindow->setModel(m_pModel); |
---|
299 | m_pSceneViewerWindow->initFromDOMElement(m_sceneViewerSettings); |
---|
300 | } |
---|
301 | m_pSceneViewerWindow->show(); |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * Create (if not exist) and show a PreferenceDialog. |
---|
306 | */ |
---|
307 | void GUIControler::showPreferenceDialog() |
---|
308 | { |
---|
309 | if (!m_pPreferenceDialog) |
---|
310 | { |
---|
311 | m_pPreferenceDialog = new QPreferenceDialog(getMainWindow()); |
---|
312 | m_pPreferenceDialog->setModel(m_pModel); |
---|
313 | } |
---|
314 | m_pPreferenceDialog->show(); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * Show an instant message in status bar of the DesignViewWindow. |
---|
319 | * |
---|
320 | * @param message Message to show. |
---|
321 | * @param timeout Time out interval to disappear in micro seconds. |
---|
322 | */ |
---|
323 | void GUIControler::showStatusBarMessage(const QString& message, int timeout) |
---|
324 | { |
---|
325 | if (m_pDesignViewWindow) |
---|
326 | m_pDesignViewWindow->statusBar()->showMessage(message, timeout); |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * Append log to Client Window. |
---|
331 | * |
---|
332 | * @param log Log text. |
---|
333 | */ |
---|
334 | void GUIControler::appendClientLog(const QString& log) |
---|
335 | { |
---|
336 | if (m_pClientWindow) |
---|
337 | m_pClientWindow->appendLog(log); |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * Close all windows to quit the application. |
---|
342 | */ |
---|
343 | void GUIControler::closeAllWindows() |
---|
344 | { |
---|
345 | if (m_pClientWindow) |
---|
346 | m_pClientWindow->close(); |
---|
347 | if (m_pSceneViewerWindow) |
---|
348 | m_pSceneViewerWindow->close(); |
---|
349 | if (m_pPreferenceDialog) |
---|
350 | m_pPreferenceDialog->close(); |
---|
351 | while (m_pProjectorWindows.size() > 0) { |
---|
352 | QProjectorWindow* pProjectorWindow = m_pProjectorWindows.takeAt(0); |
---|
353 | delete pProjectorWindow; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * Restore settings of the DesignView and the SceneViewer. |
---|
359 | * |
---|
360 | * @param Parent XML element of the DesignView and the SceneViewer settings. |
---|
361 | */ |
---|
362 | bool GUIControler::initFromDOMElement(const QDomElement& element) |
---|
363 | { |
---|
364 | if (m_pDesignViewWindow && !element.firstChildElement("DesignViewWindow").isNull()) |
---|
365 | { |
---|
366 | m_pDesignViewWindow->initFromDOMElement(element.firstChildElement("DesignViewWindow")); |
---|
367 | } |
---|
368 | if (!element.firstChildElement("SceneViewerWindow").isNull()) |
---|
369 | { |
---|
370 | m_sceneViewerSettings = element.firstChildElement("SceneViewerWindow"); |
---|
371 | if (!m_pSceneViewerWindow && m_sceneViewerSettings.attribute("show") == "true") |
---|
372 | showSceneViewerWindow(); |
---|
373 | else if (m_pSceneViewerWindow) |
---|
374 | m_pSceneViewerWindow->initFromDOMElement(m_sceneViewerSettings); |
---|
375 | } |
---|
376 | |
---|
377 | return true; // todo: secure this function and return false on any critical error |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * Store the current DesignView and SceneViewer as XML data. |
---|
382 | * |
---|
383 | * @param name XML node name of the data. |
---|
384 | * @param doc XML document to store the data. |
---|
385 | * @return Current DesignView and SceneViewersettings as XML data. |
---|
386 | */ |
---|
387 | QDomElement GUIControler::domElement(const QString& name, QDomDocument& doc) |
---|
388 | { |
---|
389 | QDomElement gui = doc.createElement(name); |
---|
390 | if (m_pDesignViewWindow) |
---|
391 | gui.appendChild(m_pDesignViewWindow->domElement("DesignViewWindow", doc)); |
---|
392 | if (m_pSceneViewerWindow) |
---|
393 | gui.appendChild(m_pSceneViewerWindow->domElement("SceneViewerWindow", doc)); |
---|
394 | else |
---|
395 | gui.appendChild(m_sceneViewerSettings); |
---|
396 | return gui; |
---|
397 | } |
---|