]> vaikene.ee Git - evaf/blob - src/plugins/SdiWindow/sdiwindow.cpp
a804dfc14cc64dc70a8739612dc8decf3a0636a7
[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
27 #include <QtGui>
28
29 namespace eVaf {
30 namespace SdiWindow {
31 namespace Internal {
32 /// iSdiWindow interface instance singleton
33 static iSdiWindow * mSdiWindow = 0;
34 } // namespace eVaf::SdiWindow::Internal
35 } // namespace eVaf::SdiWindow
36 } // namespace eVaf
37
38
39 using namespace eVaf;
40
41 //-------------------------------------------------------------------
42
43 using namespace eVaf::SdiWindow;
44
45 iSdiWindow * iSdiWindow::instance()
46 {
47 return Internal::mSdiWindow;
48 }
49
50
51 //-------------------------------------------------------------------
52
53 using namespace eVaf::SdiWindow::Internal;
54
55 MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags)
56 : QWidget(parent, flags)
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
64 setWindowSize();
65
66 EVAF_INFO("%s created", qPrintable(objectName()));
67 }
68
69 MainWindow::~MainWindow()
70 {
71 // Save geometry
72 saveSettings();
73
74 EVAF_INFO("%s destroyed", qPrintable(objectName()));
75 }
76
77 bool MainWindow::init()
78 {
79 setWindowTitle(Common::iApp::instance()->name());
80
81 show();
82
83 EVAF_INFO("%s initialized", qPrintable(objectName()));
84
85 return true;
86 }
87
88 void MainWindow::done()
89 {
90 close();
91
92 EVAF_INFO("%s finalized", qPrintable(objectName()));
93 }
94
95 void MainWindow::saveSettings()
96 {
97 static int ver[4] = {VER_FILE_VERSION};
98 QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
99 settings.setValue(QString("%1/version/major").arg(objectName()), ver[0]);
100 settings.setValue(QString("%1/version/minor").arg(objectName()), ver[1]);
101 settings.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
102 }
103
104 void MainWindow::restoreSettings()
105 {
106 static int ver[4] = {VER_FILE_VERSION};
107 QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
108
109 // Ignore saved settings if the version number is not the same
110 // More intelligent checks can be implemented to allow upgrading from previous versions
111 QVariant v = settings.value(QString("%1/version/major").arg(objectName()));
112 if (!v.isValid() || v.toInt() != ver[0])
113 return;
114 v = settings.value(QString("%1/version/minor").arg(objectName()));
115 if (!v.isValid() || v.toInt() != ver[1])
116 return;
117
118 // Restore the geometry
119 restoreGeometry(settings.value(QString("%1/geometry").arg(objectName())).toByteArray());
120 }
121
122 void MainWindow::setWindowSize()
123 {
124 // a) Get window size from properties
125 int w = 0;
126 int h = 0;
127
128 // b) Use command line arguments
129 QStringList args = QApplication::arguments();
130 for (int i = 1; i < args.size(); ++i) {
131 QStringList arg = args.at(i).simplified().split('=');
132 if (QRegExp("-[-]?w[idth]?").exactMatch(arg.at(0)) && arg.size() > 1) {
133 bool ok;
134 int v = arg.at(1).toInt(&ok);
135 if (ok)
136 w = v;
137 }
138 if (QRegExp("-[-]?h[eight]?").exactMatch(arg.at(0)) && arg.size() > 1) {
139 bool ok;
140 int v = arg.at(1).toInt(&ok);
141 if (ok)
142 h = v;
143 }
144 }
145
146 // Resize the window
147 if (w > 0 || h > 0) {
148 QSize sz = sizeHint();
149 if (w > 0)
150 sz.setWidth(w);
151 if (h > 0)
152 sz.setHeight(h);
153 resize(sz);
154 }
155 }
156
157
158 //-------------------------------------------------------------------
159
160 SdiWindowImpl::SdiWindowImpl()
161 : iSdiWindow()
162 , mReady(false)
163 {
164 setObjectName(QString("%1.iSdiWindow").arg(VER_MODULE_NAME_STR));
165
166 mSdiWindow = this;
167
168 wWindow = new MainWindow;
169
170 Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
171
172 EVAF_INFO("%s created", qPrintable(objectName()));
173 }
174
175 SdiWindowImpl::~SdiWindowImpl()
176 {
177 delete wWindow;
178
179 mSdiWindow = 0;
180
181 EVAF_INFO("%s destroyed", qPrintable(objectName()));
182 }
183
184 bool SdiWindowImpl::init(const QString & args)
185 {
186 Q_UNUSED(args);
187
188 if (!wWindow->init())
189 return false;
190
191 mReady = true;
192
193 EVAF_INFO("%s initialized", qPrintable(objectName()));
194
195 return true;
196 }
197
198 void SdiWindowImpl::done()
199 {
200 mReady = false;
201
202 wWindow->done();
203
204 EVAF_INFO("%s finalized", qPrintable(objectName()));
205 }
206
207
208 //-------------------------------------------------------------------
209
210 SdiWindowPlugin::SdiWindowPlugin()
211 : Plugins::iPlugin()
212 {
213 setObjectName(VER_MODULE_NAME_STR);
214
215 mWindow = new SdiWindowImpl;
216
217 EVAF_INFO("%s created", qPrintable(objectName()));
218 }
219
220 SdiWindowPlugin::~SdiWindowPlugin()
221 {
222 delete mWindow;
223
224 EVAF_INFO("%s destroyed", qPrintable(objectName()));
225 }
226
227 bool SdiWindowPlugin::init(const QString & args)
228 {
229 if (!mWindow->init(args))
230 return false;
231
232 EVAF_INFO("%s initialized", qPrintable(objectName()));
233
234 return true;
235 }
236
237 void SdiWindowPlugin::done()
238 {
239 mWindow->done();
240
241 EVAF_INFO("%s finalized", qPrintable(objectName()));
242 }