First version

This commit is contained in:
Aurélie Delhaie
2022-02-01 19:08:44 +01:00
commit d661cc7b42
26 changed files with 1896 additions and 0 deletions

15
src/frames/aboutdialog.cpp Executable file
View File

@@ -0,0 +1,15 @@
#include "aboutdialog.h"
#include "ui_aboutdialog.h"
AboutDialog::AboutDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AboutDialog)
{
ui->setupUi(this);
ui->appNameLabel->setText(QString("TaskNote (%1)").arg(QSysInfo::currentCpuArchitecture()));
}
AboutDialog::~AboutDialog()
{
delete ui;
}

23
src/frames/aboutdialog.h Executable file
View File

@@ -0,0 +1,23 @@
#ifndef ABOUTDIALOG_H
#define ABOUTDIALOG_H
#include <QDialog>
#include <QSysInfo>
namespace Ui {
class AboutDialog;
}
class AboutDialog : public QDialog
{
Q_OBJECT
public:
explicit AboutDialog(QWidget *parent = nullptr);
~AboutDialog();
private:
Ui::AboutDialog *ui;
};
#endif // ABOUTDIALOG_H

97
src/frames/aboutdialog.ui Executable file
View File

@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AboutDialog</class>
<widget class="QDialog" name="AboutDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>334</width>
<height>180</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>334</width>
<height>180</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>334</width>
<height>180</height>
</size>
</property>
<property name="windowTitle">
<string>About TaskNote</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>0</width>
<height>64</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>64</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">image: url(:/logo/resources/logo.png);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="appNameLabel">
<property name="font">
<font>
<pointsize>14</pointsize>
</font>
</property>
<property name="text">
<string>TaskNote</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Version 1.1.0.0</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Author: Aurélie Delhaie (aureliedelhaie.fr)</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

133
src/frames/mainwindow.cpp Executable file
View File

@@ -0,0 +1,133 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(ui->actionAdd, &QAction::triggered, this, &MainWindow::createNote);
connect(ui->actionRemove, &QAction::triggered, this, &MainWindow::removeSelected);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::save);
connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::showAboutBox);
connect(ui->noteList, &QListWidget::currentRowChanged, this, &MainWindow::selectionChanged);
connect(ui->titleEdit, &QLineEdit::textChanged, this, &MainWindow::titleChanged);
connect(ui->contentEdit, &QPlainTextEdit::textChanged, this, &MainWindow::contentChanged);
const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
ui->contentEdit->setFont(fixedFont);
this->savemng = new SaveManager();
updateListView();
connect(timer, &QTimer::timeout, this, &MainWindow::save);
}
MainWindow::~MainWindow()
{
delete timer;
delete savemng;
delete ui;
}
void MainWindow::createNote()
{
Note *n = new Note();
n->setTitle("untitled note");
savemng->addNote(n);
updateListView();
}
void MainWindow::selectionChanged(int i)
{
this->currentIndex = i;
if (i == -1)
{
clearAndDisableFields();
return;
}
Note *n = this->savemng->getNoteByIndex(i);
if (n == nullptr)
{
QMessageBox::critical(this, tr("TaskNote"), "Failed to get this note, the memory pointer is empty", QMessageBox::Ok, QMessageBox::Ok);
clearAndDisableFields();
return;
}
ui->actionRemove->setDisabled(false);
ui->titleEdit->setDisabled(false);
ui->contentEdit->setDisabled(false);
ui->titleEdit->setText(n->getTitle());
ui->contentEdit->setPlainText(n->getContent());
ui->markdownViewer->setMarkdown(n->getContent());
}
void MainWindow::removeSelected()
{
if (this->currentIndex > -1)
{
this->savemng->removeNoteByIndex(this->currentIndex);
clearAndDisableFields();
updateListView();
}
}
void MainWindow::save()
{
if (this->currentIndex > -1)
{
Note *n = this->savemng->getNoteByIndex(this->currentIndex);
n->setTitle(ui->titleEdit->text());
n->setContent(ui->contentEdit->toPlainText());
}
this->savemng->flushSave();
}
void MainWindow::titleChanged()
{
timer->stop();
if (this->currentIndex > -1)
{
Note *n = this->savemng->getNoteByIndex(this->currentIndex);
n->setTitle(ui->titleEdit->text());
ui->noteList->item(this->currentIndex)->setText(ui->titleEdit->text());
}
timer->start(1000);
}
void MainWindow::showAboutBox()
{
AboutDialog dialog;
dialog.setModal(true);
dialog.exec();
}
void MainWindow::contentChanged()
{
timer->stop();
if (this->currentIndex > -1)
{
Note *n = this->savemng->getNoteByIndex(this->currentIndex);
n->setContent(ui->contentEdit->toPlainText());
ui->markdownViewer->setMarkdown(ui->contentEdit->toPlainText());
}
timer->start(1000);
}
void MainWindow::updateListView()
{
ui->noteList->clear();
foreach (Note *n, savemng->getNotes())
{
ui->noteList->addItem(n->getTitle());
}
}
void MainWindow::clearAndDisableFields()
{
this->currentIndex = -1;
ui->actionRemove->setDisabled(true);
ui->titleEdit->setDisabled(true);
ui->contentEdit->setDisabled(true);
ui->titleEdit->clear();
ui->contentEdit->clear();
ui->markdownViewer->clear();
}

