SVN_SILENT made messages (.desktop file)
[kdegraphics.git] / okular / ui / guiutils.cpp
bloba65934301ac534c507ef46dbfc302cf82b3aff43
1 /***************************************************************************
2 * Copyright (C) 2006-2007 by Pino Toscano <pino@kde.org> *
3 * *
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 ***************************************************************************/
10 #include "guiutils.h"
12 // qt/kde includes
13 #include <qpainter.h>
14 #include <qsvgrenderer.h>
15 #include <qtextdocument.h>
16 #include <kfiledialog.h>
17 #include <kglobal.h>
18 #include <kiconloader.h>
19 #include <klocale.h>
20 #include <kmessagebox.h>
21 #include <kstandarddirs.h>
23 // local includes
24 #include "core/annotations.h"
25 #include "core/document.h"
27 #include <memory>
29 struct GuiUtilsHelper
31 GuiUtilsHelper()
32 : il( 0 )
36 QSvgRenderer* svgStamps();
38 KIconLoader * il;
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() )
52 svgStampFile.reset();
56 return svgStampFile.get();
59 K_GLOBAL_STATIC( GuiUtilsHelper, s_data )
61 namespace GuiUtils {
63 QString captionForAnnotation( const Okular::Annotation * ann )
65 Q_ASSERT( ann );
67 QString ret;
68 switch( ann->subType() )
70 case Okular::Annotation::AText:
71 if( ( (Okular::TextAnnotation*)ann )->textType() == Okular::TextAnnotation::Linked )
72 ret = i18n( "Note" );
73 else
74 ret = i18n( "Inline Note" );
75 break;
76 case Okular::Annotation::ALine:
77 ret = i18n( "Line" );
78 break;
79 case Okular::Annotation::AGeom:
80 ret = i18n( "Geometry" );
81 break;
82 case Okular::Annotation::AHighlight:
83 ret = i18n( "Highlight" );
84 break;
85 case Okular::Annotation::AStamp:
86 ret = i18n( "Stamp" );
87 break;
88 case Okular::Annotation::AInk:
89 ret = i18n( "Ink" );
90 break;
91 case Okular::Annotation::ACaret:
92 ret = i18n( "Caret" );
93 break;
94 case Okular::Annotation::AFileAttachment:
95 ret = i18n( "File Attachment" );
96 break;
97 case Okular::Annotation::ASound:
98 ret = i18n( "Sound" );
99 break;
100 case Okular::Annotation::AMovie:
101 ret = i18n( "Movie" );
102 break;
103 case Okular::Annotation::A_BASE:
104 break;
106 return ret;
109 QString authorForAnnotation( const Okular::Annotation * ann )
111 Q_ASSERT( ann );
113 return !ann->author().isEmpty() ? ann->author() : i18nc( "Unknown author", "Unknown" );
116 QString contents( const Okular::Annotation * ann )
118 Q_ASSERT( ann );
120 // 1. window text
121 QString ret = ann->window().text();
122 if ( !ret.isEmpty() )
123 return ret;
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() )
132 return ret;
136 // 3. contents
137 ret = ann->contents();
139 return ret;
142 QString contentsHtml( const Okular::Annotation * ann )
144 QString text = Qt::escape( contents( ann ) );
145 text.replace( "\n", "<br>" );
146 return text;
149 QString prettyToolTip( const Okular::Annotation * ann )
151 Q_ASSERT( 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;
160 tooltip += "</qt>";
162 return tooltip;
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 );
177 p.end();
178 return pixmap;
180 QPixmap pixmap;
181 const KIconLoader * il = iconLoader();
182 QString path;
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 );
187 return pixmap;
190 void setIconLoader( KIconLoader * loader )
192 s_data->il = 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() )
205 return;
207 QFile f( path );
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() );
213 f.close();
215 else
217 KMessageBox::error( parent, i18n( "Could not open \"%1\" for writing. File was not saved.", path ) );