File indexing completed on 2024-12-15 04:54:36
0001 /* 0002 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 0006 */ 0007 0008 #include "quicksearchline.h" 0009 #include "messagelistsettings.h" 0010 #include <KStringHandler> 0011 0012 #include "core/filter.h" 0013 #include "searchlinestatus.h" 0014 #include <KLocalizedString> 0015 0016 #include <QComboBox> 0017 #include <QHBoxLayout> 0018 #include <QPushButton> 0019 #include <QStandardPaths> 0020 0021 using namespace MessageList::Core; 0022 QuickSearchLine::QuickSearchLine(QWidget *parent) 0023 : QWidget(parent) 0024 , mSearchEdit(new SearchLineStatus(this)) 0025 , mTagFilterCombo(new QComboBox(this)) 0026 { 0027 auto vbox = new QVBoxLayout(this); 0028 vbox->setContentsMargins({}); 0029 vbox->setSpacing(0); 0030 0031 auto w = new QWidget; 0032 auto hbox = new QHBoxLayout(w); 0033 hbox->setContentsMargins({}); 0034 hbox->setSpacing(0); 0035 vbox->addWidget(w); 0036 0037 connect(mSearchEdit, &SearchLineStatus::filterActionChanged, this, &QuickSearchLine::slotFilterActionChanged); 0038 connect(mSearchEdit, &SearchLineStatus::searchOptionChanged, this, &QuickSearchLine::searchOptionChanged); 0039 connect(mSearchEdit, &SearchLineStatus::forceLostFocus, this, &QuickSearchLine::forceLostFocus); 0040 mSearchEdit->setPlaceholderText(i18nc("Search for messages.", "Search")); 0041 mSearchEdit->setObjectName(QLatin1StringView("quicksearch")); 0042 mSearchEdit->setClearButtonEnabled(true); 0043 0044 connect(mSearchEdit, &QLineEdit::textChanged, this, &QuickSearchLine::slotSearchEditTextEdited); 0045 connect(mSearchEdit, &SearchLineStatus::clearButtonClicked, this, &QuickSearchLine::slotClearButtonClicked); 0046 connect(mSearchEdit, &SearchLineStatus::saveFilter, this, &QuickSearchLine::saveFilter); 0047 connect(mSearchEdit, &SearchLineStatus::activateFilter, this, &QuickSearchLine::activateFilter); 0048 0049 hbox->addWidget(mSearchEdit); 0050 0051 // The status filter button. Will be populated later, as populateStatusFilterCombo() is virtual 0052 mTagFilterCombo->setMaximumWidth(300); 0053 mTagFilterCombo->setMaximumWidth(200); 0054 mTagFilterCombo->hide(); 0055 hbox->addWidget(mTagFilterCombo); 0056 0057 // Be disable until we have a storageModel => logical that it's disable. 0058 mSearchEdit->setEnabled(false); 0059 mTagFilterCombo->setEnabled(false); 0060 0061 installEventFilter(this); 0062 mTagFilterCombo->installEventFilter(this); 0063 changeQuicksearchVisibility(MessageListSettings::self()->showQuickSearch()); 0064 } 0065 0066 QuickSearchLine::~QuickSearchLine() = default; 0067 0068 void QuickSearchLine::slotSearchEditTextEdited(const QString &text) 0069 { 0070 int minimumStringLength = 3; 0071 if (text.startsWith(QLatin1Char('"')) && text.endsWith(QLatin1Char('"'))) { 0072 minimumStringLength = 5; 0073 } 0074 if (!text.trimmed().isEmpty()) { 0075 if (KStringHandler::logicalLength(text) >= minimumStringLength) { 0076 Q_EMIT searchEditTextEdited(text); 0077 } 0078 } else { 0079 slotClearButtonClicked(); 0080 } 0081 } 0082 0083 void QuickSearchLine::slotClearButtonClicked() 0084 { 0085 if (mTagFilterCombo->isVisible()) { 0086 mTagFilterCombo->setCurrentIndex(0); 0087 } 0088 mSearchEdit->clearFilterButtonClicked(); 0089 Q_EMIT clearButtonClicked(); 0090 } 0091 0092 void QuickSearchLine::setSearchOptions(QuickSearchLine::SearchOptions opts) 0093 { 0094 mSearchEdit->setSearchOptions(opts); 0095 } 0096 0097 QuickSearchLine::SearchOptions QuickSearchLine::searchOptions() const 0098 { 0099 return mSearchEdit->searchOptions(); 0100 } 0101 0102 void QuickSearchLine::focusQuickSearch(const QString &selectedText) 0103 { 0104 if (!selectedText.isEmpty()) { 0105 mSearchEdit->setText(selectedText); 0106 } 0107 mSearchEdit->setFocus(); 0108 } 0109 0110 QComboBox *QuickSearchLine::tagFilterComboBox() const 0111 { 0112 return mTagFilterCombo; 0113 } 0114 0115 SearchLineStatus *QuickSearchLine::searchEdit() const 0116 { 0117 return mSearchEdit; 0118 } 0119 0120 void QuickSearchLine::resetFilter() 0121 { 0122 if (mTagFilterCombo->isVisible()) { 0123 mTagFilterCombo->setCurrentIndex(0); 0124 } 0125 mSearchEdit->clearFilterButtonClicked(); 0126 mSearchEdit->setLocked(false); 0127 } 0128 0129 void QuickSearchLine::slotFilterActionChanged(const QList<Akonadi::MessageStatus> &lst) 0130 { 0131 mLstStatus = lst; 0132 Q_EMIT statusButtonsClicked(); 0133 } 0134 0135 void QuickSearchLine::setFilterMessageStatus(const QList<Akonadi::MessageStatus> &newLstStatus) 0136 { 0137 mLstStatus = newLstStatus; 0138 mSearchEdit->setFilterMessageStatus(mLstStatus); 0139 } 0140 0141 QList<Akonadi::MessageStatus> QuickSearchLine::status() const 0142 { 0143 return mLstStatus; 0144 } 0145 0146 bool QuickSearchLine::containsOutboundMessages() const 0147 { 0148 return mSearchEdit->containsOutboundMessages(); 0149 } 0150 0151 void QuickSearchLine::setContainsOutboundMessages(bool containsOutboundMessages) 0152 { 0153 mSearchEdit->setContainsOutboundMessages(containsOutboundMessages); 0154 } 0155 0156 void QuickSearchLine::updateComboboxVisibility() 0157 { 0158 mTagFilterCombo->setVisible(!mSearchEdit->isHidden() && mTagFilterCombo->count()); 0159 } 0160 0161 bool QuickSearchLine::eventFilter(QObject *object, QEvent *e) 0162 { 0163 const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride); 0164 if (shortCutOverride) { 0165 e->accept(); 0166 return true; 0167 } 0168 return QWidget::eventFilter(object, e); 0169 } 0170 0171 void QuickSearchLine::changeQuicksearchVisibility(bool show) 0172 { 0173 mSearchEdit->setVisible(show); 0174 mTagFilterCombo->setVisible(show && mTagFilterCombo->count()); 0175 } 0176 0177 void QuickSearchLine::addCompletionItem(const QString &str) 0178 { 0179 mSearchEdit->addCompletionItem(str); 0180 } 0181 0182 #include "moc_quicksearchline.cpp"