44
src/frames/mainwindow.h Executable file
View File

@@ -0,0 +1,44 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
#include <QTimer>
#include "../services/savemanager.h"
#include "aboutdialog.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void createNote();
void selectionChanged(int);
void removeSelected();
void save();
void contentChanged();
void titleChanged();
void showAboutBox();
private:
Ui::MainWindow *ui;
SaveManager *savemng;
QTimer *timer;
int currentIndex = -1;
void updateListView();
void clearAndDisableFields();
};
#endif // MAINWINDOW_H

189
src/frames/mainwindow.ui Executable file
View File

@@ -0,0 +1,189 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1075</width>
<height>710</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>1075</width>
<height>710</height>
</size>
</property>
<property name="windowTitle">
<string>TaskNote</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QListWidget" name="noteList">
<property name="maximumSize">
<size>
<width>250</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>13</height>
</size>
</property>
<property name="text">
<string>Title</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="titleEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Markdown editor</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Viewer</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPlainTextEdit" name="contentEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="markdownViewer">
<property name="enabled">
<bool>true</bool>
</property>
<property name="autoFormatting">
<set>QTextEdit::AutoAll</set>
</property>
<property name="lineWrapMode">
<enum>QTextEdit::WidgetWidth</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QToolBar" name="toolBar">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>33</height>
</size>
</property>
<property name="windowTitle">
<string>toolBar</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionAdd"/>
<addaction name="actionRemove"/>
<addaction name="separator"/>
<addaction name="actionSave"/>
<addaction name="separator"/>
<addaction name="actionAbout"/>
</widget>
<action name="actionAdd">
<property name="icon">
<iconset resource="../../icons.qrc">
<normaloff>:/icon/resources/outline_add_circle_outline_black_18dp.png</normaloff>:/icon/resources/outline_add_circle_outline_black_18dp.png</iconset>
</property>
<property name="text">
<string>Add</string>
</property>
</action>
<action name="actionRemove">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset resource="../../icons.qrc">
<normaloff>:/icon/resources/outline_delete_forever_black_18dp.png</normaloff>:/icon/resources/outline_delete_forever_black_18dp.png</iconset>
</property>
<property name="text">
<string>Remove</string>
</property>
</action>
<action name="actionSave">
<property name="icon">
<iconset resource="../../icons.qrc">
<normaloff>:/icon/resources/outline_save_black_18dp.png</normaloff>:/icon/resources/outline_save_black_18dp.png</iconset>
</property>
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionAbout">
<property name="icon">
<iconset resource="../../icons.qrc">
<normaloff>:/icon/resources/outline_help_outline_black_18dp.png</normaloff>:/icon/resources/outline_help_outline_black_18dp.png</iconset>
</property>
<property name="text">
<string>About</string>
</property>
</action>
</widget>
<resources>
<include location="../../icons.qrc"/>
</resources>
<connections/>
</ui>

