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