aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/completingtextedit.h
blob: 8aa3cbce50417fb8709f257975e3cc4b92071bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "utils_global.h"
#include "fancyiconbutton.h"

#include <QTextEdit>

QT_BEGIN_NAMESPACE
class QCompleter;
class QEvent;
class QKeyEvent;
class QFocusEvent;
class QResizeEvent;
QT_END_NAMESPACE

namespace Utils {

class FilePath;

class QTCREATOR_UTILS_EXPORT CompletingTextEdit : public QTextEdit
{
    Q_OBJECT
    Q_PROPERTY(int completionLengthThreshold
               READ completionLengthThreshold WRITE setCompletionLengthThreshold)
    Q_PROPERTY(CompletionBehavior completionBehavior READ completionBehavior WRITE setCompletionBehavior)

public:
    enum class CompletionBehavior {
        OnKeyPress,
        OnTextChange
    };
    Q_ENUM(CompletionBehavior)

    CompletingTextEdit(QWidget *parent = nullptr);
    ~CompletingTextEdit() override;

    void setCompleter(QCompleter *c);
    QCompleter *completer() const;

    int completionLengthThreshold() const;
    void setCompletionLengthThreshold(int len);

    CompletionBehavior completionBehavior() const;
    void setCompletionBehavior(CompletionBehavior behavior);

    void setRightSideIconPath(const FilePath &path);
    void setRightSideIcon(const QIcon &icon);

signals:
    void returnPressed();
    void rightSideIconClicked();

protected:
    void keyPressEvent(QKeyEvent *e) override;
    void focusInEvent(QFocusEvent *e) override;
    bool event(QEvent *e) override;
    void resizeEvent(QResizeEvent *e) override;

private:
    void updateIconPosition();
    void updateTextMargins();
    void handleIconClick();
    QString getCompletionPrefix() const;
    void updateCompletion();
    void showCompleterPopup();

private:
    class CompletingTextEditPrivate *d;
    FancyIconButton *m_rightButton = nullptr;
    CompletionBehavior m_completionBehavior = CompletionBehavior::OnKeyPress;
};

} // namespace Utils