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