#ifndef MTLISTWIDGET_H
#define MTLISTWIDGET_H
#include <QListWidget>
#include <QLineEdit>
#include "mtwidgetpalettes.h"
class MTListWidget : public QListWidget
{
Q_OBJECT
public:
MTListWidget(QWidget * parent = 0):
QListWidget(parent) {
QObject::connect(this, SIGNAL(currentTextChanged(QString)), this, SLOT(emitCurrentIndexAvailabilityChanged()));
};
public slots:
int filterItems(QLineEdit * le, const QString & keyword) {
if (keyword.isEmpty()) {
le->setPalette(((QWidget *)(parent()))->palette());
} else {
le->setPalette(searchLineEditPalettes.search_active_palette);
}
int n = 0;
for (int i = 0; i < count(); ++i) {
if (this->item(i)->text().contains(keyword, Qt::CaseInsensitive)) {
this->item(i)->setHidden(false); n++;
} else { this->item(i)->setHidden(true); }
}
if ((!keyword.isEmpty()) && count() != 0 && n == 0) {
le->setPalette(searchLineEditPalettes.search_noresults_palette);
}
return n;
};
void highlightItem(int i) { highlightItem(item(i)); };
static void highlightItem(QListWidgetItem * item)
{
if (item == NULL) { return; }
for (int i = 0; i < item->listWidget()->count(); ++i) {
item->listWidget()->item(i)->setBackground(QBrush::QBrush(QColor::QColor(255, 255, 255)));
item->listWidget()->item(i)->setForeground(QBrush::QBrush(QColor::QColor(0, 0, 0)));
}
item->setBackground(QBrush::QBrush(QColor::QColor(197, 255, 120)));
item->setForeground(QBrush::QBrush(QColor::QColor(0, 0, 0)));
item->listWidget()->clearSelection();
};
bool isItemHighlighted(int i) { return isItemHighlighted(item(i)); };
static bool isItemHighlighted(QListWidgetItem * item) {
if (item == NULL) { return false; }
return (item->background() == QBrush::QBrush(QColor::QColor(197, 255, 120)));
};
int highlightedRow() { return row(highlightedItem()); };
QListWidgetItem * highlightedItem() {
for (int i = 0; i < count(); ++i) {
if (isItemHighlighted(i)) { return this->item(i); }
}
return NULL;
};
private slots:
void emitCurrentIndexAvailabilityChanged() {
emit currentIndexAvailabilityChanged(currentIndex().isValid());
};
signals:
void currentIndexAvailabilityChanged(bool);
private:
SearchLineEditPalettes searchLineEditPalettes;
};
#endif