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 "qc_mdiwindow.h"
18 #include "rs_eventhandler.h"
19 #include "exitdialog.h"
20 #include "qg_filedialog.h"
23 int QC_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 QC_MDIWindow::QC_MDIWindow(RS_Document * doc, QWidget * parent, const char * name/*= NULL*/,
33 Qt::WindowFlags wflags/*= Qt::WDestructiveClose*/):
34 // Q3MainWindow(parent, name, wflags)
35 QMdiSubWindow(parent, Qt::SubWindow)
36 #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"
45 //No worries: Only need to be more vigilant if this were TRUE
46 // childWindows.setAutoDelete(false);
51 if (document->getLayerList() != NULL)
52 // Link the graphic view to the layer widget
53 document->getLayerList()->addListener(graphicView);
55 if (document->getBlockList() != NULL)
56 // Link the graphic view to the block widget
57 document->getBlockList()->addListener(graphicView);
61 setFocus(/*Qt::StrongFocus*/);
67 * Deletes the document associated with this window.
69 QC_MDIWindow::~QC_MDIWindow()
71 if (document->getLayerList() != NULL)
72 document->getLayerList()->removeListener(graphicView);
74 if (document->getBlockList() != NULL)
75 document->getBlockList()->removeListener(graphicView);
77 if (owner == true && document != NULL)
84 * Adds another MDI window to the list of known windows that
85 * depend on this one. This can be another view or a view for
88 void QC_MDIWindow::addChildWindow(QC_MDIWindow * w)
90 RS_DEBUG->print("RS_MDIWindow::addChildWindow()");
92 childWindows.append(w);
93 w->setParentWindow(this);
95 RS_DEBUG->print("children: %d", childWindows.count());
99 * Removes a child window.
101 * @see addChildWindow
103 void QC_MDIWindow::removeChildWindow(QC_MDIWindow * w)
105 RS_DEBUG->print("RS_MDIWindow::removeChildWindow()");
107 // bool suc = childWindows.remove(w);
108 bool suc = childWindows.removeOne(w);
109 RS_DEBUG->print("successfully removed child window: %d", (int)suc);
111 RS_DEBUG->print("children: %d", childWindows.count());
115 * @return pointer to the print preview of this drawing or NULL.
117 QC_MDIWindow * QC_MDIWindow::getPrintPreview()
119 for(uint i=0; i<childWindows.count(); ++i)
121 if (childWindows.at(i)->getGraphicView()->isPrintPreview())
122 return childWindows.at(i);
129 * closes this MDI window.
131 * @param force Disable cancel button (demo versions)
132 * @param ask Ask user before closing.
134 bool QC_MDIWindow::closeMDI(bool force, bool ask)
136 // should never happen:
137 if (document == NULL)
141 bool isBlock = (parentWindow != NULL);
143 // This is a block and we don't need to ask the user for closing
144 // since it's still available in the parent drawing after closing.
147 RS_DEBUG->print(" closing block");
148 // tell parent window we're not here anymore.
149 if (parentWindow != NULL)
151 RS_DEBUG->print(" notifying parent about closing this window");
152 parentWindow->removeChildWindow(this);
155 emit(signalClosing());
158 // This is a graphic document. ask user for closing.
159 else if (!ask || slotFileClose(force))
161 RS_DEBUG->print(" closing graphic");
162 // close all child windows:
165 #if 0 // UGH! WHY??!??!
170 if (childWindows.at(0) != NULL)
172 childWindows.at(0)->close();
178 while (!childWindows.isEmpty())
180 QC_MDIWindow * child = childWindows.takeFirst();
187 emit(signalClosing());
190 // User decided not to close graphic document:
196 return (ret || force);
200 * Called by Qt when the user closes this MDI window.
202 void QC_MDIWindow::closeEvent(QCloseEvent * ce)
204 RS_DEBUG->print("QC_MDIWindow::closeEvent begin");
206 if (closeMDI(false, !forceClosing))
211 RS_DEBUG->print("QC_MDIWindow::closeEvent end");
217 * @param type Document type. RS:EntityGraphic or RS2::EntityBlock
218 * @param container Entity container to be used as document or NULL
219 * if a new document should be created.
221 void QC_MDIWindow::initDoc(RS_Document * doc)
223 RS_DEBUG->print("QC_MDIWindow::initDoc()");
227 document = new Drawing();
241 void QC_MDIWindow::initView()
243 RS_DEBUG->print("QC_MDIWindow::initView()");
245 graphicView = new QC_GraphicView(document, this);
246 // setCentralWidget(graphicView);
247 setWidget(graphicView);
248 graphicView->setFocus();
252 * Called when the current pen (color, style, width) has changed.
253 * Sets the active pen for the document in this MDI window.
255 void QC_MDIWindow::slotPenChanged(RS_Pen pen)
257 RS_DEBUG->print("QC_MDIWindow::slotPenChanged() begin");
259 if (document != NULL)
260 document->setActivePen(pen);
262 RS_DEBUG->print("QC_MDIWindow::slotPenChanged() end");
266 * Creates a new empty document in this MDI window.
268 void QC_MDIWindow::slotFileNew()
270 RS_DEBUG->print("QC_MDIWindow::slotFileNew begin");
272 if (document != NULL && graphicView != NULL)
275 graphicView->redraw();
278 RS_DEBUG->print("QC_MDIWindow::slotFileNew end");
282 * Opens the given file in this MDI window.
284 bool QC_MDIWindow::slotFileOpen(const QString & fileName, RS2::FormatType type)
286 RS_DEBUG->print("QC_MDIWindow::slotFileOpen");
289 if (document != NULL && !fileName.isEmpty())
294 //bah RS_APP->processEvents(QEventLoop::AllEvents, 1000);
296 ret = document->open(fileName, type);
300 //QString message=tr("Loaded document: ")+fileName;
301 //statusBar()->message(message, 2000);
303 RS_DEBUG->print("QC_MDIWindow::slotFileOpen: autoZoom");
304 graphicView->zoomAuto(false);
305 RS_DEBUG->print("QC_MDIWindow::slotFileOpen: autoZoom: OK");
309 RS_DEBUG->print("QC_MDIWindow::slotFileOpen: failed");
314 RS_DEBUG->print("QC_MDIWindow::slotFileOpen: cancelled");
315 //statusBar()->message(tr("Opening aborted"), 2000);
318 RS_DEBUG->print("QC_MDIWindow::slotFileOpen: OK");
324 * Saves the current file.
326 * @return true if the file was saved successfully.
327 * false if the file could not be saved or the document
330 bool QC_MDIWindow::slotFileSave(bool & cancelled)
332 RS_DEBUG->print("QC_MDIWindow::slotFileSave()");
336 if (document != NULL)
338 if (document->getFilename().isEmpty())
339 ret = slotFileSaveAs(cancelled);
342 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
343 ret = document->save();
344 QApplication::restoreOverrideCursor();
352 * Saves the current file. The user is asked for a new filename
355 * @return true if the file was saved successfully or the user cancelled.
356 * false if the file could not be saved or the document
359 bool QC_MDIWindow::slotFileSaveAs(bool & cancelled)
361 RS_DEBUG->print("QC_MDIWindow::slotFileSaveAs");
364 RS2::FormatType t = RS2::FormatDXF;
366 QString fn = QG_FileDialog::getSaveFileName(this, &t);
368 if (document != NULL && !fn.isEmpty())
370 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
371 ret = document->saveAs(fn, t);
372 QApplication::restoreOverrideCursor();
376 // cancel is not an error - returns true
385 * Requests the closing of this MDI window.
387 * @param force Force closing by disabling the cancel button (for demo versions).
389 bool QC_MDIWindow::slotFileClose(bool force)
391 RS_DEBUG->print("QC_MDIWindow::slotFileClose()");
396 if (document != NULL && document->isModified())
398 ExitDialog dlg(this);
401 if (document->getFilename().isEmpty())
402 dlg.setText(tr("Do you really want to close the drawing?"));
405 QString fn = document->getFilename();
407 if (fn.length() > 50)
408 fn = QString("%1...%2").arg(fn.left(24)).arg(fn.right(24));
410 dlg.setText(tr("Do you really want to close the file\n%1?").arg(fn));
413 dlg.setTitle(tr("Closing Drawing"));
432 succ = slotFileSave(cancelled);
433 again = !succ || cancelled;
436 succ = slotFileSaveAs(cancelled);
437 again = !succ || cancelled;
453 void QC_MDIWindow::slotFilePrint()
455 RS_DEBUG->print("QC_MDIWindow::slotFilePrint");
457 //statusBar()->message(tr("Printing..."));
460 // if (printer.setup(this))
461 QPrintDialog dialog(&printer, this);
466 painter.begin(&printer);
468 ///////////////////////////////////////////////////////////////////
469 // TODO: Define printing by using the QPainter methods here
474 //statusBar()->message(tr("Ready."));
477 /** @return Pointer to graphic view */
478 QC_GraphicView * QC_MDIWindow::getGraphicView()
483 /** @return Pointer to document */
484 RS_Document * QC_MDIWindow::getDocument()
489 /** @return Pointer to graphic or NULL */
490 Drawing * QC_MDIWindow::getGraphic()
492 return document->getGraphic();
495 /** @return Pointer to current event handler */
496 RS_EventHandler * QC_MDIWindow::getEventHandler()
498 if (graphicView != NULL)
499 return graphicView->getEventHandler();
505 * Sets the parent window that will be notified if this
507 void QC_MDIWindow::setParentWindow(QC_MDIWindow * p)
509 RS_DEBUG->print("setParentWindow");
514 * @return The MDI window id.
516 int QC_MDIWindow::getId()
521 void QC_MDIWindow::setForceClosing(bool on)
527 * Streams some info about an MDI window to stdout.
529 std::ostream & operator<<(std::ostream & os, QC_MDIWindow & w)
531 os << "QC_MDIWindow[" << w.getId() << "]:\n";
533 if (w.parentWindow!=NULL)
534 os << " parentWindow: " << w.parentWindow->getId() << "\n";
536 os << " parentWindow: NULL\n";
538 for(uint i=0; i<w.childWindows.count(); ++i)
539 os << " childWindow[" << i << "]: " << w.childWindows.at(i)->getId() << "\n";