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"
36 QSvgRenderer
* svgStamps();
39 std::auto_ptr
< QSvgRenderer
> svgStampFile
;
42 QSvgRenderer
* GuiUtilsHelper::svgStamps()
44 if ( !svgStampFile
.get() )
46 const QString stampFile
= KStandardDirs::locate( "data", "okular/pics/stamps.svg" );
47 if ( !stampFile
.isEmpty() )
49 svgStampFile
.reset( new QSvgRenderer( stampFile
) );
50 if ( !svgStampFile
->isValid() )
56 return svgStampFile
.get();
59 K_GLOBAL_STATIC( GuiUtilsHelper
, s_data
)
63 QString
captionForAnnotation( const Okular::Annotation
* ann
)
68 switch( ann
->subType() )
70 case Okular::Annotation::AText
:
71 if( ( (Okular::TextAnnotation
*)ann
)->textType() == Okular::TextAnnotation::Linked
)
74 ret
= i18n( "Inline Note" );
76 case Okular::Annotation::ALine
:
79 case Okular::Annotation::AGeom
:
80 ret
= i18n( "Geometry" );
82 case Okular::Annotation::AHighlight
:
83 ret
= i18n( "Highlight" );
85 case Okular::Annotation::AStamp
:
86 ret
= i18n( "Stamp" );
88 case Okular::Annotation::AInk
:
91 case Okular::Annotation::ACaret
:
92 ret
= i18n( "Caret" );
94 case Okular::Annotation::AFileAttachment
:
95 ret
= i18n( "File Attachment" );
97 case Okular::Annotation::ASound
:
98 ret
= i18n( "Sound" );
100 case Okular::Annotation::AMovie
:
101 ret
= i18n( "Movie" );
103 case Okular::Annotation::A_BASE
:
109 QString
authorForAnnotation( const Okular::Annotation
* ann
)
113 return !ann
->author().isEmpty() ? ann
->author() : i18nc( "Unknown author", "Unknown" );
116 QString
contents( const Okular::Annotation
* ann
)
121 QString ret
= ann
->window().text();
122 if ( !ret
.isEmpty() )
124 // 2. if Text and InPlace, the inplace text
125 if ( ann
->subType() == Okular::Annotation::AText
)
127 const Okular::TextAnnotation
* txtann
= static_cast< const Okular::TextAnnotation
* >( ann
);
128 if ( txtann
->textType() == Okular::TextAnnotation::InPlace
)
130 ret
= txtann
->inplaceText();
131 if ( !ret
.isEmpty() )
137 ret
= ann
->contents();
142 QString
contentsHtml( const Okular::Annotation
* ann
)
144 QString text
= Qt::escape( contents( ann
) );
145 text
.replace( "\n", "<br>" );
149 QString
prettyToolTip( const Okular::Annotation
* ann
)
153 QString author
= authorForAnnotation( ann
);
154 QString contents
= contentsHtml( ann
);
156 QString tooltip
= QString( "<qt><b>" ) + i18n( "Author: %1", author
) + QString( "</b>" );
157 if ( !contents
.isEmpty() )
158 tooltip
+= QString( "<div style=\"font-size: 4px;\"><hr /></div>" ) + contents
;
165 QPixmap
loadStamp( const QString
& _name
, const QSize
& size
, int iconSize
)
167 const QString name
= _name
.toLower();
168 QSvgRenderer
* r
= 0;
169 if ( ( r
= s_data
->svgStamps() ) && r
->elementExists( name
) )
171 const QRectF stampElemRect
= r
->boundsOnElement( name
);
172 const QRectF
stampRect( size
.isValid() ? QRectF( QPointF( 0, 0 ), size
) : stampElemRect
);
173 QPixmap
pixmap( stampRect
.size().toSize() );
174 pixmap
.fill( Qt::transparent
);
175 QPainter
p( &pixmap
);
176 r
->render( &p
, name
);
181 const KIconLoader
* il
= iconLoader();
183 const int minSize
= iconSize
> 0 ? iconSize
: qMin( size
.width(), size
.height() );
184 pixmap
= il
->loadIcon( name
, KIconLoader::User
, minSize
, KIconLoader::DefaultState
, QStringList(), &path
, true );
185 if ( path
.isEmpty() )
186 pixmap
= il
->loadIcon( name
, KIconLoader::NoGroup
, minSize
);
190 void setIconLoader( KIconLoader
* loader
)
195 KIconLoader
* iconLoader()
197 return s_data
->il
? s_data
->il
: KIconLoader::global();
200 void saveEmbeddedFile( Okular::EmbeddedFile
*ef
, QWidget
*parent
)
202 const QString caption
= i18n( "Where do you want to save %1?", ef
->name() );
203 const QString path
= KFileDialog::getSaveFileName( ef
->name(), QString(), parent
, caption
);
204 if ( path
.isEmpty() )
208 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
)
210 if ( f
.open( QIODevice::WriteOnly
) )
212 f
.write( ef
->data() );
217 KMessageBox::error( parent
, i18n( "Could not open \"%1\" for writing. File was not saved.", path
) );