21 #include "textbrowser_p.h"
22 #include <kicontheme.h>
23 #include <ktextbrowser.h>
24 #include <klocalizedstring.h>
27 #include <KStandardAction>
29 #include <kmime/kmime_util.h>
31 #include <QApplication>
32 #include <QContextMenuEvent>
37 using namespace Akonadi;
39 TextBrowser::TextBrowser( QWidget *parent )
40 : KTextBrowser( parent )
45 void TextBrowser::slotCopyData()
47 #ifndef QT_NO_CLIPBOARD
48 QClipboard *clip = QApplication::clipboard();
50 if ( mDataToCopy.type() == QVariant::Pixmap ) {
51 clip->setPixmap( mDataToCopy.value<QPixmap>(), QClipboard::Clipboard );
52 clip->setPixmap( mDataToCopy.value<QPixmap>(), QClipboard::Selection );
54 clip->setText( mDataToCopy.toString(), QClipboard::Clipboard );
55 clip->setText( mDataToCopy.toString(), QClipboard::Selection );
61 #ifndef QT_NO_CONTEXTMENU
62 void TextBrowser::contextMenuEvent( QContextMenuEvent *event )
64 #ifndef QT_NO_CLIPBOARD
67 KAction *act = KStandardAction::copy(
this, SLOT(copy()),
this );
68 act->setEnabled( !textCursor().selectedText().isEmpty() );
69 act->setShortcut( QKeySequence() );
70 popup.addAction( act );
73 act =
new KAction( i18nc(
"@action:inmenu Copy the text of a general item",
"Copy Item" ),
this );
77 QString link = anchorAt( event->pos() );
78 if ( !link.isEmpty() ) {
79 if ( link.startsWith( QLatin1String(
"mailto:" ) ) ) {
80 mDataToCopy = KMime::decodeRFC2047String( KUrl( link ).path().toUtf8() );
82 act->setText( i18nc(
"@action:inmenu Copy a displayed email address",
"Copy Email Address" ) );
88 if ( !link.contains( QRegExp( QLatin1String(
"^\\w+:\\?" ) ) ) ) {
91 act->setText( i18nc(
"@action:inmenu Copy a link URL",
"Copy Link URL" ) );
96 if ( !mDataToCopy.isValid() ) {
97 QTextCursor curs = cursorForPosition( event->pos() );
98 QString text = curs.block().text();
100 if ( !text.isEmpty() ) {
106 if ( text.startsWith( QChar( 0xFFFC ) ) ) {
107 QTextCharFormat charFormat = curs.charFormat();
108 if ( charFormat.isImageFormat() ) {
109 QTextImageFormat imageFormat = charFormat.toImageFormat();
110 QString imageName = imageFormat.name();
111 QVariant imageResource = document()->resource( QTextDocument::ImageResource,
114 QPixmap pix = imageResource.value<QPixmap>();
115 if ( !pix.isNull() ) {
119 if ( imageName == QLatin1String(
"contact_photo" ) ) {
121 act->setText( i18nc(
"@action:inmenu Copy a contact photo",
"Copy Photo" ) );
122 }
else if ( imageName == QLatin1String(
"datamatrix" ) ||
123 imageName == QLatin1String(
"qrcode" ) ) {
125 act->setText( i18nc(
"@action:inmenu Copy a QR or Datamatrix image",
"Copy Code" ) );
135 text.remove( QRegExp( QLatin1String(
"\\s*\\(SMS\\)$" ) ) );
140 text.replace( QChar( 0x2028 ), QLatin1Char(
'\n' ) );
147 if ( mDataToCopy.isValid() ) {
148 connect( act, SIGNAL(triggered(
bool)), SLOT(slotCopyData()) );
150 act->setEnabled(
false );
153 popup.addAction( act );
154 popup.exec( event->globalPos() );