• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • actions
kpastetextaction.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
3  (C) 1999 Simon Hausmann <hausmann@kde.org>
4  (C) 2000 Nicolas Hadacek <haadcek@kde.org>
5  (C) 2000 Kurt Granroth <granroth@kde.org>
6  (C) 2000 Michael Koch <koch@kde.org>
7  (C) 2001 Holger Freyther <freyther@kde.org>
8  (C) 2002 Ellis Whitehead <ellis@kde.org>
9  (C) 2002 Joseph Wenninger <jowenn@kde.org>
10  (C) 2003 Andras Mantia <amantia@kde.org>
11  (C) 2005-2006 Hamish Rodda <rodda@kde.org>
12 
13  This library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU Library General Public
15  License version 2 as published by the Free Software Foundation.
16 
17  This library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  Library General Public License for more details.
21 
22  You should have received a copy of the GNU Library General Public License
23  along with this library; see the file COPYING.LIB. If not, write to
24  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  Boston, MA 02110-1301, USA.
26 */
27 
28 #include "kpastetextaction.h"
29 
30 #include <QtGui/QClipboard>
31 #include <QtDBus/QtDBus>
32 
33 #include <kapplication.h>
34 #include <kdebug.h>
35 #include <kicon.h>
36 #include <klocale.h>
37 
38 #include "kmenu.h"
39 
40 class KPasteTextActionPrivate
41 {
42 public:
43  KPasteTextActionPrivate(KPasteTextAction *parent)
44  : q(parent)
45  {
46  }
47 
48  ~KPasteTextActionPrivate()
49  {
50  delete m_popup;
51  }
52 
53  void _k_menuAboutToShow();
54  void _k_slotTriggered(QAction*);
55 
56  void init();
57 
58  KPasteTextAction *q;
59  KMenu *m_popup;
60  bool m_mixedMode;
61 };
62 
63 KPasteTextAction::KPasteTextAction(QObject *parent)
64  : KAction(parent), d(new KPasteTextActionPrivate(this))
65 {
66  d->init();
67 }
68 
69 KPasteTextAction::KPasteTextAction(const QString &text, QObject *parent)
70  : KAction(parent), d(new KPasteTextActionPrivate(this))
71 {
72  d->init();
73  setText(text);
74 }
75 
76 KPasteTextAction::KPasteTextAction(const KIcon &icon, const QString &text, QObject *parent)
77  : KAction(icon, text, parent), d(new KPasteTextActionPrivate(this))
78 {
79  d->init();
80 }
81 
82 void KPasteTextActionPrivate::init()
83 {
84  m_popup = new KMenu;
85  q->connect(m_popup, SIGNAL(aboutToShow()), q, SLOT(_k_menuAboutToShow()));
86  q->connect(m_popup, SIGNAL(triggered(QAction*)), q, SLOT(_k_slotTriggered(QAction*)));
87  m_mixedMode = true;
88 }
89 
90 KPasteTextAction::~KPasteTextAction()
91 {
92  delete d;
93 }
94 
95 void KPasteTextAction::setMixedMode(bool mode)
96 {
97  d->m_mixedMode = mode;
98 }
99 
100 void KPasteTextActionPrivate::_k_menuAboutToShow()
101 {
102  m_popup->clear();
103  QStringList list;
104  QDBusInterface klipper("org.kde.klipper", "/klipper", "org.kde.klipper.klipper");
105  if (klipper.isValid()) {
106  QDBusReply<QStringList> reply = klipper.call("getClipboardHistoryMenu");
107  if (reply.isValid())
108  list = reply;
109  }
110  QString clipboardText = qApp->clipboard()->text(QClipboard::Clipboard);
111  if (list.isEmpty())
112  list << clipboardText;
113  bool found = false;
114  const QFontMetrics fm = m_popup->fontMetrics();
115  foreach (const QString& string, list)
116  {
117  QString text = fm.elidedText(string.simplified(), Qt::ElideMiddle, fm.maxWidth() * 20);
118  text.replace('&', "&&");
119  QAction* action = m_popup->addAction(text);
120  if (!found && string == clipboardText)
121  {
122  action->setChecked(true);
123  found = true;
124  }
125  }
126 }
127 
128 void KPasteTextActionPrivate::_k_slotTriggered(QAction* action)
129 {
130  QDBusInterface klipper("org.kde.klipper", "/klipper", "org.kde.klipper.klipper");
131  if (klipper.isValid()) {
132  QDBusReply<QString> reply = klipper.call("getClipboardHistoryItem",
133  m_popup->actions().indexOf(action));
134  if (!reply.isValid())
135  return;
136  QString clipboardText = reply;
137  reply = klipper.call("setClipboardContents", clipboardText);
138  if (reply.isValid())
139  kDebug(129) << "Clipboard: " << qApp->clipboard()->text(QClipboard::Clipboard);
140  }
141 }
142 
143 /* vim: et sw=2 ts=2
144  */
145 
146 #include "kpastetextaction.moc"
QAction::setText
void setText(const QString &text)
kdebug.h
kapplication.h
QDBusReply
QAction::setChecked
void setChecked(bool)
KMenu
A menu with keyboard searching.
Definition: kmenu.h:42
QDBusReply::isValid
bool isValid() const
QFontMetrics
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
KPasteTextAction::setMixedMode
void setMixedMode(bool mode)
Controls the behavior of the clipboard history menu popup.
Definition: kpastetextaction.cpp:95
kmenu.h
QObject
QFontMetrics::maxWidth
int maxWidth() const
QList::isEmpty
bool isEmpty() const
QFontMetrics::elidedText
QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags) const
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
KPasteTextAction::~KPasteTextAction
virtual ~KPasteTextAction()
Definition: kpastetextaction.cpp:90
QString
QStringList
QDBusInterface
QString::replace
QString & replace(int position, int n, QChar after)
KPasteTextAction
An action for pasting text from the clipboard.
Definition: kpastetextaction.h:42
QAction
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:216
KPasteTextAction::KPasteTextAction
KPasteTextAction(QObject *parent)
Constructs an action with the specified parent.
Definition: kpastetextaction.cpp:63
kicon.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
kpastetextaction.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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