]> vaikene.ee Git - evaf/blobdiff - src/plugins/SdiWindow/sdiwindow.cpp
In the process of creating a more generic document interface manager (or window manag...
[evaf] / src / plugins / SdiWindow / sdiwindow.cpp
index 4be6efe4f333b9b075db4e6dbdd6e2f52a38ccf5..c0e3587711afa35e00cbab88a421d4c38b634e47 100644 (file)
 #include <Common/iLogger>
 #include <Common/iRegistry>
 #include <Common/iApp>
+#include <Common/iProp>
 
 #include <QtGui>
+#include <QXmlStreamReader>
 
 namespace eVaf {
 namespace SdiWindow {
@@ -40,79 +42,146 @@ using namespace eVaf;
 
 //-------------------------------------------------------------------
 
-using namespace eVaf::SdiWindow;
-
-iSdiWindow * iSdiWindow::instance()
+SdiWindow::iSdiWindow * SdiWindow::iSdiWindow::instance()
 {
-    return Internal::mSdiWindow;
+    return SdiWindow::Internal::mSdiWindow;
 }
 
 
 //-------------------------------------------------------------------
 
-using namespace eVaf::SdiWindow::Internal;
-
-MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags)
+SdiWindow::Internal::MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags)
     : QWidget(parent, flags)
+    , mReady(false)
+    , mTimerId(0)
 {
     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
+    // Apply the size specified in a) properties; or b) on the command line (overwrites stored geometry)
     setWindowSize();
 
     // Create the default layout
     mLayout = new QVBoxLayout;
     setLayout(mLayout);
 
+    mSdiWindow = this;
+
     EVAF_INFO("%s created", qPrintable(objectName()));
 }
 
-MainWindow::~MainWindow()
+SdiWindow::Internal::MainWindow::~MainWindow()
 {
+    mSdiWindow = 0;
+
     // Save geometry
     saveSettings();
 
     EVAF_INFO("%s destroyed", qPrintable(objectName()));
 }
 
-bool MainWindow::init()
+QString SdiWindow::Internal::MainWindow::getMainPanelName(QString const & args) const
+{
+    QXmlStreamReader xml(args);
+    while (!xml.atEnd()) {
+        xml.readNext();
+        if (xml.isStartElement() && xml.name() == "attributes") {
+            if (xml.attributes().hasAttribute("mainPanelName"))
+                return xml.attributes().value("mainPanelName").toString();
+        }
+    }
+    return QString();
+}
+
+bool SdiWindow::Internal::MainWindow::init(QString const & args)
 {
+    mMainPanelName = getMainPanelName(args);
+
+    Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
+
     setWindowTitle(Common::iApp::instance()->name());
 
+    // Start the garbage collector timer
+    mTimerId = startTimer(60 * 1000);
+
     show();
 
+    mReady = true;
+
     EVAF_INFO("%s initialized", qPrintable(objectName()));
 
     return true;
 }
 
-void MainWindow::done()
+void SdiWindow::Internal::MainWindow::done()
 {
+    mReady = false;
+
     close();
 
-    // Delete all the items added to the main window
-    while (mItemsAdded.count() > 0)
-        delete mItemsAdded.takeAt(0);
+    if (mTimerId) {
+        killTimer(mTimerId);
+        mTimerId = 0;
+    }
+
+    // Delete all the panels
+    for (int i = mPanels.size() - 1; i >= 0; --i) {
+        QWeakPointer<Gui::Panel> p = mPanels.at(i);
+        if (p)
+            delete p.data();
+    }
+    mPanels.clear();
+    mMinimizedPanels.clear();
+    mPanelNames.clear();
+    mMainPanel.clear();
+    mMainPanelName.clear();
 
     EVAF_INFO("%s finalized", qPrintable(objectName()));
 }
 
-void MainWindow::addWidget(QWidget * widget)
+void SdiWindow::Internal::MainWindow::addPanel(QString const & name, Gui::Panel * panel)
+{
+    mPanels.append(panel);
+    mPanelNames.insert(name, panel);
+
+    // If this is the predefined main panel, add it to this window
+    if (!mMainPanelName.isEmpty()) {
+        if (name == mMainPanelName) {
+            mMainPanel = panel;
+            mLayout->addWidget(panel);
+        }
+    }
+
+    // If the predefined main panel name is not set, use the first panel
+    else {
+        if (!mMainPanel) {
+            mMainPanel = panel;
+            mLayout->addWidget(panel);
+        }
+    }
+}
+
+Gui::Panel * SdiWindow::Internal::MainWindow::panel(QString const & name) const
 {
-    mLayout->addWidget(widget);
-    mItemsAdded.append(widget);
+    QHash<QString, QWeakPointer<Gui::Panel> >::const_iterator it = mPanelNames.constFind(name);
+    if (it != mPanelNames.constEnd())
+        return it.value().data();
+    return 0;
 }
 
-void MainWindow::addLayout(QLayout * layout)
+bool SdiWindow::Internal::MainWindow::showPanel(QString const & name)
 {
-    mLayout->addLayout(layout);
-    mItemsAdded.append(layout);
+    Gui::Panel * p = panel(name);
+    if (p) {
+        p->show();
+        return true;
+    }
+    return false;
 }
 
-void MainWindow::saveSettings()
+void SdiWindow::Internal::MainWindow::saveSettings()
 {
     static int ver[4] = {VER_FILE_VERSION};
     QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
@@ -121,7 +190,7 @@ void MainWindow::saveSettings()
     settings.setValue(QString("%1/geometry").arg(objectName()), saveGeometry());
 }
 
-void MainWindow::restoreSettings()
+void SdiWindow::Internal::MainWindow::restoreSettings()
 {
     static int ver[4] = {VER_FILE_VERSION};
     QSettings settings(VER_COMPANY_NAME_STR, Common::iApp::instance()->name());
@@ -139,11 +208,11 @@ void MainWindow::restoreSettings()
     restoreGeometry(settings.value(QString("%1/geometry").arg(objectName())).toByteArray());
 }
 
-void MainWindow::setWindowSize()
+void SdiWindow::Internal::MainWindow::setWindowSize()
 {
     // a) Get window size from properties
-    int w = 0;
-    int h = 0;
+    int w = Common::iProp::instance()->getValue("windowWidth", 0).toInt();
+    int h = Common::iProp::instance()->getValue("windowHeight", 0).toInt();
 
     // b) Use command line arguments
     QStringList args = QApplication::arguments();
@@ -174,77 +243,107 @@ void MainWindow::setWindowSize()
     }
 }
 
