]> vaikene.ee Git - evaf/blob - src/plugins/SdiWindow/sdiwindow.cpp
Changed the way how the main window is used.
[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 // Create the default layout
67 mLayout = new QVBoxLayout;
68 setLayout(mLayout);
69
70 EVAF_INFO("%s created", qPrintable(objectName()));
71 }
72
73 MainWindow::~MainWindow()
74 {
75 // Save geometry
76 saveSettings();
77
78 EVAF_INFO("%s destroyed", qPrintable(objectName()));
79 }
80
81 bool MainWindow::init()
82 {
83 setWindowTitle(Common::iApp::instance()->name());
84
85 show();
86
87 EVAF_INFO("%s initialized", qPrintable(objectName()));
88
89 return true;
90 }
91
92 void MainWindow::done()
93 {
94 close();
95
96 // Delete all the items added to the main window
97 while (mItemsAdded.count() > 0)
98 delete mItemsAdded.takeAt(0);
99
100 EVAF_INFO("%s finalized", qPrintable(objectName()));
101 }
102
103 void MainWindow::addWidget(QWidget * widget)
104 {
105 mLayout->addWidget(widget);
106 mItemsAdded.append(widget);
107 }
108
109 void MainWindow::addLayout(QLayout * layout)
110 {
111 mLayout->addLayout(layout);
112 mItemsAdded.append(layout);
113 }
114
115 void MainWindow::saveSettings()
116 {
117 static int ver[4] = {VER_FILE_VERSION};
118 QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
119 settings.setValue(QString("%1/version/major").arg(objectName()), ver[0]);
120 settings.setValue(QString("%1/version/minor").arg(objectName()), ver[1]);
121 settings.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
122 }
123
124 void MainWindow::restoreSettings()
125 {
126 static int ver[4] = {VER_FILE_VERSION};
127 QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
128
129 // Ignore saved settings if the version number is not the same
130 // More intelligent checks can be implemented to allow upgrading from previous versions
131 QVariant v = settings.value(QString("%1/version/major").arg(objectName()));
132 if (!v.isValid() || v.toInt() != ver[0])
133 return;
134 v = settings.value(QString("%1/version/minor").arg(objectName()));
135 if (!v.isValid() || v.toInt() != ver[1])
136 return;
137
138 // Restore the geometry
139 restoreGeometry(settings.value(QString("%1/geometry").arg(objectName())).toByteArray());
140 }
141
142 void MainWindow::setWindowSize()
143 {
144 // a) Get window size from properties
145 int w = 0;
146 int h = 0;
147
148 // b) Use command line arguments
149 QStringList args = QApplication::arguments();
150 for (int i = 1; i < args.size(); ++i) {
151 QStringList arg = args.at(i).simplified().split('=');
152 if (QRegExp("-[-]?w[idth]?").exactMatch(arg.at(0)) && arg.size() > 1) {
153 bool ok;
154 int v = arg.at(1).toInt(&ok);
155 if (ok)
156 w = v;
157 }
158 if (QRegExp("-[-]?h[eight]?").exactMatch(arg.at(0)) && arg.size() > 1) {
159 bool ok;
160 int v = arg.at(1).toInt(&ok);
161 if (ok)
162 h = v;
163 }
164 }
165
166 // Resize the window
167 if (w > 0 || h > 0) {
168 QSize sz = sizeHint();
169 if (w > 0)
170 sz.setWidth(w);
171 if (h > 0)
172 sz.setHeight(h);
173 resize(sz);
174 }
175 }
176
177
178 //-------------------------------------------------------------------
179
180 SdiWindowImpl::SdiWindowImpl()
181 : iSdiWindow()
182 , mReady(false)
183 {
184 setObjectName(QString("%1.iSdiWindow").arg(VER_MODULE_NAME_STR));
185
186 mSdiWindow = this;
187
188 wWindow = new MainWindow;
189
190 Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
191
192 EVAF_INFO("%s created", qPrintable(objectName()));
193 }
194
195 SdiWindowImpl::~SdiWindowImpl()
196 {
197 delete wWindow;
198
199 mSdiWindow = 0;
200
201 EVAF_INFO("%s destroyed", qPrintable(objectName()));
202 }
203
204 bool SdiWindowImpl::init(const QString & args)
205 {
206 Q_UNUSED(args);
207
208 if (!wWindow->init())
209 return false;
210
211 mReady = true;
212
213 EVAF_INFO("%s initialized", qPrintable(objectName()));
214
215 return true;
216 }
217
218 void SdiWindowImpl::done()
219 {
220 mReady = false;
221
222 wWindow->done();
223
224 EVAF_INFO("%s finalized", qPrintable(objectName()));
225 }
226
227
228 //-------------------------------------------------------------------
229
230 SdiWindowPlugin::SdiWindowPlugin()
231 : Plugins::iPlugin()
232 {
233 setObjectName(VER_MODULE_NAME_STR);
234
235 mWindow = new SdiWindowImpl;
236
237 EVAF_INFO("%s created", qPrintable(objectName()));
238 }
239
240 SdiWindowPlugin::~SdiWindowPlugin()
241 {
242 delete mWindow;
243
244 EVAF_INFO("%s destroyed", qPrintable(objectName()));
245 }
246
247 bool SdiWindowPlugin::init(const QString & args)
248 {
249 if (!mWindow->init(args))
250 return false;
251
252 EVAF_INFO("%s initialized", qPrintable(objectName()));
253
254 return true;
255 }
256
257 void SdiWindowPlugin::done()
258 {
259 mWindow->done();
260
261 EVAF_INFO("%s finalized", qPrintable(objectName()));
262 }