forked from KDE/okular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part.cpp
3077 lines (2618 loc) · 103 KB
/
part.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) 2002 by Wilco Greven <[email protected]> *
* Copyright (C) 2002 by Chris Cheney <[email protected]> *
* Copyright (C) 2002 by Malcolm Hunter <[email protected]> *
* Copyright (C) 2003-2004 by Christophe Devriese *
* <[email protected]> *
* Copyright (C) 2003 by Daniel Molkentin <[email protected]> *
* Copyright (C) 2003 by Andy Goossens <[email protected]> *
* Copyright (C) 2003 by Dirk Mueller <[email protected]> *
* Copyright (C) 2003 by Laurent Montel <[email protected]> *
* Copyright (C) 2004 by Dominique Devriese <[email protected]> *
* Copyright (C) 2004 by Christoph Cullmann <[email protected]> *
* Copyright (C) 2004 by Henrique Pinto <[email protected]> *
* Copyright (C) 2004 by Waldo Bastian <[email protected]> *
* Copyright (C) 2004-2008 by Albert Astals Cid <[email protected]> *
* Copyright (C) 2004 by Antti Markus <[email protected]> *
* *
* 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 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "part.h"
// qt/kde includes
#include <QApplication>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFile>
#include <QFileDialog>
#include <QIcon>
#include <QInputDialog>
#include <QLayout>
#include <QLabel>
#include <QMenu>
#include <QTimer>
#include <QTemporaryFile>
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QScrollBar>
#include <QSlider>
#include <QSpinBox>
#include <QStandardPaths>
#include <QWidgetAction>
#include <QContextMenuEvent>
#include <KAboutApplicationDialog>
#include <KActionCollection>
#include <KBookmarkAction>
#include <KBookmarkMenu>
#include <Kdelibs4ConfigMigrator>
#include <Kdelibs4Migration>
#include <KDirWatch>
#include <KFilterBase>
#include <KFilterDev>
#include <KIO/Job>
#include <KJobWidgets>
#include <KMessageBox>
#include <KPasswordDialog>
#include <KPluginFactory>
#include <KPluginMetaData>
#include <KSharedDataCache>
#include <KStandardShortcut>
#include <KToggleAction>
#include <KToggleFullScreenAction>
#include <KWallet>
#include <KXMLGUIClient>
#include <KXMLGUIFactory>
#if 0
#include <knewstuff2/engine.h>
#endif
// local includes
#include "aboutdata.h"
#include "extensions.h"
#include "ui/debug_ui.h"
#include "ui/drawingtoolactions.h"
#include "ui/pageview.h"
#include "ui/toc.h"
#include "ui/searchwidget.h"
#include "ui/thumbnaillist.h"
#include "ui/side_reviews.h"
#include "ui/minibar.h"
#include "ui/embeddedfilesdialog.h"
#include "ui/propertiesdialog.h"
#include "ui/presentationwidget.h"
#include "ui/pagesizelabel.h"
#include "ui/bookmarklist.h"
#include "ui/findbar.h"
#include "ui/sidebar.h"
#include "ui/fileprinterpreview.h"
#include "ui/guiutils.h"
#include "ui/layers.h"
#include "ui/okmenutitle.h"
#include "conf/preferencesdialog.h"
#include "settings.h"
#include "core/action.h"
#include "core/annotations.h"
#include "core/bookmarkmanager.h"
#include "core/document.h"
#include "core/generator.h"
#include "core/page.h"
#include "core/fileprinter.h"
#include <cstdio>
#include <memory>
class FileKeeper
{
public:
FileKeeper()
: m_handle( NULL )
{
}
~FileKeeper()
{
}
void open( const QString & path )
{
if ( !m_handle )
m_handle = std::fopen( QFile::encodeName( path ).constData(), "r" );
}
void close()
{
if ( m_handle )
{
int ret = std::fclose( m_handle );
Q_UNUSED( ret )
m_handle = NULL;
}
}
QTemporaryFile* copyToTemporary() const
{
if ( !m_handle )
return 0;
QTemporaryFile * retFile = new QTemporaryFile;
retFile->open();
std::rewind( m_handle );
int c = -1;
do
{
c = std::fgetc( m_handle );
if ( c == EOF )
break;
if ( !retFile->putChar( (char)c ) )
break;
} while ( !feof( m_handle ) );
retFile->flush();
return retFile;
}
private:
std::FILE * m_handle;
};
K_PLUGIN_FACTORY(OkularPartFactory, registerPlugin<Okular::Part>();)
static QAction* actionForExportFormat( const Okular::ExportFormat& format, QObject *parent = Q_NULLPTR )
{
QAction *act = new QAction( format.description(), parent );
if ( !format.icon().isNull() )
{
act->setIcon( format.icon() );
}
return act;
}
static KFilterDev::CompressionType compressionTypeFor( const QString& mime_to_check )
{
// The compressedMimeMap is here in case you have a very old shared mime database
// that doesn't have inheritance info for things like gzeps, etc
// Otherwise the "is()" calls below are just good enough
static QHash< QString, KFilterDev::CompressionType > compressedMimeMap;
static bool supportBzip = false;
static bool supportXz = false;
const QString app_gzip( QStringLiteral( "application/x-gzip" ) );
const QString app_bzip( QStringLiteral( "application/x-bzip" ) );
const QString app_xz( QStringLiteral( "application/x-xz" ) );
if ( compressedMimeMap.isEmpty() )
{
std::unique_ptr< KFilterBase > f;
compressedMimeMap[ QLatin1String( "image/x-gzeps" ) ] = KFilterDev::GZip;
// check we can read bzip2-compressed files
f.reset( KCompressionDevice::filterForCompressionType( KCompressionDevice::BZip2 ) );
if ( f.get() )
{
supportBzip = true;
compressedMimeMap[ QLatin1String( "application/x-bzpdf" ) ] = KFilterDev::BZip2;
compressedMimeMap[ QLatin1String( "application/x-bzpostscript" ) ] = KFilterDev::BZip2;
compressedMimeMap[ QLatin1String( "application/x-bzdvi" ) ] = KFilterDev::BZip2;
compressedMimeMap[ QLatin1String( "image/x-bzeps" ) ] = KFilterDev::BZip2;
}
// check if we can read XZ-compressed files
f.reset( KCompressionDevice::filterForCompressionType( KCompressionDevice::Xz ) );
if ( f.get() )
{
supportXz = true;
}
}
QHash< QString, KFilterDev::CompressionType >::const_iterator it = compressedMimeMap.constFind( mime_to_check );
if ( it != compressedMimeMap.constEnd() )
return it.value();
QMimeDatabase db;
QMimeType mime = db.mimeTypeForName( mime_to_check );
if ( mime.isValid() )
{
if ( mime.inherits( app_gzip ) )
return KFilterDev::GZip;
else if ( supportBzip && mime.inherits( app_bzip ) )
return KFilterDev::BZip2;
else if ( supportXz && mime.inherits( app_xz ) )
return KFilterDev::Xz;
}
return KFilterDev::None;
}
static Okular::EmbedMode detectEmbedMode( QWidget *parentWidget, QObject *parent, const QVariantList &args )
{
Q_UNUSED( parentWidget );
if ( parent
&& ( parent->objectName().startsWith( QLatin1String( "okular::Shell" ) )
|| parent->objectName().startsWith( QLatin1String( "okular/okular__Shell" ) ) ) )
return Okular::NativeShellMode;
if ( parent
&& ( QByteArray( "KHTMLPart" ) == parent->metaObject()->className() ) )
return Okular::KHTMLPartMode;
Q_FOREACH ( const QVariant &arg, args )
{
if ( arg.type() == QVariant::String )
{
if ( arg.toString() == QLatin1String( "Print/Preview" ) )
{
return Okular::PrintPreviewMode;
}
else if ( arg.toString() == QLatin1String( "ViewerWidget" ) )
{
return Okular::ViewerWidgetMode;
}
}
}
return Okular::UnknownEmbedMode;
}
static QString detectConfigFileName( const QVariantList &args )
{
Q_FOREACH ( const QVariant &arg, args )
{
if ( arg.type() == QVariant::String )
{
QString argString = arg.toString();
int separatorIndex = argString.indexOf( QStringLiteral("=") );
if ( separatorIndex >= 0 && argString.left( separatorIndex ) == QLatin1String( "ConfigFileName" ) )
{
return argString.mid( separatorIndex + 1 );
}
}
}
return QString();
}
#undef OKULAR_KEEP_FILE_OPEN
#ifdef OKULAR_KEEP_FILE_OPEN
static bool keepFileOpen()
{
static bool keep_file_open = !qgetenv("OKULAR_NO_KEEP_FILE_OPEN").toInt();
return keep_file_open;
}
#endif
int Okular::Part::numberOfParts = 0;
namespace Okular
{
Part::Part(QWidget *parentWidget,
QObject *parent,
const QVariantList &args)
: KParts::ReadWritePart(parent),
m_tempfile( 0 ), m_fileWasRemoved( false ), m_showMenuBarAction( 0 ), m_showFullScreenAction( 0 ), m_actionsSearched( false ),
m_cliPresentation(false), m_cliPrint(false), m_embedMode(detectEmbedMode(parentWidget, parent, args)), m_generatorGuiClient(0), m_keeper( 0 )
{
// make sure that the component name is okular otherwise the XMLGUI .rc files are not found
// when this part is used in an application other than okular (e.g. unit tests)
setComponentName(QStringLiteral("okular"), QString());
const QLatin1String configFileName("okularpartrc");
// first, we check if a config file name has been specified
QString configFilePath = detectConfigFileName( args );
if ( configFilePath.isEmpty() )
{
configFilePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + configFileName;
}
// Migrate old config
if ( !QFile::exists( configFilePath ) ) {
qCDebug(OkularUiDebug) << "Did not find a config file, attempting to look for old config";
// Migrate old config + UI
Kdelibs4ConfigMigrator configMigrator( componentName() );
// UI file is handled automatically, we only need to specify config name because we're a part
configMigrator.setConfigFiles( QStringList( configFileName ) );
// If there's no old okular config to migrate, look for kpdf
if ( !configMigrator.migrate() ) {
qCDebug(OkularUiDebug) << "Did not find an old okular config file, attempting to look for kpdf config";
// First try the automatic detection, using $KDEHOME etc.
Kdelibs4Migration migration;
QString kpdfConfig = migration.locateLocal( "config", QStringLiteral("kpdfpartrc") );
// Fallback just in case it tried e. g. ~/.kde4
if ( kpdfConfig.isEmpty() ) {
kpdfConfig = QDir::homePath() + QStringLiteral("/.kde/share/config/kpdfpartrc");
}
if ( QFile::exists( kpdfConfig ) ) {
qCDebug(OkularUiDebug) << "Found old kpdf config" << kpdfConfig << "copying to" << configFilePath;
QFile::copy( kpdfConfig, configFilePath );
} else {
qCDebug(OkularUiDebug) << "Did not find an old kpdf config file";
}
} else {
qCDebug(OkularUiDebug) << "Migrated old okular config";
}
}
Okular::Settings::instance( configFilePath );
numberOfParts++;
if (numberOfParts == 1) {
QDBusConnection::sessionBus().registerObject(QStringLiteral("/okular"), this, QDBusConnection::ExportScriptableSlots);
} else {
QDBusConnection::sessionBus().registerObject(QStringLiteral("/okular%1").arg(numberOfParts), this, QDBusConnection::ExportScriptableSlots);
}
// connect the started signal to tell the job the mimetypes we like,
// and get some more information from it
connect(this, &KParts::ReadOnlyPart::started, this, &Part::slotJobStarted);
// connect the completed signal so we can put the window caption when loading remote files
connect(this, SIGNAL(completed()), this, SLOT(setWindowTitleFromDocument()));
connect(this, &KParts::ReadOnlyPart::canceled, this, &Part::loadCancelled);
// create browser extension (for printing when embedded into browser)
m_bExtension = new BrowserExtension(this);
// create live connect extension (for integrating with browser scripting)
new OkularLiveConnectExtension( this );
GuiUtils::addIconLoader( iconLoader() );
m_sidebar = new Sidebar( parentWidget );
setWidget( m_sidebar );
connect( m_sidebar, &Sidebar::urlsDropped, this, &Part::handleDroppedUrls );
// build the document
m_document = new Okular::Document(widget());
connect( m_document, &Document::linkFind, this, &Part::slotFind );
connect( m_document, &Document::linkGoToPage, this, &Part::slotGoToPage );
connect( m_document, &Document::linkPresentation, this, &Part::slotShowPresentation );
connect( m_document, &Document::linkEndPresentation, this, &Part::slotHidePresentation );
connect( m_document, &Document::openUrl, this, &Part::openUrlFromDocument );
connect( m_document->bookmarkManager(), &BookmarkManager::openUrl, this, &Part::openUrlFromBookmarks );
connect( m_document, &Document::close, this, &Part::close );
if ( parent && parent->metaObject()->indexOfSlot( QMetaObject::normalizedSignature( "slotQuit()" ).constData() ) != -1 )
connect( m_document, SIGNAL(quit()), parent, SLOT(slotQuit()) );
else
connect( m_document, &Document::quit, this, &Part::cannotQuit );
// widgets: ^searchbar (toolbar containing label and SearchWidget)
// m_searchToolBar = new KToolBar( parentWidget, "searchBar" );
// m_searchToolBar->boxLayout()->setSpacing( KDialog::spacingHint() );
// QLabel * sLabel = new QLabel( i18n( "&Search:" ), m_searchToolBar, "kde toolbar widget" );
// m_searchWidget = new SearchWidget( m_searchToolBar, m_document );
// sLabel->setBuddy( m_searchWidget );
// m_searchToolBar->setStretchableWidget( m_searchWidget );
// [left toolbox: Table of Contents] | []
m_toc = new TOC( 0, m_document );
connect( m_toc.data(), &TOC::hasTOC, this, &Part::enableTOC );
m_sidebar->addItem( m_toc, QIcon::fromTheme(QApplication::isLeftToRight() ? QStringLiteral("format-justify-left") : QStringLiteral("format-justify-right")), i18n("Contents") );
enableTOC( false );
// [left toolbox: Layers] | []
m_layers = new Layers( 0, m_document );
connect( m_layers.data(), &Layers::hasLayers, this, &Part::enableLayers );
m_sidebar->addItem( m_layers, QIcon::fromTheme( QStringLiteral("draw-freehand") ), i18n( "Layers" ) );
enableLayers( false );
// [left toolbox: Thumbnails and Bookmarks] | []
QWidget * thumbsBox = new ThumbnailsBox( 0 );
thumbsBox->layout()->setSpacing( 6 );
m_searchWidget = new SearchWidget( thumbsBox, m_document );
thumbsBox->layout()->addWidget(m_searchWidget);
m_thumbnailList = new ThumbnailList( thumbsBox, m_document );
thumbsBox->layout()->addWidget(m_thumbnailList);
// ThumbnailController * m_tc = new ThumbnailController( thumbsBox, m_thumbnailList );
connect( m_thumbnailList.data(), &ThumbnailList::rightClick, this, &Part::slotShowMenu );
m_sidebar->addItem( thumbsBox, QIcon::fromTheme( QStringLiteral("view-preview") ), i18n("Thumbnails") );
m_sidebar->setCurrentItem( thumbsBox );
// [left toolbox: Reviews] | []
m_reviewsWidget = new Reviews( 0, m_document );
m_sidebar->addItem( m_reviewsWidget, QIcon::fromTheme(QStringLiteral("draw-freehand")), i18n("Reviews") );
m_sidebar->setItemEnabled( m_reviewsWidget, false );
// [left toolbox: Bookmarks] | []
m_bookmarkList = new BookmarkList( m_document, 0 );
m_sidebar->addItem( m_bookmarkList, QIcon::fromTheme(QStringLiteral("bookmarks")), i18n("Bookmarks") );
m_sidebar->setItemEnabled( m_bookmarkList, false );
// widgets: [../miniBarContainer] | []
#ifdef OKULAR_ENABLE_MINIBAR
QWidget * miniBarContainer = new QWidget( 0 );
m_sidebar->setBottomWidget( miniBarContainer );
QVBoxLayout * miniBarLayout = new QVBoxLayout( miniBarContainer );
miniBarLayout->setMargin( 0 );
// widgets: [../[spacer/..]] | []
miniBarLayout->addItem( new QSpacerItem( 6, 6, QSizePolicy::Fixed, QSizePolicy::Fixed ) );
// widgets: [../[../MiniBar]] | []
QFrame * bevelContainer = new QFrame( miniBarContainer );
bevelContainer->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
QVBoxLayout * bevelContainerLayout = new QVBoxLayout( bevelContainer );
bevelContainerLayout->setMargin( 4 );
m_progressWidget = new ProgressWidget( bevelContainer, m_document );
bevelContainerLayout->addWidget( m_progressWidget );
miniBarLayout->addWidget( bevelContainer );
miniBarLayout->addItem( new QSpacerItem( 6, 6, QSizePolicy::Fixed, QSizePolicy::Fixed ) );
#endif
// widgets: [] | [right 'pageView']
QWidget * rightContainer = new QWidget( 0 );
m_sidebar->setMainWidget( rightContainer );
QVBoxLayout * rightLayout = new QVBoxLayout( rightContainer );
rightLayout->setMargin( 0 );
rightLayout->setSpacing( 0 );
// KToolBar * rtb = new KToolBar( rightContainer, "mainToolBarSS" );
// rightLayout->addWidget( rtb );
m_topMessage = new KMessageWidget( rightContainer );
m_topMessage->setVisible( false );
m_topMessage->setWordWrap( true );
m_topMessage->setMessageType( KMessageWidget::Information );
m_topMessage->setText( i18n( "This document has embedded files. <a href=\"okular:/embeddedfiles\">Click here to see them</a> or go to File -> Embedded Files." ) );
m_topMessage->setIcon( QIcon::fromTheme( QStringLiteral("mail-attachment") ) );
connect( m_topMessage, &KMessageWidget::linkActivated, this, &Part::slotShowEmbeddedFiles );
rightLayout->addWidget( m_topMessage );
m_formsMessage = new KMessageWidget( rightContainer );
m_formsMessage->setVisible( false );
m_formsMessage->setWordWrap( true );
m_formsMessage->setMessageType( KMessageWidget::Information );
rightLayout->addWidget( m_formsMessage );
m_infoMessage = new KMessageWidget( rightContainer );
m_infoMessage->setVisible( false );
m_infoMessage->setWordWrap( true );
m_infoMessage->setMessageType( KMessageWidget::Information );
rightLayout->addWidget( m_infoMessage );
m_infoTimer = new QTimer();
m_infoTimer->setSingleShot( true );
connect( m_infoTimer, &QTimer::timeout, m_infoMessage, &KMessageWidget::animatedHide );
m_pageView = new PageView( rightContainer, m_document );
QMetaObject::invokeMethod( m_pageView, "setFocus", Qt::QueuedConnection ); //usability setting
// m_splitter->setFocusProxy(m_pageView);
connect( m_pageView.data(), &PageView::rightClick, this, &Part::slotShowMenu );
connect( m_document, &Document::error, this, &Part::errorMessage );
connect( m_document, &Document::warning, this, &Part::warningMessage );
connect( m_document, &Document::notice, this, &Part::noticeMessage );
connect( m_document, &Document::sourceReferenceActivated, this, &Part::slotHandleActivatedSourceReference );
connect( m_pageView.data(), &PageView::fitWindowToPage, this, &Part::fitWindowToPage );
rightLayout->addWidget( m_pageView );
m_layers->setPageView( m_pageView );
m_findBar = new FindBar( m_document, rightContainer );
rightLayout->addWidget( m_findBar );
m_bottomBar = new QWidget( rightContainer );
QHBoxLayout * bottomBarLayout = new QHBoxLayout( m_bottomBar );
m_pageSizeLabel = new PageSizeLabel( m_bottomBar, m_document );
bottomBarLayout->setMargin( 0 );
bottomBarLayout->setSpacing( 0 );
bottomBarLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
m_miniBarLogic = new MiniBarLogic( this, m_document );
m_miniBar = new MiniBar( m_bottomBar, m_miniBarLogic );
bottomBarLayout->addWidget( m_miniBar );
bottomBarLayout->addWidget( m_pageSizeLabel );
rightLayout->addWidget( m_bottomBar );
m_pageNumberTool = new MiniBar( 0, m_miniBarLogic );
connect( m_findBar, SIGNAL(forwardKeyPressEvent(QKeyEvent*)), m_pageView, SLOT(externalKeyPressEvent(QKeyEvent*)));
connect( m_findBar, SIGNAL(onCloseButtonPressed()), m_pageView, SLOT(setFocus()));
connect( m_miniBar, SIGNAL(forwardKeyPressEvent(QKeyEvent*)), m_pageView, SLOT(externalKeyPressEvent(QKeyEvent*)));
connect( m_pageView.data(), &PageView::escPressed, m_findBar, &FindBar::resetSearch );
connect( m_pageNumberTool, SIGNAL(forwardKeyPressEvent(QKeyEvent*)), m_pageView, SLOT(externalKeyPressEvent(QKeyEvent*)));
connect( m_reviewsWidget.data(), &Reviews::openAnnotationWindow,
m_pageView.data(), &PageView::openAnnotationWindow );
// add document observers
m_document->addObserver( this );
m_document->addObserver( m_thumbnailList );
m_document->addObserver( m_pageView );
m_document->registerView( m_pageView );
m_document->addObserver( m_toc );
m_document->addObserver( m_miniBarLogic );
#ifdef OKULAR_ENABLE_MINIBAR
m_document->addObserver( m_progressWidget );
#endif
m_document->addObserver( m_reviewsWidget );
m_document->addObserver( m_pageSizeLabel );
m_document->addObserver( m_bookmarkList );
connect( m_document->bookmarkManager(), &BookmarkManager::saved,
this, &Part::slotRebuildBookmarkMenu );
setupViewerActions();
if ( m_embedMode != ViewerWidgetMode )
{
setupActions();
}
else
{
setViewerShortcuts();
}
// document watcher and reloader
m_watcher = new KDirWatch( this );
connect( m_watcher, &KDirWatch::dirty, this, &Part::slotFileDirty );
m_dirtyHandler = new QTimer( this );
m_dirtyHandler->setSingleShot( true );
connect( m_dirtyHandler, &QTimer::timeout,this, &Part::slotDoFileDirty );
slotNewConfig();
// keep us informed when the user changes settings
connect( Okular::Settings::self(), &KCoreConfigSkeleton::configChanged, this, &Part::slotNewConfig );
#ifdef HAVE_SPEECH
// [SPEECH] check for TTS presence and usability
Okular::Settings::setUseTTS( true );
Okular::Settings::self()->save();
#endif
rebuildBookmarkMenu( false );
if ( m_embedMode == ViewerWidgetMode ) {
// set the XML-UI resource file for the viewer mode
setXMLFile(QStringLiteral("part-viewermode.rc"));
}
else
{
// set our main XML-UI resource file
setXMLFile(QStringLiteral("part.rc"));
}
m_pageView->setupBaseActions( actionCollection() );
m_sidebar->setSidebarVisibility( false );
if ( m_embedMode != PrintPreviewMode )
{
// now set up actions that are required for all remaining modes
m_pageView->setupViewerActions( actionCollection() );
// and if we are not in viewer mode, we want the full GUI
if ( m_embedMode != ViewerWidgetMode )
{
unsetDummyMode();
}
}
// ensure history actions are in the correct state
updateViewActions();
// also update the state of the actions in the page view
m_pageView->updateActionState( false, false, false );
if ( m_embedMode == NativeShellMode )
m_sidebar->setAutoFillBackground( false );
#ifdef OKULAR_KEEP_FILE_OPEN
m_keeper = new FileKeeper();
#endif
}
void Part::setupViewerActions()
{
// ACTIONS
KActionCollection * ac = actionCollection();
// Page Traversal actions
m_gotoPage = KStandardAction::gotoPage( this, SLOT(slotGoToPage()), ac );
ac->setDefaultShortcuts(m_gotoPage, KStandardShortcut::gotoLine());
// dirty way to activate gotopage when pressing miniBar's button
connect( m_miniBar.data(), &MiniBar::gotoPage, m_gotoPage, &QAction::trigger );
connect( m_pageNumberTool.data(), &MiniBar::gotoPage, m_gotoPage, &QAction::trigger );
m_prevPage = KStandardAction::prior(this, SLOT(slotPreviousPage()), ac);
m_prevPage->setIconText( i18nc( "Previous page", "Previous" ) );
m_prevPage->setToolTip( i18n( "Go back to the Previous Page" ) );
m_prevPage->setWhatsThis( i18n( "Moves to the previous page of the document" ) );
ac->setDefaultShortcut(m_prevPage, QKeySequence());
// dirty way to activate prev page when pressing miniBar's button
connect( m_miniBar.data(), &MiniBar::prevPage, m_prevPage, &QAction::trigger );
connect( m_pageNumberTool.data(), &MiniBar::prevPage, m_prevPage, &QAction::trigger );
#ifdef OKULAR_ENABLE_MINIBAR
connect( m_progressWidget, SIGNAL(prevPage()), m_prevPage, SLOT(trigger()) );
#endif
m_nextPage = KStandardAction::next(this, SLOT(slotNextPage()), ac );
m_nextPage->setIconText( i18nc( "Next page", "Next" ) );
m_nextPage->setToolTip( i18n( "Advance to the Next Page" ) );
m_nextPage->setWhatsThis( i18n( "Moves to the next page of the document" ) );
ac->setDefaultShortcut(m_nextPage, QKeySequence());
// dirty way to activate next page when pressing miniBar's button
connect( m_miniBar.data(), &MiniBar::nextPage, m_nextPage, &QAction::trigger );
connect( m_pageNumberTool.data(), &MiniBar::nextPage, m_nextPage, &QAction::trigger );
#ifdef OKULAR_ENABLE_MINIBAR
connect( m_progressWidget, SIGNAL(nextPage()), m_nextPage, SLOT(trigger()) );
#endif
m_beginningOfDocument = KStandardAction::firstPage( this, SLOT(slotGotoFirst()), ac );
ac->addAction(QStringLiteral("first_page"), m_beginningOfDocument);
m_beginningOfDocument->setText(i18n( "Beginning of the document"));
m_beginningOfDocument->setWhatsThis( i18n( "Moves to the beginning of the document" ) );
m_endOfDocument = KStandardAction::lastPage( this, SLOT(slotGotoLast()), ac );
ac->addAction(QStringLiteral("last_page"),m_endOfDocument);
m_endOfDocument->setText(i18n( "End of the document"));
m_endOfDocument->setWhatsThis( i18n( "Moves to the end of the document" ) );
// we do not want back and next in history in the dummy mode
m_historyBack = 0;
m_historyNext = 0;
m_addBookmark = KStandardAction::addBookmark( this, SLOT(slotAddBookmark()), ac );
m_addBookmarkText = m_addBookmark->text();
m_addBookmarkIcon = m_addBookmark->icon();
m_renameBookmark = ac->addAction(QStringLiteral("rename_bookmark"));
m_renameBookmark->setText(i18n( "Rename Bookmark" ));
m_renameBookmark->setIcon(QIcon::fromTheme( QStringLiteral("edit-rename") ));
m_renameBookmark->setWhatsThis( i18n( "Rename the current bookmark" ) );
connect( m_renameBookmark, &QAction::triggered, this, &Part::slotRenameCurrentViewportBookmark );
m_prevBookmark = ac->addAction(QStringLiteral("previous_bookmark"));
m_prevBookmark->setText(i18n( "Previous Bookmark" ));
m_prevBookmark->setIcon(QIcon::fromTheme( QStringLiteral("go-up-search") ));
m_prevBookmark->setWhatsThis( i18n( "Go to the previous bookmark" ) );
connect( m_prevBookmark, &QAction::triggered, this, &Part::slotPreviousBookmark );
m_nextBookmark = ac->addAction(QStringLiteral("next_bookmark"));
m_nextBookmark->setText(i18n( "Next Bookmark" ));
m_nextBookmark->setIcon(QIcon::fromTheme( QStringLiteral("go-down-search") ));
m_nextBookmark->setWhatsThis( i18n( "Go to the next bookmark" ) );
connect( m_nextBookmark, &QAction::triggered, this, &Part::slotNextBookmark );
m_copy = 0;
m_selectAll = 0;
// Find and other actions
m_find = KStandardAction::find( this, SLOT(slotShowFindBar()), ac );
QList<QKeySequence> s = m_find->shortcuts();
s.append( QKeySequence( Qt::Key_Slash ) );
ac->setDefaultShortcuts(m_find, s);
m_find->setEnabled( false );
m_findNext = KStandardAction::findNext( this, SLOT(slotFindNext()), ac);
m_findNext->setEnabled( false );
m_findPrev = KStandardAction::findPrev( this, SLOT(slotFindPrev()), ac );
m_findPrev->setEnabled( false );
m_saveCopyAs = 0;
m_saveAs = 0;
QAction * prefs = KStandardAction::preferences( this, SLOT(slotPreferences()), ac);
if ( m_embedMode == NativeShellMode )
{
prefs->setText( i18n( "Configure Okular..." ) );
}
else
{
// TODO: improve this message
prefs->setText( i18n( "Configure Viewer..." ) );
}
QAction * genPrefs = new QAction( ac );
ac->addAction(QStringLiteral("options_configure_generators"), genPrefs);
if ( m_embedMode == ViewerWidgetMode )
{
genPrefs->setText( i18n( "Configure Viewer Backends..." ) );
}
else
{
genPrefs->setText( i18n( "Configure Backends..." ) );
}
genPrefs->setIcon( QIcon::fromTheme( QStringLiteral("configure") ) );
genPrefs->setEnabled( m_document->configurableGenerators() > 0 );
connect( genPrefs, &QAction::triggered, this, &Part::slotGeneratorPreferences );
m_printPreview = KStandardAction::printPreview( this, SLOT(slotPrintPreview()), ac );
m_printPreview->setEnabled( false );
m_showLeftPanel = 0;
m_showBottomBar = 0;
m_showProperties = ac->addAction(QStringLiteral("properties"));
m_showProperties->setText(i18n("&Properties"));
m_showProperties->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
connect(m_showProperties, &QAction::triggered, this, &Part::slotShowProperties);
m_showProperties->setEnabled( false );
m_showEmbeddedFiles = 0;
m_showPresentation = 0;
m_exportAs = 0;
m_exportAsMenu = 0;
m_exportAsText = 0;
m_exportAsDocArchive = 0;
m_presentationDrawingActions = 0;
m_aboutBackend = ac->addAction(QStringLiteral("help_about_backend"));
m_aboutBackend->setText(i18n("About Backend"));
m_aboutBackend->setEnabled( false );
connect(m_aboutBackend, &QAction::triggered, this, &Part::slotAboutBackend);
QAction *reload = ac->add<QAction>( QStringLiteral("file_reload") );
reload->setText( i18n( "Reloa&d" ) );
reload->setIcon( QIcon::fromTheme( QStringLiteral("view-refresh") ) );
reload->setWhatsThis( i18n( "Reload the current document from disk." ) );
connect( reload, &QAction::triggered, this, &Part::slotReload );
ac->setDefaultShortcuts(reload, KStandardShortcut::reload());
m_reload = reload;
m_closeFindBar = ac->addAction( QStringLiteral("close_find_bar"), this, SLOT(slotHideFindBar()) );
m_closeFindBar->setText( i18n("Close &Find Bar") );
ac->setDefaultShortcut(m_closeFindBar, QKeySequence(Qt::Key_Escape));
m_closeFindBar->setEnabled( false );
QWidgetAction *pageno = new QWidgetAction( ac );
pageno->setText( i18n( "Page Number" ) );
pageno->setDefaultWidget( m_pageNumberTool );
ac->addAction( QStringLiteral("page_number"), pageno );
}
void Part::setViewerShortcuts()
{
KActionCollection * ac = actionCollection();
ac->setDefaultShortcut(m_gotoPage, QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_G));
ac->setDefaultShortcut(m_find, QKeySequence());
ac->setDefaultShortcut(m_findNext, QKeySequence());
ac->setDefaultShortcut(m_findPrev, QKeySequence());
ac->setDefaultShortcut(m_addBookmark, QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_B));
ac->setDefaultShortcut(m_beginningOfDocument, QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Home));
ac->setDefaultShortcut(m_endOfDocument, QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_End));
QAction *action = static_cast<QAction*>( ac->action( QStringLiteral("file_reload") ) );
if (action) {
ac->setDefaultShortcut(action, QKeySequence(Qt::ALT + Qt::Key_F5));
}
}
void Part::setupActions()
{
KActionCollection * ac = actionCollection();
QMimeDatabase db;
m_copy = KStandardAction::create( KStandardAction::Copy, m_pageView, SLOT(copyTextSelection()), ac );
m_selectAll = KStandardAction::selectAll( m_pageView, SLOT(selectAll()), ac );
m_saveCopyAs = KStandardAction::saveAs( this, SLOT(slotSaveCopyAs()), ac );
m_saveCopyAs->setText( i18n( "Save &Copy As..." ) );
ac->addAction( QStringLiteral("file_save_copy"), m_saveCopyAs );
ac->setDefaultShortcuts(m_saveCopyAs, KStandardShortcut::shortcut(KStandardShortcut::SaveAs));
m_saveCopyAs->setEnabled( false );
m_saveAs = KStandardAction::saveAs( this, SLOT(slotSaveFileAs()), ac );
ac->setDefaultShortcuts(m_saveAs, KStandardShortcut::shortcut(KStandardShortcut::Save));
m_saveAs->setEnabled( false );
m_showLeftPanel = ac->add<KToggleAction>(QStringLiteral("show_leftpanel"));
m_showLeftPanel->setText(i18n( "Show &Navigation Panel"));
m_showLeftPanel->setIcon(QIcon::fromTheme( QStringLiteral("view-sidetree") ));
connect( m_showLeftPanel, &QAction::toggled, this, &Part::slotShowLeftPanel );
ac->setDefaultShortcut(m_showLeftPanel, QKeySequence(Qt::Key_F7));
m_showLeftPanel->setChecked( Okular::Settings::showLeftPanel() );
slotShowLeftPanel();
m_showBottomBar = ac->add<KToggleAction>(QStringLiteral("show_bottombar"));
m_showBottomBar->setText(i18n( "Show &Page Bar"));
connect( m_showBottomBar, &QAction::toggled, this, &Part::slotShowBottomBar );
m_showBottomBar->setChecked( Okular::Settings::showBottomBar() );
slotShowBottomBar();
m_showEmbeddedFiles = ac->addAction(QStringLiteral("embedded_files"));
m_showEmbeddedFiles->setText(i18n("&Embedded Files"));
m_showEmbeddedFiles->setIcon( QIcon::fromTheme( QStringLiteral("mail-attachment") ) );
connect(m_showEmbeddedFiles, &QAction::triggered, this, &Part::slotShowEmbeddedFiles);
m_showEmbeddedFiles->setEnabled( false );
m_exportAs = ac->addAction(QStringLiteral("file_export_as"));
m_exportAs->setText(i18n("E&xport As"));
m_exportAs->setIcon( QIcon::fromTheme( QStringLiteral("document-export") ) );
m_exportAsMenu = new QMenu();
connect(m_exportAsMenu, &QMenu::triggered, this, &Part::slotExportAs);
m_exportAs->setMenu( m_exportAsMenu );
m_exportAsText = actionForExportFormat( Okular::ExportFormat::standardFormat( Okular::ExportFormat::PlainText ), m_exportAsMenu );
m_exportAsMenu->addAction( m_exportAsText );
m_exportAs->setEnabled( false );
m_exportAsText->setEnabled( false );
m_exportAsDocArchive = actionForExportFormat( Okular::ExportFormat(
i18nc( "A document format, Okular-specific", "Document Archive" ),
db.mimeTypeForName( QStringLiteral("application/vnd.kde.okular-archive") ) ), m_exportAsMenu );
m_exportAsMenu->addAction( m_exportAsDocArchive );
m_exportAsDocArchive->setEnabled( false );
m_showPresentation = ac->addAction(QStringLiteral("presentation"));
m_showPresentation->setText(i18n("P&resentation"));
m_showPresentation->setIcon( QIcon::fromTheme( QStringLiteral("view-presentation") ) );
connect(m_showPresentation, &QAction::triggered, this, &Part::slotShowPresentation);
ac->setDefaultShortcut(m_showPresentation, QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_P));
m_showPresentation->setEnabled( false );
QAction * importPS = ac->addAction(QStringLiteral("import_ps"));
importPS->setText(i18n("&Import PostScript as PDF..."));
importPS->setIcon(QIcon::fromTheme(QStringLiteral("document-import")));
connect(importPS, &QAction::triggered, this, &Part::slotImportPSFile);
#if 0
QAction * ghns = ac->addAction("get_new_stuff");
ghns->setText(i18n("&Get Books From Internet..."));
ghns->setIcon(QIcon::fromTheme("get-hot-new-stuff"));
connect(ghns, SIGNAL(triggered()), this, SLOT(slotGetNewStuff()));
#endif
KToggleAction *blackscreenAction = new KToggleAction( i18n( "Switch Blackscreen Mode" ), ac );
ac->addAction( QStringLiteral("switch_blackscreen_mode"), blackscreenAction );
ac->setDefaultShortcut(blackscreenAction, QKeySequence(Qt::Key_B));
blackscreenAction->setIcon( QIcon::fromTheme( QStringLiteral("view-presentation") ) );
blackscreenAction->setEnabled( false );
m_presentationDrawingActions = new DrawingToolActions( ac );
QAction *eraseDrawingAction = new QAction( i18n( "Erase Drawings" ), ac );
ac->addAction( QStringLiteral("presentation_erase_drawings"), eraseDrawingAction );
eraseDrawingAction->setIcon( QIcon::fromTheme( QStringLiteral("draw-eraser") ) );
eraseDrawingAction->setEnabled( false );
QAction *configureAnnotations = new QAction( i18n( "Configure Annotations..." ), ac );
ac->addAction( QStringLiteral("options_configure_annotations"), configureAnnotations );
configureAnnotations->setIcon( QIcon::fromTheme( QStringLiteral("configure") ) );
connect(configureAnnotations, &QAction::triggered, this, &Part::slotAnnotationPreferences);
QAction *playPauseAction = new QAction( i18n( "Play/Pause Presentation" ), ac );
ac->addAction( QStringLiteral("presentation_play_pause"), playPauseAction );
playPauseAction->setEnabled( false );
}
Part::~Part()
{
GuiUtils::removeIconLoader( iconLoader() );
m_document->removeObserver( this );
if ( m_document->isOpened() )
Part::closeUrl( false );
delete m_toc;
delete m_layers;
delete m_pageView;
delete m_thumbnailList;
delete m_miniBar;
delete m_pageNumberTool;
delete m_miniBarLogic;
delete m_bottomBar;
#ifdef OKULAR_ENABLE_MINIBAR
delete m_progressWidget;
#endif
delete m_pageSizeLabel;
delete m_reviewsWidget;
delete m_bookmarkList;
delete m_infoTimer;
delete m_document;
delete m_tempfile;
qDeleteAll( m_bookmarkActions );
delete m_exportAsMenu;
#ifdef OKULAR_KEEP_FILE_OPEN
delete m_keeper;
#endif
}
bool Part::openDocument(const QUrl& url, uint page)
{
Okular::DocumentViewport vp( page - 1 );
vp.rePos.enabled = true;
vp.rePos.normalizedX = 0;
vp.rePos.normalizedY = 0;
vp.rePos.pos = Okular::DocumentViewport::TopLeft;
if ( vp.isValid() )
m_document->setNextDocumentViewport( vp );
return openUrl( url );
}
void Part::startPresentation()
{
m_cliPresentation = true;
}
QStringList Part::supportedMimeTypes() const
{
return m_document->supportedMimeTypes();
}
QUrl Part::realUrl() const
{
if ( !m_realUrl.isEmpty() )
return m_realUrl;
return url();
}
// ViewerInterface
void Part::showSourceLocation(const QString& fileName, int line, int column, bool showGraphically)
{
Q_UNUSED(column);
const QString u = QStringLiteral( "src:%1 %2" ).arg( line + 1 ).arg( fileName );
GotoAction action( QString(), u );
m_document->processAction( &action );
if( showGraphically )
{
m_pageView->setLastSourceLocationViewport( m_document->viewport() );
}
}
void Part::clearLastShownSourceLocation()
{
m_pageView->clearLastSourceLocationViewport();
}
bool Part::isWatchFileModeEnabled() const
{
return !m_watcher->isStopped();
}
void Part::setWatchFileModeEnabled(bool enabled)
{
if ( enabled && m_watcher->isStopped() )
{
m_watcher->startScan();
}
else if( !enabled && !m_watcher->isStopped() )
{
m_dirtyHandler->stop();
m_watcher->stopScan();
}
}
bool Part::areSourceLocationsShownGraphically() const
{
return m_pageView->areSourceLocationsShownGraphically();
}
void Part::setShowSourceLocationsGraphically(bool show)
{
m_pageView->setShowSourceLocationsGraphically(show);
}