+void ApplicationWindow::FileNew(void)
+{
+ // Warn the user if drawing has changed and hasn't been saved...
+ if (drawing->dirty)
+ {
+ QMessageBox msg;
+
+ msg.setText("The document has been modified.");
+ msg.setInformativeText("Do you want to save your changes?");
+ msg.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
+ msg.setDefaultButton(QMessageBox::Save);
+ int response = msg.exec();
+
+ switch (response)
+ {
+ case QMessageBox::Save:
+ // Save was clicked
+ FileSave();
+ break;
+ case QMessageBox::Discard:
+ // Don't Save was clicked
+ break;
+ case QMessageBox::Cancel:
+ // Cancel was clicked
+ return;
+// break;
+ }
+ }
+
+ FileIO::ResetLayerVectors();
+ emit ReloadLayers();
+ DeleteContents(drawing->document.objects);
+ drawing->document.objects.clear();
+ drawing->dirty = false;
+ drawing->update();
+ documentName.clear();
+// setWindowTitle("Architektonas - Untitled");
+ setWindowFilePath(documentName);
+ statusBar()->showMessage(tr("New drawing is ready."));
+}
+
+void ApplicationWindow::FileOpen(void)
+{
+ QString filename = QFileDialog::getOpenFileName(this, tr("Open Drawing"),
+ "", tr("Architektonas files (*.drawing)"));
+
+ // User cancelled open
+ if (filename.isEmpty())
+ return;
+
+ LoadFile(filename);
+}
+
+void ApplicationWindow::FileOpenRecent(void)
+{
+ QAction * action = qobject_cast<QAction *>(sender());
+
+ if (!action)
+ return;
+
+ LoadFile(action->data().toString());
+}
+
+void ApplicationWindow::LoadFile(QString filename)
+{
+ FILE * file = fopen(filename.toUtf8().data(), "r");
+
+ if (file == 0)
+ {
+ QMessageBox msg;
+ msg.setText(QString(tr("Could not open file \"%1\" for loading!")).arg(filename));
+ msg.setIcon(QMessageBox::Critical);
+ msg.exec();
+ return;
+ }
+
+ Container container(true); // Make sure it's a top level container...
+ bool successful = FileIO::LoadAtnsFile(file, &container);
+ fclose(file);
+
+ if (!successful)
+ {
+ QMessageBox msg;
+ msg.setText(QString(tr("Could not load file \"%1\"!")).arg(filename));
+ msg.setIcon(QMessageBox::Critical);
+ msg.exec();
+ // Make sure to delete any hanging objects in the container...
+ DeleteContents(container.objects);
+ return;
+ }
+
+ // Keep memory leaks from happening by getting rid of the old document
+ DeleteContents(drawing->document.objects);
+ emit ReloadLayers();
+ // We can do this because the vector is just a bunch of pointers to our
+ // Objects, and the Containers (non-empty) can be moved this way with no
+ // problem.
+ drawing->document = container;
+ drawing->dirty = false;
+ drawing->update();
+ documentName = filename;
+// setWindowTitle(QString("Architektonas - %1").arg(documentName));
+ AdjustMRU(filename);
+ statusBar()->showMessage(tr("Drawing loaded."));
+}
+
+void ApplicationWindow::FileSave(void)
+{
+ if (documentName.isEmpty())
+ documentName = QFileDialog::getSaveFileName(this, tr("Save Drawing"),
+ "", tr("Architektonas drawings (*.drawing)"));
+
+ FILE * file = fopen(documentName.toUtf8().data(), "w");
+
+ if (file == 0)
+ {
+ QMessageBox msg;
+ msg.setText(QString(tr("Could not open file \"%1\" for saving!")).arg(documentName));
+ msg.setIcon(QMessageBox::Critical);
+ msg.exec();
+ return;
+ }
+
+ bool successful = FileIO::SaveAtnsFile(file, &drawing->document);
+ fclose(file);
+
+ if (!successful)
+ {
+ QMessageBox msg;
+ msg.setText(QString(tr("Could not save file \"%1\"!")).arg(documentName));
+ msg.setIcon(QMessageBox::Critical);
+ msg.exec();
+ // In this case, we should unlink the created file, since it's not
+ // right...
+// unlink(documentName.toUtf8().data());
+ QFile::remove(documentName);
+ return;
+ }
+
+ drawing->dirty = false;
+// setWindowTitle(QString("Architektonas - %1").arg(documentName));
+ AdjustMRU(documentName);
+ statusBar()->showMessage(tr("Drawing saved."));
+}
+
+void ApplicationWindow::FileSaveAs(void)
+{
+ QString filename = QFileDialog::getSaveFileName(this, tr("Save Drawing As"),
+ documentName, tr("Architektonas drawings (*.drawing)"));
+
+ if (!filename.isEmpty())
+ {
+ documentName = filename;
+ FileSave();
+ }
+}
+
+void ApplicationWindow::PrintPreview(void)
+{
+ QPrintPreviewDialog * dlg = new QPrintPreviewDialog(this);
+
+ connect(dlg, SIGNAL(paintRequested(QPrinter *)), this, SLOT(HandlePrintRequest(QPrinter *)));
+
+ dlg->exec();
+}
+
+void ApplicationWindow::HandlePrintRequest(QPrinter * printer)
+{
+ QPainter qtPainter(printer);
+ Painter painter(&qtPainter);
+
+ // Save vars for screen
+ Point originSave = Global::origin;
+ double zoomSave = Global::zoom;
+ Vector screenSizeSave = Global::screenSize;
+
+ // Adjust zoom + origin to fit the paper (or NxM pages if we have 'em)
+ Rect r = drawing->GetObjectExtents((Object *)(&(drawing->document)));
+
+ QPageLayout pageLayout = printer->pageLayout();
+ QRect pageRect = pageLayout.paintRectPixels(printer->resolution());
+
+ Global::origin = r.BottomLeft();
+ Global::screenSize.x = pageRect.width();
+ Global::screenSize.y = pageRect.height();
+
+ double xScale = (double)pageRect.width() / r.Width();
+ double yScale = (double)pageRect.height() / r.Height();
+ Global::zoom = qMin(xScale, yScale);
+
+ if (xScale < yScale)
+ Global::origin.y -= (((double)pageRect.height() / Global::zoom) - r.Height()) / 2.0;
+ else
+ Global::origin.x -= (((double)pageRect.width() / Global::zoom) - r.Width()) / 2.0;
+
+ // Do object rendering...
+ for(int i=0; i<Global::numLayers; i++)
+ drawing->RenderObjects(&painter, drawing->document.objects, i);
+
+ // Restore vars
+ Global::origin = originSave;
+ Global::zoom = zoomSave;
+ Global::screenSize = screenSizeSave;
+}
+
+//
+// Right-click menu
+//
+void ApplicationWindow::contextMenuEvent(QContextMenuEvent * event)
+{
+ // Proof of concept, still need to code up the real thing
+ QMenu menu(this);
+
+ VPVector hovered = drawing->GetHovered();
+
+ if ((drawing->select.size() > 0) || (hovered.size() > 0))
+ {
+ QMenu * layerMenu = menu.addMenu("To layer");
+
+ layerAct.clear();
+
+ for(int i=0; i<Global::numLayers; i++)
+ {
+ QAction * act = new QAction(QIcon(), Global::layerName[i].c_str(), this);
+ act->setData(i);
+ layerMenu->addAction(act);
+ layerAct.append(act);
+ connect(act, SIGNAL(triggered()), this, SLOT(MoveToLayer()));
+ }
+ }
+
+ menu.addAction(mirrorAct);
+ menu.addAction(rotateAct);
+ menu.addAction(trimAct);
+ menu.exec(event->globalPos());
+}
+
+void ApplicationWindow::MoveToLayer(void)
+{
+ QAction * act = qobject_cast<QAction *>(sender());
+
+ drawing->MoveSelectedToLayer(act->data().toInt());
+ drawing->update();
+}
+
+void ApplicationWindow::UpdateFromCommand(void)
+{
+ cw->SetToolPrompt();
+ drawing->update();
+}