1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2015 A. Stebich (librecad@mail.lordofbikes.de)
6 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
7 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
8 **
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file gpl-2.0.txt included in the
13 ** packaging of this file.
14 **
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ** GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 **
24 ** This copyright notice MUST APPEAR in all copies of the script!
25 **
26 **********************************************************************/
27 
28 
29 #ifndef RS_GRAPHIC_H
30 #define RS_GRAPHIC_H
31 
32 #include <QDateTime>
33 #include "rs_blocklist.h"
34 #include "rs_layerlist.h"
35 #include "rs_variabledict.h"
36 #include "rs_document.h"
37 #include "rs_units.h"
38 
39 class RS_VariableDict;
40 class QG_LayerWidget;
41 
42 /**
43  * A graphic document which can contain entities layers and blocks.
44  *
45  * @author Andrew Mustun
46  */
47 class RS_Graphic : public RS_Document {
48 public:
49     RS_Graphic(RS_EntityContainer* parent=NULL);
50     virtual ~RS_Graphic();
51 
52     //virtual RS_Entity* clone() {
53     //	return new RS_Graphic(*this);
54     //}
55 
56     /** @return RS2::EntityGraphic */
rtti()57     virtual RS2::EntityType rtti() const {
58         return RS2::EntityGraphic;
59     }
60 
61     virtual unsigned long int countLayerEntities(RS_Layer* layer);
62 
getLayerList()63     virtual RS_LayerList* getLayerList() {
64         return &layerList;
65     }
getBlockList()66     virtual RS_BlockList* getBlockList() {
67         return &blockList;
68     }
69 
70     virtual void newDoc();
71     virtual bool save(bool isAutoSave = false);
72     virtual bool saveAs(const QString& filename, RS2::FormatType type, bool force = false);
73     virtual bool open(const QString& filename, RS2::FormatType type);
74     bool loadTemplate(const QString &filename, RS2::FormatType type);
75 
76         // Wrappers for Layer functions:
clearLayers()77     void clearLayers() {
78         layerList.clear();
79     }
countLayers()80     unsigned countLayers() const {
81         return layerList.count();
82     }
layerAt(unsigned i)83     RS_Layer* layerAt(unsigned i) {
84         return layerList.at(i);
85     }
activateLayer(const QString & name)86     void activateLayer(const QString& name) {
87         layerList.activate(name);
88     }
activateLayer(RS_Layer * layer)89     void activateLayer(RS_Layer* layer) {
90         layerList.activate(layer);
91     }
getActiveLayer()92     RS_Layer* getActiveLayer() {
93         return layerList.getActive();
94     }
addLayer(RS_Layer * layer)95     virtual void addLayer(RS_Layer* layer) {
96         layerList.add(layer);
97     }
98     virtual void addEntity(RS_Entity* entity);
99     virtual void removeLayer(RS_Layer* layer);
editLayer(RS_Layer * layer,const RS_Layer & source)100     virtual void editLayer(RS_Layer* layer, const RS_Layer& source) {
101         layerList.edit(layer, source);
102     }
findLayer(const QString & name)103     RS_Layer* findLayer(const QString& name) {
104         return layerList.find(name);
105     }
toggleLayer(const QString & name)106     void toggleLayer(const QString& name) {
107         layerList.toggle(name);
108     }
toggleLayer(RS_Layer * layer)109     void toggleLayer(RS_Layer* layer) {
110         layerList.toggle(layer);
111     }
toggleLayerLock(RS_Layer * layer)112     void toggleLayerLock(RS_Layer* layer) {
113         layerList.toggleLock(layer);
114     }
toggleLayerPrint(RS_Layer * layer)115     void toggleLayerPrint(RS_Layer* layer) {
116         layerList.togglePrint(layer);
117     }
toggleLayerConstruction(RS_Layer * layer)118     void toggleLayerConstruction(RS_Layer* layer) {
119         layerList.toggleConstruction(layer);
120     }
freezeAllLayers(bool freeze)121     void freezeAllLayers(bool freeze) {
122         layerList.freezeAll(freeze);
123     }
lockAllLayers(bool lock)124     void lockAllLayers(bool lock) {
125         layerList.lockAll(lock);
126     }
127 
addLayerListListener(RS_LayerListListener * listener)128     void addLayerListListener(RS_LayerListListener* listener) {
129         layerList.addListener(listener);
130     }
removeLayerListListener(RS_LayerListListener * listener)131     void removeLayerListListener(RS_LayerListListener* listener) {
132         layerList.removeListener(listener);
133     }
134 
135 
136         // Wrapper for block functions:
clearBlocks()137     void clearBlocks() {
138         blockList.clear();
139     }
countBlocks()140     unsigned countBlocks() {
141         return blockList.count();
142     }
blockAt(unsigned i)143     RS_Block* blockAt(unsigned i) {
144         return blockList.at(i);
145     }
activateBlock(const QString & name)146     void activateBlock(const QString& name) {
147         blockList.activate(name);
148     }
activateBlock(RS_Block * block)149     void activateBlock(RS_Block* block) {
150         blockList.activate(block);
151     }
getActiveBlock()152     RS_Block* getActiveBlock() {
153         return blockList.getActive();
154     }
155     virtual bool addBlock(RS_Block* block, bool notify=true) {
156         return blockList.add(block, notify);
157     }
addBlockNotification()158     virtual void addBlockNotification() {
159         blockList.addNotification();
160     }
removeBlock(RS_Block * block)161     virtual void removeBlock(RS_Block* block) {
162         blockList.remove(block);
163     }
findBlock(const QString & name)164     RS_Block* findBlock(const QString& name) {
165         return blockList.find(name);
166     }
newBlockName()167     QString newBlockName() {
168         return blockList.newName();
169     }
toggleBlock(const QString & name)170     void toggleBlock(const QString& name) {
171         blockList.toggle(name);
172     }
toggleBlock(RS_Block * block)173     void toggleBlock(RS_Block* block) {
174         blockList.toggle(block);
175     }
freezeAllBlocks(bool freeze)176     void freezeAllBlocks(bool freeze) {
177         blockList.freezeAll(freeze);
178     }
addBlockListListener(RS_BlockListListener * listener)179     void addBlockListListener(RS_BlockListListener* listener) {
180         blockList.addListener(listener);
181     }
removeBlockListListener(RS_BlockListListener * listener)182     void removeBlockListListener(RS_BlockListListener* listener) {
183         blockList.removeListener(listener);
184     }
185 
186         // Wrappers for variable functions:
clearVariables()187     void clearVariables() {
188         variableDict.clear();
189     }
countVariables()190     int countVariables() {
191         return variableDict.count();
192     }
193 
addVariable(const QString & key,const RS_Vector & value,int code)194     void addVariable(const QString& key, const RS_Vector& value, int code) {
195         variableDict.add(key, value, code);
196     }
addVariable(const QString & key,const QString & value,int code)197     void addVariable(const QString& key, const QString& value, int code) {
198         variableDict.add(key, value, code);
199     }
addVariable(const QString & key,int value,int code)200     void addVariable(const QString& key, int value, int code) {
201         variableDict.add(key, value, code);
202     }
addVariable(const QString & key,double value,int code)203     void addVariable(const QString& key, double value, int code) {
204         variableDict.add(key, value, code);
205     }
206 
getVariableVector(const QString & key,const RS_Vector & def)207     RS_Vector getVariableVector(const QString& key, const RS_Vector& def) {
208         return variableDict.getVector(key, def);
209     }
getVariableString(const QString & key,const QString & def)210     QString getVariableString(const QString& key, const QString& def) {
211         return variableDict.getString(key, def);
212     }
getVariableInt(const QString & key,int def)213     int getVariableInt(const QString& key, int def) {
214         return variableDict.getInt(key, def);
215     }
getVariableDouble(const QString & key,double def)216     double getVariableDouble(const QString& key, double def) {
217         return variableDict.getDouble(key, def);
218     }
219 
removeVariable(const QString & key)220     void removeVariable(const QString& key) {
221         variableDict.remove(key);
222     }
223 
getVariableDict()224     QHash<QString, RS_Variable>& getVariableDict() {
225         return variableDict.getVariableDict();
226     }
227 
228     RS2::LinearFormat getLinearFormat();
229     RS2::LinearFormat getLinearFormat(int f);
230     int getLinearPrecision();
231     RS2::AngleFormat getAngleFormat();
232     int getAnglePrecision();
233 
234     RS_Vector getPaperSize();
235     void setPaperSize(const RS_Vector& s);
236     RS_Vector getPrintAreaSize(bool total=true);
237 
238     RS_Vector getPaperInsertionBase();
239     void setPaperInsertionBase(const RS_Vector& p);
240 
241     RS2::PaperFormat getPaperFormat(bool* landscape);
242     void setPaperFormat(RS2::PaperFormat f, bool landscape);
243 
244     double getPaperScale();
245     void setPaperScale(double s);
246 
247     virtual void setUnit(RS2::Unit u);
248     virtual RS2::Unit getUnit();
249 
250     bool isGridOn();
251     void setGridOn(bool on);
252     bool isIsometricGrid();
253     void setIsometricGrid(bool on);
254     void setCrosshairType(RS2::CrosshairType chType);
255     RS2::CrosshairType getCrosshairType();
256 
257     bool isDraftOn();
258     void setDraftOn(bool on);
259 
260     /** Sets the unit of this graphic's dimensions to 'u' */
261     /*virtual void setDimensionUnit(RS2::Unit u) {
262             addVariable("$INSUNITS", (int)u, 70);
263     dimensionUnit = u;
264     }*/
265 
266     /** Gets the unit of this graphic's dimension */
267     /*virtual RS2::Unit getDimensionUnit() {
268     return dimensionUnit;
269     }*/
270 
271     void centerToPage();
272     bool fitToPage();
273 
274     bool isBiggerThanPaper();
275 
276     /**
277      * @retval true The document has been modified since it was last saved.
278      * @retval false The document has not been modified since it was last saved.
279      */
isModified()280     virtual bool isModified() const {
281         return modified || layerList.isModified() || blockList.isModified();
282     }
283 
284     /**
285      * Sets the documents modified status to 'm'.
286      */
setModified(bool m)287     virtual void setModified(bool m) {
288         modified = m;
289         layerList.setModified(m);
290         blockList.setModified(m);
291     }
getModifyTime(void)292     virtual QDateTime getModifyTime(void){
293         return modifiedTime;
294     }
295 
296     //if set to true, will refuse to modify paper scale
setPaperScaleFixed(bool fixed)297     void setPaperScaleFixed(bool fixed)
298     {
299         paperScaleFixed=fixed;
300     }
getPaperScaleFixed()301     bool getPaperScaleFixed()
302     {
303         return paperScaleFixed;
304     }
305 
306     /**
307      * Paper margins in millimeters
308      */
setMargins(double left,double top,double right,double bottom)309     void setMargins(double left, double top, double right, double bottom)
310     {
311         if (left >= 0.0) marginLeft = left;
312         if (top >= 0.0) marginTop = top;
313         if (right >= 0.0) marginRight = right;
314         if (bottom >= 0.0) marginBottom = bottom;
315     }
getMarginLeft()316     double getMarginLeft() const
317     {
318         return marginLeft;
319     }
getMarginTop()320     double getMarginTop() const
321     {
322         return marginTop;
323     }
getMarginRight()324     double getMarginRight() const
325     {
326         return marginRight;
327     }
getMarginBottom()328     double getMarginBottom() const
329     {
330         return marginBottom;
331     }
332 
333     /**
334      * Paper margins in graphic units
335      */
336     void setMarginsInUnits(double left, double top, double right, double bottom);
337     double getMarginLeftInUnits();
338     double getMarginTopInUnits();
339     double getMarginRightInUnits();
340     double getMarginBottomInUnits();
341 
342     /**
343      * Number of pages drawing occupies
344      */
345     void setPagesNum(int horiz, int vert);
346     void setPagesNum(const QString &horizXvert);
getPagesNumHoriz()347     int getPagesNumHoriz() {
348         return pagesNumH;
349     }
getPagesNumVert()350     int getPagesNumVert() {
351         return pagesNumV;
352     }
353 
354     friend std::ostream& operator << (std::ostream& os, RS_Graphic& g);
355 
356     int clean();
357 
358 private:
359 
360         bool BackupDrawingFile(const QString &filename);
361         QDateTime modifiedTime;
362         QString currentFileName; //keep a copy of filename for the modifiedTime
363 
364         RS_LayerList layerList;
365         RS_BlockList blockList;
366         RS_VariableDict variableDict;
367         RS2::CrosshairType crosshairType; //crosshair type used by isometric grid
368         //if set to true, will refuse to modify paper scale
369         bool paperScaleFixed;
370 
371         // Paper margins in millimeters
372         double marginLeft;
373         double marginTop;
374         double marginRight;
375         double marginBottom;
376 
377         // Number of pages drawing occupies
378         int pagesNumH;
379         int pagesNumV;
380 };
381 
382 
383 #endif
384