File indexing completed on 2024-04-28 05:38:12

0001 /***************************************************************************
0002  *      Copyright (C) 2010 by Renaud Guezennec                             *
0003  *                                                                         *
0004  *                                                                         *
0005  *   rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #ifndef TEXTITEM_H
0021 #define TEXTITEM_H
0022 #include "rwidgets_global.h"
0023 #include "visualitem.h"
0024 #include <QDialog>
0025 #include <QFontMetrics>
0026 #include <QGraphicsTextItem>
0027 #include <QObject>
0028 #include <QTextDocument>
0029 #include <memory>
0030 class MRichTextEdit;
0031 namespace vmap
0032 {
0033 class TextController;
0034 }
0035 class RWIDGET_EXPORT TextLabel : public QGraphicsTextItem
0036 {
0037     Q_OBJECT
0038     Q_PROPERTY(QRectF textRect READ textRect WRITE setTextRect NOTIFY textRectChanged)
0039 public:
0040     TextLabel(QGraphicsItem* parent= nullptr);
0041 
0042     QRectF textRect() const;
0043     QRectF boundingRect() const override;
0044 
0045 signals:
0046     void textRectChanged();
0047 public slots:
0048     void setTextRect(const QRectF& rect);
0049 
0050 protected:
0051     void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
0052     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override;
0053 
0054 private:
0055     QRectF m_rect;
0056 };
0057 /**
0058  * @brief The RichTextEditDialog class is a simple class based on QDialog to display rich text editor
0059  */
0060 class RichTextEditDialog : public QDialog
0061 {
0062 public:
0063     RichTextEditDialog();
0064 
0065     void setText(QString str);
0066     QString getText();
0067 
0068 private:
0069     MRichTextEdit* m_richText= nullptr;
0070 };
0071 
0072 /**
0073  * @brief displays and manages text on map, part of QGraphicsScene/view.
0074  */
0075 class TextItem : public VisualItem
0076 {
0077     Q_OBJECT
0078 public:
0079     TextItem(vmap::TextController* ctrl);
0080     /**
0081      * @brief paint the item into the scene.
0082      */
0083     void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget= nullptr) override;
0084     /**
0085      * @brief accessor to the bounding rect, helpful for focus and collision detection
0086      */
0087     virtual QRectF boundingRect() const override;
0088     /**
0089      * @brief amends the position of the end point, not really useful for this kind of graphical item.
0090      */
0091     virtual void setNewEnd(const QPointF& nend) override;
0092     void updateChildPosition() override;
0093     void addActionContextMenu(QMenu& menu) override;
0094     void setBorderVisible(bool);
0095 
0096     virtual void updateItemFlags() override;
0097     // virtual void setRectSize(qreal x, qreal y, qreal w, qreal h) override;
0098 
0099     // virtual void endOfGeometryChange() override;
0100     // void endOfGeometryChange(ChildPointItem::Change change) override;
0101 public slots:
0102     // void updateTextPosition();
0103     void editText();
0104     void sizeToTheContent();
0105 
0106 protected:
0107     void wheelEvent(QGraphicsSceneWheelEvent* event) override;
0108     void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
0109     void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
0110     void keyPressEvent(QKeyEvent* event) override;
0111     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override;
0112 
0113 private:
0114     QPointer<vmap::TextController> m_textCtrl;
0115     std::unique_ptr<QTextDocument> m_doc;
0116     std::unique_ptr<TextLabel> m_textItem;
0117 
0118     std::unique_ptr<QAction> m_increaseFontSize;
0119     std::unique_ptr<QAction> m_decreaseFontSize;
0120     std::unique_ptr<QAction> m_edit;
0121     std::unique_ptr<QAction> m_adapt;
0122 
0123     static RichTextEditDialog* m_dialog;
0124 };
0125 
0126 #endif // TEXTITEM_H