feat: remove entries

This commit is contained in:
kuchy 2022-04-06 02:20:29 +02:00
parent 362ae8c36f
commit a165216ec7
11 changed files with 111 additions and 53 deletions

View File

@ -2,16 +2,6 @@
"": {
"blogId": "",
"items": [
{
"content": "",
"datetime": "01:05:33 Wed Apr 6 2022",
"title": "Title"
},
{
"content": "notempty",
"datetime": "01:05:44 Wed Apr 6 2022",
"title": "anotherTitle"
}
],
"ownerId": "1",
"title": ""

View File

@ -22,8 +22,8 @@ QT_BEGIN_MOC_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_blogEntry_t {
const uint offsetsAndSize[20];
char stringdata0[98];
const uint offsetsAndSize[24];
char stringdata0[132];
};
#define QT_MOC_LITERAL(ofs, len) \
uint(offsetof(qt_meta_stringdata_blogEntry_t, stringdata0) + ofs), len
@ -38,12 +38,15 @@ QT_MOC_LITERAL(55, 5), // "title"
QT_MOC_LITERAL(61, 12), // "saveJsonFile"
QT_MOC_LITERAL(74, 12), // "QJsonObject&"
QT_MOC_LITERAL(87, 5), // "users"
QT_MOC_LITERAL(93, 4) // "name"
QT_MOC_LITERAL(93, 4), // "name"
QT_MOC_LITERAL(98, 11), // "removeEntry"
QT_MOC_LITERAL(110, 21) // "on_pushButton_clicked"
},
"blogEntry\0on_saveEntry_clicked\0\0"
"saveEntry\0readJsonFile\0title\0saveJsonFile\0"
"QJsonObject&\0users\0name"
"QJsonObject&\0users\0name\0removeEntry\0"
"on_pushButton_clicked"
};
#undef QT_MOC_LITERAL
@ -53,7 +56,7 @@ static const uint qt_meta_data_blogEntry[] = {
10, // revision
0, // classname
0, 0, // classinfo
4, 14, // methods
6, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
@ -61,16 +64,20 @@ static const uint qt_meta_data_blogEntry[] = {
0, // signalCount
// slots: name, argc, parameters, tag, flags, initial metatype offsets
1, 0, 38, 2, 0x08, 1 /* Private */,
3, 0, 39, 2, 0x08, 2 /* Private */,
4, 1, 40, 2, 0x08, 3 /* Private */,
6, 2, 43, 2, 0x108, 5 /* Private | MethodIsConst */,
1, 0, 50, 2, 0x08, 1 /* Private */,
3, 0, 51, 2, 0x08, 2 /* Private */,
4, 1, 52, 2, 0x08, 3 /* Private */,
6, 2, 55, 2, 0x108, 5 /* Private | MethodIsConst */,
10, 0, 60, 2, 0x08, 8 /* Private */,
11, 0, 61, 2, 0x08, 9 /* Private */,
// slots: parameters
QMetaType::Void,
QMetaType::Void,
QMetaType::QJsonObject, QMetaType::QString, 5,
QMetaType::Void, 0x80000000 | 7, QMetaType::QString, 8, 9,
QMetaType::Void,
QMetaType::Void,
0 // eod
};
@ -86,6 +93,8 @@ void blogEntry::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, v
case 2: { QJsonObject _r = _t->readJsonFile((*reinterpret_cast< std::add_pointer_t<QString>>(_a[1])));
if (_a[0]) *reinterpret_cast< QJsonObject*>(_a[0]) = std::move(_r); } break;
case 3: _t->saveJsonFile((*reinterpret_cast< std::add_pointer_t<QJsonObject&>>(_a[1])),(*reinterpret_cast< std::add_pointer_t<QString>>(_a[2]))); break;
case 4: _t->removeEntry(); break;
case 5: _t->on_pushButton_clicked(); break;
default: ;
}
}
@ -99,7 +108,7 @@ const QMetaObject blogEntry::staticMetaObject = { {
nullptr,
qt_incomplete_metaTypeArray<qt_meta_stringdata_blogEntry_t
, QtPrivate::TypeAndForceComplete<blogEntry, std::true_type>
, QtPrivate::TypeAndForceComplete<void, std::false_type>, QtPrivate::TypeAndForceComplete<void, std::false_type>, QtPrivate::TypeAndForceComplete<QJsonObject, std::false_type>, QtPrivate::TypeAndForceComplete<const QString, std::false_type>, QtPrivate::TypeAndForceComplete<void, std::false_type>, QtPrivate::TypeAndForceComplete<QJsonObject &, std::false_type>, QtPrivate::TypeAndForceComplete<const QString, std::false_type>
, QtPrivate::TypeAndForceComplete<void, std::false_type>, QtPrivate::TypeAndForceComplete<void, std::false_type>, QtPrivate::TypeAndForceComplete<QJsonObject, std::false_type>, QtPrivate::TypeAndForceComplete<const QString, std::false_type>, QtPrivate::TypeAndForceComplete<void, std::false_type>, QtPrivate::TypeAndForceComplete<QJsonObject &, std::false_type>, QtPrivate::TypeAndForceComplete<const QString, std::false_type>, QtPrivate::TypeAndForceComplete<void, std::false_type>, QtPrivate::TypeAndForceComplete<void, std::false_type>
>,
@ -126,13 +135,13 @@ int blogEntry::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 4)
if (_id < 6)
qt_static_metacall(this, _c, _id, _a);
_id -= 4;
_id -= 6;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 4)
if (_id < 6)
*reinterpret_cast<QMetaType *>(_a[0]) = QMetaType();
_id -= 4;
_id -= 6;
}
return _id;
}

