3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/17/2010 Added this text. :-)
17 #include "mdiwindow.h"
20 #include "eventhandler.h"
21 #include "exitdialog.h"
22 #include "filedialog.h"
23 #include "qg_graphicview.h"
26 int MDIWindow::idCounter = 0;
31 * @param doc Pointer to an existing document of NULL if a new
32 * document shall be created for this window.
33 * @param parent Parent widget. Usually a workspace (QMdiArea).
35 MDIWindow::MDIWindow(Document * doc, QWidget * parent, const char * name/*= NULL*/,
36 Qt::WindowFlags wflags/*= Qt::SubWindow*/):
37 QMdiSubWindow(parent, Qt::SubWindow),
39 #warning "!!! wflags is ignored !!!"
41 // This warning not longer occurs...!
42 //This warning is most likely coming from the QMdiSubWindow() constructor above...
43 //#warning "QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible"
46 graphicView = new QG_GraphicView(document, this);
47 //Let's see if the trouble is in the graphicView...
48 //And it is... So... How to fix this???
49 setWidget(graphicView);
50 //THIS was the problem... Somehow, setting focus on the this widget was screwing
51 //*everything* about the MDI system up!
52 // graphicView->setFocus();
57 //not using this anymore
61 if (document->getLayerList())
62 // Link the graphic view to the layer widget
63 document->getLayerList()->addListener(graphicView);
65 if (document->getBlockList())
66 // Link the graphic view to the block widget
67 document->getBlockList()->addListener(graphicView);
72 setFocus(/*Qt::StrongFocus*/);
78 * Deletes the document associated with this window.
80 MDIWindow::~MDIWindow()
83 if (document->getLayerList())
84 document->getLayerList()->removeListener(graphicView);
86 if (document->getBlockList())
87 document->getBlockList()->removeListener(graphicView);
90 if (owner && document)
97 * Adds another MDI window to the list of known windows that
98 * depend on this one. This can be another view or a view for
101 void MDIWindow::addChildWindow(MDIWindow * w)
103 DEBUG->print("RS_MDIWindow::addChildWindow()");
104 childWindows.append(w);
105 w->setParentWindow(this);
106 DEBUG->print("children: %d", childWindows.count());
110 * Removes a child window.
112 * @see addChildWindow
114 void MDIWindow::removeChildWindow(MDIWindow * w)
116 DEBUG->print("RS_MDIWindow::removeChildWindow()");
117 bool success = childWindows.removeOne(w);
118 DEBUG->print("successfully removed child window: %s.", (success ? "yes" : "NO"));
119 DEBUG->print("# of children: %d", childWindows.count());
123 * @return pointer to the print preview of this drawing or NULL.
125 MDIWindow * MDIWindow::getPrintPreview()
127 for(int i=0; i<childWindows.count(); i++)
129 if (childWindows.at(i)->getGraphicView()->isPrintPreview())
130 return childWindows.at(i);
137 * closes this MDI window.
139 * @param force Disable cancel button (demo versions)
140 * @param ask Ask user before closing.
142 bool MDIWindow::CloseMDI(void)
144 // This should never happen:
149 bool isBlock = (parentWindow != NULL);
151 // This is a block and we don't need to ask the user for closing
152 // since it's still available in the parent drawing after closing.
155 DEBUG->print(" closing block");
157 // tell parent window we're not here anymore.
160 DEBUG->print(" notifying parent about closing this window");
161 parentWindow->removeChildWindow(this);
164 emit(signalClosing());
167 // This is a drawing document. Ask user for closing.
168 else if (slotFileClose())
170 DEBUG->print(" closing graphic");
172 // Close all child windows:
173 while (!childWindows.isEmpty())
175 MDIWindow * child = childWindows.takeFirst();
181 emit(signalClosing());
189 * Called by Qt when the user closes this MDI window.
191 void MDIWindow::closeEvent(QCloseEvent * ce)
193 DEBUG->print("MDIWindow::closeEvent begin");
195 (CloseMDI() ? ce->accept() : ce->ignore());
197 DEBUG->print("MDIWindow::closeEvent end");
201 * Initialize the document.
203 void MDIWindow::initDoc(Document * doc)
205 DEBUG->print("MDIWindow::initDoc()");
209 document = new Drawing(); // Drawing is derived from Document
221 * Called when the current pen (color, style, width) has changed.
222 * Sets the active pen for the document in this MDI window.
224 void MDIWindow::slotPenChanged(Pen pen)
226 DEBUG->print("MDIWindow::slotPenChanged() begin");
229 document->setActivePen(pen);
231 DEBUG->print("MDIWindow::slotPenChanged() end");
235 * Creates a new empty document in this MDI window.
237 void MDIWindow::slotFileNew()
239 DEBUG->print("MDIWindow::slotFileNew begin");
241 if (document && graphicView)
244 graphicView->redraw();
247 DEBUG->print("MDIWindow::slotFileNew end");
251 * Opens the given file in this MDI window.
253 bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
255 DEBUG->print("MDIWindow::slotFileOpen");
258 if (document != NULL && !fileName.isEmpty())
261 ret = document->open(fileName, type);
265 //QString message=tr("Loaded document: ")+fileName;
266 //statusBar()->message(message, 2000);
268 DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
269 graphicView->zoomAuto(false);
270 DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
274 DEBUG->print("MDIWindow::slotFileOpen: failed");
279 DEBUG->print("MDIWindow::slotFileOpen: cancelled");
280 //statusBar()->message(tr("Opening aborted"), 2000);
283 DEBUG->print("MDIWindow::slotFileOpen: OK");
289 * Saves the current file.
291 * @return true if the file was saved successfully.
292 * false if the file could not be saved or the document
295 bool MDIWindow::slotFileSave(bool & cancelled)
297 DEBUG->print("MDIWindow::slotFileSave()");
303 if (document->getFilename().isEmpty())
304 ret = slotFileSaveAs(cancelled);
307 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
308 ret = document->save();
309 QApplication::restoreOverrideCursor();
317 * Saves the current file. The user is asked for a new filename
320 * @return true if the file was saved successfully or the user cancelled.
321 * false if the file could not be saved or the document
324 bool MDIWindow::slotFileSaveAs(bool & cancelled)
326 DEBUG->print("MDIWindow::slotFileSaveAs");
329 RS2::FormatType t = RS2::FormatDXF;
331 QString fn = FileDialog::getSaveFileName(this, &t);
333 if (document != NULL && !fn.isEmpty())
335 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
336 ret = document->saveAs(fn, t);
337 QApplication::restoreOverrideCursor();
341 // cancel is not an error - returns true
350 * Requests the closing of this MDI window.
352 bool MDIWindow::slotFileClose(void)
354 DEBUG->print("MDIWindow::slotFileClose()");
355 bool succeeded = true;
357 if (document == NULL || !document->isModified())
360 ExitDialog dlg(this);
362 if (document->getFilename().isEmpty())
363 dlg.setText(tr("Do you really want to discard this drawing?"));
366 QString fn = document->getFilename();
368 if (fn.length() > 50)
369 fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
371 dlg.setText(tr("Do you really want to discard changes to the file\n%1?").arg(fn));
374 dlg.setTitle(tr("Close Drawing"));
381 int exit = dlg.exec();
389 case 1: // OK (Leave)
394 succeeded = slotFileSave(cancelled);
395 again = !succeeded || cancelled;
398 succeeded = slotFileSaveAs(cancelled);
399 again = !succeeded || cancelled;
410 void MDIWindow::slotFilePrint()
412 DEBUG->print("MDIWindow::slotFilePrint");
414 //statusBar()->message(tr("Printing..."));
417 // if (printer.setup(this))
418 QPrintDialog dialog(&printer, this);
423 painter.begin(&printer);
425 ///////////////////////////////////////////////////////////////////
426 // TODO: Define printing by using the QPainter methods here
431 //statusBar()->message(tr("Ready."));
435 * @return Pointer to graphic view
437 //QC_GraphicView * MDIWindow::getGraphicView()
438 QG_GraphicView * MDIWindow::getGraphicView()
444 * @return Pointer to document
446 Document * MDIWindow::getDocument()
452 * @return Pointer to Drawing or NULL
454 Drawing * MDIWindow::GetDrawing()
456 return document->GetDrawing();
460 * @return Pointer to current event handler
462 EventHandler * MDIWindow::getEventHandler()
464 return (graphicView ? graphicView->getEventHandler() : NULL);
468 * Sets the parent window that will be notified if this
470 void MDIWindow::setParentWindow(MDIWindow * p)
472 DEBUG->print("setParentWindow");
477 * @return The MDI window id.
479 int MDIWindow::getId()
485 * Streams some info about an MDI window to stdout.
487 std::ostream & operator<<(std::ostream & os, MDIWindow & w)
489 os << "MDIWindow[" << w.getId() << "]:\n";
492 os << " parentWindow: " << w.parentWindow->getId() << "\n";
494 os << " parentWindow: NULL\n";
496 for(int i=0; i<w.childWindows.count(); i++)
497 os << " childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";