]> Shamusworld >> Repos - architektonas/blobdiff - src/mainapp/mdiwindow.cpp
Fixed problem with MDI activation.
[architektonas] / src / mainapp / mdiwindow.cpp
index d4f4e1aeddfe6ebfe1419c250dcdc64a6ed34bde..a592c4bd6bc1f1d9ab335646b3e57c841174f2c2 100644 (file)
@@ -3,7 +3,9 @@
 // Part of the Architektonas Project
 // Originally part of QCad Community Edition by Andrew Mustun
 // Extensively rewritten and refactored by James L. Hammons
-// (C) 2010 Underground Software
+// Portions copyright (C) 2001-2003 RibbonSoft
+// Copyright (C) 2010 Underground Software
+// See the README and GPLv2 files for licensing and warranty information
 //
 // JLH = James L. Hammons <jlhamm@acm.org>
 //
 #include "mdiwindow.h"
 
 #include "drawing.h"
-#include "rs_eventhandler.h"
+#include "eventhandler.h"
 #include "exitdialog.h"
-#include "qg_filedialog.h"
+#include "filedialog.h"
+#include "qg_graphicview.h"
 
 // Class variable
 int MDIWindow::idCounter = 0;
@@ -27,34 +30,43 @@ int MDIWindow::idCounter = 0;
  *
  * @param doc Pointer to an existing document of NULL if a new
  *   document shall be created for this window.
- * @param parent Parent widget. Usually a workspace.
+ * @param parent Parent widget. Usually a workspace (QMdiArea).
  */
