KDEUI
kpastetextaction.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org> 00003 (C) 1999 Simon Hausmann <hausmann@kde.org> 00004 (C) 2000 Nicolas Hadacek <haadcek@kde.org> 00005 (C) 2000 Kurt Granroth <granroth@kde.org> 00006 (C) 2000 Michael Koch <koch@kde.org> 00007 (C) 2001 Holger Freyther <freyther@kde.org> 00008 (C) 2002 Ellis Whitehead <ellis@kde.org> 00009 (C) 2002 Joseph Wenninger <jowenn@kde.org> 00010 (C) 2003 Andras Mantia <amantia@kde.org> 00011 (C) 2005-2006 Hamish Rodda <rodda@kde.org> 00012 00013 This library is free software; you can redistribute it and/or 00014 modify it under the terms of the GNU Library General Public 00015 License version 2 as published by the Free Software Foundation. 00016 00017 This library is distributed in the hope that it will be useful, 00018 but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 Library General Public License for more details. 00021 00022 You should have received a copy of the GNU Library General Public License 00023 along with this library; see the file COPYING.LIB. If not, write to 00024 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00025 Boston, MA 02110-1301, USA. 00026 */ 00027 00028 #include "kpastetextaction.h" 00029 00030 #include <QtGui/QClipboard> 00031 #include <QtDBus/QtDBus> 00032 00033 #include <kapplication.h> 00034 #include <kdebug.h> 00035 #include <kicon.h> 00036 #include <klocale.h> 00037 00038 #include "kmenu.h" 00039 00040 class KPasteTextActionPrivate 00041 { 00042 public: 00043 KPasteTextActionPrivate(KPasteTextAction *parent) 00044 : q(parent) 00045 { 00046 } 00047 00048 ~KPasteTextActionPrivate() 00049 { 00050 delete m_popup; 00051 } 00052 00053 void _k_menuAboutToShow(); 00054 void _k_slotTriggered(QAction*); 00055 00056 void init(); 00057 00058 KPasteTextAction *q; 00059 KMenu *m_popup; 00060 bool m_mixedMode; 00061 }; 00062 00063 KPasteTextAction::KPasteTextAction(QObject *parent) 00064 : KAction(parent), d(new KPasteTextActionPrivate(this)) 00065 { 00066 d->init(); 00067 } 00068 00069 KPasteTextAction::KPasteTextAction(const QString &text, QObject *parent) 00070 : KAction(parent), d(new KPasteTextActionPrivate(this)) 00071 { 00072 d->init(); 00073 setText(text); 00074 } 00075 00076 KPasteTextAction::KPasteTextAction(const KIcon &icon, const QString &text, QObject *parent) 00077 : KAction(icon, text, parent), d(new KPasteTextActionPrivate(this)) 00078 { 00079 d->init(); 00080 } 00081 00082 void KPasteTextActionPrivate::init() 00083 { 00084 m_popup = new KMenu; 00085 q->connect(m_popup, SIGNAL(aboutToShow()), q, SLOT(_k_menuAboutToShow())); 00086 q->connect(m_popup, SIGNAL(triggered(QAction*)), q, SLOT(_k_slotTriggered(QAction*))); 00087 m_mixedMode = true; 00088 } 00089 00090 KPasteTextAction::~KPasteTextAction() 00091 { 00092 delete d; 00093 } 00094 00095 void KPasteTextAction::setMixedMode(bool mode) 00096 { 00097 d->m_mixedMode = mode; 00098 } 00099 00100 void KPasteTextActionPrivate::_k_menuAboutToShow() 00101 { 00102 m_popup->clear(); 00103 QStringList list; 00104 QDBusInterface klipper("org.kde.klipper", "/klipper", "org.kde.klipper.klipper"); 00105 if (klipper.isValid()) { 00106 QDBusReply<QStringList> reply = klipper.call("getClipboardHistoryMenu"); 00107 if (reply.isValid()) 00108 list = reply; 00109 } 00110 QString clipboardText = qApp->clipboard()->text(QClipboard::Clipboard); 00111 if (list.isEmpty()) 00112 list << clipboardText; 00113 bool found = false; 00114 const QFontMetrics fm = m_popup->fontMetrics(); 00115 foreach (const QString& string, list) 00116 { 00117 QString text = fm.elidedText(string.simplified(), Qt::ElideMiddle, fm.maxWidth() * 20); 00118 text.replace('&', "&&"); 00119 QAction* action = m_popup->addAction(text); 00120 if (!found && string == clipboardText) 00121 { 00122 action->setChecked(true); 00123 found = true; 00124 } 00125 } 00126 } 00127 00128 void KPasteTextActionPrivate::_k_slotTriggered(QAction* action) 00129 { 00130 QDBusInterface klipper("org.kde.klipper", "/klipper", "org.kde.klipper.klipper"); 00131 if (klipper.isValid()) { 00132 QDBusReply<QString> reply = klipper.call("getClipboardHistoryItem", 00133 m_popup->actions().indexOf(action)); 00134 if (!reply.isValid()) 00135 return; 00136 QString clipboardText = reply; 00137 reply = klipper.call("setClipboardContents", clipboardText); 00138 if (reply.isValid()) 00139 kDebug(129) << "Clipboard: " << qApp->clipboard()->text(QClipboard::Clipboard); 00140 } 00141 } 00142 00143 /* vim: et sw=2 ts=2 00144 */ 00145 00146 #include "kpastetextaction.moc"
KDE 4.6 API Reference