save and load genus widget data
This commit is contained in:
parent
6ec07a7301
commit
27ace83ddd
@ -25,4 +25,10 @@ void DataModel::read(const QJsonObject &source)
|
|||||||
{
|
{
|
||||||
m_metaData.read(metaData.toObject());
|
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;
|
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
|
#pragma once
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class CheckableItem
|
class CheckableItem
|
||||||
@ -9,9 +11,13 @@ private:
|
|||||||
std::string m_text;
|
std::string m_text;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
CheckableItem() = default;
|
||||||
CheckableItem(const std::string &text);
|
CheckableItem(const std::string &text);
|
||||||
|
|
||||||
std::string getText() const;
|
std::string getText() const;
|
||||||
bool isChecked() const;
|
bool isChecked() const;
|
||||||
void setState(bool checked);
|
void setState(bool checked);
|
||||||
|
|
||||||
|
void write(QJsonObject &json) const;
|
||||||
|
void read(const QJsonObject &json);
|
||||||
};
|
};
|
||||||
|
@ -1,10 +1,36 @@
|
|||||||
#include "CheckableItems.h"
|
#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()] =
|
emplace_back(itemName);
|
||||||
pair.second.isChecked();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 <QJsonObject>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
class CheckableItems : public std::map<size_t, CheckableItem>
|
class CheckableItems : public std::vector<CheckableItem>
|
||||||
{
|
{
|
||||||
public:
|
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 "GenusModel.h"
|
||||||
|
|
||||||
|
#include <QJsonArray>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
GenusModel::GenusModel(QObject *parent)
|
GenusModel::GenusModel(QObject *parent)
|
||||||
@ -104,21 +105,35 @@ QVariant GenusModel::headerData(
|
|||||||
|
|
||||||
void GenusModel::write(QJsonObject &json) const
|
void GenusModel::write(QJsonObject &json) const
|
||||||
{
|
{
|
||||||
QJsonObject tiere;
|
QJsonArray tiere;
|
||||||
m_tiere.write(tiere);
|
m_tiere.write(tiere);
|
||||||
json["Tiere"] = tiere;
|
json["Tiere"] = tiere;
|
||||||
|
|
||||||
QJsonObject futter;
|
QJsonArray futter;
|
||||||
m_futter.write(futter);
|
m_futter.write(futter);
|
||||||
json["Futter"] = futter;
|
json["Futter"] = futter;
|
||||||
|
|
||||||
QJsonObject zirkus;
|
QJsonArray zirkus;
|
||||||
m_zirkus.write(zirkus);
|
m_zirkus.write(zirkus);
|
||||||
json["Zirkus"] = zirkus;
|
json["Zirkus"] = zirkus;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenusModel::read(const QJsonObject &json)
|
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
|
bool GenusModel::isValidIndex(const QModelIndex &index) const
|
||||||
@ -126,11 +141,11 @@ bool GenusModel::isValidIndex(const QModelIndex &index) const
|
|||||||
switch (index.row())
|
switch (index.row())
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
return m_tiere.find(index.column()) != m_tiere.end();
|
return index.column() < m_tiere.size();
|
||||||
case 1:
|
case 1:
|
||||||
return m_futter.find(index.column()) != m_futter.end();
|
return index.column() < m_futter.size();
|
||||||
case 2:
|
case 2:
|
||||||
return m_zirkus.find(index.column()) != m_zirkus.end();
|
return index.column() < m_zirkus.size();
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -173,23 +188,21 @@ const CheckableItems &GenusModel::getItems(const QModelIndex &index) const
|
|||||||
CheckableItem &GenusModel::getItem(const QModelIndex &index)
|
CheckableItem &GenusModel::getItem(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
auto &items = getItems(index);
|
auto &items = getItems(index);
|
||||||
auto entry = items.find(index.column());
|
if (index.column() < items.size())
|
||||||
if (entry != items.end())
|
{
|
||||||
{
|
return items.at(index.column());
|
||||||
return entry->second;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error("invalid index");
|
throw std::runtime_error("invalid index");
|
||||||
}
|
}
|
||||||
|
|
||||||
const CheckableItem &GenusModel::getItem(const QModelIndex &index) const
|
const CheckableItem &GenusModel::getItem(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
auto &items = getItems(index);
|
auto &items = getItems(index);
|
||||||
auto entry = items.find(index.column());
|
if (index.column() < items.size())
|
||||||
if (entry != items.end())
|
{
|
||||||
{
|
return items.at(index.column());
|
||||||
return entry->second;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error("invalid index");
|
throw std::runtime_error("invalid index");
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,9 @@ class GenusModel : public QAbstractTableModel
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CheckableItems m_tiere = {{0, {"Tiger"}}, {1, {"Bär"}}, {2, {"Katze"}},
|
CheckableItems m_tiere = { "Tiger", "Bär", "Katze", "Pferd", "Gans", "Elefant", "Katze", "Hund" };
|
||||||
{3, {"Pferd"}}, {4, {"Gans"}}, {5, {"Elefant"}}, {6, {"Katze"}},
|
CheckableItems m_futter = { "Salat", "Fleisch", "Knocken", "Banane", "Apfel", "Möhre", "Honig", "Zucker" };
|
||||||
{7, {"Hund"}}};
|
CheckableItems m_zirkus = { "Kiste", "Holz", "Vorhang", "Baum" };
|
||||||
|
|
||||||
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"}}};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GenusModel(QObject *parent);
|
GenusModel(QObject *parent);
|
||||||
|
@ -6,15 +6,18 @@
|
|||||||
GenusWidget::GenusWidget(QWidget *parent)
|
GenusWidget::GenusWidget(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
, ui(new Ui::GenusWidget)
|
, ui(new Ui::GenusWidget)
|
||||||
, m_model(new GenusModel(this))
|
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
ui->tableView->horizontalHeader()->hide();
|
ui->genusTableView->horizontalHeader()->hide();
|
||||||
ui->tableView->setModel(m_model);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GenusWidget::~GenusWidget()
|
GenusWidget::~GenusWidget()
|
||||||
{
|
{
|
||||||
delete ui;
|
delete ui;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GenusWidget::setModel(GenusModel *model)
|
||||||
|
{
|
||||||
|
ui->genusTableView->setModel(model);
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,10 @@ class GenusWidget : public QWidget
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::GenusWidget *ui;
|
Ui::GenusWidget *ui;
|
||||||
GenusModel *m_model;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GenusWidget(QWidget *parent = nullptr);
|
GenusWidget(QWidget *parent = nullptr);
|
||||||
~GenusWidget();
|
~GenusWidget();
|
||||||
|
|
||||||
|
void setModel(GenusModel *model);
|
||||||
};
|
};
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QTableView" name="tableView"/>
|
<widget class="QTableView" name="genusTableView"/>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
@ -31,6 +31,7 @@ void MainWindow::newFile()
|
|||||||
{
|
{
|
||||||
m_dataModel = std::make_unique<DataModel>(this);
|
m_dataModel = std::make_unique<DataModel>(this);
|
||||||
ui->metaDataWidget->setModel(&m_dataModel->m_metaData);
|
ui->metaDataWidget->setModel(&m_dataModel->m_metaData);
|
||||||
|
ui->genusWidget->setModel(&m_dataModel->m_genus);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::openFile()
|
void MainWindow::openFile()
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QTabWidget" name="tabWidget">
|
<widget class="QTabWidget" name="tabWidget">
|
||||||
<property name="currentIndex">
|
<property name="currentIndex">
|
||||||
<number>0</number>
|
<number>1</number>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="MetaDataTab">
|
<widget class="QWidget" name="MetaDataTab">
|
||||||
<attribute name="title">
|
<attribute name="title">
|
||||||
@ -36,7 +36,7 @@
|
|||||||
</attribute>
|
</attribute>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||||
<item>
|
<item>
|
||||||
<widget class="GenusWidget" name="widget" native="true"/>
|
<widget class="GenusWidget" name="genusWidget" native="true"/>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
@ -50,7 +50,7 @@
|
|||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>905</width>
|
<width>905</width>
|
||||||
<height>19</height>
|
<height>17</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QMenu" name="menuFile">
|
<widget class="QMenu" name="menuFile">
|
||||||
|
Reference in New Issue
Block a user