moved CheckableItem to sub-project

This commit is contained in:
Michael Mandl 2018-05-24 08:16:04 +02:00
parent ee63a91604
commit 95eebd4112
10 changed files with 33 additions and 8 deletions

View file

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.6)
project(CheckableItem LANGUAGES CXX)
find_package(Qt5Core REQUIRED)
add_library(${PROJECT_NAME}
CheckableItem.cpp
CheckableItems.cpp
)
set_target_properties(${PROJECT_NAME}
PROPERTIES CXX_STANDARD 14
)
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE
Qt5::Core
)

View file

@ -0,0 +1,42 @@
#include "CheckableItem.h"
CheckableItem::CheckableItem(const std::string &text)
: m_text(text)
{
}
std::string CheckableItem::getText() const
{
return m_text;
}
bool CheckableItem::isChecked() const
{
return m_checked;
}
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();
}
}

View file

@ -0,0 +1,23 @@
#pragma once
#include <QJsonObject>
#include <string>
class CheckableItem
{
private:
bool m_checked = false;
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);
};

View file

@ -0,0 +1,36 @@
#include "CheckableItems.h"
#include <QJsonArray>
CheckableItems::CheckableItems(std::initializer_list<std::string> itemNames)
{
for (const auto &itemName : itemNames)
{
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);
}
}
}

View file

@ -0,0 +1,15 @@
#pragma once
#include "CheckableItem.h"
#include <QJsonObject>
#include <vector>
class CheckableItems : public std::vector<CheckableItem>
{
public:
CheckableItems(std::initializer_list<std::string> itemNames);
void write(QJsonArray &json) const;
void read(const QJsonArray &json);
};