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 "rs_eventhandler.h"
21 #include "exitdialog.h"
22 #include "qg_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.
35 MDIWindow::MDIWindow(RS_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 is most likely coming from the QMdiSubWindow() constructor above...
41 #warning "QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible"
47 //not using this anymore
51 if (document->getLayerList())
52 // Link the graphic view to the layer widget
53 document->getLayerList()->addListener(graphicView);
55 if (document->getBlockList())
56 // Link the graphic view to the block widget
57 document->getBlockList()->addListener(graphicView);
62 setFocus(/*Qt::StrongFocus*/);
68 * Deletes the document associated with this window.
70 MDIWindow::~MDIWindow()
73 if (document->getLayerList())
74 document->getLayerList()->removeListener(graphicView);
76 if (document->getBlockList())
77 document->getBlockList()->removeListener(graphicView);
80 if (owner && document)
87 * Adds another MDI window to the list of known windows that
88 * depend on this one. This can be another view or a view for
91 void MDIWindow::addChildWindow(MDIWindow * w)
93 RS_DEBUG->print("RS_MDIWindow::addChildWindow()");
95 childWindows.append(w);
96 w->setParentWindow(this);
98 RS_DEBUG->print("children: %d", childWindows.count());
102 * Removes a child window.
104 * @see addChildWindow
106 void MDIWindow::removeChildWindow(MDIWindow * w)
108 RS_DEBUG->print("RS_MDIWindow::removeChildWindow()");
110 // bool suc = childWindows.remove(w);
111 bool suc = childWindows.removeOne(w);
112 RS_DEBUG->print("successfully removed child window: %d", (int)suc);
114 RS_DEBUG->print("children: %d", childWindows.count());
118 * @return pointer to the print preview of this drawing or NULL.
120 MDIWindow * MDIWindow::getPrintPreview()
122 for(uint i=0; i<childWindows.count(); ++i)
124 if (childWindows.at(i)->getGraphicView()->isPrintPreview())
125 return childWindows.at(i);
132 * closes this MDI window.
134 * @param force Disable cancel button (demo versions)
135 * @param ask Ask user before closing.
137 bool MDIWindow::closeMDI(bool force, bool ask)
139 // should never happen:
144 bool isBlock = (parentWindow != NULL);
146 // This is a block and we don't need to ask the user for closing
147 // since it's still available in the parent drawing after closing.
150 RS_DEBUG->print(" closing block");
151 // tell parent window we're not here anymore.
154 RS_DEBUG->print(" notifying parent about closing this window");
155 parentWindow->removeChildWindow(this);
158 emit(signalClosing());
161 // This is a graphic document. ask user for closing.
162 else if (!ask || slotFileClose(force))
164 RS_DEBUG->print(" closing graphic");
165 // close all child windows:
168 while (!childWindows.isEmpty())
170 MDIWindow * child = childWindows.takeFirst();
176 emit(signalClosing());
179 // User decided not to close graphic document:
185 return (ret || force);
189 * Called by Qt when the user closes this MDI window.
191 void MDIWindow::closeEvent(QCloseEvent * ce)
193 RS_DEBUG->print("MDIWindow::closeEvent begin");
195 if (closeMDI(false, !forceClosing))
200 RS_DEBUG->print("MDIWindow::closeEvent end");
206 * @param type Document type. RS:EntityGraphic or RS2::EntityBlock
207 * @param container Entity container to be used as document or NULL
208 * if a new document should be created.
210 void MDIWindow::initDoc(RS_Document * doc)
212 RS_DEBUG->print("MDIWindow::initDoc()");
216 document = new Drawing();
230 void MDIWindow::initView()
232 RS_DEBUG->print("MDIWindow::initView()");
234 // graphicView = new QC_GraphicView(document, this);
235 graphicView = new QG_GraphicView(document, this);
236 setWidget(graphicView);
237 graphicView->setFocus();
241 * Called when the current pen (color, style, width) has changed.
242 * Sets the active pen for the document in this MDI window.
244 void MDIWindow::slotPenChanged(RS_Pen pen)
246 RS_DEBUG->print("MDIWindow::slotPenChanged() begin");
249 document->setActivePen(pen);
251 RS_DEBUG->print("MDIWindow::slotPenChanged() end");
255 * Creates a new empty document in this MDI window.
257 void MDIWindow::slotFileNew()
259 RS_DEBUG->print("MDIWindow::slotFileNew begin");
261 if (document && graphicView)
264 graphicView->redraw();
267 RS_DEBUG->print("MDIWindow::slotFileNew end");
271 * Opens the given file in this MDI window.
273 bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
275 RS_DEBUG->print("MDIWindow::slotFileOpen");
278 if (document != NULL && !fileName.isEmpty())
281 ret = document->open(fileName, type);
285 //QString message=tr("Loaded document: ")+fileName;
286 //statusBar()->message(message, 2000);
288 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
289 graphicView->zoomAuto(false);
290 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
294 RS_DEBUG->print("MDIWindow::slotFileOpen: failed");
299 RS_DEBUG->print("MDIWindow::slotFileOpen: cancelled");
300 //statusBar()->message(tr("Opening aborted"), 2000);
303 RS_DEBUG->print("MDIWindow::slotFileOpen: OK");
309 * Saves the current file.
311 * @return true if the file was saved successfully.
312 * false if the file could not be saved or the document
315 bool MDIWindow::slotFileSave(bool & cancelled)
317 RS_DEBUG->print("MDIWindow::slotFileSave()");
323 if (document->getFilename().isEmpty())
324 ret = slotFileSaveAs(cancelled);
327 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
328 ret = document->save();
329 QApplication::restoreOverrideCursor();
337 * Saves the current file. The user is asked for a new filename
340 * @return true if the file was saved successfully or the user cancelled.
341 * false if the file could not be saved or the document
344 bool MDIWindow::slotFileSaveAs(bool & cancelled)
346 RS_DEBUG->print("MDIWindow::slotFileSaveAs");
349 RS2::FormatType t = RS2::FormatDXF;
351 QString fn = QG_FileDialog::getSaveFileName(this, &t);
353 if (document != NULL && !fn.isEmpty())
355 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
356 ret = document->saveAs(fn, t);
357 QApplication::restoreOverrideCursor();
361 // cancel is not an error - returns true
370 * Requests the closing of this MDI window.
372 * @param force Force closing by disabling the cancel button (for demo versions).
374 bool MDIWindow::slotFileClose(bool force)
376 RS_DEBUG->print("MDIWindow::slotFileClose()");
381 if (document && document->isModified())
383 ExitDialog dlg(this);
386 if (document->getFilename().isEmpty())
387 dlg.setText(tr("Do you really want to close the drawing?"));
390 QString fn = document->getFilename();
392 if (fn.length() > 50)
393 fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
395 dlg.setText(tr("Do you really want to close the file\n%1?").arg(fn));
398 dlg.setTitle(tr("Closing Drawing"));
417 succ = slotFileSave(cancelled);
418 again = !succ || cancelled;
421 succ = slotFileSaveAs(cancelled);
422 again = !succ || cancelled;
438 void MDIWindow::slotFilePrint()
440 RS_DEBUG->print("MDIWindow::slotFilePrint");
442 //statusBar()->message(tr("Printing..."));
445 // if (printer.setup(this))
446 QPrintDialog dialog(&printer, this);
451 painter.begin(&printer);
453 ///////////////////////////////////////////////////////////////////
454 // TODO: Define printing by using the QPainter methods here
459 //statusBar()->message(tr("Ready."));
462 /** @return Pointer to graphic view */
463 //QC_GraphicView * MDIWindow::getGraphicView()
464 QG_GraphicView * MDIWindow::getGraphicView()
469 /** @return Pointer to document */
470 RS_Document * MDIWindow::getDocument()
475 /** @return Pointer to graphic or NULL */
476 Drawing * MDIWindow::getGraphic()
478 return document->getGraphic();
481 /** @return Pointer to current event handler */
482 RS_EventHandler * MDIWindow::getEventHandler()
485 return graphicView->getEventHandler();
491 * Sets the parent window that will be notified if this
493 void MDIWindow::setParentWindow(MDIWindow * p)
495 RS_DEBUG->print("setParentWindow");
500 * @return The MDI window id.
502 int MDIWindow::getId()
507 void MDIWindow::setForceClosing(bool on)
513 * Streams some info about an MDI window to stdout.
515 std::ostream & operator<<(std::ostream & os, MDIWindow & w)
517 os << "MDIWindow[" << w.getId() << "]:\n";
520 os << " parentWindow: " << w.parentWindow->getId() << "\n";
522 os << " parentWindow: NULL\n";
524 for(uint i=0; i<w.childWindows.count(); ++i)
525 os << " childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";