/
mainwindow.cpp
9707 lines (7982 loc) · 308 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
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) 2014-2017 Patrizio Bekerle -- http://www.bekerle.com
*
* 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; version 2 of the License.
*
* 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.
*
*/
#include "mainwindow.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QListWidgetItem>
#include <QSettings>
#include <QTimer>
#include <QKeyEvent>
#include <QDesktopServices>
#include <QActionGroup>
#include <QSystemTrayIcon>
#include <QShortcut>
#include <QPrinter>
#include <QPrintDialog>
#include <QPageSetupDialog>
#include <QMimeData>
#include <QTextBlock>
#include <QClipboard>
#include <QTemporaryFile>
#include <QScrollBar>
#include <QTextDocumentFragment>
#include <QProcess>
#include <QJSEngine>
#include <QInputDialog>
#include <QCompleter>
#include <QTreeWidgetItem>
#include <QCoreApplication>
#include <QUuid>
#include "ui_mainwindow.h"
#include "dialogs/linkdialog.h"
#include "services/owncloudservice.h"
#include "services/databaseservice.h"
#include "dialogs/tododialog.h"
#include "libraries/diff_match_patch/diff_match_patch.h"
#include "dialogs/notediffdialog.h"
#include "build_number.h"
#include "version.h"
#include "release.h"
#include "dialogs/aboutdialog.h"
#include "dialogs/settingsdialog.h"
#include "entities/calendaritem.h"
#include "widgets/qownnotesmarkdowntextedit.h"
#include "dialogs/passworddialog.h"
#include "services/metricsservice.h"
#include <services/cryptoservice.h>
#include <helpers/clientproxy.h>
#include <utils/misc.h>
#include <utils/gui.h>
#include <entities/notefolder.h>
#include <entities/notesubfolder.h>
#include <entities/tag.h>
#include <dialogs/tagadddialog.h>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
#include <QQmlApplicationEngine>
#include <QWidgetAction>
#include <QColorDialog>
#include <services/scriptingservice.h>
#include <dialogs/evernoteimportdialog.h>
#include <widgets/logwidget.h>
#include <dialogs/sharedialog.h>
#include <dialogs/orphanedimagesdialog.h>
#include <helpers/toolbarcontainer.h>
#include <libraries/qttoolbareditor/src/toolbar_editor.hpp>
#include <dialogs/actiondialog.h>
#include <dialogs/tabledialog.h>
#include <dialogs/notedialog.h>
#include <utils/schema.h>
#include <utils/git.h>
#include <dialogs/filedialog.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
// use our custom log handler
qInstallMessageHandler(LogWidget::logMessageOutput);
qApp->setProperty("loggingEnabled", true);
#ifdef Q_OS_MAC
// disable icons in the menu
QApplication::instance()->setAttribute(Qt::AA_DontShowIconsInMenus, true);
#endif
ui->setupUi(this);
setWindowIcon(getSystemTrayIcon());
// initialize the workspace combo box
initWorkspaceComboBox();
#ifdef Q_OS_MAC
// disable icons in the menu that weren't handled by
// Qt::AA_DontShowIconsInMenus
ui->actionShare_note->setIconVisibleInMenu(false);
// set an other shortcut for delete line under macOS
ui->actionDelete_line->setShortcut(QKeySequence("Ctrl+Backspace"));
ui->actionDelete_word->setShortcut(QKeySequence("Alt+Backspace"));
#endif
_noteViewIsRegenerated = false;
_searchLineEditFromCompleter = false;
_isNotesDirectoryWasModifiedDisabled = false;
_isNotesWereModifiedDisabled = false;
_isDefaultShortcutInitialized = false;
_showNotesFromAllNoteSubFolders = showNotesFromAllNoteSubFolders();
_noteFolderDockWidgetWasVisible = true;
_noteSubFolderDockWidgetVisible = true;
_noteExternallyRemovedCheckEnabled = true;
this->setWindowTitle(
"QOwnNotes - version " + QString(VERSION) +
" - build " + QString::number(BUILD));
qApp->setProperty(
"mainWindow",
QVariant::fromValue<MainWindow *>(this));
ClientProxy proxy;
// refresh the Qt proxy settings
proxy.setupQtProxyFromSettings();
QActionGroup *sorting = new QActionGroup(this);
sorting->addAction(ui->actionAlphabetical);
sorting->addAction(ui->actionBy_date);
QActionGroup *sortingOrder = new QActionGroup(this);
sortingOrder->addAction(ui->actionAscending);
sortingOrder->addAction(ui->actionDescending);
sortingOrder->setExclusive(true);
// hide the encrypted note text edit by default
ui->encryptedNoteTextEdit->hide();
// set the search frames for the note text edits
QSettings settings;
bool darkMode = settings.value("darkMode").toBool();
ui->noteTextEdit->initSearchFrame(ui->noteTextEditSearchFrame, darkMode);
ui->encryptedNoteTextEdit->initSearchFrame(ui->noteTextEditSearchFrame,
darkMode);
// set the main window for accessing it's public methods
ui->noteTextEdit->setMainWindow(this);
ui->encryptedNoteTextEdit->setMainWindow(this);
// initialize the tag button scroll area
initTagButtonScrollArea();
noteHistory = NoteHistory();
// set our signal mapper
recentNoteFolderSignalMapper = new QSignalMapper(this);
// initialize the toolbars
initToolbars();
#ifdef Q_OS_MAC
// add some different shortcuts for the note history on the mac
ui->action_Back_in_note_history->
setShortcut(Qt::CTRL + Qt::ALT + Qt::Key_Left);
ui->action_Forward_in_note_history->
setShortcut(Qt::CTRL + Qt::ALT + Qt::Key_Right);
// add an other shortcut for the auto-completer
ui->actionAutocomplete->setShortcut(Qt::META + Qt::Key_Space);
// add an other shortcut for inserting media
ui->actionPaste_image->setShortcut(Qt::CTRL + Qt::ALT + Qt::Key_V);
#endif
// adding some alternate shortcuts for changing the current note
QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+PgDown"), this);
QObject::connect(shortcut, SIGNAL(activated()),
this, SLOT(on_actionNext_note_triggered()));
shortcut = new QShortcut(QKeySequence("Ctrl+PgUp"), this);
QObject::connect(shortcut, SIGNAL(activated()),
this, SLOT(on_actionPrevious_Note_triggered()));
// read the settings (shortcuts have to be defined before that)
readSettings();
// do a bit more styling
initStyling();
// initialize the dock widgets
initDockWidgets();
// initialize the scripting engine
// initDockWidgets() has to be called before that so the scripting dock
// widget is already in place
initScriptingEngine();
// restore toolbars
// initDockWidgets() has to be called first so panel checkboxes can be
// used in toolbars
restoreToolbars();
// update the workspace menu and combobox entries again after
// restoreToolbars() to fill the workspace combo box again
updateWorkspaceLists();
// check if we want to start the application hidden
initShowHidden();
// set the show in system tray checkbox
ui->actionShow_system_tray->setChecked(showSystemTray);
createSystemTrayIcon();
buildNotesIndexAndLoadNoteDirectoryList();
// setup the update available button
setupStatusBarWidgets();
this->noteDiffDialog = new NoteDiffDialog();
// look if we need to save something every 10 sec (default)
this->noteSaveTimer = new QTimer(this);
QObject::connect(
this->noteSaveTimer,
SIGNAL(timeout()),
this,
SLOT(storeUpdatedNotesToDisk()));
this->noteSaveTimer->start(this->noteSaveIntervalTime * 1000);
// look if we need update the note view every two seconds
_noteViewUpdateTimer = new QTimer(this);
QObject::connect(
_noteViewUpdateTimer,
SIGNAL(timeout()),
this,
SLOT(noteViewUpdateTimerSlot()));
_noteViewUpdateTimer->start(2000);
// commit changes from the current note folder to git every 30 sec
gitCommitCurrentNoteFolder();
_gitCommitTimer = new QTimer(this);
QObject::connect(
_gitCommitTimer,
SIGNAL(timeout()),
this,
SLOT(gitCommitCurrentNoteFolder()));
_gitCommitTimer->start(_gitCommitInterval * 1000);
// check if we have a tasks reminder every minute
this->todoReminderTimer = new QTimer(this);
QObject::connect(
this->todoReminderTimer,
SIGNAL(timeout()),
this,
SLOT(frequentPeriodicChecker()));
this->todoReminderTimer->start(60000);
QObject::connect(
&this->noteDirectoryWatcher,
SIGNAL(directoryChanged(QString)),
this,
SLOT(notesDirectoryWasModified(QString)));
QObject::connect(
&this->noteDirectoryWatcher,
SIGNAL(fileChanged(QString)),
this,
SLOT(notesWereModified(QString)));
ui->searchLineEdit->installEventFilter(this);
ui->noteTreeWidget->installEventFilter(this);
ui->noteTextEdit->installEventFilter(this);
ui->noteTextEdit->viewport()->installEventFilter(this);
ui->encryptedNoteTextEdit->installEventFilter(this);
ui->encryptedNoteTextEdit->viewport()->installEventFilter(this);
ui->tagTreeWidget->installEventFilter(this);
ui->newNoteTagLineEdit->installEventFilter(this);
// init the saved searches completer
initSavedSearchesCompleter();
// ignores note clicks in QMarkdownTextEdit in the note text edit
ui->noteTextEdit->setIgnoredClickUrlSchemata(
QStringList() << "note" << "task");
ui->encryptedNoteTextEdit->setIgnoredClickUrlSchemata(
QStringList() << "note" << "task");
// handle note url externally in the note text edit
QObject::connect(
ui->noteTextEdit,
SIGNAL(urlClicked(QString)),
this,
SLOT(openLocalUrl(QString)));
// also handle note url externally in the encrypted note text edit
QObject::connect(
ui->encryptedNoteTextEdit,
SIGNAL(urlClicked(QString)),
this,
SLOT(openLocalUrl(QString)));
// set the tab stop to the width of 4 spaces in the editor
const int tabStop = 4;
QFont font = ui->noteTextEdit->font();
QFontMetrics metrics(font);
int width = tabStop * metrics.width(' ');
ui->noteTextEdit->setTabStopWidth(width);
ui->encryptedNoteTextEdit->setTabStopWidth(width);
// called now in readSettingsFromSettingsDialog() line 494
// set the edit mode for the note text edit
// this->setNoteTextEditMode(true);
// load the note folder list in the menu
this->loadNoteFolderListMenu();
// update panels sort and order
updatePanelsSortOrder();
this->updateService = new UpdateService(this);
this->updateService->checkForUpdates(this, UpdateService::AppStart);
// we need to restore the current workspace a little later when
// application window is maximized or in full-screen mode
if (isMaximized() || isFullScreen()) {
QTimer::singleShot(500, this, SLOT(restoreCurrentWorkspace()));
}
// update the current folder tooltip
updateCurrentFolderTooltip();
// show the app metrics notification if not already shown
showAppMetricsNotificationIfNeeded();
frequentPeriodicChecker();
// setup the shortcuts for the note bookmarks
setupNoteBookmarkShortcuts();
// restore the distraction free mode
restoreDistractionFreeMode();
// add action tracking
connect(ui->menuBar, SIGNAL(triggered(QAction *)),
this, SLOT(trackAction(QAction *)));
// set "show toolbar" menu item checked/unchecked
const QSignalBlocker blocker(ui->actionShow_toolbar);
{
Q_UNUSED(blocker);
ui->actionShow_toolbar->setChecked(isToolbarVisible());
}
Q_FOREACH(QToolBar *toolbar, findChildren<QToolBar*>()) {
connect(toolbar, SIGNAL(visibilityChanged(bool)),
this, SLOT(toolbarVisibilityChanged(bool)));
}
// set the action group for the width selector of the distraction free mode
QActionGroup *dfmEditorWidthActionGroup = new QActionGroup(this);
dfmEditorWidthActionGroup->addAction(ui->actionEditorWidthNarrow);
dfmEditorWidthActionGroup->addAction(ui->actionEditorWidthMedium);
dfmEditorWidthActionGroup->addAction(ui->actionEditorWidthWide);
dfmEditorWidthActionGroup->addAction(ui->actionEditorWidthFull);
dfmEditorWidthActionGroup->setExclusive(true);
connect(dfmEditorWidthActionGroup, SIGNAL(triggered(QAction *)),
this, SLOT(dfmEditorWidthActionTriggered(QAction *)));
setAcceptDrops(true);
// we need to disallow this explicitly under Windows
// so that the MainWindow gets the event
ui->noteTextEdit->setAcceptDrops(false);
// act on position clicks in the navigation widget
QObject::connect(ui->navigationWidget, SIGNAL(positionClicked(int)),
this, SLOT(onNavigationWidgetPositionClicked(int)));
// do the navigation parsing after the highlighter was finished
QObject::connect(ui->noteTextEdit->highlighter(),
SIGNAL(highlightingFinished()),
this,
SLOT(startNavigationParser()));
QObject::connect(ui->encryptedNoteTextEdit->highlighter(),
SIGNAL(highlightingFinished()),
this,
SLOT(startNavigationParser()));
// act on note preview resize
QObject::connect(ui->noteTextView,
SIGNAL(resize(QSize, QSize)),
this,
SLOT(onNoteTextViewResize(QSize, QSize)));
// reloads all tasks from the ownCloud server
reloadTodoLists();
// create a timer to load all tasks every 10min
_todoListTimer = new QTimer(this);
QObject::connect(
_todoListTimer, SIGNAL(timeout()),
this, SLOT(reloadTodoLists()));
_todoListTimer->start(600000);
// setup the soft-wrap checkbox
const QSignalBlocker blocker2(ui->actionUse_softwrap_in_note_editor);
Q_UNUSED(blocker2);
ui->actionUse_softwrap_in_note_editor->setChecked(
settings.value("useSoftWrapInNoteEditor", true).toBool());
// initialize the editor soft wrapping
initEditorSoftWrap();
// check if user has set ownCloud settings
MetricsService::instance()->sendEventIfEnabled(
"app/has-owncloud-settings", "app", "has owncloud settings",
OwnCloudService::hasOwnCloudSettings() ? "yes" : "no");
// send an event for counting the editor color schemes
int schemaCount = settings.value("Editor/ColorSchemes").toStringList()
.count();
MetricsService::instance()->sendEventIfEnabled(
"app/editor-color-schema-count",
"editor",
"editor color schema count",
QString::number(schemaCount) + " schemas",
schemaCount);
_actionDialog = Q_NULLPTR;
_todoDialog = Q_NULLPTR;
_settingsDialog = Q_NULLPTR;
// track cursor position changes for the line number label
QObject::connect(ui->noteTextEdit,
SIGNAL(cursorPositionChanged()),
this,
SLOT(noteEditCursorPositionChanged()));
// track cursor position changes for the line number label
QObject::connect(ui->encryptedNoteTextEdit,
SIGNAL(cursorPositionChanged()),
this,
SLOT(noteEditCursorPositionChanged()));
// try to restore the last note before the app was quit
// if that fails jump to the first note
// we do that with a timer, because otherwise the scrollbar will not be
// restored correctly, because the maximum position of the scrollbar is 0
QTimer::singleShot(250, this, SLOT(restoreActiveNoteHistoryItem()));
// wait some time for the tagTree to get visible, if selected, and apply last
// selected tag search
QTimer::singleShot(250, this, SLOT(filterNotesByTag()));
// attempt to check the api app version
startAppVersionTest();
}
/**
* Attempts to check the api app version
*/
void MainWindow::startAppVersionTest() const {
if (!OwnCloudService::hasOwnCloudSettings()) {
return;
}
OwnCloudService *ownCloud = OwnCloudService::instance();
ownCloud->startAppVersionTest();
}
/**
* Restores the active note history item
*/
bool MainWindow::restoreActiveNoteHistoryItem() {
QSettings settings;
QVariant var = settings.value("ActiveNoteHistoryItem");
// qDebug() << __func__ << " - 'var': " << var;
// check if the NoteHistoryItem could be de-serialized
if (var.isValid()) {
NoteHistoryItem noteHistoryItem = var.value<NoteHistoryItem>();
// qDebug() << __func__ << " - 'noteHistoryItem': " << noteHistoryItem;
if (jumpToNoteName(noteHistoryItem.getNoteName())) {
noteHistoryItem.restoreTextEditPosition(ui->noteTextEdit);
return true;
}
}
// if restoring the last note failed jump to the first note
resetCurrentNote();
return false;
}
MainWindow::~MainWindow() {
if (!isInDistractionFreeMode()) {
storeCurrentWorkspace();
}
storeUpdatedNotesToDisk();
// commit the changes in the current note folder to git
gitCommitCurrentNoteFolder();
qApp->setProperty("loggingEnabled", false);
if (showSystemTray) {
// if we are using the system tray lets delete the log window so the
// app can quit
delete(LogWidget::instance());
}
delete ui;
}
/*!
* Methods
*/
/**
* Initializes the workspace combo box
*/
void MainWindow::initWorkspaceComboBox() {
_workspaceComboBox = new QComboBox(this);
connect(_workspaceComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(onWorkspaceComboBoxCurrentIndexChanged(int)));
_workspaceComboBox->setToolTip(tr("Workspaces"));
_workspaceComboBox->setObjectName("workspaceComboBox");
_workspaceSignalMapper = new QSignalMapper(this);
}
/**
* Initializes the dock widgets
*/
void MainWindow::initDockWidgets() {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
setDockOptions(dockOptions() | GroupedDragging);
#endif
QSizePolicy sizePolicy;
_noteSubFolderDockWidget = new QDockWidget(tr("Subfolders"), this);
_noteSubFolderDockWidget->setObjectName("noteSubFolderDockWidget");
_noteSubFolderDockWidget->setWidget(ui->noteSubFolderFrame);
_noteSubFolderDockTitleBarWidget =
_noteSubFolderDockWidget->titleBarWidget();
sizePolicy = _noteSubFolderDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(2);
_noteSubFolderDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::LeftDockWidgetArea, _noteSubFolderDockWidget,
Qt::Horizontal);
_taggingDockWidget = new QDockWidget(tr("Tags"), this);
_taggingDockWidget->setObjectName("taggingDockWidget");
_taggingDockWidget->setWidget(ui->tagFrame);
_taggingDockTitleBarWidget = _taggingDockWidget->titleBarWidget();
sizePolicy = _taggingDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(2);
_taggingDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::LeftDockWidgetArea, _taggingDockWidget, Qt::Vertical);
_noteSearchDockWidget = new QDockWidget(tr("Note search"), this);
_noteSearchDockWidget->setObjectName("noteSearchDockWidget");
_noteSearchDockWidget->setWidget(ui->searchLineEdit);
_noteSearchDockTitleBarWidget = _noteSearchDockWidget->titleBarWidget();
sizePolicy = _noteSearchDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(2);
_noteSearchDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::LeftDockWidgetArea, _noteSearchDockWidget, Qt::Vertical);
_noteFolderDockWidget = new QDockWidget(tr("Note folder"), this);
_noteFolderDockWidget->setObjectName("noteFolderDockWidget");
_noteFolderDockWidget->setWidget(ui->noteFolderComboBox);
_noteFolderDockTitleBarWidget = _noteFolderDockWidget->titleBarWidget();
sizePolicy = _noteFolderDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(2);
_noteFolderDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::LeftDockWidgetArea, _noteFolderDockWidget, Qt::Vertical);
_noteListDockWidget = new QDockWidget(tr("Note list"), this);
_noteListDockWidget->setObjectName("noteListDockWidget");
_noteListDockWidget->setWidget(ui->notesListFrame);
_noteListDockTitleBarWidget = _noteListDockWidget->titleBarWidget();
sizePolicy = _noteListDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(2);
_noteListDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::LeftDockWidgetArea, _noteListDockWidget, Qt::Vertical);
_noteNavigationDockWidget = new QDockWidget(tr("Navigation"), this);
_noteNavigationDockWidget->setObjectName("noteNavigationDockWidget");
_noteNavigationDockWidget->setWidget(ui->navigationFrame);
_noteNavigationDockTitleBarWidget =
_noteNavigationDockWidget->titleBarWidget();
sizePolicy = _noteNavigationDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(2);
_noteNavigationDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::LeftDockWidgetArea, _noteNavigationDockWidget,
Qt::Vertical);
// we want the navigation under the note list
splitDockWidget(_noteListDockWidget, _noteNavigationDockWidget,
Qt::Vertical);
_noteEditDockWidget = new QDockWidget(tr("Note edit"), this);
_noteEditDockWidget->setObjectName("noteEditDockWidget");
_noteEditDockWidget->setWidget(ui->noteEditFrame);
_noteEditDockTitleBarWidget = _noteEditDockWidget->titleBarWidget();
sizePolicy = _noteEditDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(5);
_noteEditDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::RightDockWidgetArea, _noteEditDockWidget, Qt::Horizontal);
_noteTagDockWidget = new QDockWidget(tr("Note tags"), this);
_noteTagDockWidget->setObjectName("noteTagDockWidget");
_noteTagDockWidget->setWidget(ui->noteTagFrame);
_noteTagDockTitleBarWidget = _noteTagDockWidget->titleBarWidget();
sizePolicy = _noteTagDockWidget->sizePolicy();
sizePolicy.setHorizontalStretch(5);
_noteTagDockWidget->setSizePolicy(sizePolicy);
addDockWidget(Qt::RightDockWidgetArea, _noteTagDockWidget, Qt::Vertical);
_notePreviewDockWidget = new QDockWidget(tr("Note preview"), this);
_notePreviewDockWidget->setObjectName("notePreviewDockWidget");
_notePreviewDockWidget->setWidget(ui->noteViewFrame);
_notePreviewDockTitleBarWidget = _notePreviewDockWidget->titleBarWidget();
addDockWidget(Qt::RightDockWidgetArea, _notePreviewDockWidget,
Qt::Horizontal);
_logDockWidget = new QDockWidget(tr("Log"), this);
_logDockWidget->setObjectName("logDockWidget");
_logDockWidget->setWidget(LogWidget::instance());
_logDockTitleBarWidget = _logDockWidget->titleBarWidget();
addDockWidget(Qt::RightDockWidgetArea, _logDockWidget, Qt::Vertical);
_logDockWidget->hide();
_scriptingDockWidget = new QDockWidget(tr("Scripting"), this);
_scriptingDockWidget->setObjectName("scriptingDockWidget");
_scriptingDockWidget->setWidget(ui->scriptingScrollArea);
_scriptingDockTitleBarWidget = _scriptingDockWidget->titleBarWidget();
addDockWidget(Qt::RightDockWidgetArea, _scriptingDockWidget, Qt::Vertical);
_scriptingDockWidget->hide();
// we only needed that label to set a layout in QtCreator
delete ui->scriptingDemoLabel;
QSettings settings;
// forcing some dock widget sizes on the first application start
if (!settings.value("dockWasInitializedOnce").toBool()) {
// setting a small height for the note tag panel
_noteTagDockWidget->setMaximumHeight(40);
// giving the left panels with the note list a fifth of the screen
_noteListDockWidget->setMaximumWidth(width() / 5);
// giving the preview pane a third of the screen, the rest goes to the
// note edit pane
_notePreviewDockWidget->setMaximumWidth(width() / 3);
settings.setValue("dockWasInitializedOnce", true);
// releasing the forced maximum sizes
QTimer::singleShot(250, this, SLOT(releaseDockWidgetSizes()));
}
// ui->noteEditFrame->setStyleSheet("* { border: none; }");
// ui->noteTextEdit->setStyleSheet("* { border: none; }");
// ui->noteEditFrame->layout()->setContentsMargins(0, 0, 0, 0);
setDockNestingEnabled(true);
setCentralWidget(Q_NULLPTR);
// restore the current workspace
restoreCurrentWorkspace();
// lock the dock widgets
on_actionUnlock_panels_toggled(false);
// update the workspace menu and combobox entries
updateWorkspaceLists();
// initialize the panel menu
initPanelMenu();
}
/**
* Releaseing the forced maximum sizes on some dock widgets
*/
void MainWindow::releaseDockWidgetSizes() {
_noteListDockWidget->setMaximumWidth(10000);
_notePreviewDockWidget->setMaximumWidth(10000);
_noteTagDockWidget->setMaximumHeight(10000);
}
/**
* Initializes if we want to start the application hidden
*/
void MainWindow::initShowHidden() {
QSettings settings;
bool startHidden = settings.value("StartHidden", false).toBool();
ui->actionStart_hidden->setChecked(startHidden);
ui->actionStart_hidden->setEnabled(showSystemTray);
if (startHidden) {
QTimer::singleShot(250, this, SLOT(hide()));
}
}
/**
* Initializes the tag button scroll area
*
* If there are more tags assigned to a note than the width of the edit
* pane allows there now will be used a scrollbar to scroll through the
* tags, so that the width of the edit pane can still be small
*/
void MainWindow::initTagButtonScrollArea() {
_noteTagButtonScrollArea = new QScrollArea(this);
_noteTagButtonScrollArea->setWidgetResizable(true);
_noteTagButtonScrollArea->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::Ignored);
_noteTagButtonScrollArea->setAlignment(Qt::AlignLeft);
_noteTagButtonScrollArea->setWidget(ui->noteTagButtonFrame);
ui->noteTagButtonFrame->layout()->setContentsMargins(0, 0, 0, 0);
_noteTagButtonScrollArea->setContentsMargins(0, 0, 0, 0);
#ifdef Q_OS_MAC
// we need to set a minimum height under OS X or else the scroll area
// will be far to high
_noteTagButtonScrollArea->setMinimumHeight(36);
#endif
#ifdef Q_OS_WIN32
// we need to set a minimum height under Windows or else the scroll area
// will be far to high
_noteTagButtonScrollArea->setMinimumHeight(40);
#endif
ui->noteTagFrame->layout()->addWidget(_noteTagButtonScrollArea);
ui->noteTagFrame->layout()->addWidget(ui->newNoteTagButton);
ui->noteTagFrame->layout()->addWidget(ui->newNoteTagLineEdit);
}
/**
* Returns all menus from the menu bar
*/
QList<QMenu *> MainWindow::menuList() {
return ui->menuBar->findChildren<QMenu *>();
}
/**
* Finds an action in all menus of the menu bar
*/
QAction *MainWindow::findAction(QString objectName) {
QList<QMenu*> menus = menuList();
// loop through all menus because we were not able to find the action with
// ui->menuBar->findChild<QAction *>(objectName);
foreach(QMenu* menu, menus) {
// loop through all actions of the menu
foreach(QAction* action, menu->actions()) {
if (action->objectName() == objectName) {
return action;
}
}
}
return Q_NULLPTR;
}
/**
* Builds the note index and loads the note directory list
*
* @param forceBuild
* @param forceLoad
*/
void MainWindow::buildNotesIndexAndLoadNoteDirectoryList(bool forceBuild,
bool forceLoad) {
bool wasBuilt = buildNotesIndex(0, forceBuild);
if (wasBuilt || forceLoad) {
loadNoteDirectoryList();
}
}
/**
* Returns the global main window instance
*/
MainWindow *MainWindow::instance() {
return qApp->property("mainWindow").value<MainWindow *>();
}
/**
* Initializes the editor soft wrapping
*/
void MainWindow::initEditorSoftWrap() {
QSettings settings;
bool useSoftWrapInNoteEditor =
settings.value("useSoftWrapInNoteEditor", true).toBool();
QTextEdit::LineWrapMode mode =
useSoftWrapInNoteEditor ?
QTextEdit::WidgetWidth : QTextEdit::NoWrap;
QPlainTextEdit::LineWrapMode pMode =
useSoftWrapInNoteEditor ?
QPlainTextEdit::WidgetWidth : QPlainTextEdit::NoWrap;
ui->noteTextEdit->setLineWrapMode(pMode);
ui->encryptedNoteTextEdit->setLineWrapMode(pMode);
ui->noteTextView->setLineWrapMode(mode);
}
/**
* Reloads all tasks from the ownCloud server
*/
void MainWindow::reloadTodoLists() {
QSettings settings;
QStringList calendars =
settings.value("ownCloud/todoCalendarEnabledList").toStringList();
QString serverUrl =
settings.value("ownCloud/serverUrl").toString().trimmed();
if (calendars.count() > 0 && !serverUrl.isEmpty()) {
OwnCloudService *ownCloud = OwnCloudService::instance();
QListIterator<QString> itr(calendars);
while (itr.hasNext()) {
QString calendar = itr.next();
ownCloud->todoGetTodoList(calendar, Q_NULLPTR);
}
showStatusBarMessage(
tr("Your tasks are being loaded from your server"),
4000);
// generate the system tray context menu to show modified tasks
// in 15 sec (because we don't know when all new tasks will be loaded)
QTimer::singleShot(15000, this, SLOT(generateSystemTrayContextMenu()));
}
}
/**
* Initializes the scripting engine
*/
void MainWindow::initScriptingEngine() {
_customActionSignalMapper = new QSignalMapper(this);
ui->menuCustom_actions->hide();
_customActionToolbar->hide();
// connect the custom action signal mapper
QObject::connect(_customActionSignalMapper,
SIGNAL(mapped(QString)),
this,
SLOT(onCustomActionInvoked(QString)));
ScriptingService* scriptingService = ScriptingService::createInstance(this);
QQmlEngine* engine = scriptingService->engine();
// engine->setObjectOwnership(ui->noteTextEdit, QQmlEngine::CppOwnership);
engine->rootContext()->setContextProperty("noteTextEdit", ui->noteTextEdit);
scriptingService->initComponents();
}
/**
* Invokes the custom action in the scripting service
*
* @param identifier
*/
void MainWindow::onCustomActionInvoked(QString identifier) {
ScriptingService *scriptingService = ScriptingService::instance();
if (scriptingService != Q_NULLPTR) {
scriptingService->onCustomActionInvoked(identifier);
}
}
/**
* Initializes the toolbars
*/
void MainWindow::initToolbars() {
_formattingToolbar = new QToolBar(tr("formatting toolbar"), this);
_formattingToolbar->addAction(ui->actionFormat_text_bold);
_formattingToolbar->addAction(ui->actionFormat_text_italic);
_formattingToolbar->addAction(ui->actionStrike_out_text);
_formattingToolbar->addAction(ui->actionInsert_code_block);
_formattingToolbar->addAction(ui->actionInsert_block_quote);
_formattingToolbar->setObjectName("formattingToolbar");
addToolBar(_formattingToolbar);
_insertingToolbar = new QToolBar(tr("inserting toolbar"), this);
_insertingToolbar->addAction(ui->actionInsert_Link_to_note);
_insertingToolbar->addAction(ui->actionInsert_image);
_insertingToolbar->addAction(ui->actionInsert_current_time);
_insertingToolbar->setObjectName("insertingToolbar");
addToolBar(_insertingToolbar);
_encryptionToolbar = new QToolBar(tr("encryption toolbar"), this);
_encryptionToolbar->addAction(ui->action_Encrypt_note);
_encryptionToolbar->addAction(ui->actionEdit_encrypted_note);
_encryptionToolbar->addAction(ui->actionDecrypt_note);
_encryptionToolbar->setObjectName("encryptionToolbar");
addToolBar(_encryptionToolbar);
_windowToolbar = new QToolBar(tr("window toolbar"), this);
updateWindowToolbar();
_windowToolbar->setObjectName("windowToolbar");
addToolBar(_windowToolbar);
_customActionToolbar = new QToolBar(tr("custom action toolbar"), this);
_customActionToolbar->setObjectName("customActionsToolbar");
_customActionToolbar->hide();
addToolBar(_customActionToolbar);
_quitToolbar = new QToolBar(tr("quit toolbar"), this);
_quitToolbar->addAction(ui->action_Quit);
_quitToolbar->setObjectName("quitToolbar");
addToolBar(_quitToolbar);
}
/**
* Populates the window toolbar
*/
void MainWindow::updateWindowToolbar() {
_windowToolbar->clear();
QWidgetAction *widgetAction = new QWidgetAction(this);
widgetAction->setDefaultWidget(_workspaceComboBox);
widgetAction->setObjectName("actionWorkspaceComboBox");
widgetAction->setText(tr("Workspace selector"));
_windowToolbar->addAction(widgetAction);
_windowToolbar->addAction(ui->actionStore_as_new_workspace);
_windowToolbar->addAction(ui->actionRemove_current_workspace);
_windowToolbar->addAction(ui->actionRename_current_workspace);
_windowToolbar->addAction(ui->actionSwitch_to_previous_workspace);
_windowToolbar->addAction(ui->actionUnlock_panels);
_windowToolbar->addSeparator();
_windowToolbar->addAction(
ui->actionToggle_distraction_free_mode);
_windowToolbar->addAction(ui->action_Increase_note_text_size);
_windowToolbar->addAction(ui->action_Decrease_note_text_size);
_windowToolbar->addAction(ui->action_Reset_note_text_size);
}
/**
* Updates the workspace menu and combobox entries
*/
void MainWindow::updateWorkspaceLists(bool rebuild) {
QSettings settings;
QStringList workspaces = getWorkspaceUuidList();
QString currentUuid = currentWorkspaceUuid();
if (rebuild) {
// we need to create a new combo box so the width gets updated in the
// window toolbar
initWorkspaceComboBox();
ui->menuWorkspaces->clear();
}
const QSignalBlocker blocker(_workspaceComboBox);
Q_UNUSED(blocker);
int currentIndex = 0;
for (int i = 0; i < workspaces.count(); i++) {
QString uuid = workspaces.at(i);
if (uuid == currentUuid) {
currentIndex = i;
}
// check if we want to skip the rebuilding part
if (!rebuild) {
continue;
}
QString name = settings.value("workspace-" + uuid + "/name").toString();
QString objectName = "restoreWorkspace-" + uuid;
_workspaceComboBox->addItem(name, uuid);
QAction *action = new QAction(name, ui->menuWorkspaces);
QObject::connect(action, SIGNAL(triggered()),
_workspaceSignalMapper, SLOT(map()));
// add a parameter to the signal mapper
_workspaceSignalMapper->setMapping(action, uuid);
// set an object name for creating shortcuts
action->setObjectName(objectName);
// try to load a key sequence from the settings
QKeySequence shortcut = QKeySequence(settings.value(
"Shortcuts/MainWindow-" + objectName).toString());
action->setShortcut(shortcut);