File indexing completed on 2024-03-24 03:47:43
0001 /* 0002 SPDX-FileCopyrightText: 2005 Jason Harris <kstars@30doradus.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "draglistbox.h" 0008 0009 #include <KLocalizedString> 0010 0011 #include <QDrag> 0012 #include <QDragEnterEvent> 0013 #include <QDropEvent> 0014 #include <QMimeData> 0015 #include <QMouseEvent> 0016 0017 DragListBox::DragListBox(QWidget *parent, const char *name) : QListWidget(parent) 0018 { 0019 if (name) 0020 { 0021 setObjectName(name); 0022 } 0023 setAcceptDrops(true); 0024 } 0025 0026 bool DragListBox::contains(const QString &s) const 0027 { 0028 QList<QListWidgetItem *> foundList = findItems(s, Qt::MatchExactly); 0029 if (foundList.isEmpty()) 0030 return false; 0031 else 0032 return true; 0033 } 0034 0035 void DragListBox::dragEnterEvent(QDragEnterEvent *evt) 0036 { 0037 if (evt->mimeData()->hasText()) 0038 { 0039 evt->acceptProposedAction(); 0040 } 0041 else 0042 { 0043 evt->ignore(); 0044 } 0045 } 0046 0047 void DragListBox::dragMoveEvent(QDragMoveEvent *evt) 0048 { 0049 if (evt->mimeData()->hasText()) 0050 { 0051 evt->acceptProposedAction(); 0052 } 0053 else 0054 { 0055 evt->ignore(); 0056 } 0057 } 0058 0059 void DragListBox::dropEvent(QDropEvent *evt) 0060 { 0061 QString text; 0062 0063 if (evt->mimeData()->hasText()) 0064 { 0065 text = evt->mimeData()->text(); 0066 0067 //Copy an item dragged from FieldPool to FieldList 0068 //If we dragged an "Ignore" item from the FieldPool to the FieldList, then we don't 0069 //need to insert the item, because FieldPool already has a persistent Ignore item. 0070 if (!(text == i18n("Ignore") && QString(evt->source()->objectName()) == "FieldList" && evt->source() != this)) 0071 { 0072 QListWidgetItem *lwi = itemAt(evt->pos()); 0073 if (lwi == nullptr && evt->pos().y() > visualItemRect(item(count() - 1)).bottom()) 0074 { 0075 addItem(text); 0076 } 0077 else 0078 { 0079 int i = row(itemAt(evt->pos())); 0080 insertItem(i, text); 0081 } 0082 } 0083 0084 //Remove an item dragged from FieldList to FieldPool. 0085 //If we dragged the "Ignore" item from FieldList to FieldPool, then we don't 0086 //want to remove the item from the FieldPool 0087 if (!(text == i18n("Ignore") && QString(evt->source()->objectName()) == "FieldPool" && evt->source() != this)) 0088 { 0089 DragListBox *fp = (DragListBox *)evt->source(); 0090 delete fp->takeItem(fp->currentRow()); 0091 } 0092 } 0093 evt->acceptProposedAction(); 0094 } 0095 0096 void DragListBox::mousePressEvent(QMouseEvent *evt) 0097 { 0098 QListWidget::mousePressEvent(evt); 0099 0100 if (evt->button() == Qt::LeftButton) 0101 { 0102 leftButtonDown = true; 0103 } 0104 } 0105 0106 void DragListBox::mouseReleaseEvent(QMouseEvent *evt) 0107 { 0108 QListWidget::mouseReleaseEvent(evt); 0109 0110 if (evt->button() == Qt::LeftButton) 0111 { 0112 leftButtonDown = false; 0113 } 0114 } 0115 0116 void DragListBox::mouseMoveEvent(QMouseEvent *evt) 0117 { 0118 if (leftButtonDown) 0119 { 0120 leftButtonDown = false; //Don't create multiple QDrag objects! 0121 0122 QDrag *drag = new QDrag(this); 0123 QMimeData *mimeData = new QMimeData; 0124 mimeData->setText(currentItem()->text()); 0125 drag->setMimeData(mimeData); 0126 0127 drag->exec(); 0128 evt->accept(); 0129 } 0130 }