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"
23 int MDIWindow::idCounter = 0;
28 * @param doc Pointer to an existing document of NULL if a new
29 * document shall be created for this window.
30 * @param parent Parent widget. Usually a workspace.
32 MDIWindow::MDIWindow(RS_Document * doc, QWidget * parent, const char * name/*= NULL*/,
33 Qt::WindowFlags wflags/*= Qt::WDestructiveClose*/):
34 QMdiSubWindow(parent, Qt::SubWindow)
35 #warning "!!! wflags is ignored !!!"
39 //This warning is most likely coming from the QMdiSubWindow() constructor above...
40 #warning "QWidget::setMinimumSize: (/QMdi::ControlLabel) Negative sizes (-1,-1) are not possible"
44 //No worries: Only need to be more vigilant if this were TRUE
45 // childWindows.setAutoDelete(false);
50 if (document->getLayerList() != NULL)
51 // Link the graphic view to the layer widget
52 document->getLayerList()->addListener(graphicView);
54 if (document->getBlockList() != NULL)
55 // Link the graphic view to the block widget
56 document->getBlockList()->addListener(graphicView);
60 setFocus(/*Qt::StrongFocus*/);
66 * Deletes the document associated with this window.
68 MDIWindow::~MDIWindow()
70 if (document->getLayerList() != NULL)
71 document->getLayerList()->removeListener(graphicView);
73 if (document->getBlockList() != NULL)
74 document->getBlockList()->removeListener(graphicView);
76 if (owner == true && document != NULL)
83 * Adds another MDI window to the list of known windows that
84 * depend on this one. This can be another view or a view for
87 void MDIWindow::addChildWindow(MDIWindow * w)
89 RS_DEBUG->print("RS_MDIWindow::addChildWindow()");
91 childWindows.append(w);
92 w->setParentWindow(this);
94 RS_DEBUG->print("children: %d", childWindows.count());
98 * Removes a child window.
100 * @see addChildWindow
102 void MDIWindow::removeChildWindow(MDIWindow * w)
104 RS_DEBUG->print("RS_MDIWindow::removeChildWindow()");
106 // bool suc = childWindows.remove(w);
107 bool suc = childWindows.removeOne(w);
108 RS_DEBUG->print("successfully removed child window: %d", (int)suc);
110 RS_DEBUG->print("children: %d", childWindows.count());
114 * @return pointer to the print preview of this drawing or NULL.
116 MDIWindow * MDIWindow::getPrintPreview()
118 for(uint 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(bool force, bool ask)
135 // should never happen:
136 if (document == NULL)
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 RS_DEBUG->print(" closing block");
147 // tell parent window we're not here anymore.
148 if (parentWindow != NULL)
150 RS_DEBUG->print(" notifying parent about closing this window");
151 parentWindow->removeChildWindow(this);
154 emit(signalClosing());
157 // This is a graphic document. ask user for closing.
158 else if (!ask || slotFileClose(force))
160 RS_DEBUG->print(" closing graphic");
161 // close all child windows:
164 #if 0 // UGH! WHY??!??!
169 if (childWindows.at(0) != NULL)
171 childWindows.at(0)->close();
177 while (!childWindows.isEmpty())
179 MDIWindow * child = childWindows.takeFirst();
186 emit(signalClosing());
189 // User decided not to close graphic document:
195 return (ret || force);
199 * Called by Qt when the user closes this MDI window.
201 void MDIWindow::closeEvent(QCloseEvent * ce)
203 RS_DEBUG->print("MDIWindow::closeEvent begin");
205 if (closeMDI(false, !forceClosing))
210 RS_DEBUG->print("MDIWindow::closeEvent end");
216 * @param type Document type. RS:EntityGraphic or RS2::EntityBlock
217 * @param container Entity container to be used as document or NULL
218 * if a new document should be created.
220 void MDIWindow::initDoc(RS_Document * doc)
222 RS_DEBUG->print("MDIWindow::initDoc()");
226 document = new Drawing();
240 void MDIWindow::initView()
242 RS_DEBUG->print("MDIWindow::initView()");
244 graphicView = new QC_GraphicView(document, this);
245 // setCentralWidget(graphicView);
246 setWidget(graphicView);
247 graphicView->setFocus();
251 * Called when the current pen (color, style, width) has changed.
252 * Sets the active pen for the document in this MDI window.
254 void MDIWindow::slotPenChanged(RS_Pen pen)
256 RS_DEBUG->print("MDIWindow::slotPenChanged() begin");
258 if (document != NULL)
259 document->setActivePen(pen);
261 RS_DEBUG->print("MDIWindow::slotPenChanged() end");
265 * Creates a new empty document in this MDI window.
267 void MDIWindow::slotFileNew()
269 RS_DEBUG->print("MDIWindow::slotFileNew begin");
271 if (document != NULL && graphicView != NULL)
274 graphicView->redraw();
277 RS_DEBUG->print("MDIWindow::slotFileNew end");
281 * Opens the given file in this MDI window.
283 bool MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
285 RS_DEBUG->print("MDIWindow::slotFileOpen");
288 if (document != NULL && !fileName.isEmpty())
293 //bah RS_APP->processEvents(QEventLoop::AllEvents, 1000);
295 ret = document->open(fileName, type);
299 //QString message=tr("Loaded document: ")+fileName;
300 //statusBar()->message(message, 2000);
302 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom");
303 graphicView->zoomAuto(false);
304 RS_DEBUG->print("MDIWindow::slotFileOpen: autoZoom: OK");
308 RS_DEBUG->print("MDIWindow::slotFileOpen: failed");
313 RS_DEBUG->print("MDIWindow::slotFileOpen: cancelled");
314 //statusBar()->message(tr("Opening aborted"), 2000);
317 RS_DEBUG->print("MDIWindow::slotFileOpen: OK");
323 * Saves the current file.
325 * @return true if the file was saved successfully.
326 * false if the file could not be saved or the document
329 bool MDIWindow::slotFileSave(bool & cancelled)
331 RS_DEBUG->print("MDIWindow::slotFileSave()");
335 if (document != NULL)
337 if (document->getFilename().isEmpty())
338 ret = slotFileSaveAs(cancelled);
341 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
342 ret = document->save();
343 QApplication::restoreOverrideCursor();
351 * Saves the current file. The user is asked for a new filename
354 * @return true if the file was saved successfully or the user cancelled.
355 * false if the file could not be saved or the document
358 bool MDIWindow::slotFileSaveAs(bool & cancelled)
360 RS_DEBUG->print("MDIWindow::slotFileSaveAs");
363 RS2::FormatType t = RS2::FormatDXF;
365 QString fn = QG_FileDialog::getSaveFileName(this, &t);
367 if (document != NULL && !fn.isEmpty())
369 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
370 ret = document->saveAs(fn, t);
371 QApplication::restoreOverrideCursor();
375 // cancel is not an error - returns true
384 * Requests the closing of this MDI window.
386 * @param force Force closing by disabling the cancel button (for demo versions).
388 bool MDIWindow::slotFileClose(bool force)
390 RS_DEBUG->print("MDIWindow::slotFileClose()");
395 if (document != NULL && document->isModified())
397 ExitDialog dlg(this);
400 if (document->getFilename().isEmpty())
401 dlg.setText(tr("Do you really want to close the drawing?"));
404 QString fn = document->getFilename();
406 if (fn.length() > 50)
407 fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
409 dlg.setText(tr("Do you really want to close the file\n%1?").arg(fn));
412 dlg.setTitle(tr("Closing Drawing"));
431 succ = slotFileSave(cancelled);
432 again = !succ || cancelled;
435 succ = slotFileSaveAs(cancelled);
436 again = !succ || cancelled;
452 void MDIWindow::slotFilePrint()
454 RS_DEBUG->print("MDIWindow::slotFilePrint");
456 //statusBar()->message(tr("Printing..."));
459 // if (printer.setup(this))
460 QPrintDialog dialog(&printer, this);
465 painter.begin(&printer);
467 ///////////////////////////////////////////////////////////////////
468 // TODO: Define printing by using the QPainter methods here
473 //statusBar()->message(tr("Ready."));
476 /** @return Pointer to graphic view */
477 QC_GraphicView * MDIWindow::getGraphicView()
482 /** @return Pointer to document */
483 RS_Document * MDIWindow::getDocument()
488 /** @return Pointer to graphic or NULL */
489 Drawing * MDIWindow::getGraphic()
491 return document->getGraphic();
494 /** @return Pointer to current event handler */
495 RS_EventHandler * MDIWindow::getEventHandler()
497 if (graphicView != NULL)
498 return graphicView->getEventHandler();
504 * Sets the parent window that will be notified if this
506 void MDIWindow::setParentWindow(MDIWindow * p)
508 RS_DEBUG->print("setParentWindow");
513 * @return The MDI window id.
515 int MDIWindow::getId()
520 void MDIWindow::setForceClosing(bool on)
526 * Streams some info about an MDI window to stdout.
528 std::ostream & operator<<(std::ostream & os, MDIWindow & w)
530 os << "MDIWindow[" << w.getId() << "]:\n";
532 if (w.parentWindow!=NULL)
533 os << " parentWindow: " << w.parentWindow->getId() << "\n";
535 os << " parentWindow: NULL\n";
537 for(uint i=0; i<w.childWindows.count(); ++i)
538 os << " childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";