WUT_Computer_Science/EGUI/lab1/mainwindow.cpp

96 lines
2.8 KiB
C++
Raw Normal View History

#include "mainwindow.h" // Notepad class header file that was generated by the wizard
2022-04-03 21:23:02 +02:00
#include "ui_mainwindow.h"// UI header file that was generated by the uic tool
#include "login.h"
2022-04-06 14:30:57 +02:00
#include "universalFunctions.h"
#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
#include <QJsonObject>
#include <QJsonDocument>
#include <QTextStream>
2022-04-06 01:06:01 +02:00
#include <QJsonArray>
2022-03-26 12:43:58 +01:00
MainWindow::MainWindow(QWidget *parent) // class definition, constructor
: QMainWindow(parent) // calls qmainwindow parent constructor we have,
// base class for the MainWindow class:
, ui(new Ui::MainWindow) // create ui class and assign to ui member
// creates the UI class instance and assigns it to the ui member:
2022-03-26 12:43:58 +01:00
{
ui->setupUi(this); // setup user interface
2022-04-06 14:30:57 +02:00
defineConnections();
2022-03-26 12:43:58 +01:00
}
2022-04-06 14:30:57 +02:00
void MainWindow::defineConnections() const
2022-04-03 21:23:02 +02:00
{
2022-04-06 14:30:57 +02:00
connect(ui -> loginButton, &QPushButton::clicked, this, &MainWindow::goToLogin);
connect(ui -> pushButton, &QPushButton::clicked, this, &MainWindow::saveRegisteredUser);
connect(ui -> actionExit, &QAction::triggered, this, &MainWindow::exit);
connect(ui -> actionLogin, &QAction::triggered, this, &MainWindow::goToLogin);
2022-04-03 21:23:02 +02:00
}
2022-04-06 14:30:57 +02:00
void MainWindow::exit()
{
2022-04-06 14:30:57 +02:00
QApplication::quit();
2022-04-03 21:23:02 +02:00
}
2022-04-06 14:30:57 +02:00
MainWindow::~MainWindow()
2022-04-05 00:26:36 +02:00
{
2022-04-06 14:30:57 +02:00
delete ui; // in the destructor, we delete the ui
2022-04-05 00:26:36 +02:00
}
2022-04-06 14:30:57 +02:00
void MainWindow::saveRegisteredUser() const
2022-04-03 21:23:02 +02:00
{
2022-04-06 14:30:57 +02:00
QJsonObject blogs = readJsonFile("blogs.json");
QJsonObject users = readJsonFile("user.json");
2022-04-03 21:23:02 +02:00
QString id = ui->inputId->text();
if(id == "")
{
2022-04-06 14:30:57 +02:00
outputMessageBox("THIS ID IS EMPTY!");
2022-04-03 21:23:02 +02:00
return;
}
if(users.find(id) == users.end())
{
QString mail = ui->inputMail->text();
QString password = ui->inputPassword->text();
qDebug() << mail << " " << password;
QJsonObject user;
user["userId"] = id;
user["email"] = mail;
user["password"]=password;
2022-04-05 00:26:36 +02:00
users.insert(id, user);
2022-04-03 21:23:02 +02:00
/*
userId - unique user id - text obtained from the user during user registration
email - e-mail address of the user
password - password provided by the user
*/
2022-04-05 00:26:36 +02:00
QJsonObject blog;
QString blogTitle = ui -> inputBlogTitle->text();
QString blogId = ui -> inputBlogID->text();
if(blogs.find(blogId) == blogs.end())
{
blog["ownerId"] = id;
blog["title"] = blogTitle;
blog["blogId"] = blogId;
2022-04-06 01:06:01 +02:00
QJsonArray items;
blog["items"] = items;
2022-04-05 00:26:36 +02:00
blogs.insert(blogId, blog);
saveJsonFile(blogs, "blogs.json");
saveJsonFile(users, "user.json");
2022-04-06 14:30:57 +02:00
}else outputMessageBox("THIS BLOG ID IS ALREADY TAKEN!");
}else outputMessageBox("THIS ID IS ALREADY TAKEN!");
2022-04-03 21:23:02 +02:00
}
void MainWindow::goToLogin()
{
login *l = new login();
l -> show();
hide();
}
2022-04-06 14:30:57 +02:00
void MainWindow::test()
2022-04-03 21:23:02 +02:00
{
2022-04-06 14:30:57 +02:00
qDebug() << "pls work";
2022-03-26 12:43:58 +01:00
}
2022-04-06 14:30:57 +02:00