-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
1246 lines (1061 loc) · 49 KB
/
Copy pathwidget.cpp
File metadata and controls
1246 lines (1061 loc) · 49 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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "widget.h"
#include "ui_widget.h"
#include "notewidgetdelegate.h"
#include "edit_page.h"
#define FIRST_LINE_MAX 80
int sink = 0;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
//, m_notebook(new Edit_page(this))
, m_autoSaveTimer(new QTimer(this))
, m_settingsDatabase(Q_NULLPTR)
, m_ukui_SearchLine(Q_NULLPTR)
, m_newKynote(Q_NULLPTR)
, m_trashButton(Q_NULLPTR)
, m_countLabel(Q_NULLPTR)
, m_sortLabel(Q_NULLPTR)
, m_noteView(Q_NULLPTR)
, m_noteModel(new NoteModel(this))
, m_deletedNotesModel(new NoteModel(this))
, m_proxyModel(new QSortFilterProxyModel(this))
, m_dbManager(Q_NULLPTR)
, m_dbThread(Q_NULLPTR)
, m_isContentModified(false)
, m_isColorModified(false)
, m_isOperationRunning(false)
, m_isTemp(false)
{
ui->setupUi(this);
setupDatabases();
setupModelView();
ukui_init();
ukui_conn();
QTimer::singleShot(200,this, SLOT(InitData()));
setAttribute(Qt::WA_TranslucentBackground);
black_show();
sourch_Icon();
dack_wight_flag = -1;
ui->set_btn->hide();
ui->change_page_btn->hide();
ui->add_more_btn->move(575, 0);
ui->frame->hide();
//ui->widget->setStyleSheet("QWidget{background-color: rgba(0, 0, 0, 0.2);}");
}
Widget::~Widget()
{
for (auto it = m_editors.begin(); it!= m_editors.end();it++) {
delete *it;
qDebug()<<"aa-----------------------";
}
m_editors.clear();
delete ui;
m_dbThread->quit();
m_dbThread->wait();
delete m_dbThread;
}
// 初始化数据库中的数据并选中第一个便签(如果有)
void Widget::InitData()
{
QFileInfo fi(m_settingsDatabase->fileName());
QDir dir(fi.absolutePath());
QString oldNoteDBPath(dir.path() + QStringLiteral("/Notes.ini"));
QString oldTrashDBPath(dir.path() + QStringLiteral("/Trash.ini"));
bool exist = (QFile::exists(oldNoteDBPath) || QFile::exists(oldTrashDBPath));
if(exist){
QProgressDialog* pd = new QProgressDialog(QStringLiteral("Migrating database, please wait."), QString(), 0, 0, this);
pd->setCancelButton(Q_NULLPTR);
pd->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
pd->setMinimumDuration(0);
pd->show();
//setButtonsAndFieldsEnabled(false);
QFutureWatcher<void>* watcher = new QFutureWatcher<void>(this);
connect(watcher, &QFutureWatcher<void>::finished, this, [&, pd](){
pd->deleteLater();
//setButtonsAndFieldsEnabled(true);
emit requestNotesList();
});
QFuture<void> migration = QtConcurrent::run(this, &Widget::checkMigration);
watcher->setFuture(migration);
} else {
emit requestNotesList();
}
/// Check if it is running with an argument (ex. hide)
if (qApp->arguments().contains(QStringLiteral("--autostart"))) {
//setMainWindowVisibility(false);
}
}
void Widget::setupModelView()
{
m_noteView = static_cast<NoteView*>(ui->listView);
m_proxyModel->setSourceModel(m_noteModel); //代理真正的数据模型,对数据进行排序和过滤
m_proxyModel->setFilterKeyColumn(0); //此属性保存用于读取源模型内容的键的列,listview只有一列所以是0
m_proxyModel->setFilterRole(NoteModel::NoteContent);//此属性保留项目角色,该角色用于在过滤项目时查询源模型的数据
m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);//
m_noteView->setItemDelegate(new NoteWidgetDelegate(m_noteView)); //安装定制delegate提供编辑功能
m_noteView->setModel(m_proxyModel);//设置view的model是proxyModel,proxyModel作为view和QAbstractListModel的桥梁
}
void Widget::initializeSettingsDatabase()
{
if(m_settingsDatabase->value(QStringLiteral("version"), "NULL") == "NULL")
m_settingsDatabase->setValue(QStringLiteral("version"), qApp->applicationVersion());
if(m_settingsDatabase->value(QStringLiteral("windowGeometry"), "NULL") == "NULL"){
int initWidth = 704;
int initHeight = 590;
QPoint center = qApp->desktop()->geometry().center();
QRect rect(center.x() - initWidth/2, center.y() - initHeight/2, initWidth, initHeight);
setGeometry(rect); //设置窗口居中
m_settingsDatabase->setValue(QStringLiteral("windowGeometry"), saveGeometry()); //保存窗口的几何形状
}
// if(m_settingsDatabase->value(QStringLiteral("splitterSizes"), "NULL") == "NULL"){
// m_splitter->resize(width()-2*m_layoutMargin, height()-2*m_layoutMargin);
// QList<int> sizes = m_splitter->sizes();
// m_noteListWidth = ui->frameLeft->minimumWidth() != 0 ? ui->frameLeft->minimumWidth() : m_noteListWidth;
// sizes[0] = m_noteListWidth;
// sizes[1] = m_splitter->width() - m_noteListWidth;
// m_splitter->setSizes(sizes);
// m_settingsDatabase->setValue(QStringLiteral("splitterSizes"), m_splitter->saveState());
// }
}
void Widget::setupDatabases()
{
//QSettings::IniFormat 将设置存储在INI文件中。从INI文件读取设置时不会保留类型信息。所有值将作为QString返回
//QSettings::UserScope 将设置存储在特定于当前用户的位置
m_settingsDatabase = new QSettings(QSettings::IniFormat, QSettings::UserScope,
QStringLiteral("kylin-note"), QStringLiteral("Settings"), this);
m_settingsDatabase->setFallbacksEnabled(false); //禁用回退
initializeSettingsDatabase();
bool doCreate = false;
QFileInfo fi(m_settingsDatabase->fileName());
QDir dir(fi.absolutePath());
bool folderCreated = dir.mkpath(QStringLiteral("."));
if(!folderCreated)
qFatal("ERROR: Can't create settings folder : %s", dir.absolutePath().toStdString().c_str());
QString noteDBFilePath(dir.path() + QDir::separator() + QStringLiteral("kyNotes.db"));
if(!QFile::exists(noteDBFilePath)){
QFile noteDBFile(noteDBFilePath);
if(!noteDBFile.open(QIODevice::WriteOnly))
qFatal("ERROR : Can't create database file");
noteDBFile.close();
doCreate = true;
}
m_dbManager = new DBManager;
m_dbThread = new QThread;
m_dbThread->setObjectName(QStringLiteral("dbThread"));
m_dbManager->moveToThread(m_dbThread);
connect(m_dbThread, &QThread::started, [=](){emit requestOpenDBManager(noteDBFilePath, doCreate);});
connect(this, &Widget::requestOpenDBManager, m_dbManager, &DBManager::onOpenDBManagerRequested);
connect(m_dbThread, &QThread::finished, m_dbManager, &QObject::deleteLater);
m_dbThread->start();
}
void Widget::error_throw()
{
try
{
MY_THROW(ExceptionDerived,"error throw");
}
catch(ExceptionDerived &e)
{
std::cout << e.what() << std::endl;
}
}
void Widget::ukui_init()
{
sortflag = 1;
m_ukui_SearchLine = ui->SearchLine;
m_newKynote = ui->newKynote;
m_trashButton = ui->add_more_btn;
m_countLabel = ui->label;
m_sortLabel = ui->sort_btn;
ui->listView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// m_trashButton->setToolTip(QStringLiteral("Delete Selected Note"));
// m_newKynote->setToolTip(QStringLiteral("Create New Note"));
// //窗口属性
// setWindowFlags(Qt::FramelessWindowHint);//开启窗口无边框
// setWindowOpacity(0.8);//窗口透明度
//弹出位置
QDesktopWidget *desktop = QApplication::desktop();
move((desktop->width() - this->width())/2, (desktop->height() - this->height())/2);
// //组件属性
// //ui->listWidget->setAttribute(Qt::WA_TranslucentBackground);//设置透明度
// //ui->newKynote->setAttribute(Qt::WA_TranslucentBackground);
//标题
this->setWindowTitle(tr("ukui-note"));
//任务栏图标
setWindowIcon(QIcon(":/image/kylin-notebook.svg"));
// //按钮
// ui->newKynote->setStyleSheet(tristateButton(QPushButton,:/new/prefix1/SVG/new-b));
// ui->pushButton_Mini->setStyleSheet(tristateButton(QPushButton,:/new/prefix1/SVG/dark_theme/min));
// //ui->pushButton_Exit->setStyleSheet(tristateButton(QPushButton,:/new/prefix1/SVG/dark_theme/close));
QBitmap bmp(this->size());
bmp.fill();
QPainter p(&bmp);
p.setPen(Qt::NoPen);
p.setBrush(Qt::black);
p.setRenderHint(QPainter::Antialiasing);
p.drawRoundedRect(bmp.rect(),6,6);
setMask(bmp);
//setWindowOpacity(0.9);
setWindowFlags(Qt::FramelessWindowHint);
//搜索框
ui->SearchLine->setPlaceholderText(tr("Search"));//设置详细输入框的提示信息
set_table_list_page_attribute();
set_all_btn_attribute();
ui->tableView->hide();
listflag = 1;
}
void Widget::ukui_conn()
{
qDebug() << "conn";
//主界面退出按钮
connect(ui->pushButton_Exit,SIGNAL(clicked()),this,SLOT(exitSlot()));
//主界面最小化按钮
connect(ui->pushButton_Mini,SIGNAL(clicked()),this,SLOT(miniSlot()));
//新建按钮
connect(m_newKynote,&QPushButton::clicked, this, &Widget::newSlot);
//删除按钮
connect(m_trashButton, &QPushButton::clicked, this, &Widget::onTrashButtonClicked);
//升/降序按钮
connect(m_sortLabel,&QPushButton::clicked,this,&Widget::sortSlot);
//搜索栏文本输入
connect(m_ukui_SearchLine, &QLineEdit::textChanged, this, &Widget::onSearchEditTextChanged);
//listview单击事件
connect(m_noteView, &NoteView::pressed, this, &Widget::onNotePressed);
//listview双击事件
connect(m_noteView,&NoteView::doubleClicked,this,&Widget::listDoubleClickSlot);
// noteView viewport pressed
connect(m_noteView, &NoteView::viewportPressed, this, [this](){
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
if(m_isTemp && m_proxyModel->rowCount() > 1){
QModelIndex indexInProxy = m_proxyModel->index(1, 0);
selectNote(indexInProxy);
}else if(m_isTemp && m_proxyModel->rowCount() == 1){
QModelIndex indexInProxy = m_proxyModel->index(0, 0);
deleteNote(indexInProxy, false);
}
});
//connect(ui->ukui_SearchLine,&QLineEdit::textChanged,this,&Widget::lineeditChangedSlot);
// connect(ui->searchClearButton,&QPushButton::clicked,this,[=]{
// ui->ukui_SearchLine->setText("");
// });
// auto save timer
connect(m_autoSaveTimer, &QTimer::timeout, [this](){
m_autoSaveTimer->stop();
saveNoteToDB(m_currentSelectedNoteProxy);
});
//指定传递信号的方式Qt::BlockingQueuedConnection
//这种类型类似于QueuedConnection,但是它只能应用于跨线程操作即发送者和接收者处于不同的线程中的情况
//并且信号发送者线程会阻塞等待接收者的槽函数执行结束
connect(this, &Widget::requestNotesList,
m_dbManager,&DBManager::onNotesListRequested, Qt::BlockingQueuedConnection);
connect(this, &Widget::requestCreateUpdateNote,
m_dbManager, &DBManager::onCreateUpdateRequested, Qt::BlockingQueuedConnection);
connect(this, &Widget::requestDeleteNote,
m_dbManager, &DBManager::onDeleteNoteRequested);
// connect(this, &Widget::requestRestoreNotes,
// m_dbManager, &DBManager::onRestoreNotesRequested, Qt::BlockingQueuedConnection);
// connect(this, &Widget::requestImportNotes,
// m_dbManager, &DBManager::onImportNotesRequested, Qt::BlockingQueuedConnection);
// connect(this, &Widget::requestExportNotes,
// m_dbManager, &DBManager::onExportNotesRequested, Qt::BlockingQueuedConnection);
connect(this, &Widget::requestMigrateNotes,
m_dbManager, &DBManager::onMigrateNotesRequested, Qt::BlockingQueuedConnection);
connect(this, &Widget::requestMigrateTrash,
m_dbManager, &DBManager::onMigrateTrashRequested, Qt::BlockingQueuedConnection);
connect(this, &Widget::requestForceLastRowIndexValue,
m_dbManager, &DBManager::onForceLastRowIndexValueRequested, Qt::BlockingQueuedConnection);
connect(m_dbManager, &DBManager::notesReceived, this, &Widget::loadNotes);
}
void Widget::checkMigration()
{
QFileInfo fi(m_settingsDatabase->fileName());
QDir dir(fi.absolutePath());
QString oldNoteDBPath(dir.path() + QDir::separator() + "Notes.ini");
if(QFile::exists(oldNoteDBPath))
migrateNote(oldNoteDBPath);
QString oldTrashDBPath(dir.path() + QDir::separator() + "Trash.ini");
if(QFile::exists(oldTrashDBPath))
//migrateTrash(oldTrashDBPath);
emit requestForceLastRowIndexValue(m_noteCounter);
}
void Widget::migrateNote(QString notePath)
{
QSettings notesIni(notePath, QSettings::IniFormat);
QStringList dbKeys = notesIni.allKeys();
m_noteCounter = notesIni.value(QStringLiteral("notesCounter"), "0").toInt();
QList<NoteData *> noteList;
auto it = dbKeys.begin();
for(; it < dbKeys.end()-1; it += 3){
QString noteName = it->split(QStringLiteral("/"))[0];
int id = noteName.split(QStringLiteral("_"))[1].toInt();
// sync db index with biggest notes id
m_noteCounter = m_noteCounter < id ? id : m_noteCounter;
NoteData* newNote = new NoteData();
newNote->setId(id);
QString createdDateDB = notesIni.value(noteName + QStringLiteral("/dateCreated"), "Error").toString();
newNote->setCreationDateTime(QDateTime::fromString(createdDateDB, Qt::ISODate));
QString lastEditedDateDB = notesIni.value(noteName + QStringLiteral("/dateEdited"), "Error").toString();
newNote->setLastModificationDateTime(QDateTime::fromString(lastEditedDateDB, Qt::ISODate));
QString contentText = notesIni.value(noteName + QStringLiteral("/content"), "Error").toString();
newNote->setContent(contentText);
QString firstLine = getFirstLine(contentText);
newNote->setFullTitle(firstLine);
noteList.append(newNote);
}
if(!noteList.isEmpty())
emit requestMigrateNotes(noteList);
QFile oldNoteDBFile(notePath);
oldNoteDBFile.rename(QFileInfo(notePath).dir().path() + QDir::separator() + QStringLiteral("oldNotes.ini"));
}
void Widget::set_all_btn_attribute()
{
pixmap1 = QPixmap(":/image/1x/new.png");
pixmap2 = QPixmap(":/image/1x/close_light.png");
pixmap3 = QPixmap(":/image/1x/mini_light.png");
pixmap4 = QPixmap(":/image/1x/more_light.png");
pixmap5 = QPixmap(":/image/1x/table.png");
// pixmap6 = QPixmap(":/image/1x/ Insert_multiple_box .png");
pixmap6 = QPixmap(":/image/1x/delete.png");
pixmap7 = QPixmap(":/image/1x/Symbol.png");
pixmap8 = QPixmap(":/image/1x/array.png");
// pixmap9 = QPixmap(":/image/1x/go-bottom-symbolic.png");
pixmap9 = QPixmap(":/image/1x/skin.png");
pixmap10 = QPixmap(":/image/1x/close_block.png");
pixmap11 = QPixmap(":/image/1x/mini_block.png");
pixmap12 = QPixmap(":/image/1x/more_block.png");
pixmap13 = QPixmap(":/image/1x/mini2.png");
pixmap14 = QPixmap(":/image/1x/mini3.png");
pixmap15 = QPixmap(":/image/1x/close2.png");
pixmap16 = QPixmap(":/image/1x/close3.png");
ui->newKynote->setIcon(pixmap1);
ui->pushButton_Exit->setIcon(pixmap10);
ui->pushButton_Mini->setIcon(pixmap11);
ui->set_btn->setIcon(pixmap12);
ui->change_page_btn->setIcon(pixmap5);
ui->add_more_btn->setIcon(pixmap6);
ui->add_more_btn->setIconSize(QSize(36,36));
ui->sort_btn->setIcon(pixmap8);
ui->sort_btn->setIconSize(QSize(36,36));
ui->sort_2_btn->setIcon(pixmap9);
ui->sort_2_btn->setIconSize(QSize(36,36));
ui->newKynote->setToolTip(tr("Create New Note"));
ui->add_more_btn->setToolTip(tr("Delete Selected Note"));
ui->sort_btn->setToolTip(tr("Sort"));
ui->sort_2_btn->setToolTip(tr("Switching Themes"));
ui->pushButton_Exit->setToolTip(tr("Exit"));
ui->pushButton_Mini->setToolTip(tr("Mini"));
}
void Widget::set_table_list_page_attribute()
{
}
void Widget::set_tablewidget()
{
}
void Widget::deleteNote(const QModelIndex ¬eIndex, bool isFromUser)
{
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
if(noteIndex.isValid()){
// delete from model
QModelIndex indexToBeRemoved = m_proxyModel->mapToSource(m_currentSelectedNoteProxy);
NoteData* noteTobeRemoved = m_noteModel->removeNote(indexToBeRemoved);
if(m_isTemp){
m_isTemp = false;
--m_noteCounter;
}else{
noteTobeRemoved->setDeletionDateTime(QDateTime::currentDateTime());
qDebug() << "emit requestDeleteNote";
emit requestDeleteNote(noteTobeRemoved);
}
if(isFromUser){
// clear text edit and time date label
// m_textEdit->blockSignals(true);
// m_textEdit->clear();
// m_textEdit->clearFocus();
// m_textEdit->blockSignals(false);
if(m_noteModel->rowCount() > 0){
QModelIndex index = m_noteView->currentIndex();
m_currentSelectedNoteProxy = index;
}else{
m_currentSelectedNoteProxy = QModelIndex();
}
}
}else{
qDebug() << "Widget::deleteNote noteIndex is not valid";
}
m_noteView->setFocus();
}
void Widget::deleteSelectedNote()
{
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
if(!m_isOperationRunning){
m_isOperationRunning = true;
if(m_currentSelectedNoteProxy.isValid()){
// update the index of the selected note before searching
if(!m_ukui_SearchLine->text().isEmpty()){
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
QModelIndex currentIndexInSource = m_proxyModel->mapToSource(m_currentSelectedNoteProxy);
int beforeSearchSelectedRow = m_selectedNoteBeforeSearchingInSource.row();
if(currentIndexInSource.row() < beforeSearchSelectedRow){
m_selectedNoteBeforeSearchingInSource = m_noteModel->index(beforeSearchSelectedRow-1);
}
}
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
qDebug() << m_currentSelectedNoteProxy;
deleteNote(m_currentSelectedNoteProxy, true);
}
m_isOperationRunning = false;
}
}
void Widget::onNotePressed(const QModelIndex& index)
{
return;
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
if(sender() != Q_NULLPTR){
QModelIndex indexInProxy = m_proxyModel->index(index.row(), 0);
if(indexInProxy.isValid()){
// save the position of text edit scrollbar
if(!m_isTemp && m_currentSelectedNoteProxy.isValid()){
//int pos = m_textEdit->verticalScrollBar()->value();
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
qDebug() << m_currentSelectedNoteProxy;
QModelIndex indexSrc = m_proxyModel->mapToSource(m_currentSelectedNoteProxy);
//m_noteModel->setData(indexSrc, QVariant::fromValue(pos), NoteModel::NoteScrollbarPos);
}
if(m_isTemp && indexInProxy.row() != 0){
// delete the unmodified new note
//deleteNote(m_currentSelectedNoteProxy, false);
//m_currentSelectedNoteProxy = m_proxyModel->index(indexInProxy.row()-1, 0);
}else if(!m_isTemp
&& m_currentSelectedNoteProxy.isValid()
&& indexInProxy != m_currentSelectedNoteProxy
&& m_isContentModified){
// save if the previous selected note was modified
saveNoteToDB(m_currentSelectedNoteProxy);
m_currentSelectedNoteProxy = indexInProxy;
}else{
m_currentSelectedNoteProxy = indexInProxy;
}
m_noteView->selectionModel()->select(m_currentSelectedNoteProxy, QItemSelectionModel::ClearAndSelect);
m_noteView->setCurrentIndex(m_currentSelectedNoteProxy);
m_noteView->scrollTo(m_currentSelectedNoteProxy);
}else{
qDebug() << "Widget::selectNote() : indexInProxy is not valid";
}
m_noteView->setCurrentRowActive(false);
}
}
//双击选中笔记
void Widget::selectNote(const QModelIndex ¬eIndex)
{
QModelIndex indexSrc = m_proxyModel->mapToSource(noteIndex);
showNoteInEditor(indexSrc);
m_currentSelectedNoteProxy = noteIndex;
return;
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
qDebug() << noteIndex;
if(noteIndex.isValid()){
// save the position of text edit scrollbar
if(!m_isTemp && m_currentSelectedNoteProxy.isValid()){
//int pos = m_textEdit->verticalScrollBar()->value();
QModelIndex indexSrc = m_proxyModel->mapToSource(m_currentSelectedNoteProxy);
//m_noteModel->setData(indexSrc, QVariant::fromValue(pos), NoteModel::NoteScrollbarPos);
}
// show the content of the pressed note in the text editor
showNoteInEditor(noteIndex);
if(m_isTemp && noteIndex.row() != 0){
// delete the unmodified new note
// deleteNote(m_currentSelectedNoteProxy, false);
// m_currentSelectedNoteProxy = m_proxyModel->index(noteIndex.row()-1, 0);
}else if(!m_isTemp
&& m_currentSelectedNoteProxy.isValid()
&& noteIndex != m_currentSelectedNoteProxy
&& m_isContentModified){
// save if the previous selected note was modified
saveNoteToDB(m_currentSelectedNoteProxy);
m_currentSelectedNoteProxy = noteIndex;
}else{
m_currentSelectedNoteProxy = noteIndex;
}
m_noteView->selectionModel()->select(m_currentSelectedNoteProxy, QItemSelectionModel::ClearAndSelect);
m_noteView->setCurrentIndex(m_currentSelectedNoteProxy);
m_noteView->scrollTo(m_currentSelectedNoteProxy);
}else{
qDebug() << "Widget::selectNote() : noteIndex is not valid";
}
}
void Widget::showNoteInEditor(const QModelIndex ¬eIndex)
{
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
//m_textEdit->blockSignals(true);
/// fixing bug #202
//m_textEdit->setTextBackgroundColor(QColor(255,255,255, 0));
QString content = noteIndex.data(NoteModel::NoteContent).toString();
QDateTime dateTime = noteIndex.data(NoteModel::NoteLastModificationDateTime).toDateTime();
int scrollbarPos = noteIndex.data(NoteModel::NoteScrollbarPos).toInt();
int noteColor = noteIndex.data(NoteModel::NoteColor).toInt();
const NoteWidgetDelegate delegate;
QColor m_color = delegate.intToQcolor(noteColor);
// set text and date
m_notebook->ui->textEdit->setText(content);
m_notebook->caitou->color_widget = QColor(m_color);
m_notebook->update();
QString noteDate = dateTime.toString(Qt::ISODate);
QString noteDateEditor = getNoteDateEditor(noteDate);
// set scrollbar position
//m_textEdit->verticalScrollBar()->setValue(scrollbarPos);
//m_textEdit->blockSignals(false);
//highlightSearch();
}
void Widget::selectFirstNote()
{
if(m_proxyModel->rowCount() > 0){
QModelIndex index = m_proxyModel->index(0,0);
m_noteView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
m_noteView->setCurrentIndex(index);
m_currentSelectedNoteProxy = index;
}
}
void Widget::createNewNoteIfEmpty()
{
if(m_proxyModel->rowCount() == 0)
createNewNote();
}
void Widget::loadNotes(QList<NoteData *> noteList, int noteCounter)
{
//排序
if(!noteList.isEmpty()){
m_noteModel->addListNote(noteList);
//Qt::AscendingOrder 升序排序
//参见 NoteModel::sort
m_noteModel->sort(0,Qt::AscendingOrder);
}
m_noteCounter = noteCounter;
// TODO: move this from here
createNewNoteIfEmpty();
selectFirstNote();
m_countLabel->setText(QObject::tr("%1 records in total").arg(m_proxyModel->rowCount()));
}
void Widget::moveNoteToTop()
{
// check if the current note is note on the top of the list
// if true move the note to the top
if(m_currentSelectedNoteProxy.isValid()){
m_noteView->scrollToTop();
// move the current selected note to the top
//当前要移动到顶端的item QSortFilterProxyModel
QModelIndex sourceIndex = m_proxyModel->mapToSource(m_currentSelectedNoteProxy);
//目前顶端的item QAbstractListModel
QModelIndex destinationIndex = m_noteModel->index(0);
//将 sourceIndex.row() 移动到第0行,第0行变第一行
m_noteModel->moveRow(sourceIndex, sourceIndex.row(), destinationIndex, 0);
// 更新当前 最顶端QAbstractListModel item 并添加代理
m_currentSelectedNoteProxy = m_proxyModel->mapFromSource(destinationIndex);
//修改当前选中
m_noteView->setCurrentIndex(m_currentSelectedNoteProxy);
}else{
qDebug() << "Widget::moveNoteTop : m_currentSelectedNoteProxy not valid";
}
}
QString Widget::getFirstLine(const QString& str)
{
if(str.simplified().isEmpty())
return "New Note";
QString text = str.trimmed();
QTextStream ts(&text);
return ts.readLine(FIRST_LINE_MAX);
}
void Widget::findNotesContain(const QString& keyword)
{
//将用于过滤源模型内容的固定字符串设置为给定模式
m_proxyModel->setFilterFixedString(keyword);
//m_clearButton->show();
//如果匹配到不止一行
if(m_proxyModel->rowCount() > 0){
selectFirstNote();
}else{
m_currentSelectedNoteProxy = QModelIndex();
}
}
void Widget::clearSearch()
{
m_noteView->setFocusPolicy(Qt::StrongFocus);
m_ukui_SearchLine->blockSignals(true);
m_ukui_SearchLine->clear();
m_ukui_SearchLine->blockSignals(false);
m_proxyModel->setFilterFixedString(QString());
//m_clearButton->hide();
m_ukui_SearchLine->setFocus();
}
void Widget::paintEvent(QPaintEvent *event)
{
// QPainter painter(this);
// painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
// painter.setBrush(QBrush(QColor(14, 19, 22)));
// painter.setPen(Qt::transparent);
// QRect rect = this->rect();
// rect.setWidth(rect.width() - 0);
// rect.setHeight(rect.height() - 0);
// painter.drawRoundedRect(rect, 7, 7);
// //也可用QPainterPath 绘制代替 painter.drawRoundedRect(rect, 15, 15);
// {
// QPainterPath painterPath;
// painterPath.addRoundedRect(rect, 7, 7);
// painter.drawPath(painterPath);
// }
// QWidget::paintEvent(event);
}
//********************Slots************************//
void Widget::onTextEditTextChanged(const QModelIndex &index,int i)
{
qDebug() << "receive signal textchange";
qDebug() << index;
if(index.isValid()){
//m_notebook->ui->textEdit->blockSignals(true);
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
QString content = index.data(NoteModel::NoteContent).toString();
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
if(m_editors[i]->ui->textEdit->toPlainText() != content){
// move note to the top of the list
qDebug() << "########"<< index;
QModelIndex sourceIndex = m_proxyModel->mapFromSource(index);
// qDebug() << "!!!!!!!!" << sourceIndex;
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
qDebug() << index.row(); //0
//bool b = ui->SearchLine->text().isEmpty();
// qDebug() << sourceIndex.row(); //0
// if(index.row() != 0){
//moveNoteToTop();
// }else if(!ui->SearchLine->text().isEmpty() && sourceIndex.row() != 0){
//m_noteView->setAnimationEnabled(false);
//moveNoteToTop();
//m_noteView->setAnimationEnabled(true);
// }
// Get the new data
QString firstline = getFirstLine(m_editors[i]->ui->textEdit->toPlainText());
QDateTime dateTime = QDateTime::currentDateTime();
QString noteDate = dateTime.toString(Qt::ISODate);
// update model
QMap<int, QVariant> dataValue;
dataValue[NoteModel::NoteContent] = QVariant::fromValue(m_editors[i]->ui->textEdit->toPlainText());
dataValue[NoteModel::NoteFullTitle] = QVariant::fromValue(firstline);
dataValue[NoteModel::NoteLastModificationDateTime] = QVariant::fromValue(dateTime);
qDebug()<<"now change text is"<< firstline;
QModelIndex index2 = m_proxyModel->mapToSource(index);
m_noteModel->setItemData(index, dataValue);
m_isContentModified = true;
//m_autoSaveTimer->start(500);
saveNoteToDB(index);
}
//m_notebook->ui->textEdit->blockSignals(false);
//获取当前调色板颜色
//当前调色
m_isTemp = false;
}else{
qDebug() << "Widget::onTextEditTextChanged() : m_currentSelectedNoteProxy is not valid";
}
}
void Widget::onColorChanged(const QColor &color)
{
qDebug() << "receive signal onColorChanged";
if(m_currentSelectedNoteProxy.isValid()){
const NoteWidgetDelegate delegate;
int m_color = delegate.qcolorToInt(color);
qDebug () << "m_color" << m_color;
QMap<int, QVariant> dataValue;
dataValue[NoteModel::NoteColor] = QVariant::fromValue(m_color);
QModelIndex index = m_proxyModel->mapToSource(m_currentSelectedNoteProxy);
m_noteModel->setItemData(index, dataValue);
qDebug() << "m_currentSelectedNoteProxy" << m_currentSelectedNoteProxy.data(NoteModel::NoteColor).toInt();
m_isColorModified = true;
m_autoSaveTimer->start(500);
}
}
void Widget::exitSlot(){
tuichu = new tanchuang(this);
tuichu->show();
if(tuichu->close_flage)
{
this->close();
}
}
void Widget::miniSlot()
{
this->showMinimized();
}
void Widget::editSlot()
{
qDebug() << "edit";
}
//将当前便笺保存到数据库
void Widget::saveNoteToDB(const QModelIndex& noteIndex)
{ //如果实例变量 noteIndex 是个有效对象 &&
if(noteIndex.isValid() && m_isContentModified){
//从排序过滤器模型返回与给定 noteIndex 对应的源模型索引。
QModelIndex indexInSrc = m_proxyModel->mapToSource(noteIndex);
NoteData* note = m_noteModel->getNote(noteIndex);
if(note != Q_NULLPTR)
emit requestCreateUpdateNote(note);
m_isContentModified = false;
}else if(noteIndex.isValid() && m_isColorModified)
{
//从排序过滤器模型返回与给定 noteIndex 对应的源模型索引。
QModelIndex indexInSrc = m_proxyModel->mapToSource(noteIndex);
NoteData* note = m_noteModel->getNote(noteIndex);
if(note != Q_NULLPTR)
emit requestCreateUpdateNote(note);
m_isColorModified = false;
}
}
QDateTime Widget::getQDateTime(QString date)
{
QDateTime dateTime = QDateTime::fromString(date, Qt::ISODate);
return dateTime;
}
QString Widget::getNoteDateEditor(QString dateEdited)
{
QDateTime dateTimeEdited(getQDateTime(dateEdited));
QLocale usLocale = QLocale::system();
return usLocale.toString(dateTimeEdited, QStringLiteral("yyyy/MM/dd hh:mm"));
}
//初始化一个笔记
NoteData* Widget::generateNote(const int noteID)
{
NoteData* newNote = new NoteData(this);
newNote->setId(noteID);
QDateTime noteDate = QDateTime::currentDateTime();
newNote->setCreationDateTime(noteDate);
newNote->setLastModificationDateTime(noteDate);
newNote->setFullTitle(QStringLiteral("New Note"));
return newNote;
}
/*
* create a new note
* add it to the database
* add it to the scrollArea
*/
void Widget::createNewNote()
{
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
qDebug() << m_isOperationRunning;
if(!m_isOperationRunning){
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
m_isOperationRunning = true;
m_noteView->scrollToTop();
// clear the textEdit
//ukui_notebook->m_textEdit->blockSignals(true);
//ukui_notebook->m_textEdit->clear();
//ukui_notebook->m_textEdit->setFocus();
//ukui_notebook->m_textEdit->blockSignals(false);
if(!m_isTemp){
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
++m_noteCounter;
NoteData* tmpNote = generateNote(m_noteCounter);
m_isTemp = true;
// insert the new note to NoteModel
QModelIndex indexSrc = m_noteModel->insertNote(tmpNote, m_noteCounter-1);
// update the editor header date label
QString dateTimeFromDB = tmpNote->lastModificationdateTime().toString(Qt::ISODate);
QString dateTimeForEditor = getNoteDateEditor(dateTimeFromDB);
// 从排序过滤器模型返回与给定 indexSrc 对应的源模型索引。
m_currentSelectedNoteProxy = m_proxyModel->mapFromSource(indexSrc);
}else{
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
int row = m_currentSelectedNoteProxy.row();
m_noteView->animateAddedRow(QModelIndex(),row, row);
}
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
//设置索引 m_currentSelectedNoteProxy 所在的页面为当前页面
m_noteView->setCurrentIndex(m_currentSelectedNoteProxy);
m_isOperationRunning = false;
}
}
void Widget::newSlot()
{
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
//新建一个笔记本
//m_notebook = new Edit_page(this);
//m_notebook->show();
//m_notebook->ui->textEdit->setFocus();
//如果搜索栏有内容,则在新建便签时清空
if(!m_ukui_SearchLine->text().isEmpty())
{
clearSearch();
m_selectedNoteBeforeSearchingInSource = QModelIndex();
}
// save the data of the previous selected
if(m_currentSelectedNoteProxy.isValid()
&& m_isContentModified){
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
qDebug() << m_currentSelectedNoteProxy;
saveNoteToDB(m_currentSelectedNoteProxy);
m_isContentModified = false;
}
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
this->createNewNote();
qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
m_countLabel->setText(QObject::tr("%1 records in total").arg(m_proxyModel->rowCount()));
// connect(m_notebook,SIGNAL(texthasChanged()), this,SLOT(onTextEditTextChanged()));
// connect(m_notebook,SIGNAL(colorhasChanged(QColor)),this,SLOT(onColorChanged(QColor)));
}
void Widget::onTrashButtonClicked()
{
m_trashButton->blockSignals(true);
deleteSelectedNote();
m_trashButton->blockSignals(false);
m_countLabel->setText(QObject::tr("%1 records in total").arg(m_proxyModel->rowCount()));
}
void Widget::listClickSlot()
{
// int listnum = ui->listWidget->currentRow();
// rowNum = sqlModel->rowCount();
// for (int i = 0;i < rowNum; i++)
// {
// qDebug() << rowNum;
// singleItem[i]->pushButtonDel->hide();
// }
// qDebug() << "listClickSLot rowNum = " << listnum;
// singleItem[listnum]->pushButtonDel->show();
}
//* 在滚动区域中单击便笺时:
//* 取消突出显示上一个选定的便笺
//* 如果在临时便笺存在时选择便笺,请删除临时便笺
//* 突出显示所选便笺
//* 将所选便笺内容加载到textedit
void Widget::listDoubleClickSlot(const QModelIndex& index)
{
//QModelIndex * p= const_cast<QModelIndex*>(&index);
qDebug() << "listDoubleClickSlot(const QModelIndex& index)" << index;
QModelIndex sourceIndex = m_proxyModel->mapToSource(index);
qDebug() << "!!!!!!!!listDoubleClickSlot" << sourceIndex;
m_notebook = new Edit_page(this,sourceIndex);
m_editors.push_back(m_notebook);
m_notebook->id = m_editors.size() - 1;
if(sender() != Q_NULLPTR){
//获取当前选中item下标
QModelIndex indexInProxy = m_proxyModel->index(index.row(), 0);
//加载便签
selectNote(index);
m_noteView->setCurrentRowActive(false);
}