-MDIWindow::MDIWindow(RS_Document * doc, QWidget * parent, const char * name/*= NULL*/,
-       Qt::WindowFlags wflags/*= Qt::WDestructiveClose*/):
-       QMdiSubWindow(parent, Qt::SubWindow)
+MDIWindow::MDIWindow(Document * doc, QWidget * parent, const char * name/*= NULL*/,
+       Qt::WindowFlags wflags/*= Qt::SubWindow*/):
+       QMdiSubWindow(parent, Qt::SubWindow), 
+owner(false)
 #warning "!!! wflags is ignored !!!"
 {
-       owner = false;
-       forceClosing = false;
+// This warning not longer occurs...!
 //This warning is most likely coming from the QMdiSubWindow() constructor above...
-#warning "QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible"
+//#warning "QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible"
        initDoc(doc);
-       initView();
+
+       graphicView = new QG_GraphicView(document, this);
+//Let's see if the trouble is in the graphicView...
+//And it is... So... How to fix this???
+       setWidget(graphicView);
+//THIS was the problem... Somehow, setting focus on the this widget was screwing
+//*everything* about the MDI system up!
+//     graphicView->setFocus();
+
        id = idCounter++;
-//No worries: Only need to be more vigilant if this were TRUE
-//     childWindows.setAutoDelete(false);
        parentWindow = NULL;
 
-       if (document != NULL)
+//not using this anymore
+#if 0
+       if (document)
        {
-               if (document->getLayerList() != NULL)
+               if (document->getLayerList())
                        // Link the graphic view to the layer widget
                        document->getLayerList()->addListener(graphicView);
 
-               if (document->getBlockList() != NULL)
+               if (document->getBlockList())
                        // Link the graphic view to the block widget
                        document->getBlockList()->addListener(graphicView);
        }
+#endif
 
 //hm.
        setFocus(/*Qt::StrongFocus*/);
@@ -67,13 +79,15 @@ MDIWindow::MDIWindow(RS_Document * doc, QWidget * parent, const char * name/*= N
  */
 MDIWindow::~MDIWindow()
 {
-       if (document->getLayerList() != NULL)
+#if 0
+       if (document->getLayerList())
                document->getLayerList()->removeListener(graphicView);
 
-       if (document->getBlockList() != NULL)
+       if (document->getBlockList())
                document->getBlockList()->removeListener(graphicView);
+#endif
 
-       if (owner == true && document != NULL)
+       if (owner && document)
                delete document;
 
        document = NULL;
@@ -86,12 +100,10 @@ MDIWindow::~MDIWindow()
  */
 void MDIWindow::addChildWindow(MDIWindow * w)
 {
-    RS_DEBUG->print("RS_MDIWindow::addChildWindow()");
-
-    childWindows.append(w);
-    w->setParentWindow(this);
-
-    RS_DEBUG->print("children: %d", childWindows.count());
+       DEBUG->print("RS_MDIWindow::addChildWindow()");
+       childWindows.append(w);
+       w->setParentWindow(this);
+       DEBUG->print("children: %d", childWindows.count());
 }
 
 /**
@@ -101,13 +113,10 @@ void MDIWindow::addChildWindow(MDIWindow * w)
  */
 void MDIWindow::removeChildWindow(MDIWindow * w)
 {
-    RS_DEBUG->print("RS_MDIWindow::removeChildWindow()");
-
-//    bool suc = childWindows.remove(w);
-    bool suc = childWindows.removeOne(w);
-    RS_DEBUG->print("successfully removed child window: %d", (int)suc);
-
-    RS_DEBUG->print("children: %d", childWindows.count());
+       DEBUG->print("RS_MDIWindow::removeChildWindow()");
+       bool success = childWindows.removeOne(w);
+       DEBUG->print("successfully removed child window: %s.", (success ? "yes" : "NO"));
+       DEBUG->print("# of children: %d", childWindows.count());
 }
 
 /**
@@ -115,11 +124,11 @@ void MDIWindow::removeChildWindow(MDIWindow * w)
  */
 MDIWindow * MDIWindow::getPrintPreview()
 {
-    for(uint i=0; i<childWindows.count(); ++i)
+       for(int i=0; i<childWindows.count(); i++)
        {
-        if (childWindows.at(i)->getGraphicView()->isPrintPreview())
+               if (childWindows.at(i)->getGraphicView()->isPrintPreview())
                        return childWindows.at(i);
-    }
+       }
 
        return NULL;
 }
@@ -130,50 +139,37 @@ MDIWindow * MDIWindow::getPrintPreview()
  * @param force Disable cancel button (demo versions)
  * @param ask Ask user before closing.
  */
-bool MDIWindow::closeMDI(bool force, bool ask)
+bool MDIWindow::CloseMDI(void)
 {
-       // should never happen:
-       if (document == NULL)
+       // This should never happen:
+       if (!document)
                return true;
 
-       bool ret = false;
+       bool closed = false;
        bool isBlock = (parentWindow != NULL);
 
        // This is a block and we don't need to ask the user for closing
-       //   since it's still available in the parent drawing after closing.
+       // since it's still available in the parent drawing after closing.
        if (isBlock)
        {
-               RS_DEBUG->print("  closing block");
+               DEBUG->print("  closing block");
+
                // tell parent window we're not here anymore.
-               if (parentWindow != NULL)
+               if (parentWindow)
                {
-                       RS_DEBUG->print("    notifying parent about closing this window");
+                       DEBUG->print("    notifying parent about closing this window");
                        parentWindow->removeChildWindow(this);
                }
 
                emit(signalClosing());
-               ret = true;
+               closed = true;
        }
-       // This is a graphic document. ask user for closing.
-       else if (!ask || slotFileClose(force))
+       // This is a drawing document. Ask user for closing.
+       else if (slotFileClose())
        {
-               RS_DEBUG->print("  closing graphic");
-               // close all child windows:
-               bool done;
-
-#if 0  // UGH! WHY??!??!
-               do
-               {
-                       done = true;
+               DEBUG->print("  closing graphic");
 
-                       if (childWindows.at(0) != NULL)
-                       {
-                               childWindows.at(0)->close();
-                               done = false;
-                       }
-               }
-               while (!done);
-#else
+               // Close all child windows:
                while (!childWindows.isEmpty())
                {
                        MDIWindow * child = childWindows.takeFirst();
@@ -181,18 +177,12 @@ bool MDIWindow::closeMDI(bool force, bool ask)
                        if (child)
                                child->close();
                }
-#endif
 
                emit(signalClosing());
-               ret = true;
-       }
-       // User decided not to close graphic document:
-       else
-       {
-               ret = false;
+               closed = true;
        }
 
-       return (ret || force);
+       return closed;
 }
 
 /**
@@ -200,30 +190,23 @@ bool MDIWindow::closeMDI(bool force, bool ask)
  */
 void MDIWindow::closeEvent(QCloseEvent * ce)
 {
-    RS_DEBUG->print("MDIWindow::closeEvent begin");
+       DEBUG->print("MDIWindow::closeEvent begin");
 
-    if (closeMDI(false, !forceClosing))
-        ce->accept();
-    else
-        ce->ignore();
+       (CloseMDI() ? ce->accept() : ce->ignore());
 
-    RS_DEBUG->print("MDIWindow::closeEvent end");
+       DEBUG->print("MDIWindow::closeEvent end");
 }
 
 /**
- * Init the document.
- *
- * @param type Document type. RS:EntityGraphic or RS2::EntityBlock
- * @param container Entity container to be used as document or NULL
- * if a new document should be created.
+ * Initialize the document.
  */
-void MDIWindow::initDoc(RS_Document * doc)
+void MDIWindow::initDoc(Document * doc)
 {
-       RS_DEBUG->print("MDIWindow::initDoc()");
+       DEBUG->print("MDIWindow::initDoc()");
 
-       if (doc == NULL)
+       if (!doc)
        {
-               document = new Drawing();
+               document = new Drawing();                               // Drawing is derived from Document
                document->newDoc();
                owner = true;
        }
@@ -234,31 +217,18 @@ void MDIWindow::initDoc(RS_Document * doc)
        }
 }
 
-/**
- * Init the view.
- */
-void MDIWindow::initView()
-{
-       RS_DEBUG->print("MDIWindow::initView()");
-
-       graphicView = new QC_GraphicView(document, this);
-//     setCentralWidget(graphicView);
-       setWidget(graphicView);
-       graphicView->setFocus();
-}
-
 /**
  * Called when the current pen (color, style, width) has changed.
  * Sets the active pen for the document in this MDI window.
  */
-void MDIWindow::slotPenChanged(RS_Pen pen)
+void MDIWindow::slotPenChanged(Pen pen)
 {
-       RS_DEBUG->print("MDIWindow::slotPenChanged() begin");
+       DEBUG->print("MDIWindow::slotPenChanged() begin");
 
-       if (document != NULL)
+       if (document)
                document->setActivePen(pen);
 
-       RS_DEBUG->print("MDIWindow::slotPenChanged() end");
+       DEBUG->print("MDIWindow::slotPenChanged() end");
 }
 
 /**
@@ -266,15 +236,15 @@ void MDIWindow::slotPenChanged(RS_Pen pen)
  */
 void MDIWindow::slotFileNew()
 {
-       RS_DEBUG->print("MDIWindow::slotFileNew begin");
+       DEBUG->print("MDIWindow::slotFileNew begin");
 
-       if (document != NULL && graphicView != NULL)
+       if (document && graphicView)
        {
                document->newDoc();
                graphicView->redraw();
        }
 
-       RS_DEBUG->print("MDIWindow::slotFileNew end");
+       DEBUG->print("MDIWindow::slotFileNew end");
 }
 
 /**
@@ -282,16 +252,12 @@ void MDIWindow::slotFileNew()
  */
 bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
 {
-       RS_DEBUG->print("MDIWindow::slotFileOpen");
+       DEBUG->print("MDIWindow::slotFileOpen");
        bool ret = false;
 
        if (document != NULL && !fileName.isEmpty())
        {
                document->newDoc();
-
-               // cosmetics..
-//bah          RS_APP->processEvents(QEventLoop::AllEvents, 1000);
-
                ret = document->open(fileName, type);
 
                if (ret)
@@ -299,22 +265,22 @@ bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
                        //QString message=tr("Loaded document: ")+fileName;
                        //statusBar()->message(message, 2000);
 
-                       RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
+                       DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
                        graphicView->zoomAuto(false);
-                       RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
+                       DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
                }
                else
                {
-                       RS_DEBUG->print("MDIWindow::slotFileOpen: failed");
+                       DEBUG->print("MDIWindow::slotFileOpen: failed");
                }
        }
        else
        {
-               RS_DEBUG->print("MDIWindow::slotFileOpen: cancelled");
+               DEBUG->print("MDIWindow::slotFileOpen: cancelled");
                //statusBar()->message(tr("Opening aborted"), 2000);
        }
 
-       RS_DEBUG->print("MDIWindow::slotFileOpen: OK");
+       DEBUG->print("MDIWindow::slotFileOpen: OK");
 
        return ret;
 }
