]>
vaikene.ee Git - evaf/blob - SdiWindow/sdiwindow.cpp
2 * @file SdiWindow/sdiwindow.cpp
3 * @brief SdiWindow module's implementation
6 * Copyright (c) 2011-2019 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
= nullptr;
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());
110 EVAF_INFO("%s initialized", qPrintable(objectName()));
115 void SdiWindow::Internal::MainWindow::done()
121 // Delete all the panels
122 for (int i
= mPanels
.size() - 1; i
>= 0; --i
) {
123 disconnect(mPanels
.at(i
), SIGNAL(destroyed(QObject
*)), this, SLOT(panelDestroyed(QObject
*)));
124 delete mPanels
.at(i
);
127 mMinimizedPanels
.clear();
129 mMainPanel
= nullptr;
130 mMainPanelName
.clear();
132 EVAF_INFO("%s finalized", qPrintable(objectName()));
135 void SdiWindow::Internal::MainWindow::addPanel(QString
const & name
, Gui::Panel
* panel
)
137 connect(panel
, SIGNAL(destroyed(QObject
*)), this, SLOT(panelDestroyed(QObject
*)));
138 mPanels
.append(panel
);
139 mPanelNames
.insert(name
, panel
);
141 // If this is the predefined main panel, add it to this window
142 if (!mMainPanelName
.isEmpty()) {
143 if (name
== mMainPanelName
) {
145 mLayout
->addWidget(panel
);
149 // If the predefined main panel name is not set, use the first panel
153 mLayout
->addWidget(panel
);
158 Gui::Panel
* SdiWindow::Internal::MainWindow::panel(QString
const & name
) const
160 QHash
<QString
, Gui::Panel
*>::const_iterator it
= mPanelNames
.constFind(name
);
161 if (it
!= mPanelNames
.constEnd())
166 bool SdiWindow::Internal::MainWindow::showPanel(QString
const & name
)
168 Gui::Panel
* p
= panel(name
);
176 void SdiWindow::Internal::MainWindow::saveSettings()
178 static int ver
[4] = {VER_FILE_VERSION
};
179 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
180 settings
.setValue(QString("%1/version/major").arg(objectName()), ver
[0]);
181 settings
.setValue(QString("%1/version/minor").arg(objectName()), ver
[1]);
182 settings
.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
185 void SdiWindow::Internal::MainWindow::restoreSettings()
187 static int ver
[4] = {VER_FILE_VERSION
};
188 QSettings
settings(VER_COMPANY_NAME_STR
, Common::iApp::instance()->name());
190 // Ignore saved settings if the version number is not the same
191 // More intelligent checks can be implemented to allow upgrading from previous versions
192 QVariant v
= settings
.value(QString("%1/version/major").arg(objectName()));
193 if (!v
.isValid() || v
.toInt() != ver
[0])
195 v
= settings
.value(QString("%1/version/minor").arg(objectName()));
196 if (!v
.isValid() || v
.toInt() != ver
[1])
199 // Restore the geometry
200 restoreGeometry(settings
.value(QString("%1/geometry").arg(objectName())).toByteArray());
203 void SdiWindow::Internal::MainWindow::setWindowSize()
205 // a) Get window size from properties
206 int w
= Common::iProp::instance()->getValue("windowWidth", 0).toInt();
207 int h
= Common::iProp::instance()->getValue("windowHeight", 0).toInt();
209 // b) Use command line arguments
210 QStringList args
= QApplication::arguments();
211 for (int i
= 1; i
< args
.size(); ++i
) {
212 QStringList arg
= args
.at(i
).simplified().split('=');
213 if (QRegExp("-[-]?w[idth]?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
215 int v
= arg
.at(1).toInt(&ok
);
219 if (QRegExp("-[-]?h[eight]?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
221 int v
= arg
.at(1).toInt(&ok
);
228 if (w
> 0 || h
> 0) {
229 QSize sz
= sizeHint();
238 void SdiWindow::Internal::MainWindow::closeEvent(QCloseEvent
* e
)
240 // Try to close all the managed panels; ignore the event if one of the managed panels refuses to close
241 foreach (Gui::Panel
* p
, mPanels
) {
248 QWidget::closeEvent(e
);
251 void SdiWindow::Internal::MainWindow::changeEvent(QEvent
* e
)
253 if (e
->type() == QEvent::WindowStateChange
) {
254 QWindowStateChangeEvent
* wse
= static_cast<QWindowStateChangeEvent
*>(e
);
256 if (windowState() == Qt::WindowNoState
&& wse
->oldState() == Qt::WindowMinimized
) {
258 // Restore all the managed panels that were previously minimized
259 foreach (Gui::Panel
* p
, mMinimizedPanels
) {
263 mMinimizedPanels
.clear();
266 else if (windowState() == Qt::WindowMinimized
&& wse
->oldState() != Qt::WindowMinimized
) {
268 // Minimize all the managed panels that are not minimized yet
269 mMinimizedPanels
.clear();
270 foreach (Gui::Panel
* p
, mPanels
) {
271 if (p
->windowState() != Qt::WindowMinimized
&& p
->isVisible()) {
272 mMinimizedPanels
.append(p
);
278 QWidget::changeEvent(e
);
281 void SdiWindow::Internal::MainWindow::panelDestroyed(QObject
* obj
)
283 // Remove panels that are deleted
285 QList
<Gui::Panel
*>::iterator it
= mPanels
.begin();
286 while (it
!= mPanels
.end()) {
288 it
= mPanels
.erase(it
);
296 QVector
<Gui::Panel
*>::iterator it
= mMinimizedPanels
.begin();
297 while (it
!= mMinimizedPanels
.end())
301 it
= mMinimizedPanels
.erase(it
);
310 // Do the same with panel names
312 QHash
<QString
, Gui::Panel
*>::iterator it
= mPanelNames
.begin();
313 while (it
!= mPanelNames
.end()) {
314 if (it
.value() == obj
) {
315 it
= mPanelNames
.erase(it
);
323 // If it was the main panel, set the main panel to NULL
324 if (mMainPanel
== obj
) {
325 mMainPanel
= nullptr;
330 //-------------------------------------------------------------------
332 SdiWindow::Internal::SdiWindowPlugin::SdiWindowPlugin()
334 , mWindow(new MainWindow
)
336 setObjectName(VER_MODULE_NAME_STR
);
338 EVAF_INFO("%s created", qPrintable(objectName()));
341 SdiWindow::Internal::SdiWindowPlugin::~SdiWindowPlugin()
345 EVAF_INFO("%s destroyed", qPrintable(objectName()));
348 bool SdiWindow::Internal::SdiWindowPlugin::init(const QString
& args
)
350 if (!mWindow
->init(args
))
353 EVAF_INFO("%s initialized", qPrintable(objectName()));
358 void SdiWindow::Internal::SdiWindowPlugin::done()
362 EVAF_INFO("%s finalized", qPrintable(objectName()));