summaryrefslogtreecommitdiff
path: root/src/plugins/qmldesigner/components/bindingeditor/signallistdialog.cpp
blob: 9be6894073f2eda3e9b4b7d3be504097f81d1286 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "signallistdialog.h"

#include "signallist.h"
#include "signallistdelegate.h"

#include <qmldesignerplugin.h>

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

#include <QStandardItemModel>
#include <QPainter>
#include <QTableView>
#include <QHeaderView>
#include <QVBoxLayout>
#include <QSortFilterProxyModel>

namespace QmlDesigner {

QWidget *createFilterWidget(Utils::FancyLineEdit *lineEdit)
{
    const QString unicode = Theme::getIconUnicode(Theme::Icon::search);
    const QString fontName = "qtds_propertyIconFont.ttf";
    QIcon icon = Utils::StyleHelper::getIconFromIconFont(fontName, unicode, 28, 28);
    auto *label = new QLabel;
    label->setPixmap(icon.pixmap(QSize(24, 24)));
    label->setAlignment(Qt::AlignCenter);
    lineEdit->setPlaceholderText(QObject::tr("<Filter>", "Library search input hint text"));
    lineEdit->setDragEnabled(false);
    lineEdit->setMinimumWidth(75);
    lineEdit->setTextMargins(0, 0, 20, 0);
    lineEdit->setFiltering(true);
    auto *box = new QHBoxLayout;
    box->addWidget(label);
    box->addWidget(lineEdit);
    auto *widget = new QWidget;
    widget->setLayout(box);
    return widget;
}

void modifyPalette(QTableView *view, const QColor &selectionColor)
{
    QPalette p = view->palette();
    p.setColor(QPalette::AlternateBase, p.color(QPalette::Base).lighter(120));
    p.setColor(QPalette::Highlight, selectionColor);
    view->setPalette(p);
    view->setAlternatingRowColors(true);
}


SignalListDialog::SignalListDialog(QWidget *parent)
    : QDialog(parent)
    , m_table(new QTableView())
    , m_searchLine(new Utils::FancyLineEdit())
{
    auto *signalListDelegate = new SignalListDelegate(m_table);
    m_table->setItemDelegate(signalListDelegate);
    m_table->setFocusPolicy(Qt::NoFocus);
    m_table->setSelectionMode(QAbstractItemView::NoSelection);
    m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_table->verticalHeader()->hide();

    modifyPalette(m_table, QColor("#d87b00"));

    auto *layout = new QVBoxLayout;
    layout->addWidget(createFilterWidget(m_searchLine));
    layout->addWidget(m_table);
    setLayout(layout);

    setWindowFlag(Qt::Tool, true);
    setModal(true);
    resize(600, 480);
}

SignalListDialog::~SignalListDialog()
{
}

void SignalListDialog::initialize(QStandardItemModel *model)
{
    m_searchLine->clear();

    auto *proxyModel = new SignalListFilterModel(this);
    proxyModel->setSourceModel(model);
    m_table->setModel(proxyModel);

    QHeaderView *header = m_table->horizontalHeader();
    header->setSectionResizeMode(SignalListModel::TargetColumn, QHeaderView::Stretch);
    header->setSectionResizeMode(SignalListModel::SignalColumn, QHeaderView::Stretch);
    header->resizeSection(SignalListModel::ButtonColumn, 120);
    header->setStretchLastSection(false);

    auto eventFilterFun = [this](const QString &str) {
        if (auto *fm = qobject_cast<SignalListFilterModel *>(m_table->model())) {
            const QRegularExpression::PatternOption option
                = fm->filterCaseSensitivity() == Qt::CaseInsensitive
                      ? QRegularExpression::CaseInsensitiveOption
                      : QRegularExpression::NoPatternOption;
            fm->setFilterRegularExpression(
                QRegularExpression(QRegularExpression::escape(str), option));
        }
    };
    connect(m_searchLine, &Utils::FancyLineEdit::filterChanged, eventFilterFun);
}

QTableView *SignalListDialog::tableView() const
{
    return m_table;
}

} // QmlDesigner namespace