1 /***************************************************************************
2 * Copyright (C) 2006-2007 by Pino Toscano <pino@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
14 #include <qsvgrenderer.h>
15 #include <qtextdocument.h>
16 #include <kfiledialog.h>
18 #include <kiconloader.h>
20 #include <kmessagebox.h>
21 #include <kstandarddirs.h>
24 #include "core/annotations.h"
25 #include "core/document.h"
37 K_GLOBAL_STATIC( GuiUtilsHelper
, s_data
)
41 QString
captionForAnnotation( const Okular::Annotation
* ann
)
46 switch( ann
->subType() )
48 case Okular::Annotation::AText
:
49 if( ( (Okular::TextAnnotation
*)ann
)->textType() == Okular::TextAnnotation::Linked
)
52 ret
= i18n( "Inline Note" );
54 case Okular::Annotation::ALine
:
57 case Okular::Annotation::AGeom
:
58 ret
= i18n( "Geometry" );
60 case Okular::Annotation::AHighlight
:
61 ret
= i18n( "Highlight" );
63 case Okular::Annotation::AStamp
:
64 ret
= i18n( "Stamp" );
66 case Okular::Annotation::AInk
:
69 case Okular::Annotation::ACaret
:
70 ret
= i18n( "Caret" );
72 case Okular::Annotation::AFileAttachment
:
73 ret
= i18n( "File Attachment" );
75 case Okular::Annotation::ASound
:
76 ret
= i18n( "Sound" );
78 case Okular::Annotation::AMovie
:
79 ret
= i18n( "Movie" );
81 case Okular::Annotation::A_BASE
:
87 QString
authorForAnnotation( const Okular::Annotation
* ann
)
91 return !ann
->author().isEmpty() ? ann
->author() : i18nc( "Unknown author", "Unknown" );
94 QString
contents( const Okular::Annotation
* ann
)
99 QString ret
= ann
->window().text();
100 if ( !ret
.isEmpty() )
102 // 2. if Text and InPlace, the inplace text
103 if ( ann
->subType() == Okular::Annotation::AText
)
105 const Okular::TextAnnotation
* txtann
= static_cast< const Okular::TextAnnotation
* >( ann
);
106 if ( txtann
->textType() == Okular::TextAnnotation::InPlace
)
108 ret
= txtann
->inplaceText();
109 if ( !ret
.isEmpty() )
115 ret
= ann
->contents();
120 QString
contentsHtml( const Okular::Annotation
* ann
)
122 QString text
= Qt::escape( contents( ann
) );
123 text
.replace( "\n", "<br>" );
127 QString
prettyToolTip( const Okular::Annotation
* ann
)
131 QString author
= authorForAnnotation( ann
);
132 QString contents
= contentsHtml( ann
);
134 QString tooltip
= QString( "<qt><b>" ) + i18n( "Author: %1", author
) + QString( "</b>" );
135 if ( !contents
.isEmpty() )
136 tooltip
+= QString( "<div style=\"font-size: 4px;\"><hr /></div>" ) + contents
;
143 QPixmap
loadStamp( const QString
& _name
, const QSize
& size
, int iconSize
)
145 const QString name
= _name
.toLower();
146 if ( name
.startsWith( QLatin1String( "stamp-" ) ) )
148 QSvgRenderer
r( KStandardDirs::locate( "data", QString::fromLatin1( "okular/pics/" ) + name
+ ".svg" ) );
151 QPixmap
pixmap( size
.isValid() ? size
: r
.defaultSize() );
152 pixmap
.fill( Qt::transparent
);
153 QPainter
p( &pixmap
);
160 const KIconLoader
* il
= iconLoader();
162 const int minSize
= iconSize
> 0 ? iconSize
: qMin( size
.width(), size
.height() );
163 pixmap
= il
->loadIcon( name
, KIconLoader::User
, minSize
, KIconLoader::DefaultState
, QStringList(), &path
, true );
164 if ( path
.isEmpty() )
165 pixmap
= il
->loadIcon( name
, KIconLoader::NoGroup
, minSize
);
169 void setIconLoader( KIconLoader
* loader
)
174 KIconLoader
* iconLoader()
176 return s_data
->il
? s_data
->il
: KIconLoader::global();
179 void saveEmbeddedFile( Okular::EmbeddedFile
*ef
, QWidget
*parent
)
181 const QString caption
= i18n( "Where do you want to save %1?", ef
->name() );
182 const QString path
= KFileDialog::getSaveFileName( ef
->name(), QString(), parent
, caption
);
183 if ( path
.isEmpty() )
187 if ( !f
.exists() || KMessageBox::warningContinueCancel( parent
, i18n( "A file named \"%1\" already exists. Are you sure you want to overwrite it?", path
), QString(), KGuiItem( i18nc( "@action:button", "&Overwrite" ) ) ) == KMessageBox::Continue
)
189 if ( f
.open( QIODevice::WriteOnly
) )
191 f
.write( ef
->data() );
196 KMessageBox::error( parent
, i18n( "Could not open \"%1\" for writing. File was not saved.", path
) );