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