-
Notifications
You must be signed in to change notification settings - Fork 0
/
diarywindow.cpp
123 lines (109 loc) · 2.59 KB
/
diarywindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "diarywindow.hpp"
#include "ui_diarywindow.h"
DiaryWindow::DiaryWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::DiaryWindow)
{
ui->setupUi(this);
database = database->getInstance();
database->setView(ui->list);
}
DiaryWindow::~DiaryWindow()
{
delete ui;
}
std::string DiaryWindow::getTimeFormat()
{
time_t now;
struct tm* info;
char buffer[80];
time (&now);
info = localtime(&now);
strftime(buffer,sizeof(buffer),"%H:%M:%S_%d.%m.%Y", info);
string ftime(buffer);
ftime += "#" + to_string(rand() % 10000 + 1);
return ftime;
}
void DiaryWindow::clearInput()
{
ui->text_field->clear();
ui->text_label->clear();
}
void DiaryWindow::setDefault()
{
isEdited = false;
clearInput();
ui->tabBar->setCurrentIndex(LIST_TAB_ID);
}
void DiaryWindow::on_RemoveButton_clicked()
{
if(selectedItem >= 0)
{
database->removeData(selectedItem);
}
ui->EditButton->setEnabled(false);
selectedItem = -1;
}
void DiaryWindow::on_EditButton_clicked()
{
if(selectedItem >= 0)
{
try
{
UserData eData = database->getData(selectedItem);
ui->tabBar->setCurrentIndex(NEW_TAB_ID);
ui->text_field->setPlainText(eData.getText());
ui->text_label->setText(QString(eData.getDate().c_str()));
isEdited = true;
}
catch(std::out_of_range &e)
{
cerr << e.what() << endl;
}
}
ui->EditButton->setEnabled(false);
selectedItem = -1;
}
void DiaryWindow::on_list_clicked(const QModelIndex &index)
{
ui->EditButton->setEnabled(true);
selectedItem = index.row();
}
void DiaryWindow::on_list_doubleClicked(const QModelIndex &index)
{
selectedItem = index.row();
on_EditButton_clicked();
}
void DiaryWindow::on_save_button_clicked()
{
if(isEdited)
{
UserData data(ui->text_label->text().toStdString(), ui->text_field->toPlainText());
if(data.getText().isEmpty())
database->removeData(data);
else
{
database->editData(data);
}
isEdited = false;
clearInput();
ui->tabBar->setCurrentIndex(LIST_TAB_ID);
}
else
{
QString text = ui->text_field->toPlainText();
if(!text.isEmpty())
{
UserData usd(getTimeFormat(), text);
database->addData(usd);
}
clearInput();
ui->tabBar->setCurrentIndex(LIST_TAB_ID);
}
}
void DiaryWindow::on_tabBar_tabBarClicked(int index)
{
if(index == 0)
{
isEdited = false;
clearInput();
}
}