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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/17/2010 Added this text. :-)
15 #include "mdiwindow.h"
18 #include "rs_eventhandler.h"
19 #include "exitdialog.h"
20 #include "qg_filedialog.h"
21 #include "qg_graphicview.h"
24 int MDIWindow::idCounter = 0;
29 * @param doc Pointer to an existing document of NULL if a new
30 * document shall be created for this window.
31 * @param parent Parent widget. Usually a workspace.
33 MDIWindow::MDIWindow(RS_Document * doc, QWidget * parent, const char * name/*= NULL*/,
34 Qt::WindowFlags wflags/*= Qt::WDestructiveClose*/):
35 QMdiSubWindow(parent, Qt::SubWindow), owner(false), forceClosing(false)
36 #warning "!!! wflags is ignored !!!"
38 //This warning is most likely coming from the QMdiSubWindow() constructor above...
39 #warning "QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible"
45 //not using this anymore
49 if (document->getLayerList())
50 // Link the graphic view to the layer widget
51 document->getLayerList()->addListener(graphicView);
53 if (document->getBlockList())
54 // Link the graphic view to the block widget
55 document->getBlockList()->addListener(graphicView);
60 setFocus(/*Qt::StrongFocus*/);
66 * Deletes the document associated with this window.
68 MDIWindow::~MDIWindow()
71 if (document->getLayerList())
72 document->getLayerList()->removeListener(graphicView);
74 if (document->getBlockList())
75 document->getBlockList()->removeListener(graphicView);
78 if (owner && document)
85 * Adds another MDI window to the list of known windows that
86 * depend on this one. This can be another view or a view for
89 void MDIWindow::addChildWindow(MDIWindow * w)
91 RS_DEBUG->print("RS_MDIWindow::addChildWindow()");
93 childWindows.append(w);
94 w->setParentWindow(this);
96 RS_DEBUG->print("children: %d", childWindows.count());
100 * Removes a child window.
102 * @see addChildWindow
104 void MDIWindow::removeChildWindow(MDIWindow * w)
106 RS_DEBUG->print("RS_MDIWindow::removeChildWindow()");
108 // bool suc = childWindows.remove(w);
109 bool suc = childWindows.removeOne(w);
110 RS_DEBUG->print("successfully removed child window: %d", (int)suc);
112 RS_DEBUG->print("children: %d", childWindows.count());
116 * @return pointer to the print preview of this drawing or NULL.
118 MDIWindow * MDIWindow::getPrintPreview()
120 for(uint i=0; i<childWindows.count(); ++i)
122 if (childWindows.at(i)->getGraphicView()->isPrintPreview())
123 return childWindows.at(i);
130 * closes this MDI window.
132 * @param force Disable cancel button (demo versions)
133 * @param ask Ask user before closing.
135 bool MDIWindow::closeMDI(bool force, bool ask)
137 // should never happen:
142 bool isBlock = (parentWindow != NULL);
144 // This is a block and we don't need to ask the user for closing
145 // since it's still available in the parent drawing after closing.
148 RS_DEBUG->print(" closing block");
149 // tell parent window we're not here anymore.
152 RS_DEBUG->print(" notifying parent about closing this window");
153 parentWindow->removeChildWindow(this);
156 emit(signalClosing());
159 // This is a graphic document. ask user for closing.
160 else if (!ask || slotFileClose(force))
162 RS_DEBUG->print(" closing graphic");
163 // close all child windows:
166 while (!childWindows.isEmpty())
168 MDIWindow * child = childWindows.takeFirst();
174 emit(signalClosing());
177 // User decided not to close graphic document:
183 return (ret || force);
187 * Called by Qt when the user closes this MDI window.
189 void MDIWindow::closeEvent(QCloseEvent * ce)
191 RS_DEBUG->print("MDIWindow::closeEvent begin");
193 if (closeMDI(false, !forceClosing))
198 RS_DEBUG->print("MDIWindow::closeEvent end");
204 * @param type Document type. RS:EntityGraphic or RS2::EntityBlock
205 * @param container Entity container to be used as document or NULL
206 * if a new document should be created.
208 void MDIWindow::initDoc(RS_Document * doc)
210 RS_DEBUG->print("MDIWindow::initDoc()");
214 document = new Drawing();
228 void MDIWindow::initView()
230 RS_DEBUG->print("MDIWindow::initView()");
232 // graphicView = new QC_GraphicView(document, this);
233 graphicView = new QG_GraphicView(document, this);
234 setWidget(graphicView);
235 graphicView->setFocus();
239 * Called when the current pen (color, style, width) has changed.
240 * Sets the active pen for the document in this MDI window.
242 void MDIWindow::slotPenChanged(RS_Pen pen)
244 RS_DEBUG->print("MDIWindow::slotPenChanged() begin");
247 document->setActivePen(pen);
249 RS_DEBUG->print("MDIWindow::slotPenChanged() end");
253 * Creates a new empty document in this MDI window.
255 void MDIWindow::slotFileNew()
257 RS_DEBUG->print("MDIWindow::slotFileNew begin");
259 if (document && graphicView)
262 graphicView->redraw();
265 RS_DEBUG->print("MDIWindow::slotFileNew end");
269 * Opens the given file in this MDI window.
271 bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
273 RS_DEBUG->print("MDIWindow::slotFileOpen");
276 if (document != NULL && !fileName.isEmpty())
279 ret = document->open(fileName, type);
283 //QString message=tr("Loaded document: ")+fileName;
284 //statusBar()->message(message, 2000);
286 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
287 graphicView->zoomAuto(false);
288 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
292 RS_DEBUG->print("MDIWindow::slotFileOpen: failed");
297 RS_DEBUG->print("MDIWindow::slotFileOpen: cancelled");
298 //statusBar()->message(tr("Opening aborted"), 2000);
301 RS_DEBUG->print("MDIWindow::slotFileOpen: OK");
307 * Saves the current file.
309 * @return true if the file was saved successfully.
310 * false if the file could not be saved or the document
313 bool MDIWindow::slotFileSave(bool & cancelled)
315 RS_DEBUG->print("MDIWindow::slotFileSave()");
321 if (document->getFilename().isEmpty())
322 ret = slotFileSaveAs(cancelled);
325 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
326 ret = document->save();
327 QApplication::restoreOverrideCursor();
335 * Saves the current file. The user is asked for a new filename
338 * @return true if the file was saved successfully or the user cancelled.
339 * false if the file could not be saved or the document
342 bool MDIWindow::slotFileSaveAs(bool & cancelled)
344 RS_DEBUG->print("MDIWindow::slotFileSaveAs");
347 RS2::FormatType t = RS2::FormatDXF;
349 QString fn = QG_FileDialog::getSaveFileName(this, &t);
351 if (document != NULL && !fn.isEmpty())
353 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
354 ret = document->saveAs(fn, t);
355 QApplication::restoreOverrideCursor();
359 // cancel is not an error - returns true
368 * Requests the closing of this MDI window.
370 * @param force Force closing by disabling the cancel button (for demo versions).
372 bool MDIWindow::slotFileClose(bool force)
374 RS_DEBUG->print("MDIWindow::slotFileClose()");
379 if (document && document->isModified())
381 ExitDialog dlg(this);
384 if (document->getFilename().isEmpty())
385 dlg.setText(tr("Do you really want to close the drawing?"));
388 QString fn = document->getFilename();
390 if (fn.length() > 50)
391 fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
393 dlg.setText(tr("Do you really want to close the file\n%1?").arg(fn));
396 dlg.setTitle(tr("Closing Drawing"));
415 succ = slotFileSave(cancelled);
416 again = !succ || cancelled;
419 succ = slotFileSaveAs(cancelled);
420 again = !succ || cancelled;
436 void MDIWindow::slotFilePrint()
438 RS_DEBUG->print("MDIWindow::slotFilePrint");
440 //statusBar()->message(tr("Printing..."));
443 // if (printer.setup(this))
444 QPrintDialog dialog(&printer, this);
449 painter.begin(&printer);
451 ///////////////////////////////////////////////////////////////////
452 // TODO: Define printing by using the QPainter methods here
457 //statusBar()->message(tr("Ready."));
460 /** @return Pointer to graphic view */
461 //QC_GraphicView * MDIWindow::getGraphicView()
462 QG_GraphicView * MDIWindow::getGraphicView()
467 /** @return Pointer to document */
468 RS_Document * MDIWindow::getDocument()
473 /** @return Pointer to graphic or NULL */
474 Drawing * MDIWindow::getGraphic()
476 return document->getGraphic();
479 /** @return Pointer to current event handler */
480 RS_EventHandler * MDIWindow::getEventHandler()
483 return graphicView->getEventHandler();
489 * Sets the parent window that will be notified if this
491 void MDIWindow::setParentWindow(MDIWindow * p)
493 RS_DEBUG->print("setParentWindow");
498 * @return The MDI window id.
500 int MDIWindow::getId()
505 void MDIWindow::setForceClosing(bool on)
511 * Streams some info about an MDI window to stdout.
513 std::ostream & operator<<(std::ostream & os, MDIWindow & w)
515 os << "MDIWindow[" << w.getId() << "]:\n";
518 os << " parentWindow: " << w.parentWindow->getId() << "\n";
520 os << " parentWindow: NULL\n";
522 for(uint i=0; i<w.childWindows.count(); ++i)
523 os << " childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";