• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • contact
textbrowser.cpp
1/*
2 Copyright (c) 2012 Montel Laurent <montel@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18
19*/
20
21#include "textbrowser_p.h"
22#include <kicontheme.h>
23#include <ktextbrowser.h>
24#include <klocalizedstring.h>
25#include <KUrl>
26#include <KAction>
27#include <KStandardAction>
28
29#include <kmime/kmime_util.h>
30
31#include <QApplication>
32#include <QContextMenuEvent>
33#include <QMenu>
34#include <QClipboard>
35#include <QTextBlock>
36
37using namespace Akonadi;
38
39TextBrowser::TextBrowser( QWidget *parent )
40 : KTextBrowser( parent )
41{
42}
43
44
45void TextBrowser::slotCopyData()
46{
47#ifndef QT_NO_CLIPBOARD
48 QClipboard *clip = QApplication::clipboard();
49 // put the data into the mouse selection and the clipboard
50 if ( mDataToCopy.type() == QVariant::Pixmap ) {
51 clip->setPixmap( mDataToCopy.value<QPixmap>(), QClipboard::Clipboard );
52 clip->setPixmap( mDataToCopy.value<QPixmap>(), QClipboard::Selection );
53 } else {
54 clip->setText( mDataToCopy.toString(), QClipboard::Clipboard );
55 clip->setText( mDataToCopy.toString(), QClipboard::Selection );
56 }
57#endif
58}
59
60
61#ifndef QT_NO_CONTEXTMENU
62void TextBrowser::contextMenuEvent( QContextMenuEvent *event )
63{
64#ifndef QT_NO_CLIPBOARD
65 QMenu popup;
66
67 KAction *act = KStandardAction::copy( this, SLOT(copy()), this );
68 act->setEnabled( !textCursor().selectedText().isEmpty() );
69 act->setShortcut( QKeySequence() );
70 popup.addAction( act );
71
72 // Create a new action to correspond with what is under the click
73 act = new KAction( i18nc( "@action:inmenu Copy the text of a general item", "Copy Item" ), this );
74
75 mDataToCopy.clear(); // nothing found to copy yet
76
77 QString link = anchorAt( event->pos() );
78 if ( !link.isEmpty() ) {
79 if ( link.startsWith( QLatin1String( "mailto:" ) ) ) {
80 mDataToCopy = KMime::decodeRFC2047String( KUrl( link ).path().toUtf8() );
81 // Action text matches that used in KMail
82 act->setText( i18nc( "@action:inmenu Copy a displayed email address", "Copy Email Address" ) );
83 } else {
84 // A link, but it could be one of our internal ones. There is
85 // no point in copying these. Internal links are always in the
86 // form "protocol:?argument", whereas valid external links should
87 // be in the form starting with "protocol://".
88 if ( !link.contains( QRegExp( QLatin1String( "^\\w+:\\?" ) ) ) ) {
89 mDataToCopy = link;
90 // Action text matches that used in Konqueror
91 act->setText( i18nc( "@action:inmenu Copy a link URL", "Copy Link URL" ) );
92 }
93 }
94 }
95
96 if ( !mDataToCopy.isValid() ) { // no link was found above
97 QTextCursor curs = cursorForPosition( event->pos() );
98 QString text = curs.block().text(); // try the text under cursor
99
100 if ( !text.isEmpty() ) {
101
102 // curs().block().text() over an image (contact photo or QR code)
103 // returns a string starting with the character 0xFFFC (Unicode
104 // object replacement character). See the documentation for
105 // QTextImageFormat.
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,
112 QUrl( imageName ) );
113
114 QPixmap pix = imageResource.value<QPixmap>();
115 if ( !pix.isNull() ) {
116
117 // There may be other images (e.g. contact type icons) that
118 // there is no point in copying.
119 if ( imageName == QLatin1String( "contact_photo" ) ) {
120 mDataToCopy = pix;
121 act->setText( i18nc( "@action:inmenu Copy a contact photo", "Copy Photo" ) );
122 } else if ( imageName == QLatin1String( "datamatrix" ) ||
123 imageName == QLatin1String( "qrcode" ) ) {
124 mDataToCopy = pix;
125 act->setText( i18nc( "@action:inmenu Copy a QR or Datamatrix image", "Copy Code" ) );
126 }
127 }
128 }
129 } else {
130
131 // Added by our formatter (but not I18N'ed) for a mobile
132 // telephone number. See
133 // kdepim/kaddressbook/grantlee/grantleecontactformatter.cpp and
134 // kdepimlibs/akonadi/contact/standardcontactformatter.cpp
135 text.remove( QRegExp( QLatin1String( "\\s*\\(SMS\\)$" ) ) );
136
137 // For an item which was formatted with line breaks (as <br>
138 // in HTML), the returned text contains the character 0x2028
139 // (Unicode line separator). Convert any of these back to newlines.
140 text.replace( QChar( 0x2028 ), QLatin1Char( '\n' ) );
141
142 mDataToCopy = text;
143 }
144 }
145 }
146
147 if ( mDataToCopy.isValid() ) {
148 connect( act, SIGNAL(triggered(bool)), SLOT(slotCopyData()) );
149 } else {
150 act->setEnabled( false );
151 }
152
153 popup.addAction( act );
154 popup.exec( event->globalPos() );
155#endif
156}
157#endif
Akonadi
FreeBusyManager::Singleton.
Definition: actionstatemanager_p.h:28
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal