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"
47 if (document->getLayerList())
48 // Link the graphic view to the layer widget
49 document->getLayerList()->addListener(graphicView);
51 if (document->getBlockList())
52 // Link the graphic view to the block widget
53 document->getBlockList()->addListener(graphicView);
57 setFocus(/*Qt::StrongFocus*/);
63 * Deletes the document associated with this window.
65 MDIWindow::~MDIWindow()
67 if (document->getLayerList())
68 document->getLayerList()->removeListener(graphicView);
70 if (document->getBlockList())
71 document->getBlockList()->removeListener(graphicView);
73 if (owner && document)
80 * Adds another MDI window to the list of known windows that
81 * depend on this one. This can be another view or a view for
84 void MDIWindow::addChildWindow(MDIWindow * w)
86 RS_DEBUG->print("RS_MDIWindow::addChildWindow()");
88 childWindows.append(w);
89 w->setParentWindow(this);
91 RS_DEBUG->print("children: %d", childWindows.count());
95 * Removes a child window.
99 void MDIWindow::removeChildWindow(MDIWindow * w)
101 RS_DEBUG->print("RS_MDIWindow::removeChildWindow()");
103 // bool suc = childWindows.remove(w);
104 bool suc = childWindows.removeOne(w);
105 RS_DEBUG->print("successfully removed child window: %d", (int)suc);
107 RS_DEBUG->print("children: %d", childWindows.count());
111 * @return pointer to the print preview of this drawing or NULL.
113 MDIWindow * MDIWindow::getPrintPreview()
115 for(uint i=0; i<childWindows.count(); ++i)
117 if (childWindows.at(i)->getGraphicView()->isPrintPreview())
118 return childWindows.at(i);
125 * closes this MDI window.
127 * @param force Disable cancel button (demo versions)
128 * @param ask Ask user before closing.
130 bool MDIWindow::closeMDI(bool force, bool ask)
132 // should never happen:
137 bool isBlock = (parentWindow != NULL);
139 // This is a block and we don't need to ask the user for closing
140 // since it's still available in the parent drawing after closing.
143 RS_DEBUG->print(" closing block");
144 // tell parent window we're not here anymore.
147 RS_DEBUG->print(" notifying parent about closing this window");
148 parentWindow->removeChildWindow(this);
151 emit(signalClosing());
154 // This is a graphic document. ask user for closing.
155 else if (!ask || slotFileClose(force))
157 RS_DEBUG->print(" closing graphic");
158 // close all child windows:
161 while (!childWindows.isEmpty())
163 MDIWindow * child = childWindows.takeFirst();
169 emit(signalClosing());
172 // User decided not to close graphic document:
178 return (ret || force);
182 * Called by Qt when the user closes this MDI window.
184 void MDIWindow::closeEvent(QCloseEvent * ce)
186 RS_DEBUG->print("MDIWindow::closeEvent begin");
188 if (closeMDI(false, !forceClosing))
193 RS_DEBUG->print("MDIWindow::closeEvent end");
199 * @param type Document type. RS:EntityGraphic or RS2::EntityBlock
200 * @param container Entity container to be used as document or NULL
201 * if a new document should be created.
203 void MDIWindow::initDoc(RS_Document * doc)
205 RS_DEBUG->print("MDIWindow::initDoc()");
209 document = new Drawing();
223 void MDIWindow::initView()
225 RS_DEBUG->print("MDIWindow::initView()");
227 // graphicView = new QC_GraphicView(document, this);
228 graphicView = new QG_GraphicView(document, this);
229 setWidget(graphicView);
230 graphicView->setFocus();
234 * Called when the current pen (color, style, width) has changed.
235 * Sets the active pen for the document in this MDI window.
237 void MDIWindow::slotPenChanged(RS_Pen pen)
239 RS_DEBUG->print("MDIWindow::slotPenChanged() begin");
242 document->setActivePen(pen);
244 RS_DEBUG->print("MDIWindow::slotPenChanged() end");
248 * Creates a new empty document in this MDI window.
250 void MDIWindow::slotFileNew()
252 RS_DEBUG->print("MDIWindow::slotFileNew begin");
254 if (document && graphicView)
257 graphicView->redraw();
260 RS_DEBUG->print("MDIWindow::slotFileNew end");
264 * Opens the given file in this MDI window.
266 bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
268 RS_DEBUG->print("MDIWindow::slotFileOpen");
271 if (document != NULL && !fileName.isEmpty())
274 ret = document->open(fileName, type);
278 //QString message=tr("Loaded document: ")+fileName;
279 //statusBar()->message(message, 2000);
281 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
282 graphicView->zoomAuto(false);
283 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
287 RS_DEBUG->print("MDIWindow::slotFileOpen: failed");
292 RS_DEBUG->print("MDIWindow::slotFileOpen: cancelled");
293 //statusBar()->message(tr("Opening aborted"), 2000);
296 RS_DEBUG->print("MDIWindow::slotFileOpen: OK");
302 * Saves the current file.
304 * @return true if the file was saved successfully.
305 * false if the file could not be saved or the document
308 bool MDIWindow::slotFileSave(bool & cancelled)
310 RS_DEBUG->print("MDIWindow::slotFileSave()");
316 if (document->getFilename().isEmpty())
317 ret = slotFileSaveAs(cancelled);
320 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
321 ret = document->save();
322 QApplication::restoreOverrideCursor();
330 * Saves the current file. The user is asked for a new filename
333 * @return true if the file was saved successfully or the user cancelled.
334 * false if the file could not be saved or the document
337 bool MDIWindow::slotFileSaveAs(bool & cancelled)
339 RS_DEBUG->print("MDIWindow::slotFileSaveAs");
342 RS2::FormatType t = RS2::FormatDXF;
344 QString fn = QG_FileDialog::getSaveFileName(this, &t);
346 if (document != NULL && !fn.isEmpty())
348 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
349 ret = document->saveAs(fn, t);
350 QApplication::restoreOverrideCursor();
354 // cancel is not an error - returns true
363 * Requests the closing of this MDI window.
365 * @param force Force closing by disabling the cancel button (for demo versions).
367 bool MDIWindow::slotFileClose(bool force)
369 RS_DEBUG->print("MDIWindow::slotFileClose()");
374 if (document && document->isModified())
376 ExitDialog dlg(this);
379 if (document->getFilename().isEmpty())
380 dlg.setText(tr("Do you really want to close the drawing?"));
383 QString fn = document->getFilename();
385 if (fn.length() > 50)
386 fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
388 dlg.setText(tr("Do you really want to close the file\n%1?").arg(fn));
391 dlg.setTitle(tr("Closing Drawing"));
410 succ = slotFileSave(cancelled);
411 again = !succ || cancelled;
414 succ = slotFileSaveAs(cancelled);
415 again = !succ || cancelled;
431 void MDIWindow::slotFilePrint()
433 RS_DEBUG->print("MDIWindow::slotFilePrint");
435 //statusBar()->message(tr("Printing..."));
438 // if (printer.setup(this))
439 QPrintDialog dialog(&printer, this);
444 painter.begin(&printer);
446 ///////////////////////////////////////////////////////////////////
447 // TODO: Define printing by using the QPainter methods here
452 //statusBar()->message(tr("Ready."));
455 /** @return Pointer to graphic view */
456 //QC_GraphicView * MDIWindow::getGraphicView()
457 QG_GraphicView * MDIWindow::getGraphicView()
462 /** @return Pointer to document */
463 RS_Document * MDIWindow::getDocument()
468 /** @return Pointer to graphic or NULL */
469 Drawing * MDIWindow::getGraphic()
471 return document->getGraphic();
474 /** @return Pointer to current event handler */
475 RS_EventHandler * MDIWindow::getEventHandler()
478 return graphicView->getEventHandler();
484 * Sets the parent window that will be notified if this
486 void MDIWindow::setParentWindow(MDIWindow * p)
488 RS_DEBUG->print("setParentWindow");
493 * @return The MDI window id.
495 int MDIWindow::getId()
500 void MDIWindow::setForceClosing(bool on)
506 * Streams some info about an MDI window to stdout.
508 std::ostream & operator<<(std::ostream & os, MDIWindow & w)
510 os << "MDIWindow[" << w.getId() << "]:\n";
513 os << " parentWindow: " << w.parentWindow->getId() << "\n";
515 os << " parentWindow: NULL\n";
517 for(uint i=0; i<w.childWindows.count(); ++i)
518 os << " childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";