diff --git a/src/vlistwidget.cpp b/src/vlistwidget.cpp index c5d93a66..11a6a047 100644 --- a/src/vlistwidget.cpp +++ b/src/vlistwidget.cpp @@ -1,19 +1,28 @@ #include "vlistwidget.h" + #include #include #include #include #include + +#include "vlineedit.h" #include "utils/vutils.h" const QString searchPrefix("Search for: "); + //TODO: make the style configuable const QString c_searchKeyStyle("border:none; background:#eaeaea; color:%1;"); + const QString c_colorNotMatch("#fd676b"); + const QString c_colorMatch("grey"); -VListWidget::VListWidget(QWidget *parent):QListWidget(parent), m_isInSearch(false), - m_curItemIdx(-1), m_curItem(nullptr) +VListWidget::VListWidget(QWidget *parent) + : QListWidget(parent), + m_isInSearch(false), + m_curItemIdx(-1), + m_curItem(nullptr) { m_label = new QLabel(searchPrefix, this); //TODO: make the style configuable @@ -122,13 +131,15 @@ void VListWidget::keyPressEvent(QKeyEvent *p_event) } } -void VListWidget::enterSearchMode() { +void VListWidget::enterSearchMode() +{ m_label->show(); m_searchKey->show(); setSelectionMode(QAbstractItemView::SingleSelection); } -void VListWidget::exitSearchMode(bool restoreSelection) { +void VListWidget::exitSearchMode(bool restoreSelection) +{ m_searchKey->clear(); m_label->hide(); m_searchKey->hide(); @@ -138,8 +149,8 @@ void VListWidget::exitSearchMode(bool restoreSelection) { } } - -void VListWidget::refresh() { +void VListWidget::refresh() +{ m_isInSearch = false; m_hitItems = findItems("", Qt::MatchContains); m_hitCount = m_hitItems.count(); @@ -160,7 +171,8 @@ void VListWidget::refresh() { } } -void VListWidget::clear() { +void VListWidget::clear() +{ QListWidget::clear(); m_hitCount = 0; m_hitItems.clear(); @@ -170,7 +182,8 @@ void VListWidget::clear() { exitSearchMode(); } -void VListWidget::selectItem(QListWidgetItem *item) { +void VListWidget::selectItem(QListWidgetItem *item) +{ if (item) { for(const auto& it : selectedItems()) { it->setSelected(false); diff --git a/src/vlistwidget.h b/src/vlistwidget.h index f7c72afb..c7df808e 100644 --- a/src/vlistwidget.h +++ b/src/vlistwidget.h @@ -2,22 +2,27 @@ #define VLISTWIDGET_H #include -#include #include #include #include #include -#include "vlineedit.h" #include #include +class VLineEdit; + class VItemDelegate : public QItemDelegate { public: - explicit VItemDelegate(QObject *parent = Q_NULLPTR):QItemDelegate(parent), m_searchKey() { + explicit VItemDelegate(QObject *parent = Q_NULLPTR) + : QItemDelegate(parent), m_searchKey() + { } - void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { + void paint(QPainter *painter, + const QStyleOptionViewItem &option, + const QModelIndex &index) const Q_DECL_OVERRIDE + { painter->save(); QPainter::CompositionMode oldCompMode = painter->compositionMode(); // set background color @@ -28,6 +33,7 @@ public: } else { // use default brush } + painter->drawRect(option.rect); Qt::GlobalColor hitPenColor = Qt::blue; @@ -39,7 +45,8 @@ public: if (value.isValid()) { QString text = value.toString(); int idx; - bool isHit = !m_searchKey.isEmpty() && (idx=text.indexOf(m_searchKey, 0, Qt::CaseInsensitive)) != -1; + bool isHit = !m_searchKey.isEmpty() + && (idx = text.indexOf(m_searchKey, 0, Qt::CaseInsensitive)) != -1; if (isHit) { qDebug() << QString("highlight: %1 (with: %2)").arg(text).arg(m_searchKey); // split the text by the search key @@ -63,7 +70,13 @@ public: painter->restore(); } - void drawText(QPainter *painter, Qt::GlobalColor penColor, QRectF& rect, int flags, QString text, QRectF& boundRect) const { + void drawText(QPainter *painter, + Qt::GlobalColor penColor, + QRectF& rect, + int flags, + QString text, + QRectF& boundRect) const + { if (!text.isEmpty()) { painter->setPen(QPen(penColor)); painter->drawText(rect, flags, text, &boundRect); @@ -71,7 +84,8 @@ public: } } - void setSearchKey(const QString& key) { + void setSearchKey(const QString& key) + { m_searchKey = key; } @@ -83,16 +97,23 @@ class VListWidget : public QListWidget { public: explicit VListWidget(QWidget *parent = Q_NULLPTR); - void keyPressEvent(QKeyEvent *event); + void selectItem(QListWidgetItem *item); + void exitSearchMode(bool restoreSelection=true); + void enterSearchMode(); + void refresh(); -public Q_SLOTS: - void handleSearchKeyChanged(const QString& updatedText); +public slots: void clear(); +private slots: + void handleSearchKeyChanged(const QString& updatedText); + +protected: + void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE; private: QLabel *m_label; @@ -100,10 +121,19 @@ private: bool m_isInSearch; VItemDelegate* m_delegateObj; - QList m_hitItems; // items that are matched by the search key - int m_hitCount; // how many items are matched, if no search key or key is empty string, all items are matched - int m_curItemIdx; // current selected item index - QListWidgetItem* m_curItem; // current selected item + + // Items that are matched by the search key. + QList m_hitItems; + + // How many items are matched, if no search key or key is empty string, + // all items are matched. + int m_hitCount; + + // Current selected item index. + int m_curItemIdx; + + // Current selected item. + QListWidgetItem* m_curItem; }; #endif // VLISTWIDGET_H