From 263678a6d295d492351698db50a57c9db3bfe8ae Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Sat, 6 Oct 2018 19:55:55 +0200 Subject: switched to custom style model --- src/data/nodestylelist.cpp | 114 ---------------------------------------- src/data/nodestylelist.h | 40 -------------- src/data/stylelist.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++ src/data/stylelist.h | 41 +++++++++++++++ src/data/tikzstyles.cpp | 55 +++++++++++--------- src/data/tikzstyles.h | 9 ++-- 6 files changed, 204 insertions(+), 182 deletions(-) delete mode 100644 src/data/nodestylelist.cpp delete mode 100644 src/data/nodestylelist.h create mode 100644 src/data/stylelist.cpp create mode 100644 src/data/stylelist.h (limited to 'src/data') diff --git a/src/data/nodestylelist.cpp b/src/data/nodestylelist.cpp deleted file mode 100644 index 7f17ff0..0000000 --- a/src/data/nodestylelist.cpp +++ /dev/null @@ -1,114 +0,0 @@ -#include "nodestylelist.h" - -#include - -NodeStyleList::NodeStyleList(QObject *parent) : QAbstractListModel(parent) -{ -} - -Style *NodeStyleList::style(QString name) -{ - foreach (Style *s, _styles) - if (s->name() == name) return s; - return nullptr; -} - -Style *NodeStyleList::style(int i) -{ - return _styles[i]; -} - -int NodeStyleList::length() const -{ - return _styles.length(); -} - -void NodeStyleList::addStyle(Style *s) -{ - s->setParent(this); - if (s->category() == _category) { - int n = numInCategory(); - beginInsertRows(QModelIndex(), n, n); - _styles << s; - endInsertRows(); - } else { - _styles << s; - } -} - -void NodeStyleList::clear() -{ - int n = numInCategory(); - if (n > 0) { - beginRemoveRows(QModelIndex(), 0, n - 1); - _styles.clear(); - endRemoveRows(); - } else { - _styles.clear(); - } - - _category = ""; -} - -QString NodeStyleList::tikz() -{ - QString str; - QTextStream code(&str); - foreach (Style *s, _styles) code << s->tikz() << "\n"; - code.flush(); - return str; -} - -int NodeStyleList::numInCategory() const -{ - int c = 0; - foreach (Style *s, _styles) { - if (_category == "" || s->category() == _category) { - ++c; - } - } - return c; -} - -int NodeStyleList::nthInCategory(int n) const -{ - int c = 0; - for (int j = 0; j < _styles.length(); ++j) { - if (_category == "" || _styles[j]->category() == _category) { - if (c == n) return j; - else ++c; - } - } - return -1; -} - -Style *NodeStyleList::styleInCategory(int n) const -{ - return _styles[nthInCategory(n)]; -} - -QVariant NodeStyleList::data(const QModelIndex &index, int role) const -{ - if (role == Qt::DisplayRole) { - return QVariant(styleInCategory(index.row())->name()); - } else if (role == Qt::DecorationRole) { - return QVariant(styleInCategory(index.row())->icon()); - } else { - return QVariant(); - } -} - -int NodeStyleList::rowCount(const QModelIndex &/*parent*/) const -{ - return numInCategory(); -} - -QString NodeStyleList::category() const -{ - return _category; -} - -void NodeStyleList::setCategory(const QString &category) -{ - _category = category; -} diff --git a/src/data/nodestylelist.h b/src/data/nodestylelist.h deleted file mode 100644 index 5a53721..0000000 --- a/src/data/nodestylelist.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef NODESTYLELIST_H -#define NODESTYLELIST_H - -#include "style.h" - -#include - -class NodeStyleList : public QAbstractListModel -{ - Q_OBJECT -public: - explicit NodeStyleList(QObject *parent = nullptr); - Style *style(QString name); - Style *style(int i); - int length() const; - void addStyle(Style *s); - void clear(); - QString tikz(); - - int numInCategory() const; - int nthInCategory(int n) const; - Style *styleInCategory(int n) const; - - QVariant data(const QModelIndex &index, int role) const override; - int rowCount(const QModelIndex &/*parent*/) const override; - - - QString category() const; - void setCategory(const QString &category); - -signals: - -public slots: - -private: - QVector _styles; - QString _category; -}; - -#endif // NODESTYLELIST_H diff --git a/src/data/stylelist.cpp b/src/data/stylelist.cpp new file mode 100644 index 0000000..2c79d10 --- /dev/null +++ b/src/data/stylelist.cpp @@ -0,0 +1,127 @@ +#include "stylelist.h" + +#include + +StyleList::StyleList(bool edgeStyles, QObject *parent) : QAbstractListModel(parent), _edgeStyles(edgeStyles) +{ + if (edgeStyles) { + _styles << noneEdgeStyle; + } else { + _styles << noneStyle; + } +} + +Style *StyleList::style(QString name) +{ + foreach (Style *s, _styles) + if (s->name() == name) return s; + return nullptr; +} + +Style *StyleList::style(int i) +{ + return _styles[i]; +} + +int StyleList::length() const +{ + return _styles.length(); +} + +void StyleList::addStyle(Style *s) +{ + s->setParent(this); + if (s->category() == _category) { + int n = numInCategory(); + beginInsertRows(QModelIndex(), n, n); + _styles << s; + endInsertRows(); + } else { + _styles << s; + } +} + +void StyleList::clear() +{ + int n = numInCategory(); + if (n > 0) { + beginRemoveRows(QModelIndex(), 1, n - 1); + _styles.clear(); + if (_edgeStyles) _styles << noneEdgeStyle; + else _styles << noneStyle; + endRemoveRows(); + } else { + _styles.clear(); + if (_edgeStyles) _styles << noneEdgeStyle; + else _styles << noneStyle; + } + + _category = ""; +} + +QString StyleList::tikz() +{ + QString str; + QTextStream code(&str); + foreach (Style *s, _styles) code << s->tikz() << "\n"; + code.flush(); + return str; +} + +int StyleList::numInCategory() const +{ + int c = 0; + foreach (Style *s, _styles) { + if (_category == "" || s->isNone() || s->category() == _category) { + ++c; + } + } + return c; +} + +int StyleList::nthInCategory(int n) const +{ + int c = 0; + for (int j = 0; j < _styles.length(); ++j) { + if (_category == "" || _styles[j]->isNone() || _styles[j]->category() == _category) { + if (c == n) return j; + else ++c; + } + } + return -1; +} + +Style *StyleList::styleInCategory(int n) const +{ + return _styles[nthInCategory(n)]; +} + +QVariant StyleList::data(const QModelIndex &index, int role) const +{ + if (role == Qt::DisplayRole) { + return QVariant(styleInCategory(index.row())->name()); + } else if (role == Qt::DecorationRole) { + return QVariant(styleInCategory(index.row())->icon()); + } else { + return QVariant(); + } +} + +int StyleList::rowCount(const QModelIndex &/*parent*/) const +{ + return numInCategory(); +} + +QString StyleList::category() const +{ + return _category; +} + +void StyleList::setCategory(const QString &category) +{ + if (category != _category) { + beginResetModel(); + _category = category; + endResetModel(); + } +} diff --git a/src/data/stylelist.h b/src/data/stylelist.h new file mode 100644 index 0000000..f698761 --- /dev/null +++ b/src/data/stylelist.h @@ -0,0 +1,41 @@ +#ifndef NODESTYLELIST_H +#define NODESTYLELIST_H + +#include "style.h" + +#include + +class StyleList : public QAbstractListModel +{ + Q_OBJECT +public: + explicit StyleList(bool edgeStyles = false, QObject *parent = nullptr); + Style *style(QString name); + Style *style(int i); + int length() const; + void addStyle(Style *s); + void clear(); + QString tikz(); + + int numInCategory() const; + int nthInCategory(int n) const; + Style *styleInCategory(int n) const; + + QVariant data(const QModelIndex &index, int role) const override; + int rowCount(const QModelIndex &/*parent*/) const override; + + + QString category() const; + void setCategory(const QString &category); + +signals: + +public slots: + +private: + QVector _styles; + QString _category; + bool _edgeStyles; +}; + +#endif // NODESTYLELIST_H diff --git a/src/data/tikzstyles.cpp b/src/data/tikzstyles.cpp index 0645a72..522b3f5 100644 --- a/src/data/tikzstyles.cpp +++ b/src/data/tikzstyles.cpp @@ -26,7 +26,8 @@ TikzStyles::TikzStyles(QObject *parent) : QObject(parent) { - _nodeStyles = new NodeStyleList(this); + _nodeStyles = new StyleList(false, this); + _edgeStyles = new StyleList(true, this); } Style *TikzStyles::nodeStyle(QString name) const @@ -38,16 +39,15 @@ Style *TikzStyles::nodeStyle(QString name) const Style *TikzStyles::edgeStyle(QString name) const { - foreach (Style *s , _edgeStyles) - if (s->name() == name) return s; - return noneEdgeStyle; + Style *s = _edgeStyles->style(name); + return (s == nullptr) ? noneEdgeStyle : s; } void TikzStyles::clear() { _nodeStyles->clear(); - _edgeStyles.clear(); + _edgeStyles->clear(); } bool TikzStyles::loadStyles(QString fileName) @@ -98,14 +98,14 @@ void TikzStyles::refreshModels(QStandardItemModel *nodeModel, QStandardItemModel it->setSizeHint(QSize(48,48)); } - Style *ns; + Style *s; for (int i = 0; i < _nodeStyles->length(); ++i) { - ns = _nodeStyles->style(i); - if (category == "" || category == ns->propertyWithDefault("tikzit category", "", false)) + s = _nodeStyles->style(i); + if (category == "" || category == s->propertyWithDefault("tikzit category", "", false)) { - it = new QStandardItem(ns->icon(), ns->name()); + it = new QStandardItem(s->icon(), s->name()); it->setEditable(false); - it->setData(ns->name()); + it->setData(s->name()); it->setSizeHint(QSize(48,48)); nodeModel->appendRow(it); } @@ -118,17 +118,26 @@ void TikzStyles::refreshModels(QStandardItemModel *nodeModel, QStandardItemModel edgeModel->appendRow(it); } - foreach(Style *es, _edgeStyles) { - //if (category == "" || category == es->propertyWithDefault("tikzit category", "", false)) - //{ - it = new QStandardItem(es->icon(), es->name()); - it->setEditable(false); - it->setData(es->name()); - edgeModel->appendRow(it); - //} + for (int i = 0; i < _edgeStyles->length(); ++i) { + s = _edgeStyles->style(i); + it = new QStandardItem(s->icon(), s->name()); + it->setEditable(false); + it->setData(s->name()); + it->setSizeHint(QSize(48,48)); + edgeModel->appendRow(it); } } +StyleList *TikzStyles::nodeStyles() const +{ + return _nodeStyles; +} + +StyleList *TikzStyles::edgeStyles() const +{ + return _edgeStyles; +} + QStringList TikzStyles::categories() const { QMap cats; // use a QMap to keep keys sorted @@ -156,7 +165,7 @@ QString TikzStyles::tikz() const code << _nodeStyles->tikz(); code << "\n% Edge styles\n"; - foreach (Style *s, _edgeStyles) code << s->tikz() << "\n"; + code << _edgeStyles->tikz(); code.flush(); return str; @@ -165,12 +174,8 @@ QString TikzStyles::tikz() const void TikzStyles::addStyle(QString name, GraphElementData *data) { Style *s = new Style(name, data); - if (s->isEdgeStyle()) - { // edge style - _edgeStyles << s; - } else { // node style - _nodeStyles->addStyle(new Style(name, data)); - } + if (s->isEdgeStyle()) _edgeStyles->addStyle(s); + else _nodeStyles->addStyle(s); } diff --git a/src/data/tikzstyles.h b/src/data/tikzstyles.h index fbb12ff..5f372ab 100644 --- a/src/data/tikzstyles.h +++ b/src/data/tikzstyles.h @@ -20,7 +20,7 @@ #define PROJECT_H #include "graphelementdata.h" -#include "nodestylelist.h" +#include "stylelist.h" #include "style.h" #include @@ -48,13 +48,16 @@ public: QString category="", bool includeNone=true); + StyleList *nodeStyles() const; + StyleList *edgeStyles() const; + signals: public slots: private: - NodeStyleList *_nodeStyles; - QVector _edgeStyles; + StyleList *_nodeStyles; + StyleList* _edgeStyles; QStringList _colNames; QVector _cols; }; -- cgit v1.2.3