|
|
@@ -2,22 +2,27 @@
|
|
|
#define VLISTWIDGET_H
|
|
|
|
|
|
#include <QListWidget>
|
|
|
-#include <QLineEdit>
|
|
|
#include <QPainter>
|
|
|
#include <QStyleOptionViewItem>
|
|
|
#include <QModelIndex>
|
|
|
#include <QItemDelegate>
|
|
|
-#include "vlineedit.h"
|
|
|
#include <QDebug>
|
|
|
#include <QLabel>
|
|
|
|
|
|
+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<QListWidgetItem*> 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<QListWidgetItem*> 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
|