]>
vaikene.ee Git - evaf/blob - src/plugins/SdiWindow/sdiwindow.cpp
c0e3587711afa35e00cbab88a421d4c38b634e47
2 * @file SdiWindow/sdiwindow.cpp
3 * @brief SdiWindow module's implementation
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
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.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
20 #include "sdiwindow.h"
23 #include <Common/iLogger>
24 #include <Common/iRegistry>
25 #include <Common/iApp>
26 #include <Common/iProp>
29 #include <QXmlStreamReader>
34 /// iSdiWindow interface instance singleton
35 static iSdiWindow
* mSdiWindow
= 0;
36 } // namespace eVaf::SdiWindow::Internal
37 } // namespace eVaf::SdiWindow
43 //-------------------------------------------------------------------
45 SdiWindow::iSdiWindow
* SdiWindow::iSdiWindow::instance()
47 return SdiWindow::Internal::mSdiWindow
;
51 //-------------------------------------------------------------------
53 SdiWindow::Internal::MainWindow::MainWindow(QWidget
* parent
, Qt::WindowFlags flags
)
54 : QWidget(parent
, flags
)
58 setObjectName(QString("%1-%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
63 // Apply the size specified in a) properties; or b) on the command line (overwrites stored geometry)
66 // Create the default layout
67 mLayout
= new QVBoxLayout
;
72 EVAF_INFO("%s created", qPrintable(objectName()));
75 SdiWindow::Internal::MainWindow::~MainWindow()
82 EVAF_INFO("%s destroyed", qPrintable(objectName()));
85 QString
SdiWindow::Internal::MainWindow::getMainPanelName(QString
const & args
) const
87 QXmlStreamReader
xml(args
);
88 while (!xml
.atEnd()) {
90 if (xml
.isStartElement() && xml
.name() == "attributes") {
91 if (xml
.attributes().hasAttribute("mainPanelName"))
92 return xml
.attributes().value("mainPanelName").toString();
98 bool SdiWindow::Internal::MainWindow::init(QString
const & args
)
100 mMainPanelName
= getMainPanelName(args
);
102 Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
104 setWindowTitle(Common::iApp::instance()->name());
106 // Start the garbage collector timer
107 mTimerId
= startTimer(60 * 1000);
113 EVAF_INFO("%s initialized", qPrintable(objectName()));
118 void SdiWindow::Internal::MainWindow::done()
129 // Delete all the panels
130 for (int i
= mPanels
.size() - 1; i
>= 0; --i
) {
131 QWeakPointer
<Gui::Panel
> p
= mPanels
.at(i
);
136 mMinimizedPanels
.clear();
139 mMainPanelName
.clear();
141 EVAF_INFO("%s finalized", qPrintable(objectName()));
144 void SdiWindow::Internal::MainWindow::addPanel(QString
const & name
, Gui::Panel
* panel
)
146 mPanels
.append(panel
);
147 mPanelNames
.insert(name
, panel
);
149 // If this is the predefined main panel, add it to this window
150 if (!mMainPanelName
.isEmpty()) {
151 if (name
== mMainPanelName
) {
153 mLayout
->addWidget(panel
);
157 // If the predefined main panel name is not set, use the first panel
161 mLayout
->addWidget(panel
);
166 Gui::Panel
* SdiWindow::Internal::MainWindow::panel(QString
const & name
) const
168 QHash
<QString
, QWeakPointer
<Gui::Panel
> >::const_iterator it
= mPanelNames
.constFind(name
);
169 if (it
!= mPanelNames
.constEnd())
170 return it
.value().data();
174 bool SdiWindow::Internal::MainWindow::showPanel(QString
const & name
)
176 Gui::Panel
* p
= panel(name
);
184 void SdiWindow::Internal::MainWindow::saveSettings()
186 static int ver
[4] = {VER_FILE_VERSION
};
187 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
188 settings
.setValue(QString("%1/version/major").arg(objectName()), ver
[0]);
189 settings
.setValue(QString("%1/version/minor").arg(objectName()), ver
[1]);
190 settings
.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
193 void SdiWindow::Internal::MainWindow::restoreSettings()
195 static int ver
[4] = {VER_FILE_VERSION
};
196 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
198 // Ignore saved settings if the version number is not the same
199 // More intelligent checks can be implemented to allow upgrading from previous versions
200 QVariant v
= settings
.value(QString("%1/version/major").arg(objectName()));
201 if (!v
.isValid() || v
.toInt() != ver
[0])
203 v
= settings
.value(QString("%1/version/minor").arg(objectName()));
204 if (!v
.isValid() || v
.toInt() != ver
[1])
207 // Restore the geometry
208 restoreGeometry(settings
.value(QString("%1/geometry").arg(objectName())).toByteArray());
211 void SdiWindow::Internal::MainWindow::setWindowSize()
213 // a) Get window size from properties
214 int w
= Common::iProp::instance()->getValue("windowWidth", 0).toInt();
215 int h
= Common::iProp::instance()->getValue("windowHeight", 0).toInt();
217 // b) Use command line arguments
218 QStringList args
= QApplication::arguments();
219 for (int i
= 1; i
< args
.size(); ++i
) {
220 QStringList arg
= args
.at(i
).simplified().split('=');
221 if (QRegExp("-[-]?w[idth]?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
223 int v
= arg
.at(1).toInt(&ok
);
227 if (QRegExp("-[-]?h[eight]?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
229 int v
= arg
.at(1).toInt(&ok
);
236 if (w
> 0 || h
> 0) {
237 QSize sz
= sizeHint();
246 void SdiWindow::Internal::MainWindow::closeEvent(QCloseEvent
* e
)
248 // Try to close all the managed panels; ignore the event if one of the managed panels refuses to close
249 foreach (QWeakPointer
<Gui::Panel
> p
, mPanels
) {
251 if (!p
.data()->close()) {
258 QWidget::closeEvent(e
);
261 void SdiWindow::Internal::MainWindow::changeEvent(QEvent
* e
)
263 if (e
->type() == QEvent::WindowStateChange
) {
264 QWindowStateChangeEvent
* wse
= static_cast<QWindowStateChangeEvent
*>(e
);
266 if (windowState() == Qt::WindowNoState
&& wse
->oldState() == Qt::WindowMinimized
) {
268 // Restore all the managed panels that were previously minimized
269 foreach (QWeakPointer
<Gui::Panel
> p
, mMinimizedPanels
) {
270 if (p
&& p
.data()->isVisible())
271 p
.data()->showNormal();
273 mMinimizedPanels
.clear();
276 else if (windowState() == Qt::WindowMinimized
&& wse
->oldState() != Qt::WindowMinimized
) {
278 // Minimize all the managed panels that are not minimized yet
279 mMinimizedPanels
.clear();
280 foreach (QWeakPointer
<Gui::Panel
> p
, mPanels
) {
284 if (p
.data()->windowState() != Qt::WindowMinimized
&& p
.data()->isVisible()) {
285 mMinimizedPanels
.append(p
);
286 p
.data()->showMinimized();
291 QWidget::changeEvent(e
);
294 void SdiWindow::Internal::MainWindow::timerEvent(QTimerEvent
* e
)
296 if (e
->timerId() == mTimerId
) {
298 // Remove panels that are deleted
300 QList
<QWeakPointer
<Gui::Panel
> >::iterator it
= mPanels
.begin();
301 while (it
!= mPanels
.end()) {
302 QWeakPointer
<Gui::Panel
> p
= *it
;
304 it
= mPanels
.erase(it
);
310 // Do the same with panel names
312 QHash
<QString
, QWeakPointer
<Gui::Panel
> >::iterator it
= mPanelNames
.begin();
313 while (it
!= mPanelNames
.end()) {
314 QWeakPointer
<Gui::Panel
> p
= it
.value();
316 it
= mPanelNames
.erase(it
);
323 QWidget::timerEvent(e
);
327 //-------------------------------------------------------------------
329 SdiWindow::Internal::SdiWindowPlugin::SdiWindowPlugin()
332 setObjectName(VER_MODULE_NAME_STR
);
334 mWindow
= new MainWindow
;
336 EVAF_INFO("%s created", qPrintable(objectName()));
339 SdiWindow::Internal::SdiWindowPlugin::~SdiWindowPlugin()
343 EVAF_INFO("%s destroyed", qPrintable(objectName()));
346 bool SdiWindow::Internal::SdiWindowPlugin::init(const QString
& args
)
348 if (!mWindow
->init(args
))
351 EVAF_INFO("%s initialized", qPrintable(objectName()));
356 void SdiWindow::Internal::SdiWindowPlugin::done()
360 EVAF_INFO("%s finalized", qPrintable(objectName()));