-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainwindow.cpp
534 lines (454 loc) · 17.3 KB
/
mainwindow.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <setpassword.h>
#include "btntextcolor.h"
#include "scribblearea.h"
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QGridLayout>
#include <QPushButton>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QSqlRecord>
#include <QFileDialog>
#include <QPixmap>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include <QColor>
#include <QColorDialog>
#include<QFontDialog>
#include<QFont>
#include<QToolButton>
#include<QDateTime>
#include<QDateTimeEdit>
#include<QPushButton>
#include<QToolButton>
//Ứng dụng ghi chú được hỗ trợ bởi trình tạo giao diện Qt xài ngôn ngữ Qmake với thiên hướng giống ngôn ngữ C++ và sự hỗ trợ của database Sqlite3 ( và 1 số hình ảnh, fonts khác )
//Mỗi một form thiết kế của Qt sẽ cung cấp 3 loại file ( .h, .cpp, .ui ) được liên kết. Trong đó file .ui có format theo XML có thiên hướng giống html, css.. sẽ bao gồm những thành
// phần được đặt tên theo từng nút, khung đặc biệt ( 1 số chưa được đổi tên còn để ở dạng mặc định như "pushbutton", nhưng có chú thích ở phía trên )
QString MainWindow:: pro_user;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Tạo giao diện ban đầu từ dữ liệu trong databse
//Mở database
connOpen();
//Tạo modal...
QSqlQueryModel *modal= new QSqlQueryModel();
QSqlQueryModel *modal1= new QSqlQueryModel();
QSqlQueryModel *modal2= new QSqlQueryModel();
//Tạo query...
QSqlQuery *qry= new QSqlQuery(mydb);
QSqlQuery *qry1= new QSqlQuery(mydb);
QSqlQuery *qry2= new QSqlQuery(mydb);
//Chọn data gắn vào...
qry->prepare("select id from note1");
qry1->prepare("select title from note1");
qry2->prepare("select content from note1");
//Thực hiện query...
qry->exec();
qry1->exec();
qry2->exec();
//Gắn query với modal...
modal->setQuery(*qry);
modal1->setQuery(*qry1);
modal2->setQuery(*qry2);
//Tạo khung danh sách note...
QGridLayout *lay=new QGridLayout(this);
QPushButton *content[2000];
//Thêm các thành phần vào khu vực kéo chuột (scrollArea)
for(int j=0;j<=modal1->rowCount()-1;j++)
{
QString id=modal->record(j).value(0).toString();//(id)
QString title=modal1->record(j).value(0).toString();//(title)
QString contentString=modal2->record(j).value(0).toString();//(content)
//Biến string thành giao diện
content[j]=new QPushButton(title);
content[j]->setProperty("id",id);
QLabel *lab=new QLabel(""+contentString+"...");
lab->setStyleSheet("color:white");
//Chia từng note trong danh sách ra bằng đường kẻ trắng
QFrame *line;
line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
line->setStyleSheet("background:white");
//Nổi hiệu ứng khi rê chuột tới cho nút title
content[j]->setObjectName("btnName_1");
content[j]->setStyleSheet(
" QPushButton#btnName_1 {"
" background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:orange;"
" }"
" QPushButton#btnName_1:hover {"
" color: yellow;font-size:25px;"
" }");
//Gắn giao diện với từng layout đã tạo
lab->setStyleSheet("background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:white");
lay->addWidget(content[j]);
lay->addWidget(lab);
lay->addWidget(line);
//Nhấn title sẽ thực hiện hàm onNameClicked
connect(content[j],SIGNAL(clicked()),this,SLOT(onNameClicked()));
//Bỏ layout vào khu vực kéo (scrollArea)
ui->scrollContents->setLayout(lay);
}
connClose();
//Tắt mở các khung xung quanh
ui->frame_2->hide();//khung note
ui->frame_3->show();//lớp phủ để che note
}
//Hàm hủy
MainWindow::~MainWindow()
{
delete ui;
}
//Gắn x bên header = 0
int MainWindow::x=0;
QPushButton *pButton[50];
QString currentID;
QString contentText;
void MainWindow::refreshNoteList()
{
connOpen();
QSqlQueryModel *modal = new QSqlQueryModel();
QSqlQueryModel *modal1= new QSqlQueryModel();
QSqlQueryModel *modal2= new QSqlQueryModel();
QSqlQuery *qry = new QSqlQuery(mydb);
QSqlQuery *qry1= new QSqlQuery(mydb);
QSqlQuery *qry2= new QSqlQuery(mydb);
qry->prepare("select id from note1");
qry1->prepare("select title from note1");
qry2->prepare("select content from note1");
qry->exec();
qry1->exec();
qry2->exec();
modal->setQuery(*qry);
modal1->setQuery(*qry1);
modal2->setQuery(*qry2);
//Xóa
if ( ui->scrollContents->layout() != NULL )
{
QLayoutItem* item;
while ( ( item = ui->scrollContents->layout()->takeAt( 0 ) ) != NULL )
{
delete item->widget();
delete item;
}
delete ui->scrollContents->layout();
}
//Tạo khung mới
QGridLayout *lay=new QGridLayout(this);
QPushButton *content[2000];
for(int j=0;j<=modal->rowCount()-1;j++)
{
QString id=modal->record(j).value(0).toString();//id
QString title=modal1->record(j).value(0).toString();//(title)
QString contentText=modal2->record(j).value(0).toString();//(content)
content[j]=new QPushButton(title);
content[j]->setProperty("id",id);
QLabel *lab=new QLabel(""+contentText+"...");
lab->setStyleSheet("color:white");
QFrame *line;
line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
line->setStyleSheet("background:white");
content[j]->setObjectName("btnName_1");
content[j]->setStyleSheet(
" QPushButton#btnName_1 {"
" background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:orange;"
" }"
" QPushButton#btnName_1:hover {"
" color: yellow;font-size:25px;"
" }");
lab->setStyleSheet("background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:white");
lay->addWidget(content[j]);
lay->addWidget(lab);
lay->addWidget(line);
connect(content[j],SIGNAL(clicked()),this,SLOT(onNameClicked()));
ui->scrollContents->setLayout(lay);
x=0;
}
}
//Mở note
void MainWindow::onNameClicked()
{
//Hiện khung note
ui->frame_2->show();
//Khởi tạo nút đã tạo
pButton[x]=new QPushButton;
//Nhận tên của nút và gán nó vào pButton[x]
pButton[x] = qobject_cast<QPushButton*>(sender());
//Mỗi lần nhấn nút khác thì nút vừa nhấn trước đó sẽ quay về stylesheet mặc định như bên dưới
if(x>0)
{
pButton[x-1]->setObjectName("btnName_1");
pButton[x-1]->setStyleSheet("QPushButton#btnName_1 {"
" background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:orange;"
" }"
" QPushButton#btnName_1:hover {"
" color: yellow;font-size:25px;"
" }");
} else { pButton[x]->setStyleSheet("color: yellow;font-size:25px;Text-align:left;font-family:century gothic");}
//stylesheet cho nút để nhấn
pButton[x]->setStyleSheet("color: yellow;font-size:25px;Text-align:left;font-family:century gothic");
//Nhận giá trị id đồng thời
QString idText= pButton[x]->property("id").value<QString>();
currentID= idText;
connOpen();
QSqlQuery qry;
//Gọi dữ liệu nội dung note
qry.prepare("select content from note1 where id='"+idText+"'");
if(!qry.exec()) return ;
qry.next();
QString contentText = qry.value(0).toString();
ui->content->setText(contentText);
ui->content->setStyleSheet("font-family:century gothic;background:transparent;font-size:20px;color:orange;Text-align:center");
ui->title->setStyleSheet("font-family:century gothic;background:transparent;font-size:20px;color:orange;Text-align:center");
//Gọi dữ liệu tiêu đề note
qry.prepare("select title from note1 where id='"+idText+"'");
if(!qry.exec()) return ;
qry.next();
QString titleText = qry.value(0).toString();
ui->title->setPlainText(titleText);
x++;
connClose();
}
//Tìm kiếm
void MainWindow::on_search_clicked()
{
connOpen();
QSqlQueryModel *modal= new QSqlQueryModel();
QSqlQueryModel *modal1= new QSqlQueryModel();
QSqlQueryModel *modal2= new QSqlQueryModel();
QSqlQuery *qry= new QSqlQuery(mydb);
QSqlQuery *qry1= new QSqlQuery(mydb);
QSqlQuery *qry2= new QSqlQuery(mydb);
qry->prepare("select id from note1");
qry1->prepare("select title from note1");
qry2->prepare("select content from note1");
qry->exec();
qry1->exec();
qry2->exec();
modal->setQuery(*qry);
modal1->setQuery(*qry1);
modal2->setQuery(*qry2);
QVBoxLayout *lay=new QVBoxLayout(this);
QString s=ui->lineEdit->text();
QPushButton *label;
QGridLayout *lay1=new QGridLayout(this);
QPushButton *content[2000];
for(int j=0;j<=modal1->rowCount();j++)
{
QString id=modal->record(j).value(0).toString();
QString title=modal1->record(j).value(0).toString();
QString contentText=modal2->record(j).value(0).toString();
//Kiểm tra xem note có đang trống hay ko
if(QString(s).isEmpty() ){
content[j]=new QPushButton(contentText);
content[j]->setProperty("id",id);
QLabel *lab=new QLabel("Content: "+contentText+".");
lab->setStyleSheet("color:white");
//Chia từng note trong danh sách bằng đường kẻ trắng
QFrame *line;
line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
line->setStyleSheet("background:white");
//Nổi hiệu ứng khi rê chuột tới cho nút title
content[j]->setObjectName("btnName_1");
content[j]->setStyleSheet(
" QPushButton#btnName_1 {"
" background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:orange;"
" }"
" QPushButton#btnName_1:hover {"
" color: yellow;font-size:25px;"
" }");
//Gắn giao diện với từng layout đã tạo
lab->setStyleSheet("background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:white");
lay1->addWidget(content[j]);
lay1->addWidget(lab);
lay1->addWidget(line);
//Nhấn title sẽ thực hiện hàm onNameClicked ( tạo kết nối signals & slots trong Qt )
connect(content[j],SIGNAL(clicked()),this,SLOT(onNameClicked()));
//Bỏ layout vào khu vực kéo (scrollArea)
ui->scrollContents->setLayout(lay1);
}
int x = QString::compare(s, title, Qt::CaseInsensitive);
//Khớp kết quả
if(x==0)
{
//clearing previous layout
if ( ui->scrollContents->layout() != NULL )
{
QLayoutItem* item;
while ( ( item = ui->scrollContents->layout()->takeAt( 0 ) ) != NULL )
{
delete item->widget();
delete item;
}
delete ui->scrollContents->layout();
}
//Tạo layout mới cho danh sách note
ui->frame_2->hide();
label=new QPushButton(title);
label->setObjectName(title);
QLabel *lab=new QLabel("Content: "+contentText+".");
lab->setStyleSheet("color:white");
QFrame *line=new QFrame;
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
line->setStyleSheet("background:white");
label->setStyleSheet(
" QPushButton {"
" background:transparent; Text-align:left;font-family:century gothic;font-size:18px; color:orange;"
" }"
" QPushButton:hover {"
" color: rgb(224, 255, 0);font-size:25px;"
" }");
lay->addWidget(label);
lay->addWidget(lab);
lay->addWidget(line);
connect(label,SIGNAL(clicked()),this,SLOT(onNameClicked()));
}
}
ui->scrollContents->setLayout(lay);
ui->scrollContents->setStyleSheet("background: transparent;border-color: rgb(62, 62, 62);padding-left:20px;padding-top:20px");
ui->scrollContents->setVisible(true);
ui->scrollContents->show();
}
//Hiển thị khung đặt khẩu để khóa note mình muốn
void MainWindow::on_setPassword_clicked()
{
Setpassword = new setpassword(this);
Setpassword->setGeometry(450,250,300,300);
Setpassword->show();
}
//Hiển thị khung mở khóa note
void MainWindow::on_getPassword_clicked()
{
Getpassword = new getpassword(this);
Getpassword->setGeometry(450,250,300,300);
Getpassword->show();
}
//Thêm note trống mới
void MainWindow::on_add_clicked()
{
QString title = ui->title->toPlainText();
QString content = ui->content->toPlainText();
currentID= "";
ui->title->clear();
ui->content->clear();
ui->frame_2->show();
}
//Lưu note vào database
void MainWindow::on_save_clicked()
{
QString title = ui->title->toPlainText();
QString content = ui->content->toPlainText();
if(!QString(title).isEmpty())
{
connOpen();
QSqlQuery qry;
qry.prepare("update note1 set title ='"+title+"'where id='"+currentID+"'");
if(qry.exec())
{
connClose();
}
}
if(currentID != "")
{
connOpen();
QSqlQuery qry;
qry.prepare("update note1 set content ='"+content+"'where id='"+currentID+"'");
qry.exec();
refreshNoteList();
connClose();
} else
{
connOpen();
QSqlQuery qry;
qry.prepare("INSERT INTO note1 (title, content)" "VALUES (:title, :content)");
qry.bindValue(":title", title);
qry.bindValue(":content", content);
qry.exec();
refreshNoteList();
connClose();
}
}
void MainWindow::on_deleteNote_clicked()
{
connOpen();
QSqlQuery qry;
qry.prepare("delete from note1 where id="+currentID+" ");
qry.exec();
refreshNoteList();
ui->frame_2->hide();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Tạo màu cho chữ
void MainWindow::on_btnTextColor_clicked()
{
QColor color = QColorDialog :: getColor(Qt::black,this);
if (color.isValid())
{
ui->content->setTextColor(color);
}
}
//Quay lại hành động
void MainWindow::on_btnUndo_clicked()
{
ui->content->undo();
}
//Tiến tới hành động
void MainWindow::on_btnRedo_clicked()
{
ui->content->redo();
}
//In đậm
void MainWindow::on_toolButton_bold_clicked()
{
QFont font;
font.setWeight(QFont::ExtraBold); // set font weight with enum QFont::Weight
//font.setPixelSize(13); // this for setting font size
ui->title->setFont(font);
ui->content->setFont(font);
}
//In nghiêng
void MainWindow::on_toolButton_italic_clicked()
{
QFont font;
font.setItalic(QFont::StyleItalic); // set font weight with enum QFont::Weight
//font.setPixelSize(13); // this for setting font size
ui->title->setFont(font);
ui->content->setFont(font);
}
//Gạch dưới
void MainWindow::on_toolButton_underline_clicked()
{
QFont font;
font.setUnderline(QFont::UnderlineResolved); // set font weight with enum QFont::Weight
//font.setPixelSize(13); // this for setting font size
ui->title->setFont(font);
ui->content->setFont(font);
}
//Tạo bullets
void MainWindow::on_toolButton_plus_clicked()
{
QTextDocument* document = ui->content->document();
QTextCursor* cursor = new QTextCursor(document);
QTextListFormat listFormat;
listFormat.setStyle(QTextListFormat::ListDisc);
cursor->insertList(listFormat);
cursor->insertText("");
}
// Tạo khung vẽ
void MainWindow::on_paint_clicked()
{
paintwindow = new PaintWindow(this);
paintwindow->show();
}