1 // XDrawChem
2 // Copyright (C) 2004-2005  Bryan Herger <bherger@users.sourceforge.net>
3 // Copyright (C) 2020  Yaman Qalieh <ybq987@gmail.com>
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 <https://www.gnu.org/licenses/>.
17 
18 // text.h -- subclass of Drawable for text/labels
19 
20 #ifndef TEXT_H
21 #define TEXT_H
22 
23 #include "dpoint.h"
24 #include "drawable.h"
25 #include "render2d.h"
26 
27 #define TEXT_AUTO 0
28 #define TEXT_LALIGN 1
29 #define TEXT_CALIGN 2
30 #define TEXT_RALIGN 3
31 
32 class Molecule;
33 
34 class Text : public Drawable {
35   public:
36     Text(Render2D *, QObject *parent = 0);
37     Text *CloneTo(Drawable *target = nullptr) const;
38     void Render(); // draw this object
setRenderer(Render2D * r1)39     void setRenderer(Render2D *r1) { r = r1; }
40     int Type();          // return type of object
41     bool Find(DPoint *); // does this Text contain this DPoint?
42     DPoint *FindNearestPoint(DPoint *, double &);
43     Drawable *FindNearestObject(DPoint *, double &);
44     void setPoint(DPoint *);
Justify()45     int Justify() { return justify; }
setJustify(int a)46     void setJustify(int a) { justify = a; }
47     bool WithinBounds(DPoint *);
48     bool isWithinRect(QRect, bool);
49     void InsertCharacter(QKeyEvent *);
50     void InsertString(QString);
51     void DeleteKeyPressed();
52     QPoint GetTopLeftPoint(); // get real top left point, based on justify
53     QPoint NearestCenter(QPoint, int, int &);
54 
setText(QString nt1,QString nt2)55     void setText(QString nt1, QString nt2) {
56         if (start != 0) {
57             start->element = nt1;     // element info (plain text)
58             start->elementmask = nt2; // actual display text in underlying point!
59             displayText = nt2;
60         }
61     }
62 
setText(QString nt)63     void setText(QString nt) { setText(nt, nt); }
64 
getText()65     QString getText() {
66         if (start != 0)
67             return start->element;
68         else
69             return QString();
70     } // not just Text()!
71 
getRichText()72     QString getRichText() {
73         if (start != 0)
74             return displayText;
75         else
76             return QString();
77     }
78 
79     /*
80         void setTextMask( QString nm )
81         {
82             // this function is (probably) obsolete.  Use setText() instead.
83             //if (start != 0) start->elementmask = nm;
84         }
85     */
86 
getTextMask()87     QString getTextMask() {
88         if (start != 0)
89             return start->elementmask;
90         else
91             return QString();
92     }
93 
94     QFont subscriptFont(QFont);
setMolecule(Molecule * m1)95     void setMolecule(Molecule *m1) { molecule = m1; }
getMolecule()96     Molecule *getMolecule() { return molecule; }
setDataType(int d1)97     void setDataType(int d1) { DataType = d1; }
getDataType()98     int getDataType() { return DataType; }
getFont()99     const QFont &getFont() { return font; }
setFont(const QFont & f_in)100     void setFont(const QFont &f_in) { font = f_in; }
101     void MoveCursor(DPoint *);
102     void Select(DPoint *, DPoint *);
103     QRect BoundingBox();
104     void DoSuperscript();
105     void DoSubscript();
106     void DoBold();
107     void DoItalic();
108     void DoUnderline();
DeselectAllText()109     void DeselectAllText() {
110         selectMin = -1;
111         selectMax = -1;
112     }
113     // set text parsed from XML file
SetTextFromXML(QString)114     void SetTextFromXML(QString) {}
115     QString ToXML(QString);
116     QString ToCDXML(QString);
117     void FromXML(QString);
118     void SetFontFromXML(QString);
119     void SetTextstringFromXML(QString);
120     void SetRichstringFromXML(QString);
121     void SetTextmaskFromXML(QString);
122     void SetBorderColorFromXML(QString);
123     void SetFillColorFromXML(QString);
124     void SetShapeGeometryFromXML(QString);
125     void CheckAlignment(int);
126     void ForceAlignment(int);
127     void UpdateDisplayText();
128     // set shape/border/fill
setShape(int s1)129     void setShape(int s1) { shape = s1; }
getShape()130     int getShape() { return shape; }
setBorder(bool b1)131     void setBorder(bool b1) { drawBorder = b1; }
getBorder()132     bool getBorder() { return drawBorder; }
setBorderColor(QColor c1)133     void setBorderColor(QColor c1) { borderColor = c1; }
getBorderColor()134     QColor getBorderColor() { return borderColor; }
setFill(bool b1)135     void setFill(bool b1) { drawFill = b1; }
getFill()136     bool getFill() { return drawFill; }
setFillColor(QColor c1)137     void setFillColor(QColor c1) { fillColor = c1; }
getFillColor()138     QColor getFillColor() { return fillColor; }
setShapeWidth(int i1)139     void setShapeWidth(int i1) { shapewidth = i1; }
setShapeHeight(int i1)140     void setShapeHeight(int i1) { shapeheight = i1; }
141 
getShapeWidth()142     int getShapeWidth() {
143         if (shapewidth < 0)
144             return oshapewidth;
145         return shapewidth;
146     }
147 
getShapeHeight()148     int getShapeHeight() {
149         if (shapeheight < 0)
150             return oshapeheight;
151         return shapeheight;
152     }
153 
154   private:
155     // Renderer
156     Render2D *r;
157     // Text this object holds
158     // QString text;  *** NOW DEFINED IN dpoint.h
159     // Text modifier -- super/subscript flags
160     // QString textmask;  *** NOW DEFINED IN dpoint.h
161     // Text and flags which are actually displayed
162     QString displayText, displayTextMask;
163     // for text which labels a point -- which side is less hindered?
164     int whichside; // 1 = left, 2 = right
165     // Text font(s) - regular, bold, italic, underline
166     QFont font;
167     // justify -- how/where to draw this text
168     int justify, tjustify;
169     // dimensions of text box
170     QRect BBox;
171     // cursor position (character # within text)
172     int cursor;
173     // selection range
174     int selectMin, selectMax;
175     // is the Shift key down?
176     bool shiftdown;
177     // Molecule this text gets data from
178     Molecule *molecule;
179     int DataType;
180     // shape/border/fill variables
181     int shape; // 0 = no shape
182     bool drawBorder, drawFill;
183     QColor borderColor, fillColor;
184     int shapewidth, shapeheight;
185     int oshapewidth, oshapeheight;
186 };
187 
188 #endif
189