-
-//-------------------------------------------------------------------
-
-SdiWindowImpl::SdiWindowImpl()
-    : iSdiWindow()
-    , mReady(false)
+void SdiWindow::Internal::MainWindow::closeEvent(QCloseEvent * e)
 {
-    setObjectName(QString("%1.iSdiWindow").arg(VER_MODULE_NAME_STR));
-
-    mSdiWindow = this;
-
-    wWindow = new MainWindow;
-
-    Common::iRegistry::instance()->registerInterface("iSdiWindow", this);
+    // Try to close all the managed panels; ignore the event if one of the managed panels refuses to close
+    foreach (QWeakPointer<Gui::Panel> p, mPanels) {
+        if (p) {
+            if (!p.data()->close()) {
+                e->ignore();
+                return;
+            }
+        }
+    }
 
-    EVAF_INFO("%s created", qPrintable(objectName()));
+    QWidget::closeEvent(e);
 }
 
-SdiWindowImpl::~SdiWindowImpl()
+void SdiWindow::Internal::MainWindow::changeEvent(QEvent * e)
 {
-    delete wWindow;
+    if (e->type() == QEvent::WindowStateChange) {
+        QWindowStateChangeEvent * wse = static_cast<QWindowStateChangeEvent *>(e);
 
-    mSdiWindow = 0;
-
-    EVAF_INFO("%s destroyed", qPrintable(objectName()));
-}
+        if (windowState() == Qt::WindowNoState && wse->oldState() == Qt::WindowMinimized) {
 
-bool SdiWindowImpl::init(const QString & args)
-{
-    Q_UNUSED(args);
-
-    if (!wWindow->init())
-        return false;
+            // Restore all the managed panels that were previously minimized
+            foreach (QWeakPointer<Gui::Panel> p, mMinimizedPanels) {
+                if (p && p.data()->isVisible())
+                    p.data()->showNormal();
+            }
+            mMinimizedPanels.clear();
+        }
 
-    mReady = true;
+        else if (windowState() == Qt::WindowMinimized && wse->oldState() != Qt::WindowMinimized) {
 
-    EVAF_INFO("%s initialized", qPrintable(objectName()));
+            // Minimize all the managed panels that are not minimized yet
+            mMinimizedPanels.clear();
+            foreach (QWeakPointer<Gui::Panel> p, mPanels) {
+                if (!p)
+                    continue;
 
-    return true;
+                if (p.data()->windowState() != Qt::WindowMinimized && p.data()->isVisible()) {
+                    mMinimizedPanels.append(p);
+                    p.data()->showMinimized();
+                }
+            }
+        }
+    }
+    QWidget::changeEvent(e);
 }
 
-void SdiWindowImpl::done()
+void SdiWindow::Internal::MainWindow::timerEvent(QTimerEvent * e)
 {
-    mReady = false;
-
-    wWindow->done();
+    if (e->timerId() == mTimerId) {
+
+        // Remove panels that are deleted
+        {
+            QList<QWeakPointer<Gui::Panel> >::iterator it = mPanels.begin();
+            while (it != mPanels.end()) {
+                QWeakPointer<Gui::Panel> p = *it;
+                if (!p)
+                    it = mPanels.erase(it);
+                else
+                    ++it;
+            }
+        }
 
-    EVAF_INFO("%s finalized", qPrintable(objectName()));
+        // Do the same with panel names
+        {
+            QHash<QString, QWeakPointer<Gui::Panel> >::iterator it = mPanelNames.begin();
+            while (it != mPanelNames.end()) {
+                QWeakPointer<Gui::Panel> p = it.value();
+                if (!p)
+                    it = mPanelNames.erase(it);
+                else
+                    ++it;
+            }
+        }
+    }
+    else
+        QWidget::timerEvent(e);
 }
 
 
 //-------------------------------------------------------------------
 
-SdiWindowPlugin::SdiWindowPlugin()
+SdiWindow::Internal::SdiWindowPlugin::SdiWindowPlugin()
     : Plugins::iPlugin()
 {
     setObjectName(VER_MODULE_NAME_STR);
 
-    mWindow = new SdiWindowImpl;
+    mWindow = new MainWindow;
 
     EVAF_INFO("%s created", qPrintable(objectName()));
 }
 
-SdiWindowPlugin::~SdiWindowPlugin()
+SdiWindow::Internal::SdiWindowPlugin::~SdiWindowPlugin()
 {
     delete mWindow;
 
     EVAF_INFO("%s destroyed", qPrintable(objectName()));
 }
 
-bool SdiWindowPlugin::init(const QString & args)
+bool SdiWindow::Internal::SdiWindowPlugin::init(const QString & args)
 {
     if (!mWindow->init(args))
         return false;
@@ -254,7 +353,7 @@ bool SdiWindowPlugin::init(const QString & args)
     return true;
 }
 
-void SdiWindowPlugin::done()
+void SdiWindow::Internal::SdiWindowPlugin::done()
 {
     mWindow->done();