1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Lorenz Haas
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include <QPlainTextEdit>
29 #include <QRegularExpression>
30 #include <QString>
31 #include <QStringList>
32 #include <QSyntaxHighlighter>
33 #include <QTextCharFormat>
34 
35 QT_BEGIN_NAMESPACE
36 class QCompleter;
37 class QStringListModel;
38 class QTextDocument;
39 QT_END_NAMESPACE
40 
41 namespace Beautifier {
42 namespace Internal {
43 
44 class AbstractSettings;
45 
46 class ConfigurationSyntaxHighlighter : public QSyntaxHighlighter
47 {
48     Q_OBJECT
49 
50 public:
51     explicit ConfigurationSyntaxHighlighter(QTextDocument *parent);
52     void setKeywords(const QStringList &keywords);
53     void setCommentExpression(const QRegularExpression &rx);
54 
55 protected:
56     void highlightBlock(const QString &text) override;
57 
58 private:
59     QRegularExpression m_expressionKeyword;
60     QRegularExpression m_expressionComment;
61     QTextCharFormat m_formatKeyword;
62     QTextCharFormat m_formatComment;
63 };
64 
65 class ConfigurationEditor : public QPlainTextEdit
66 {
67     Q_OBJECT
68 
69 public:
70     explicit ConfigurationEditor(QWidget *parent = nullptr);
71     void setSettings(AbstractSettings *settings);
72     void setCommentExpression(const QRegularExpression &rx);
73 
74 protected:
75     bool eventFilter(QObject *object, QEvent *event) override;
76     void keyPressEvent(QKeyEvent *event) override;
77 
78 signals:
79     void documentationChanged(const QString &word, const QString &documentation);
80 
81 private:
82     void insertCompleterText(const QString &text);
83     void updateDocumentation();
84     QTextCursor cursorForTextUnderCursor(QTextCursor tc = QTextCursor()) const;
85 
86     AbstractSettings *m_settings = nullptr;
87     QCompleter *m_completer;
88     QStringListModel *m_model;
89     ConfigurationSyntaxHighlighter *m_highlighter;
90     QString m_lastDocumentation;
91 };
92 
93 } // namespace Internal
94 } // namespace Beautifier
95