2 // applicationwindow.cpp: Architektonas
4 // Part of the Architektonas Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
8 // JLH = James Hammons <jlhamm@acm.org>
11 // --- ---------- -------------------------------------------------------------
12 // JLH 03/22/2011 Created this file
13 // JLH 09/29/2011 Added simple zoom in/out functionality
14 // JLH 10/03/2011 Fixed zoom tool to zoom in/out from center of screen
24 // Uncomment this for debugging...
26 //#define DEBUGFOO // Various tool debugging...
27 //#define DEBUGTP // Toolpalette debugging...
29 #include "applicationwindow.h"
32 #include "drawingview.h"
33 #include "generaltab.h"
35 #include "settingsdialog.h"
38 ApplicationWindow::ApplicationWindow(): settings("Underground Software", "Architektonas")
40 drawing = new DrawingView(this);
41 drawing->setMouseTracking(true); // We want *all* mouse events...!
42 setCentralWidget(drawing);
44 aboutWin = new AboutWindow(this);
46 // ((TTEdit *)qApp)->charWnd = new CharWindow(this);
48 setWindowIcon(QIcon(":/res/atns-icon.png"));
49 setWindowTitle("Architektonas");
56 statusBar()->showMessage(tr("Ready"));
60 // connect(textEdit->document(), SIGNAL(contentsChanged()),
61 // this, SLOT(documentWasModified()));
63 // setCurrentFile("");
64 setUnifiedTitleAndToolBarOnMac(true);
66 // ((TTEdit *)qApp)->charWnd->show();//eh?
67 Object::SetFont(new QFont("Verdana", 15, QFont::Bold));
70 void ApplicationWindow::closeEvent(QCloseEvent * event)
73 event->accept(); // Use ignore() if can't close for some reason
74 //Do we have a memory leak here if we don't delete the font in the Object???
77 //void ApplicationWindow::FileOpen(void)
81 void ApplicationWindow::FixAngle(void)
83 Object::SetFixedAngle(fixAngleAct->isChecked());
86 void ApplicationWindow::FixLength(void)
88 Object::SetFixedLength(fixLengthAct->isChecked());
91 void ApplicationWindow::DeleteTool(void)
93 Object::SetDeleteActive(deleteAct->isChecked());
96 void ApplicationWindow::DimensionTool(void)
98 Object::SetDimensionActive(addDimensionAct->isChecked());
101 void ApplicationWindow::RotateTool(void)
103 drawing->SetRotateToolActive(rotateAct->isChecked());
106 void ApplicationWindow::AddLineTool(void)
108 addCircleAct->setChecked(false);
109 addArcAct->setChecked(false);
110 rotateAct->setChecked(false);
112 drawing->SetAddLineToolActive(addLineAct->isChecked());
115 void ApplicationWindow::AddCircleTool(void)
117 addLineAct->setChecked(false);
118 addArcAct->setChecked(false);
119 rotateAct->setChecked(false);
121 drawing->SetAddCircleToolActive(addCircleAct->isChecked());
124 void ApplicationWindow::ZoomInTool(void)
126 double zoomFactor = 2.0;
128 We need to find the center of the screen, then figure out where the new corner
129 will be in the zoomed in window.
131 So we know in Qt coords, the center is found via:
132 size.width() / 2 --> xCenter
133 size.height() / 2 --> yCenter
135 transform x/yCenter to Cartesian coordinates. So far, so good.
137 when zooming in, new origin will be (xCenter - origin.x) / 2, (yCenter - origin.y) / 2
138 (after subtracting from center, that is...)
140 QSize size = drawing->size();
141 Vector center(size.width() / 2.0, size.height() / 2.0);
142 //printf("Zoom in... Center=%.2f,%.2f; ", center.x, center.y);
143 center = Painter::QtToCartesianCoords(center);
144 //printf("(%.2f,%.2f); origin=%.2f,%.2f; ", center.x, center.y, Painter::origin.x, Painter::origin.y);
145 Vector newOrigin = center - ((center - Painter::origin) / zoomFactor);
146 //printf("newOrigin=%.2f,%.2f;\n", newOrigin.x, newOrigin.y);
147 Painter::origin = newOrigin;
149 //printf("Zoom in... level going from %02f to ", Painter::zoom);
150 // This just zooms leaving origin intact... should zoom in at the current center! [DONE]
151 Painter::zoom *= zoomFactor;
155 void ApplicationWindow::ZoomOutTool(void)
160 origin = (223, 160.5)
161 newOrigin should be (-10, -10)
164 center - origin = (213, 150.5)
165 origin - center = (-213, -150.5)
167 + center = (-10, -10)
170 double zoomFactor = 2.0;
171 QSize size = drawing->size();
172 Vector center(size.width() / 2.0, size.height() / 2.0);
173 //printf("Zoom out... Center=%.2f,%.2f; ", center.x, center.y);
174 center = Painter::QtToCartesianCoords(center);
175 //printf("(%.2f,%.2f); origin=%.2f,%.2f; ", center.x, center.y, Painter::origin.x, Painter::origin.y);
176 // Vector newOrigin = (center - Painter::origin) * zoomFactor;
177 // Vector newOrigin = center - (Painter::origin * zoomFactor);
178 Vector newOrigin = center + ((Painter::origin - center) * zoomFactor);
179 //printf("newOrigin=%.2f,%.2f;\n", newOrigin.x, newOrigin.y);
180 Painter::origin = newOrigin;
181 //printf("Zoom out...\n");
182 // This just zooms leaving origin intact... should zoom out at the current center! [DONE]
183 Painter::zoom /= zoomFactor;
187 void ApplicationWindow::HelpAbout(void)
192 void ApplicationWindow::Settings(void)
194 SettingsDialog dlg(this);
195 dlg.generalTab->antialiasChk->setChecked(drawing->useAntialiasing);
197 if (dlg.exec() == false)
200 // Deal with stuff here (since user hit "OK" button...)
201 drawing->useAntialiasing = dlg.generalTab->antialiasChk->isChecked();
205 void ApplicationWindow::CreateActions(void)
207 exitAct = CreateAction(tr("&Quit"), tr("Quit"), tr("Exits the application."),
208 QIcon(":/res/quit.png"), QKeySequence(tr("Ctrl+q")));
209 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
211 fixAngleAct = CreateAction(tr("Fix &Angle"), tr("Fix Angle"), tr("Fixes the angle of an object."),
212 QIcon(":/res/fix-angle.png"), QKeySequence(tr("F,A")), true);
213 connect(fixAngleAct, SIGNAL(triggered()), this, SLOT(FixAngle()));
215 fixLengthAct = CreateAction(tr("Fix &Length"), tr("Fix Length"), tr("Fixes the length of an object."),
216 QIcon(":/res/fix-length.png"), QKeySequence(tr("F,L")), true);
217 connect(fixLengthAct, SIGNAL(triggered()), this, SLOT(FixLength()));
219 deleteAct = CreateAction(tr("&Delete"), tr("Delete Object"), tr("Deletes selected objects."), QIcon(":/res/generic-tool.png"), QKeySequence(), true);
220 connect(deleteAct, SIGNAL(triggered()), this, SLOT(DeleteTool()));
222 addDimensionAct = CreateAction(tr("Add &Dimension"), tr("Add Dimension"), tr("Adds a dimension to the drawing."), QIcon(":/res/dimension-tool.png"), QKeySequence("D,I"), true);
223 connect(addDimensionAct, SIGNAL(triggered()), this, SLOT(DimensionTool()));
225 addLineAct = CreateAction(tr("Add &Line"), tr("Add Line"), tr("Adds lines to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence("A,L"), true);
226 connect(addLineAct, SIGNAL(triggered()), this, SLOT(AddLineTool()));
228 addCircleAct = CreateAction(tr("Add &Circle"), tr("Add Circle"), tr("Adds circles to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence("A,C"), true);
229 connect(addCircleAct, SIGNAL(triggered()), this, SLOT(AddCircleTool()));
231 addArcAct = CreateAction(tr("Add &Arc"), tr("Add Arc"), tr("Adds arcs to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence("A,A"), true);
233 aboutAct = CreateAction(tr("About &Architektonas"), tr("About Architektonas"), tr("Gives information about this program."), QIcon(":/res/generic-tool.png"), QKeySequence());
234 connect(aboutAct, SIGNAL(triggered()), this, SLOT(HelpAbout()));
236 rotateAct = CreateAction(tr("&Rotate Objects"), tr("Rotate"), tr("Rotate object(s) around an arbitrary center."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("R,O")), true);
237 connect(rotateAct, SIGNAL(triggered()), this, SLOT(RotateTool()));
239 zoomInAct = CreateAction(tr("Zoom &In"), tr("Zoom In"), tr("Zoom in on the document."), QIcon(":/res/zoom-in.png"), QKeySequence(tr("+")), QKeySequence(tr("=")));
240 connect(zoomInAct, SIGNAL(triggered()), this, SLOT(ZoomInTool()));
242 zoomOutAct = CreateAction(tr("Zoom &Out"), tr("Zoom Out"), tr("Zoom out of the document."), QIcon(":/res/zoom-out.png"), QKeySequence(tr("-")));
243 connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(ZoomOutTool()));
245 fileNewAct = CreateAction(tr("&New Drawing"), tr("New Drawing"), tr("Creates a new drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+n")));
247 fileOpenAct = CreateAction(tr("&Open Drawing"), tr("Open Drawing"), tr("Opens an existing drawing from a file."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+o")));
249 fileSaveAct = CreateAction(tr("&Save Drawing"), tr("Save Drawing"), tr("Saves the current drawing to a file."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+s")));
251 fileSaveAsAct = CreateAction(tr("Save Drawing &As"), tr("Save As"), tr("Saves the current drawing to a file with a different name."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+Shift+s")));
253 fileCloseAct = CreateAction(tr("&Close Drawing"), tr("Close Drawing"), tr("Closes the current drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+w")));
255 settingsAct = CreateAction(tr("&Settings"), tr("Settings"), tr("Change certain defaults for Architektonas."), QIcon(":/res/generic-tool.png"), QKeySequence());
256 connect(settingsAct, SIGNAL(triggered()), this, SLOT(Settings()));
258 //Hm. I think we'll have to have separate logic to do the "Radio Group Toolbar" thing...
259 /* QActionGroup * group = new QActionGroup(this);
260 group->addAction(deleteAct);
261 group->addAction(addDimensionAct);
262 group->addAction(addLineAct);
263 group->addAction(addCircleAct);
264 group->addAction(addArcAct);//*/
268 // Consolidates action creation from a multi-step process to a single-step one.
270 QAction * ApplicationWindow::CreateAction(QString name, QString tooltip, QString statustip,
271 QIcon icon, QKeySequence key, bool checkable/*= false*/)
273 QAction * action = new QAction(icon, name, this);
274 action->setToolTip(tooltip);
275 action->setStatusTip(statustip);
276 action->setShortcut(key);
277 action->setCheckable(checkable);
283 // This is essentially the same as the previous function, but this allows more
284 // than one key sequence to be added as key shortcuts.
286 QAction * ApplicationWindow::CreateAction(QString name, QString tooltip, QString statustip,
287 QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/)
289 QAction * action = new QAction(icon, name, this);
290 action->setToolTip(tooltip);
291 action->setStatusTip(statustip);
292 QList<QKeySequence> keyList;
293 keyList.append(key1);
294 keyList.append(key2);
295 action->setShortcuts(keyList);
296 action->setCheckable(checkable);
301 void ApplicationWindow::CreateMenus(void)
303 QMenu * menu = menuBar()->addMenu(tr("&File"));
304 menu->addAction(fileNewAct);
305 menu->addAction(fileOpenAct);
306 menu->addAction(fileSaveAct);
307 menu->addAction(fileSaveAsAct);
308 menu->addAction(fileCloseAct);
309 menu->addSeparator();
310 menu->addAction(exitAct);
312 menu = menuBar()->addMenu(tr("&View"));
313 menu->addAction(zoomInAct);
314 menu->addAction(zoomOutAct);
316 menu = menuBar()->addMenu(tr("&Edit"));
317 menu->addAction(fixAngleAct);
318 menu->addAction(fixLengthAct);
319 menu->addAction(rotateAct);
320 menu->addSeparator();
321 menu->addAction(deleteAct);
322 menu->addSeparator();
323 menu->addAction(addLineAct);
324 menu->addAction(addCircleAct);
325 menu->addAction(addArcAct);
326 menu->addAction(addDimensionAct);
327 menu->addSeparator();
328 menu->addAction(settingsAct);
330 menu = menuBar()->addMenu(tr("&Help"));
331 menu->addAction(aboutAct);
334 void ApplicationWindow::CreateToolbars(void)
336 QToolBar * toolbar = addToolBar(tr("File"));
337 toolbar->addAction(exitAct);
339 toolbar = addToolBar(tr("View"));
340 toolbar->addAction(zoomInAct);
341 toolbar->addAction(zoomOutAct);
343 toolbar = addToolBar(tr("Edit"));
344 toolbar->addAction(fixAngleAct);
345 toolbar->addAction(fixLengthAct);
346 toolbar->addAction(rotateAct);
347 toolbar->addAction(deleteAct);
348 toolbar->addAction(addLineAct);
349 toolbar->addAction(addCircleAct);
350 toolbar->addAction(addArcAct);
351 toolbar->addAction(addDimensionAct);
354 void ApplicationWindow::ReadSettings(void)
356 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
357 QSize size = settings.value("size", QSize(400, 400)).toSize();
358 drawing->useAntialiasing = settings.value("useAntialiasing", true).toBool();
361 // pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
362 // size = settings.value("charWndSize", QSize(200, 200)).toSize();
363 // ((TTEdit *)qApp)->charWnd->resize(size);
364 // ((TTEdit *)qApp)->charWnd->move(pos);
367 void ApplicationWindow::WriteSettings(void)
369 settings.setValue("pos", pos());
370 settings.setValue("size", size());
371 settings.setValue("useAntialiasing", drawing->useAntialiasing);
372 // settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
373 // settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());