View File

@ -11,7 +11,6 @@
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
@ -25,12 +24,12 @@ class Ui_blogEntry
{
public:
QFormLayout *formLayout;
QCheckBox *checkBox;
QLineEdit *lineEdit;
QLabel *ownerID;
QLabel *dateTime;
QTextEdit *textEdit;
QPushButton *saveEntry;
QPushButton *pushButton;
void setupUi(QWidget *blogEntry)
{
@ -39,36 +38,36 @@ public:
blogEntry->resize(400, 300);
formLayout = new QFormLayout(blogEntry);
formLayout->setObjectName(QString::fromUtf8("formLayout"));
checkBox = new QCheckBox(blogEntry);
checkBox->setObjectName(QString::fromUtf8("checkBox"));
formLayout->setWidget(0, QFormLayout::FieldRole, checkBox);
lineEdit = new QLineEdit(blogEntry);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
formLayout->setWidget(1, QFormLayout::FieldRole, lineEdit);
formLayout->setWidget(0, QFormLayout::FieldRole, lineEdit);
ownerID = new QLabel(blogEntry);
ownerID->setObjectName(QString::fromUtf8("ownerID"));
formLayout->setWidget(2, QFormLayout::FieldRole, ownerID);
formLayout->setWidget(1, QFormLayout::FieldRole, ownerID);
dateTime = new QLabel(blogEntry);
dateTime->setObjectName(QString::fromUtf8("dateTime"));
formLayout->setWidget(3, QFormLayout::FieldRole, dateTime);
formLayout->setWidget(2, QFormLayout::FieldRole, dateTime);
textEdit = new QTextEdit(blogEntry);
textEdit->setObjectName(QString::fromUtf8("textEdit"));
textEdit->setReadOnly(false);
formLayout->setWidget(4, QFormLayout::FieldRole, textEdit);
formLayout->setWidget(3, QFormLayout::FieldRole, textEdit);
saveEntry = new QPushButton(blogEntry);
saveEntry->setObjectName(QString::fromUtf8("saveEntry"));
formLayout->setWidget(5, QFormLayout::FieldRole, saveEntry);
formLayout->setWidget(4, QFormLayout::FieldRole, saveEntry);
pushButton = new QPushButton(blogEntry);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
formLayout->setWidget(5, QFormLayout::FieldRole, pushButton);
retranslateUi(blogEntry);
@ -79,11 +78,11 @@ public:
void retranslateUi(QWidget *blogEntry)
{
blogEntry->setWindowTitle(QCoreApplication::translate("blogEntry", "Form", nullptr));
checkBox->setText(QCoreApplication::translate("blogEntry", "Remove", nullptr));
lineEdit->setText(QCoreApplication::translate("blogEntry", "Title", nullptr));
ownerID->setText(QCoreApplication::translate("blogEntry", "ownerID", nullptr));
dateTime->setText(QCoreApplication::translate("blogEntry", "Date Time", nullptr));
saveEntry->setText(QCoreApplication::translate("blogEntry", "Save Entry", nullptr));
pushButton->setText(QCoreApplication::translate("blogEntry", "Remove Entry", nullptr));
} // retranslateUi
};

View File

@ -11,12 +11,17 @@ blogEntry::blogEntry(QWidget *parent) :
ui(new Ui::blogEntry)
{
ui->setupUi(this);
QString Time = QTime::currentTime().toString();
QString Date = QDate::currentDate().toString();
ui -> dateTime -> setText(Date + " " + Time);
qDebug() << "blogEntry id: " << userId;
qDebug() << Time;
}
void blogEntry::setUpBlogEntryFromJson(const QJsonObject entry)
{
ui -> lineEdit-> setText(entry["title"].toString());
ui -> textEdit -> setText(entry["content"].toString());
ui -> dateTime -> setText(entry["datetime"].toString());
ui -> ownerID -> setText("Owner ID: " + userId);
ui -> textEdit -> setReadOnly(true);
ui -> lineEdit -> setReadOnly(true);
}
blogEntry::~blogEntry()
@ -72,7 +77,9 @@ void blogEntry::saveEntry()
entry["title"] = ui -> lineEdit -> text();
QString Time = QTime::currentTime().toString();
QString Date = QDate::currentDate().toString();
entry["datetime"] = Time + " " + Date;
ui -> dateTime -> setText( Time + " " + Date);
entry["content"] = ui -> textEdit -> toPlainText();
QJsonArray items = blogEntryJson["items"].toArray();
items.append(entry);
@ -82,8 +89,36 @@ void blogEntry::saveEntry()
}
void blogEntry::removeEntry()
{
QJsonObject blogsFile = readJsonFile("blogs.json");
QJsonObject blogEntryJson = blogsFile[blogId].toObject();
QJsonArray items = blogEntryJson["items"].toArray();
for(int i = 0; i < items.size(); i++)
{
QJsonObject currentItem = items[i].toObject();
qDebug() << currentItem["datetime"].toString();
qDebug() << ui -> dateTime -> text();
if(currentItem["datetime"].toString() == ui -> dateTime -> text())
{
items.removeAt(i);
break;
}
}
blogEntryJson.insert("items", items);
blogsFile.insert(blogId, blogEntryJson);
saveJsonFile(blogsFile, "blogs.json");
this -> ~blogEntry();
}
void blogEntry::on_saveEntry_clicked()
{
saveEntry();
}
void blogEntry::on_pushButton_clicked()
{
removeEntry();
}

