WUT_Computer_Science/Programming/EGUI/lab1/universalFunctions.cpp

77 lines
1.8 KiB
C++
Raw Normal View History

2022-04-06 14:30:57 +02:00
#ifndef UNIVERSAL_FUNCTIONS_CPP
#define UNIVERSAL_FUNCTIONS_CPP
#include <QJsonObject>
#include <QString>
#include <QJsonDocument>
#include <QFile>
#include <QMessageBox>
#include <QApplication>
QJsonObject readJsonFile(const QString title)
{
QFile file(title);
2022-04-06 16:31:31 +02:00
file.open(QIODevice::ReadWrite); // If the file does not exist we create it
2022-04-06 14:30:57 +02:00
QByteArray bytes = file.readAll();
file.close();
QJsonDocument document = QJsonDocument::fromJson( bytes );
return document.object();
}
void saveJsonFile(QJsonObject &users, const QString name)
{
QFile jsonFile(name);
QJsonDocument document;
document.setObject( users );
QByteArray bytes = document.toJson( QJsonDocument::Indented );
jsonFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate );
QTextStream iStream( &jsonFile );
// iStream.setCodec( "utf-8" );
iStream << bytes;
jsonFile.close();
}
void outputMessageBox(const QString messageBoxText)
{
QMessageBox idEmpty;
idEmpty.setText(messageBoxText);
idEmpty.exec();
}
void exit()
{
QApplication::quit();
}
2022-04-06 16:31:31 +02:00
bool stringEmpty(const QString &string, const QString &messageBoxMessage)
{
if(string == "")
{
outputMessageBox(messageBoxMessage);
return true;
}
return false;
}
2022-04-06 19:44:14 +02:00
bool idExists(const QString &id, const QJsonObject &json, const QString &message, const bool &whenMessage)
2022-04-06 16:31:31 +02:00
{
if (json.find(id) != json.end())
{
2022-04-06 19:44:14 +02:00
if (whenMessage) outputMessageBox(message);
2022-04-06 16:31:31 +02:00
return true;
}
2022-04-06 19:44:14 +02:00
if (!whenMessage) outputMessageBox(message);
2022-04-06 16:31:31 +02:00
return false;
}
2022-04-06 14:30:57 +02:00
2022-04-06 19:44:14 +02:00
bool sameString(const QString &stringToCompare, const QString &jsonId, const QJsonObject &json, const QString &message)
{
if(json[jsonId].toString() != stringToCompare)
{
outputMessageBox(message);
return false;
}
return true;
}
2022-04-06 14:30:57 +02:00
#endif