+/**
+ * @file SdiWindow/sdiwindow.cpp
+ * @brief SdiWindow module's implementation
+ * @author Enar Vaikene
+ *
+ * Copyright (c) 2011 Enar Vaikene
+ *
+ * This file is part of the eVaf C++ cross-platform application development framework.
+ *
+ * This file can be used under the terms of the GNU General Public License
+ * version 3.0 as published by the Free Software Foundation and appearing in
+ * the file LICENSE included in the packaging of this file. Please review the
+ * the following information to ensure the GNU General Public License version
+ * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
+ *
+ * Alternatively, this file may be used in accordance with the Commercial License
+ * Agreement provided with the Software.
+ */
+
+#include "sdiwindow.h"
+#include "version.h"
+
+#include <Common/iLogger>
+#include <Common/iRegistry>
+
+#include <QtGui>
+
+namespace eVaf {
+namespace SdiWindow {
+namespace Internal {
+ /// iSdiWindow interface instance singleton
+ static iSdiWindow * mSdiWindow = 0;
+} // namespace eVaf::SdiWindow::Internal
+} // namespace eVaf::SdiWindow
+} // namespace eVaf
+
+
+using namespace eVaf;
+
+//-------------------------------------------------------------------
+
+using namespace eVaf::SdiWindow;
+
+iSdiWindow * iSdiWindow::instance()
+{
+ return Internal::mSdiWindow;
+}
+
+
+//-------------------------------------------------------------------
+
+using namespace eVaf::SdiWindow::Internal;
+
+MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags)
+ : QWidget(parent, flags)
+{
+ setObjectName(QString("%1-%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
+
+ // Restore geometry
+ restoreSettings();
+
+ // Apply the size specified in a) properties; or b) on the command line
+ setWindowSize();
+
+ EVAF_INFO("%s created", qPrintable(objectName()));
+}
+
+MainWindow::~MainWindow()
+{
+ // Save geometry
+ saveSettings();
+
+ EVAF_INFO("%s destroyed", qPrintable(objectName()));
+}
+
+bool MainWindow::init()
+{
+ show();
+
+ EVAF_INFO("%s initialized", qPrintable(objectName()));
+
+ return true;
+}
+
+void MainWindow::done()
+{
+ close();
+
+ EVAF_INFO("%s finalized", qPrintable(objectName()));
+}
+
+void MainWindow::saveSettings()
+{
+ static int ver[4] = {VER_FILE_VERSION};
+ QSettings settings(VER_COMPANY_NAME_STR, VER_PRODUCT_NAME_STR);
+ settings.setValue(QString("%1/version/major").arg(objectName()), ver[0]);
+ settings.setValue(QString("%1/version/minor").arg(objectName()), ver[1]);
+ settings.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
+}
+
+void MainWindow::restoreSettings()
+{
+ static int ver[4] = {VER_FILE_VERSION};
+ QSettings settings(VER_COMPANY_NAME_STR, VER_PRODUCT_NAME_STR);
+
+ // Ignore saved settings if the version number is not the same
+ // More intelligent checks can be implemented to allow upgrading from previous versions
+ QVariant v = settings.value(QString("%1/version/major").arg(objectName()));
+ if (!v.isValid() || v.toInt() != ver[0])
+ return;
+ v = settings.value(QString("%1/version/minor").arg(objectName()));
+ if (!v.isValid() || v.toInt() != ver[1])
+ return;
+
+ // Restore the geometry
+ restoreGeometry(settings.value(QString("%1/geometry").arg(objectName())).toByteArray());
+}
+
+void MainWindow::setWindowSize()
+{
+ // a) Get window size from properties
+ int w = 0;
+ int h = 0;
+
+ // b) Use command line arguments
+ QStringList args = QApplication::arguments();
+ for (int i = 1; i < args.size(); ++i) {
+ QStringList arg = args.at(i).simplified().split('=');
+ if (QRegExp("-[-]?w[idth]?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ bool ok;
+ int v = arg.at(1).toInt(&ok);
+ if (ok)
+ w = v;
+ }
+ if (QRegExp("-[-]?h[eight]?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ bool ok;
+ int v = arg.at(1).toInt(&ok);
+ if (ok)
+ h = v;
+ }
+ }
+
+ // Resize the window
+ if (w > 0 || h > 0) {
+ QSize sz = sizeHint();
+ if (w > 0)
+ sz.setWidth(w);
+ if (h > 0)
+ sz.setHeight(h);
+ resize(sz);
+ }
+}
+
+
+//-------------------------------------------------------------------
+
+SdiWindowImpl::SdiWindowImpl()
+ : iSdiWindow()
+ , mReady(false)
+{
+ setObjectName(QString("%1.iSdiWindow").arg(VER_MODULE_NAME_STR));
+
+ mSdiWindow = this;
+
+ wWindow = new MainWindow;
+
+ Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
+
+ EVAF_INFO("%s created", qPrintable(objectName()));
+}
+
+SdiWindowImpl::~SdiWindowImpl()
+{
+ delete wWindow;
+
+ mSdiWindow = 0;
+
+ EVAF_INFO("%s destroyed", qPrintable(objectName()));
+}
+
+bool SdiWindowImpl::init(const QString & args)
+{
+ Q_UNUSED(args);
+
+ if (!wWindow->init())
+ return false;
+
+ mReady = true;
+
+ EVAF_INFO("%s initialized", qPrintable(objectName()));
+
+ return true;
+}
+
+void SdiWindowImpl::done()
+{
+ mReady = false;
+
+ wWindow->done();
+
+ EVAF_INFO("%s finalized", qPrintable(objectName()));
+}
+
+
+//-------------------------------------------------------------------
+
+SdiWindowPlugin::SdiWindowPlugin()
+ : Plugins::iPlugin()
+{
+ setObjectName(VER_MODULE_NAME_STR);
+
+ mWindow = new SdiWindowImpl;
+
+ EVAF_INFO("%s created", qPrintable(objectName()));
+}
+
+SdiWindowPlugin::~SdiWindowPlugin()
+{
+ delete mWindow;
+
+ EVAF_INFO("%s destroyed", qPrintable(objectName()));
+}
+
+bool SdiWindowPlugin::init(const QString & args)
+{
+ if (!mWindow->init(args))
+ return false;
+
+ EVAF_INFO("%s initialized", qPrintable(objectName()));
+
+ return true;
+}
+
+void SdiWindowPlugin::done()
+{
+ mWindow->done();
+
+ EVAF_INFO("%s finalized", qPrintable(objectName()));
+}