1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include <QFileDialog> |
|
20 #include <QMessageBox> |
|
21 #include <QTextEdit> |
|
22 #include <QBoxLayout> |
|
23 #include <QDialogButtonBox> |
|
24 #include <QPushButton> |
|
25 #include <QInputDialog> |
|
26 |
|
27 #include "mainWindow.h" |
|
28 #include "ldDocument.h" |
|
29 #include "editHistory.h" |
|
30 #include "configDialog.h" |
|
31 #include "addObjectDialog.h" |
|
32 #include "miscallenous.h" |
|
33 #include "glRenderer.h" |
|
34 #include "dialogs.h" |
|
35 #include "primitives.h" |
|
36 #include "radioGroup.h" |
|
37 #include "colors.h" |
|
38 #include "glCompiler.h" |
|
39 #include "ui_newpart.h" |
|
40 #include "editmodes/abstractEditMode.h" |
|
41 |
|
42 EXTERN_CFGENTRY (Bool, DrawWireframe) |
|
43 EXTERN_CFGENTRY (Bool, BFCRedGreenView) |
|
44 EXTERN_CFGENTRY (String, DefaultName) |
|
45 EXTERN_CFGENTRY (String, DefaultUser) |
|
46 EXTERN_CFGENTRY (Bool, UseCALicense) |
|
47 EXTERN_CFGENTRY (Bool, DrawAngles) |
|
48 EXTERN_CFGENTRY (Bool, RandomColors) |
|
49 EXTERN_CFGENTRY (Bool, DrawSurfaces) |
|
50 EXTERN_CFGENTRY (Bool, DrawEdgeLines) |
|
51 EXTERN_CFGENTRY (Bool, DrawConditionalLines) |
|
52 EXTERN_CFGENTRY (Bool, DrawAxes) |
|
53 |
|
54 // ============================================================================= |
|
55 // |
|
56 void MainWindow::slot_actionNew() |
|
57 { |
|
58 QDialog* dlg = new QDialog (g_win); |
|
59 Ui::NewPartUI ui; |
|
60 ui.setupUi (dlg); |
|
61 |
|
62 QString authortext = cfg::DefaultName; |
|
63 |
|
64 if (not cfg::DefaultUser.isEmpty()) |
|
65 authortext.append (format (" [%1]", cfg::DefaultUser)); |
|
66 |
|
67 ui.le_author->setText (authortext); |
|
68 ui.caLicense->setChecked (cfg::UseCALicense); |
|
69 |
|
70 if (dlg->exec() == QDialog::Rejected) |
|
71 return; |
|
72 |
|
73 newFile(); |
|
74 |
|
75 BFCStatement const bfctype = ui.rb_bfc_ccw->isChecked() ? BFCStatement::CertifyCCW |
|
76 : ui.rb_bfc_cw->isChecked() ? BFCStatement::CertifyCW |
|
77 : BFCStatement::NoCertify; |
|
78 QString const license = ui.caLicense->isChecked() ? CALicenseText : ""; |
|
79 |
|
80 LDObjectList objs; |
|
81 objs << LDSpawn<LDComment> (ui.le_title->text()); |
|
82 objs << LDSpawn<LDComment> ("Name: <untitled>.dat"); |
|
83 objs << LDSpawn<LDComment> (format ("Author: %1", ui.le_author->text())); |
|
84 objs << LDSpawn<LDComment> (format ("!LDRAW_ORG Unofficial_Part")); |
|
85 |
|
86 if (not license.isEmpty()) |
|
87 objs << LDSpawn<LDComment> (license); |
|
88 |
|
89 objs << LDSpawn<LDEmpty>(); |
|
90 objs << LDSpawn<LDBFC> (bfctype); |
|
91 objs << LDSpawn<LDEmpty>(); |
|
92 CurrentDocument()->addObjects (objs); |
|
93 doFullRefresh(); |
|
94 } |
|
95 |
|
96 // ============================================================================= |
|
97 // |
|
98 void MainWindow::slot_actionNewFile() |
|
99 { |
|
100 newFile(); |
|
101 } |
|
102 |
|
103 // ============================================================================= |
|
104 // |
|
105 void MainWindow::slot_actionOpen() |
|
106 { |
|
107 QString name = QFileDialog::getOpenFileName (g_win, "Open File", "", "LDraw files (*.dat *.ldr)"); |
|
108 |
|
109 if (name.isEmpty()) |
|
110 return; |
|
111 |
|
112 OpenMainModel (name); |
|
113 } |
|
114 |
|
115 // ============================================================================= |
|
116 // |
|
117 void MainWindow::slot_actionSave() |
|
118 { |
|
119 save (CurrentDocument(), false); |
|
120 } |
|
121 |
|
122 // ============================================================================= |
|
123 // |
|
124 void MainWindow::slot_actionSaveAs() |
|
125 { |
|
126 save (CurrentDocument(), true); |
|
127 } |
|
128 |
|
129 // ============================================================================= |
|
130 // |
|
131 void MainWindow::slot_actionSaveAll() |
|
132 { |
|
133 for (LDDocument* file : LDDocument::explicitDocuments()) |
|
134 save (file, false); |
|
135 } |
|
136 |
|
137 // ============================================================================= |
|
138 // |
|
139 void MainWindow::slot_actionClose() |
|
140 { |
|
141 if (not CurrentDocument()->isSafeToClose()) |
|
142 return; |
|
143 |
|
144 CurrentDocument()->dismiss(); |
|
145 } |
|
146 |
|
147 // ============================================================================= |
|
148 // |
|
149 void MainWindow::slot_actionCloseAll() |
|
150 { |
|
151 if (not IsSafeToCloseAll()) |
|
152 return; |
|
153 |
|
154 CloseAllDocuments(); |
|
155 } |
|
156 |
|
157 // ============================================================================= |
|
158 // |
|
159 void MainWindow::slot_actionSettings() |
|
160 { |
|
161 (new ConfigDialog)->exec(); |
|
162 } |
|
163 |
|
164 // ============================================================================= |
|
165 // |
|
166 void MainWindow::slot_actionSetLDrawPath() |
|
167 { |
|
168 (new LDrawPathDialog (true))->exec(); |
|
169 } |
|
170 |
|
171 // ============================================================================= |
|
172 // |
|
173 void MainWindow::slot_actionExit() |
|
174 { |
|
175 Exit(); |
|
176 } |
|
177 |
|
178 // ============================================================================= |
|
179 // |
|
180 void MainWindow::slot_actionNewSubfile() |
|
181 { |
|
182 AddObjectDialog::staticDialog (OBJ_Subfile, nullptr); |
|
183 } |
|
184 |
|
185 // ============================================================================= |
|
186 // |
|
187 void MainWindow::slot_actionNewLine() |
|
188 { |
|
189 AddObjectDialog::staticDialog (OBJ_Line, nullptr); |
|
190 } |
|
191 |
|
192 // ============================================================================= |
|
193 // |
|
194 void MainWindow::slot_actionNewTriangle() |
|
195 { |
|
196 AddObjectDialog::staticDialog (OBJ_Triangle, nullptr); |
|
197 } |
|
198 |
|
199 // ============================================================================= |
|
200 // |
|
201 void MainWindow::slot_actionNewQuad() |
|
202 { |
|
203 AddObjectDialog::staticDialog (OBJ_Quad, nullptr); |
|
204 } |
|
205 |
|
206 // ============================================================================= |
|
207 // |
|
208 void MainWindow::slot_actionNewCLine() |
|
209 { |
|
210 AddObjectDialog::staticDialog (OBJ_CondLine, nullptr); |
|
211 } |
|
212 |
|
213 // ============================================================================= |
|
214 // |
|
215 void MainWindow::slot_actionNewComment() |
|
216 { |
|
217 AddObjectDialog::staticDialog (OBJ_Comment, nullptr); |
|
218 } |
|
219 |
|
220 // ============================================================================= |
|
221 // |
|
222 void MainWindow::slot_actionNewBFC() |
|
223 { |
|
224 AddObjectDialog::staticDialog (OBJ_BFC, nullptr); |
|
225 } |
|
226 |
|
227 // ============================================================================= |
|
228 // |
|
229 void MainWindow::slot_actionNewVertex() |
|
230 { |
|
231 AddObjectDialog::staticDialog (OBJ_Vertex, nullptr); |
|
232 } |
|
233 |
|
234 // ============================================================================= |
|
235 // |
|
236 void MainWindow::slot_actionEdit() |
|
237 { |
|
238 if (Selection().size() != 1) |
|
239 return; |
|
240 |
|
241 LDObject* obj = Selection() [0]; |
|
242 AddObjectDialog::staticDialog (obj->type(), obj); |
|
243 } |
|
244 |
|
245 // ============================================================================= |
|
246 // |
|
247 void MainWindow::slot_actionHelp() |
|
248 { |
|
249 } |
|
250 |
|
251 // ============================================================================= |
|
252 // |
|
253 void MainWindow::slot_actionAbout() |
|
254 { |
|
255 AboutDialog().exec(); |
|
256 } |
|
257 |
|
258 // ============================================================================= |
|
259 // |
|
260 void MainWindow::slot_actionAboutQt() |
|
261 { |
|
262 QMessageBox::aboutQt (g_win); |
|
263 } |
|
264 |
|
265 // ============================================================================= |
|
266 // |
|
267 void MainWindow::slot_actionSelectAll() |
|
268 { |
|
269 for (LDObject* obj : CurrentDocument()->objects()) |
|
270 obj->select(); |
|
271 } |
|
272 |
|
273 // ============================================================================= |
|
274 // |
|
275 void MainWindow::slot_actionSelectByColor() |
|
276 { |
|
277 if (Selection().isEmpty()) |
|
278 return; |
|
279 |
|
280 QList<LDColor> colors; |
|
281 |
|
282 for (LDObject* obj : Selection()) |
|
283 { |
|
284 if (obj->isColored()) |
|
285 colors << obj->color(); |
|
286 } |
|
287 |
|
288 RemoveDuplicates (colors); |
|
289 CurrentDocument()->clearSelection(); |
|
290 |
|
291 for (LDObject* obj : CurrentDocument()->objects()) |
|
292 { |
|
293 if (colors.contains (obj->color())) |
|
294 obj->select(); |
|
295 } |
|
296 } |
|
297 |
|
298 // ============================================================================= |
|
299 // |
|
300 void MainWindow::slot_actionSelectByType() |
|
301 { |
|
302 if (Selection().isEmpty()) |
|
303 return; |
|
304 |
|
305 QList<LDObjectType> types; |
|
306 QStringList subfilenames; |
|
307 |
|
308 for (LDObject* obj : Selection()) |
|
309 { |
|
310 types << obj->type(); |
|
311 |
|
312 if (types.last() == OBJ_Subfile) |
|
313 subfilenames << static_cast<LDSubfile*> (obj)->fileInfo()->name(); |
|
314 } |
|
315 |
|
316 RemoveDuplicates (types); |
|
317 RemoveDuplicates (subfilenames); |
|
318 CurrentDocument()->clearSelection(); |
|
319 |
|
320 for (LDObject* obj : CurrentDocument()->objects()) |
|
321 { |
|
322 LDObjectType type = obj->type(); |
|
323 |
|
324 if (not types.contains (type)) |
|
325 continue; |
|
326 |
|
327 // For subfiles, type check is not enough, we check the name of the document as well. |
|
328 if (type == OBJ_Subfile and not subfilenames.contains (static_cast<LDSubfile*> (obj)->fileInfo()->name())) |
|
329 continue; |
|
330 |
|
331 obj->select(); |
|
332 } |
|
333 } |
|
334 |
|
335 // ============================================================================= |
|
336 // |
|
337 void MainWindow::slot_actionGridCoarse() |
|
338 { |
|
339 cfg::Grid = Grid::Coarse; |
|
340 updateGridToolBar(); |
|
341 } |
|
342 |
|
343 void MainWindow::slot_actionGridMedium() |
|
344 { |
|
345 cfg::Grid = Grid::Medium; |
|
346 updateGridToolBar(); |
|
347 } |
|
348 |
|
349 void MainWindow::slot_actionGridFine() |
|
350 { |
|
351 cfg::Grid = Grid::Fine; |
|
352 updateGridToolBar(); |
|
353 } |
|
354 |
|
355 // ============================================================================= |
|
356 // |
|
357 void MainWindow::slot_actionResetView() |
|
358 { |
|
359 R()->resetAngles(); |
|
360 R()->update(); |
|
361 } |
|
362 |
|
363 // ============================================================================= |
|
364 // |
|
365 void MainWindow::slot_actionInsertFrom() |
|
366 { |
|
367 QString fname = QFileDialog::getOpenFileName(); |
|
368 int idx = getInsertionPoint(); |
|
369 |
|
370 if (not fname.length()) |
|
371 return; |
|
372 |
|
373 QFile f (fname); |
|
374 |
|
375 if (not f.open (QIODevice::ReadOnly)) |
|
376 { |
|
377 Critical (format ("Couldn't open %1 (%2)", fname, f.errorString())); |
|
378 return; |
|
379 } |
|
380 |
|
381 LDObjectList objs = LoadFileContents (&f, null); |
|
382 |
|
383 CurrentDocument()->clearSelection(); |
|
384 |
|
385 for (LDObject* obj : objs) |
|
386 { |
|
387 CurrentDocument()->insertObj (idx, obj); |
|
388 obj->select(); |
|
389 R()->compileObject (obj); |
|
390 |
|
391 idx++; |
|
392 } |
|
393 |
|
394 refresh(); |
|
395 scrollToSelection(); |
|
396 } |
|
397 |
|
398 // ============================================================================= |
|
399 // |
|
400 void MainWindow::slot_actionExportTo() |
|
401 { |
|
402 if (Selection().isEmpty()) |
|
403 return; |
|
404 |
|
405 QString fname = QFileDialog::getSaveFileName(); |
|
406 |
|
407 if (fname.length() == 0) |
|
408 return; |
|
409 |
|
410 QFile file (fname); |
|
411 |
|
412 if (not file.open (QIODevice::WriteOnly | QIODevice::Text)) |
|
413 { |
|
414 Critical (format ("Unable to open %1 for writing (%2)", fname, file.errorString())); |
|
415 return; |
|
416 } |
|
417 |
|
418 for (LDObject* obj : Selection()) |
|
419 { |
|
420 QString contents = obj->asText(); |
|
421 QByteArray data = contents.toUtf8(); |
|
422 file.write (data, data.size()); |
|
423 file.write ("\r\n", 2); |
|
424 } |
|
425 } |
|
426 |
|
427 // ============================================================================= |
|
428 // |
|
429 void MainWindow::slot_actionInsertRaw() |
|
430 { |
|
431 int idx = getInsertionPoint(); |
|
432 |
|
433 QDialog* const dlg = new QDialog; |
|
434 QVBoxLayout* const layout = new QVBoxLayout; |
|
435 QTextEdit* const te_edit = new QTextEdit; |
|
436 QDialogButtonBox* const bbx_buttons = new QDialogButtonBox (QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
|
437 |
|
438 layout->addWidget (te_edit); |
|
439 layout->addWidget (bbx_buttons); |
|
440 dlg->setLayout (layout); |
|
441 dlg->setWindowTitle (APPNAME " - Insert Raw"); |
|
442 dlg->connect (bbx_buttons, SIGNAL (accepted()), dlg, SLOT (accept())); |
|
443 dlg->connect (bbx_buttons, SIGNAL (rejected()), dlg, SLOT (reject())); |
|
444 |
|
445 if (dlg->exec() == QDialog::Rejected) |
|
446 return; |
|
447 |
|
448 CurrentDocument()->clearSelection(); |
|
449 |
|
450 for (QString line : QString (te_edit->toPlainText()).split ("\n")) |
|
451 { |
|
452 LDObject* obj = ParseLine (line); |
|
453 |
|
454 CurrentDocument()->insertObj (idx, obj); |
|
455 obj->select(); |
|
456 idx++; |
|
457 } |
|
458 |
|
459 refresh(); |
|
460 scrollToSelection(); |
|
461 } |
|
462 |
|
463 // ============================================================================= |
|
464 // |
|
465 void MainWindow::slot_actionScreenshot() |
|
466 { |
|
467 setlocale (LC_ALL, "C"); |
|
468 |
|
469 int w, h; |
|
470 uchar* imgdata = R()->getScreencap (w, h); |
|
471 QImage img = GetImageFromScreencap (imgdata, w, h); |
|
472 |
|
473 QString root = Basename (CurrentDocument()->name()); |
|
474 |
|
475 if (root.right (4) == ".dat") |
|
476 root.chop (4); |
|
477 |
|
478 QString defaultname = (root.length() > 0) ? format ("%1.png", root) : ""; |
|
479 QString fname = QFileDialog::getSaveFileName (g_win, "Save Screencap", defaultname, |
|
480 "PNG images (*.png);;JPG images (*.jpg);;BMP images (*.bmp);;All Files (*.*)"); |
|
481 |
|
482 if (not fname.isEmpty() and not img.save (fname)) |
|
483 Critical (format ("Couldn't open %1 for writing to save screencap: %2", fname, strerror (errno))); |
|
484 |
|
485 delete[] imgdata; |
|
486 } |
|
487 |
|
488 // ============================================================================= |
|
489 // |
|
490 void MainWindow::slot_actionAxes() |
|
491 { |
|
492 cfg::DrawAxes = not cfg::DrawAxes; |
|
493 updateActions(); |
|
494 R()->update(); |
|
495 } |
|
496 |
|
497 // ============================================================================= |
|
498 // |
|
499 void MainWindow::slot_actionVisibilityToggle() |
|
500 { |
|
501 for (LDObject* obj : Selection()) |
|
502 obj->setHidden (not obj->isHidden()); |
|
503 |
|
504 refresh(); |
|
505 } |
|
506 |
|
507 // ============================================================================= |
|
508 // |
|
509 void MainWindow::slot_actionVisibilityHide() |
|
510 { |
|
511 for (LDObject* obj : Selection()) |
|
512 obj->setHidden (true); |
|
513 |
|
514 refresh(); |
|
515 } |
|
516 |
|
517 // ============================================================================= |
|
518 // |
|
519 void MainWindow::slot_actionVisibilityReveal() |
|
520 { |
|
521 for (LDObject* obj : Selection()) |
|
522 obj->setHidden (false); |
|
523 refresh(); |
|
524 } |
|
525 |
|
526 // ============================================================================= |
|
527 // |
|
528 void MainWindow::slot_actionWireframe() |
|
529 { |
|
530 cfg::DrawWireframe = not cfg::DrawWireframe; |
|
531 R()->refresh(); |
|
532 } |
|
533 |
|
534 // ============================================================================= |
|
535 // |
|
536 void MainWindow::slot_actionSetOverlay() |
|
537 { |
|
538 OverlayDialog dlg; |
|
539 |
|
540 if (not dlg.exec()) |
|
541 return; |
|
542 |
|
543 R()->setupOverlay ((ECamera) dlg.camera(), dlg.fpath(), dlg.ofsx(), |
|
544 dlg.ofsy(), dlg.lwidth(), dlg.lheight()); |
|
545 } |
|
546 |
|
547 // ============================================================================= |
|
548 // |
|
549 void MainWindow::slot_actionClearOverlay() |
|
550 { |
|
551 R()->clearOverlay(); |
|
552 } |
|
553 |
|
554 // ============================================================================= |
|
555 // |
|
556 void MainWindow::slot_actionModeSelect() |
|
557 { |
|
558 R()->setEditMode (EditModeType::Select); |
|
559 } |
|
560 |
|
561 // ============================================================================= |
|
562 // |
|
563 void MainWindow::slot_actionModeDraw() |
|
564 { |
|
565 R()->setEditMode (EditModeType::Draw); |
|
566 } |
|
567 |
|
568 // ============================================================================= |
|
569 // |
|
570 void MainWindow::slot_actionModeRectangle() |
|
571 { |
|
572 R()->setEditMode (EditModeType::Rectangle); |
|
573 } |
|
574 |
|
575 // ============================================================================= |
|
576 // |
|
577 void MainWindow::slot_actionModeCircle() |
|
578 { |
|
579 R()->setEditMode (EditModeType::Circle); |
|
580 } |
|
581 |
|
582 // ============================================================================= |
|
583 // |
|
584 void MainWindow::slot_actionModeMagicWand() |
|
585 { |
|
586 R()->setEditMode (EditModeType::MagicWand); |
|
587 } |
|
588 |
|
589 void MainWindow::slot_actionModeLinePath() |
|
590 { |
|
591 R()->setEditMode (EditModeType::LinePath); |
|
592 } |
|
593 |
|
594 // ============================================================================= |
|
595 // |
|
596 void MainWindow::slot_actionDrawAngles() |
|
597 { |
|
598 cfg::DrawAngles = not cfg::DrawAngles; |
|
599 R()->refresh(); |
|
600 } |
|
601 |
|
602 // ============================================================================= |
|
603 // |
|
604 void MainWindow::slot_actionSetDrawDepth() |
|
605 { |
|
606 if (R()->camera() == EFreeCamera) |
|
607 return; |
|
608 |
|
609 bool ok; |
|
610 double depth = QInputDialog::getDouble (g_win, "Set Draw Depth", |
|
611 format ("Depth value for %1 Camera:", R()->getCameraName()), |
|
612 R()->getDepthValue(), -10000.0f, 10000.0f, 3, &ok); |
|
613 |
|
614 if (ok) |
|
615 R()->setDepthValue (depth); |
|
616 } |
|
617 |
|
618 #if 0 |
|
619 // This is a test to draw a dummy axle. Meant to be used as a primitive gallery, |
|
620 // but I can't figure how to generate these pictures properly. Multi-threading |
|
621 // these is an immense pain. |
|
622 void MainWindow::slot_actiontestpic() |
|
623 { |
|
624 LDDocument* file = getFile ("axle.dat"); |
|
625 setlocale (LC_ALL, "C"); |
|
626 |
|
627 if (not file) |
|
628 { |
|
629 critical ("couldn't load axle.dat"); |
|
630 return; |
|
631 } |
|
632 |
|
633 int w, h; |
|
634 |
|
635 GLRenderer* rend = new GLRenderer; |
|
636 rend->resize (64, 64); |
|
637 rend->setAttribute (Qt::WA_DontShowOnScreen); |
|
638 rend->show(); |
|
639 rend->setFile (file); |
|
640 rend->setDrawOnly (true); |
|
641 rend->compileAllObjects(); |
|
642 rend->initGLData(); |
|
643 rend->drawGLScene(); |
|
644 |
|
645 uchar* imgdata = rend->screencap (w, h); |
|
646 QImage img = imageFromScreencap (imgdata, w, h); |
|
647 |
|
648 if (img.isNull()) |
|
649 { |
|
650 critical ("Failed to create the image!\n"); |
|
651 } |
|
652 else |
|
653 { |
|
654 QLabel* label = new QLabel; |
|
655 QDialog* dlg = new QDialog; |
|
656 label->setPixmap (QPixmap::fromImage (img)); |
|
657 QVBoxLayout* layout = new QVBoxLayout (dlg); |
|
658 layout->addWidget (label); |
|
659 dlg->exec(); |
|
660 } |
|
661 |
|
662 delete[] imgdata; |
|
663 rend->deleteLater(); |
|
664 } |
|
665 #endif |
|
666 |
|
667 // ============================================================================= |
|
668 // |
|
669 void MainWindow::slot_actionScanPrimitives() |
|
670 { |
|
671 PrimitiveScanner::start(); |
|
672 } |
|
673 |
|
674 // ============================================================================= |
|
675 // |
|
676 void MainWindow::slot_actionBFCView() |
|
677 { |
|
678 cfg::BFCRedGreenView = not cfg::BFCRedGreenView; |
|
679 |
|
680 if (cfg::BFCRedGreenView) |
|
681 cfg::RandomColors = false; |
|
682 |
|
683 updateActions(); |
|
684 R()->refresh(); |
|
685 } |
|
686 |
|
687 // ============================================================================= |
|
688 // |
|
689 void MainWindow::slot_actionJumpTo() |
|
690 { |
|
691 bool ok; |
|
692 int defval = 0; |
|
693 LDObject* obj; |
|
694 |
|
695 if (Selection().size() == 1) |
|
696 defval = Selection()[0]->lineNumber(); |
|
697 |
|
698 int idx = QInputDialog::getInt (null, "Go to line", "Go to line:", defval, |
|
699 1, CurrentDocument()->getObjectCount(), 1, &ok); |
|
700 |
|
701 if (not ok or (obj = CurrentDocument()->getObject (idx - 1)) == null) |
|
702 return; |
|
703 |
|
704 CurrentDocument()->clearSelection(); |
|
705 obj->select(); |
|
706 updateSelection(); |
|
707 } |
|
708 |
|
709 // ============================================================================= |
|
710 // |
|
711 void MainWindow::slot_actionSubfileSelection() |
|
712 { |
|
713 if (Selection().size() == 0) |
|
714 return; |
|
715 |
|
716 QString parentpath (CurrentDocument()->fullPath()); |
|
717 |
|
718 // BFC type of the new subfile - it shall inherit the BFC type of the parent document |
|
719 BFCStatement bfctype (BFCStatement::NoCertify); |
|
720 |
|
721 // Dirname of the new subfile |
|
722 QString subdirname (Dirname (parentpath)); |
|
723 |
|
724 // Title of the new subfile |
|
725 QString subtitle; |
|
726 |
|
727 // Comment containing the title of the parent document |
|
728 LDComment* titleobj = dynamic_cast<LDComment*> (CurrentDocument()->getObject (0)); |
|
729 |
|
730 // License text for the subfile |
|
731 QString license (PreferredLicenseText()); |
|
732 |
|
733 // LDraw code body of the new subfile (i.e. code of the selection) |
|
734 QStringList code; |
|
735 |
|
736 // Full path of the subfile to be |
|
737 QString fullsubname; |
|
738 |
|
739 // Where to insert the subfile reference? |
|
740 int refidx (Selection()[0]->lineNumber()); |
|
741 |
|
742 // Determine title of subfile |
|
743 if (titleobj != null) |
|
744 subtitle = "~" + titleobj->text(); |
|
745 else |
|
746 subtitle = "~subfile"; |
|
747 |
|
748 // Remove duplicate tildes |
|
749 while (subtitle.startsWith ("~~")) |
|
750 subtitle.remove (0, 1); |
|
751 |
|
752 // If this the parent document isn't already in s/, we need to stuff it into |
|
753 // a subdirectory named s/. Ensure it exists! |
|
754 QString topdirname = Basename (Dirname (CurrentDocument()->fullPath())); |
|
755 |
|
756 if (topdirname != "s") |
|
757 { |
|
758 QString desiredPath = subdirname + "/s"; |
|
759 QString title = tr ("Create subfile directory?"); |
|
760 QString text = format (tr ("The directory <b>%1</b> is suggested for " |
|
761 "subfiles. This directory does not exist, create it?"), desiredPath); |
|
762 |
|
763 if (QDir (desiredPath).exists() or Confirm (title, text)) |
|
764 { |
|
765 subdirname = desiredPath; |
|
766 QDir().mkpath (subdirname); |
|
767 } |
|
768 else |
|
769 return; |
|
770 } |
|
771 |
|
772 // Determine the body of the name of the subfile |
|
773 if (not parentpath.isEmpty()) |
|
774 { |
|
775 // Chop existing '.dat' suffix |
|
776 if (parentpath.endsWith (".dat")) |
|
777 parentpath.chop (4); |
|
778 |
|
779 // Remove the s?? suffix if it's there, otherwise we'll get filenames |
|
780 // like s01s01.dat when subfiling subfiles. |
|
781 QRegExp subfilesuffix ("s[0-9][0-9]$"); |
|
782 if (subfilesuffix.indexIn (parentpath) != -1) |
|
783 parentpath.chop (subfilesuffix.matchedLength()); |
|
784 |
|
785 int subidx = 1; |
|
786 QString digits; |
|
787 QFile f; |
|
788 QString testfname; |
|
789 |
|
790 // Now find the appropriate filename. Increase the number of the subfile |
|
791 // until we find a name which isn't already taken. |
|
792 do |
|
793 { |
|
794 digits.setNum (subidx++); |
|
795 |
|
796 // pad it with a zero |
|
797 if (digits.length() == 1) |
|
798 digits.prepend ("0"); |
|
799 |
|
800 fullsubname = subdirname + "/" + Basename (parentpath) + "s" + digits + ".dat"; |
|
801 } while (FindDocument ("s\\" + Basename (fullsubname)) != null or QFile (fullsubname).exists()); |
|
802 } |
|
803 |
|
804 // Determine the BFC winding type used in the main document - it is to |
|
805 // be carried over to the subfile. |
|
806 LDIterate<LDBFC> (CurrentDocument()->objects(), [&] (LDBFC* const& bfc) |
|
807 { |
|
808 if (Eq (bfc->statement(), BFCStatement::CertifyCCW, BFCStatement::CertifyCW, |
|
809 BFCStatement::NoCertify)) |
|
810 { |
|
811 bfctype = bfc->statement(); |
|
812 Break(); |
|
813 } |
|
814 }); |
|
815 |
|
816 // Get the body of the document in LDraw code |
|
817 for (LDObject* obj : Selection()) |
|
818 code << obj->asText(); |
|
819 |
|
820 // Create the new subfile document |
|
821 LDDocument* doc = LDDocument::createNew(); |
|
822 doc->setImplicit (false); |
|
823 doc->setFullPath (fullsubname); |
|
824 doc->setName (LDDocument::shortenName (fullsubname)); |
|
825 |
|
826 LDObjectList objs; |
|
827 objs << LDSpawn<LDComment> (subtitle); |
|
828 objs << LDSpawn<LDComment> ("Name: "); // This gets filled in when the subfile is saved |
|
829 objs << LDSpawn<LDComment> (format ("Author: %1 [%2]", cfg::DefaultName, cfg::DefaultUser)); |
|
830 objs << LDSpawn<LDComment> ("!LDRAW_ORG Unofficial_Subpart"); |
|
831 |
|
832 if (not license.isEmpty()) |
|
833 objs << LDSpawn<LDComment> (license); |
|
834 |
|
835 objs << LDSpawn<LDEmpty>(); |
|
836 objs << LDSpawn<LDBFC> (bfctype); |
|
837 objs << LDSpawn<LDEmpty>(); |
|
838 |
|
839 doc->addObjects (objs); |
|
840 |
|
841 // Add the actual subfile code to the new document |
|
842 for (QString line : code) |
|
843 { |
|
844 LDObject* obj = ParseLine (line); |
|
845 doc->addObject (obj); |
|
846 } |
|
847 |
|
848 // Try save it |
|
849 if (save (doc, true)) |
|
850 { |
|
851 // Save was successful. Delete the original selection now from the |
|
852 // main document. |
|
853 for (LDObject* obj : Selection()) |
|
854 obj->destroy(); |
|
855 |
|
856 // Add a reference to the new subfile to where the selection was |
|
857 LDSubfile* ref (LDSpawn<LDSubfile>()); |
|
858 ref->setColor (MainColor); |
|
859 ref->setFileInfo (doc); |
|
860 ref->setPosition (Origin); |
|
861 ref->setTransform (IdentityMatrix); |
|
862 CurrentDocument()->insertObj (refidx, ref); |
|
863 |
|
864 // Refresh stuff |
|
865 updateDocumentList(); |
|
866 doFullRefresh(); |
|
867 } |
|
868 else |
|
869 { |
|
870 // Failed to save. |
|
871 doc->dismiss(); |
|
872 } |
|
873 } |
|
874 |
|
875 void MainWindow::slot_actionRandomColors() |
|
876 { |
|
877 cfg::RandomColors = not cfg::RandomColors; |
|
878 |
|
879 if (cfg::RandomColors) |
|
880 cfg::BFCRedGreenView = false; |
|
881 |
|
882 updateActions(); |
|
883 R()->refresh(); |
|
884 } |
|
885 |
|
886 void MainWindow::slot_actionOpenSubfiles() |
|
887 { |
|
888 for (LDObject* obj : Selection()) |
|
889 { |
|
890 LDSubfile* ref = dynamic_cast<LDSubfile*> (obj); |
|
891 |
|
892 if (ref == null or not ref->fileInfo()->isImplicit()) |
|
893 continue; |
|
894 |
|
895 ref->fileInfo()->setImplicit (false); |
|
896 } |
|
897 } |
|
898 |
|
899 void MainWindow::slot_actionDrawSurfaces() |
|
900 { |
|
901 cfg::DrawSurfaces = not cfg::DrawSurfaces; |
|
902 updateActions(); |
|
903 update(); |
|
904 } |
|
905 |
|
906 void MainWindow::slot_actionDrawEdgeLines() |
|
907 { |
|
908 cfg::DrawEdgeLines = not cfg::DrawEdgeLines; |
|
909 updateActions(); |
|
910 update(); |
|
911 } |
|
912 |
|
913 void MainWindow::slot_actionDrawConditionalLines() |
|
914 { |
|
915 cfg::DrawConditionalLines = not cfg::DrawConditionalLines; |
|
916 updateActions(); |
|
917 update(); |
|
918 } |
|