]> Shamusworld >> Repos - architektonas/blob - src/applicationwindow.cpp
Added preliminary zooming and panning.
[architektonas] / src / applicationwindow.cpp
1 //
2 // applicationwindow.cpp: Architektonas
3 //
4 // Part of the Architektonas Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -------------------------------------------------------------
12 // JLH  03/22/2011  Created this file
13 //
14
15 // FIXED:
16 //
17 //
18 // STILL TO BE DONE:
19 //
20 //
21
22 // Uncomment this for debugging...
23 //#define DEBUG
24 //#define DEBUGFOO                      // Various tool debugging...
25 //#define DEBUGTP                               // Toolpalette debugging...
26
27 #include "applicationwindow.h"
28
29 #include "about.h"
30 #include "drawingview.h"
31 #include "generaltab.h"
32 #include "painter.h"
33 #include "settingsdialog.h"
34
35
36 ApplicationWindow::ApplicationWindow(): settings("Underground Software", "Architektonas")
37 {
38         drawing = new DrawingView(this);
39         drawing->setMouseTracking(true);                // We want *all* mouse events...!
40         setCentralWidget(drawing);
41
42         aboutWin = new AboutWindow(this);
43
44 //      ((TTEdit *)qApp)->charWnd = new CharWindow(this);
45
46         setWindowIcon(QIcon(":/res/atns-icon.png"));
47         setWindowTitle("Architektonas");
48
49         CreateActions();
50         CreateMenus();
51         CreateToolbars();
52
53         //      Create status bar
54         statusBar()->showMessage(tr("Ready"));
55
56         ReadSettings();
57
58 //      connect(textEdit->document(), SIGNAL(contentsChanged()),
59 //                      this, SLOT(documentWasModified()));
60
61 //      setCurrentFile("");
62         setUnifiedTitleAndToolBarOnMac(true);
63
64 //      ((TTEdit *)qApp)->charWnd->show();//eh?
65         Object::SetFont(new QFont("Verdana", 15, QFont::Bold));
66 }
67
68 void ApplicationWindow::closeEvent(QCloseEvent * event)
69 {
70         WriteSettings();
71         event->accept();                                                        // Use ignore() if can't close for some reason
72         //Do we have a memory leak here if we don't delete the font in the Object???
73 }
74
75 //void ApplicationWindow::FileOpen(void)
76 //{
77 //}
78
79 void ApplicationWindow::FixAngle(void)
80 {
81         Object::SetFixedAngle(fixAngleAct->isChecked());
82 }
83
84 void ApplicationWindow::FixLength(void)
85 {
86         Object::SetFixedLength(fixLengthAct->isChecked());
87 }
88
89 void ApplicationWindow::DeleteTool(void)
90 {
91         Object::SetDeleteActive(deleteAct->isChecked());
92 }
93
94 void ApplicationWindow::DimensionTool(void)
95 {
96         Object::SetDimensionActive(addDimensionAct->isChecked());
97 }
98
99 void ApplicationWindow::RotateTool(void)
100 {
101         drawing->SetRotateToolActive(rotateAct->isChecked());
102 }
103
104 void ApplicationWindow::ZoomInTool(void)
105 {
106 //printf("Zoom in... level going from %02f to ", Painter::zoom);
107         // This just zooms leaving origin intact... should zoom in at the current center!
108 //      drawing->ZoomIn();
109         Painter::zoom *= 2.0;
110         drawing->update();
111 }
112
113 void ApplicationWindow::ZoomOutTool(void)
114 {
115 //printf("Zoom out...\n");
116         // This just zooms leaving origin intact... should zoom out at the current center!
117 //      drawing->ZoomOut();
118         Painter::zoom /= 2.0;
119         drawing->update();
120 }
121
122 void ApplicationWindow::HelpAbout(void)
123 {
124         aboutWin->show();
125 }
126
127 void ApplicationWindow::Settings(void)
128 {
129         SettingsDialog dlg(this);
130         dlg.generalTab->antialiasChk->setChecked(drawing->useAntialiasing);
131
132         if (dlg.exec() == false)
133                 return;
134
135         // Deal with stuff here (since user hit "OK" button...)
136         drawing->useAntialiasing = dlg.generalTab->antialiasChk->isChecked();
137         WriteSettings();
138 }
139
140 void ApplicationWindow::CreateActions(void)
141 {
142         exitAct = CreateAction(tr("&Quit"), tr("Quit"), tr("Exits the application."),
143                 QIcon(":/res/quit.png"), QKeySequence(tr("Ctrl+q")));
144         connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
145
146         fixAngleAct = CreateAction(tr("Fix &Angle"), tr("Fix Angle"), tr("Fixes the angle of an object."),
147                 QIcon(":/res/fix-angle.png"), QKeySequence(tr("F,A")), true);
148         connect(fixAngleAct, SIGNAL(triggered()), this, SLOT(FixAngle()));
149
150         fixLengthAct = CreateAction(tr("Fix &Length"), tr("Fix Length"), tr("Fixes the length of an object."),
151                 QIcon(":/res/fix-length.png"), QKeySequence(tr("F,L")), true);
152         connect(fixLengthAct, SIGNAL(triggered()), this, SLOT(FixLength()));
153
154         deleteAct = CreateAction(tr("&Delete"), tr("Delete Object"), tr("Deletes selected objects."), QIcon(":/res/generic-tool.png"), QKeySequence(), true);
155         connect(deleteAct, SIGNAL(triggered()), this, SLOT(DeleteTool()));
156
157         addDimensionAct = CreateAction(tr("Add &Dimension"), tr("Add Dimension"), tr("Adds a dimension to the drawing."), QIcon(":/res/dimension-tool.png"), QKeySequence("D,I"), true);
158         connect(addDimensionAct, SIGNAL(triggered()), this, SLOT(DimensionTool()));
159
160         addLineAct = CreateAction(tr("Add &Line"), tr("Add Line"), tr("Adds a line to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(), true);
161
162         addCircleAct = CreateAction(tr("Add &Circle"), tr("Add Circle"), tr("Adds a circle to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(), true);
163
164         addArcAct = CreateAction(tr("Add &Arc"), tr("Add Arc"), tr("Adds an arc to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(), true);
165
166         aboutAct = CreateAction(tr("About &Architektonas"), tr("About Architektonas"), tr("Gives information about this program."), QIcon(":/res/generic-tool.png"), QKeySequence());
167         connect(aboutAct, SIGNAL(triggered()), this, SLOT(HelpAbout()));
168
169         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);
170         connect(rotateAct, SIGNAL(triggered()), this, SLOT(RotateTool()));
171
172         zoomInAct = CreateAction(tr("Zoom &In"), tr("Zoom In"), tr("Zoom in on the document."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("=")));
173         connect(zoomInAct, SIGNAL(triggered()), this, SLOT(ZoomInTool()));
174
175         zoomOutAct = CreateAction(tr("Zoom &Out"), tr("Zoom Out"), tr("Zoom out of the document."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("-")));
176         connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(ZoomOutTool()));
177
178         fileNewAct = CreateAction(tr("&New Drawing"), tr("New Drawing"), tr("Creates a new drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+n")));
179
180         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")));
181
182         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")));
183
184         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")));
185
186         fileCloseAct = CreateAction(tr("&Close Drawing"), tr("Close Drawing"), tr("Closes the current drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+w")));
187
188         settingsAct = CreateAction(tr("&Settings"), tr("Settings"), tr("Change certain defaults for Architektonas."), QIcon(":/res/generic-tool.png"), QKeySequence());
189         connect(settingsAct, SIGNAL(triggered()), this, SLOT(Settings()));
190
191 //Hm. I think we'll have to have separate logic to do the "Radio Group Toolbar" thing...
192 /*      QActionGroup * group = new QActionGroup(this);
193         group->addAction(deleteAct);
194         group->addAction(addDimensionAct);
195         group->addAction(addLineAct);
196         group->addAction(addCircleAct);
197         group->addAction(addArcAct);//*/
198 }
199
200 //
201 // Consolidates action creation from a multi-step process to a single-step one.
202 //
203 QAction * ApplicationWindow::CreateAction(QString name, QString tooltip, QString statustip,
204         QIcon icon, QKeySequence key, bool checkable/*= false*/)
205 {
206         QAction * action = new QAction(icon, name, this);
207         action->setToolTip(tooltip);
208         action->setStatusTip(statustip);
209         action->setShortcut(key);
210         action->setCheckable(checkable);
211
212         return action;
213 }
214
215 //
216 // This is essentially the same as the previous function, but this allows more
217 // than one key sequence to be added as key shortcuts.
218 //
219 QAction * ApplicationWindow::CreateAction2(QString name, QString tooltip, QString statustip,
220         QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/)
221 {
222         QAction * action = new QAction(icon, name, this);
223         action->setToolTip(tooltip);
224         action->setStatusTip(statustip);
225         QList<QKeySequence> keyList;
226         keyList.append(key1);
227         keyList.append(key2);
228         action->setShortcuts(keyList);
229         action->setCheckable(checkable);
230
231         return action;
232 }
233
234 void ApplicationWindow::CreateMenus(void)
235 {
236         QMenu * menu = menuBar()->addMenu(tr("&File"));
237         menu->addAction(fileNewAct);
238         menu->addAction(fileOpenAct);
239         menu->addAction(fileSaveAct);
240         menu->addAction(fileSaveAsAct);
241         menu->addAction(fileCloseAct);
242         menu->addSeparator();
243         menu->addAction(exitAct);
244
245         menu = menuBar()->addMenu(tr("&View"));
246         menu->addAction(zoomInAct);
247         menu->addAction(zoomOutAct);
248
249         menu = menuBar()->addMenu(tr("&Edit"));
250         menu->addAction(fixAngleAct);
251         menu->addAction(fixLengthAct);
252         menu->addAction(rotateAct);
253         menu->addSeparator();
254         menu->addAction(deleteAct);
255         menu->addSeparator();
256         menu->addAction(addLineAct);
257         menu->addAction(addCircleAct);
258         menu->addAction(addArcAct);
259         menu->addAction(addDimensionAct);
260         menu->addSeparator();
261         menu->addAction(settingsAct);
262
263 //      editMenu = menuBar()->addMenu(tr("&Edit"));
264 //      editMenu->addAction(cutAct);
265 //      editMenu->addAction(copyAct);
266 //      editMenu->addAction(pasteAct);
267
268 //      menuBar()->addSeparator();
269
270         menu = menuBar()->addMenu(tr("&Help"));
271         menu->addAction(aboutAct);
272 //      helpMenu->addAction(aboutQtAct);
273 }
274
275 void ApplicationWindow::CreateToolbars(void)
276 {
277         QToolBar * toolbar = addToolBar(tr("File"));
278         toolbar->addAction(exitAct);
279
280         toolbar = addToolBar(tr("View"));
281         toolbar->addAction(zoomInAct);
282         toolbar->addAction(zoomOutAct);
283
284         toolbar = addToolBar(tr("Edit"));
285         toolbar->addAction(fixAngleAct);
286         toolbar->addAction(fixLengthAct);
287         toolbar->addAction(rotateAct);
288         toolbar->addAction(deleteAct);
289         toolbar->addAction(addLineAct);
290         toolbar->addAction(addCircleAct);
291         toolbar->addAction(addArcAct);
292         toolbar->addAction(addDimensionAct);
293 }
294
295 void ApplicationWindow::ReadSettings(void)
296 {
297         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
298         QSize size = settings.value("size", QSize(400, 400)).toSize();
299         drawing->useAntialiasing = settings.value("useAntialiasing", true).toBool();
300         resize(size);
301         move(pos);
302 //      pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
303 //      size = settings.value("charWndSize", QSize(200, 200)).toSize();
304 //      ((TTEdit *)qApp)->charWnd->resize(size);
305 //      ((TTEdit *)qApp)->charWnd->move(pos);
306 }
307
308 void ApplicationWindow::WriteSettings(void)
309 {
310         settings.setValue("pos", pos());
311         settings.setValue("size", size());
312         settings.setValue("useAntialiasing", drawing->useAntialiasing);
313 //      settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
314 //      settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
315 }