Skip to content

Commit

Permalink
Add extra abstraction for "ghost" root item in treeview
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Friedel committed Dec 5, 2023
1 parent 7c19b0c commit 1913c34
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
3 changes: 1 addition & 2 deletions tools/fm-editor/FeatureModelEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ void FeatureModelEditor::loadFeatureFromSelection(
if (Index.isValid()) {
auto *Item = static_cast<FeatureTreeItem *>(Index.internalPointer())
->child(Index.row());
if (Item->getKind() ==
vara::feature::FeatureTreeNode::NodeKind::NK_FEATURE) {
if (Item->getKind() == ItemKind::IK_Feature) {
loadFeature(dynamic_cast<FeatureTreeItemFeature *>(Item)->getFeature());
}
}
Expand Down
4 changes: 1 addition & 3 deletions tools/fm-editor/tree/FeatureTreeItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ FeatureTreeItem::createFeatureTreeItem(vara::feature::FeatureTreeNode *Item) {
}

void FeatureTreeItem::addChild(FeatureTreeItem *Child) {
if (!Children.empty() &&
Children[0]->getKind() ==
vara::feature::FeatureTreeNode::NodeKind::NK_RELATIONSHIP) {
if (!Children.empty() && Children[0]->getKind() == ItemKind::IK_Relation) {
Children[0]->addChild(Child);
} else {
Children.push_back(Child);
Expand Down
56 changes: 37 additions & 19 deletions tools/fm-editor/tree/FeatureTreeItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

#include <vector>

enum ItemKind {
IK_Feature,
IK_Relation,
IK_Root

};

class FeatureTreeItem : public QObject {
Q_OBJECT

Expand Down Expand Up @@ -48,10 +55,10 @@ class FeatureTreeItem : public QObject {
[[nodiscard]] virtual QVariant data(int Column) const = 0;
std::unique_ptr<FeatureTreeItem> static createFeatureTreeItem(
vara::feature::FeatureTreeNode *Item);
bool booleanColumn(int Column) { return false; }
bool booleanColumn(int Column) { return false; };
virtual void contextMenu(QPoint Pos) = 0;
virtual vara::feature::FeatureTreeNode *getItem() const = 0;
vara::feature::FeatureTreeNode::NodeKind getKind() { return Kind; }
virtual ItemKind getKind() = 0;
virtual string getName() { return ""; };
void setParent(FeatureTreeItem *ParentItem) { this->Parent = ParentItem; }

Expand All @@ -61,23 +68,33 @@ class FeatureTreeItem : public QObject {
void removeFeature(bool Recursive, vara::feature::Feature *Feature);

protected:
FeatureTreeItem(vara::feature::FeatureTreeNode::NodeKind Kind) : Kind(Kind) {}
FeatureTreeItem() = default;

FeatureTreeItem *Parent = nullptr;

std::vector<FeatureTreeItem *> Children = {};
};

private:
const vara::feature::FeatureTreeNode::NodeKind Kind;
class FeatureTreeItemRoot : public FeatureTreeItem {
Q_OBJECT

public:
FeatureTreeItemRoot(){};
[[nodiscard]] int columnCount() const override { return 5; };
[[nodiscard]] QVariant data(int Column) const override { return {}; };
void contextMenu(QPoint Pos) override{};
[[nodiscard]] vara::feature::FeatureTreeNode *getItem() const override {
return nullptr;
};
ItemKind getKind() override { return IK_Root; };
};

class FeatureTreeItemFeature : public FeatureTreeItem {
Q_OBJECT

public:
FeatureTreeItemFeature(vara::feature::Feature *Item)
: FeatureTreeItem(vara::feature::FeatureTreeNode::NodeKind::NK_FEATURE),
Item(Item) {
: FeatureTreeItem(), Item(Item) {
ContextMenu = std::make_unique<QMenu>();
ContextMenu->addAction("Inspect Sources", this,
&FeatureTreeItemFeature::inspect);
Expand All @@ -87,16 +104,20 @@ class FeatureTreeItemFeature : public FeatureTreeItem {
connect(RemoveAction.get(), &QAction::triggered, this,
&FeatureTreeItemFeature::remove, Qt::QueuedConnection);
}
virtual ~FeatureTreeItemFeature() = default;
~FeatureTreeItemFeature() override = default;

[[nodiscard]] QVariant data(int Column) const override;
[[nodiscard]] int columnCount() const override { return 5; }
bool booleanColumn(int Column) { return Column == 1; }
void contextMenu(QPoint Pos) override;
vara::feature::FeatureTreeNode *getItem() const override { return Item; }
const vara::feature::Feature *getFeature() const { return Item; }
[[nodiscard]] vara::feature::FeatureTreeNode *getItem() const override {
return Item;
}
[[nodiscard]] const vara::feature::Feature *getFeature() const {
return Item;
}
string getName() override { return Item->getName().str(); }

ItemKind getKind() override { return IK_Feature; }
public slots:
void inspect();
void addChild();
Expand All @@ -110,10 +131,7 @@ public slots:

class FeatureTreeItemRelation : public FeatureTreeItem {
public:
FeatureTreeItemRelation(vara::feature::Relationship *Item)
: FeatureTreeItem(
vara::feature::FeatureTreeNode::NodeKind::NK_RELATIONSHIP),
Item(Item) {}
FeatureTreeItemRelation(vara::feature::Relationship *Item) : Item(Item){};
~FeatureTreeItemRelation() override = default;

[[nodiscard]] QVariant data(int Column) const override {
Expand All @@ -124,13 +142,13 @@ class FeatureTreeItemRelation : public FeatureTreeItem {
}
[[nodiscard]] int columnCount() const override { return 1; }
void contextMenu(QPoint Pos) override {}
vara::feature::FeatureTreeNode *getItem() const override { return Item; }
[[nodiscard]] vara::feature::FeatureTreeNode *getItem() const override {
return Item;
}
ItemKind getKind() override { return IK_Relation; }

private:
vara::feature::Relationship *Item;
static const vara::feature::FeatureTreeNode::NodeKind Kind =
vara::feature::FeatureTreeNode::NodeKind::NK_RELATIONSHIP;

[[nodiscard]] std::string relationType() const {
std::string Type;
switch (Item->getKind()) {
Expand Down
2 changes: 1 addition & 1 deletion tools/fm-editor/tree/FeatureTreeViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FeatureTreeViewModel : public QAbstractItemModel {
FeatureTreeViewModel(vara::feature::FeatureModel *Model, QObject *Parent)
: QAbstractItemModel(Parent) {
auto UniqueRoot = FeatureTreeItem::createFeatureTreeItem(Model->getRoot());
RootItem = new FeatureTreeItemFeature(nullptr);
RootItem = new FeatureTreeItemRoot();
RootItem->addChild(UniqueRoot.get());
auto RawRoot = UniqueRoot.get();
Items.push_back(std::move(UniqueRoot));
Expand Down

0 comments on commit 1913c34

Please sign in to comment.