save and load genus widget data
This commit is contained in:
parent
6ec07a7301
commit
27ace83ddd
12 changed files with 114 additions and 43 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <QJsonObject>
|
||||
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
};
|
||||
|
|
|
@ -1,10 +1,36 @@
|
|||
#include "CheckableItems.h"
|
||||
|
||||
void CheckableItems::write(QJsonObject &json) const
|
||||
#include <QJsonArray>
|
||||
|
||||
CheckableItems::CheckableItems(std::initializer_list<std::string> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,10 +5,11 @@
|
|||
#include <QJsonObject>
|
||||
#include <map>
|
||||
|
||||
class CheckableItems : public std::map<size_t, CheckableItem>
|
||||
class CheckableItems : public std::vector<CheckableItem>
|
||||
{
|
||||
public:
|
||||
using std::map<size_t, CheckableItem>::map;
|
||||
CheckableItems(std::initializer_list<std::string> itemNames);
|
||||
|
||||
void write(QJsonObject &json) const;
|
||||
void write(QJsonArray &json) const;
|
||||
void read(const QJsonArray &json);
|
||||
};
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "GenusModel.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QDebug>
|
||||
|
||||
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");
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView"/>
|
||||
<widget class="QTableView" name="genusTableView"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
|
|
|
@ -31,6 +31,7 @@ void MainWindow::newFile()
|
|||
{
|
||||
m_dataModel = std::make_unique<DataModel>(this);
|
||||
ui->metaDataWidget->setModel(&m_dataModel->m_metaData);
|
||||
ui->genusWidget->setModel(&m_dataModel->m_genus);
|
||||
}
|
||||
|
||||
void MainWindow::openFile()
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="MetaDataTab">
|
||||
<attribute name="title">
|
||||
|
@ -36,7 +36,7 @@
|
|||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||
<item>
|
||||
<widget class="GenusWidget" name="widget" native="true"/>
|
||||
<widget class="GenusWidget" name="genusWidget" native="true"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
|
@ -50,7 +50,7 @@
|
|||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>905</width>
|
||||
<height>19</height>
|
||||
<height>17</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menuFile">
|
||||
|
|
Loading…
Reference in a new issue