Kstars

draglistbox.cpp
1/*
2 SPDX-FileCopyrightText: 2005 Jason Harris <kstars@30doradus.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "draglistbox.h"
8
9#include <KLocalizedString>
10
11#include <QDrag>
12#include <QDragEnterEvent>
13#include <QDropEvent>
14#include <QMimeData>
15#include <QMouseEvent>
16
17DragListBox::DragListBox(QWidget *parent, const char *name) : QListWidget(parent)
18{
19 if (name)
20 {
21 setObjectName(name);
22 }
23 setAcceptDrops(true);
24}
25
26bool DragListBox::contains(const QString &s) const
27{
29 if (foundList.isEmpty())
30 return false;
31 else
32 return true;
33}
34
35void DragListBox::dragEnterEvent(QDragEnterEvent *evt)
36{
37 if (evt->mimeData()->hasText())
38 {
40 }
41 else
42 {
43 evt->ignore();
44 }
45}
46
47void DragListBox::dragMoveEvent(QDragMoveEvent *evt)
48{
49 if (evt->mimeData()->hasText())
50 {
52 }
53 else
54 {
55 evt->ignore();
56 }
57}
58
59void DragListBox::dropEvent(QDropEvent *evt)
60{
61 QString text;
62
63 if (evt->mimeData()->hasText())
64 {
65 text = evt->mimeData()->text();
66
67 //Copy an item dragged from FieldPool to FieldList
68 //If we dragged an "Ignore" item from the FieldPool to the FieldList, then we don't
69 //need to insert the item, because FieldPool already has a persistent Ignore item.
70 if (!(text == i18n("Ignore") && QString(evt->source()->objectName()) == "FieldList" && evt->source() != this))
71 {
72 QListWidgetItem *lwi = itemAt(evt->pos());
73 if (lwi == nullptr && evt->pos().y() > visualItemRect(item(count() - 1)).bottom())
74 {
75 addItem(text);
76 }
77 else
78 {
79 int i = row(itemAt(evt->pos()));
80 insertItem(i, text);
81 }
82 }
83
84 //Remove an item dragged from FieldList to FieldPool.
85 //If we dragged the "Ignore" item from FieldList to FieldPool, then we don't
86 //want to remove the item from the FieldPool
87 if (!(text == i18n("Ignore") && QString(evt->source()->objectName()) == "FieldPool" && evt->source() != this))
88 {
89 DragListBox *fp = (DragListBox *)evt->source();
90 delete fp->takeItem(fp->currentRow());
91 }
92 }
94}
95
96void DragListBox::mousePressEvent(QMouseEvent *evt)
97{
99
100 if (evt->button() == Qt::LeftButton)
101 {
102 leftButtonDown = true;
103 }
104}
105
106void DragListBox::mouseReleaseEvent(QMouseEvent *evt)
107{
109
110 if (evt->button() == Qt::LeftButton)
111 {
112 leftButtonDown = false;
113 }
114}
115
116void DragListBox::mouseMoveEvent(QMouseEvent *evt)
117{
118 if (leftButtonDown)
119 {
120 leftButtonDown = false; //Don't create multiple QDrag objects!
121
122 QDrag *drag = new QDrag(this);
124 mimeData->setText(currentItem()->text());
125 drag->setMimeData(mimeData);
126
127 drag->exec();
128 evt->accept();
129 }
130}
Extension of KListWidget that allows Drag-and-Drop with other DragListBoxes.
Definition draglistbox.h:23
QString i18n(const char *text, const TYPE &arg...)
virtual void mousePressEvent(QMouseEvent *event) override
virtual void mouseReleaseEvent(QMouseEvent *event) override
Qt::DropAction exec(Qt::DropActions supportedActions)
void setMimeData(QMimeData *data)
QPoint pos() const const
void acceptProposedAction()
const QMimeData * mimeData() const const
QObject * source() const const
void accept()
bool isEmpty() const const
void addItem(QListWidgetItem *item)
QListWidgetItem * currentItem() const const
QList< QListWidgetItem * > findItems(const QString &text, Qt::MatchFlags flags) const const
void insertItem(int row, QListWidgetItem *item)
QListWidgetItem * item(int row) const const
QListWidgetItem * itemAt(const QPoint &p) const const
virtual QMimeData * mimeData(const QList< QListWidgetItem * > &items) const const
int row(const QListWidgetItem *item) const const
QListWidgetItem * takeItem(int row)
QRect visualItemRect(const QListWidgetItem *item) const const
bool hasText() const const
void setText(const QString &text)
QString text() const const
int y() const const
Qt::MouseButton button() const const
MatchExactly
LeftButton
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.