]> vaikene.ee Git - evaf/blob - src/plugins/SdiWindow/sdiwindow.cpp
Fixed uninitialized main panel variable.
[evaf] / src / plugins / SdiWindow / sdiwindow.cpp
1 /**
2 * @file SdiWindow/sdiwindow.cpp
3 * @brief SdiWindow module's implementation
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "sdiwindow.h"
21 #include "version.h"
22
23 #include <Common/iLogger>
24 #include <Common/iRegistry>
25 #include <Common/iApp>
26 #include <Common/iProp>
27
28 #include <QtWidgets>
29 #include <QXmlStreamReader>
30
31 namespace eVaf {
32 namespace SdiWindow {
33 namespace Internal {
34 /// iSdiWindow interface instance singleton
35 static iSdiWindow * mSdiWindow = 0;
36 } // namespace eVaf::SdiWindow::Internal
37 } // namespace eVaf::SdiWindow
38 } // namespace eVaf
39
40
41 using namespace eVaf;
42
43 //-------------------------------------------------------------------
44
45 SdiWindow::iSdiWindow * SdiWindow::iSdiWindow::instance()
46 {
47 return SdiWindow::Internal::mSdiWindow;
48 }
49
50
51 //-------------------------------------------------------------------
52
53 SdiWindow::Internal::MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags)
54 : QWidget(parent, flags)
55 , mReady(false)
56 , mMainPanel(0)
57 {
58 setObjectName(QString("%1-%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
59
60 // Restore geometry
61 restoreSettings();
62
63 // Apply the size specified in a) properties; or b) on the command line (overwrites stored geometry)
64 setWindowSize();
65
66 // Create the default layout
67 mLayout = new QVBoxLayout;
68 setLayout(mLayout);
69
70 mSdiWindow = this;
71
72 EVAF_INFO("%s created", qPrintable(objectName()));
73 }
74
75 SdiWindow::Internal::MainWindow::~MainWindow()
76 {
77 mSdiWindow = 0;
78
79 // Save geometry
80 saveSettings();
81
82 EVAF_INFO("%s destroyed", qPrintable(objectName()));
83 }
84
85 QString SdiWindow::Internal::MainWindow::getMainPanelName(QString const & args) const
86 {
87 QXmlStreamReader xml(args);
88 while (!xml.atEnd()) {
89 xml.readNext();
90 if (xml.isStartElement() && xml.name() == "attributes") {
91 if (xml.attributes().hasAttribute("mainPanelName"))
92 return xml.attributes().value("mainPanelName").toString();
93 }
94 }
95 return QString();
96 }
97
98 bool SdiWindow::Internal::MainWindow::init(QString const & args)
99 {
100 mMainPanelName = getMainPanelName(args);
101
102 Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
103
104 setWindowTitle(Common::iApp::instance()->name());
105
106 show();
107
108 mReady = true;
109
110 EVAF_INFO("%s initialized", qPrintable(objectName()));
111
112 return true;
113 }
114
115 void SdiWindow::Internal::MainWindow::done()
116 {
117 mReady = false;
118
119 close();
120
121 // Delete all the panels
122 for (int i = mPanels.size() - 1; i >= 0; --i) {
123 disconnect(mPanels.at(i), SIGNAL(destroyed(QObject *)), this, SLOT(panelDestroyed(QObject *)));
124 delete mPanels.at(i);
125 }
126 mPanels.clear();
127 mMinimizedPanels.clear();
128 mPanelNames.clear();
129 mMainPanel = 0;
130 mMainPanelName.clear();
131
132 EVAF_INFO("%s finalized", qPrintable(objectName()));
133 }
134
135 void SdiWindow::Internal::MainWindow::addPanel(QString const & name, Gui::Panel * panel)
136 {
137 connect(panel, SIGNAL(destroyed(QObject *)), this, SLOT(panelDestroyed(QObject*)));
138 mPanels.append(panel);
139 mPanelNames.insert(name, panel);
140
141 // If this is the predefined main panel, add it to this window
142 if (!mMainPanelName.isEmpty()) {
143 if (name == mMainPanelName) {
144 mMainPanel = panel;
145 mLayout->addWidget(panel);
146 }
147 }
148
149 // If the predefined main panel name is not set, use the first panel
150 else {
151 if (!mMainPanel) {
152 mMainPanel = panel;
153 mLayout->addWidget(panel);
154 }
155 }
156 }
157
158 Gui::Panel * SdiWindow::Internal::MainWindow::panel(QString const & name) const
159 {
160 QHash<QString, Gui::Panel *>::const_iterator it = mPanelNames.constFind(name);
161 if (it != mPanelNames.constEnd())
162 return it.value();
163 return 0;
164 }
165
166 bool SdiWindow::Internal::MainWindow::showPanel(QString const & name)
167 {
168 Gui::Panel * p = panel(name);
169 if (p) {
170 p->show();
171 return true;
172 }
173 return false;
174 }
175
176 void SdiWindow::Internal::MainWindow::saveSettings()
177 {
178 static int ver[4] = {VER_FILE_VERSION};
179 QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
180 settings.setValue(QString("%1/version/major").arg(objectName()), ver[0]);
181 settings.setValue(QString("%1/version/minor").arg(objectName()), ver[1]);
182 settings.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
183 }
184
185 void SdiWindow::Internal::MainWindow::restoreSettings()
186 {
187 static int ver[4] = {VER_FILE_VERSION};
188 QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
189
190 // Ignore saved settings if the version number is not the same
191 // More intelligent checks can be implemented to allow upgrading from previous versions
192 QVariant v = settings.value(QString("%1/version/major").arg(objectName()));
193 if (!v.isValid() || v.toInt() != ver[0])
194 return;
195 v = settings.value(QString("%1/version/minor").arg(objectName()));
196 if (!v.isValid() || v.toInt() != ver[1])
197 return;
198
199 // Restore the geometry
200 restoreGeometry(settings.value(QString("%1/geometry").arg(objectName())).toByteArray());
201 }
202
203 void SdiWindow::Internal::MainWindow::setWindowSize()
204 {
205 // a) Get window size from properties
206 int w = Common::iProp::instance()->getValue("windowWidth", 0).toInt();
207 int h = Common::iProp::instance()->getValue("windowHeight", 0).toInt();
208
209 // b) Use command line arguments
210 QStringList args = QApplication::arguments();
211 for (int i = 1; i < args.size(); ++i) {
212 QStringList arg = args.at(i).simplified().split('=');
213 if (QRegExp("-[-]?w[idth]?").exactMatch(arg.at(0)) && arg.size() > 1) {
214 bool ok;
215 int v = arg.at(1).toInt(&ok);
216 if (ok)
217 w = v;
218 }
219 if (QRegExp("-[-]?h[eight]?").exactMatch(arg.at(0)) && arg.size() > 1) {
220 bool ok;
221 int v = arg.at(1).toInt(&ok);
222 if (ok)
223 h = v;
224 }
225 }
226
227 // Resize the window
228 if (w > 0 || h > 0) {
229 QSize sz = sizeHint();
230 if (w > 0)
231 sz.setWidth(w);
232 if (h > 0)
233 sz.setHeight(h);
234 resize(sz);
235 }
236 }
237
238 void SdiWindow::Internal::MainWindow::closeEvent(QCloseEvent * e)
239 {
240 // Try to close all the managed panels; ignore the event if one of the managed panels refuses to close
241 foreach (Gui::Panel * p, mPanels) {
242 if (!p->close()) {
243 e->ignore();
244 return;
245 }
246 }
247
248 QWidget::closeEvent(e);
249 }
250
251 void SdiWindow::Internal::MainWindow::changeEvent(QEvent * e)
252 {
253 if (e->type() == QEvent::WindowStateChange) {
254 QWindowStateChangeEvent * wse = static_cast<QWindowStateChangeEvent *>(e);
255
256 if (windowState() == Qt::WindowNoState && wse->oldState() == Qt::WindowMinimized) {
257
258 // Restore all the managed panels that were previously minimized
259 foreach (Gui::Panel * p, mMinimizedPanels) {
260 if (p->isVisible())
261 p->showNormal();
262 }
263 mMinimizedPanels.clear();
264 }
265
266 else if (windowState() == Qt::WindowMinimized && wse->oldState() != Qt::WindowMinimized) {
267
268 // Minimize all the managed panels that are not minimized yet
269 mMinimizedPanels.clear();
270 foreach (Gui::Panel * p, mPanels) {
271 if (p->windowState() != Qt::WindowMinimized && p->isVisible()) {
272 mMinimizedPanels.append(p);
273 p->showMinimized();
274 }
275 }
276 }
277 }
278 QWidget::changeEvent(e);
279 }
280
281 void SdiWindow::Internal::MainWindow::panelDestroyed(QObject * obj)
282 {
283 // Remove panels that are deleted
284 {
285 QList<Gui::Panel *>::iterator it = mPanels.begin();
286 while (it != mPanels.end()) {
287 if (*it == obj) {
288 it = mPanels.erase(it);
289 }
290 else {
291 ++it;
292 }
293 }
294 }
295
296 // Do the same with panel names
297 {
298 QHash<QString, Gui::Panel *>::iterator it = mPanelNames.begin();
299 while (it != mPanelNames.end()) {
300 if (it.value() == obj) {
301 it = mPanelNames.erase(it);
302 }
303 else {
304 ++it;
305 }
306 }
307 }
308
309 if (mMainPanel == obj) {
310 mMainPanel = 0;
311 }
312 }
313
314
315 //-------------------------------------------------------------------
316
317 SdiWindow::Internal::SdiWindowPlugin::SdiWindowPlugin()
318 : Plugins::iPlugin()
319 {
320 setObjectName(VER_MODULE_NAME_STR);
321
322 mWindow = new MainWindow;
323
324 EVAF_INFO("%s created", qPrintable(objectName()));
325 }
326
327 SdiWindow::Internal::SdiWindowPlugin::~SdiWindowPlugin()
328 {
329 delete mWindow;
330
331 EVAF_INFO("%s destroyed", qPrintable(objectName()));
332 }
333
334 bool SdiWindow::Internal::SdiWindowPlugin::init(const QString & args)
335 {
336 if (!mWindow->init(args))
337 return false;
338
339 EVAF_INFO("%s initialized", qPrintable(objectName()));
340
341 return true;
342 }
343
344 void SdiWindow::Internal::SdiWindowPlugin::done()
345 {
346 mWindow->done();
347
348 EVAF_INFO("%s finalized", qPrintable(objectName()));
349 }