11
src/main.cpp Executable file
View File

@@ -0,0 +1,11 @@
#include "src/frames/mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

45
src/models/note.cpp Executable file
View File

@@ -0,0 +1,45 @@
#include "note.h"
Note::Note()
{
}
QJsonObject Note::toJson()
{
QJsonObject o;
o["title"] = this->title;
o["content"] = this->content;
return o;
}
Note *Note::fromJson(QJsonObject o)
{
Note *n = new Note();
n->title = o["title"].toString();
n->content = o["content"].toString();
return n;
}
QString Note::getTitle()
{
return title;
}
QString Note::getContent()
{
return content;
}
void Note::setTitle(QString value)
{
this->title = value;
}
void Note::setContent(QString value)
{
this->content = value;
}

26
src/models/note.h Executable file
View File

@@ -0,0 +1,26 @@
#ifndef NOTE_H
#define NOTE_H
#include <QString>
#include <QJsonObject>
class Note
{
public:
Note();
QJsonObject toJson();
static Note* fromJson(QJsonObject o);
QString getTitle();
QString getContent();
void setTitle(QString value);
void setContent(QString value);
private:
QString title;
QString content;
};
#endif // NOTE_H

99
src/services/savemanager.cpp Executable file
View File

@@ -0,0 +1,99 @@
#include "savemanager.h"
SaveManager::SaveManager()
{
openSave();
}
SaveManager::~SaveManager()
{
flushSave();
}
void SaveManager::flushSave()
{
QJsonArray json_notes;
foreach (Note *n, notes)
{
json_notes.append(n->toJson());
}
QJsonObject obj
{
{"notes", json_notes},
};
QJsonDocument doc(obj);
QFile *f = new QFile(getSaveFilePath());
if (f->open(QIODevice::WriteOnly))
{
f->write(doc.toJson());
f->close();
}
delete f;
}
QVector<Note *> SaveManager::getNotes()
{
return notes;
}
void SaveManager::addNote(Note *n)
{
this->notes.append(n);
flushSave();
}
bool SaveManager::removeNoteByIndex(int i)
{
if ((i < 0) || (i >= this->notes.length()))
{
return false;
}
Note *n = this->notes[i];
this->notes.removeAt(i);
delete n;
flushSave();
return true;
}
Note *SaveManager::getNoteByIndex(int i)
{
if ((i < 0) || (i >= this->notes.length()))
{
return nullptr;
}
return this->notes[i];
}
QString SaveManager::getSaveFilePath() {
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (!QDir(path).exists())
{
QDir().mkpath(path);
}
path += "/data/";
if (!QDir(path).exists())
{
QDir().mkpath(path);
}
path += SAVE_FILENAME;
return QDir::cleanPath(path);
}
void SaveManager::openSave() {
QFile* file = new QFile(getSaveFilePath());
file->open(QIODevice::ReadOnly);
auto json = QString(file->readAll());
file->close();
delete file;
QJsonObject obj = QJsonDocument::fromJson(json.toUtf8()).object();
QJsonArray arr = obj["notes"].toArray();
for (int i = 0; i < arr.size(); i++)
{
this->notes.append(Note::fromJson(arr[i].toObject()));
}
}

35
src/services/savemanager.h Executable file
View File

@@ -0,0 +1,35 @@
#ifndef SAVEMANAGER_H
#define SAVEMANAGER_H
#define SAVE_FILENAME "data.json"
#include <QStandardPaths>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QVector>
#include "../models/note.h"
class SaveManager
{
public:
SaveManager();
~SaveManager();
void flushSave();
QVector<Note*> getNotes();
void addNote(Note *n);
bool removeNoteByIndex(int i);
Note *getNoteByIndex(int i);
private:
QString getSaveFilePath();
void openSave();
QVector<Note*> notes;
};
#endif // SAVEMANAGER_H