Skip to content

Commit

Permalink
Allow for saving the story tree state (#36)
Browse files Browse the repository at this point in the history
* Rename word count variable and add expanded status variable
* Save and restore expanded state of items
  • Loading branch information
vkbo authored Feb 12, 2022
1 parent 905aa0d commit b3f5970
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 21 deletions.
3 changes: 3 additions & 0 deletions sample/project/story.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"u:type": "ROOT",
"x:items": [
{
"m:expanded": true,
"m:handle": "e709ba3f-3141-4b4b-95df-4a8d3e91a8ba",
"m:order": 0,
"m:words": 1234,
Expand All @@ -16,6 +17,7 @@
"u:type": "PAGE"
},
{
"m:expanded": true,
"m:handle": "af3dc4a4-5f66-462d-b63b-9c4d7a7dec77",
"m:order": 1,
"m:words": 1234,
Expand All @@ -39,6 +41,7 @@
]
},
{
"m:expanded": true,
"m:handle": "61fc6238-87cf-4b78-b2c7-a430776a2b7a",
"m:order": 2,
"m:words": 1234,
Expand Down
29 changes: 29 additions & 0 deletions src/gui/storytree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ GuiStoryTree::GuiStoryTree(QWidget *parent)
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(doOpenContextMenu(QPoint)));
connect(this, SIGNAL(expanded(const QModelIndex&)),
this, SLOT(saveExpanded(const QModelIndex&)));
connect(this, SIGNAL(collapsed(const QModelIndex&)),
this, SLOT(saveCollapsed(const QModelIndex&)));
}

/**
Expand All @@ -67,8 +71,18 @@ GuiStoryTree::GuiStoryTree(QWidget *parent)
*/

void GuiStoryTree::setTreeModel(StoryModel *model) {

m_model = model;
this->setModel(m_model);

// Restore Expanded State
QModelIndexList allChildren;
this->getAllChildren(m_model->index(0, 0), allChildren);
this->blockSignals(true);
for (const QModelIndex &child : allChildren) {
this->setExpanded(child, m_model->isExpanded(child));
}
this->blockSignals(false);
}

