src/document.cc

changeset 629
b75c6cce02e2
parent 628
6b13e4c2e97b
child 630
42ec68fcad9e
child 675
450827da2376
equal deleted inserted replaced
628:6b13e4c2e97b 629:b75c6cce02e2
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri 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 <QMessageBox>
20 #include <QFileDialog>
21 #include <QDir>
22 #include <QApplication>
23 #include "main.h"
24 #include "config.h"
25 #include "document.h"
26 #include "misc.h"
27 #include "gui.h"
28 #include "history.h"
29 #include "dialogs.h"
30 #include "gldraw.h"
31 #include "misc/invokationDeferer.h"
32 #include "moc_document.cpp"
33
34 cfg (String, io_ldpath, "");
35 cfg (List, io_recentfiles, {});
36 extern_cfg (String, net_downloadpath);
37 extern_cfg (Bool, gl_logostuds);
38
39 static bool g_loadingMainFile = false;
40 static const int g_maxRecentFiles = 10;
41 static bool g_aborted = false;
42 static LDDocumentPointer g_logoedStud = null;
43 static LDDocumentPointer g_logoedStud2 = null;
44
45 LDDocument* LDDocument::m_curdoc = null;
46
47 const QStringList g_specialSubdirectories ({ "s", "48", "8" });
48
49 // =============================================================================
50 // -----------------------------------------------------------------------------
51 namespace LDPaths
52 {
53 static QString pathError;
54
55 struct
56 {
57 QString LDConfigPath;
58 QString partsPath, primsPath;
59 } pathInfo;
60
61 void initPaths()
62 {
63 if (!tryConfigure (io_ldpath))
64 {
65 LDrawPathDialog dlg (false);
66
67 if (!dlg.exec())
68 exit (0);
69
70 io_ldpath = dlg.filename();
71 }
72 }
73
74 bool tryConfigure (QString path)
75 {
76 QDir dir;
77
78 if (!dir.cd (path))
79 {
80 pathError = "Directory does not exist.";
81 return false;
82 }
83
84 QStringList mustHave = { "LDConfig.ldr", "parts", "p" };
85 QStringList contents = dir.entryList (mustHave);
86
87 if (contents.size() != mustHave.size())
88 {
89 pathError = "Not an LDraw directory! Must<br />have LDConfig.ldr, parts/ and p/.";
90 return false;
91 }
92
93 pathInfo.partsPath = fmt ("%1" DIRSLASH "parts", path);
94 pathInfo.LDConfigPath = fmt ("%1" DIRSLASH "LDConfig.ldr", path);
95 pathInfo.primsPath = fmt ("%1" DIRSLASH "p", path);
96
97 return true;
98 }
99
100 // Accessors
101 QString getError()
102 {
103 return pathError;
104 }
105
106 QString ldconfig()
107 {
108 return pathInfo.LDConfigPath;
109 }
110
111 QString prims()
112 {
113 return pathInfo.primsPath;
114 }
115
116 QString parts()
117 {
118 return pathInfo.partsPath;
119 }
120 }
121
122 // =============================================================================
123 // -----------------------------------------------------------------------------
124 LDDocument::LDDocument() :
125 m_gldata (new LDGLData)
126 {
127 setImplicit (true);
128 setSavePosition (-1);
129 setListItem (null);
130 setHistory (new History);
131 m_History->setDocument (this);
132 }
133
134 // =============================================================================
135 // -----------------------------------------------------------------------------
136 LDDocument::~LDDocument()
137 {
138 // Remove this file from the list of files. This MUST be done FIRST, otherwise
139 // a ton of other functions will think this file is still valid when it is not!
140 g_loadedFiles.removeOne (this);
141
142 m_History->setIgnoring (true);
143
144 // Clear everything from the model
145 for (LDObject* obj : getObjects())
146 obj->deleteSelf();
147
148 // Clear the cache as well
149 for (LDObject* obj : getCache())
150 obj->deleteSelf();
151
152 delete m_History;
153 delete m_gldata;
154
155 // If we just closed the current file, we need to set the current
156 // file as something else.
157 if (this == getCurrentDocument())
158 {
159 bool found = false;
160
161 // Try find an explicitly loaded file - if we can't find one,
162 // we need to create a new file to switch to.
163 for (LDDocument* file : g_loadedFiles)
164 {
165 if (!file->isImplicit())
166 {
167 LDDocument::setCurrent (file);
168 found = true;
169 break;
170 }
171 }
172
173 if (!found)
174 newFile();
175 }
176
177 if (this == g_logoedStud)
178 g_logoedStud = null;
179 elif (this == g_logoedStud2)
180 g_logoedStud2 = null;
181
182 g_win->updateDocumentList();
183 log ("Closed %1", getName());
184 }
185
186 // =============================================================================
187 // -----------------------------------------------------------------------------
188 LDDocument* findDocument (QString name)
189 {
190 for (LDDocument * file : g_loadedFiles)
191 if (!file->getName().isEmpty() && file->getName() == name)
192 return file;
193
194 return null;
195 }
196
197 // =============================================================================
198 // -----------------------------------------------------------------------------
199 QString dirname (QString path)
200 {
201 long lastpos = path.lastIndexOf (DIRSLASH);
202
203 if (lastpos > 0)
204 return path.left (lastpos);
205
206 #ifndef _WIN32
207 if (path[0] == DIRSLASH_CHAR)
208 return DIRSLASH;
209 #endif // _WIN32
210
211 return "";
212 }
213
214 // =============================================================================
215 // -----------------------------------------------------------------------------
216 QString basename (QString path)
217 {
218 long lastpos = path.lastIndexOf (DIRSLASH);
219
220 if (lastpos != -1)
221 return path.mid (lastpos + 1);
222
223 return path;
224 }
225
226 // =============================================================================
227 // -----------------------------------------------------------------------------
228 static QString findLDrawFilePath (QString relpath, bool subdirs)
229 {
230 QString fullPath;
231
232 // LDraw models use Windows-style path separators. If we're not on Windows,
233 // replace the path separator now before opening any files. Qt expects
234 // forward-slashes as directory separators.
235 #ifndef WIN32
236 relpath.replace ("\\", "/");
237 #endif // WIN32
238
239 // Try find it relative to other currently open documents. We want a file
240 // in the immediate vicinity of a current model to override stock LDraw stuff.
241 QString reltop = basename (dirname (relpath));
242
243 for (LDDocument* doc : g_loadedFiles)
244 {
245 if (doc->getFullPath().isEmpty())
246 continue;
247
248 QString partpath = fmt ("%1/%2", dirname (doc->getFullPath()), relpath);
249 QFile f (partpath);
250
251 if (f.exists())
252 {
253 // ensure we don't mix subfiles and 48-primitives with non-subfiles and non-48
254 QString proptop = basename (dirname (partpath));
255
256 bool bogus = false;
257
258 for (QString s : g_specialSubdirectories)
259 {
260 if ((proptop == s && reltop != s) || (reltop == s && proptop != s))
261 {
262 bogus = true;
263 break;
264 }
265 }
266
267 if (!bogus)
268 return partpath;
269 }
270 }
271
272 if (QFile::exists (relpath))
273 return relpath;
274
275 // Try with just the LDraw path first
276 fullPath = fmt ("%1" DIRSLASH "%2", io_ldpath, relpath);
277
278 if (QFile::exists (fullPath))
279 return fullPath;
280
281 if (subdirs)
282 {
283 // Look in sub-directories: parts and p. Also look in net_downloadpath, since that's
284 // where we download parts from the PT to.
285 for (const QString& topdir : initlist<QString> ({ io_ldpath, net_downloadpath }))
286 {
287 for (const QString& subdir : initlist<QString> ({ "parts", "p" }))
288 {
289 fullPath = fmt ("%1" DIRSLASH "%2" DIRSLASH "%3", topdir, subdir, relpath);
290
291 if (QFile::exists (fullPath))
292 return fullPath;
293 }
294 }
295 }
296
297 // Did not find the file.
298 return "";
299 }
300
301 QFile* openLDrawFile (QString relpath, bool subdirs, QString* pathpointer)
302 {
303 log ("Opening %1...\n", relpath);
304 QString path = findLDrawFilePath (relpath, subdirs);
305
306 if (pathpointer != null)
307 *pathpointer = path;
308
309 if (path.isEmpty())
310 return null;
311
312 QFile* fp = new QFile (path);
313
314 if (fp->open (QIODevice::ReadOnly))
315 return fp;
316
317 fp->deleteLater();
318 return null;
319 }
320
321 // =============================================================================
322 // -----------------------------------------------------------------------------
323 void LDFileLoader::start()
324 {
325 setDone (false);
326 setProgress (0);
327 setAborted (false);
328
329 if (isOnForeground())
330 {
331 g_aborted = false;
332
333 // Show a progress dialog if we're loading the main document.here so we can
334 // show progress updates and keep the WM posted that we're still here.
335 // Of course we cannot exec() the dialog because then the dialog would
336 // block.
337 dlg = new OpenProgressDialog (g_win);
338 dlg->setNumLines (getLines().size());
339 dlg->setModal (true);
340 dlg->show();
341
342 // Connect the loader in so we can show updates
343 connect (this, SIGNAL (workDone()), dlg, SLOT (accept()));
344 connect (dlg, SIGNAL (rejected()), this, SLOT (abort()));
345 }
346 else
347 dlg = null;
348
349 // Begin working
350 work (0);
351 }
352
353 // =============================================================================
354 // -----------------------------------------------------------------------------
355 void LDFileLoader::work (int i)
356 {
357 // User wishes to abort, so stop here now.
358 if (isAborted())
359 {
360 for (LDObject* obj : m_Objects)
361 obj->deleteSelf();
362
363 m_Objects.clear();
364 setDone (true);
365 return;
366 }
367
368 // Parse up to 300 lines per iteration
369 int max = i + 300;
370
371 for (; i < max && i < (int) getLines().size(); ++i)
372 {
373 QString line = getLines()[i];
374
375 // Trim the trailing newline
376 QChar c;
377
378 while (line.endsWith ("\n") || line.endsWith ("\r"))
379 line.chop (1);
380
381 LDObject* obj = parseLine (line);
382
383 // Check for parse errors and warn about tthem
384 if (obj->getType() == LDObject::EError)
385 {
386 log ("Couldn't parse line #%1: %2", getProgress() + 1, static_cast<LDError*> (obj)->reason);
387
388 if (getWarnings() != null)
389 (*getWarnings())++;
390 }
391
392 m_Objects << obj;
393 setProgress (i);
394
395 // If we have a dialog pointer, update the progress now
396 if (isOnForeground())
397 dlg->updateProgress (i);
398 }
399
400 // If we're done now, tell the environment we're done and stop.
401 if (i >= ((int) getLines().size()) - 1)
402 {
403 emit workDone();
404 setDone (true);
405 return;
406 }
407
408 // Otherwise, continue, by recursing back.
409 if (!isDone())
410 {
411 // If we have a dialog to show progress output to, we cannot just call
412 // work() again immediately as the dialog needs some processor cycles as
413 // well. Thus, take a detour through the event loop by using the
414 // meta-object system.
415 //
416 // This terminates the loop here and control goes back to the function
417 // which called the file loader. It will keep processing the event loop
418 // until we're ready (see loadFileContents), thus the event loop will
419 // eventually catch the invokation we throw here and send us back. Though
420 // it's not technically recursion anymore, more like a for loop. :P
421 if (isOnForeground())
422 QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection, Q_ARG (int, i));
423 else
424 work (i);
425 }
426 }
427
428 // =============================================================================
429 // -----------------------------------------------------------------------------
430 void LDFileLoader::abort()
431 {
432 setAborted (true);
433
434 if (isOnForeground())
435 g_aborted = true;
436 }
437
438 // =============================================================================
439 // -----------------------------------------------------------------------------
440 LDObjectList loadFileContents (QFile* fp, int* numWarnings, bool* ok)
441 {
442 QStringList lines;
443 LDObjectList objs;
444
445 if (numWarnings)
446 *numWarnings = 0;
447
448 // Read in the lines
449 while (fp->atEnd() == false)
450 lines << QString::fromUtf8 (fp->readLine());
451
452 LDFileLoader* loader = new LDFileLoader;
453 loader->setWarnings (numWarnings);
454 loader->setLines (lines);
455 loader->setOnForeground (g_loadingMainFile);
456 loader->start();
457
458 // After start() returns, if the loader isn't done yet, it's delaying
459 // its next iteration through the event loop. We need to catch this here
460 // by telling the event loop to tick, which will tick the file loader again.
461 // We keep doing this until the file loader is ready.
462 while (loader->isDone() == false)
463 qApp->processEvents();
464
465 // If we wanted the success value, supply that now
466 if (ok)
467 *ok = !loader->isAborted();
468
469 objs = loader->getObjects();
470 return objs;
471 }
472
473 // =============================================================================
474 // -----------------------------------------------------------------------------
475 LDDocument* openDocument (QString path, bool search)
476 {
477 // Convert the file name to lowercase since some parts contain uppercase
478 // file names. I'll assume here that the library will always use lowercase
479 // file names for the actual parts..
480 QFile* fp;
481 QString fullpath;
482
483 if (search)
484 fp = openLDrawFile (path.toLower(), true, &fullpath);
485 else
486 {
487 fp = new QFile (path);
488 fullpath = path;
489
490 if (!fp->open (QIODevice::ReadOnly))
491 {
492 delete fp;
493 return null;
494 }
495 }
496
497 if (!fp)
498 return null;
499
500 LDDocument* load = new LDDocument;
501 load->setFullPath (fullpath);
502 load->setName (LDDocument::shortenName (load->getFullPath()));
503 dlog ("name: %1 (%2)", load->getName(), load->getFullPath());
504 g_loadedFiles << load;
505
506 // Don't take the file loading as actual edits to the file
507 load->getHistory()->setIgnoring (true);
508
509 int numWarnings;
510 bool ok;
511 LDObjectList objs = loadFileContents (fp, &numWarnings, &ok);
512 fp->close();
513 fp->deleteLater();
514
515 if (!ok)
516 {
517 g_loadedFiles.removeOne (load);
518 delete load;
519 return null;
520 }
521
522 load->addObjects (objs);
523
524 if (g_loadingMainFile)
525 {
526 LDDocument::setCurrent (load);
527 g_win->R()->setFile (load);
528 log (QObject::tr ("File %1 parsed successfully (%2 errors)."), path, numWarnings);
529 }
530
531 load->getHistory()->setIgnoring (false);
532 return load;
533 }
534
535 // =============================================================================
536 // -----------------------------------------------------------------------------
537 bool LDDocument::isSafeToClose()
538 {
539 typedef QMessageBox msgbox;
540 setlocale (LC_ALL, "C");
541
542 // If we have unsaved changes, warn and give the option of saving.
543 if (hasUnsavedChanges())
544 {
545 QString message = fmt (tr ("There are unsaved changes to %1. Should it be saved?"),
546 (getName().length() > 0) ? getName() : tr ("<anonymous>"));
547
548 int button = msgbox::question (g_win, tr ("Unsaved Changes"), message,
549 (msgbox::Yes | msgbox::No | msgbox::Cancel), msgbox::Cancel);
550
551 switch (button)
552 {
553 case msgbox::Yes:
554 {
555 // If we don't have a file path yet, we have to ask the user for one.
556 if (getName().length() == 0)
557 {
558 QString newpath = QFileDialog::getSaveFileName (g_win, tr ("Save As"),
559 getCurrentDocument()->getName(), tr ("LDraw files (*.dat *.ldr)"));
560
561 if (newpath.length() == 0)
562 return false;
563
564 setName (newpath);
565 }
566
567 if (!save())
568 {
569 message = fmt (tr ("Failed to save %1 (%2)\nDo you still want to close?"),
570 getName(), strerror (errno));
571
572 if (msgbox::critical (g_win, tr ("Save Failure"), message,
573 (msgbox::Yes | msgbox::No), msgbox::No) == msgbox::No)
574 {
575 return false;
576 }
577 }
578 } break;
579
580 case msgbox::Cancel:
581 return false;
582
583 default:
584 break;
585 }
586 }
587
588 return true;
589 }
590
591 // =============================================================================
592 // -----------------------------------------------------------------------------
593 void closeAll()
594 {
595 // Remove all loaded files and the objects they contain
596 QList<LDDocument*> files = g_loadedFiles;
597
598 for (LDDocument* file : files)
599 delete file;
600 }
601
602 // =============================================================================
603 // -----------------------------------------------------------------------------
604 void newFile()
605 {
606 // Create a new anonymous file and set it to our current
607 LDDocument* f = new LDDocument;
608 f->setName ("");
609 f->setImplicit (false);
610 g_loadedFiles << f;
611 LDDocument::setCurrent (f);
612 LDDocument::closeInitialFile();
613 g_win->R()->setFile (f);
614 g_win->doFullRefresh();
615 g_win->updateTitle();
616 g_win->updateActions();
617 }
618
619 // =============================================================================
620 // -----------------------------------------------------------------------------
621 void addRecentFile (QString path)
622 {
623 auto& rfiles = io_recentfiles;
624 int idx = rfiles.indexOf (path);
625
626 // If this file already is in the list, pop it out.
627 if (idx != -1)
628 {
629 if (rfiles.size() == 1)
630 return; // only recent file - abort and do nothing
631
632 // Pop it out.
633 rfiles.removeAt (idx);
634 }
635
636 // If there's too many recent files, drop one out.
637 while (rfiles.size() > (g_maxRecentFiles - 1))
638 rfiles.removeAt (0);
639
640 // Add the file
641 rfiles << path;
642
643 Config::save();
644 g_win->updateRecentFilesMenu();
645 }
646
647 // =============================================================================
648 // Open an LDraw file and set it as the main model
649 // -----------------------------------------------------------------------------
650 void openMainFile (QString path)
651 {
652 g_loadingMainFile = true;
653
654 // If there's already a file with the same name, this file must replace it.
655 LDDocument* documentToReplace = null;
656 QString shortName = LDDocument::shortenName (path);
657
658 for (LDDocument* doc : g_loadedFiles)
659 {
660 if (doc->getName() == shortName)
661 {
662 documentToReplace = doc;
663 break;
664 }
665 }
666
667 // We cannot open this file if the document this would replace is not
668 // safe to close.
669 if (documentToReplace != null && documentToReplace->isSafeToClose() == false)
670 {
671 g_loadingMainFile = false;
672 return;
673 }
674
675 LDDocument* file = openDocument (path, false);
676
677 if (!file)
678 {
679 // Loading failed, thus drop down to a new file since we
680 // closed everything prior.
681 newFile();
682
683 if (!g_aborted)
684 {
685 // Tell the user loading failed.
686 setlocale (LC_ALL, "C");
687 critical (fmt (QObject::tr ("Failed to open %1: %2"), path, strerror (errno)));
688 }
689
690 g_loadingMainFile = false;
691 return;
692 }
693
694 file->setImplicit (false);
695
696 // Replace references to the old file with the new file.
697 if (documentToReplace != null)
698 {
699 for (LDDocumentPointer* ptr : documentToReplace->getReferences())
700 { dlog ("ptr: %1 (%2)\n",
701 ptr, ptr->getPointer() ? ptr->getPointer()->getName() : "<null>");
702
703 ptr->operator= (file);
704 }
705
706 assert (documentToReplace->countReferences() == 0);
707 delete documentToReplace;
708 }
709
710 // If we have an anonymous, unchanged file open as the only open file
711 // (aside of the one we just opened), close it now.
712 LDDocument::closeInitialFile();
713
714 // Rebuild the object tree view now.
715 LDDocument::setCurrent (file);
716 g_win->doFullRefresh();
717
718 // Add it to the recent files list.
719 addRecentFile (path);
720 g_loadingMainFile = false;
721 }
722
723 // =============================================================================
724 // -----------------------------------------------------------------------------
725 bool LDDocument::save (QString savepath)
726 {
727 if (!savepath.length())
728 savepath = getFullPath();
729
730 QFile f (savepath);
731
732 if (!f.open (QIODevice::WriteOnly))
733 return false;
734
735 // If the second object in the list holds the file name, update that now.
736 // Only do this if the file is explicitly open.
737 LDObject* nameObject = getObject (1);
738
739 if (!isImplicit() && nameObject != null && nameObject->getType() == LDObject::EComment)
740 {
741 LDComment* nameComment = static_cast<LDComment*> (nameObject);
742
743 if (nameComment->text.left (6) == "Name: ")
744 {
745 QString newname = shortenName (savepath);
746 nameComment->text = fmt ("Name: %1", newname);
747 g_win->buildObjList();
748 }
749 }
750
751 // File is open, now save the model to it. Note that LDraw requires files to
752 // have DOS line endings, so we terminate the lines with \r\n.
753 for (LDObject* obj : getObjects())
754 f.write ((obj->raw() + "\r\n").toUtf8());
755
756 // File is saved, now clean up.
757 f.close();
758
759 // We have successfully saved, update the save position now.
760 setSavePosition (getHistory()->getPosition());
761 setFullPath (savepath);
762 setName (shortenName (savepath));
763
764 g_win->updateDocumentListItem (this);
765 g_win->updateTitle();
766 return true;
767 }
768
769 // =============================================================================
770 // -----------------------------------------------------------------------------
771 class LDParseError : public std::exception
772 {
773 PROPERTY (private, QString, Error, STR_OPS, STOCK_WRITE)
774 PROPERTY (private, QString, Line, STR_OPS, STOCK_WRITE)
775
776 public:
777 LDParseError (QString line, QString a) : m_Error (a), m_Line (line) {}
778
779 const char* what() const throw()
780 {
781 return getError().toLocal8Bit().constData();
782 }
783 };
784
785 // =============================================================================
786 // -----------------------------------------------------------------------------
787 void checkTokenCount (QString line, const QStringList& tokens, int num)
788 {
789 if (tokens.size() != num)
790 throw LDParseError (line, fmt ("Bad amount of tokens, expected %1, got %2", num, tokens.size()));
791 }
792
793 // =============================================================================
794 // -----------------------------------------------------------------------------
795 void checkTokenNumbers (QString line, const QStringList& tokens, int min, int max)
796 {
797 bool ok;
798
799 // Check scientific notation, e.g. 7.99361e-15
800 QRegExp scient ("\\-?[0-9]+\\.[0-9]+e\\-[0-9]+");
801
802 for (int i = min; i <= max; ++i)
803 {
804 tokens[i].toDouble (&ok);
805
806 if (!ok && !scient.exactMatch (tokens[i]))
807 throw LDParseError (line, fmt ("Token #%1 was `%2`, expected a number (matched length: %3)", (i + 1), tokens[i], scient.matchedLength()));
808 }
809 }
810
811 // =============================================================================
812 // -----------------------------------------------------------------------------
813 static Vertex parseVertex (QStringList& s, const int n)
814 {
815 Vertex v;
816
817 for_axes (ax)
818 v[ax] = s[n + ax].toDouble();
819
820 return v;
821 }
822
823 // =============================================================================
824 // This is the LDraw code parser function. It takes in a string containing LDraw
825 // code and returns the object parsed from it. parseLine never returns null,
826 // the object will be LDError if it could not be parsed properly.
827 // -----------------------------------------------------------------------------
828 LDObject* parseLine (QString line)
829 {
830 try
831 {
832 QStringList tokens = line.split (" ", QString::SkipEmptyParts);
833
834 if (tokens.size() <= 0)
835 {
836 // Line was empty, or only consisted of whitespace
837 return new LDEmpty;
838 }
839
840 if (tokens[0].length() != 1 || tokens[0][0].isDigit() == false)
841 throw LDParseError (line, "Illogical line code");
842
843 int num = tokens[0][0].digitValue();
844
845 switch (num)
846 {
847 case 0:
848 {
849 // Comment
850 QString comm = line.mid (line.indexOf ("0") + 1).simplified();
851
852 // Handle BFC statements
853 if (tokens.size() > 2 && tokens[1] == "BFC")
854 {
855 for (int i = 0; i < LDBFC::NumStatements; ++i)
856 if (comm == fmt ("BFC %1", LDBFC::statements [i]))
857 return new LDBFC ( (LDBFC::Type) i);
858
859 // MLCAD is notorious for stuffing these statements in parts it
860 // creates. The above block only handles valid statements, so we
861 // need to handle MLCAD-style invertnext, clip and noclip separately.
862 struct
863 {
864 QString a;
865 LDBFC::Type b;
866 } BFCData[] =
867 {
868 { "INVERTNEXT", LDBFC::InvertNext },
869 { "NOCLIP", LDBFC::NoClip },
870 { "CLIP", LDBFC::Clip }
871 };
872
873 for (const auto& i : BFCData)
874 if (comm == "BFC CERTIFY " + i.a)
875 return new LDBFC (i.b);
876 }
877
878 if (tokens.size() > 2 && tokens[1] == "!LDFORGE")
879 {
880 // Handle LDForge-specific types, they're embedded into comments too
881 if (tokens[2] == "VERTEX")
882 {
883 // Vertex (0 !LDFORGE VERTEX)
884 checkTokenCount (line, tokens, 7);
885 checkTokenNumbers (line, tokens, 3, 6);
886
887 LDVertex* obj = new LDVertex;
888 obj->setColor (tokens[3].toLong());
889
890 for_axes (ax)
891 obj->pos[ax] = tokens[4 + ax].toDouble(); // 4 - 6
892
893 return obj;
894 } elif (tokens[2] == "OVERLAY")
895 {
896 checkTokenCount (line, tokens, 9);;
897 checkTokenNumbers (line, tokens, 5, 8);
898
899 LDOverlay* obj = new LDOverlay;
900 obj->setFileName (tokens[3]);
901 obj->setCamera (tokens[4].toLong());
902 obj->setX (tokens[5].toLong());
903 obj->setY (tokens[6].toLong());
904 obj->setWidth (tokens[7].toLong());
905 obj->setHeight (tokens[8].toLong());
906 return obj;
907 }
908 }
909
910 // Just a regular comment:
911 LDComment* obj = new LDComment;
912 obj->text = comm;
913 return obj;
914 }
915
916 case 1:
917 {
918 // Subfile
919 checkTokenCount (line, tokens, 15);
920 checkTokenNumbers (line, tokens, 1, 13);
921
922 // Try open the file. Disable g_loadingMainFile temporarily since we're
923 // not loading the main file now, but the subfile in question.
924 bool tmp = g_loadingMainFile;
925 g_loadingMainFile = false;
926 LDDocument* load = getDocument (tokens[14]);
927 g_loadingMainFile = tmp;
928
929 // If we cannot open the file, mark it an error. Note we cannot use LDParseError
930 // here because the error object needs the document reference.
931 if (!load)
932 {
933 LDError* obj = new LDError (line, fmt ("Could not open %1", tokens[14]));
934 obj->setFileReferenced (tokens[14]);
935 return obj;
936 }
937
938 LDSubfile* obj = new LDSubfile;
939 obj->setColor (tokens[1].toLong());
940 obj->setPosition (parseVertex (tokens, 2)); // 2 - 4
941
942 Matrix transform;
943
944 for (int i = 0; i < 9; ++i)
945 transform[i] = tokens[i + 5].toDouble(); // 5 - 13
946
947 obj->setTransform (transform);
948 obj->setFileInfo (load);
949 return obj;
950 }
951
952 case 2:
953 {
954 checkTokenCount (line, tokens, 8);
955 checkTokenNumbers (line, tokens, 1, 7);
956
957 // Line
958 LDLine* obj = new LDLine;
959 obj->setColor (tokens[1].toLong());
960
961 for (int i = 0; i < 2; ++i)
962 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 7
963
964 return obj;
965 }
966
967 case 3:
968 {
969 checkTokenCount (line, tokens, 11);
970 checkTokenNumbers (line, tokens, 1, 10);
971
972 // Triangle
973 LDTriangle* obj = new LDTriangle;
974 obj->setColor (tokens[1].toLong());
975
976 for (int i = 0; i < 3; ++i)
977 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 10
978
979 return obj;
980 }
981
982 case 4:
983 case 5:
984 {
985 checkTokenCount (line, tokens, 14);
986 checkTokenNumbers (line, tokens, 1, 13);
987
988 // Quadrilateral / Conditional line
989 LDObject* obj;
990
991 if (num == 4)
992 obj = new LDQuad;
993 else
994 obj = new LDCondLine;
995
996 obj->setColor (tokens[1].toLong());
997
998 for (int i = 0; i < 4; ++i)
999 obj->setVertex (i, parseVertex (tokens, 2 + (i * 3))); // 2 - 13
1000
1001 return obj;
1002 }
1003
1004 default: // Strange line we couldn't parse
1005 throw LDError (line, "Unknown line code number");
1006 }
1007 }
1008 catch (LDParseError& e)
1009 {
1010 return new LDError (e.getLine(), e.getError());
1011 }
1012 }
1013
1014 // =============================================================================
1015 // -----------------------------------------------------------------------------
1016 LDDocument* getDocument (QString filename)
1017 {
1018 // Try find the file in the list of loaded files
1019 LDDocument* doc = findDocument (filename);
1020
1021 // If it's not loaded, try open it
1022 if (!doc)
1023 doc = openDocument (filename, true);
1024
1025 return doc;
1026 }
1027
1028 // =============================================================================
1029 // -----------------------------------------------------------------------------
1030 void reloadAllSubfiles()
1031 {
1032 if (!getCurrentDocument())
1033 return;
1034
1035 g_loadedFiles.clear();
1036 g_loadedFiles << getCurrentDocument();
1037
1038 // Go through all objects in the current file and reload the subfiles
1039 for (LDObject* obj : getCurrentDocument()->getObjects())
1040 {
1041 if (obj->getType() == LDObject::ESubfile)
1042 {
1043 LDSubfile* ref = static_cast<LDSubfile*> (obj);
1044 LDDocument* fileInfo = getDocument (ref->getFileInfo()->getName());
1045
1046 if (fileInfo)
1047 ref->setFileInfo (fileInfo);
1048 else
1049 ref->replace (new LDError (ref->raw(), fmt ("Could not open %1", ref->getFileInfo()->getName())));
1050 }
1051
1052 // Reparse gibberish files. It could be that they are invalid because
1053 // of loading errors. Circumstances may be different now.
1054 if (obj->getType() == LDObject::EError)
1055 obj->replace (parseLine (static_cast<LDError*> (obj)->contents));
1056 }
1057 }
1058
1059 // =============================================================================
1060 // -----------------------------------------------------------------------------
1061 int LDDocument::addObject (LDObject* obj)
1062 {
1063 getHistory()->add (new AddHistory (getObjects().size(), obj));
1064 m_Objects << obj;
1065
1066 if (obj->getType() == LDObject::EVertex)
1067 m_Vertices << obj;
1068
1069 #ifdef DEBUG
1070 if (!isImplicit())
1071 dlog ("Added object #%1 (%2)\n", obj->getID(), obj->getTypeName());
1072 #endif
1073
1074 obj->setFile (this);
1075 return getObjectCount() - 1;
1076 }
1077
1078 // =============================================================================
1079 // -----------------------------------------------------------------------------
1080 void LDDocument::addObjects (const LDObjectList objs)
1081 {
1082 for (LDObject* obj : objs)
1083 if (obj)
1084 addObject (obj);
1085 }
1086
1087 // =============================================================================
1088 // -----------------------------------------------------------------------------
1089 void LDDocument::insertObj (int pos, LDObject* obj)
1090 {
1091 getHistory()->add (new AddHistory (pos, obj));
1092 m_Objects.insert (pos, obj);
1093 obj->setFile (this);
1094
1095 #ifdef DEBUG
1096 if (!isImplicit())
1097 dlog ("Inserted object #%1 (%2) at %3\n", obj->getID(), obj->getTypeName(), pos);
1098 #endif
1099 }
1100
1101 // =============================================================================
1102 // -----------------------------------------------------------------------------
1103 void LDDocument::forgetObject (LDObject* obj)
1104 {
1105 int idx = obj->getIndex();
1106 obj->unselect();
1107 assert (m_Objects[idx] == obj);
1108
1109 if (!getHistory()->isIgnoring())
1110 getHistory()->add (new DelHistory (idx, obj));
1111
1112 m_Objects.removeAt (idx);
1113 obj->setFile (null);
1114 }
1115
1116 // =============================================================================
1117 // -----------------------------------------------------------------------------
1118 bool safeToCloseAll()
1119 {
1120 for (LDDocument* f : g_loadedFiles)
1121 if (!f->isSafeToClose())
1122 return false;
1123
1124 return true;
1125 }
1126
1127 // =============================================================================
1128 // -----------------------------------------------------------------------------
1129 void LDDocument::setObject (int idx, LDObject* obj)
1130 {
1131 assert (idx >= 0 && idx < m_Objects.size());
1132
1133 // Mark this change to history
1134 if (!m_History->isIgnoring())
1135 {
1136 QString oldcode = getObject (idx)->raw();
1137 QString newcode = obj->raw();
1138 *m_History << new EditHistory (idx, oldcode, newcode);
1139 }
1140
1141 m_Objects[idx]->unselect();
1142 m_Objects[idx]->setFile (null);
1143 obj->setFile (this);
1144 m_Objects[idx] = obj;
1145 }
1146
1147 // =============================================================================
1148 // Close all implicit files with no references
1149 // -----------------------------------------------------------------------------
1150 void LDDocument::closeUnused()
1151 {
1152 for (LDDocument* file : g_loadedFiles)
1153 if (file->isImplicit() && file->countReferences() == 0)
1154 delete file;
1155 }
1156
1157 // =============================================================================
1158 // -----------------------------------------------------------------------------
1159 LDObject* LDDocument::getObject (int pos) const
1160 {
1161 if (m_Objects.size() <= pos)
1162 return null;
1163
1164 return m_Objects[pos];
1165 }
1166
1167 // =============================================================================
1168 // -----------------------------------------------------------------------------
1169 int LDDocument::getObjectCount() const
1170 {
1171 return getObjects().size();
1172 }
1173
1174 // =============================================================================
1175 // -----------------------------------------------------------------------------
1176 bool LDDocument::hasUnsavedChanges() const
1177 {
1178 return !isImplicit() && getHistory()->getPosition() != getSavePosition();
1179 }
1180
1181 // =============================================================================
1182 // -----------------------------------------------------------------------------
1183 QString LDDocument::getDisplayName()
1184 {
1185 if (!getName().isEmpty())
1186 return getName();
1187
1188 if (!getDefaultName().isEmpty())
1189 return "[" + getDefaultName() + "]";
1190
1191 return tr ("<anonymous>");
1192 }
1193
1194 // =============================================================================
1195 // -----------------------------------------------------------------------------
1196 LDObjectList LDDocument::inlineContents (LDSubfile::InlineFlags flags)
1197 {
1198 // Possibly substitute with logoed studs:
1199 // stud.dat -> stud-logo.dat
1200 // stud2.dat -> stud-logo2.dat
1201 if (gl_logostuds && (flags & LDSubfile::RendererInline))
1202 {
1203 // Ensure logoed studs are loaded first
1204 loadLogoedStuds();
1205
1206 if (getName() == "stud.dat" && g_logoedStud)
1207 return g_logoedStud->inlineContents (flags);
1208 elif (getName() == "stud2.dat" && g_logoedStud2)
1209 return g_logoedStud2->inlineContents (flags);
1210 }
1211
1212 LDObjectList objs, objcache;
1213
1214 bool deep = flags & LDSubfile::DeepInline,
1215 doCache = flags & LDSubfile::CacheInline;
1216
1217 if (m_needsCache)
1218 {
1219 clearCache();
1220 doCache = true;
1221 }
1222
1223 // If we have this cached, just create a copy of that
1224 if (deep && getCache().isEmpty() == false)
1225 {
1226 for (LDObject* obj : getCache())
1227 objs << obj->createCopy();
1228 }
1229 else
1230 {
1231 if (!deep)
1232 doCache = false;
1233
1234 for (LDObject* obj : getObjects())
1235 {
1236 // Skip those without scemantic meaning
1237 if (!obj->isScemantic())
1238 continue;
1239
1240 // Got another sub-file reference, inline it if we're deep-inlining. If not,
1241 // just add it into the objects normally. Also, we only cache immediate
1242 // subfiles and this is not one. Yay, recursion!
1243 if (deep && obj->getType() == LDObject::ESubfile)
1244 {
1245 LDSubfile* ref = static_cast<LDSubfile*> (obj);
1246
1247 // We only want to cache immediate subfiles, so shed the caching
1248 // flag when recursing deeper in hierarchy.
1249 LDObjectList otherobjs = ref->inlineContents (flags & ~ (LDSubfile::CacheInline));
1250
1251 for (LDObject* otherobj : otherobjs)
1252 {
1253 // Cache this object, if desired
1254 if (doCache)
1255 objcache << otherobj->createCopy();
1256
1257 objs << otherobj;
1258 }
1259 }
1260 else
1261 {
1262 if (doCache)
1263 objcache << obj->createCopy();
1264
1265 objs << obj->createCopy();
1266 }
1267 }
1268
1269 if (doCache)
1270 setCache (objcache);
1271 }
1272
1273 return objs;
1274 }
1275
1276 // =============================================================================
1277 // -----------------------------------------------------------------------------
1278 LDDocument* LDDocument::current()
1279 {
1280 return m_curdoc;
1281 }
1282
1283 // =============================================================================
1284 // Sets the given file as the current one on display. At some point in time this
1285 // was an operation completely unheard of. ;)
1286 //
1287 // TODO: f can be temporarily null. This probably should not be the case.
1288 // -----------------------------------------------------------------------------
1289 void LDDocument::setCurrent (LDDocument* f)
1290 {
1291 // Implicit files were loaded for caching purposes and must never be set
1292 // current.
1293 if (f && f->isImplicit())
1294 return;
1295
1296 m_curdoc = f;
1297
1298 if (g_win && f)
1299 {
1300 // A ton of stuff needs to be updated
1301 g_win->updateDocumentListItem (f);
1302 g_win->buildObjList();
1303 g_win->updateTitle();
1304 g_win->R()->setFile (f);
1305 g_win->R()->repaint();
1306 log ("Changed file to %1", f->getDisplayName());
1307 }
1308 }
1309
1310 // =============================================================================
1311 // -----------------------------------------------------------------------------
1312 int LDDocument::countExplicitFiles()
1313 {
1314 int count = 0;
1315
1316 for (LDDocument* f : g_loadedFiles)
1317 if (f->isImplicit() == false)
1318 count++;
1319
1320 return count;
1321 }
1322
1323 // =============================================================================
1324 // This little beauty closes the initial file that was open at first when opening
1325 // a new file over it.
1326 // -----------------------------------------------------------------------------
1327 void LDDocument::closeInitialFile()
1328 {
1329 if (
1330 countExplicitFiles() == 2 &&
1331 g_loadedFiles[0]->getName().isEmpty() &&
1332 g_loadedFiles[1]->getName().isEmpty() == false &&
1333 !g_loadedFiles[0]->hasUnsavedChanges()
1334 )
1335 delete g_loadedFiles[0];
1336 }
1337
1338 // =============================================================================
1339 // -----------------------------------------------------------------------------
1340 void loadLogoedStuds()
1341 {
1342 if (g_logoedStud && g_logoedStud2)
1343 return;
1344
1345 delete g_logoedStud;
1346 delete g_logoedStud2;
1347
1348 g_logoedStud = openDocument ("stud-logo.dat", true);
1349 g_logoedStud2 = openDocument ("stud2-logo.dat", true);
1350
1351 log (LDDocument::tr ("Logoed studs loaded.\n"));
1352 }
1353
1354 // =============================================================================
1355 // -----------------------------------------------------------------------------
1356 void LDDocument::addToSelection (LDObject* obj) // [protected]
1357 {
1358 if (obj->isSelected())
1359 return;
1360
1361 assert (obj->getFile() == this);
1362 m_sel << obj;
1363 obj->setSelected (true);
1364 }
1365
1366 // =============================================================================
1367 // -----------------------------------------------------------------------------
1368 void LDDocument::removeFromSelection (LDObject* obj) // [protected]
1369 {
1370 if (!obj->isSelected())
1371 return;
1372
1373 assert (obj->getFile() == this);
1374 m_sel.removeOne (obj);
1375 obj->setSelected (false);
1376 }
1377
1378 // =============================================================================
1379 // -----------------------------------------------------------------------------
1380 void LDDocument::clearSelection()
1381 {
1382 for (LDObject* obj : m_sel)
1383 removeFromSelection (obj);
1384
1385 assert (m_sel.isEmpty());
1386 }
1387
1388 // =============================================================================
1389 // -----------------------------------------------------------------------------
1390 const LDObjectList& LDDocument::getSelection() const
1391 {
1392 return m_sel;
1393 }
1394
1395 // =============================================================================
1396 // -----------------------------------------------------------------------------
1397 void LDDocument::swapObjects (LDObject* one, LDObject* other)
1398 {
1399 int a = m_Objects.indexOf (one);
1400 int b = m_Objects.indexOf (other);
1401 assert (a != b && a != -1 && b != -1);
1402 m_Objects[b] = one;
1403 m_Objects[a] = other;
1404 addToHistory (new SwapHistory (one->getID(), other->getID()));
1405 }
1406
1407 // =============================================================================
1408 // -----------------------------------------------------------------------------
1409 QString LDDocument::shortenName (QString a) // [static]
1410 {
1411 QString shortname = basename (a);
1412 QString topdirname = basename (dirname (a));
1413
1414 if (g_specialSubdirectories.contains (topdirname))
1415 shortname.prepend (topdirname + "\\");
1416
1417 return shortname;
1418 }
1419
1420 // =============================================================================
1421 // -----------------------------------------------------------------------------
1422 void LDDocument::addReference (LDDocumentPointer* ptr)
1423 {
1424 pushToReferences (ptr);
1425 }
1426
1427 // =============================================================================
1428 // -----------------------------------------------------------------------------
1429 void LDDocument::removeReference (LDDocumentPointer* ptr)
1430 {
1431 removeFromReferences (ptr);
1432
1433 if (getReferences().size() == 0)
1434 invokeLater (closeUnused);
1435 }

mercurial