@@ -328,11 +294,11 @@ bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
  */
 bool MDIWindow::slotFileSave(bool & cancelled)
 {
-       RS_DEBUG->print("MDIWindow::slotFileSave()");
+       DEBUG->print("MDIWindow::slotFileSave()");
        bool ret = false;
        cancelled = false;
 
-       if (document != NULL)
+       if (document)
        {
                if (document->getFilename().isEmpty())
                        ret = slotFileSaveAs(cancelled);
@@ -357,12 +323,12 @@ bool MDIWindow::slotFileSave(bool & cancelled)
  */
 bool MDIWindow::slotFileSaveAs(bool & cancelled)
 {
-       RS_DEBUG->print("MDIWindow::slotFileSaveAs");
+       DEBUG->print("MDIWindow::slotFileSaveAs");
        bool ret = false;
        cancelled = false;
        RS2::FormatType t = RS2::FormatDXF;
 
-       QString fn = QG_FileDialog::getSaveFileName(this, &t);
+       QString fn = FileDialog::getSaveFileName(this, &t);
 
        if (document != NULL && !fn.isEmpty())
        {
@@ -382,76 +348,68 @@ bool MDIWindow::slotFileSaveAs(bool & cancelled)
 
 /**
  * Requests the closing of this MDI window.
- *
- * @param force Force closing by disabling the cancel button (for demo versions).
  */
-bool MDIWindow::slotFileClose(bool force)
+bool MDIWindow::slotFileClose(void)
 {
-       RS_DEBUG->print("MDIWindow::slotFileClose()");
+       DEBUG->print("MDIWindow::slotFileClose()");
+       bool succeeded = true;
+
+       if (document == NULL || !document->isModified())
+               return succeeded;
 
-       bool succ = true;
-       int exit = 0;
+       ExitDialog dlg(this);
 
-       if (document != NULL && document->isModified())
+       if (document->getFilename().isEmpty())
+               dlg.setText(tr("Do you really want to discard this drawing?"));
+       else
        {
-               ExitDialog dlg(this);
-               dlg.setForce(force);
+               QString fn = document->getFilename();
 
-               if (document->getFilename().isEmpty())
-                       dlg.setText(tr("Do you really want to close the drawing?"));
-               else
-               {
-                       QString fn = document->getFilename();
+               if (fn.length() > 50)
+                       fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
 
-                       if (fn.length() > 50)
-                               fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
+               dlg.setText(tr("Do you really want to discard changes to the file\n%1?").arg(fn));
+       }
 
-                       dlg.setText(tr("Do you really want to close the file\n%1?").arg(fn));
-               }
+       dlg.setTitle(tr("Close Drawing"));
 
-               dlg.setTitle(tr("Closing Drawing"));
+       bool again = false;
 
-               bool again;
+       do
+       {
                bool cancelled;
+               int exit = dlg.exec();
 
-               do
+               switch (exit)
                {
+               case 0:                                                                 // Cancel
+                       succeeded = false;
                        again = false;
-                       exit = dlg.exec();
-
-                       switch (exit)
-                       {
-                       case 0: // cancel
-                               succ = false;
-                               break;
-                       case 1: // leave
-                               succ = true;
-                               break;
-                       case 2: // save
-                               succ = slotFileSave(cancelled);
-                               again = !succ || cancelled;
-                               break;
-                       case 3: // save as
-                               succ = slotFileSaveAs(cancelled);
-                               again = !succ || cancelled;
-                               break;
-                       default:
-                               break;
-                       }
+                       break;
+               case 1:                                                                 // OK (Leave)
+                       succeeded = true;
+                       again = false;
+                       break;
+               case 2:                                                                 // Save
+                       succeeded = slotFileSave(cancelled);
+                       again = !succeeded || cancelled;
+                       break;
+               case 3:                                                                 // Save As
+                       succeeded = slotFileSaveAs(cancelled);
+                       again = !succeeded || cancelled;
+                       break;
+               default:
+                       break;
                }
-               while (again);
-       }
-       else
-       {
-               succ = true;
        }
+       while (again);
 
-       return succ;
+       return succeeded;
 }
 
 void MDIWindow::slotFilePrint()
 {
-       RS_DEBUG->print("MDIWindow::slotFilePrint");
+       DEBUG->print("MDIWindow::slotFilePrint");
 
        //statusBar()->message(tr("Printing..."));
        QPrinter printer;
@@ -468,36 +426,42 @@ void MDIWindow::slotFilePrint()
                // TODO: Define printing by using the QPainter methods here
 
                painter.end();
-       };
+       }
 
        //statusBar()->message(tr("Ready."));
 }
 
-/** @return Pointer to graphic view */
-QC_GraphicView * MDIWindow::getGraphicView()
+/**
+ * @return Pointer to graphic view
+ */
+//QC_GraphicView * MDIWindow::getGraphicView()
+QG_GraphicView * MDIWindow::getGraphicView()
 {
        return graphicView;
 }
 
-/** @return Pointer to document */
-RS_Document * MDIWindow::getDocument()
+/**
+ * @return Pointer to document
+ */
+Document * MDIWindow::getDocument()
 {
        return document;
 }
 
-/** @return Pointer to graphic or NULL */
-Drawing * MDIWindow::getGraphic()
+/**
+ * @return Pointer to Drawing or NULL
+ */
+Drawing * MDIWindow::GetDrawing()
 {
-       return document->getGraphic();
+       return document->GetDrawing();
 }
 
-/** @return Pointer to current event handler */
-RS_EventHandler * MDIWindow::getEventHandler()
+/**
+ * @return Pointer to current event handler
+ */
+EventHandler * MDIWindow::getEventHandler()
 {
-       if (graphicView != NULL)
-               return graphicView->getEventHandler();
-       else
-               return NULL;
+       return (graphicView ? graphicView->getEventHandler() : NULL);
 }
 
 /**
@@ -505,7 +469,7 @@ RS_EventHandler * MDIWindow::getEventHandler()
  */
 void MDIWindow::setParentWindow(MDIWindow * p)
 {
-       RS_DEBUG->print("setParentWindow");
+       DEBUG->print("setParentWindow");
        parentWindow = p;
 }
 
@@ -517,11 +481,6 @@ int MDIWindow::getId()
        return id;
 }
 
-void MDIWindow::setForceClosing(bool on)
-{
-       forceClosing = on;
-}
-
 /**
  * Streams some info about an MDI window to stdout.
  */
@@ -529,12 +488,12 @@ std::ostream & operator<<(std::ostream & os, MDIWindow & w)
 {
        os << "MDIWindow[" << w.getId() << "]:\n";
 
-       if (w.parentWindow!=NULL)
+       if (w.parentWindow)
                os << "  parentWindow: " << w.parentWindow->getId() << "\n";
        else
                os << "  parentWindow: NULL\n";
 
-       for(uint i=0; i<w.childWindows.count(); ++i)
+       for(int i=0; i<w.childWindows.count(); i++)
                os << "  childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";
 
        return os;