]>
vaikene.ee Git - evaf/blob - src/plugins/SdiWindow/sdiwindow.cpp
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
)
57 setObjectName(QString("%1-%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
62 // Apply the size specified in a) properties; or b) on the command line (overwrites stored geometry)
65 // Create the default layout
66 mLayout
= new QVBoxLayout
;
71 EVAF_INFO("%s created", qPrintable(objectName()));
74 SdiWindow::Internal::MainWindow::~MainWindow()
81 EVAF_INFO("%s destroyed", qPrintable(objectName()));
84 QString
SdiWindow::Internal::MainWindow::getMainPanelName(QString
const & args
) const
86 QXmlStreamReader
xml(args
);
87 while (!xml
.atEnd()) {
89 if (xml
.isStartElement() && xml
.name() == "attributes") {
90 if (xml
.attributes().hasAttribute("mainPanelName"))
91 return xml
.attributes().value("mainPanelName").toString();
97 bool SdiWindow::Internal::MainWindow::init(QString
const & args
)
99 mMainPanelName
= getMainPanelName(args
);
101 Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
103 setWindowTitle(Common::iApp::instance()->name());
109 EVAF_INFO("%s initialized", qPrintable(objectName()));
114 void SdiWindow::Internal::MainWindow::done()
120 // Delete all the panels
121 for (int i
= mPanels
.size() - 1; i
>= 0; --i
) {
122 disconnect(mPanels
.at(i
), SIGNAL(destroyed(QObject
*)), this, SLOT(panelDestroyed(QObject
*)));
123 delete mPanels
.at(i
);
126 mMinimizedPanels
.clear();
129 mMainPanelName
.clear();
131 EVAF_INFO("%s finalized", qPrintable(objectName()));
134 void SdiWindow::Internal::MainWindow::addPanel(QString
const & name
, Gui::Panel
* panel
)
136 connect(panel
, SIGNAL(destroyed(QObject
*)), this, SLOT(panelDestroyed(QObject
*)));
137 mPanels
.append(panel
);
138 mPanelNames
.insert(name
, panel
);
140 // If this is the predefined main panel, add it to this window
141 if (!mMainPanelName
.isEmpty()) {
142 if (name
== mMainPanelName
) {
144 mLayout
->addWidget(panel
);
148 // If the predefined main panel name is not set, use the first panel
152 mLayout
->addWidget(panel
);
157 Gui::Panel
* SdiWindow::Internal::MainWindow::panel(QString
const & name
) const
159 QHash
<QString
, Gui::Panel
*>::const_iterator it
= mPanelNames
.constFind(name
);
160 if (it
!= mPanelNames
.constEnd())
165 bool SdiWindow::Internal::MainWindow::showPanel(QString
const & name
)
167 Gui::Panel
* p
= panel(name
);
175 void SdiWindow::Internal::MainWindow::saveSettings()
177 static int ver
[4] = {VER_FILE_VERSION
};
178 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
179 settings
.setValue(QString("%1/version/major").arg(objectName()), ver
[0]);
180 settings
.setValue(QString("%1/version/minor").arg(objectName()), ver
[1]);
181 settings
.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
184 void SdiWindow::Internal::MainWindow::restoreSettings()
186 static int ver
[4] = {VER_FILE_VERSION
};
187 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
189 // Ignore saved settings if the version number is not the same
190 // More intelligent checks can be implemented to allow upgrading from previous versions
191 QVariant v
= settings
.value(QString("%1/version/major").arg(objectName()));
192 if (!v
.isValid() || v
.toInt() != ver
[0])
194 v
= settings
.value(QString("%1/version/minor").arg(objectName()));
195 if (!v
.isValid() || v
.toInt() != ver
[1])
198 // Restore the geometry
199 restoreGeometry(settings
.value(QString("%1/geometry").arg(objectName())).toByteArray());
202 void SdiWindow::Internal::MainWindow::setWindowSize()
204 // a) Get window size from properties
205 int w
= Common::iProp::instance()->getValue("windowWidth", 0).toInt();
206 int h
= Common::iProp::instance()->getValue("windowHeight", 0).toInt();
208 // b) Use command line arguments
209 QStringList args
= QApplication::arguments();
210 for (int i
= 1; i
< args
.size(); ++i
) {
211 QStringList arg
= args
.at(i
).simplified().split('=');
212 if (QRegExp("-[-]?w[idth]?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
214 int v
= arg
.at(1).toInt(&ok
);
218 if (QRegExp("-[-]?h[eight]?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
220 int v
= arg
.at(1).toInt(&ok
);
227 if (w
> 0 || h
> 0) {
228 QSize sz
= sizeHint();
237 void SdiWindow::Internal::MainWindow::closeEvent(QCloseEvent
* e
)
239 // Try to close all the managed panels; ignore the event if one of the managed panels refuses to close
240 foreach (Gui::Panel
* p
, mPanels
) {
247 QWidget::closeEvent(e
);
250 void SdiWindow::Internal::MainWindow::changeEvent(QEvent
* e
)
252 if (e
->type() == QEvent::WindowStateChange
) {
253 QWindowStateChangeEvent
* wse
= static_cast<QWindowStateChangeEvent
*>(e
);
255 if (windowState() == Qt::WindowNoState
&& wse
->oldState() == Qt::WindowMinimized
) {
257 // Restore all the managed panels that were previously minimized
258 foreach (Gui::Panel
* p
, mMinimizedPanels
) {
262 mMinimizedPanels
.clear();
265 else if (windowState() == Qt::WindowMinimized
&& wse
->oldState() != Qt::WindowMinimized
) {
267 // Minimize all the managed panels that are not minimized yet
268 mMinimizedPanels
.clear();
269 foreach (Gui::Panel
* p
, mPanels
) {
270 if (p
->windowState() != Qt::WindowMinimized
&& p
->isVisible()) {
271 mMinimizedPanels
.append(p
);
277 QWidget::changeEvent(e
);
280 void SdiWindow::Internal::MainWindow::panelDestroyed(QObject
* obj
)
282 // Remove panels that are deleted
284 QList
<Gui::Panel
*>::iterator it
= mPanels
.begin();
285 while (it
!= mPanels
.end()) {
287 it
= mPanels
.erase(it
);
295 // Do the same with panel names
297 QHash
<QString
, Gui::Panel
*>::iterator it
= mPanelNames
.begin();
298 while (it
!= mPanelNames
.end()) {
299 if (it
.value() == obj
) {
300 it
= mPanelNames
.erase(it
);
308 if (mMainPanel
== obj
) {
314 //-------------------------------------------------------------------
316 SdiWindow::Internal::SdiWindowPlugin::SdiWindowPlugin()
319 setObjectName(VER_MODULE_NAME_STR
);
321 mWindow
= new MainWindow
;
323 EVAF_INFO("%s created", qPrintable(objectName()));
326 SdiWindow::Internal::SdiWindowPlugin::~SdiWindowPlugin()
330 EVAF_INFO("%s destroyed", qPrintable(objectName()));
333 bool SdiWindow::Internal::SdiWindowPlugin::init(const QString
& args
)
335 if (!mWindow
->init(args
))
338 EVAF_INFO("%s initialized", qPrintable(objectName()));
343 void SdiWindow::Internal::SdiWindowPlugin::done()
347 EVAF_INFO("%s finalized", qPrintable(objectName()));