Replaced std::vector with std::set, for automatic sorting in ListScene widget.
And added `ListScene::removeListItem()` function
This commit is contained in:
14
src/Helpers/DereferenceLessComparator.hpp
Normal file
14
src/Helpers/DereferenceLessComparator.hpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace Bloom {
|
||||||
|
template<typename Type>
|
||||||
|
requires std::is_pointer<Type>::value
|
||||||
|
struct DereferenceLessComparator
|
||||||
|
{
|
||||||
|
constexpr bool operator () (const Type& lhs, const Type& rhs) const {
|
||||||
|
return *lhs < *rhs;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
namespace Bloom::Widgets
|
namespace Bloom::Widgets
|
||||||
{
|
{
|
||||||
ListScene::ListScene(
|
ListScene::ListScene(
|
||||||
std::vector<ListItem*> items,
|
ListScene::ListItemSetType&& items,
|
||||||
QGraphicsView* parent
|
QGraphicsView* parent
|
||||||
)
|
)
|
||||||
: parent(parent)
|
: parent(parent)
|
||||||
@@ -48,7 +48,7 @@ namespace Bloom::Widgets
|
|||||||
this->update();
|
this->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListScene::setItems(const std::vector<ListItem*>& items) {
|
void ListScene::setItems(const ListScene::ListItemSetType& items) {
|
||||||
for (auto& item : this->items()) {
|
for (auto& item : this->items()) {
|
||||||
this->removeItem(item);
|
this->removeItem(item);
|
||||||
}
|
}
|
||||||
@@ -58,23 +58,16 @@ namespace Bloom::Widgets
|
|||||||
for (const auto& listItem : this->listItems) {
|
for (const auto& listItem : this->listItems) {
|
||||||
this->addItem(listItem);
|
this->addItem(listItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->sortItems();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListScene::addListItem(ListItem* item) {
|
void ListScene::addListItem(ListItem* item) {
|
||||||
this->listItems.push_back(item);
|
this->listItems.insert(item);
|
||||||
this->addItem(item);
|
this->addItem(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListScene::sortItems() {
|
void ListScene::removeListItem(ListItem* item) {
|
||||||
std::sort(
|
this->listItems.erase(item);
|
||||||
this->listItems.begin(),
|
this->removeItem(item);
|
||||||
this->listItems.end(),
|
|
||||||
[] (const ListItem* itemA, const ListItem* itemB) {
|
|
||||||
return *itemA < *itemB;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListScene::setEnabled(bool enabled) {
|
void ListScene::setEnabled(bool enabled) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include <QGraphicsScene>
|
#include <QGraphicsScene>
|
||||||
#include <QGraphicsView>
|
#include <QGraphicsView>
|
||||||
#include <QMargins>
|
#include <QMargins>
|
||||||
#include <vector>
|
#include <set>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <QGraphicsSceneMouseEvent>
|
#include <QGraphicsSceneMouseEvent>
|
||||||
#include <QGraphicsSceneWheelEvent>
|
#include <QGraphicsSceneWheelEvent>
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <QPointF>
|
#include <QPointF>
|
||||||
|
|
||||||
#include "ListItem.hpp"
|
#include "ListItem.hpp"
|
||||||
|
#include "src/Helpers/DereferenceLessComparator.hpp"
|
||||||
|
|
||||||
namespace Bloom::Widgets
|
namespace Bloom::Widgets
|
||||||
{
|
{
|
||||||
@@ -21,18 +22,19 @@ namespace Bloom::Widgets
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
using ListItemSetType = std::set<ListItem*, DereferenceLessComparator<ListItem*>>;
|
||||||
QMargins margins = QMargins(0, 0, 0, 10);
|
QMargins margins = QMargins(0, 0, 0, 10);
|
||||||
|
|
||||||
ListScene(
|
ListScene(
|
||||||
std::vector<ListItem*> items,
|
ListScene::ListItemSetType&& items,
|
||||||
QGraphicsView* parent
|
QGraphicsView* parent
|
||||||
);
|
);
|
||||||
|
|
||||||
void refreshGeometry();
|
void refreshGeometry();
|
||||||
void setSelectionLimit(std::uint8_t selectionLimit);
|
void setSelectionLimit(std::uint8_t selectionLimit);
|
||||||
void setItems(const std::vector<ListItem*>& items);
|
void setItems(const ListScene::ListItemSetType& items);
|
||||||
void addListItem(ListItem* item);
|
void addListItem(ListItem* item);
|
||||||
void sortItems();
|
void removeListItem(ListItem* item);
|
||||||
void setEnabled(bool enabled);
|
void setEnabled(bool enabled);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
@@ -47,7 +49,7 @@ namespace Bloom::Widgets
|
|||||||
void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;
|
void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<ListItem*> listItems;
|
ListScene::ListItemSetType listItems;
|
||||||
QGraphicsView* const parent;
|
QGraphicsView* const parent;
|
||||||
bool enabled = false;
|
bool enabled = false;
|
||||||
std::list<ListItem*> selectedItems;
|
std::list<ListItem*> selectedItems;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
namespace Bloom::Widgets
|
namespace Bloom::Widgets
|
||||||
{
|
{
|
||||||
ListView::ListView(
|
ListView::ListView(
|
||||||
const std::vector<ListItem*>& items,
|
ListScene::ListItemSetType&& items,
|
||||||
QWidget* parent
|
QWidget* parent
|
||||||
)
|
)
|
||||||
: QGraphicsView(parent)
|
: QGraphicsView(parent)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace Bloom::Widgets
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
ListView(
|
ListView(
|
||||||
const std::vector<ListItem*>& items,
|
ListScene::ListItemSetType&& items,
|
||||||
QWidget* parent
|
QWidget* parent
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -173,7 +173,6 @@ namespace Bloom::Widgets
|
|||||||
this->addSnapshot(std::move(snapshot));
|
this->addSnapshot(std::move(snapshot));
|
||||||
}
|
}
|
||||||
|
|
||||||
this->snapshotListScene->sortItems();
|
|
||||||
this->snapshotListScene->refreshGeometry();
|
this->snapshotListScene->refreshGeometry();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -218,7 +217,6 @@ namespace Bloom::Widgets
|
|||||||
this,
|
this,
|
||||||
[this] (MemorySnapshot snapshot) {
|
[this] (MemorySnapshot snapshot) {
|
||||||
this->addSnapshot(std::move(snapshot));
|
this->addSnapshot(std::move(snapshot));
|
||||||
this->snapshotListScene->sortItems();
|
|
||||||
this->snapshotListScene->refreshGeometry();
|
this->snapshotListScene->refreshGeometry();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ namespace Bloom::Widgets
|
|||||||
}
|
}
|
||||||
|
|
||||||
this->registerListView = new ListView(
|
this->registerListView = new ListView(
|
||||||
std::vector<ListItem*>(this->registerGroupItems.begin(), this->registerGroupItems.end()),
|
ListScene::ListItemSetType(this->registerGroupItems.begin(), this->registerGroupItems.end()),
|
||||||
this
|
this
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user