File indexing completed on 2025-01-26 04:15:01

0001 /*
0002  * Copyright (C) 2021 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017  *
0018  */
0019 
0020 #ifndef TEXTVIEWERITEM_H
0021 #define TEXTVIEWERITEM_H
0022 
0023 #include <QQuickItem>
0024 
0025 /**
0026  * A QQuickItem which fits a set of paragraphs of text with a given style into an arbitrary polygon
0027  * Particularly, this is designed to render data from AdvancedComicBookFormat::Textarea instances.
0028  */
0029 class TextViewerItem : public QQuickItem
0030 {
0031     Q_OBJECT
0032     /**
0033      * A list of paragraphs which should be displayed inside the polygon
0034      * These are expected to be in the format returned by AdvancedComicBookFormat::Textarea::paragraphs()
0035      */
0036     Q_PROPERTY(QStringList paragraphs READ paragraphs WRITE setParagraphs NOTIFY paragraphsChanged)
0037     /**
0038      * A list of points which make up the polygon the text should fit inside.
0039      * This is expected to be in the format returned by AdvancedComicBookFormat::Textarea::points()
0040      */
0041     Q_PROPERTY(QVariantList shape READ shape WRITE setShape NOTIFY shapeChanged)
0042     /**
0043      * The offset of the shape relative to the page (useful when the item is nested inside others,
0044      * such as our own TextAreaHandler component).
0045      */
0046     Q_PROPERTY(QPoint shapeOffset READ shapeOffset WRITE setShapeOffset NOTIFY shapeOffsetChanged)
0047     /**
0048      * The zoom ratio of the view port
0049      */
0050     Q_PROPERTY(double shapeMultiplier READ shapeMultiplier WRITE setShapeMultiplier NOTIFY shapeMultiplierChanged)
0051     /**
0052      * The style which should be used to render the text in this item. This must be
0053      * an instance of AdvancedComicBookFormat::Style.
0054      */
0055     Q_PROPERTY(QObject* style READ style WRITE setStyle NOTIFY styleChanged)
0056     /**
0057      * The specific font family to be used for the text. This is expected to exist on the system
0058      * and works best in conjunction with the ArchiveBookModel::fontFamilyName(QString) function.
0059      */
0060     Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
0061     /**
0062      * This is a list of all the active rects in the item (in essence, anywhere there is an anchor
0063      * in one of the paragraphs, there will be a corresponding rect in this list).
0064      */
0065     Q_PROPERTY(QVariantList linkRects READ linkRects NOTIFY linkRectsChanged)
0066     /**
0067      * The address for the link currently underneath the pointer (empty if none).
0068      */
0069     Q_PROPERTY(QString hoveredLink READ hoveredLink NOTIFY hoveredLinkChanged)
0070 public:
0071     explicit TextViewerItem(QQuickItem *parent = nullptr);
0072     virtual ~TextViewerItem();
0073 
0074     QStringList paragraphs() const;
0075     void setParagraphs(const QStringList& newParagraphs);
0076     Q_SIGNAL void paragraphsChanged();
0077 
0078     QVariantList shape() const;
0079     void setShape(const QVariantList& newShape);
0080     Q_SIGNAL void shapeChanged();
0081 
0082     QPoint shapeOffset() const;
0083     void setShapeOffset(const QPoint& newShapeOffset);
0084     Q_SIGNAL void shapeOffsetChanged();
0085 
0086     double shapeMultiplier() const;
0087     void setShapeMultiplier(double newShapeMultiplier);
0088     Q_SIGNAL void shapeMultiplierChanged();
0089 
0090     QObject* style() const;
0091     void setStyle(QObject* newStyle);
0092     Q_SIGNAL void styleChanged();
0093 
0094     QString fontFamily() const;
0095     void setFontFamily(const QString& newFontFamily);
0096     Q_SIGNAL void fontFamilyChanged();
0097 
0098     QVariantList linkRects() const;
0099     Q_SIGNAL void linkRectsChanged();
0100 
0101     QString hoveredLink() const;
0102     Q_SIGNAL void hoveredLinkChanged();
0103     Q_SIGNAL void linkHovered(const QString& link);
0104     Q_SIGNAL void linkActivated(const QString& link);
0105 protected:
0106     void updatePolish() override;
0107     QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
0108     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0109     void hoverMoveEvent(QHoverEvent* event) override;
0110     void hoverLeaveEvent(QHoverEvent* event) override;
0111     void mousePressEvent(QMouseEvent *event) override;
0112     void mouseReleaseEvent(QMouseEvent * event) override;
0113 
0114 private:
0115     class Private;
0116     Private* d;
0117 };
0118 
0119 #endif