aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/common/extlineedit.cpp
blob: 325e93cc7db965814d2f00420d2aab9e28416a59 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "extlineedit.h"
#include "iconmanager.h"
#include <QStyle>
#include <QAction>
#include <QDebug>

ExtLineEdit::ExtLineEdit(QWidget* parent)
    : QLineEdit(parent)
{
    init();
}

ExtLineEdit::ExtLineEdit(const QString& text, QWidget *parent)
    : QLineEdit(text, parent)
{
    init();
}

void ExtLineEdit::init()
{
    connect(this, &QLineEdit::textChanged, this, &ExtLineEdit::handleTextChanged);
}

void ExtLineEdit::updateMinSize()
{
    setMinimumSize(expandingMinWidth, 0);
}

int ExtLineEdit::getExpandingMaxWidth() const
{
    return expandingMaxWidth;
}

void ExtLineEdit::setExpandingMaxWidth(int value)
{
    expandingMaxWidth = value;
    setMaximumWidth(value);
}

void ExtLineEdit::setClearButtonEnabled(bool enable)
{
    QLineEdit::setClearButtonEnabled(enable);
    if (enable)
    {
        // This is a hack to get to know when QLineEdit's clear button is pressed.
        // Unfortunately Qt 5.2 API doesn't provide such information,
        // but we can find QAction responsible for it by its object name
        // and handle its triggered() signal.
        // This is not part of an official Qt's API and may be modified in any Qt version.
        // Ugly, but works.
        static const char* qtClearBtnActionName = "_q_qlineeditclearaction";
        QAction *clearAction = findChild<QAction*>(qtClearBtnActionName);
        if (!clearAction)
        {
            qWarning() << "Could not find 'clear action' in QLineEdit, so 'valueErased()' signal won't be emitted from ExtLineEdit.";
            return;
        }
        connect(clearAction, SIGNAL(triggered()), this, SLOT(checkForValueErased()));
        connect(this, SIGNAL(textEdited(QString)), this, SLOT(checkForValueErased(QString)));
    }
}

void ExtLineEdit::checkForValueErased()
{
    nextClearingIsFromButton = true;
}

void ExtLineEdit::checkForValueErased(const QString& text)
{
    if (nextClearingIsFromButton && text.isEmpty())
        emit valueErased();

    nextClearingIsFromButton = false;
}

bool ExtLineEdit::getExpanding() const
{
    return expanding;
}

void ExtLineEdit::setExpanding(bool value)
{
    expanding = value;
    if (!expanding)
        setFixedWidth(-1);
    else
        setFixedWidth(expandingMinWidth);
}

int ExtLineEdit::getExpandingMinWidth() const
{
    return expandingMinWidth;
}

void ExtLineEdit::setExpandingMinWidth(int value)
{
    expandingMinWidth = value;
    updateMinSize();
}

void ExtLineEdit::handleTextChanged()
{
    QString txt = text();
    if (!expanding)
        return;

    // Text width
    int newWidth = fontMetrics().horizontalAdvance(txt);

    // Text margins
    QMargins margins = textMargins();
    newWidth += margins.left() + margins.right();

    // Content margins
    QMargins localContentsMargins = contentsMargins();
    newWidth += localContentsMargins.left() + localContentsMargins.right();

    // Frame
    int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
    newWidth += frameWidth * 2;

    // Extra space
    newWidth += expandingExtraSpace;

    if (newWidth < expandingMinWidth)
        newWidth = expandingMinWidth;
    else if (expandingMaxWidth > 0 && newWidth > expandingMaxWidth)
        newWidth = expandingMaxWidth;

    setFixedWidth(newWidth);
}