-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
168 lines (148 loc) · 5.05 KB
/
mainwindow.cpp
File metadata and controls
168 lines (148 loc) · 5.05 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "mainwindow.h"
#include "noteeditor.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QTreeWidget>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QStatusBar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), centralWidget(new QWidget(this))
{
// UI Setup
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
searchBar = new QLineEdit(this);
noteList = new QTreeWidget(this);
noteList->setHeaderLabels({"Title", "Modified"});
QPushButton *btnNew = new QPushButton("New Note", this);
QPushButton *btnEdit = new QPushButton("Edit Note", this);
QPushButton *btnExport = new QPushButton("Export", this);
// Layout organization
layout->addWidget(searchBar);
layout->addWidget(noteList);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(btnNew);
buttonLayout->addWidget(btnEdit);
buttonLayout->addWidget(btnExport);
layout->addLayout(buttonLayout);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
// Connections
connect(btnNew, &QPushButton::clicked, this, &MainWindow::onNewNote);
connect(btnEdit, &QPushButton::clicked, this, &MainWindow::onEditNote);
connect(btnExport, &QPushButton::clicked, this, &MainWindow::onExport);
connect(searchBar, &QLineEdit::textChanged, this, &MainWindow::onSearch);
loadNotes();
}
MainWindow::~MainWindow()
{
// Cleanup if needed
}
void MainWindow::createNewNote(const QString &initialText)
{
NoteEditor editor(this);
editor.setContent(initialText);
if (editor.exec() == QDialog::Accepted) {
Note note;
note.filename = QString("notes/note_%1.txt").arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"));
note.content = editor.getContent();
note.title = note.content.left(30).replace('\n', ' ');
note.modified = QDateTime::currentDateTime();
note.created = note.modified;
QDir().mkpath("notes");
QFile file(note.filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << note.content;
file.close();
loadNotes();
statusBar()->showMessage("Note saved", 2000);
}
}
}
void MainWindow::loadNotes()
{
noteList->clear();
notes.clear();
QDir dir("notes");
if (!dir.exists()) {
dir.mkpath(".");
return;
}
foreach (QString filename, dir.entryList(QStringList() << "*.txt")) {
Note note;
note.filename = dir.filePath(filename);
QFile file(note.filename);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
note.content = in.readAll();
note.title = filename.left(filename.lastIndexOf('.'));
note.modified = QFileInfo(file).lastModified();
note.created = note.modified;
notes.append(note);
file.close();
}
}
foreach (const Note ¬e, notes) {
QTreeWidgetItem *item = new QTreeWidgetItem(noteList);
item->setText(0, note.title);
item->setText(1, note.modified.toString("MMM d h:mm AP"));
}
}
void MainWindow::onNewNote()
{
createNewNote();
}
void MainWindow::onEditNote()
{
if (noteList->currentItem()) {
int index = noteList->indexOfTopLevelItem(noteList->currentItem());
if (index >= 0 && index < notes.size()) {
NoteEditor editor(this);
editor.setContent(notes[index].content);
if (editor.exec() == QDialog::Accepted) {
notes[index].content = editor.getContent();
notes[index].modified = QDateTime::currentDateTime();
QFile file(notes[index].filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << notes[index].content;
file.close();
loadNotes();
}
}
}
}
}
void MainWindow::onSearch()
{
QString term = searchBar->text().toLower();
noteList->clear();
foreach (const Note ¬e, notes) {
if (note.content.toLower().contains(term) ||
note.title.toLower().contains(term)) {
QTreeWidgetItem *item = new QTreeWidgetItem(noteList);
item->setText(0, note.title);
item->setText(1, note.modified.toString("MMM d h:mm AP"));
}
}
}
void MainWindow::onExport()
{
QString fileName = QFileDialog::getSaveFileName(this, "Export Notes", "", "Text Files (*.txt)");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
foreach (const Note ¬e, notes) {
out << "=== " << note.title << " ===\n"
<< "Created: " << note.created.toString("yyyy-MM-dd hh:mm") << "\n"
<< note.content << "\n\n";
}
}
}
}