compile
[kdegraphics.git] / okular / ui / guiutils.cpp
blob1d0eefb89044582c4bc85912219e62d468dc2c9b
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 struct GuiUtilsHelper
29 GuiUtilsHelper()
30 : il( 0 )
34 KIconLoader * il;
37 K_GLOBAL_STATIC( GuiUtilsHelper, s_data )
39 namespace GuiUtils {
41 QString captionForAnnotation( const Okular::Annotation * ann )
43 Q_ASSERT( ann );
45 QString ret;
46 switch( ann->subType() )
48 case Okular::Annotation::AText:
49 if( ( (Okular::TextAnnotation*)ann )->textType() == Okular::TextAnnotation::Linked )
50 ret = i18n( "Note" );
51 else
52 ret = i18n( "Inline Note" );
53 break;
54 case Okular::Annotation::ALine:
55 ret = i18n( "Line" );
56 break;
57 case Okular::Annotation::AGeom:
58 ret = i18n( "Geometry" );
59 break;
60 case Okular::Annotation::AHighlight:
61 ret = i18n( "Highlight" );
62 break;
63 case Okular::Annotation::AStamp:
64 ret = i18n( "Stamp" );
65 break;
66 case Okular::Annotation::AInk:
67 ret = i18n( "Ink" );
68 break;
69 case Okular::Annotation::ACaret:
70 ret = i18n( "Caret" );
71 break;
72 case Okular::Annotation::AFileAttachment:
73 ret = i18n( "File Attachment" );
74 break;
75 case Okular::Annotation::ASound:
76 ret = i18n( "Sound" );
77 break;
78 case Okular::Annotation::AMovie:
79 ret = i18n( "Movie" );
80 break;
81 case Okular::Annotation::A_BASE:
82 break;
84 return ret;
87 QString authorForAnnotation( const Okular::Annotation * ann )
89 Q_ASSERT( ann );
91 return !ann->author().isEmpty() ? ann->author() : i18nc( "Unknown author", "Unknown" );
94 QString contents( const Okular::Annotation * ann )
96 Q_ASSERT( ann );
98 // 1. window text
99 QString ret = ann->window().text();
100 if ( !ret.isEmpty() )
101 return ret;
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() )
110 return ret;
114 // 3. contents
115 ret = ann->contents();
117 return ret;
120 QString contentsHtml( const Okular::Annotation * ann )
122 QString text = Qt::escape( contents( ann ) );
123 text.replace( "\n", "<br>" );
124 return text;
127 QString prettyToolTip( const Okular::Annotation * ann )
129 Q_ASSERT( 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;
138 tooltip += "</qt>";
140 return tooltip;
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" ) );
149 if ( r.isValid() )
151 QPixmap pixmap( size.isValid() ? size : r.defaultSize() );
152 pixmap.fill( Qt::transparent );
153 QPainter p( &pixmap );
154 r.render( &p );
155 p.end();
156 return pixmap;
159 QPixmap pixmap;
160 const KIconLoader * il = iconLoader();
161 QString path;
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 );
166 return pixmap;
169 void setIconLoader( KIconLoader * loader )
171 s_data->il = 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() )
184 return;
186 QFile f( path );
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() );
192 f.close();
194 else
196 KMessageBox::error( parent, i18n( "Could not open \"%1\" for writing. File was not saved.", path ) );