configurationeditor.h 2.85 KB
Newer Older
1
/****************************************************************************
Lorenz Haas's avatar
Lorenz Haas committed
2
**
3 4
** Copyright (C) 2016 Lorenz Haas
** Contact: https://www.qt.io/licensing/
Lorenz Haas's avatar
Lorenz Haas committed
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Lorenz Haas's avatar
Lorenz Haas committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Lorenz Haas's avatar
Lorenz Haas committed
23 24 25
**
****************************************************************************/

hjk's avatar
hjk committed
26
#pragma once
Lorenz Haas's avatar
Lorenz Haas committed
27 28

#include <QPlainTextEdit>
Lorenz Haas's avatar
Lorenz Haas committed
29
#include <QRegularExpression>
Lorenz Haas's avatar
Lorenz Haas committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <QString>
#include <QStringList>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>

QT_BEGIN_NAMESPACE
class QCompleter;
class QStringListModel;
class QTextDocument;
QT_END_NAMESPACE

namespace Beautifier {
namespace Internal {

class AbstractSettings;

class ConfigurationSyntaxHighlighter : public QSyntaxHighlighter
{
    Q_OBJECT

public:
    explicit ConfigurationSyntaxHighlighter(QTextDocument *parent);
    void setKeywords(const QStringList &keywords);
Lorenz Haas's avatar
Lorenz Haas committed
53
    void setCommentExpression(const QRegularExpression &rx);
Lorenz Haas's avatar
Lorenz Haas committed
54 55

protected:
56
    void highlightBlock(const QString &text) override;
Lorenz Haas's avatar
Lorenz Haas committed
57 58

private:
Lorenz Haas's avatar
Lorenz Haas committed
59 60
    QRegularExpression m_expressionKeyword;
    QRegularExpression m_expressionComment;
Lorenz Haas's avatar
Lorenz Haas committed
61 62 63 64 65 66 67 68 69
    QTextCharFormat m_formatKeyword;
    QTextCharFormat m_formatComment;
};

class ConfigurationEditor : public QPlainTextEdit
{
    Q_OBJECT

public:
70
    explicit ConfigurationEditor(QWidget *parent = nullptr);
Lorenz Haas's avatar
Lorenz Haas committed
71
    void setSettings(AbstractSettings *settings);
Lorenz Haas's avatar
Lorenz Haas committed
72
    void setCommentExpression(const QRegularExpression &rx);
Lorenz Haas's avatar
Lorenz Haas committed
73 74

protected:
75 76
    bool eventFilter(QObject *object, QEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
Lorenz Haas's avatar
Lorenz Haas committed
77 78 79 80 81

signals:
    void documentationChanged(const QString &word, const QString &documentation);

private:
82 83
    void insertCompleterText(const QString &text);
    void updateDocumentation();
Lorenz Haas's avatar
Lorenz Haas committed
84 85
    QTextCursor cursorForTextUnderCursor(QTextCursor tc = QTextCursor()) const;

86
    AbstractSettings *m_settings = nullptr;
Lorenz Haas's avatar
Lorenz Haas committed
87 88 89 90 91 92 93 94
    QCompleter *m_completer;
    QStringListModel *m_model;
    ConfigurationSyntaxHighlighter *m_highlighter;
    QString m_lastDocumentation;
};

} // namespace Internal
} // namespace Beautifier