mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
clean up code styles
This commit is contained in:
parent
74cb54e02b
commit
abe40cc74f
@ -1,19 +1,28 @@
|
|||||||
#include "vlistwidget.h"
|
#include "vlistwidget.h"
|
||||||
|
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
|
||||||
|
#include "vlineedit.h"
|
||||||
#include "utils/vutils.h"
|
#include "utils/vutils.h"
|
||||||
|
|
||||||
const QString searchPrefix("Search for: ");
|
const QString searchPrefix("Search for: ");
|
||||||
|
|
||||||
//TODO: make the style configuable
|
//TODO: make the style configuable
|
||||||
const QString c_searchKeyStyle("border:none; background:#eaeaea; color:%1;");
|
const QString c_searchKeyStyle("border:none; background:#eaeaea; color:%1;");
|
||||||
|
|
||||||
const QString c_colorNotMatch("#fd676b");
|
const QString c_colorNotMatch("#fd676b");
|
||||||
|
|
||||||
const QString c_colorMatch("grey");
|
const QString c_colorMatch("grey");
|
||||||
|
|
||||||
VListWidget::VListWidget(QWidget *parent):QListWidget(parent), m_isInSearch(false),
|
VListWidget::VListWidget(QWidget *parent)
|
||||||
m_curItemIdx(-1), m_curItem(nullptr)
|
: QListWidget(parent),
|
||||||
|
m_isInSearch(false),
|
||||||
|
m_curItemIdx(-1),
|
||||||
|
m_curItem(nullptr)
|
||||||
{
|
{
|
||||||
m_label = new QLabel(searchPrefix, this);
|
m_label = new QLabel(searchPrefix, this);
|
||||||
//TODO: make the style configuable
|
//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_label->show();
|
||||||
m_searchKey->show();
|
m_searchKey->show();
|
||||||
setSelectionMode(QAbstractItemView::SingleSelection);
|
setSelectionMode(QAbstractItemView::SingleSelection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VListWidget::exitSearchMode(bool restoreSelection) {
|
void VListWidget::exitSearchMode(bool restoreSelection)
|
||||||
|
{
|
||||||
m_searchKey->clear();
|
m_searchKey->clear();
|
||||||
m_label->hide();
|
m_label->hide();
|
||||||
m_searchKey->hide();
|
m_searchKey->hide();
|
||||||
@ -138,8 +149,8 @@ void VListWidget::exitSearchMode(bool restoreSelection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VListWidget::refresh()
|
||||||
void VListWidget::refresh() {
|
{
|
||||||
m_isInSearch = false;
|
m_isInSearch = false;
|
||||||
m_hitItems = findItems("", Qt::MatchContains);
|
m_hitItems = findItems("", Qt::MatchContains);
|
||||||
m_hitCount = m_hitItems.count();
|
m_hitCount = m_hitItems.count();
|
||||||
@ -160,7 +171,8 @@ void VListWidget::refresh() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VListWidget::clear() {
|
void VListWidget::clear()
|
||||||
|
{
|
||||||
QListWidget::clear();
|
QListWidget::clear();
|
||||||
m_hitCount = 0;
|
m_hitCount = 0;
|
||||||
m_hitItems.clear();
|
m_hitItems.clear();
|
||||||
@ -170,7 +182,8 @@ void VListWidget::clear() {
|
|||||||
exitSearchMode();
|
exitSearchMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VListWidget::selectItem(QListWidgetItem *item) {
|
void VListWidget::selectItem(QListWidgetItem *item)
|
||||||
|
{
|
||||||
if (item) {
|
if (item) {
|
||||||
for(const auto& it : selectedItems()) {
|
for(const auto& it : selectedItems()) {
|
||||||
it->setSelected(false);
|
it->setSelected(false);
|
||||||
|
@ -2,22 +2,27 @@
|
|||||||
#define VLISTWIDGET_H
|
#define VLISTWIDGET_H
|
||||||
|
|
||||||
#include <QListWidget>
|
#include <QListWidget>
|
||||||
#include <QLineEdit>
|
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QStyleOptionViewItem>
|
#include <QStyleOptionViewItem>
|
||||||
#include <QModelIndex>
|
#include <QModelIndex>
|
||||||
#include <QItemDelegate>
|
#include <QItemDelegate>
|
||||||
#include "vlineedit.h"
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
|
||||||
|
class VLineEdit;
|
||||||
|
|
||||||
class VItemDelegate : public QItemDelegate
|
class VItemDelegate : public QItemDelegate
|
||||||
{
|
{
|
||||||
public:
|
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();
|
painter->save();
|
||||||
QPainter::CompositionMode oldCompMode = painter->compositionMode();
|
QPainter::CompositionMode oldCompMode = painter->compositionMode();
|
||||||
// set background color
|
// set background color
|
||||||
@ -28,6 +33,7 @@ public:
|
|||||||
} else {
|
} else {
|
||||||
// use default brush
|
// use default brush
|
||||||
}
|
}
|
||||||
|
|
||||||
painter->drawRect(option.rect);
|
painter->drawRect(option.rect);
|
||||||
|
|
||||||
Qt::GlobalColor hitPenColor = Qt::blue;
|
Qt::GlobalColor hitPenColor = Qt::blue;
|
||||||
@ -39,7 +45,8 @@ public:
|
|||||||
if (value.isValid()) {
|
if (value.isValid()) {
|
||||||
QString text = value.toString();
|
QString text = value.toString();
|
||||||
int idx;
|
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) {
|
if (isHit) {
|
||||||
qDebug() << QString("highlight: %1 (with: %2)").arg(text).arg(m_searchKey);
|
qDebug() << QString("highlight: %1 (with: %2)").arg(text).arg(m_searchKey);
|
||||||
// split the text by the search key
|
// split the text by the search key
|
||||||
@ -63,7 +70,13 @@ public:
|
|||||||
painter->restore();
|
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()) {
|
if (!text.isEmpty()) {
|
||||||
painter->setPen(QPen(penColor));
|
painter->setPen(QPen(penColor));
|
||||||
painter->drawText(rect, flags, text, &boundRect);
|
painter->drawText(rect, flags, text, &boundRect);
|
||||||
@ -71,7 +84,8 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSearchKey(const QString& key) {
|
void setSearchKey(const QString& key)
|
||||||
|
{
|
||||||
m_searchKey = key;
|
m_searchKey = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,16 +97,23 @@ class VListWidget : public QListWidget
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit VListWidget(QWidget *parent = Q_NULLPTR);
|
explicit VListWidget(QWidget *parent = Q_NULLPTR);
|
||||||
void keyPressEvent(QKeyEvent *event);
|
|
||||||
void selectItem(QListWidgetItem *item);
|
void selectItem(QListWidgetItem *item);
|
||||||
|
|
||||||
void exitSearchMode(bool restoreSelection=true);
|
void exitSearchMode(bool restoreSelection=true);
|
||||||
|
|
||||||
void enterSearchMode();
|
void enterSearchMode();
|
||||||
|
|
||||||
void refresh();
|
void refresh();
|
||||||
|
|
||||||
public Q_SLOTS:
|
public slots:
|
||||||
void handleSearchKeyChanged(const QString& updatedText);
|
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void handleSearchKeyChanged(const QString& updatedText);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QLabel *m_label;
|
QLabel *m_label;
|
||||||
@ -100,10 +121,19 @@ private:
|
|||||||
bool m_isInSearch;
|
bool m_isInSearch;
|
||||||
|
|
||||||
VItemDelegate* m_delegateObj;
|
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
|
// Items that are matched by the search key.
|
||||||
int m_curItemIdx; // current selected item index
|
QList<QListWidgetItem*> m_hitItems;
|
||||||
QListWidgetItem* m_curItem; // current selected item
|
|
||||||
|
// 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
|
#endif // VLISTWIDGET_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user