2006-12-27 23:56:38 +00:00
/***************************************************************************
2007-12-24 00:54:21 +00:00
* Copyright ( C ) 2006 - 2007 by Pino Toscano < pino @ kde . org > *
2006-12-27 23:56:38 +00:00
* *
* 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 . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-12-24 00:54:21 +00:00
# include "guiutils.h"
2007-04-19 18:30:20 +00:00
2006-12-27 23:56:38 +00:00
// qt/kde includes
2008-09-19 22:43:41 +00:00
# include <qpainter.h>
# include <qsvgrenderer.h>
2008-04-03 09:52:05 +00:00
# include <qtextdocument.h>
2008-04-14 22:31:28 +00:00
# include <kfiledialog.h>
2007-12-24 15:02:32 +00:00
# include <kglobal.h>
# include <kiconloader.h>
2006-12-27 23:56:38 +00:00
# include <klocale.h>
2008-04-14 22:31:28 +00:00
# include <kmessagebox.h>
2008-09-19 22:43:41 +00:00
# include <kstandarddirs.h>
2006-12-27 23:56:38 +00:00
// local includes
# include "core/annotations.h"
2008-04-14 22:31:28 +00:00
# include "core/document.h"
2006-12-27 23:56:38 +00:00
2007-12-24 15:02:32 +00:00
struct GuiUtilsHelper
{
GuiUtilsHelper ( )
: il ( 0 )
{
}
KIconLoader * il ;
} ;
K_GLOBAL_STATIC ( GuiUtilsHelper , s_data )
2007-12-24 00:54:21 +00:00
namespace GuiUtils {
2007-09-08 14:44:21 +00:00
QString captionForAnnotation ( const Okular : : Annotation * ann )
2006-12-27 23:56:38 +00:00
{
2007-10-03 22:53:27 +00:00
Q_ASSERT ( ann ) ;
2006-12-27 23:56:38 +00:00
QString ret ;
switch ( ann - > subType ( ) )
{
case Okular : : Annotation : : AText :
if ( ( ( Okular : : TextAnnotation * ) ann ) - > textType ( ) = = Okular : : TextAnnotation : : Linked )
ret = i18n ( " Note " ) ;
else
2007-08-19 16:15:55 +00:00
ret = i18n ( " Inline Note " ) ;
2006-12-27 23:56:38 +00:00
break ;
case Okular : : Annotation : : ALine :
ret = i18n ( " Line " ) ;
break ;
case Okular : : Annotation : : AGeom :
2007-08-19 16:15:55 +00:00
ret = i18n ( " Geometry " ) ;
2006-12-27 23:56:38 +00:00
break ;
case Okular : : Annotation : : AHighlight :
ret = i18n ( " Highlight " ) ;
break ;
case Okular : : Annotation : : AStamp :
ret = i18n ( " Stamp " ) ;
break ;
case Okular : : Annotation : : AInk :
ret = i18n ( " Ink " ) ;
break ;
2008-04-13 16:42:14 +00:00
case Okular : : Annotation : : ACaret :
ret = i18n ( " Caret " ) ;
break ;
case Okular : : Annotation : : AFileAttachment :
ret = i18n ( " File Attachment " ) ;
break ;
2008-04-13 20:04:08 +00:00
case Okular : : Annotation : : ASound :
ret = i18n ( " Sound " ) ;
break ;
2008-08-23 00:09:59 +00:00
case Okular : : Annotation : : AMovie :
ret = i18n ( " Movie " ) ;
break ;
2006-12-27 23:56:38 +00:00
case Okular : : Annotation : : A_BASE :
break ;
}
return ret ;
}
2007-09-08 14:44:21 +00:00
QString authorForAnnotation ( const Okular : : Annotation * ann )
2007-09-08 14:41:40 +00:00
{
2007-10-03 22:53:27 +00:00
Q_ASSERT ( ann ) ;
2007-09-08 14:41:40 +00:00
return ! ann - > author ( ) . isEmpty ( ) ? ann - > author ( ) : i18nc ( " Unknown author " , " Unknown " ) ;
}
2007-09-08 14:44:21 +00:00
QString contents ( const Okular : : Annotation * ann )
2006-12-28 00:40:09 +00:00
{
2007-10-03 22:53:27 +00:00
Q_ASSERT ( ann ) ;
2006-12-28 00:40:09 +00:00
// 1. window text
QString ret = ann - > window ( ) . text ( ) ;
if ( ! ret . isEmpty ( ) )
return ret ;
// 2. if Text and InPlace, the inplace text
if ( ann - > subType ( ) = = Okular : : Annotation : : AText )
{
2007-06-04 20:49:48 +00:00
const Okular : : TextAnnotation * txtann = static_cast < const Okular : : TextAnnotation * > ( ann ) ;
2006-12-28 00:40:09 +00:00
if ( txtann - > textType ( ) = = Okular : : TextAnnotation : : InPlace )
{
ret = txtann - > inplaceText ( ) ;
if ( ! ret . isEmpty ( ) )
return ret ;
}
}
// 3. contents
ret = ann - > contents ( ) ;
return ret ;
}
2007-09-08 14:44:21 +00:00
QString contentsHtml ( const Okular : : Annotation * ann )
2006-12-28 00:40:09 +00:00
{
2008-04-03 09:52:05 +00:00
QString text = Qt : : escape ( contents ( ann ) ) ;
text . replace ( " \n " , " <br> " ) ;
return text ;
2006-12-28 00:40:09 +00:00
}
2007-09-08 14:44:21 +00:00
QString prettyToolTip ( const Okular : : Annotation * ann )
2007-09-08 14:41:40 +00:00
{
2007-10-03 22:53:27 +00:00
Q_ASSERT ( ann ) ;
2007-09-08 14:41:40 +00:00
QString author = authorForAnnotation ( ann ) ;
QString contents = contentsHtml ( ann ) ;
QString tooltip = QString ( " <qt><b> " ) + i18n ( " Author: %1 " , author ) + QString ( " </b> " ) ;
if ( ! contents . isEmpty ( ) )
2007-10-03 22:45:20 +00:00
tooltip + = QString ( " <div style= \" font-size: 4px; \" ><hr /></div> " ) + contents ;
2007-09-08 14:41:40 +00:00
tooltip + = " </qt> " ;
return tooltip ;
}
2008-09-19 22:43:41 +00:00
QPixmap loadStamp ( const QString & _name , const QSize & size , int iconSize )
2008-09-19 20:21:57 +00:00
{
const QString name = _name . toLower ( ) ;
2008-09-19 22:43:41 +00:00
if ( name . startsWith ( QLatin1String ( " stamp- " ) ) )
{
QSvgRenderer r ( KStandardDirs : : locate ( " data " , QString : : fromLatin1 ( " okular/pics/ " ) + name + " .svg " ) ) ;
if ( r . isValid ( ) )
{
QPixmap pixmap ( size . isValid ( ) ? size : r . defaultSize ( ) ) ;
pixmap . fill ( Qt : : transparent ) ;
QPainter p ( & pixmap ) ;
r . render ( & p ) ;
p . end ( ) ;
return pixmap ;
}
}
2008-09-19 20:21:57 +00:00
QPixmap pixmap ;
const KIconLoader * il = iconLoader ( ) ;
QString path ;
2008-09-19 22:43:41 +00:00
const int minSize = iconSize > 0 ? iconSize : qMin ( size . width ( ) , size . height ( ) ) ;
2008-09-19 20:21:57 +00:00
pixmap = il - > loadIcon ( name , KIconLoader : : User , minSize , KIconLoader : : DefaultState , QStringList ( ) , & path , true ) ;
if ( path . isEmpty ( ) )
pixmap = il - > loadIcon ( name , KIconLoader : : NoGroup , minSize ) ;
return pixmap ;
}
2007-12-24 15:02:32 +00:00
void setIconLoader ( KIconLoader * loader )
{
s_data - > il = loader ;
}
KIconLoader * iconLoader ( )
{
return s_data - > il ? s_data - > il : KIconLoader : : global ( ) ;
}
2008-04-14 22:31:28 +00:00
void saveEmbeddedFile ( Okular : : EmbeddedFile * ef , QWidget * parent )
{
const QString caption = i18n ( " Where do you want to save %1? " , ef - > name ( ) ) ;
const QString path = KFileDialog : : getSaveFileName ( ef - > name ( ) , QString ( ) , parent , caption ) ;
if ( path . isEmpty ( ) )
return ;
QFile f ( path ) ;
2008-08-01 20:12:09 +00:00
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 )
2008-04-14 22:31:28 +00:00
{
if ( f . open ( QIODevice : : WriteOnly ) )
{
f . write ( ef - > data ( ) ) ;
f . close ( ) ;
}
else
{
KMessageBox : : error ( parent , i18n ( " Could not open \" %1 \" for writing. File was not saved. " , path ) ) ;
}
}
}
2007-09-08 14:44:21 +00:00
}