File indexing completed on 2024-04-28 05:51:09

0001 /*
0002  *  SPDX-FileCopyrightText: 2002-2003 Jesper K. Pedersen <blackie@kde.org>
0003  *  SPDX-FileCopyrightText: 2011 Morten A. B. Sjøgren <m_abs@mabs.dk>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-only
0006  **/
0007 
0008 //---------------------
0009 // ccp = Cut-Copy-Paste
0010 //---------------------
0011 
0012 #include "ccp.h"
0013 
0014 #include <QEvent>
0015 #include <QMenu>
0016 #include <QMouseEvent>
0017 
0018 #include "kmultiformlistbox-multivisible.h"
0019 
0020 CCP::CCP(KMultiFormListBoxMultiVisible *ee_, KMultiFormListBoxEntry *eee_)
0021     : QObject()
0022 {
0023     ee = ee_;
0024     eee = eee_;
0025     install(eee);
0026 }
0027 
0028 void CCP::install(QObject *elm)
0029 {
0030     elm->installEventFilter(this);
0031     const QList<QObject *> children = elm->children();
0032     if (!children.isEmpty()) {
0033         for (int i = 0; i < children.size(); ++i) {
0034             if (children.at(i)->inherits("KMultiFormListBoxMultiVisible")) {
0035                 // Stop if the widget is an KMultiFormListBox, as this widget has its own cut/copy/paste
0036             } else {
0037                 install(children.at(i));
0038             }
0039         }
0040     }
0041 }
0042 
0043 // This function post the Cut/Copy/Paste menu
0044 bool CCP::eventFilter(QObject *, QEvent *event)
0045 {
0046     if (event->type() != QEvent::MouseButtonPress) {
0047         return false;
0048     }
0049 
0050     auto mouseEvent = static_cast<QMouseEvent *>(event);
0051     if (mouseEvent->button() != Qt::RightButton || (mouseEvent->modifiers() & Qt::ControlModifier) == 0) {
0052         return false;
0053     }
0054 
0055     QPoint pos = mouseEvent->globalPos();
0056 
0057     QMenu menu;
0058     QAction *cutAction = menu.addAction(i18n("Cut"));
0059     QAction *copyAction = menu.addAction(i18n("Copy"));
0060     QAction *pasteAction = menu.addAction(i18n("Paste"));
0061     QAction *blankAction = menu.addAction(i18n("Insert Blank"));
0062 
0063     QAction *res = menu.exec(pos);
0064     if (res) {
0065         if (res == cutAction) {
0066             ee->cut(eee);
0067         } else if (res == copyAction) {
0068             ee->copy(eee);
0069         } else if (res == pasteAction) {
0070             ee->paste(eee);
0071         } else if (res == blankAction) {
0072             ee->addElement(eee);
0073         }
0074     }
0075     return true;
0076 }
0077 
0078 #include "moc_ccp.cpp"