1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2011 Rallaz (rallazz@gmail.com)
7 **
8 **
9 ** This file is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 #ifndef DOCUMENT_INTERFACE_H
27 #define DOCUMENT_INTERFACE_H
28 
29 #include <QPointF>
30 #include <QHash>
31 #include <QVariant>
32 #include<vector>
33 //#include <QColor>
34 class QString;
35 
36 namespace DPI {
37     //! Vertical alignments.
38     enum VAlign {
39         VAlignTop,      /*!< Top. */
40         VAlignMiddle,   /*!< Middle */
41         VAlignBottom    /*!< Bottom */
42     };
43 
44     //! Horizontal alignments.
45     enum HAlign {
46         HAlignLeft,     /*!< Left */
47         HAlignCenter,   /*!< Centered */
48         HAlignRight     /*!< Right */
49     };
50 
51 
52    //! Entity's type.
53     enum ETYPE {
54         POINT,
55         LINE,
56         CONSTRUCTIONLINE,
57         CIRCLE,
58         ARC,
59         ELLIPSE,
60         IMAGE,
61         OVERLAYBOX,
62         SOLID,/*end atomicEntity, start entityContainer*/
63         MTEXT,
64         TEXT,
65         INSERT,
66         POLYLINE,
67         SPLINE,
68 		SPLINEPOINTS,
69         HATCH,
70         DIMLEADER,
71         DIMALIGNED,
72         DIMLINEAR,
73         DIMRADIAL,
74         DIMDIAMETRIC,
75         DIMANGULAR,
76         UNKNOWN
77     };
78 
79     //! Entity's data (dxf like).
80 
81     enum EDATA {
82         ETYPE=0,        /*!< enum: entity type */
83         TEXTCONTENT=1,  /*!< QString: text string */
84         BLKNAME=2,      /*!< QString: block name */
85         EID=5,          /*!< qulonglong: entity identifier */
86         LTYPE=6,        /*!< QString: line type */
87         TXTSTYLE=7,     /*!< QString: text style */
88         LAYER=8,        /*!< QString: layer */
89         STARTX=10,      /*!< double: start x coordinate */
90         ENDX=11,        /*!< double: end x coordinate, or U-vector for images */
91         VVECTORX=12,    /*!< double: V-vector for images, x coordinate */
92         SIZEU=13,       /*!< double: image U size (width) */
93         STARTY=20,      /*!< double: start y coordinate */
94         ENDY=21,        /*!< double: end y coordinate, or U-vector for images */
95         VVECTORY=22,    /*!< double: V-vector for images, y coordinate */
96         SIZEV=23,       /*!< double: image V size (height) */
97         STARTZ=30,      /*!< double: start z coordinate always 0 */
98         ENDZ=30,        /*!< double: end z coordinate always 0 */
99         LWIDTH=38,      /*!< QString: line width */  //verify number
100         RADIUS=39,      /*!< double: radius */
101         HEIGHT=40,      /*!< double: text height or ellipse ratio*/
102         XSCALE=41,      /*!< double: x insert scale */
103         YSCALE=42,      /*!< double: y insert scale */
104         ZSCALE=43,      /*!< double: z insert scale always 1? */
105         COLSPACE = 44,  ///< double: insert column spacing
106         ROWSPACE = 45,  ///< double: insert row spacing
107         LTSCALE=48,     /*!< line type scale (not in LibreCAD) */
108         STARTANGLE=50,  /*!< double: arc start angle or rotation angle for insert and text */
109         ENDANGLE=51,    /*!< double: arc end angle */
110         VISIBLE=60,     /*!< int: 1 visible, 0 invisible (reversed from dxf spec, but more logic*/
111         COLOR=62,       /*!< int: -1 ByLayer, -2 ByBlock, other 24 bit RGB color: entity color */
112         CLOSEPOLY=70,   /*!< int: closed polyline 0=open, 1=closed */
113         COLCOUNT = 70,  ///< int: insert number of columns
114         ROWCOUNT = 71,  ///< int: insert number of rows
115         TXTALIGNH=72,   /*!< enum: horizontal alignment for text */
116         TXTALIGNV=73,   /*!< enum: vertical alignment for text */
117         REVERSED=291 /*!< bool: true if arc is reversed (clockwise) */
118     };
119 //Note about 24 bit RGB color:
120 //    decimal integer 1632041, in hex 18 E7 29, in RGB dec 24,231,41
121 // example red RGB dec 255,0,0; hex FF0000; decimal integer 16711680
122 
123     enum LineWidth {
124         Width00 = 0,       /**< Width 1.  (0.00mm) */
125         Width01 = 5,       /**< Width 2.  (0.05mm) */
126         Width02 = 9,       /**< Width 3.  (0.09mm) */
127         Width03 = 13,      /**< Width 4.  (0.13mm) */
128         Width04 = 15,      /**< Width 5.  (0.15mm) */
129         Width05 = 18,      /**< Width 6.  (0.18mm) */
130         Width06 = 20,      /**< Width 7.  (0.20mm) */
131         Width07 = 25,      /**< Width 8.  (0.25mm) */
132         Width08 = 30,      /**< Width 9.  (0.30mm) */
133         Width09 = 35,      /**< Width 10. (0.35mm) */
134         Width10 = 40,      /**< Width 11. (0.40mm) */
135         Width11 = 50,      /**< Width 12. (0.50mm) */
136         Width12 = 53,      /**< Width 13. (0.53mm) */
137         Width13 = 60,      /**< Width 14. (0.60mm) */
138         Width14 = 70,      /**< Width 15. (0.70mm) */
139         Width15 = 80,      /**< Width 16. (0.80mm) */
140         Width16 = 90,      /**< Width 17. (0.90mm) */
141         Width17 = 100,     /**< Width 18. (1.00mm) */
142         Width18 = 106,     /**< Width 19. (1.06mm) */
143         Width19 = 120,     /**< Width 20. (1.20mm) */
144         Width20 = 140,     /**< Width 21. (1.40mm) */
145         Width21 = 158,     /**< Width 22. (1.58mm) */
146         Width22 = 200,     /**< Width 23. (2.00mm) */
147         Width23 = 211,     /**< Width 24. (2.11mm) */
148         WidthByLayer = -1, /**< Line width defined by layer not entity. */
149         WidthByBlock = -2, /**< Line width defined by block not entity. */
150         WidthDefault = -3  /**< Line width defaults to the predefined line width. */
151     };
152 
153     enum LineType {
154         NoPen = 0,            /**< No line at all. */
155         SolidLine = 1,        /**< Normal line. */
156         DotLine = 2,          /**< Dotted line. */
157         DotLine2 = 3,         /**< Dotted line small. */
158         DotLineX2 = 4,        /**< Dotted line large. */
159         DashLine = 5,         /**< Dashed line. */
160         DashLine2 = 6,        /**< Dashed line small. */
161         DashLineX2 = 7,       /**< Dashed line large. */
162         DashDotLine = 8,      /**< Alternate dots and dashes. */
163         DashDotLine2 = 9,     /**< Alternate dots and dashes small. */
164         DashDotLineX2 = 10,   /**< Alternate dots and dashes large. */
165         DivideLine = 11,      /**< dash, dot, dot. */
166         DivideLine2 = 12,     /**< dash, dot, dot small. */
167         DivideLineX2 = 13,    /**< dash, dot, dot large. */
168         CenterLine = 14,      /**< dash, small dash. */
169         CenterLine2 = 15,     /**< dash, small dash small. */
170         CenterLineX2 = 16,    /**< dash, small dash large. */
171         BorderLine = 17,      /**< dash, dash, dot. */
172         BorderLine2 = 18,     /**< dash, dash, dot small. */
173         BorderLineX2 = 19,    /**< dash, dash, dot large. */
174         LineByLayer = -1,     /**< Line type defined by layer not entity */
175         LineByBlock = -2      /**< Line type defined by block not entity */
176     };
177 
178     enum EntColor {
179         das,
180     };
181 
182 }
183 
184 class Plug_VertexData
185 {
186 public:
Plug_VertexData(QPointF p,double b)187     Plug_VertexData(QPointF p, double b){
188         point = p;
189         bulge = b;
190     }
191     QPointF point;
192     double bulge;
193 };
194 
195 //! Wrapper for access entities from plugins.
196  /*!
197  *  Wrapper class for create, access and modify entities from plugins.
198  *  TODO: terminate access function -> getData()
199  *        terminate implementation of modify function -> updateData()
200  *        terminate implementation of create function (ctor called from document)
201  *           can't create entities:
202  *           - atomic = CONSTRUCTIONLINE, OVERLAYBOX, SOLID,
203  *           - container = INSERT, POLYLINE, SPLINE, HATCH, DIMLEADER,
204  *             DIMALIGNED, DIMLINEAR, DIMRADIAL, DIMDIAMETRIC, DIMANGULAR,
205  *        verify destructor if needed or not to delete entity
206  * @author Rallaz
207  */
208 class Plug_Entity
209 {
210 public:
~Plug_Entity()211     virtual ~Plug_Entity() {}
212 
213     //! Obtain the entity data.
214     /*!
215     * The data is a QHash with the EDATA keys relevant to the type of entity
216     *  \param data pointer to a QHash<int, QVariant> to store the entity data.
217     */
218     virtual void getData(QHash<int, QVariant> *data) = 0;
219 
220     //! Update the entity data.
221     /*!
222     * The data is a QHash with the EDATA keys relevant to the type of entity
223     *  \param data pointer to a QHash<int, QVariant> with the entity data.
224     */
225     virtual void updateData(QHash<int, QVariant> *data) = 0;
226 
227     //! Obtain the polyline list of vertex.
228     /*!
229     * The data is a QList to store a Plug_VertexData for heach vertex form the polyline
230     *  \param data pointer to a QList<Plug_VertexData> to store the vertex list.
231     */
232     virtual void getPolylineData(QList<Plug_VertexData> *data) = 0;
233 
234     //! Update the polyline list of vertex.
235     /*!
236     * The data is a QList of Plug_VertexData with the coordinates of each vertex to the
237     * polyline the cuurent vertex are removed and replaced for the new list.
238     *  \param data pointer to a QList<Plug_VertexData> with the coordinates vertex's.
239     */
240     virtual void updatePolylineData(QList<Plug_VertexData> *data) = 0;
241 
242     //! Move the entity.
243     /*!
244     *  \param offset move the entity by the given QPointF.
245     */
246     virtual void move(QPointF offset) = 0;
247 
248 	virtual void moveRotate(QPointF const& offset, QPointF const& center, double angle)=0;
249 
250     //! rotate the entity.
251     /*!
252     *  \param center center of rotation.
253     *  \param angle angle to rotate.
254     */
255     virtual void rotate(QPointF center, double angle) = 0;
256 
257     //! Scale the entity.
258     /*!
259     *  \param center base point for scale.
260     *  \param factor scale factor.
261     */
262     virtual void scale(QPointF center, QPointF factor) = 0;
263 
264     //! Utility: Get color as string.
265     /*!
266     *  \param color color as integer to convert as string.
267     */
268     virtual QString intColor2str(int color) = 0;
269 };
270 
271 //! Interface for communicate plugins.
272  /*!
273  * Class for communicate plugins with document (drawing).
274  * entities without add*() function:
275  * atomic = CONSTRUCTIONLINE, OVERLAYBOX, SOLID,
276  * container = INSERT, POLYLINE, SPLINE, HATCH, DIMLEADER,
277  * DIMALIGNED, DIMLINEAR, DIMRADIAL, DIMDIAMETRIC, DIMANGULAR,
278  * TODO: memory assignation and cleanup in plugin
279  *   added----    implementation of function -> deleteEntity()
280  * @author Rallaz
281  */
282 class Document_Interface
283 {
284 public:
285     Document_Interface() = default;
286     virtual ~Document_Interface() = default;
287     //! Force to update the graphic view.
288     /*! Force to update the graphic view.
289     */
290     virtual void updateView() = 0;
291 
292     //! Add point entity to current document.
293     /*! Add point entity to current document with current attributes.
294     *  \param start point coordinate.
295     */
296     virtual void addPoint(QPointF *start) = 0;
297 
298     //! Add line entity to current document.
299     /*! Add line entity to current document with current attributes.
300     *  \param start start point coordinate.
301     *  \param end end point coordinate.
302     */
303     virtual void addLine(QPointF *start, QPointF *end) = 0;
304 
305     //! Add text entity to current document.
306     /*! Add text entity to current document with current attributes
307     *  \param txt a QString with text content
308     *  \param sty a QString with text style name
309     *  \param start insertion point coordinate
310     *  \param height height of text
311     *  \param angle rotation angle of text
312     *  \param ha horizontal alignment of text
313     *  \param va vertical alignment of text
314     */
315     virtual void addText(QString txt, QString sty, QPointF *start, double height,
316                 double angle, DPI::HAlign ha,  DPI::VAlign va) = 0;
317 
318     //! Add circle entity to current document.
319     /*! Add circle entity to current document with current attributes.
320     *  \param start center point coordinate.
321     *  \param radius radius for circle.
322     */
323     virtual void addCircle(QPointF *start, qreal radius) = 0;
324 
325     //! Add arc entity to current document.
326     /*! Add arc of circumference entity to current document with current attributes.
327     *  \param start center point coordinate.
328     *  \param radius radius for circle.
329     */
330     virtual void addArc(QPointF *start, qreal radius, qreal a1, qreal a2) = 0;
331 
332     //! Add ellipse entity to current document.
333     /*! Add ellipse entity to current document with current attributes.
334     *  \param start center point coordinate.
335     *  \param radius radius for arc.
336     */
337     virtual void addEllipse(QPointF *start, QPointF *end, qreal ratio, qreal a1, qreal a2) = 0;
338 
339     //! Add addLine entity to current document.
340     /*! Add addLine entity to current document with current attributes; polyline is by lines.
341     *  \param points of line segments
342     *  \param closed whether line is closed
343     */
344     virtual void addLines(std::vector<QPointF> const& points, bool closed=false) = 0;
345 
346     //! Add polyline entity to current document.
347     /*! Add polyline entity to current document with current attributes.
348     *  \param points polyline points
349     *  \param closed whether polyline is closed
350     */
351     virtual void addPolyline(std::vector<Plug_VertexData> const& points, bool closed=false) = 0;
352     //! Add LC_SplinePoints entity to current document.
353     /*! Add splinepoints entity to current document with current attributes.
354     *  \param points interpolation points
355     *  \param closed whether splinepoints is closed
356     */
357     virtual void addSplinePoints(std::vector<QPointF> const& points, bool closed=false) = 0;
358 
359     //! Add image entity to current document.
360     /*! Add image entity to current document with current attributes.
361     *  \param start start point coordinate.
362     *  \param end end point coordinate.
363     */
364     virtual void addImage(int handle, QPointF *start, QPointF *uvr, QPointF *vvr,
365                   int w, int h, QString name, int br, int con, int fade) = 0;
366 
367     //! Add insert entity to current document.
368     /*! Add a block insert entity to current document with current attributes.
369     *  \param name name of block to insert.
370     *  \param ins insertion point coordinate.
371     *  \param scale x,y scale factor.
372     *  \param rot rotation angle.
373     */
374     virtual void addInsert(QString name, QPointF ins, QPointF scale, qreal rot) = 0;
375 
376     //! Add block definition from disk to current document.
377     /*! Add block definition from disk to current document.
378     *  \param fullName path+name of dxf file to add.
379     *  \return name of created block or NULL if fail.
380     */
381     virtual QString addBlockfromFromdisk(QString fullName) = 0;
382 
383     //! Add a entity to current document.
384     /*! Add a entity to current document with the data sets with Plug_Entity.updateData().
385     *  \param handle a pointer to Plug_Entity.
386     */
387     virtual void addEntity(Plug_Entity *handle) = 0;
388 
389     //! Create a new Plug_Entity.
390     /*! Create a new Plug_Entity of type ETYPE with default data.
391     * sets the data with Plug_Entity.updateData().
392     * if fail (unknoun or unhandled entity) return NULL
393     *  \param type an DPI::ETYPE type.
394     *  \return handle to pointer of Plug_Entity with type DPI::ETYPE or NULL if fail.
395     */
396     virtual Plug_Entity *newEntity(enum DPI::ETYPE type) = 0;
397 
398     //! Remove a Entity.
399     /*! Remove a entity from current drawing.
400     *  \param ent handle to pointer of Plug_Entity.
401     */
402     virtual void removeEntity(Plug_Entity *ent) = 0;
403 
404     //! Set the current layer in current document.
405     /*! Set the current layer in current document, if not exist create it.
406     *  \param name a QString with the name of the layer.
407     */
408     virtual void setLayer(QString name) = 0;
409 
410     //! Get the current layer in current document.
411     /*! Get the current layer in current document.
412     *  \return The name of the current layer.
413     */
414     virtual QString getCurrentLayer() = 0;
415 
416     //! Gets the layers list in current document.
417     /*! Gets the list of names of all layers in current document.
418     *  \return A list with the name of all layers in document.
419     */
420     virtual QStringList getAllLayer() = 0;
421 
422     //! Gets the blocks list in current document.
423     /*! Gets the list of names of all blocks in current document.
424     *  \return A list with the name of all blocks in document.
425     */
426     virtual QStringList getAllBlocks() = 0;
427 
428     //! Delete a layer in current document.
429     /*! Delete the layer "name" in current document if it exist.
430     *  \return The name of the current layer.
431     */
432     virtual bool deleteLayer(QString name) = 0;
433 
434     virtual void getCurrentLayerProperties(int *c, DPI::LineWidth *w, DPI::LineType *t) = 0;
435     virtual void getCurrentLayerProperties(int *c, QString *w, QString *t) = 0;
436     virtual void setCurrentLayerProperties(int c, DPI::LineWidth w, DPI::LineType t) = 0;
437 	virtual void setCurrentLayerProperties(int c, QString const& w,
438 										   QString const& t) = 0;
439 
440     //! Gets a point.
441     /*! Prompt message or an default message to the user asking for a point.
442     *   If base is present draw a line from base to cursor position.
443     * \param point a pointer to QPointF to store the obtained point.
444     * \param message an optional QString with prompt message.
445     * \param base visual helper point, if present.
446     * \return true if success.
447     * \return false if fail, i.e. user cancel.
448     */
449     virtual bool getPoint(QPointF *point, const QString& message = "", QPointF *base = 0) = 0;
450 
451     //! Select a entity.
452     /*! Prompt message or a default message to the user asking for a single selection.
453     * You can delete the Plug_Entity wen no more needed.
454     * \param message an optional QString with prompt message.
455     * \return a Plug_Entity handle the selected entity or NULL.
456     */
457     virtual Plug_Entity *getEnt(const QString& message = "") = 0;
458 
459     //! Gets a entities selection.
460     /*! Prompt message or an default message to the user asking for a selection.
461     * You can delete all, the Plug_Entity and the returned QList wen no more needed.
462     * \param sel a QList of pointers to Plug_Entity handled the selected entities.
463     * \param message an optional QString with prompt message.
464     * \return true if success.
465     * \return false if fail, i.e. user cancel.
466     */
467     virtual bool getSelect(QList<Plug_Entity *> *sel, const QString& message = "") = 0;
468 
469     //! Gets all entities in document.
470     /*! You can delete all, the Plug_Entity and the returned QList wen no more needed.
471     * \param sel a QList of pointers to Plug_Entity handled the selected entities.
472     * \param visible default for false, do not select entities in hidden layers.
473     * \return true if success.
474     * \return false if fail, i.e. user cancel.
475     */
476     virtual bool getAllEntities(QList<Plug_Entity *> *sel, bool visible = false) = 0;
477 
478     virtual bool getVariableInt(const QString& key, int *num) = 0;
479     virtual bool getVariableDouble(const QString& key, double *num) = 0;
480     virtual bool addVariable(const QString& key, int value, int code=70) = 0;
481     virtual bool addVariable(const QString& key, double value, int code=40) = 0;
482 
483     virtual bool getInt(int *num, const QString& message = "", const QString& title = "") = 0;
484     virtual bool getReal(qreal *num, const QString& message = "", const QString& title = "") = 0;
485     virtual bool getString(QString *txt, const QString& message = "", const QString& title = "") = 0;
486 
487     //! Convert real to string.
488     /*! Convert a real number to string using indicated units format & precision. If omitted
489     * are the current drawing units & precision are used.
490     * \param num Number to convert.
491     * \param units Units format to use. current configured=0, Scientific=1,
492     * Decimal=2, Engineering=3, Architectural=4, Fractional=5,
493     * ArchitecturalMetric=6.
494     * \param prec number of decimals added in the string.
495     * \return a string with the converted number.
496     */
497     virtual QString realToStr(const qreal num, const int units = 0, const int prec = 0) = 0;
498 };
499 
500 
501 #endif // DOCUMENT_INTERFACE_H
502