From 27ace83ddd703ad451bd54bed9e901abd239444a Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 23 May 2018 11:08:50 +0200 Subject: [PATCH] save and load genus widget data --- source/DataModel.cpp | 6 +++++ source/Genus/CheckableItem.cpp | 21 +++++++++++++++ source/Genus/CheckableItem.h | 6 +++++ source/Genus/CheckableItems.cpp | 34 +++++++++++++++++++++--- source/Genus/CheckableItems.h | 7 ++--- source/Genus/GenusModel.cpp | 47 +++++++++++++++++++++------------ source/Genus/GenusModel.h | 13 +++------ source/Genus/GenusWidget.cpp | 11 +++++--- source/Genus/GenusWidget.h | 3 ++- source/Genus/GenusWidget.ui | 2 +- source/mainwindow.cpp | 1 + source/mainwindow.ui | 6 ++--- 12 files changed, 114 insertions(+), 43 deletions(-) diff --git a/source/DataModel.cpp b/source/DataModel.cpp index ada23b5..e174c9e 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -25,4 +25,10 @@ void DataModel::read(const QJsonObject &source) { m_metaData.read(metaData.toObject()); } + + const auto &genus = source["Genus"]; + if (genus.isObject()) + { + m_genus.read(genus.toObject()); + } } diff --git a/source/Genus/CheckableItem.cpp b/source/Genus/CheckableItem.cpp index 566bab9..9d67826 100644 --- a/source/Genus/CheckableItem.cpp +++ b/source/Genus/CheckableItem.cpp @@ -19,3 +19,24 @@ void CheckableItem::setState(bool checked) { m_checked = checked; } + +void CheckableItem::write(QJsonObject &json) const +{ + json["text"] = m_text.c_str(); + json["checked"] = m_checked; +} + +void CheckableItem::read(const QJsonObject &json) +{ + const auto &text = json["text"]; + if (text.isString()) + { + m_text = text.toString().toStdString(); + } + + const auto &checked = json["checked"]; + if (checked.isBool()) + { + m_checked = checked.toBool(); + } +} diff --git a/source/Genus/CheckableItem.h b/source/Genus/CheckableItem.h index bc12ff2..f097bdf 100644 --- a/source/Genus/CheckableItem.h +++ b/source/Genus/CheckableItem.h @@ -1,5 +1,7 @@ #pragma once +#include + #include class CheckableItem @@ -9,9 +11,13 @@ private: std::string m_text; public: + CheckableItem() = default; CheckableItem(const std::string &text); std::string getText() const; bool isChecked() const; void setState(bool checked); + + void write(QJsonObject &json) const; + void read(const QJsonObject &json); }; diff --git a/source/Genus/CheckableItems.cpp b/source/Genus/CheckableItems.cpp index 4863ff3..c3348a2 100644 --- a/source/Genus/CheckableItems.cpp +++ b/source/Genus/CheckableItems.cpp @@ -1,10 +1,36 @@ #include "CheckableItems.h" -void CheckableItems::write(QJsonObject &json) const +#include + +CheckableItems::CheckableItems(std::initializer_list itemNames) { - for (const auto &pair : *this) + for (const auto &itemName : itemNames) { - json[pair.second.getText().c_str()] = - pair.second.isChecked(); + emplace_back(itemName); + } +} + +void CheckableItems::write(QJsonArray &json) const +{ + for (const auto &item : *this) + { + QJsonObject itemObject; + item.write(itemObject); + json.append(itemObject); + } +} + +void CheckableItems::read(const QJsonArray &json) +{ + clear(); + + for (const auto &itemObject : json) + { + if (itemObject.isObject()) + { + CheckableItem item; + item.read(itemObject.toObject()); + emplace_back(item); + } } } diff --git a/source/Genus/CheckableItems.h b/source/Genus/CheckableItems.h index 715f77b..2a1a77d 100644 --- a/source/Genus/CheckableItems.h +++ b/source/Genus/CheckableItems.h @@ -5,10 +5,11 @@ #include #include -class CheckableItems : public std::map +class CheckableItems : public std::vector { public: - using std::map::map; + CheckableItems(std::initializer_list itemNames); - void write(QJsonObject &json) const; + void write(QJsonArray &json) const; + void read(const QJsonArray &json); }; diff --git a/source/Genus/GenusModel.cpp b/source/Genus/GenusModel.cpp index e081261..229ea31 100644 --- a/source/Genus/GenusModel.cpp +++ b/source/Genus/GenusModel.cpp @@ -1,5 +1,6 @@ #include "GenusModel.h" +#include #include GenusModel::GenusModel(QObject *parent) @@ -104,21 +105,35 @@ QVariant GenusModel::headerData( void GenusModel::write(QJsonObject &json) const { - QJsonObject tiere; + QJsonArray tiere; m_tiere.write(tiere); json["Tiere"] = tiere; - QJsonObject futter; + QJsonArray futter; m_futter.write(futter); json["Futter"] = futter; - QJsonObject zirkus; + QJsonArray zirkus; m_zirkus.write(zirkus); json["Zirkus"] = zirkus; } void GenusModel::read(const QJsonObject &json) { + if (json["Tiere"].isArray()) + { + m_tiere.read(json["Tiere"].toArray()); + } + + if (json["Futter"].isArray()) + { + m_futter.read(json["Futter"].toArray()); + } + + if (json["Zirkus"].isArray()) + { + m_zirkus.read(json["Zirkus"].toArray()); + } } bool GenusModel::isValidIndex(const QModelIndex &index) const @@ -126,11 +141,11 @@ bool GenusModel::isValidIndex(const QModelIndex &index) const switch (index.row()) { case 0: - return m_tiere.find(index.column()) != m_tiere.end(); + return index.column() < m_tiere.size(); case 1: - return m_futter.find(index.column()) != m_futter.end(); + return index.column() < m_futter.size(); case 2: - return m_zirkus.find(index.column()) != m_zirkus.end(); + return index.column() < m_zirkus.size(); default: return false; } @@ -173,23 +188,21 @@ const CheckableItems &GenusModel::getItems(const QModelIndex &index) const CheckableItem &GenusModel::getItem(const QModelIndex &index) { auto &items = getItems(index); - auto entry = items.find(index.column()); - if (entry != items.end()) - { - return entry->second; - } + if (index.column() < items.size()) + { + return items.at(index.column()); + } throw std::runtime_error("invalid index"); } const CheckableItem &GenusModel::getItem(const QModelIndex &index) const { - auto &items = getItems(index); - auto entry = items.find(index.column()); - if (entry != items.end()) - { - return entry->second; - } + auto &items = getItems(index); + if (index.column() < items.size()) + { + return items.at(index.column()); + } throw std::runtime_error("invalid index"); } diff --git a/source/Genus/GenusModel.h b/source/Genus/GenusModel.h index c57524f..436f2a3 100644 --- a/source/Genus/GenusModel.h +++ b/source/Genus/GenusModel.h @@ -10,16 +10,9 @@ class GenusModel : public QAbstractTableModel Q_OBJECT private: - CheckableItems m_tiere = {{0, {"Tiger"}}, {1, {"Bär"}}, {2, {"Katze"}}, - {3, {"Pferd"}}, {4, {"Gans"}}, {5, {"Elefant"}}, {6, {"Katze"}}, - {7, {"Hund"}}}; - - CheckableItems m_futter = {{0, {"Salat"}}, {1, {"Fleisch"}}, - {2, {"Knocken"}}, {3, {"Banane"}}, {4, {"Apfel"}}, {5, {"Möhre"}}, - {6, {"Honig"}}, {7, {"Zucker"}}}; - - CheckableItems m_zirkus = { - {0, {"Kiste"}}, {1, {"Holz"}}, {2, {"Vorhang"}}, {3, {"Baum"}}}; + CheckableItems m_tiere = { "Tiger", "Bär", "Katze", "Pferd", "Gans", "Elefant", "Katze", "Hund" }; + CheckableItems m_futter = { "Salat", "Fleisch", "Knocken", "Banane", "Apfel", "Möhre", "Honig", "Zucker" }; + CheckableItems m_zirkus = { "Kiste", "Holz", "Vorhang", "Baum" }; public: GenusModel(QObject *parent); diff --git a/source/Genus/GenusWidget.cpp b/source/Genus/GenusWidget.cpp index 773d44b..41faea5 100644 --- a/source/Genus/GenusWidget.cpp +++ b/source/Genus/GenusWidget.cpp @@ -6,15 +6,18 @@ GenusWidget::GenusWidget(QWidget *parent) : QWidget(parent) , ui(new Ui::GenusWidget) - , m_model(new GenusModel(this)) { ui->setupUi(this); - ui->tableView->horizontalHeader()->hide(); - ui->tableView->setModel(m_model); + ui->genusTableView->horizontalHeader()->hide(); } GenusWidget::~GenusWidget() { - delete ui; + delete ui; +} + +void GenusWidget::setModel(GenusModel *model) +{ + ui->genusTableView->setModel(model); } diff --git a/source/Genus/GenusWidget.h b/source/Genus/GenusWidget.h index f5212fc..b8ae706 100644 --- a/source/Genus/GenusWidget.h +++ b/source/Genus/GenusWidget.h @@ -14,9 +14,10 @@ class GenusWidget : public QWidget private: Ui::GenusWidget *ui; - GenusModel *m_model; public: GenusWidget(QWidget *parent = nullptr); ~GenusWidget(); + + void setModel(GenusModel *model); }; diff --git a/source/Genus/GenusWidget.ui b/source/Genus/GenusWidget.ui index c06e810..b59f922 100644 --- a/source/Genus/GenusWidget.ui +++ b/source/Genus/GenusWidget.ui @@ -15,7 +15,7 @@ - + diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 348e1c5..7927602 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -31,6 +31,7 @@ void MainWindow::newFile() { m_dataModel = std::make_unique(this); ui->metaDataWidget->setModel(&m_dataModel->m_metaData); + ui->genusWidget->setModel(&m_dataModel->m_genus); } void MainWindow::openFile() diff --git a/source/mainwindow.ui b/source/mainwindow.ui index 5183b74..8d2d210 100644 --- a/source/mainwindow.ui +++ b/source/mainwindow.ui @@ -18,7 +18,7 @@ - 0 + 1 @@ -36,7 +36,7 @@ - + @@ -50,7 +50,7 @@ 0 0 905 - 19 + 17