]> Shamusworld >> Repos - architektonas/blob - src/base/pattern.cpp
Fixed problem with MDI activation.
[architektonas] / src / base / pattern.cpp
1 // pattern.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/01/2010  Added this text. :-)
15 //
16
17 #include "pattern.h"
18
19 #include <QtCore>
20 #include "system.h"
21 #include "fileio.h"
22 #include "drawing.h"
23
24 /**
25  * Constructor.
26  *
27  * @param fileName File name of a DXF file defining the pattern
28  */
29 Pattern::Pattern(const QString & file): EntityContainer(NULL),
30         filename(file), loaded(false)
31 {
32         DEBUG->print("Pattern::Pattern() ");
33 }
34
35 Pattern::~Pattern()
36 {
37 }
38
39 /**
40  * Loads the given pattern file into this pattern.
41  * Entities other than lines are ignored.
42  *
43  * @param filename File name of the pattern file (without path and
44  * extension or full path.
45  */
46 bool Pattern::loadPattern()
47 {
48         if (loaded)
49                 return true;
50
51         DEBUG->print("Pattern::loadPattern");
52         QString path;
53
54         // Search for the appropriate pattern if we have only the name of the pattern:
55         if (!filename.toLower().contains(".dxf"))
56         {
57                 QStringList patterns = SYSTEM->getPatternList();
58                 QFileInfo file;
59
60                 for(QStringList::Iterator it=patterns.begin(); it!=patterns.end(); it++)
61                 {
62                         if (QFileInfo(*it).baseName().toLower() == filename.toLower())
63                         {
64                                 path = *it;
65                                 DEBUG->print("Pattern found: %s", path.toAscii().data());
66                                 break;
67                         }
68                 }
69         }
70         // We have the full path of the pattern:
71         else
72         {
73                 path = filename;
74         }
75
76         // No pattern paths found:
77         if (path.isEmpty())
78         {
79                 DEBUG->print("No pattern \"%s\"available.", filename.toAscii().data());
80                 return false;
81         }
82
83         Drawing * drawing = new Drawing();
84
85         // TODO: Find out why the new dxf filter doesn't work for patterns:
86         FILEIO->fileImport(*drawing, path, RS2::FormatDXF1);
87
88         for(Entity * e=drawing->firstEntity(); e!=NULL; e=drawing->nextEntity())
89         {
90                 if (e->rtti() == RS2::EntityLine || e->rtti() == RS2::EntityArc)
91                 {
92                         Entity * clone = e->clone();
93                         clone->reparent(this);
94                         Layer * layer = e->getLayer();
95
96                         if (layer)
97                                 clone->setLayer(layer->getName());
98
99                         addEntity(clone);
100                 }
101         }
102
103         delete drawing;
104         loaded = true;
105         DEBUG->print("Pattern::loadPattern: OK");
106
107         return true;
108 }
109
110 /** @return the fileName of this pattern. */
111 QString Pattern::getFileName() const
112 {
113         return filename;
114 }