quicksearchline.cpp 4.61 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  SPDX-FileCopyrightText: 2014-2021 Laurent Montel <montel@kde.org>
3

4
 SPDX-License-Identifier: LGPL-2.0-or-later
5
6
7
8
9

*/

#include "quicksearchline.h"
#include "messagelistsettings.h"
10
#include <KStringHandler>
11

Laurent Montel's avatar
Laurent Montel committed
12
#include "searchlinestatus.h"
13
14
#include <KLocalizedString>

Laurent Montel's avatar
Laurent Montel committed
15
#include <QComboBox>
16
#include <QHBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
17
#include <QPushButton>
18
19
20
21
#include <QStandardPaths>

using namespace MessageList::Core;
QuickSearchLine::QuickSearchLine(QWidget *parent)
22
    : QWidget(parent)
Laurent Montel's avatar
Laurent Montel committed
23
24
    , mSearchEdit(new SearchLineStatus(this))
    , mTagFilterCombo(new QComboBox(this))
25
{
Laurent Montel's avatar
Laurent Montel committed
26
    auto vbox = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
27
    vbox->setContentsMargins({});
28
29
    vbox->setSpacing(0);

Laurent Montel's avatar
Laurent Montel committed
30
31
    auto w = new QWidget;
    auto hbox = new QHBoxLayout(w);
Laurent Montel's avatar
Laurent Montel committed
32
    hbox->setContentsMargins({});
33
    hbox->setSpacing(0);
Laurent Montel's avatar
Laurent Montel committed
34
    vbox->addWidget(w);
35

Laurent Montel's avatar
Laurent Montel committed
36
    connect(mSearchEdit, &SearchLineStatus::filterActionChanged, this, &QuickSearchLine::slotFilterActionChanged);
37
    connect(mSearchEdit, &SearchLineStatus::searchOptionChanged, this, &QuickSearchLine::searchOptionChanged);
38
    connect(mSearchEdit, &SearchLineStatus::forceLostFocus, this, &QuickSearchLine::forceLostFocus);
39
40
    mSearchEdit->setPlaceholderText(i18nc("Search for messages.", "Search"));
    mSearchEdit->setObjectName(QStringLiteral("quicksearch"));
41
    mSearchEdit->setClearButtonEnabled(true);
42

43
    connect(mSearchEdit, &QLineEdit::textChanged, this, &QuickSearchLine::slotSearchEditTextEdited);
Laurent Montel's avatar
Laurent Montel committed
44
    connect(mSearchEdit, &SearchLineStatus::clearButtonClicked, this, &QuickSearchLine::slotClearButtonClicked);
45
46
47
48
49
50
51
52
53

    hbox->addWidget(mSearchEdit);

    // The status filter button. Will be populated later, as populateStatusFilterCombo() is virtual
    mTagFilterCombo->setMaximumWidth(300);
    mTagFilterCombo->setMaximumWidth(200);
    mTagFilterCombo->hide();
    hbox->addWidget(mTagFilterCombo);

Laurent Montel's avatar
Laurent Montel committed
54
    // Be disable until we have a storageModel => logical that it's disable.
55
56
57
58
59
60
61
62
63
64
65
    mSearchEdit->setEnabled(false);
    mTagFilterCombo->setEnabled(false);

    installEventFilter(this);
    mTagFilterCombo->installEventFilter(this);
    changeQuicksearchVisibility(MessageListSettings::self()->showQuickSearch());
}

QuickSearchLine::~QuickSearchLine()
{
}
Laurent Montel's avatar
Laurent Montel committed
66

67
68
69
70
71
72
73
void QuickSearchLine::slotSearchEditTextEdited(const QString &text)
{
    int minimumStringLength = 3;
    if (text.startsWith(QLatin1Char('"')) && text.endsWith(QLatin1Char('"'))) {
        minimumStringLength = 5;
    }
    if (!text.trimmed().isEmpty()) {
Laurent Montel's avatar
Use >=    
Laurent Montel committed
74
        if (KStringHandler::logicalLength(text) >= minimumStringLength) {
75
76
77
78
79
80
81
82
83
84
85
86
            Q_EMIT searchEditTextEdited(text);
        }
    } else {
        slotClearButtonClicked();
    }
}

void QuickSearchLine::slotClearButtonClicked()
{
    if (mTagFilterCombo->isVisible()) {
        mTagFilterCombo->setCurrentIndex(0);
    }
87
    mSearchEdit->clearFilterButtonClicked();
88
89
90
91
92
    Q_EMIT clearButtonClicked();
}

QuickSearchLine::SearchOptions QuickSearchLine::searchOptions() const
{
93
    return mSearchEdit->searchOptions();
94
95
96
97
98
99
100
101
102
103
}

void QuickSearchLine::focusQuickSearch(const QString &selectedText)
{
    if (!selectedText.isEmpty()) {
        mSearchEdit->setText(selectedText);
    }
    mSearchEdit->setFocus();
}

Laurent Montel's avatar
Laurent Montel committed
104
QComboBox *QuickSearchLine::tagFilterComboBox() const
105
106
107
108
{
    return mTagFilterCombo;
}

Laurent Montel's avatar
Laurent Montel committed
109
SearchLineStatus *QuickSearchLine::searchEdit() const
110
111
112
113
114
115
116
117
118
{
    return mSearchEdit;
}

void QuickSearchLine::resetFilter()
{
    if (mTagFilterCombo->isVisible()) {
        mTagFilterCombo->setCurrentIndex(0);
    }
119
    mSearchEdit->clearFilterButtonClicked();
Laurent Montel's avatar
Laurent Montel committed
120
    mSearchEdit->setLocked(false);
121
122
}

Laurent Montel's avatar
Laurent Montel committed
123
void QuickSearchLine::slotFilterActionChanged(const QVector<Akonadi::MessageStatus> &lst)
124
{
Laurent Montel's avatar
Laurent Montel committed
125
126
    mLstStatus = lst;
    Q_EMIT statusButtonsClicked();
127
128
}

Laurent Montel's avatar
Laurent Montel committed
129
QVector<Akonadi::MessageStatus> QuickSearchLine::status() const
Laurent Montel's avatar
Laurent Montel committed
130
131
132
133
{
    return mLstStatus;
}

134
135
bool QuickSearchLine::containsOutboundMessages() const
{
136
    return mSearchEdit->containsOutboundMessages();
137
138
139
140
}

void QuickSearchLine::setContainsOutboundMessages(bool containsOutboundMessages)
{
141
    mSearchEdit->setContainsOutboundMessages(containsOutboundMessages);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
}

void QuickSearchLine::updateComboboxVisibility()
{
    mTagFilterCombo->setVisible(!mSearchEdit->isHidden() && mTagFilterCombo->count());
}

bool QuickSearchLine::eventFilter(QObject *object, QEvent *e)
{
    const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
    if (shortCutOverride) {
        e->accept();
        return true;
    }
    return QWidget::eventFilter(object, e);
}

void QuickSearchLine::changeQuicksearchVisibility(bool show)
{
    mSearchEdit->setVisible(show);
    mTagFilterCombo->setVisible(show && mTagFilterCombo->count());
}
164
165
166
167
168

void QuickSearchLine::addCompletionItem(const QString &str)
{
    mSearchEdit->addCompletionItem(str);
}