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