File indexing completed on 2024-05-19 05:21:43

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kpimtextedit_export.h"
0010 
0011 #include <TextCustomEditor/RichTextEditor>
0012 class KActionCollection;
0013 
0014 namespace KPIMTextEdit
0015 {
0016 class RichTextComposerControler;
0017 class RichTextComposerActions;
0018 class RichTextExternalComposer;
0019 class RichTextComposerEmailQuoteHighlighter;
0020 /**
0021  * @brief The RichTextComposer class
0022  * @author Laurent Montel <montel@kde.org>
0023  */
0024 class KPIMTEXTEDIT_EXPORT RichTextComposer : public TextCustomEditor::RichTextEditor
0025 {
0026     Q_OBJECT
0027 public:
0028     explicit RichTextComposer(QWidget *parent = nullptr);
0029     ~RichTextComposer() override;
0030 
0031     enum Mode {
0032         Plain, ///< Plain text mode
0033         Rich ///< Rich text mode
0034     };
0035 
0036     /**
0037      * @return The current text mode
0038      */
0039     [[nodiscard]] Mode textMode() const;
0040 
0041     /**
0042      * Enables word wrap. Words will be wrapped at the specified column.
0043      *
0044      * @param wrapColumn the column where words will be wrapped
0045      */
0046     void enableWordWrap(int wrapColumn);
0047 
0048     /**
0049      * Disables word wrap.
0050      * Note that words are still wrapped at the end of the editor; no scrollbar
0051      * will appear.
0052      */
0053     void disableWordWrap();
0054 
0055     /**
0056      * @return the line number where the cursor is. This takes word-wrapping
0057      *         into account. Line numbers start at 0.
0058      */
0059     [[nodiscard]] int linePosition() const;
0060 
0061     /**
0062      * @return the column number where the cursor is.
0063      */
0064     [[nodiscard]] int columnNumber() const;
0065 
0066     void forcePlainTextMarkup(bool force);
0067 
0068     void activateRichText();
0069     void switchToPlainText();
0070 
0071     void setTextOrHtml(const QString &text);
0072     [[nodiscard]] QString textOrHtml() const;
0073 
0074     virtual void setHighlighterColors(KPIMTextEdit::RichTextComposerEmailQuoteHighlighter *highlighter);
0075 
0076     void setUseExternalEditor(bool use);
0077     void setExternalEditorPath(const QString &path);
0078     [[nodiscard]] bool checkExternalEditorFinished();
0079     void killExternalEditor();
0080 
0081     // Redefine it for each apps
0082     virtual QString smartQuote(const QString &msg); // need by kmail
0083 
0084     void setQuotePrefixName(const QString &quotePrefix);
0085     [[nodiscard]] QString quotePrefixName() const;
0086 
0087     void setCursorPositionFromStart(unsigned int pos);
0088     [[nodiscard]] int quoteLength(const QString &line, bool oneQuote = false) const;
0089     [[nodiscard]] bool isLineQuoted(const QString &line) const;
0090     const QString defaultQuoteSign() const;
0091     void createActions(KActionCollection *ac);
0092 
0093     [[nodiscard]] QList<QAction *> richTextActionList() const;
0094     void setEnableActions(bool state);
0095 
0096     KPIMTextEdit::RichTextComposerControler *composerControler() const;
0097     KPIMTextEdit::RichTextExternalComposer *externalComposer() const;
0098     KPIMTextEdit::RichTextComposerActions *composerActions() const;
0099     void createHighlighter() override;
0100     [[nodiscard]] virtual bool processModifyText(QKeyEvent *event);
0101 
0102 public Q_SLOTS:
0103     void insertPlainTextImplementation();
0104     void slotChangeInsertMode();
0105 
0106 Q_SIGNALS:
0107     void insertModeChanged();
0108     /**
0109      * Emitted whenever the text mode is changed.
0110      *
0111      * @param mode The new text mode
0112      */
0113     void textModeChanged(KPIMTextEdit::RichTextComposer::Mode mode);
0114     /**
0115      * Emitted when the user uses the up arrow in the first line. The application
0116      * should then put the focus on the widget above the text edit.
0117      */
0118     void focusUp();
0119 
0120     void externalEditorStarted();
0121     void externalEditorClosed();
0122     void insertEmoticon(const QString &str);
0123 
0124 protected:
0125     void keyPressEvent(QKeyEvent *event) override;
0126     Sonnet::SpellCheckDecorator *createSpellCheckDecorator() override;
0127     void insertFromMimeData(const QMimeData *source) override;
0128     bool canInsertFromMimeData(const QMimeData *source) const override;
0129     void mouseReleaseEvent(QMouseEvent *event) override;
0130     void clearDecorator() override;
0131     void updateHighLighter() override;
0132     bool processKeyEvent(QKeyEvent *e);
0133 
0134 private:
0135     KPIMTEXTEDIT_NO_EXPORT void slotTextModeChanged(KPIMTextEdit::RichTextComposer::Mode mode);
0136     KPIMTEXTEDIT_NO_EXPORT void evaluateListSupport(QKeyEvent *event);
0137     KPIMTEXTEDIT_NO_EXPORT void evaluateReturnKeySupport(QKeyEvent *event);
0138     class RichTextComposerPrivate;
0139     std::unique_ptr<RichTextComposerPrivate> const d;
0140 };
0141 }