From 6baa7366f20eabac32c3d84ae9832e517f5e0400 Mon Sep 17 00:00:00 2001 From: Arno Date: Fri, 20 Jul 2018 14:34:45 +0200 Subject: More code churn Create separate files for MappingData and MappingTreeResultModel. Hopefully no functional changes. --- mappingdata.cpp | 39 +++++++++ mappingdata.h | 32 +++++++ mappingeditwidget.cpp | 2 + mappingtreemodel.cpp | 203 +-------------------------------------------- mappingtreemodel.h | 46 ---------- mappingtreeresultmodel.cpp | 182 ++++++++++++++++++++++++++++++++++++++++ mappingtreeresultmodel.h | 42 ++++++++++ mappingtreewidget.cpp | 1 + mappingtreewidget.h | 5 -- newpicsdialog.cpp | 1 + picfilesmodel.cpp | 2 +- pictureswidget.cpp | 2 + pictureviewer2.cpp | 1 + shemov.pro | 8 +- 14 files changed, 310 insertions(+), 256 deletions(-) create mode 100644 mappingdata.cpp create mode 100644 mappingdata.h create mode 100644 mappingtreeresultmodel.cpp create mode 100644 mappingtreeresultmodel.h diff --git a/mappingdata.cpp b/mappingdata.cpp new file mode 100644 index 0000000..a279c96 --- /dev/null +++ b/mappingdata.cpp @@ -0,0 +1,39 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#include "mappingdata.h" + +MappingData::MappingData() : mappingId(-1), descId(-1) {} + +bool MappingData::operator ==(const MappingData &other){ + bool retval = ( + mappingId == other.mappingId && + descId == other.descId && + name == other.name && + parents == other.parents && + path == other.path + ); + return retval; +} + +QDataStream &operator <<(QDataStream &out, const MappingData &v){ + out << v.mappingId << v.descId << v.name << v.parents << v.path; + return out; +} + +QDataStream &operator >>(QDataStream &in, MappingData &v){ + in >> v.mappingId; + in >> v.descId; + in >> v.name; + in >> v.parents; + in >> v.path; + return in; +} + +bool MappingData::isValid(){ + return !(mappingId == -1 && descId == -1); +} diff --git a/mappingdata.h b/mappingdata.h new file mode 100644 index 0000000..f76932b --- /dev/null +++ b/mappingdata.h @@ -0,0 +1,32 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#ifndef MAPPINGDATA_H +#define MAPPINGDATA_H + +#include +#include +#include + +struct MappingData { + MappingData(); + bool operator==(const MappingData &other); + //QDataStream &operator<<(QDataStream &out, const MappingData &d); + bool isValid(); + int mappingId; + int descId; + QString name; + QList parents; + QStringList path; +}; + +QDataStream &operator<<(QDataStream &out, const MappingData &d); +QDataStream &operator>>(QDataStream &in, MappingData &v); + +Q_DECLARE_METATYPE(MappingData) + +#endif // MAPPINGDATA_H diff --git a/mappingeditwidget.cpp b/mappingeditwidget.cpp index 9ae13d5..936c5f1 100644 --- a/mappingeditwidget.cpp +++ b/mappingeditwidget.cpp @@ -13,8 +13,10 @@ #include "mappingeditwidget.h" #include "mappingtreewidget.h" #include "mappingtreeresultview.h" +#include "mappingtreeresultmodel.h" #include "mappingtreeview.h" #include "mappingtreeproxy.h" +#include "mappingdata.h" MappingEditWidget::MappingEditWidget(QWidget *parent) : QWidget(parent){ //the views diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 58fed18..2790ef7 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -10,6 +10,7 @@ #include #include "mappingtreemodel.h" +#include "mappingdata.h" #include "smtreeitem.h" #include "smglobals.h" @@ -376,205 +377,3 @@ int MappingTreeModel::lowerBound(SmTreeItem *item, const QVariant &value, int co return item->childCount(); } -MappingTreeResultModel::MappingTreeResultModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent) { - mSourceModel = static_cast(SmGlobals::instance()->model("MappingTree")); -} - -Qt::ItemFlags MappingTreeResultModel::flags(const QModelIndex &index) const { - Q_UNUSED(index); - return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); -} - -QVariant MappingTreeResultModel::data(const QModelIndex &index, int role) const{ - if(!index.isValid()){ - return QVariant(); - } - SmTreeItem *item = itemAt(index); - if(role == NameRole){ - return item->data(Name); - } - if(role == MappingIdRole){ - return item->data(MappingId); - } - if(role == DescIdRole){ - return item->data(DescId); - } - if(role == ParentIdRole){ - return item->data(ParentId); - } - return SmTreeModel::data(index, role); -} - -bool MappingTreeResultModel::setData(const QModelIndex &index, const QVariant &value, int role){ - SmTreeItem *item = itemAt(index); - if(role == NameRole){ - item->setData(Name, value); - return true; - } - if(role == MappingIdRole){ - item->setData(MappingId, value); - return true; - } - if(role == ParentIdRole){ - item->setData(ParentId, value); - return true; - } - if(role == DescIdRole){ - item->setData(DescId, value); - return true; - } - return SmTreeModel::setData(index, value, role); -} - -void MappingTreeResultModel::addItem(const MappingData &data){ - QList pPath = data.parents; - - if(!mCurrentData.contains(data)){ - mCurrentData << data; - } - - QList curPath; - std::reverse(pPath.begin(), pPath.end()); - - MappingTreeModel *mapModel = qobject_cast(SmGlobals::instance()->model("MappingTree")); - QModelIndex curIdx = QModelIndex(); - int curId = -1; - beginResetModel(); - while(!pPath.isEmpty()){ - curId = pPath.last(); - curPath << curId; - pPath.removeLast(); - QModelIndex lastIdx = curIdx; - curIdx = find(curId, MappingId, curIdx); - if(!curIdx.isValid()){ - QModelIndex sourceIdx = mapModel->indexFromParentPath(curPath, true); - if(!sourceIdx.isValid()){ - return; //should never happen! - } - SmTreeItem *pItem; - if(!lastIdx.isValid()){ - pItem = root(); - }else{ - pItem = static_cast(lastIdx.internalPointer()); - } - int row = pItem->childCount(); - const QString curName = sourceIdx.data(MappingTreeModel::NameRole).toString(); - for(int i = 0; i < pItem->childCount(); ++i){ - if(pItem->child(i)->data(Name).toString() > curName){ - row = i; - } - } - QVariantList data; - data << curName << sourceIdx.data(MappingTreeModel::MappingIdRole) << sourceIdx.data(MappingTreeModel::MappingParentIdRole) << sourceIdx.data(MappingTreeModel::DescIdRole); - SmTreeItem *newItem = new SmTreeItem(data, pItem); - pItem->insertChild(row, newItem); - curIdx = createIndex(row, 0, newItem); - } - } - endResetModel(); -} - -void MappingTreeResultModel::removeItem(const QModelIndex &idx){ - if(!idx.isValid()){ - return; - } - SmTreeItem *curItem = static_cast(idx.internalPointer()); - MappingData rmData = mSourceModel->mappingDataFromItem(curItem); - beginResetModel(); - int row = curItem->row(); - SmTreeItem *parent = curItem->parent(); - parent->removeChild(row); - int count = mCurrentData.removeAll(rmData); - int toRemove = -1; - if(count == 0){ - for(int i = 0; i < mCurrentData.count(); ++i){ - MappingData cur = mCurrentData.at(i); - if(cur.parents.contains(rmData.mappingId)){ - toRemove = i; - break; - } - } - } - if(toRemove > -1 && toRemove < mCurrentData.count()){ - mCurrentData.removeAt(toRemove); - } - endResetModel(); -} - -QList MappingTreeResultModel::getMappings(SmTreeItem *start) const{ - QList retval; - for(int i = 0; i < start->childCount(); ++i){ - SmTreeItem *childItem = start->child(i); - if(childItem->childCount()){ - retval.append(getMappings(childItem)); - }else{ - retval << childItem->data(MappingId); - } - } - return retval; -} - -QList MappingTreeResultModel::columnValues(int column) const { - return columnValuesRecursive(root(), column); -} - -void MappingTreeResultModel::clearData(){ - setRoot(new SmTreeItem(NumFields)); - mCurrentData.clear(); -} - -int MappingTreeResultModel::hasChild(SmTreeItem *item, const QVariant &name, int column) const{ - for(int i = 0; i < item->childCount(); ++i){ - if(item->child(i)->data(column) == name){ - return i; - } - } - return -1; -} - -QList MappingTreeResultModel::columnValuesRecursive(SmTreeItem *parent, int column) const { - QList retval; - if(!parent->childCount()){ - return retval; - } - for(int i = 0; i < parent->childCount(); ++i){ - SmTreeItem *child = parent->child(i); - QVariant value = child->data(column); - if(value.canConvert(QVariant::Int) && (value.toInt() != -1)){ - retval << value; - } - retval << columnValuesRecursive(child, column); - } - return retval; -} - -MappingData::MappingData() : mappingId(-1), descId(-1) {} - -bool MappingData::operator ==(const MappingData &other){ - bool retval = ( - mappingId == other.mappingId && - descId == other.descId && - name == other.name && - parents == other.parents && - path == other.path - ); - return retval; -} - -QDataStream &operator <<(QDataStream &out, const MappingData &v){ - out << v.mappingId << v.descId << v.name << v.parents << v.path; - return out; -} - -QDataStream &operator >>(QDataStream &in, MappingData &v){ - in >> v.mappingId; - in >> v.descId; - in >> v.name; - in >> v.parents; - in >> v.path; - return in; -} - -bool MappingData::isValid(){ - return !(mappingId == -1 && descId == -1); -} diff --git a/mappingtreemodel.h b/mappingtreemodel.h index 3f3b903..bf3d583 100644 --- a/mappingtreemodel.h +++ b/mappingtreemodel.h @@ -75,50 +75,4 @@ class MappingTreeModel : public SmTreeModel { QSqlError mLastError; }; -class MappingTreeResultModel : public SmTreeModel { - Q_OBJECT - public: - enum Roles { NameRole = Qt::UserRole + 1, MappingIdRole = Qt::UserRole + 2, ParentIdRole = Qt::UserRole + 3, DescIdRole = Qt::UserRole + 4 }; - enum Fields { Name = 0, MappingId = 1, ParentId = 2, DescId = 3 }; - enum { NumFields = 4 }; - explicit MappingTreeResultModel(const QStringList &headers, QObject *parent = 0); - - //data + flags - Qt::ItemFlags flags(const QModelIndex &index) const; - virtual QVariant data(const QModelIndex &index, int role) const; - virtual bool setData(const QModelIndex &index, const QVariant &value, int role); - QList getMappings(SmTreeItem *start) const; - QList columnValues(int column) const; - const QList mappingData() const { return mCurrentData; } - void clearData(); - - public slots: - void addItem(const MappingData &data); - void removeItem(const QModelIndex &idx); - - private: - int hasChild(SmTreeItem *item, const QVariant &name, int column = 0) const; - MappingTreeModel *mSourceModel; - QList columnValuesRecursive(SmTreeItem *parent, int column) const; - QList mCurrentData; -}; - -Q_DECLARE_METATYPE(MappingData) - -struct MappingData { - MappingData(); - bool operator==(const MappingData &other); - //QDataStream &operator<<(QDataStream &out, const MappingData &d); - bool isValid(); - int mappingId; - int descId; - QString name; - QList parents; - QStringList path; -}; - -QDataStream &operator<<(QDataStream &out, const MappingData &d); -QDataStream &operator>>(QDataStream &in, MappingData &v); - - #endif // MAPPINGTREEMODEL_H diff --git a/mappingtreeresultmodel.cpp b/mappingtreeresultmodel.cpp new file mode 100644 index 0000000..e2ff75e --- /dev/null +++ b/mappingtreeresultmodel.cpp @@ -0,0 +1,182 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#include "mappingtreeresultmodel.h" +#include "smtreeitem.h" +#include "smglobals.h" + +MappingTreeResultModel::MappingTreeResultModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent) { + mSourceModel = static_cast(SmGlobals::instance()->model("MappingTree")); +} + +Qt::ItemFlags MappingTreeResultModel::flags(const QModelIndex &index) const { + Q_UNUSED(index); + return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); +} + +QVariant MappingTreeResultModel::data(const QModelIndex &index, int role) const{ + if(!index.isValid()){ + return QVariant(); + } + SmTreeItem *item = itemAt(index); + if(role == NameRole){ + return item->data(Name); + } + if(role == MappingIdRole){ + return item->data(MappingId); + } + if(role == DescIdRole){ + return item->data(DescId); + } + if(role == ParentIdRole){ + return item->data(ParentId); + } + return SmTreeModel::data(index, role); +} + +bool MappingTreeResultModel::setData(const QModelIndex &index, const QVariant &value, int role){ + SmTreeItem *item = itemAt(index); + if(role == NameRole){ + item->setData(Name, value); + return true; + } + if(role == MappingIdRole){ + item->setData(MappingId, value); + return true; + } + if(role == ParentIdRole){ + item->setData(ParentId, value); + return true; + } + if(role == DescIdRole){ + item->setData(DescId, value); + return true; + } + return SmTreeModel::setData(index, value, role); +} + +void MappingTreeResultModel::addItem(const MappingData &data){ + QList pPath = data.parents; + + if(!mCurrentData.contains(data)){ + mCurrentData << data; + } + + QList curPath; + std::reverse(pPath.begin(), pPath.end()); + + MappingTreeModel *mapModel = qobject_cast(SmGlobals::instance()->model("MappingTree")); + QModelIndex curIdx = QModelIndex(); + int curId = -1; + beginResetModel(); + while(!pPath.isEmpty()){ + curId = pPath.last(); + curPath << curId; + pPath.removeLast(); + QModelIndex lastIdx = curIdx; + curIdx = find(curId, MappingId, curIdx); + if(!curIdx.isValid()){ + QModelIndex sourceIdx = mapModel->indexFromParentPath(curPath, true); + if(!sourceIdx.isValid()){ + return; //should never happen! + } + SmTreeItem *pItem; + if(!lastIdx.isValid()){ + pItem = root(); + }else{ + pItem = static_cast(lastIdx.internalPointer()); + } + int row = pItem->childCount(); + const QString curName = sourceIdx.data(MappingTreeModel::NameRole).toString(); + for(int i = 0; i < pItem->childCount(); ++i){ + if(pItem->child(i)->data(Name).toString() > curName){ + row = i; + } + } + QVariantList data; + data << curName << sourceIdx.data(MappingTreeModel::MappingIdRole) << sourceIdx.data(MappingTreeModel::MappingParentIdRole) << sourceIdx.data(MappingTreeModel::DescIdRole); + SmTreeItem *newItem = new SmTreeItem(data, pItem); + pItem->insertChild(row, newItem); + curIdx = createIndex(row, 0, newItem); + } + } + endResetModel(); +} + +void MappingTreeResultModel::removeItem(const QModelIndex &idx){ + if(!idx.isValid()){ + return; + } + SmTreeItem *curItem = static_cast(idx.internalPointer()); + MappingData rmData = mSourceModel->mappingDataFromItem(curItem); + beginResetModel(); + int row = curItem->row(); + SmTreeItem *parent = curItem->parent(); + parent->removeChild(row); + int count = mCurrentData.removeAll(rmData); + int toRemove = -1; + if(count == 0){ + for(int i = 0; i < mCurrentData.count(); ++i){ + MappingData cur = mCurrentData.at(i); + if(cur.parents.contains(rmData.mappingId)){ + toRemove = i; + break; + } + } + } + if(toRemove > -1 && toRemove < mCurrentData.count()){ + mCurrentData.removeAt(toRemove); + } + endResetModel(); +} + +QList MappingTreeResultModel::getMappings(SmTreeItem *start) const{ + QList retval; + for(int i = 0; i < start->childCount(); ++i){ + SmTreeItem *childItem = start->child(i); + if(childItem->childCount()){ + retval.append(getMappings(childItem)); + }else{ + retval << childItem->data(MappingId); + } + } + return retval; +} + +QList MappingTreeResultModel::columnValues(int column) const { + return columnValuesRecursive(root(), column); +} + +void MappingTreeResultModel::clearData(){ + setRoot(new SmTreeItem(NumFields)); + mCurrentData.clear(); +} + +int MappingTreeResultModel::hasChild(SmTreeItem *item, const QVariant &name, int column) const{ + for(int i = 0; i < item->childCount(); ++i){ + if(item->child(i)->data(column) == name){ + return i; + } + } + return -1; +} + +QList MappingTreeResultModel::columnValuesRecursive(SmTreeItem *parent, int column) const { + QList retval; + if(!parent->childCount()){ + return retval; + } + for(int i = 0; i < parent->childCount(); ++i){ + SmTreeItem *child = parent->child(i); + QVariant value = child->data(column); + if(value.canConvert(QVariant::Int) && (value.toInt() != -1)){ + retval << value; + } + retval << columnValuesRecursive(child, column); + } + return retval; +} diff --git a/mappingtreeresultmodel.h b/mappingtreeresultmodel.h new file mode 100644 index 0000000..e86fc51 --- /dev/null +++ b/mappingtreeresultmodel.h @@ -0,0 +1,42 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#ifndef MAPPINGTREERESULTMODEL_H +#define MAPPINGTREERESULTMODEL_H + +#include "mappingtreemodel.h" +#include "mappingdata.h" + +class MappingTreeResultModel : public SmTreeModel { + Q_OBJECT + public: + enum Roles { NameRole = Qt::UserRole + 1, MappingIdRole = Qt::UserRole + 2, ParentIdRole = Qt::UserRole + 3, DescIdRole = Qt::UserRole + 4 }; + enum Fields { Name = 0, MappingId = 1, ParentId = 2, DescId = 3 }; + enum { NumFields = 4 }; + explicit MappingTreeResultModel(const QStringList &headers, QObject *parent = 0); + + //data + flags + Qt::ItemFlags flags(const QModelIndex &index) const; + virtual QVariant data(const QModelIndex &index, int role) const; + virtual bool setData(const QModelIndex &index, const QVariant &value, int role); + QList getMappings(SmTreeItem *start) const; + QList columnValues(int column) const; + const QList mappingData() const { return mCurrentData; } + void clearData(); + + public slots: + void addItem(const MappingData &data); + void removeItem(const QModelIndex &idx); + + private: + int hasChild(SmTreeItem *item, const QVariant &name, int column = 0) const; + MappingTreeModel *mSourceModel; + QList columnValuesRecursive(SmTreeItem *parent, int column) const; + QList mCurrentData; +}; + +#endif // MAPPINGTREERESULTMODEL_H diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp index 7a5675a..f22c6dd 100644 --- a/mappingtreewidget.cpp +++ b/mappingtreewidget.cpp @@ -28,6 +28,7 @@ #include "mappingtreeview.h" #include "mappingtreeproxy.h" #include "mappinginputdialog.h" +#include "mappingdata.h" #include "smglobals.h" MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){ diff --git a/mappingtreewidget.h b/mappingtreewidget.h index 99f7c17..7a7bfd0 100644 --- a/mappingtreewidget.h +++ b/mappingtreewidget.h @@ -21,8 +21,6 @@ class QStringListModel; class MappingTreeResultModel; class QLineEdit; class MappingTreeProxy; - -// defined in mappingtreemodel.h struct MappingData; class MappingTreeWidget : public QWidget { @@ -75,7 +73,4 @@ class MappingTreeWidget : public QWidget { QAction *mEditChildA; }; - - - #endif // MAPPINGTREEWIDGET_H diff --git a/newpicsdialog.cpp b/newpicsdialog.cpp index b972013..7120255 100644 --- a/newpicsdialog.cpp +++ b/newpicsdialog.cpp @@ -25,6 +25,7 @@ #include "helper.h" #include "mappingtreewidget.h" #include "mappingtreemodel.h" +#include "mappingtreeresultmodel.h" #include "mappingtreeview.h" #include "mappingeditwidget.h" #include "delegates.h" diff --git a/picfilesmodel.cpp b/picfilesmodel.cpp index f0d02dc..717812c 100644 --- a/picfilesmodel.cpp +++ b/picfilesmodel.cpp @@ -17,9 +17,9 @@ #include "picfilesmodel.h" #include "smglobals.h" #include "smtreeitem.h" +#include "mappingdata.h" #include "helper.h" - PicFilesModel::PicFilesModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent) { //conjure up model mMappingTreeModel = static_cast(SmGlobals::instance()->model("MappingTree")); diff --git a/pictureswidget.cpp b/pictureswidget.cpp index 5a90a71..4b1f9db 100644 --- a/pictureswidget.cpp +++ b/pictureswidget.cpp @@ -21,6 +21,8 @@ #include "mappingtreewidget.h" #include "mappingeditwidget.h" #include "mappingeditdialog.h" +#include "mappingtreeresultmodel.h" +#include "mappingdata.h" #include "delegates.h" #include "smglobals.h" #include "helper.h" diff --git a/pictureviewer2.cpp b/pictureviewer2.cpp index 778e091..5b30e1b 100644 --- a/pictureviewer2.cpp +++ b/pictureviewer2.cpp @@ -48,6 +48,7 @@ #include "pictureswidget.h" #include "smglobals.h" #include "mappingtreemodel.h" +#include "mappingtreeresultmodel.h" #include "mappingtreewidget.h" #include "mappingeditwidget.h" #include "smtreeitem.h" diff --git a/shemov.pro b/shemov.pro index d97a94a..544d2a3 100644 --- a/shemov.pro +++ b/shemov.pro @@ -55,7 +55,9 @@ SOURCES = main.cpp \ mappingeditwidget.cpp \ mappinginputdialog.cpp \ mappingeditdialog.cpp \ - mappingtreeproxy.cpp + mappingtreeproxy.cpp \ + mappingtreeresultmodel.cpp \ + mappingdata.cpp HEADERS = \ shemov.h \ helper.h \ @@ -105,7 +107,9 @@ HEADERS = \ mappingeditwidget.h \ mappinginputdialog.h \ mappingeditdialog.h \ - mappingtreeproxy.h + mappingtreeproxy.h \ + mappingtreeresultmodel.h \ + mappingdata.h LIBS += -lmagic -lXfixes -lX11 -lMagick++-6.Q16HDRI INCLUDEPATH += /usr/include/ImageMagick-6/ RESOURCES = shemov.qrc -- cgit v1.2.3-70-g09d2