File indexing completed on 2024-05-12 17:22:45

0001 // SPDX-FileCopyrightText: 2017 The Qt Company Ltd.
0002 // SPDX-License-Identifier:  BSD-3-Clause
0003 
0004 #ifndef DOCUMENTHANDLER_H
0005 #define DOCUMENTHANDLER_H
0006 
0007 #include <QAbstractListModel>
0008 #include <QDebug>
0009 #include <QFont>
0010 #include <QObject>
0011 #include <QTextCursor>
0012 #include <QThread>
0013 #include <QTimer>
0014 #include <QUrl>
0015 #include <qstringliteral.h>
0016 #include <QQuickTextDocument>
0017 
0018 QT_BEGIN_NAMESPACE
0019 class QFileSystemWatcher;
0020 class QTextDocument;
0021 QT_END_NAMESPACE
0022 
0023 namespace KSyntaxHighlighting
0024 {
0025 class Repository;
0026 class SyntaxHighlighter;
0027 }
0028 
0029 class FileLoader : public QObject
0030 {
0031     Q_OBJECT
0032 
0033 public Q_SLOTS:
0034     /**
0035      * @brief loadFile
0036      * @param url
0037      */
0038     void loadFile(const QUrl &url);
0039 
0040 Q_SIGNALS:
0041     /**
0042      * @brief fileReady
0043      * @param array
0044      * @param url
0045      */
0046     void fileReady(QString array, QUrl url);
0047 };
0048 
0049 /**
0050  * @brief The DocumentHandler class
0051  */
0052 class DocumentHandler : public QObject
0053 {
0054     Q_OBJECT
0055 
0056     Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged)
0057     Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
0058     Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
0059     Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
0060 
0061     Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
0062     Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
0063     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
0064 
0065     Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
0066     Q_PROPERTY(bool uppercase READ uppercase WRITE setUppercase NOTIFY uppercaseChanged)
0067     Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged)
0068     Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged)
0069     Q_PROPERTY(bool isRich READ getIsRich NOTIFY isRichChanged)
0070 
0071     Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged)
0072     Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
0073     Q_PROPERTY(qreal tabSpace READ tabSpace WRITE setTabSpace NOTIFY tabSpaceChanged)
0074 
0075     Q_PROPERTY(QString fileName READ fileName NOTIFY fileUrlChanged)
0076     Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged)
0077 
0078     Q_PROPERTY(QVariantMap fileInfo READ fileInfo NOTIFY fileInfoChanged)
0079     Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged)
0080 
0081     Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
0082 
0083     Q_PROPERTY(bool externallyModified READ getExternallyModified WRITE setExternallyModified NOTIFY externallyModifiedChanged)
0084     Q_PROPERTY(bool modified READ getModified NOTIFY modifiedChanged)
0085 
0086     Q_PROPERTY(bool autoReload READ getAutoReload WRITE setAutoReload NOTIFY autoReloadChanged)
0087     Q_PROPERTY(bool autoSave READ autoSave WRITE setAutoSave NOTIFY autoSaveChanged)
0088 
0089     Q_PROPERTY(QString formatName READ formatName WRITE setFormatName NOTIFY formatNameChanged)
0090 
0091     Q_PROPERTY(int currentLineIndex READ getCurrentLineIndex NOTIFY currentLineIndexChanged)
0092 
0093     Q_PROPERTY(QColor backgroundColor READ getBackgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
0094     Q_PROPERTY(QString theme READ theme WRITE setTheme NOTIFY themeChanged)
0095     Q_PROPERTY(bool enableSyntaxHighlighting READ enableSyntaxHighlighting WRITE setEnableSyntaxHighlighting NOTIFY enableSyntaxHighlightingChanged)
0096 
0097     Q_PROPERTY(bool findWholeWords MEMBER m_findWholeWords NOTIFY findWholeWordsChanged)
0098     Q_PROPERTY(bool findCaseSensitively MEMBER m_findCaseSensitively NOTIFY findCaseSensitivelyChanged)
0099 
0100 public:
0101     explicit DocumentHandler(QObject *parent = nullptr);
0102     ~DocumentHandler();
0103 
0104     /**
0105      * @brief document
0106      * @return
0107      */
0108     QQuickTextDocument *document() const;
0109 
0110     /**
0111      * @brief setDocument
0112      * @param document
0113      */
0114     void setDocument(QQuickTextDocument *document);
0115 
0116     /**
0117      * @brief cursorPosition
0118      * @return
0119      */
0120     int cursorPosition() const;
0121 
0122     /**
0123      * @brief setCursorPosition
0124      * @param position
0125      */
0126     void setCursorPosition(int position);
0127 
0128     /**
0129      * @brief selectionStart
0130      * @return
0131      */
0132     int selectionStart() const;
0133 
0134     /**
0135      * @brief setSelectionStart
0136      * @param position
0137      */
0138     void setSelectionStart(int position);
0139 
0140     /**
0141      * @brief selectionEnd
0142      * @return
0143      */
0144     int selectionEnd() const;
0145 
0146     /**
0147      * @brief setSelectionEnd
0148      * @param position
0149      */
0150     void setSelectionEnd(int position);
0151 
0152     /**
0153      * @brief fontFamily
0154      * @return
0155      */
0156     QString fontFamily() const;
0157 
0158     /**
0159      * @brief setFontFamily
0160      * @param family
0161      */
0162     void setFontFamily(const QString &family);
0163 
0164     /**
0165      * @brief textColor
0166      * @return
0167      */
0168     QColor textColor() const;
0169 
0170     /**
0171      * @brief setTextColor
0172      * @param color
0173      */
0174     void setTextColor(const QColor &color);
0175 
0176     /**
0177      * @brief alignment
0178      * @return
0179      */
0180     Qt::Alignment alignment() const;
0181 
0182     /**
0183      * @brief setAlignment
0184      * @param alignment
0185      */
0186     void setAlignment(Qt::Alignment alignment);
0187 
0188     /**
0189      * @brief bold
0190      * @return
0191      */
0192     bool bold() const;
0193 
0194     /**
0195      * @brief setBold
0196      * @param bold
0197      */
0198     void setBold(bool bold);
0199 
0200     /**
0201      * @brief uppercase
0202      * @return
0203      */
0204     bool uppercase() const;
0205 
0206     /**
0207      * @brief setUppercase
0208      * @param uppercase
0209      */
0210     void setUppercase(bool uppercase);
0211 
0212     /**
0213      * @brief italic
0214      * @return
0215      */
0216     bool italic() const;
0217 
0218     /**
0219      * @brief setItalic
0220      * @param italic
0221      */
0222     void setItalic(bool italic);
0223 
0224     /**
0225      * @brief underline
0226      * @return
0227      */
0228     bool underline() const;
0229 
0230     /**
0231      * @brief setUnderline
0232      * @param underline
0233      */
0234     void setUnderline(bool underline);
0235 
0236     /**
0237      * @brief getIsRich
0238      * @return
0239      */
0240     bool getIsRich() const;
0241 
0242     /**
0243      * @brief fontSize
0244      * @return
0245      */
0246     int fontSize() const;
0247 
0248     /**
0249      * @brief setFontSize
0250      * @param size
0251      */
0252     void setFontSize(int size);
0253 
0254     qreal tabSpace() const;
0255     void setTabSpace(qreal value);
0256 
0257     /**
0258      * @brief fileName
0259      * @return
0260      */
0261     QString fileName() const;
0262 
0263     /**
0264      * @brief fileType
0265      * @return
0266      */
0267     QString fileType() const;
0268 
0269     /**
0270      * @brief fileUrl
0271      * @return
0272      */
0273     QUrl fileUrl() const;
0274 
0275     /**
0276      * @brief setFileUrl
0277      * @param url
0278      */
0279     void setFileUrl(const QUrl &url);
0280 
0281     /**
0282      * @brief fileInfo
0283      * @return
0284      */
0285     QVariantMap fileInfo() const;
0286 
0287     /**
0288      * @brief text
0289      * @return
0290      */
0291     inline QString text() const
0292     {
0293         return m_text;
0294     }
0295 
0296     /**
0297      * @brief setText
0298      * @param text
0299      */
0300     void setText(const QString &text);
0301 
0302     /**
0303      * @brief getAutoReload
0304      * @return
0305      */
0306     bool getAutoReload() const;
0307 
0308     /**
0309      * @brief setAutoReload
0310      * @param value
0311      */
0312     void setAutoReload(const bool &value);
0313 
0314     /**
0315      * @brief autoSave
0316      * @return
0317      */
0318     bool autoSave() const;
0319 
0320     /**
0321      * @brief setAutoSave
0322      * @param value
0323      */
0324     void setAutoSave(const bool &value);
0325 
0326     /**
0327      * @brief getModified
0328      * @return
0329      */
0330     bool getModified() const;
0331 
0332     /**
0333      * @brief getExternallyModified
0334      * @return
0335      */
0336     bool getExternallyModified() const;
0337 
0338     /**
0339      * @brief setExternallyModified
0340      * @param value
0341      */
0342     void setExternallyModified(const bool &value);
0343 
0344     /**
0345      * @brief formatName
0346      * @return
0347      */
0348     QString formatName() const;
0349 
0350     /**
0351      * @brief setFormatName
0352      * @param formatName
0353      */
0354     void setFormatName(const QString &formatName);
0355 
0356     /**
0357      * @brief getBackgroundColor
0358      * @return
0359      */
0360     QColor getBackgroundColor() const;
0361 
0362     /**
0363      * @brief setBackgroundColor
0364      * @param color
0365      */
0366     void setBackgroundColor(const QColor &color);
0367 
0368     /**
0369      * @brief theme
0370      * @return
0371      */
0372     QString theme() const;
0373 
0374     /**
0375      * @brief setTheme
0376      * @param theme
0377      */
0378     void setTheme(const QString &theme);
0379 
0380     /**
0381      * @brief enableSyntaxHighlighting
0382      * @return
0383      */
0384     bool enableSyntaxHighlighting() const;
0385 
0386     /**
0387      * @brief setEnableSyntaxHighlighting
0388      * @param value
0389      */
0390     void setEnableSyntaxHighlighting(const bool &value);
0391 
0392     /**
0393      * @brief isDark
0394      * @param color
0395      * @return
0396      */
0397     static bool isDark(const QColor &color)
0398     {
0399         const double darkness = 1 - (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255;
0400         return (darkness > 0.5);
0401     }
0402 
0403 public Q_SLOTS:
0404     /**
0405      * @brief load
0406      * @param url
0407      */
0408     void load(const QUrl &url);
0409 
0410     /**
0411      * @brief saveAs
0412      * @param url
0413      */
0414     void saveAs(const QUrl &url);
0415 
0416     /**
0417      * @brief find
0418      * @param query
0419      */
0420     void find(const QString &query, const bool &forward = true);
0421 
0422     void replace(const QString &query, const QString &value);
0423 
0424     void replaceAll(const QString &query, const QString &value);
0425 
0426     bool isFoldable(const int &line) const;
0427     bool isFolded(const int &line) const;
0428     void toggleFold(const int &line);
0429 
0430     /**
0431      * @brief lineHeight
0432      * @param line
0433      * @return
0434      */
0435     int lineHeight(const int &line);
0436 
0437     int lineCount();
0438 
0439     /**
0440      * @brief getCurrentLineIndex
0441      * @return
0442      */
0443     int getCurrentLineIndex();
0444 
0445     int goToLine(const int &line);
0446 
0447     /**
0448      * @brief getLanguageNameList
0449      * @return
0450      */
0451     static const QStringList getLanguageNameList();
0452 
0453     /**
0454      * @brief getLanguageNameFromFileName
0455      * @param fileName
0456      * @return
0457      */
0458     static const QString getLanguageNameFromFileName(const QUrl &fileName);
0459 
0460     /**
0461      * @brief getThemes
0462      * @return
0463      */
0464     static const QStringList getThemes();
0465 
0466 Q_SIGNALS:
0467     void documentChanged();
0468     void fileSaved();
0469 
0470     void cursorPositionChanged();
0471     void selectionStartChanged();
0472     void selectionEndChanged();
0473 
0474     void fontFamilyChanged();
0475     void textColorChanged();
0476     void alignmentChanged();
0477 
0478     void boldChanged();
0479     void uppercaseChanged();
0480     void italicChanged();
0481     void underlineChanged();
0482     void isRichChanged();
0483 
0484     void lineCountChanged();
0485     void fontSizeChanged();
0486     void tabSpaceChanged();
0487 
0488     void textChanged();
0489     void fileUrlChanged();
0490     void fileInfoChanged();
0491 
0492     void loaded(const QUrl &url);
0493     void error(const QString &message);
0494     void loadFile(QUrl url);
0495 
0496     void autoReloadChanged();
0497     void autoSaveChanged();
0498 
0499     void externallyModifiedChanged();
0500 
0501     void backgroundColorChanged();
0502 
0503     void formatNameChanged();
0504 
0505     void modifiedChanged();
0506 
0507     void currentLineIndexChanged();
0508 
0509     void enableSyntaxHighlightingChanged();
0510     void themeChanged();
0511 
0512     void searchFound(int start, int end);
0513     void findCaseSensitivelyChanged();
0514     void findWholeWordsChanged();
0515 
0516 private:
0517     void reset();
0518     void setStyle();
0519 
0520     QTextCursor textCursor() const;
0521     QTextDocument *textDocument() const;
0522     void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
0523 
0524     QQuickTextDocument *m_document;
0525     QFileSystemWatcher *m_watcher;
0526 
0527     int m_cursorPosition;
0528     int m_selectionStart;
0529     int m_selectionEnd;
0530 
0531     bool isRich = false;
0532 
0533     QFont m_font;
0534     int m_fontSize = 12;
0535     qreal m_tabSpace = 8;
0536 
0537     QUrl m_fileUrl;
0538 
0539     QThread m_worker;
0540     QString m_text;
0541 
0542     bool m_autoReload = false;
0543     bool m_autoSave = false;
0544 
0545     bool m_externallyModified = false;
0546     bool m_internallyModified = false;
0547 
0548     bool m_findCaseSensitively = false;
0549     bool m_findWholeWords = false;
0550 
0551     QColor m_backgroundColor;
0552 
0553     static int m_instanceCount;
0554     QString m_formatName = QStringLiteral("None");
0555     static KSyntaxHighlighting::Repository *m_repository;
0556     KSyntaxHighlighting::SyntaxHighlighter *m_highlighter;
0557 
0558     bool m_enableSyntaxHighlighting = false;
0559     QString m_theme;
0560 
0561     QString m_searchQuery;
0562     QString m_replaceText;
0563 
0564     QTimer m_autoSaveTimer;
0565 
0566     void refreshAllBlocks();
0567 };
0568 
0569 #endif // DOCUMENTHANDLER_H