-
Notifications
You must be signed in to change notification settings - Fork 19
/
JuffEd.cpp
1411 lines (1153 loc) · 35.8 KB
/
JuffEd.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
#include <QDebug>
#include "JuffEd.h"
#include <QApplication>
#include <QClipboard>
#include <QDockWidget>
#include <QDomDocument>
#include <QDomElement>
#include <QFileInfo>
#include <QInputDialog>
#include <QMessageBox>
#include <QToolBar>
#include "AppInfo.h"
#include "CharsetSettings.h"
#include "Constants.h"
#include "Document.h"
#include "DocViewer.h"
//#include "EditorSettings.h"
#include "JuffMW.h"
#include "Log.h"
#include "NullDoc.h"
#include "MainSettings.h"
#include "PluginManager.h"
#include "Project.h"
#include "SearchEngine.h"
#include "Settings.h"
#include "StatusLabel.h"
#include "Utils.h"
#include "ui/settings/SettingsDlg.h"
#include "SciDocEngine.h"
static const int RecentFilesCount = 10;
JuffEd::JuffEd() : Juff::PluginNotifier(), Juff::DocHandlerInt() {
mw_ = new JuffMW();
initActions();
viewer_ = new Juff::DocViewer(this);
mw_->setMainWidget(viewer_);
settingsDlg_ = new SettingsDlg(mw_);
connect(settingsDlg_, SIGNAL(applied()), SLOT(onSettingsApplied()));
connect(viewer_, SIGNAL(docActivated(Juff::Document*)), SLOT(onDocActivated(Juff::Document*)));
connect(mw_, SIGNAL(closeRequested(bool&)), SLOT(onCloseRequested(bool&)));
// UI must be initialized before engines and plugins because
// we need menus to be already created when loading engines and plugins.
initUI();
loadEngines();
pluginMgr_ = new PluginManager(this, this);
// buildUI() *must* go before loadPlugins() because
// it creates structures and widgets expected by loadPlugins()
buildUI();
loadPlugins();
search_ = new SearchEngine(this, mw_);
setSessionName( MainSettings::get( MainSettings::LastSession ) );
if ( !loadSession() ) {
slotFileNew();
}
if ( viewer_->docCount(Juff::PanelAll) == 0 ) {
openDoc("", Juff::PanelLeft);
viewer_->hidePanel(Juff::PanelRight);
}
else if ( viewer_->docCount(Juff::PanelLeft) == 0 ) {
viewer_->hidePanel(Juff::PanelLeft);
}
else if ( viewer_->docCount(Juff::PanelRight) == 0 ) {
viewer_->hidePanel(Juff::PanelRight);
}
onSettingsApplied();
mw_->restoreState();
}
JuffEd::~JuffEd() {
Settings::instance()->write("juff", "juffed");
}
void JuffEd::initActions() {
CommandStorageInt* st = Juff::Utils::commandStorage();
st->addAction(FILE_NEW, tr("&New"), this, SLOT(slotFileNew()));
st->addAction(FILE_OPEN, tr("&Open"), this, SLOT(slotFileOpen()));
st->addAction(FILE_SAVE, tr("&Save"), this, SLOT(slotFileSave()));
st->addAction(FILE_SAVE_AS, tr("Save as"), this, SLOT(slotFileSaveAs()));
st->addAction(FILE_SAVE_ALL, tr("Save all"), this, SLOT(slotFileSaveAll()));
st->addAction(FILE_RELOAD, tr("&Reload"), this, SLOT(slotFileReload()));
st->addAction(FILE_RENAME, tr("Rename"), this, SLOT(slotFileRename()));
st->addAction(FILE_CLOSE, tr("Close"), this, SLOT(slotFileClose()));
st->addAction(FILE_CLOSE_ALL, tr("Close All"), this, SLOT(slotFileCloseAll()));
st->addAction(FILE_PRINT, tr("&Print"), this, SLOT(slotFilePrint()));
st->addAction(FILE_EXIT, tr("Exit"), this, SLOT(slotFileExit()));
st->addAction(SESSION_NEW, tr("New session"), this, SLOT(slotSessionNew()));
st->addAction(SESSION_OPEN, tr("Open session"), this, SLOT(slotSessionOpen()));
st->addAction(SESSION_SAVE, tr("Save session as..."), this, SLOT(slotSessionSaveAs()));
st->addAction(EDIT_UNDO, tr("Undo"), this, SLOT(slotEditUndo()));
st->addAction(EDIT_REDO, tr("Redo"), this, SLOT(slotEditRedo()));
st->addAction(EDIT_CUT, tr("Cut"), this, SLOT(slotEditCut()));
st->addAction(EDIT_COPY, tr("Copy"), this, SLOT(slotEditCopy()));
st->addAction(EDIT_PASTE, tr("Paste"), this, SLOT(slotEditPaste()));
st->addAction(SEARCH_FIND, tr("Find"), this, SLOT(slotFind()));
st->addAction(SEARCH_FIND_NEXT, tr("Find next"), this, SLOT(slotFindNext()));
st->addAction(SEARCH_FIND_PREV, tr("Find previous"), this, SLOT(slotFindPrev()));
st->addAction(SEARCH_REPLACE, tr("Replace"), this, SLOT(slotReplace()));
st->addAction(SEARCH_GOTO_LINE, tr("Go to line"), this, SLOT(slotGotoLine()));
st->addAction(SEARCH_GOTO_FILE, tr("Go to file"), this, SLOT(slotGotoFile()));
st->addAction(VIEW_ZOOM_IN, tr("Zoom In"), this, SLOT(slotZoomIn()));
st->addAction(VIEW_ZOOM_OUT, tr("Zoom Out"), this, SLOT(slotZoomOut()));
st->addAction(VIEW_ZOOM_100, tr("Zoom 100%"), this, SLOT(slotZoom100()));
st->addAction(VIEW_FULLSCREEN, tr("Fullscreen"), this, SLOT(slotFullscreen()));
st->addAction(TOOLS_SETTINGS, tr("Settings"), this, SLOT(slotSettings()));
st->addAction(HELP_ABOUT, tr("About"), mw_, SLOT(slotAbout()));
st->addAction(HELP_ABOUT_QT, tr("About Qt"), mw_, SLOT(slotAboutQt()));
}
void JuffEd::initUI() {
menus_[Juff::MenuFile] = new QMenu(tr("&File"));
menus_[Juff::MenuEdit] = new QMenu(tr("&Edit"));
menus_[Juff::MenuView] = new QMenu(tr("&View"));
menus_[Juff::MenuSearch] = new QMenu(tr("&Search"));
menus_[Juff::MenuFormat] = new QMenu(tr("For&mat"));
menus_[Juff::MenuTools] = new QMenu(tr("&Tools"));
menus_[Juff::MenuHelp] = new QMenu(tr("&Help"));
// recent files
recentFilesMenu_ = new QMenu(JuffEd::tr("Recent files"));
connect(recentFilesMenu_, SIGNAL(aboutToShow()), SLOT(initRecentFilesMenu()));
QString recentFiles = MainSettings::get(MainSettings::RecentFiles);
if ( !recentFiles.isEmpty() ) {
QStringList fileList = recentFiles.split(";");
int count = fileList.count();
for(int i = count - 1; i >= 0; --i) {
const QString& fileName = fileList.at(i);
if ( !fileName.isEmpty() )
addToRecentFiles(fileName);
}
}
openWithCharsetGr_ = new QActionGroup(this);
setCharsetGr_ = new QActionGroup(this);
charsetMenu_ = new QMenu(JuffEd::tr("Charset"));
openWithCharsetMenu_ = new QMenu(JuffEd::tr("Open with charset..."));
setCharsetMenu_ = new QMenu(JuffEd::tr("Set charset"));
initCharsetMenus();
charsetMenu_->addMenu(openWithCharsetMenu_);
charsetMenu_->addMenu(setCharsetMenu_);
docksMenu_ = new QMenu(tr("Docks"));
toolBarsMenu_ = new QMenu(tr("Toolbars"));
menus_[Juff::MenuTools]->addMenu(docksMenu_);
menus_[Juff::MenuTools]->addMenu(toolBarsMenu_);
CommandStorageInt* st = Juff::Utils::commandStorage();
QToolBar* mainToolBar = new QToolBar("mainToolBar");
mainToolBar->setObjectName("mainToolBar");
mainToolBar->addAction(st->action(FILE_NEW));
mainToolBar->addAction(st->action(FILE_OPEN));
mainToolBar->addAction(st->action(FILE_SAVE));
mainToolBar->addSeparator();
mainToolBar->addAction(st->action(FILE_PRINT));
mainToolBar->addSeparator();
mainToolBar->addAction(st->action(EDIT_UNDO));
mainToolBar->addAction(st->action(EDIT_REDO));
mainToolBar->addSeparator();
mainToolBar->addAction(st->action(EDIT_CUT));
mainToolBar->addAction(st->action(EDIT_COPY));
mainToolBar->addAction(st->action(EDIT_PASTE));
mainToolBar->addSeparator();
mainToolBar->addAction(st->action(SEARCH_FIND));
mw_->addToolBar(mainToolBar);
}
void JuffEd::loadEngines() {
SciDocEngine* sciEng = new SciDocEngine();
engines_[sciEng->type()] = sciEng;
QStringList titles;
QWidgetList pages;
if ( sciEng->getSettingsPages(titles, pages) ) {
settingsDlg_->addPages(titles, pages);
}
QList<Juff::DocEngine::ColorOption> colorOpts;
sciEng->getColorOptions(colorOpts);
foreach (Juff::DocEngine::ColorOption option, colorOpts) {
settingsDlg_->addColorSetting(option.title, option.section, option.key, option.defaultColor);
}
}
void JuffEd::loadPlugins() {
pluginMgr_->loadPlugins(settingsDlg_);
// docks
QList<Qt::DockWidgetArea> positions;
QList<bool> visibility;
QWidgetList widgets = pluginMgr_->docks(positions, visibility);
int index = 0;
foreach (QWidget* w, widgets) {
QString title = w->windowTitle();
QDockWidget* dock = new QDockWidget(title);
dock->setObjectName(title);
dock->setWidget(w);
mw_->addDockWidget(positions[index], dock);
dock->setVisible(visibility[index]);
docksMenu_->addAction(dock->toggleViewAction());
++index;
}
// toolbars
Juff::ToolBarList toolbars = pluginMgr_->toolbars();
foreach (QToolBar* bar, toolbars) {
if ( bar != NULL ) {
mw_->addToolBar(bar);
toolBarsMenu_->addAction(bar->toggleViewAction());
}
}
// load plugin actions into menues
QMapIterator<Juff::MenuID, QMenu*> menuiter(menus_);
while (menuiter.hasNext()) {
menuiter.next();
Juff::ActionList acts = pluginMgr_->actions(menuiter.key());
if ( acts.count() > 0 ) {
menuiter.value()->addSeparator();
menuiter.value()->addActions(acts);
}
}
// menus
Juff::MenuList menus = pluginMgr_->menus();
foreach (QMenu* menu, menus) {
mw_->insertMenu(menus_[Juff::MenuTools], menu);
}
}
void JuffEd::buildUI() {
CommandStorageInt* st = Juff::Utils::commandStorage();
// sessions menu
QMenu* sessionMenu = new QMenu(tr("Session"));
sessionMenu->addAction(st->action(SESSION_NEW));
sessionMenu->addAction(st->action(SESSION_OPEN));
sessionMenu->addAction(st->action(SESSION_SAVE));
// FILE
QMenu* menu = menus_[Juff::MenuFile];
menu->addAction(st->action(FILE_NEW));
menu->addAction(st->action(FILE_OPEN));
menu->addMenu(recentFilesMenu_);
menu->addAction(st->action(FILE_SAVE));
menu->addAction(st->action(FILE_SAVE_AS));
menu->addAction(st->action(FILE_SAVE_ALL));
menu->addAction(st->action(FILE_RENAME));
menu->addAction(st->action(FILE_RELOAD));
menu->addSeparator();
menu->addMenu( sessionMenu );
menu->addSeparator();
menu->addAction(st->action(FILE_CLOSE));
menu->addAction(st->action(FILE_CLOSE_ALL));
menu->addAction(st->action(FILE_PRINT));
menu->addSeparator();
menu->addAction(st->action(FILE_EXIT));
// EDIT
menu = menus_[Juff::MenuEdit];
menu->addAction(st->action(EDIT_UNDO));
menu->addAction(st->action(EDIT_REDO));
menu->addSeparator();
menu->addAction(st->action(EDIT_CUT));
menu->addAction(st->action(EDIT_COPY));
menu->addAction(st->action(EDIT_PASTE));
menu->addSeparator();
// VIEW
menu = menus_[Juff::MenuView];
menu->addAction(st->action(VIEW_FULLSCREEN));
menu->addSeparator();
menu->addAction(st->action(VIEW_ZOOM_IN));
menu->addAction(st->action(VIEW_ZOOM_OUT));
menu->addAction(st->action(VIEW_ZOOM_100));
menu->addSeparator();
// SEARCH
menu = menus_[Juff::MenuSearch];
menu->addAction(st->action(SEARCH_FIND));
menu->addAction(st->action(SEARCH_FIND_NEXT));
menu->addAction(st->action(SEARCH_FIND_PREV));
menu->addAction(st->action(SEARCH_REPLACE));
menu->addSeparator();
menu->addAction(st->action(SEARCH_GOTO_LINE));
menu->addAction(st->action(SEARCH_GOTO_FILE));
menu->addSeparator();
// FORMAT
menu = menus_[Juff::MenuFormat];
menu->addMenu(charsetMenu_);
// TOOLS
menu = menus_[Juff::MenuTools];
menu->addAction(st->action(TOOLS_SETTINGS));
// HELP
menu = menus_[Juff::MenuHelp];
menu->addAction(st->action(HELP_ABOUT));
menu->addAction(st->action(HELP_ABOUT_QT));
Juff::MenuID menuIDs[] = { Juff::MenuFile, Juff::MenuEdit, Juff::MenuView, Juff::MenuSearch,
Juff::MenuFormat, Juff::MenuTools, Juff::MenuHelp };
int i = 0;
while ( true ) {
Juff::MenuID id = menuIDs[i++];
QMenu* m = menus_.value(id, NULL);
if ( m != NULL ) {
// add engines' items
foreach(Juff::DocEngine* eng, engines_) {
Juff::ActionList actions = eng->mainMenuActions(id);
foreach (QAction* act, actions) {
m->addAction(act);
}
}
// add menu to the menubar
mw_->addMenu(m);
}
if ( id == Juff::MenuLAST )
break;
}
// statusbar
posL_ = new Juff::StatusLabel("");
nameL_ = new Juff::StatusLabel("");
charsetL_ = new Juff::StatusLabel("");
linesL_ = new Juff::StatusLabel("");
posL_->setToolTip(JuffEd::tr("Cursor position"));
nameL_->setToolTip(JuffEd::tr("File full name"));
charsetL_->setToolTip(JuffEd::tr("Current character set"));
linesL_->setToolTip(JuffEd::tr("Lines count"));
posL_->setMinimumWidth(130);
charsetL_->setMenu(charsetMenu_);
charsetL_->setAlignment(Qt::AlignCenter);
connect(linesL_, SIGNAL(clicked()), SLOT(slotGotoLine()));
connect(posL_, SIGNAL(clicked()), SLOT(slotGotoLine()));
QMenu* filePathMenu = new QMenu();
filePathMenu->addAction(Juff::Utils::iconManager()->icon(EDIT_COPY), tr("Copy"), this, SLOT(slotCopyFilePath()));
nameL_->setMenu(filePathMenu);
mw_->addStatusWidget(posL_, 100);
mw_->addStatusWidget(nameL_, -1);
mw_->addStatusWidget(linesL_, 80);
mw_->addStatusWidget(charsetL_, 100);
posL_->hide();
nameL_->hide();
charsetL_->hide();
linesL_->hide();
// status widgets from engines
foreach(Juff::DocEngine* eng, engines_) {
QWidgetList statusWidgets = eng->statusWidgets();
foreach (QWidget* w, statusWidgets) {
mw_->addStatusWidget(w, -1);
w->show();
}
}
}
void JuffEd::initCharsetMenus() {
openWithCharsetMenu_->clear();
setCharsetMenu_->clear();
foreach (QAction* a, openWithCharsetGr_->actions())
openWithCharsetGr_->removeAction(a);
QStringList charsets = CharsetSettings::getCharsetsList();
foreach (QString charset, charsets) {
if ( CharsetSettings::charsetEnabled(charset) ) {
QAction* openAct = openWithCharsetMenu_->addAction(charset, this, SLOT(slotOpenWithCharset()));
openAct->setCheckable(true);
openWithCharsetGr_->addAction(openAct);
QAction* setAct = setCharsetMenu_->addAction(charset, this, SLOT(slotSetCharset()));
setAct->setCheckable(true);
setCharsetGr_->addAction(setAct);
// mInt_->charsetActions_[charset] = action;
}
}
}
void JuffEd::initRecentFilesMenu() {
recentFilesMenu_->clear();
foreach (QString fileName, recentFiles_) {
recentFilesMenu_->addAction(fileName, this, SLOT(slotFileRecent()));
}
if ( recentFiles_.count() == 0 )
recentFilesMenu_->setEnabled(false);
else
recentFilesMenu_->setEnabled(true);
}
QWidget* JuffEd::mainWindow() const {
return mw_;
}
void JuffEd::onMessageReceived(const QString& msg) {
// LOGGER;
QStringList params = msg.split("\n");
params.removeFirst();
foreach (QString param, params) {
if ( QFileInfo(param).exists() )
openDoc(param);
}
}
void JuffEd::onCloseRequested(bool& confirm) {
// LOGGER;
QMap <QString, Juff::Document*> unsaved;
QList<Juff::Document*> docs = viewer_->docList(Juff::PanelAll);
foreach (Juff::Document* doc, docs)
if ( doc->isModified() )
unsaved[doc->fileName()] = doc;
confirm = true;
if ( !unsaved.isEmpty() ) {
QStringList toSave;
// ask for list of files needed to save
if ( mw_->askForSave(unsaved.keys(), toSave) ) {
foreach (QString fileName, toSave) {
Juff::Document* doc = unsaved.value(fileName, 0);
if ( 0 != doc ) {
if ( Juff::Document::isNoname(fileName) ) {
if ( !saveDocAs(doc) ) {
// saving failed or was cancelled
confirm = false;
}
}
else {
if ( !saveDoc(doc) ) {
// saving failed
confirm = false;
}
}
}
}
}
else {
// closing was cancelled
confirm = false;
}
}
if ( confirm ) {
mw_->saveState();
saveCurSession();
MainSettings::set( MainSettings::LastSession, _sessionName );
}
}
void JuffEd::onSettingsApplied() {
initCharsetMenus();
// TODO : apply shortcuts
// CommandStorage::instance()->updateShortcuts();
pluginMgr_->applySettings();
viewer_->applySettings();
mw_->applySettings();
// notify plugins
emit settingsApplied();
}
////////////////////////////////////////////////////////////////////////////////
// Public slots from QActions (menus and toolbars)
////////////////////////////////////////////////////////////////////////////////
//
void JuffEd::slotFileNew() {
// LOGGER;
openDoc("");
}
void JuffEd::slotFileOpen() {
// TODO : proper filters here
QString filters = "All files (*)";
QStringList files = mw_->getOpenFileNames(openDialogDirectory(), filters);
QString fileName;
foreach (fileName, files) {
openDoc(fileName);
addToRecentFiles(fileName);
}
// store the last used directory
MainSettings::set(MainSettings::LastDir, QFileInfo(fileName).absolutePath());
}
void JuffEd::slotFileRecent() {
// LOGGER;
QAction* act = qobject_cast<QAction*>(sender());
if ( act != 0 ) {
openDoc(act->text());
addToRecentFiles(act->text());
}
}
void JuffEd::slotFileSave() {
Juff::Document* doc = curDoc();
if ( doc->isNull() )
return;
if ( doc->isNoname() ) {
slotFileSaveAs();
}
else {
saveDoc(doc);
}
}
void JuffEd::slotFileSaveAs() {
Juff::Document* doc = curDoc();
if ( doc->isNull() )
return;
saveDocAs(doc);
}
void JuffEd::slotFileRename() {
Juff::Document* doc = curDoc();
if ( doc->isNull() || doc->isNoname() )
return;
QString oldName = doc->fileName();
QFileInfo fi(oldName);
QString newName = mw_->getRenameFileName(fi.fileName());
if ( !newName.isEmpty() ) {
QString fullNewName = fi.absolutePath() + "/" + newName;
if ( QFile::rename(doc->fileName(), fullNewName) ) {
doc->setFileName(fullNewName);
}
}
}
void JuffEd::slotFileSaveAll() {
QList<Juff::Document*> docs = viewer_->docList(Juff::PanelAll);
foreach (Juff::Document* doc, docs) {
if ( doc->isNoname() )
saveDocAs(doc);
else
saveDoc(doc);
}
}
void JuffEd::slotFileReload() {
Juff::Document* doc = curDoc();
if ( doc->isNull() )
return;
doc->reload();
}
void JuffEd::slotFileClose() {
Juff::Document* doc = curDoc();
if ( doc->isNull() )
return;
closeDocWithConfirmation(doc);
}
void JuffEd::slotFileCloseAll() {
closeAllDocs(Juff::PanelAll);
}
void JuffEd::slotFilePrint() {
// LOGGER;
Juff::Document* doc = curDoc();
if ( doc->isNull() )
return;
doc->print();
}
void JuffEd::slotFileExit() {
// LOGGER;
bool exit;
onCloseRequested(exit);
if ( exit ) {
qApp->quit();
}
}
void JuffEd::slotSessionNew() {
// LOGGER;
if ( closeAllDocs( Juff::PanelAll ) ) {
setSessionName( "" );
}
}
void JuffEd::slotSessionOpen() {
// LOGGER;
saveCurSession();
bool accepted = false;
QString sessName = mw_->getOpenSessionName( accepted );
if ( accepted && closeSession() ) {
if ( !sessName.isEmpty() ) {
// open session
setSessionName( sessName );
if ( loadSession() ) {
}
}
else {
// new session
slotSessionNew();
}
}
}
void JuffEd::slotSessionSaveAs() {
// LOGGER;
QString sessionName = QInputDialog::getText( mw_, tr("Save session as..."), tr("Session name") );
if ( !sessionName.isEmpty() ) {
setSessionName( sessionName );
saveCurSession();
}
}
void JuffEd::slotEditUndo() {
// LOGGER;
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->undo();
}
}
void JuffEd::slotEditRedo() {
// LOGGER;
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->redo();
}
}
void JuffEd::slotEditCut(){
// LOGGER;
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->cut();
}
}
void JuffEd::slotEditCopy() {
// LOGGER;
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->copy();
}
}
void JuffEd::slotEditPaste() {
// LOGGER;
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->paste();
}
}
void JuffEd::slotFind() {
search_->find();
}
void JuffEd::slotFindNext() {
search_->findNext();
}
void JuffEd::slotFindPrev() {
search_->findPrev();
}
void JuffEd::slotReplace() {
search_->replace();
}
void JuffEd::slotGotoLine() {
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
int line = mw_->getGotoLineNumber(doc->lineCount());
if ( line >= 0 )
doc->gotoLine(line);
}
}
void JuffEd::slotGotoFile() {
QStringList list = viewer_->docNamesList(Juff::PanelAll);
for ( int i = 0; i < list.count(); ++i ) {
if ( Juff::Document::isNoname(list[i]) ) {
list.removeAt(i);
--i;
}
}
QString fileName = mw_->getJumpToFileName(list);
if ( !fileName.isEmpty() )
openDoc(fileName);
}
void JuffEd::slotZoomIn(){
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->zoomIn();
}
}
void JuffEd::slotZoomOut(){
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->zoomOut();
}
}
void JuffEd::slotZoom100(){
Juff::Document* doc = curDoc();
if ( !doc->isNull() ) {
doc->zoom100();
}
}
void JuffEd::slotFullscreen() {
mw_->toggleFullscreen();
}
void JuffEd::slotOpenWithCharset() {
Juff::Document* doc = qobject_cast<Juff::Document*>(curDoc());
QAction* action = qobject_cast<QAction*>(sender());
if ( doc != 0 && action != 0 ) {
QString charset = doc->charset();
doc->setCharset(action->text());
doc->reload();
}
}
void JuffEd::slotSetCharset() {
Juff::Document* doc = qobject_cast<Juff::Document*>(curDoc());
QAction* action = qobject_cast<QAction*>(sender());
if ( doc != 0 && action != 0 ) {
QString charset = doc->charset();
doc->setCharset(action->text());
}
}
void JuffEd::slotSettings() {
settingsDlg_->exec();
}
void JuffEd::slotCopyFilePath() {
// LOGGER;
QString fileName = nameL_->text().trimmed();
if ( !fileName.isEmpty() ) {
QApplication::clipboard()->setText(fileName);
}
}
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Private slots from document events
////////////////////////////////////////////////////////////////////////////////
//
// Send notifications about these events to PluginManager and JuffMW.
// Don't send to DocViewer - it catches them by its own.
//
void JuffEd::onDocModified(bool) {
Juff::Document* doc = qobject_cast<Juff::Document*>(sender());
if ( doc != 0 ) {
updateMW(doc);
// notify plugins
emit docModified(doc);
}
}
void JuffEd::onDocFocused() {
}
void JuffEd::onDocCursorPosChanged(int, int) {
Juff::Document* doc = qobject_cast<Juff::Document*>(sender());
if ( doc != 0 ) {
updateCursorPos(doc);
}
}
void JuffEd::onDocLineCountChanged(int) {
Juff::Document* doc = qobject_cast<Juff::Document*>(sender());
if ( doc != 0 ) {
updateLineCount(doc);
}
}
void JuffEd::onDocTextChanged() {
Juff::Document* doc = qobject_cast<Juff::Document*>(sender());
if ( doc != 0 ) {
// notify plugins
emit docTextChanged(doc);
}
}
void JuffEd::onDocSyntaxChanged(const QString& oldSyntax) {
Juff::Document* doc = qobject_cast<Juff::Document*>(sender());
if ( doc != 0 ) {
// notify plugins
emit docSyntaxChanged(doc, oldSyntax);
}
}
void JuffEd::onDocCharsetChanged(const QString& oldCharset) {
Juff::Document* doc = qobject_cast<Juff::Document*>(sender());
if ( doc != 0 ) {
// notify plugins
emit docCharsetChanged(doc, oldCharset);
}
}
void JuffEd::onDocRenamed(const QString& oldName) {
// LOGGER;
Juff::Document* doc = qobject_cast<Juff::Document*>(sender());
if ( doc == 0 ) {
return;
}
emit docRenamed(doc, oldName);
viewer_->updateDocTitle(doc);
updateMW(doc);
}
// this slot is connected to a signal from DocViewer
void JuffEd::onDocActivated(Juff::Document* doc) {
// LOGGER;
updateMW(doc);
search_->setCurDoc(doc);
foreach (Juff::DocEngine* eng, engines_) {
eng->onDocActivated(doc);
}
// notify plugins
emit docActivated(doc);
}
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// DocHandlerInt interface implementation
////////////////////////////////////////////////////////////////////////////////
//
Juff::Document* JuffEd::curDoc() const {
return viewer_->currentDoc();
}
Juff::Document* JuffEd::getDoc(const QString& fileName) const {
return viewer_->document(fileName);
}
Juff::Project* JuffEd::curPrj() const {
return NULL;
}
void JuffEd::openDoc(const QString& fileName, Juff::PanelIndex panel, bool addToRecent) {
// First try to activate an existing doc
if ( !viewer_->activateDoc(fileName) ) {
Juff::DocEngine* eng = engineForFileName(fileName);
if ( eng == NULL )
return;
Juff::Document* doc = eng->createDoc(fileName);
connect(doc, SIGNAL(modified(bool)), SLOT(onDocModified(bool)));
connect(doc, SIGNAL(focused()), SLOT(onDocFocused()));
connect(doc, SIGNAL(cursorPosChanged(int, int)), SLOT(onDocCursorPosChanged(int, int)));
connect(doc, SIGNAL(lineCountChanged(int)), SLOT(onDocLineCountChanged(int)));
connect(doc, SIGNAL(textChanged()), SLOT(onDocTextChanged()));
connect(doc, SIGNAL(syntaxChanged(const QString&)), SLOT(onDocSyntaxChanged(const QString&)));
connect(doc, SIGNAL(charsetChanged(const QString&)), SLOT(onDocCharsetChanged(const QString&)));
connect(doc, SIGNAL(renamed(const QString&)), SLOT(onDocRenamed(const QString&)));
connect(doc, SIGNAL(escapePressed()), mw_, SLOT(hideSearchPopup()));
// determine if we need to close single unchanged noname doc
Juff::Document* nonameDocToClose = NULL;
if ( viewer_->docCount(panel) == 1 ) {
nonameDocToClose = viewer_->documentAt(0, panel);
if ( !nonameDocToClose->isNoname() || nonameDocToClose->isModified() || doc->isNoname() ) {
nonameDocToClose = NULL;
}
}
viewer_->addDoc(doc, panel);
doc->setFocus();
search_->setCurDoc(doc);
// close single unchanged noname doc
if ( nonameDocToClose != NULL ) {
closeDocWithConfirmation(nonameDocToClose);
}
// notify plugins
emit docOpened(doc, panel);
QFileInfo fi(doc->fileName());
QString fileNameAdjusted = doc->fileName();
static const int lengthLimit = 60;
static const int partLength = 28;
if ( fileNameAdjusted.length() > lengthLimit ) {
fileNameAdjusted = fileNameAdjusted.left(partLength) + "..." + fileNameAdjusted.right(partLength);
}
// check for existance
if ( !doc->isNoname() && !fi.exists() ) {
mw_->message(QIcon(), "", tr("File '%1' doesn't exist").arg(fileNameAdjusted));
}
else {
// check for read-only
if ( !doc->isNoname() ) {
if ( !fi.isWritable() ) {
mw_->message(QIcon(), "", tr("File '%1' is read-only").arg(fileNameAdjusted));
}
}
}
if ( addToRecent && !doc->isNoname() ) {
addToRecentFiles(doc->fileName());
}
doc->applySettings();
}
}
void JuffEd::closeDoc(const QString& fileName) {
Juff::Document* doc = viewer_->document(fileName);
if ( !doc->isNull() )
closeDocWithConfirmation(doc);
}