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