KDEUI
kpastetextaction.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00144
00145
00146 #include "kpastetextaction.moc"