File indexing completed on 2022-12-06 18:50:32

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004 */
0005 
0006 
0007 #ifndef WORKSHEET_TEXT_ITEM_H
0008 #define WORKSHEET_TEXT_ITEM_H
0009 
0010 #include <QGraphicsTextItem>
0011 #include <QTextDocument>
0012 #include <QTextCursor>
0013 
0014 #include <QMenu>
0015 #include <KStandardAction>
0016 
0017 class Worksheet;
0018 class WorksheetEntry;
0019 class WorksheetView;
0020 class WorksheetCursor;
0021 
0022 namespace Cantor {
0023     class Session;
0024 }
0025 
0026 class QTextCharFormat;
0027 
0028 class WorksheetTextItem : public QGraphicsTextItem
0029 {
0030   Q_OBJECT
0031   public:
0032     enum DoubleClickEventBehaviour {Simple, ImageReplacement};
0033 
0034     explicit WorksheetTextItem(WorksheetEntry* parent,
0035                       Qt::TextInteractionFlags ti = Qt::NoTextInteraction);
0036     ~WorksheetTextItem() override;
0037 
0038     void setCursorPosition(QPointF);
0039     QPointF cursorPosition() const;
0040     QTextCursor cursorForPosition(QPointF) const;
0041     QRectF sceneCursorRect(QTextCursor cursor = QTextCursor()) const;
0042     QRectF cursorRect(QTextCursor cursor = QTextCursor()) const;
0043 
0044     enum {TopLeft, BottomRight, TopCoord, BottomCoord};
0045     enum {Type = UserType + 100};
0046 
0047     int type() const override;
0048 
0049     void setFocusAt(int pos = TopLeft, qreal xCoord = 0);
0050 
0051     void enableCompletion(bool b);
0052     void activateCompletion(bool b);
0053     void setItemDragable(bool b);
0054     void enableRichText(bool b);
0055 
0056     virtual void populateMenu(QMenu*, QPointF);
0057     QString resolveImages(const QTextCursor&);
0058 
0059     bool isEditable();
0060     void allowEditing();
0061     void denyEditing();
0062     void setBackgroundColor(const QColor&);
0063     const QColor& backgroundColor() const;
0064     bool richTextEnabled();
0065     double width() const;
0066     double height() const;
0067     virtual qreal setGeometry(qreal x, qreal y, qreal w, bool centered=false);
0068 
0069     Worksheet* worksheet();
0070     WorksheetView* worksheetView();
0071 
0072     void clearSelection();
0073 
0074     bool isUndoAvailable();
0075     bool isRedoAvailable();
0076     bool isCutAvailable();
0077     bool isCopyAvailable();
0078     bool isPasteAvailable();
0079 
0080     // richtext
0081     void setTextForegroundColor();
0082     void setTextBackgroundColor();
0083     void setTextBold(bool);
0084     void setTextItalic(bool);
0085     void setTextUnderline(bool);
0086     void setTextStrikeOut(bool);
0087     void setAlignment(Qt::Alignment);
0088     void setFontFamily(const QString&);
0089     void setFontSize(int);
0090 
0091     QTextCursor search(QString pattern,
0092                        QTextDocument::FindFlags qt_flags,
0093                        const WorksheetCursor& pos);
0094 
0095     DoubleClickEventBehaviour doubleClickBehaviour();
0096     void setDoubleClickBehaviour(DoubleClickEventBehaviour);
0097 
0098   Q_SIGNALS:
0099     void moveToPrevious(int pos, qreal xCoord);
0100     void moveToNext(int pos, qreal xCoord);
0101     void cursorPositionChanged(QTextCursor);
0102     void receivedFocus(WorksheetTextItem*);
0103     void tabPressed();
0104     void backtabPressed();
0105     void applyCompletion();
0106     void doubleClick();
0107     void execute();
0108     void deleteEntry();
0109     void sizeChanged();
0110     void menuCreated(QMenu*, QPointF);
0111     void drag(const QPointF&, QPointF);
0112     void undoAvailable(bool);
0113     void redoAvailable(bool);
0114     void cutAvailable(bool);
0115     void copyAvailable(bool);
0116     void pasteAvailable(bool);
0117 
0118   public Q_SLOTS:
0119     void insertTab();
0120     void cut();
0121     void copy();
0122     void paste();
0123     void undo();
0124     void redo();
0125     void clipboardChanged();
0126     void selectionChanged();
0127     void testSize();
0128 
0129   protected:
0130     void keyPressEvent(QKeyEvent*) override;
0131     void focusInEvent(QFocusEvent*) override;
0132     void focusOutEvent(QFocusEvent*) override;
0133     void mousePressEvent(QGraphicsSceneMouseEvent*) override;
0134     void mouseReleaseEvent(QGraphicsSceneMouseEvent*) override;
0135     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent*) override;
0136     void mouseMoveEvent(QGraphicsSceneMouseEvent*) override;
0137     void contextMenuEvent(QGraphicsSceneContextMenuEvent*) override;
0138     void dragEnterEvent(QGraphicsSceneDragDropEvent*) override;
0139     //void dragLeaveEvent(QGraphicsSceneDragDropEvent*);
0140     void dragMoveEvent(QGraphicsSceneDragDropEvent*) override;
0141     void dropEvent(QGraphicsSceneDragDropEvent*) override;
0142     bool sceneEvent(QEvent*) override;
0143     void wheelEvent(QGraphicsSceneWheelEvent*) override;
0144     void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override;
0145 
0146   private Q_SLOTS:
0147     //void setHeight();
0148     void updateRichTextActions(QTextCursor cursor);
0149 
0150   private:
0151     void setLocalCursorPosition(QPointF);
0152     QPointF localCursorPosition() const;
0153 
0154     QKeyEvent* eventForStandardAction(KStandardAction::StandardAction);
0155     Cantor::Session* session();
0156 
0157     // richtext
0158     void mergeFormatOnWordOrSelection(const QTextCharFormat&);
0159 
0160   private:
0161     QSizeF m_size;
0162     bool m_completionEnabled{false};
0163     bool m_completionActive{false};
0164     bool m_itemDragable{false};
0165     bool m_richTextEnabled{false};
0166     QColor m_backgroundColor;
0167     DoubleClickEventBehaviour m_eventBehaviour{DoubleClickEventBehaviour::ImageReplacement};
0168 };
0169 
0170 #endif // WORKSHEET_TEXT_ITEM_H