1 /***************************************************************************
2  *   This file is part of the Lime Report project                          *
3  *   Copyright (C) 2015 by Alexander Arin                                  *
4  *   arin_a@bk.ru                                                          *
5  *                                                                         *
6  **                   GNU General Public License Usage                    **
7  *                                                                         *
8  *   This library is free software: you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation, either version 3 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *   You should have received a copy of the GNU General Public License     *
13  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
14  *                                                                         *
15  **                  GNU Lesser General Public License                    **
16  *                                                                         *
17  *   This library is free software: you can redistribute it and/or modify  *
18  *   it under the terms of the GNU Lesser General Public License as        *
19  *   published by the Free Software Foundation, either version 3 of the    *
20  *   License, or (at your option) any later version.                       *
21  *   You should have received a copy of the GNU Lesser General Public      *
22  *   License along with this library.                                      *
23  *   If not, see <http://www.gnu.org/licenses/>.                           *
24  *                                                                         *
25  *   This library is distributed in the hope that it will be useful,       *
26  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
27  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
28  *   GNU General Public License for more details.                          *
29  ****************************************************************************/
30 #ifndef LRTEXTITEM_H
31 #define LRTEXTITEM_H
32 #include <QGraphicsTextItem>
33 #include <QtGui>
34 #include <QLabel>
35 #include <QTextDocument>
36 
37 #include "lritemdesignintf.h"
38 #include "lritemdesignintf.h"
39 #include "lrpageinitintf.h"
40 
41 namespace LimeReport {
42 
43 class Tag;
44 class TextItem : public ContentItemDesignIntf, IPageInit {
45     Q_OBJECT
46     Q_ENUMS(AutoWidth)
47     Q_ENUMS(AngleType)
48     Q_ENUMS(ValueType)
49     Q_PROPERTY(QString content READ content WRITE setContent)
50     Q_PROPERTY(int margin READ marginSize WRITE setMarginSize)
51     Q_PROPERTY(Qt::Alignment alignment READ alignment() WRITE setAlignment)
52     Q_PROPERTY(AutoWidth autoWidth READ autoWidth() WRITE setAutoWidth)
53     Q_PROPERTY(bool autoHeight READ autoHeight() WRITE setAutoHeight)
54     Q_PROPERTY(QFont font READ font() WRITE setTextItemFont)
55     Q_PROPERTY(int backgroundOpacity READ opacity WRITE setBackgroundOpacity)
56     Q_PROPERTY(BGMode backgroundMode READ backgroundMode WRITE setBackgroundModeProperty)
57     Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColorProperty)
58     Q_PROPERTY(QColor fontColor READ fontColor WRITE setFontColorProperty)
59     Q_PROPERTY(AngleType angle READ angle WRITE setAngle)
60     Q_PROPERTY(int foregroundOpacity READ foregroundOpacity WRITE setForegroundOpacity)
61     Q_PROPERTY(bool underlines READ underlines WRITE setUnderlines)
62     Q_PROPERTY(bool adaptFontToSize READ adaptFontToSize WRITE setAdaptFontToSize)
63     Q_PROPERTY(bool trimValue READ trimValue WRITE setTrimValue)
64     Q_PROPERTY(int lineSpacing READ lineSpacing WRITE setLineSpacing)
65     Q_PROPERTY(int underlineLineSize READ underlineLineSize WRITE setUnderlineLineSize)
66     Q_PROPERTY(bool allowHTML READ allowHTML WRITE setAllowHTML)
67     Q_PROPERTY(bool allowHTMLInFields READ allowHTMLInFields WRITE setAllowHTMLInFields)
68     Q_PROPERTY(QString format READ format WRITE setFormat)
69     Q_PROPERTY(ValueType valueType READ valueType WRITE setValueType)
70     Q_PROPERTY(QString followTo READ followTo WRITE setFollowTo)
71     Q_PROPERTY(BrushStyle backgroundBrushStyle READ backgroundBrushStyle WRITE setBackgroundBrushStyle)
72     Q_PROPERTY(qreal textIndent READ textIndent WRITE setTextIndent)
73     Q_PROPERTY(Qt::LayoutDirection textLayoutDirection READ textLayoutDirection WRITE setTextLayoutDirection)
74     Q_PROPERTY(bool fillInSecondPass READ fillInSecondPass WRITE setFillInSecondPass)
75     Q_PROPERTY(bool watermark READ isWatermark WRITE setWatermark)
76     Q_PROPERTY(bool replaceCRwithBR READ isReplaceCarriageReturns WRITE setReplaceCarriageReturns)
77     Q_PROPERTY(bool hideIfEmpty READ hideIfEmpty WRITE setHideIfEmpty)
78     Q_PROPERTY(int fontLetterSpacing READ fontLetterSpacing WRITE setFontLetterSpacing)
79 public:
80 
81     enum AutoWidth{NoneAutoWidth, MaxWordLength, MaxStringLength};
82     enum AngleType{Angle0, Angle90, Angle180, Angle270, Angle45, Angle315};
83     enum ValueType{Default, DateTime, Double};
84 
85     void Init();
86     TextItem(QObject* owner=0, QGraphicsItem* parent=0);
87     ~TextItem();
88 
89     void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*);
90     QString content() const;
91     void setContent(const QString& value);
92 
93     void setAlignment(Qt::Alignment value);
alignment()94     Qt::Alignment alignment(){return m_alignment;}
95 
96     void geometryChangedEvent(QRectF, QRectF);
97     bool isNeedUpdateSize(RenderPass) const;
98     void updateItemSize(DataSourceManager *dataManager, RenderPass pass, int maxHeight);
99     void expandContent(DataSourceManager *dataManager, RenderPass pass);
100 
101     void setAutoHeight(bool value);
autoHeight()102     bool autoHeight() const {return m_autoHeight;}
103 
104     void setAutoWidth(AutoWidth value);
autoWidth()105     AutoWidth autoWidth() const {return m_autoWidth;}
106 
107     void setAdaptFontToSize(bool value);
adaptFontToSize()108     bool adaptFontToSize() const {return m_adaptFontToSize;}
109 
110     bool canBeSplitted(int height) const;
isSplittable()111     bool isSplittable() const { return true;}
isEmpty()112     bool isEmpty() const{return m_strText.trimmed().isEmpty();}
113     BaseDesignIntf* cloneUpperPart(int height, QObject *owner, QGraphicsItem *parent);
114     BaseDesignIntf* cloneBottomPart(int height, QObject *owner, QGraphicsItem *parent);
115     BaseDesignIntf* createSameTypeItem(QObject* owner=0, QGraphicsItem* parent=0);
116     BaseDesignIntf* cloneEmpty(int height, QObject *owner, QGraphicsItem *parent);
117     void objectLoadFinished();
118 
119     void setTextItemFont(QFont value);
120     QWidget* defaultEditor();
121     void setBackgroundOpacity(int value);
122     void setBackgroundModeProperty(BGMode value);
123     void setBackgroundColorProperty(QColor value);
124     void setFontColorProperty(QColor value);
125     AngleType angle() const;
126     void setAngle(const AngleType& value);
foregroundOpacity()127     int foregroundOpacity(){return m_foregroundOpacity;}
128     void setForegroundOpacity(int value);
underlines()129     bool underlines(){return m_underlines;}
130     void setUnderlines(bool value);
131 
132     bool trimValue() const;
133     void setTrimValue(bool trimValue);
134 
135     int lineSpacing() const;
136     void setLineSpacing(int value);
137 
138     int underlineLineSize() const;
139     void setUnderlineLineSize(int value);
140 
141     bool allowHTML() const;
142     void setAllowHTML(bool allowHTML);
143 
144     bool allowHTMLInFields() const;
145     void setAllowHTMLInFields(bool allowHTMLInFields);
146 
147     QString format() const;
148     void setFormat(const QString &format);
149 
150     ValueType valueType() const;
151     void setValueType(const ValueType valueType);
152 
textSize()153     QSizeF textSize(){ return m_textSize;}
154     QString followTo() const;
155     void setFollowTo(const QString &followTo);
156     void setFollower(TextItem* follower);
157     void clearFollower();
158     bool hasFollower() const;
follower()159     TextItem* follower() const { return m_follower;}
160     bool initFollower(QString follower);
161 
162     // IPageInit interface
163     void pageObjectHasBeenLoaded();
164 
165     typedef QSharedPointer<QTextDocument> TextPtr;
166 
167     qreal textIndent() const;
168     void setTextIndent(const qreal &textIndent);
169     Qt::LayoutDirection textLayoutDirection() const;
170     void setTextLayoutDirection(const Qt::LayoutDirection &textLayoutDirection);
171 
172     void setWatermark(bool watermark);
173 
174     bool isReplaceCarriageReturns() const;
175     void setReplaceCarriageReturns(bool isReplaceCarriageReturns);
176 
177     bool hideIfEmpty() const;
178     void setHideIfEmpty(bool hideIfEmpty);
179 
180     int fontLetterSpacing() const;
181     void setFontLetterSpacing(int fontLetterSpacing);
182 
183 protected:
184     void updateLayout();
185     bool isNeedExpandContent() const;
186     QString replaceBR(QString text) const;
187     QString replaceReturns(QString text) const;
188     int fakeMarginSize() const;
189     QString getTextPart(int height, int skipHeight);
190     void restoreLinksEvent();
191     void preparePopUpMenu(QMenu &menu);
192     void processPopUpAction(QAction *action);
193 private:
194     void initTextSizes() const;
195     void setTextFont(TextPtr text, const QFont &value) const;
196     void adaptFontSize(TextPtr text) const;
197     QString formatDateTime(const QDateTime &value);
198     QString formatNumber(const double value);
199     QString formatFieldValue();
200     QString extractText(QTextBlock& curBlock, int height);
201     TextPtr textDocument() const;
202 private:
203     QString m_strText;
204     Qt::Alignment m_alignment;
205     bool m_autoHeight;
206     AutoWidth m_autoWidth;
207     QSizeF mutable m_textSize;
208     qreal  mutable m_firstLineSize;
209     AngleType m_angle;
210     int m_foregroundOpacity;
211     bool m_underlines;
212     bool m_adaptFontToSize;
213     bool m_trimValue;
214     int m_lineSpacing;
215     int m_underlineLineSize;
216     bool m_allowHTML;
217     bool m_allowHTMLInFields;
218     bool m_replaceCarriageReturns;
219 
220     QString m_format;
221     ValueType m_valueType;
222     QString   m_followTo;
223     TextItem* m_follower;
224     qreal m_textIndent;
225     Qt::LayoutDirection m_textLayoutDirection;
226     bool m_hideIfEmpty;
227     int m_fontLetterSpacing;
228 };
229 
230 }
231 #endif // LRTEXTITEM_H
232