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::WDestructiveClose*/):
37 QMdiSubWindow(parent, Qt::SubWindow), owner(false)//, forceClosing(false)
38 #warning "!!! wflags is ignored !!!"
40 // This warning not longer occurs...!
41 //This warning is most likely coming from the QMdiSubWindow() constructor above...
42 //#warning "QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible"
48 //not using this anymore
52 if (document->getLayerList())
53 // Link the graphic view to the layer widget
54 document->getLayerList()->addListener(graphicView);
56 if (document->getBlockList())
57 // Link the graphic view to the block widget
58 document->getBlockList()->addListener(graphicView);
63 setFocus(/*Qt::StrongFocus*/);
69 * Deletes the document associated with this window.
71 MDIWindow::~MDIWindow()
74 if (document->getLayerList())
75 document->getLayerList()->removeListener(graphicView);
77 if (document->getBlockList())
78 document->getBlockList()->removeListener(graphicView);
81 if (owner && document)
88 * Adds another MDI window to the list of known windows that
89 * depend on this one. This can be another view or a view for
92 void MDIWindow::addChildWindow(MDIWindow * w)
94 DEBUG->print("RS_MDIWindow::addChildWindow()");
95 childWindows.append(w);
96 w->setParentWindow(this);
97 DEBUG->print("children: %d", childWindows.count());
101 * Removes a child window.
103 * @see addChildWindow
105 void MDIWindow::removeChildWindow(MDIWindow * w)
107 DEBUG->print("RS_MDIWindow::removeChildWindow()");
108 bool success = childWindows.removeOne(w);
109 DEBUG->print("successfully removed child window: %s.", (success ? "yes" : "NO"));
110 DEBUG->print("# of children: %d", childWindows.count());
114 * @return pointer to the print preview of this drawing or NULL.
116 MDIWindow * MDIWindow::getPrintPreview()
118 for(int i=0; i<childWindows.count(); i++)
120 if (childWindows.at(i)->getGraphicView()->isPrintPreview())
121 return childWindows.at(i);
128 * closes this MDI window.
130 * @param force Disable cancel button (demo versions)
131 * @param ask Ask user before closing.
133 bool MDIWindow::CloseMDI(void)
135 // This should never happen:
140 bool isBlock = (parentWindow != NULL);
142 // This is a block and we don't need to ask the user for closing
143 // since it's still available in the parent drawing after closing.
146 DEBUG->print(" closing block");
148 // tell parent window we're not here anymore.
151 DEBUG->print(" notifying parent about closing this window");
152 parentWindow->removeChildWindow(this);
155 emit(signalClosing());
158 // This is a drawing document. Ask user for closing.
159 else if (slotFileClose())
161 DEBUG->print(" closing graphic");
163 // Close all child windows:
164 while (!childWindows.isEmpty())
166 MDIWindow * child = childWindows.takeFirst();
172 emit(signalClosing());
180 * Called by Qt when the user closes this MDI window.
182 void MDIWindow::closeEvent(QCloseEvent * ce)
184 DEBUG->print("MDIWindow::closeEvent begin");
186 (CloseMDI() ? ce->accept() : ce->ignore());
188 DEBUG->print("MDIWindow::closeEvent end");
192 * Initialize the document.
194 void MDIWindow::initDoc(Document * doc)
196 DEBUG->print("MDIWindow::initDoc()");
200 document = new Drawing(); // Drawing is derived from Document
212 * Initialize the view.
214 void MDIWindow::initView()
216 // This is only called once: In the MDIWindow constructor.
217 DEBUG->print("MDIWindow::initView()");
219 // graphicView = new QC_GraphicView(document, this);
220 graphicView = new QG_GraphicView(document, this);
221 setWidget(graphicView);
222 graphicView->setFocus();
226 * Called when the current pen (color, style, width) has changed.
227 * Sets the active pen for the document in this MDI window.
229 void MDIWindow::slotPenChanged(Pen pen)
231 DEBUG->print("MDIWindow::slotPenChanged() begin");
234 document->setActivePen(pen);
236 DEBUG->print("MDIWindow::slotPenChanged() end");
240 * Creates a new empty document in this MDI window.
242 void MDIWindow::slotFileNew()
244 DEBUG->print("MDIWindow::slotFileNew begin");
246 if (document && graphicView)
249 graphicView->redraw();
252 DEBUG->print("MDIWindow::slotFileNew end");
256 * Opens the given file in this MDI window.
258 bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
260 DEBUG->print("MDIWindow::slotFileOpen");
263 if (document != NULL && !fileName.isEmpty())
266 ret = document->open(fileName, type);
270 //QString message=tr("Loaded document: ")+fileName;
271 //statusBar()->message(message, 2000);
273 DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
274 graphicView->zoomAuto(false);
275 DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
279 DEBUG->print("MDIWindow::slotFileOpen: failed");
284 DEBUG->print("MDIWindow::slotFileOpen: cancelled");
285 //statusBar()->message(tr("Opening aborted"), 2000);
288 DEBUG->print("MDIWindow::slotFileOpen: OK");
294 * Saves the current file.
296 * @return true if the file was saved successfully.
297 * false if the file could not be saved or the document
300 bool MDIWindow::slotFileSave(bool & cancelled)
302 DEBUG->print("MDIWindow::slotFileSave()");
308 if (document->getFilename().isEmpty())
309 ret = slotFileSaveAs(cancelled);
312 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
313 ret = document->save();
314 QApplication::restoreOverrideCursor();
322 * Saves the current file. The user is asked for a new filename
325 * @return true if the file was saved successfully or the user cancelled.
326 * false if the file could not be saved or the document
329 bool MDIWindow::slotFileSaveAs(bool & cancelled)
331 DEBUG->print("MDIWindow::slotFileSaveAs");
334 RS2::FormatType t = RS2::FormatDXF;
336 QString fn = FileDialog::getSaveFileName(this, &t);
338 if (document != NULL && !fn.isEmpty())
340 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
341 ret = document->saveAs(fn, t);
342 QApplication::restoreOverrideCursor();
346 // cancel is not an error - returns true
355 * Requests the closing of this MDI window.
357 bool MDIWindow::slotFileClose(void)
359 DEBUG->print("MDIWindow::slotFileClose()");
360 bool succeeded = true;
362 if (document == NULL || !document->isModified())
365 ExitDialog dlg(this);
367 if (document->getFilename().isEmpty())
368 dlg.setText(tr("Do you really want to discard this drawing?"));
371 QString fn = document->getFilename();
373 if (fn.length() > 50)
374 fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
376 dlg.setText(tr("Do you really want to discard changes to the file\n%1?").arg(fn));
379 dlg.setTitle(tr("Close Drawing"));
386 int exit = dlg.exec();
394 case 1: // OK (Leave)
399 succeeded = slotFileSave(cancelled);
400 again = !succeeded || cancelled;
403 succeeded = slotFileSaveAs(cancelled);
404 again = !succeeded || cancelled;
415 void MDIWindow::slotFilePrint()
417 DEBUG->print("MDIWindow::slotFilePrint");
419 //statusBar()->message(tr("Printing..."));
422 // if (printer.setup(this))
423 QPrintDialog dialog(&printer, this);
428 painter.begin(&printer);
430 ///////////////////////////////////////////////////////////////////
431 // TODO: Define printing by using the QPainter methods here
436 //statusBar()->message(tr("Ready."));
440 * @return Pointer to graphic view
442 //QC_GraphicView * MDIWindow::getGraphicView()
443 QG_GraphicView * MDIWindow::getGraphicView()
449 * @return Pointer to document
451 Document * MDIWindow::getDocument()
457 * @return Pointer to Drawing or NULL
459 Drawing * MDIWindow::GetDrawing()
461 return document->GetDrawing();
465 * @return Pointer to current event handler
467 EventHandler * MDIWindow::getEventHandler()
469 return (graphicView ? graphicView->getEventHandler() : NULL);
473 * Sets the parent window that will be notified if this
475 void MDIWindow::setParentWindow(MDIWindow * p)
477 DEBUG->print("setParentWindow");
482 * @return The MDI window id.
484 int MDIWindow::getId()
490 * Streams some info about an MDI window to stdout.
492 std::ostream & operator<<(std::ostream & os, MDIWindow & w)
494 os << "MDIWindow[" << w.getId() << "]:\n";
497 os << " parentWindow: " << w.parentWindow->getId() << "\n";
499 os << " parentWindow: NULL\n";
501 for(int i=0; i<w.childWindows.count(); i++)
502 os << " childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";