Added support for sorting ListItems in a ListView
This commit is contained in:
@@ -26,6 +26,20 @@ namespace Bloom::Widgets
|
||||
return;
|
||||
}
|
||||
|
||||
virtual bool operator < (const ListItem& rhs) const = 0;
|
||||
|
||||
bool operator > (const ListItem& rhs) const {
|
||||
return rhs < *this;
|
||||
}
|
||||
|
||||
bool operator <= (const ListItem& rhs) const {
|
||||
return !(rhs < *this);
|
||||
}
|
||||
|
||||
bool operator >= (const ListItem& rhs) const {
|
||||
return !(*this < rhs);
|
||||
}
|
||||
|
||||
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override = 0;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -12,15 +12,11 @@ namespace Bloom::Widgets
|
||||
std::vector<ListItem*> items,
|
||||
QGraphicsView* parent
|
||||
)
|
||||
: listItems(std::move(items))
|
||||
, parent(parent)
|
||||
: parent(parent)
|
||||
, QGraphicsScene(parent)
|
||||
{
|
||||
this->setItemIndexMethod(QGraphicsScene::NoIndex);
|
||||
|
||||
for (const auto& listItem : this->listItems) {
|
||||
this->addItem(listItem);
|
||||
}
|
||||
this->setItems(std::move(items));
|
||||
}
|
||||
|
||||
void ListScene::refreshGeometry() {
|
||||
@@ -48,6 +44,35 @@ namespace Bloom::Widgets
|
||||
this->update();
|
||||
}
|
||||
|
||||
void ListScene::setItems(const std::vector<ListItem*>& items) {
|
||||
for (auto& item : this->items()) {
|
||||
this->removeItem(item);
|
||||
}
|
||||
|
||||
this->listItems = items;
|
||||
|
||||
for (const auto& listItem : this->listItems) {
|
||||
this->addItem(listItem);
|
||||
}
|
||||
|
||||
this->sortItems();
|
||||
}
|
||||
|
||||
void ListScene::addListItem(ListItem* item) {
|
||||
this->listItems.push_back(item);
|
||||
this->addItem(item);
|
||||
}
|
||||
|
||||
void ListScene::sortItems() {
|
||||
std::sort(
|
||||
this->listItems.begin(),
|
||||
this->listItems.end(),
|
||||
[] (const ListItem* itemA, const ListItem* itemB) {
|
||||
return *itemA < *itemB;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void ListScene::setEnabled(bool enabled) {
|
||||
if (this->enabled == enabled) {
|
||||
return;
|
||||
|
||||
@@ -28,6 +28,9 @@ namespace Bloom::Widgets
|
||||
);
|
||||
|
||||
void refreshGeometry();
|
||||
void setItems(const std::vector<ListItem*>& items);
|
||||
void addListItem(ListItem* item);
|
||||
void sortItems();
|
||||
void setEnabled(bool enabled);
|
||||
|
||||
signals:
|
||||
@@ -42,7 +45,7 @@ namespace Bloom::Widgets
|
||||
void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;
|
||||
|
||||
private:
|
||||
const std::vector<ListItem*> listItems;
|
||||
std::vector<ListItem*> listItems;
|
||||
QGraphicsView* const parent;
|
||||
bool enabled = false;
|
||||
ListItem* selectedItem = nullptr;
|
||||
|
||||
@@ -37,6 +37,11 @@ namespace Bloom::Widgets
|
||||
|
||||
void refreshGeometry();
|
||||
|
||||
bool operator < (const ListItem& rhs) const override {
|
||||
const auto& rhsRegisterGroupItem = dynamic_cast<const RegisterGroupItem&>(rhs);
|
||||
return this->groupName < rhsRegisterGroupItem.groupName;
|
||||
}
|
||||
|
||||
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
|
||||
|
||||
private:
|
||||
|
||||
@@ -29,6 +29,11 @@ namespace Bloom::Widgets
|
||||
this->valueText.clear();
|
||||
}
|
||||
|
||||
bool operator < (const ListItem& rhs) const override {
|
||||
const auto& rhsRegisterItem = dynamic_cast<const RegisterItem&>(rhs);
|
||||
return this->registerName < rhsRegisterItem.registerName;
|
||||
}
|
||||
|
||||
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user