-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathManager.cpp
More file actions
467 lines (396 loc) · 14.8 KB
/
Manager.cpp
File metadata and controls
467 lines (396 loc) · 14.8 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
#include "Manager.h"
#include "ui_Manager.h"
#include <QStandardItem>
#include <QFileInfo>
#include <QClipboard>
#include <QDesktopWidget>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QDesktopServices>
#include "FormattingToolbar/FormattingToolbar.h"
#include "utils/func.h"
#include "ImageCropper/fullscreenshotcropper.h"
#include "NavigationPanel/notebook.h"
#include <QDockWidget>
#include <QtWebKit/QWebView>
Manager * Manager::self = 0;
Manager::Manager( QWidget * parent ) :
QMainWindow( parent ),
ui( new Ui::Manager ),
tray( new QSystemTrayIcon( QIcon( ":/App/icon-mini" ) ) ),
settings(0)
{
self = this;
ui->setupUi( this );
ui->sidebar->addTab( QIcon( ":/Manager/notebook" ), tr( "Notes" ) );
ui->sidebar->addTab( QIcon( ":/Manager/settings" ), tr( "Settings" ) );
ui->sidebar->addTab( QIcon( ":/Manager/about" ), tr( "About" ) );
ui->sidebar->addTab( QIcon( ":/Manager/quit" ), tr( "Quit" ) );
ui->sidebar->setCurrentIndex( 0 );
QObject::connect( ui->sidebar, SIGNAL( currentChanged(int) ), SLOT( buttonSidebarClicked(int) ) );
pageNotes = new Page_Notes();
pageSettings = new Page_Settings();
pageAbout = new Page_About();
QObject::connect( pageNotes, SIGNAL( sg_About_UpdateStates() ), SLOT( updateStates() ) );
QObject::connect( pageSettings, SIGNAL( message(QString, int) ), ui->statusBar, SLOT( showMessage(QString, int) ) );
QObject::connect( pageSettings, SIGNAL( acceptChangeSettings() ), SLOT( acceptChangeSettings() ) );
ui->stackedWidget_Pages->addWidget( pageNotes );
ui->stackedWidget_Pages->addWidget( pageSettings );
ui->stackedWidget_Pages->addWidget( pageAbout );
ui->stackedWidget_Pages->setCurrentIndex( ui->sidebar->currentIndex() );
// Меню и действия
{
ui->menuFile->addAction( pageNotes->ui->actionAddNote );
ui->menuFile->addAction( pageNotes->ui->actionAddNoteFromClipboard );
ui->menuFile->addAction( pageNotes->ui->actionAddNoteFromScreen );
ui->menuFile->addSeparator();
ui->menuFile->addAction( pageNotes->ui->actionPrintNote );
ui->menuFile->addAction( pageNotes->ui->actionPreviewPrintNote );
ui->menuFile->addSeparator();
ui->menuFile->addAction( pageNotes->ui->actionSaveNoteAs );
ui->menuFile->addSeparator();
ui->menuFile->addAction( pageNotes->ui->actionShowAllNotes );
ui->menuFile->addAction( pageNotes->ui->actionHideAllNotes );
ui->menuFile->addSeparator();
ui->menuFile->addAction( ui->actionQuit );
ui->menuNote->addActions( pageNotes->ui->toolBarNote->actions() );
ui->menuService->addAction( ui->actionSettings );
ui->menuHelp->addSeparator();
ui->menuHelp->addAction( ui->actionAbout );
QObject::connect( ui->actionSettings, SIGNAL( triggered() ), SLOT( showPageSettings() ) );
QObject::connect( ui->actionAbout, SIGNAL( triggered() ), SLOT( showPageAbout() ) );
QObject::connect( ui->actionQuit, SIGNAL( triggered() ), SLOT( quit() ) );
QObject::connect( ui->actionOpenDict, SIGNAL( triggered() ), SLOT( openDictionary() ) );
QObject::connect( ui->actionCloseDict, SIGNAL( triggered() ), SLOT( closeDictionary() ) );
QObject::connect( ui->actionFullScreen, SIGNAL( triggered(bool) ), SLOT( setFullScreen(bool) ) );
QObject::connect( ui->actionTopOfAllWindows, SIGNAL(triggered(bool)), SLOT(setTopOfAllWindows(bool)) );
QObject::connect( ui->actionShowSidebar, SIGNAL( triggered(bool) ), SLOT( setShowSidebar(bool) ) );
QObject::connect( ui->actionShowStatusBar, SIGNAL( triggered(bool) ), SLOT( setShowStatusBar(bool) ) );
// Меню трея
{
QObject::connect( tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(messageReceived(QSystemTrayIcon::ActivationReason)) );
QObject::connect( tray, SIGNAL(messageClicked()), SLOT(showManager()) );
QMenu * trayMenu = new QMenu();
trayMenu->addAction( QIcon( ":/Manager/manager" ), tr( "Open manager" ), this, SLOT( showManager() ), QKeySequence() );
trayMenu->addSeparator();
trayMenu->addAction( pageNotes->ui->actionAddNote );
trayMenu->addAction( pageNotes->ui->actionAddNoteFromClipboard );
trayMenu->addAction( pageNotes->ui->actionAddNoteFromScreen );
trayMenu->addSeparator();
trayMenu->addAction( pageNotes->ui->actionShowAllNotes );
trayMenu->addAction( pageNotes->ui->actionHideAllNotes );
trayMenu->addAction( pageNotes->ui->actionSaveAllNotes );
trayMenu->addSeparator();
trayMenu->addAction( ui->actionSettings );
trayMenu->addSeparator();
trayMenu->addAction( ui->actionAbout );
trayMenu->addSeparator();
trayMenu->addAction( ui->actionQuit );
tray->setContextMenu( trayMenu );
updateSystemTray();
QObject::connect( Notebook::instance(), SIGNAL(notesChange()), SLOT(updateSystemTray()) );
tray->show();
}
// Управление видимостью тулбаров, размещенных на странице Заметки
{
QToolBar * toolBarManage = pageNotes->ui->toolBarManage;
QAction * actionVisibleToolbarManage = new QAction( QIcon( "" ), toolBarManage->windowTitle(), this );
actionVisibleToolbarManage->setCheckable( true );
QObject::connect( actionVisibleToolbarManage, SIGNAL(triggered(bool)), toolBarManage, SLOT(setVisible(bool)) );
QObject::connect( toolBarManage, SIGNAL(visibilityChanged(bool)), actionVisibleToolbarManage, SLOT(setChecked(bool)) );
QToolBar * toolBarNote = pageNotes->ui->toolBarNote;
QAction * actionVisibleToolbarNote = new QAction( QIcon( "" ), toolBarNote->windowTitle(), this );
actionVisibleToolbarNote->setCheckable( true );
QObject::connect( actionVisibleToolbarNote, SIGNAL(triggered(bool)), toolBarNote, SLOT(setVisible(bool)) );
QObject::connect( toolBarNote, SIGNAL(visibilityChanged(bool)), actionVisibleToolbarNote, SLOT(setChecked(bool)) );
ui->menuToolbars->addAction( actionVisibleToolbarManage );
ui->menuToolbars->addAction( actionVisibleToolbarNote );
}
}
QObject::connect( &autoSaveTimer, SIGNAL( timeout() ), SLOT( writeSettings() ) );
updateStates();
}
Manager::~Manager()
{
self = 0;
delete ui;
}
void Manager::loadNotes()
{
const QString & fileName = qApp->applicationDirPath() + "/notes/notebook.xml";
QFile file( fileName );
if ( !file.open( QFile::ReadOnly | QFile::Text ) )
{
QMessageBox::warning( this, tr( "Error" ), tr( "Cannot read file %1:\n%2." ).arg( fileName ).arg( file.errorString() ) );
return;
}
pageNotes->read( &file );
updateStates();
}
void Manager::setSettings( QSettings * s )
{
settings = s;
pageNotes->setSettings( settings );
pageSettings->setSettings( settings );
}
Manager * Manager::instance()
{
return self;
}
void Manager::nowReadyPhase()
{
bool minimizeTrayOnStartup = pageSettings->mapSettings[ "MinimizeTrayOnStartup" ].toBool();
if ( minimizeTrayOnStartup )
hide();
else
showManager();
}
void Manager::buttonSidebarClicked( int index )
{
switch ( index )
{
case 0:
ui->stackedWidget_Pages->setCurrentWidget( pageNotes );
break;
case 1:
ui->stackedWidget_Pages->setCurrentWidget( pageSettings );
break;
case 2:
ui->stackedWidget_Pages->setCurrentWidget( pageAbout );
pageAbout->setCurrentTab( Page_About::About );
break;
case 3:
quit();
break;
}
}
void Manager::messageReceived( const QString & text )
{
foreach ( const QString & command, text.split(" ") )
{
if ( command == "-show" )
showManager();
else if ( command == "-message" )
tray->showMessage( tr( "Information" ), tr( "Application is already running" ), QSystemTrayIcon::Information, 5000 );
else if ( command == "-beep" )
qApp->beep();
}
}
void Manager::messageReceived( QSystemTrayIcon::ActivationReason reason )
{
if ( reason == QSystemTrayIcon::Trigger )
{
if ( isVisible() )
hide();
else
showManager();
}
}
void Manager::acceptChangeSettings()
{
setActivateTimerAutosave( pageSettings->mapSettings[ "Autosave" ].toBool() );
setIntervalAutosave( pageSettings->mapSettings[ "AutosaveInterval" ].toInt() );
}
void Manager::showPageNotes()
{
showManager();
ui->sidebar->setCurrentIndex( 0 );
}
void Manager::showPageSettings()
{
showManager();
ui->sidebar->setCurrentIndex( 1 );
}
void Manager::showPageAbout()
{
showManager();
ui->sidebar->setCurrentIndex( 2 );
}
void Manager::updateStates()
{
ui->actionFullScreen->setChecked( isFullScreen() );
ui->actionTopOfAllWindows->setChecked( isTopOfAllWindows() );
bool isAutocomplete = Completer::instance()->isAutocomplete();
ui->actionCloseDict->setEnabled( isAutocomplete );
ui->actionOpenDict->setEnabled( !isAutocomplete );
ui->actionVisibleToolbarMain->setChecked( ui->toolBarMain->isVisible() );
}
void Manager::updateSystemTray()
{
const QList < Note * > & notes = Notebook::instance()->getNotesList();
int count = notes.size();
int visible_count = 0;
foreach ( Note * note, notes)
if ( note->getRichTextNote()->isVisible() )
visible_count++;
QString details;
details += "\n";
details += tr( "In total notes: %1" ).arg( count ) + "\n";
details += tr( "Visible notes: %1" ).arg( visible_count ) + "\n";
details += tr( "Invisible notes: %1" ).arg( count - visible_count );
const QString & name = qApp->applicationName();
const QString & version = qApp->applicationVersion();
const QString & description = tr( "The program creates notes" );
QString trayToolTip;
trayToolTip += QString( "%1 %2\n" ).arg( name ).arg( version );
trayToolTip += description + "\n";
trayToolTip += details;
tray->setToolTip( trayToolTip );
}
void Manager::showManager()
{
if ( isHidden() )
QTimer::singleShot( 0, this, SLOT( show() ) );
if ( isMinimized() )
QTimer::singleShot( 0, this, SLOT( showNormal() ) );
if ( !hasFocus() )
setFocus();
if ( !isActiveWindow() )
activateWindow();
}
void Manager::setFullScreen( bool fs )
{
if ( fs )
showFullScreen();
else
showNormal();
updateStates();
}
void Manager::setTopOfAllWindows( bool top )
{
setWindowFlags( top ? windowFlags() | Qt::WindowStaysOnTopHint : windowFlags() & ~Qt::WindowStaysOnTopHint );
show();
updateStates();
}
bool Manager::isTopOfAllWindows()
{
return windowFlags().testFlag( Qt::WindowStaysOnTopHint );
}
void Manager::quit()
{
bool askBeforeExiting = pageSettings->mapSettings[ "AskBeforeExiting" ].toBool();
if ( askBeforeExiting )
{
QMessageBox::StandardButton result = showNewMessageBox( this, QMessageBox::Question, tr( "Question" ), tr( "Really quit?" ), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok );
if ( result == QMessageBox::Cancel )
return;
}
// Если не скрыть, то после закрытия иконка будет "висеть" в трее, пока на нее не наведешь мышку
tray->hide();
writeSettings();
qApp->quit();
}
void Manager::readSettings()
{
if ( !settings )
return;
settings->beginGroup( "Manager" );
restoreGeometry( settings->value( "Geometry" ).toByteArray() );
restoreState( settings->value( "State" ).toByteArray() );
setActivateDictionary( settings->value( "Autocomplete" ).toBool() );
setShowSidebar( settings->value( "Sidebar_Visible", true ).toBool() );
setShowStatusBar( settings->value( "StatusBar_Visible", true ).toBool() );
setTopOfAllWindows( settings->value( "TopOfAllWindows", false ).toBool() );
settings->endGroup();
pageNotes->readSettings();
pageSettings->readSettings();
acceptChangeSettings();
updateStates();
}
void Manager::writeSettings()
{
if ( !settings )
return;
settings->beginGroup( "Manager" );
settings->setValue( "Geometry", saveGeometry() );
settings->setValue( "State", saveState() );
settings->setValue( "Autocomplete", isActivateDictionary() );
settings->setValue( "Sidebar_Visible", isShowSidebar() );
settings->setValue( "StatusBar_Visible", isShowStatusBar() );
settings->setValue( "TopOfAllWindows", isTopOfAllWindows() );
settings->endGroup();
pageNotes->writeSettings();
pageNotes->writeToXmlStateNotes();
pageSettings->writeSettings();
settings->sync();
ui->statusBar->showMessage( tr( "Save completed" ), 5000 );
}
void Manager::setActivateDictionary( bool activate )
{
Completer::instance()->setAutocomplete( activate );
updateStates();
}
bool Manager::isActivateDictionary()
{
return Completer::instance()->isAutocomplete();
}
void Manager::openDictionary()
{
setActivateDictionary( true );
}
void Manager::closeDictionary()
{
setActivateDictionary( false );
}
void Manager::setShowSidebar( bool show )
{
ui->sidebar->setVisible( show );
ui->actionShowSidebar->setChecked(show);
}
bool Manager::isShowSidebar()
{
return ui->actionShowSidebar->isChecked();
}
void Manager::setShowStatusBar( bool show )
{
ui->statusBar->setVisible( show );
ui->actionShowStatusBar->setChecked(show);
}
bool Manager::isShowStatusBar()
{
return ui->actionShowStatusBar->isChecked();
}
void Manager::setActivateTimerAutosave( bool activate )
{
if ( activate )
autoSaveTimer.start();
else
autoSaveTimer.stop();
}
bool Manager::isActivateTimerAutosave()
{
return autoSaveTimer.isActive();
}
void Manager::setIntervalAutosave( quint64 minutes )
{
autoSaveTimer.setInterval( 1000 * 60 * minutes );
}
int Manager::intervalAutosave()
{
return autoSaveTimer.interval() / ( 1000 * 60 );
}
void Manager::closeEvent( QCloseEvent * event )
{
event->ignore();
bool minimizeTrayWhenClosing = pageSettings->mapSettings[ "MinimizeTrayWhenClosing" ].toBool();
if ( minimizeTrayWhenClosing )
hide();
else
quit();
}
void Manager::changeEvent( QEvent * event )
{
QEvent::Type type = event->type();
if ( type == QEvent::WindowStateChange )
{
bool minimizeTrayWhenMinimizing = pageSettings->mapSettings[ "MinimizeTrayWhenMinimizing" ].toBool();
if ( isMinimized() && minimizeTrayWhenMinimizing )
{
event->ignore();
QTimer::singleShot( 0, this, SLOT( hide() ) );
return;
}
}
QMainWindow::changeEvent( event );
}