File indexing completed on 2024-05-19 15:02:12

0001 /***************************************************************************
0002     File                 : TextLabel.h
0003     Project              : LabPlot
0004     Description          : Text label supporting reach text and latex formatting
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2009 Tilman Benkert (thzs@gmx.net)
0007     Copyright            : (C) 2012-2014 Alexander Semke (alexander.semke@web.de)
0008 
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *  This program is free software; you can redistribute it and/or modify   *
0014  *  it under the terms of the GNU General Public License as published by   *
0015  *  the Free Software Foundation; either version 2 of the License, or      *
0016  *  (at your option) any later version.                                    *
0017  *                                                                         *
0018  *  This program is distributed in the hope that it will be useful,        *
0019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0021  *  GNU General Public License for more details.                           *
0022  *                                                                         *
0023  *   You should have received a copy of the GNU General Public License     *
0024  *   along with this program; if not, write to the Free Software           *
0025  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0026  *   Boston, MA  02110-1301  USA                                           *
0027  *                                                                         *
0028  ***************************************************************************/
0029 
0030 #ifndef TEXTLABEL_H
0031 #define TEXTLABEL_H
0032 
0033 #include "backend/lib/macros.h"
0034 #include "tools/TeXRenderer.h"
0035 #include "backend/worksheet/WorksheetElement.h"
0036 
0037 #include <QPen>
0038 #include <QTextEdit>
0039 
0040 class QBrush;
0041 class QFont;
0042 class TextLabelPrivate;
0043 
0044 class TextLabel : public WorksheetElement {
0045     Q_OBJECT
0046 
0047 public:
0048     enum class Type {General, PlotTitle, AxisTitle, PlotLegendTitle};
0049     enum class BorderShape {NoBorder, Rect, Ellipse, RoundSideRect, RoundCornerRect, InwardsRoundCornerRect, DentedBorderRect,
0050             Cuboid, UpPointingRectangle, DownPointingRectangle, LeftPointingRectangle, RightPointingRectangle};
0051 
0052     // The text is always in HMTL format
0053     struct TextWrapper {
0054         TextWrapper() {}
0055         TextWrapper(const QString& t, bool b, bool html): teXUsed(b) {
0056             if (b) {
0057                 text = t; // LaTeX does not support HTML, so assume t is a plain text string
0058                 return;
0059             }
0060             text = createHtml(t, html);
0061         }
0062         TextWrapper(const QString& t, bool html = false) {
0063             text = createHtml(t, html);
0064         }
0065         QString createHtml(QString text, bool isHtml) {
0066             if (isHtml)
0067                 return text;
0068 
0069             QTextEdit te(text);
0070             return te.toHtml();
0071         }
0072 
0073         QString text;
0074         bool teXUsed{false};
0075     };
0076 
0077     explicit TextLabel(const QString& name, Type type = Type::General);
0078     ~TextLabel() override;
0079 
0080     Type type() const;
0081     QIcon icon() const override;
0082     QMenu* createContextMenu() override;
0083     QGraphicsItem* graphicsItem() const override;
0084     void setParentGraphicsItem(QGraphicsItem*);
0085 
0086     void save(QXmlStreamWriter*) const override;
0087     bool load(XmlStreamReader*, bool preview) override;
0088     void loadThemeConfig(const KConfig&) override;
0089     void saveThemeConfig(const KConfig&) override;
0090 
0091     CLASS_D_ACCESSOR_DECL(TextWrapper, text, Text)
0092     BASIC_D_ACCESSOR_DECL(QColor, fontColor, FontColor)
0093     BASIC_D_ACCESSOR_DECL(QColor, backgroundColor, BackgroundColor)
0094     CLASS_D_ACCESSOR_DECL(QFont, teXFont, TeXFont)
0095     CLASS_D_ACCESSOR_DECL(WorksheetElement::PositionWrapper, position, Position)
0096     void setPosition(QPointF);
0097     void setPositionInvalid(bool);
0098     BASIC_D_ACCESSOR_DECL(WorksheetElement::HorizontalAlignment, horizontalAlignment, HorizontalAlignment)
0099     BASIC_D_ACCESSOR_DECL(WorksheetElement::VerticalAlignment, verticalAlignment, VerticalAlignment)
0100     BASIC_D_ACCESSOR_DECL(qreal, rotationAngle, RotationAngle)
0101 
0102     BASIC_D_ACCESSOR_DECL(BorderShape, borderShape, BorderShape);
0103     CLASS_D_ACCESSOR_DECL(QPen, borderPen, BorderPen)
0104     BASIC_D_ACCESSOR_DECL(qreal, borderOpacity, BorderOpacity)
0105 
0106     void setVisible(bool on) override;
0107     bool isVisible() const override;
0108     void setPrinting(bool) override;
0109 
0110     void retransform() override;
0111     void handleResize(double horizontalRatio, double verticalRatio, bool pageResize) override;
0112 
0113     typedef TextLabelPrivate Private;
0114 
0115 private slots:
0116     void updateTeXImage();
0117 
0118     //SLOTs for changes triggered via QActions in the context menu
0119     void visibilityChanged();
0120 
0121 protected:
0122     TextLabelPrivate* const d_ptr;
0123     TextLabel(const QString& name, TextLabelPrivate* dd, Type type = Type::General);
0124 
0125 private:
0126     Q_DECLARE_PRIVATE(TextLabel)
0127     void init();
0128 
0129     Type m_type;
0130     QAction* visibilityAction{nullptr};
0131 
0132 signals:
0133     void textWrapperChanged(const TextLabel::TextWrapper&);
0134     void teXFontSizeChanged(const int);
0135     void teXFontChanged(const QFont);
0136     void fontColorChanged(const QColor);
0137     void backgroundColorChanged(const QColor);
0138     void positionChanged(const WorksheetElement::PositionWrapper&);
0139     void horizontalAlignmentChanged(WorksheetElement::HorizontalAlignment);
0140     void verticalAlignmentChanged(WorksheetElement::VerticalAlignment);
0141     void rotationAngleChanged(qreal);
0142     void visibleChanged(bool);
0143     void borderShapeChanged(TextLabel::BorderShape);
0144     void borderPenChanged(QPen&);
0145     void borderOpacityChanged(float);
0146 
0147     void teXImageUpdated(bool);
0148     void changed();
0149 };
0150 
0151 #endif