/**
Expand All @@ -85,6 +99,13 @@ QModelIndex GuiStoryTree::firstSelectedIndex() {
}
}

void GuiStoryTree::getAllChildren(const QModelIndex &index, QModelIndexList &children) {
children.append(index);
for (int i = 0; i < model()->rowCount(index); ++i) {
getAllChildren(model()->index(i, 0, index), children);
}
}

/**
* Class Private Slots
* ===================
Expand Down Expand Up @@ -247,4 +268,12 @@ void GuiStoryTree::doAddChild(StoryItem *item, StoryItem::ItemType type, StoryMo
}
}

void GuiStoryTree::saveExpanded(const QModelIndex &index) {
if (m_model) m_model->setExpanded(index, true);
}

void GuiStoryTree::saveCollapsed(const QModelIndex &index) {
if (m_model) m_model->setExpanded(index, false);
}

} // namespace Collett
3 changes: 3 additions & 0 deletions src/gui/storytree.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ class GuiStoryTree : public QTreeView
// Class Getters

QModelIndex firstSelectedIndex();
void getAllChildren(const QModelIndex &index, QModelIndexList &children);

public slots:
void doEditName(bool checked);

private slots:
void doOpenContextMenu(const QPoint &pos);
void doAddChild(StoryItem *item, StoryItem::ItemType type, StoryModel::AddLocation loc);
void saveExpanded(const QModelIndex &index);
void saveCollapsed(const QModelIndex &index);

private:
StoryModel *m_model = nullptr;
Expand Down
77 changes: 57 additions & 20 deletions src/project/storyitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ StoryItem::StoryItem(const QUuid &uuid, const QString &name, ItemType type, Stor
: m_parentItem(parent)
{
m_childItems = QVector<StoryItem*>{};
m_handle = uuid;
m_name = name;
m_type = type;
m_wCount = 0;
m_handle = uuid;
m_name = name;
m_type = type;
m_words = 0;
m_expanded = false;
}

StoryItem::~StoryItem() {
Expand Down Expand Up @@ -101,10 +102,11 @@ StoryItem *StoryItem::addChild(const QJsonObject &json) {
return nullptr;
}

QUuid handle = QUuid();
QString name = "";
ItemType type = StoryItem::Invalid;
int wCount = 0;
QUuid handle = QUuid();
QString name = "";
ItemType type = StoryItem::Invalid;
int words = 0;
bool expanded = false;

if (json.contains(QLatin1String("m:handle"))) {
handle = QUuid(json[QLatin1String("m:handle")].toString());
Expand All @@ -116,7 +118,10 @@ StoryItem *StoryItem::addChild(const QJsonObject &json) {
type = StoryItem::typeFromString(json[QLatin1String("u:type")].toString());
}
if (json.contains(QLatin1String("m:words"))) {
wCount = json[QLatin1String("m:words")].toInt();
words = json[QLatin1String("m:words")].toInt();
}
if (json.contains(QLatin1String("m:expanded"))) {
expanded = json[QLatin1String("m:expanded")].toBool();
}

if (type == StoryItem::Invalid) {
Expand Down Expand Up @@ -144,7 +149,8 @@ StoryItem *StoryItem::addChild(const QJsonObject &json) {
}

StoryItem *item = new StoryItem(handle, name, type, this);
item->setWordCount(wCount);
item->setWordCount(words);
item->setExpanded(expanded);
m_childItems.append(item);

if (json.contains(QLatin1String("x:items"))) {
Expand Down Expand Up @@ -180,15 +186,35 @@ QJsonObject StoryItem::toJsonObject() {
}

QLatin1String type;
bool expandable = false;
switch (m_type) {
case StoryItem::Root: type = QLatin1String("ROOT"); break;
case StoryItem::Book: type = QLatin1String("BOOK"); break;
case StoryItem::Partition: type = QLatin1String("PARTITION"); break;
case StoryItem::Chapter: type = QLatin1String("CHAPTER"); break;
case StoryItem::Scene: type = QLatin1String("SCENE"); break;
case StoryItem::Page: type = QLatin1String("PAGE"); break;
case StoryItem::Root:
type = QLatin1String("ROOT");
expandable = false;
break;
case StoryItem::Book:
type = QLatin1String("BOOK");
expandable = true;
break;
case StoryItem::Partition:
type = QLatin1String("PARTITION");
expandable = true;
break;
case StoryItem::Chapter:
type = QLatin1String("CHAPTER");
expandable = true;
break;
case StoryItem::Scene:
type = QLatin1String("SCENE");
expandable = false;
break;
case StoryItem::Page:
type = QLatin1String("PAGE");
expandable = false;
break;
case StoryItem::Invalid:
return QJsonObject();
expandable = false;
break;
}

Expand All @@ -198,12 +224,15 @@ QJsonObject StoryItem::toJsonObject() {
} else {
item[QLatin1String("m:handle")] = m_handle.toString(QUuid::WithoutBraces);
item[QLatin1String("m:order")] = row();
item[QLatin1String("m:words")] = m_wCount;
item[QLatin1String("m:words")] = m_words;
item[QLatin1String("u:name")] = m_name;
item[QLatin1String("u:type")] = type;
if (children.size() > 0) {
item[QLatin1String("x:items")] = children;
}
if (expandable) {
item[QLatin1String("m:expanded")] = m_expanded;
}
}

return item;
Expand Down Expand Up @@ -266,7 +295,11 @@ void StoryItem::setName(const QString &name) {


void StoryItem::setWordCount(int count) {
m_wCount = count > 0 ? count : 0;
m_words = count > 0 ? count : 0;
}

void StoryItem::setExpanded(bool state) {
m_expanded = state;
}

/**
Expand All @@ -287,7 +320,7 @@ QString StoryItem::name() const {
}

int StoryItem::wordCount() const {
return m_wCount;
return m_words;
}

int StoryItem::childWordCounts() const {
Expand All @@ -298,6 +331,10 @@ int StoryItem::childWordCounts() const {
return tCount;
}

bool StoryItem::isExpanded() const {
return m_expanded;
}

/**
* Static Methods
* ==============
Expand Down Expand Up @@ -372,7 +409,7 @@ int StoryItem::row() const {

QVariant StoryItem::data() const {
QVariantList itemData;
itemData << m_name << m_wCount << typeToString(m_type);
itemData << m_name << m_words << typeToString(m_type);
return QVariant::fromValue(itemData);
}

Expand Down
5 changes: 4 additions & 1 deletion src/project/storyitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class StoryItem : public QObject

void setName(const QString &name);
void setWordCount(int count);
void setExpanded(bool state);

// Class Getters

Expand All @@ -60,6 +61,7 @@ class StoryItem : public QObject
QString name() const;
int wordCount() const;
int childWordCounts() const;
bool isExpanded() const;

// Static Methods

Expand All @@ -83,7 +85,8 @@ class StoryItem : public QObject
QUuid m_handle;
QString m_name;
ItemType m_type;
int m_wCount;
int m_words;
bool m_expanded;

};
} // namespace Collett
Expand Down
17 changes: 17 additions & 0 deletions src/project/storymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QJsonObject>
#include <QModelIndex>
#include <QLatin1String>
#include <QModelIndexList>
#include <QAbstractItemModel>

namespace Collett {
Expand Down Expand Up @@ -201,6 +202,15 @@ QString StoryModel::itemName(const QModelIndex &index) {
}
}

bool StoryModel::isExpanded(const QModelIndex &index) {
if (index.isValid()) {
StoryItem *item = static_cast<StoryItem*>(index.internalPointer());
return item->isExpanded();
} else {
return false;
}
}

/**
* Model Edit
* ==========
Expand All @@ -213,6 +223,13 @@ void StoryModel::setItemName(const QModelIndex &index, const QString &name) {
}
}

void StoryModel::setExpanded(const QModelIndex &index, bool state) {
if (index.isValid()) {
StoryItem *item = static_cast<StoryItem*>(index.internalPointer());
item->setExpanded(state);
}
}

/**
* Model Access
* ============
Expand Down
3 changes: 3 additions & 0 deletions src/project/storymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QString>
#include <QJsonObject>
#include <QModelIndex>
#include <QModelIndexList>
#include <QAbstractItemModel>

namespace Collett {
Expand Down Expand Up @@ -55,10 +56,12 @@ class StoryModel : public QAbstractItemModel
StoryItem *storyItem(const QModelIndex &index);
QUuid itemHandle(const QModelIndex &index);
QString itemName(const QModelIndex &index);
bool isExpanded(const QModelIndex &index);

// Model Edit

void setItemName(const QModelIndex &index, const QString &name);
void setExpanded(const QModelIndex &index, bool state);

// Model Access

Expand Down

0 comments on commit b3f5970

Please sign in to comment.