Fri, 15 Mar 2013 21:53:50 +0200
Set window title dynamically based on filename
0 | 1 | #include <QtGui> |
2 | ||
3 | #include "common.h" | |
4 | #include "draw.h" | |
5 | #include "gui.h" | |
6 | #include "model.h" | |
7 | #include "io.h" | |
8 | ||
9 | LDForgeWindow::LDForgeWindow () { | |
10 | R = new renderer; | |
11 | ||
12 | qObjList = new QTreeWidget; | |
13 | qObjList->setHeaderHidden (true); | |
14 | qObjList->setMaximumWidth (256); | |
15 | ||
16 | qMessageLog = new QTextEdit; | |
17 | qMessageLog->setReadOnly (true); | |
18 | qMessageLog->setMaximumHeight (96); | |
19 | ||
20 | QWidget* w = new QWidget; | |
21 | QGridLayout* layout = new QGridLayout; | |
22 | layout->setColumnMinimumWidth (0, 192); | |
23 | layout->setColumnStretch (0, 1); | |
24 | layout->addWidget (R, 0, 0); | |
25 | layout->addWidget (qObjList, 0, 1); | |
26 | layout->addWidget (qMessageLog, 1, 0, 1, 2); | |
27 | w->setLayout (layout); | |
28 | setCentralWidget (w); | |
29 | ||
30 | createMenuActions (); | |
31 | createMenus (); | |
32 | createToolbars (); | |
33 | ||
7
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
34 | setTitle (); |
0 | 35 | setMinimumSize (320, 200); |
36 | resize (800, 600); | |
37 | } | |
38 | ||
39 | #define MAKE_ACTION(OBJECT, DISPLAYNAME, IMAGENAME, DESCR) \ | |
40 | qAct_##OBJECT = new QAction (QIcon ("./icons/" IMAGENAME ".png"), tr (DISPLAYNAME), this); \ | |
41 | qAct_##OBJECT->setStatusTip (tr (DESCR)); \ | |
42 | connect (qAct_##OBJECT, SIGNAL (triggered ()), this, SLOT (slot_##OBJECT ())); | |
43 | ||
44 | void LDForgeWindow::createMenuActions () { | |
45 | // Long menu names go here so my cool action definition table doesn't get out of proportions | |
46 | const char* sNewCdLineText = "New Conditional Line", | |
47 | *sNewQuadText = "New Quadrilateral", | |
48 | *sAboutText = "About " APPNAME_DISPLAY; | |
49 | ||
50 | MAKE_ACTION (new, "&New", "file-new", "Create a new part model.") | |
5
727e02d6b87a
rename file-specific icons to file-*
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
51 | MAKE_ACTION (open, "&Open", "file-open", "Load a part model from a file.") |
727e02d6b87a
rename file-specific icons to file-*
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
52 | MAKE_ACTION (save, "&Save", "file-save", "Save the part model.") |
727e02d6b87a
rename file-specific icons to file-*
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
53 | MAKE_ACTION (saveAs, "Save &As", "file-save-as", "Save the part to a specific file.") |
0 | 54 | MAKE_ACTION (exit, "&Exit", "exit", "Close " APPNAME_DISPLAY ".") |
55 | ||
56 | MAKE_ACTION (cut, "Cut", "cut", "Cut the current selection to clipboard.") | |
57 | MAKE_ACTION (copy, "Copy", "copy", "Copy the current selection to clipboard.") | |
58 | MAKE_ACTION (paste, "Paste", "paste", "Paste clipboard contents.") | |
59 | MAKE_ACTION (about, sAboutText, "about", "Shows information about " APPNAME_DISPLAY ".") | |
60 | MAKE_ACTION (aboutQt, "About Qt", "aboutQt", "Shows information about Qt.") | |
61 | ||
62 | MAKE_ACTION (newSubfile, "New Subfile", "add-subfile", "Creates a new subfile reference.") | |
63 | MAKE_ACTION (newLine, "New Line", "add-line", "Creates a new line.") | |
64 | MAKE_ACTION (newTriangle, "New Triangle", "add-triangle", "Creates a new triangle.") | |
65 | MAKE_ACTION (newQuad, sNewQuadText, "add-quad", "Creates a new quadrilateral.") | |
66 | MAKE_ACTION (newCondLine, sNewCdLineText, "add-condline", "Creates a new conditional line."); | |
67 | MAKE_ACTION (newComment, "New Comment", "add-comment", "Creates a new comment."); | |
68 | MAKE_ACTION (newVector, "New Vector", "add-vector", "Creates a new vector.") | |
69 | MAKE_ACTION (newVertex, "New Vertex", "add-vertex", "Creates a new vertex.") | |
70 | ||
71 | // Keyboard shortcuts | |
72 | qAct_new->setShortcut (Qt::CTRL | Qt::Key_N); | |
73 | qAct_open->setShortcut (Qt::CTRL | Qt::Key_O); | |
74 | qAct_save->setShortcut (Qt::CTRL | Qt::Key_S); | |
75 | qAct_saveAs->setShortcut (Qt::CTRL | Qt::SHIFT | Qt::Key_S); | |
76 | ||
77 | qAct_cut->setShortcut (Qt::CTRL | Qt::Key_X); | |
78 | qAct_copy->setShortcut (Qt::CTRL | Qt::Key_C); | |
79 | qAct_paste->setShortcut (Qt::CTRL | Qt::Key_V); | |
80 | ||
81 | // things not implemented yet | |
82 | QAction* qaDisabledActions[] = { | |
83 | qAct_save, | |
84 | qAct_saveAs, | |
85 | qAct_newSubfile, | |
86 | qAct_newLine, | |
87 | qAct_newTriangle, | |
88 | qAct_newQuad, | |
89 | qAct_newCondLine, | |
90 | qAct_newComment, | |
91 | qAct_newVector, | |
92 | qAct_newVertex, | |
93 | qAct_cut, | |
94 | qAct_copy, | |
95 | qAct_paste, | |
96 | qAct_about | |
97 | }; | |
98 | ||
99 | for (ushort i = 0; i < sizeof qaDisabledActions / sizeof *qaDisabledActions; ++i) | |
100 | qaDisabledActions[i]->setEnabled (false); | |
101 | } | |
102 | ||
103 | void LDForgeWindow::createMenus () { | |
104 | // File menu | |
105 | qFileMenu = menuBar ()->addMenu (tr ("&File")); | |
106 | qFileMenu->addAction (qAct_new); // New | |
107 | qFileMenu->addAction (qAct_open); // Open | |
108 | qFileMenu->addAction (qAct_save); // Save | |
109 | qFileMenu->addAction (qAct_saveAs); // Save As | |
110 | qFileMenu->addSeparator (); // ------- | |
111 | qFileMenu->addAction (qAct_exit); // Exit | |
112 | ||
113 | // Edit menu | |
114 | qInsertMenu = menuBar ()->addMenu (tr ("&Insert")); | |
115 | qInsertMenu->addAction (qAct_newSubfile); // New Subfile | |
116 | qInsertMenu->addAction (qAct_newLine); // New Line | |
117 | qInsertMenu->addAction (qAct_newTriangle); // New Triangle | |
118 | qInsertMenu->addAction (qAct_newQuad); // New Quad | |
119 | qInsertMenu->addAction (qAct_newCondLine); // New Conditional Line | |
120 | qInsertMenu->addAction (qAct_newComment); // New Comment | |
121 | qInsertMenu->addAction (qAct_newVector); // New Vector | |
122 | qInsertMenu->addAction (qAct_newVertex); // New Vertex | |
123 | ||
124 | qEditMenu = menuBar ()->addMenu (tr ("&Edit")); | |
125 | qEditMenu->addAction (qAct_cut); // Cut | |
126 | qEditMenu->addAction (qAct_copy); // Copy | |
127 | qEditMenu->addAction (qAct_paste); // Paste | |
128 | ||
129 | // Help menu | |
130 | qHelpMenu = menuBar ()->addMenu (tr ("&Help")); | |
131 | qHelpMenu->addAction (qAct_about); // About | |
132 | qHelpMenu->addAction (qAct_aboutQt); // About Qt | |
133 | } | |
134 | ||
135 | void LDForgeWindow::createToolbars () { | |
136 | qFileToolBar = new QToolBar ("File"); | |
137 | qFileToolBar->addAction (qAct_new); | |
138 | qFileToolBar->addAction (qAct_open); | |
139 | qFileToolBar->addAction (qAct_save); | |
140 | qFileToolBar->addAction (qAct_saveAs); | |
141 | addToolBar (qFileToolBar); | |
142 | ||
143 | qInsertToolBar = new QToolBar ("Insert"); | |
144 | qInsertToolBar->addAction (qAct_newSubfile); | |
145 | qInsertToolBar->addAction (qAct_newLine); | |
146 | qInsertToolBar->addAction (qAct_newTriangle); | |
147 | qInsertToolBar->addAction (qAct_newQuad); | |
148 | qInsertToolBar->addAction (qAct_newCondLine); | |
149 | qInsertToolBar->addAction (qAct_newComment); | |
150 | qInsertToolBar->addAction (qAct_newVector); | |
151 | qInsertToolBar->addAction (qAct_newVertex); | |
152 | addToolBar (qInsertToolBar); | |
153 | ||
154 | qEditToolBar = new QToolBar ("Edit"); | |
155 | qEditToolBar->addAction (qAct_cut); | |
156 | qEditToolBar->addAction (qAct_copy); | |
157 | qEditToolBar->addAction (qAct_paste); | |
158 | addToolBar (qEditToolBar); | |
159 | } | |
160 | ||
7
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
161 | void LDForgeWindow::setTitle () { |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
162 | str zTitle = APPNAME_DISPLAY " v" VERSION_STRING; |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
163 | |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
164 | // Append our current file if we have one |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
165 | if (g_CurrentFile) { |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
166 | zTitle.appendformat (": %s", basename (g_CurrentFile->zFileName.chars())); |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
167 | |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
168 | if (g_CurrentFile->objects.size() > 0 && |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
169 | g_CurrentFile->objects[0]->getType() == OBJ_Comment) |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
170 | { |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
171 | // Append title |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
172 | LDComment* comm = static_cast<LDComment*> (g_CurrentFile->objects[0]); |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
173 | zTitle.appendformat (":%s", comm->zText.chars()); |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
174 | } |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
175 | } |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
176 | |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
177 | setWindowTitle (zTitle.chars()); |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
178 | } |
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
179 | |
0 | 180 | void LDForgeWindow::slot_new () { |
181 | printf ("new file\n"); | |
182 | ||
183 | closeModel (); | |
184 | newModel (); | |
185 | } | |
186 | ||
187 | void LDForgeWindow::slot_open () { | |
4
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
188 | str name = QFileDialog::getOpenFileName (this, "Open File", |
0 | 189 | "", "LDraw files (*.dat *.ldr)").toStdString().c_str(); |
190 | ||
191 | if (g_LoadedFiles.size()) | |
192 | closeModel (); | |
193 | ||
4
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
194 | IO_OpenLDrawFile (name); |
0 | 195 | } |
196 | ||
197 | void LDForgeWindow::slot_save () { | |
198 | printf ("save file\n"); | |
199 | } | |
200 | ||
201 | void LDForgeWindow::slot_saveAs () { | |
202 | printf ("save as file\n"); | |
203 | } | |
204 | ||
205 | void LDForgeWindow::slot_exit () { | |
206 | printf ("exit\n"); | |
207 | exit (0); | |
208 | } | |
209 | ||
210 | void LDForgeWindow::slot_newSubfile () { | |
211 | ||
212 | } | |
213 | ||
214 | void LDForgeWindow::slot_newLine () { | |
215 | ||
216 | } | |
217 | ||
218 | void LDForgeWindow::slot_newTriangle () { | |
219 | ||
220 | } | |
221 | ||
222 | void LDForgeWindow::slot_newQuad () { | |
223 | ||
224 | } | |
225 | ||
226 | void LDForgeWindow::slot_newCondLine () { | |
227 | ||
228 | } | |
229 | ||
230 | void LDForgeWindow::slot_newComment () { | |
231 | ||
232 | } | |
233 | ||
234 | void LDForgeWindow::slot_about () { | |
235 | ||
236 | } | |
237 | ||
238 | void LDForgeWindow::slot_aboutQt () { | |
239 | QMessageBox::aboutQt (this); | |
240 | } | |
241 | ||
242 | void LDForgeWindow::slot_cut () { | |
243 | ||
244 | } | |
245 | ||
246 | void LDForgeWindow::slot_copy () { | |
247 | ||
248 | } | |
249 | ||
250 | void LDForgeWindow::slot_paste () { | |
251 | ||
252 | } | |
253 | ||
254 | void LDForgeWindow::slot_newVector () { | |
255 | ||
256 | } | |
257 | ||
258 | void LDForgeWindow::slot_newVertex () { | |
259 | ||
260 | } | |
261 | ||
262 | ||
263 | static QIcon IconForObjectType (LDObject* obj) { | |
264 | switch (obj->getType ()) { | |
265 | case OBJ_Empty: | |
266 | return QIcon ("icons/empty.png"); | |
267 | ||
268 | case OBJ_Line: | |
269 | return QIcon ("icons/line.png"); | |
270 | ||
271 | case OBJ_Quad: | |
272 | return QIcon ("icons/quad.png"); | |
273 | ||
274 | case OBJ_Subfile: | |
275 | return QIcon ("icons/subfile.png"); | |
276 | ||
277 | case OBJ_Triangle: | |
278 | return QIcon ("icons/triangle.png"); | |
279 | ||
280 | case OBJ_CondLine: | |
281 | return QIcon ("icons/condline.png"); | |
282 | ||
283 | case OBJ_Comment: | |
284 | return QIcon ("icons/comment.png"); | |
285 | ||
286 | case OBJ_Vector: | |
287 | return QIcon ("icons/vector.png"); | |
288 | ||
289 | case OBJ_Vertex: | |
290 | return QIcon ("icons/vertex.png"); | |
291 | ||
292 | case OBJ_Gibberish: | |
293 | case OBJ_Unidentified: | |
294 | return QIcon ("icons/error.png"); | |
295 | } | |
296 | ||
297 | return QIcon (); | |
298 | } | |
299 | ||
300 | void LDForgeWindow::buildObjList () { | |
3
2b78cf8634c3
don't crash if g_CurrentFile is null
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
301 | if (!g_CurrentFile) |
2b78cf8634c3
don't crash if g_CurrentFile is null
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
302 | return; |
2b78cf8634c3
don't crash if g_CurrentFile is null
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
303 | |
0 | 304 | QList<QTreeWidgetItem*> qaItems; |
3
2b78cf8634c3
don't crash if g_CurrentFile is null
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
305 | |
2b78cf8634c3
don't crash if g_CurrentFile is null
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
306 | qObjList->clear (); |
2b78cf8634c3
don't crash if g_CurrentFile is null
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
307 | |
0 | 308 | for (ushort i = 0; i < g_CurrentFile->objects.size(); ++i) { |
309 | LDObject* obj = g_CurrentFile->objects[i]; | |
310 | ||
311 | str zText; | |
312 | switch (obj->getType ()) { | |
313 | case OBJ_Comment: | |
314 | zText = static_cast<LDComment*> (obj)->zText; | |
315 | break; | |
316 | ||
317 | case OBJ_Empty: | |
318 | break; // leave it empty | |
319 | ||
320 | case OBJ_Line: | |
321 | case OBJ_CondLine: | |
322 | { | |
323 | LDLine* line = static_cast<LDLine*> (obj); | |
324 | zText.format ("%s, %s", | |
325 | line->vaCoords[0].getStringRep ().chars(), | |
326 | line->vaCoords[1].getStringRep ().chars()); | |
327 | } | |
328 | break; | |
329 | ||
330 | case OBJ_Triangle: | |
331 | { | |
332 | LDTriangle* triangle = static_cast<LDTriangle*> (obj); | |
333 | zText.format ("%s, %s, %s", | |
334 | triangle->vaCoords[0].getStringRep ().chars(), | |
335 | triangle->vaCoords[1].getStringRep ().chars(), | |
336 | triangle->vaCoords[2].getStringRep ().chars()); | |
337 | } | |
338 | break; | |
339 | ||
340 | case OBJ_Quad: | |
341 | { | |
342 | LDQuad* quad = static_cast<LDQuad*> (obj); | |
343 | zText.format ("%s, %s, %s", | |
344 | quad->vaCoords[0].getStringRep ().chars(), | |
345 | quad->vaCoords[1].getStringRep ().chars(), | |
346 | quad->vaCoords[2].getStringRep ().chars(), | |
347 | quad->vaCoords[3].getStringRep ().chars()); | |
348 | } | |
349 | break; | |
350 | ||
351 | case OBJ_Gibberish: | |
352 | zText.format ("ERROR: %s", | |
353 | static_cast<LDGibberish*> (obj)->zContent.chars()); | |
354 | break; | |
355 | ||
356 | case OBJ_Vector: | |
357 | zText.format ("%s", static_cast<LDVector*> (obj)->vPos.getStringRep().chars()); | |
358 | break; | |
359 | ||
360 | case OBJ_Vertex: | |
361 | zText.format ("%s", static_cast<LDVertex*> (obj)->vPosition.getStringRep().chars()); | |
362 | break; | |
363 | ||
364 | default: | |
365 | zText = g_saObjTypeNames[obj->getType ()]; | |
366 | break; | |
367 | } | |
368 | ||
369 | QTreeWidgetItem* item = new QTreeWidgetItem ((QTreeWidget*)nullptr, | |
370 | QStringList (zText.chars()), 0); | |
371 | item->setIcon (0, IconForObjectType (obj)); | |
372 | ||
373 | qaItems.append (item); | |
374 | } | |
375 | ||
376 | printf ("insert top level items\n"); | |
377 | qObjList->insertTopLevelItems (0, qaItems); | |
378 | } |