View File

@ -19,17 +19,21 @@ public:
~blogEntry();
void setUserId(const QString &userId);
void setBlogId(const QString &blogId);
void setUpBlogEntryFromJson(const QJsonObject entry);
private slots:
void on_saveEntry_clicked();
void saveEntry();
QJsonObject readJsonFile(const QString title);
void saveJsonFile(QJsonObject &users, const QString name) const;
void removeEntry();
void on_pushButton_clicked();
private:
Ui::blogEntry *ui;
QString userId;
QString blogId;
};
#endif // BLOGENTRY_H

View File

@ -15,47 +15,47 @@
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="1">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string>Title</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="1" column="1">
<widget class="QLabel" name="ownerID">
<property name="text">
<string>ownerID</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="2" column="1">
<widget class="QLabel" name="dateTime">
<property name="text">
<string>Date Time</string>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="3" column="1">
<widget class="QTextEdit" name="textEdit">
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="5" column="1">
<item row="4" column="1">
<widget class="QPushButton" name="saveEntry">
<property name="text">
<string>Save Entry</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Remove Entry</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>

View File

@ -2,15 +2,18 @@
#include "ui_blogsview.h"
#include "blogentry.h"
#include "ui_blogentry.h"
#include <algorithm>
#include <QJsonObject>
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
blogsView::blogsView(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::blogsView)
{
ui->setupUi(this);
displayBlogEntries();
}
blogsView::~blogsView()
@ -65,6 +68,23 @@ void blogsView::createNewBlogEntry()
ui -> blogsLayout -> addWidget(e);
}
void blogsView::displayBlogEntries()
{
QJsonObject blogsFile = readJsonFile("blogs.json");
QJsonObject userBlog = blogsFile[blogId].toObject();
QJsonArray blogEntries = userBlog["items"].toArray();
for(int i = 0; i < blogEntries.size(); i++)
{
QJsonObject entry = (blogEntries.at(i)).toObject();
blogEntry *e = new blogEntry();
e -> setUserId(userId);
e -> setUpBlogEntryFromJson(entry);
ui -> blogsLayout -> addWidget(e);
}
}
void blogsView::on_actionCreate_new_Blog_entry_triggered()
{
createNewBlogEntry();

View File

@ -22,6 +22,7 @@ public:
private slots:
void on_actionCreate_new_Blog_entry_triggered();
void displayBlogEntries();
private: