aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/navigator/navigatorsearchwidget.cpp
blob: c602c70734bdc7926f2f303d45da5b8b340978a8 (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
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "navigatorsearchwidget.h"

#include <utils/stylehelper.h>
#include <theme.h>

#include <QAction>
#include <QBoxLayout>
#include <QLabel>
#include <QStyle>
#include <QToolButton>

namespace QmlDesigner {

LineEdit::LineEdit(QWidget *parent)
    : QLineEdit(parent)
{
    clearButton = new QToolButton(this);

    const QString fontName = "qtds_propertyIconFont.ttf";
    const int searchIconSize = 16;
    const int clearIconSize = 12;
    const QColor iconColor(QmlDesigner::Theme::getColor(QmlDesigner::Theme::DSiconColor));

    const QIcon searchIcon
        = Utils::StyleHelper::getIconFromIconFont(fontName,
                                                  QmlDesigner::Theme::getIconUnicode(
                                                      QmlDesigner::Theme::Icon::search),
                                                  searchIconSize,
                                                  searchIconSize,
                                                  iconColor);

    const QIcon clearIcon
        = Utils::StyleHelper::getIconFromIconFont(fontName,
                                                  QmlDesigner::Theme::getIconUnicode(
                                                      QmlDesigner::Theme::Icon::closeCross),
                                                  clearIconSize,
                                                  clearIconSize,
                                                  iconColor);

    addAction(searchIcon, QLineEdit::LeadingPosition);

    clearButton->setIcon(clearIcon);
    clearButton->setIconSize(QSize(clearIconSize, clearIconSize));
    clearButton->setCursor(Qt::ArrowCursor);
    clearButton->hide();
    clearButton->setStyleSheet(Theme::replaceCssColors(
        QString("QToolButton { border: none; padding: 0px; }"
                "QToolButton:hover { background: creatorTheme.DScontrolBackgroundHover; }")));

    setClearButtonEnabled(false);

    connect(clearButton, &QToolButton::clicked, this, &QLineEdit::clear);
    connect(this, &QLineEdit::textChanged, this, &LineEdit::updateClearButton);

    int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
    setStyleSheet(QString("QLineEdit { padding-right: %1px; } ")
                      .arg(clearButton->sizeHint().width() + frameWidth + 8));

    setFixedHeight(29);
}

void LineEdit::resizeEvent(QResizeEvent *)
{
    QSize hint = clearButton->sizeHint();
    int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);

    clearButton->move(rect().right() - frameWidth - hint.width() - 3,
                      (rect().bottom() + 1 - hint.height()) / 2);
}

void LineEdit::updateClearButton(const QString& text)
{
    clearButton->setVisible(!text.isEmpty());
}

NavigatorSearchWidget::NavigatorSearchWidget(QWidget *parent)
    : QWidget(parent)
{
    auto layout = new QBoxLayout(QBoxLayout::LeftToRight);
    layout->setSpacing(0);
    layout->setContentsMargins(5, 5, 5, 3);
    setLayout(layout);

    m_textField = new LineEdit;
    m_textField->setPlaceholderText(tr("Search"));
    m_textField->setFrame(false);

    connect(m_textField, &QLineEdit::textChanged, this, &NavigatorSearchWidget::textChanged);

    layout->addWidget(m_textField);
}

void NavigatorSearchWidget::clear()
{
    m_textField->clear();
}

} // QmlDesigner