Kstars

draglistbox.cpp
1 /*
2  SPDX-FileCopyrightText: 2005 Jason Harris <[email protected]>
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 
17 DragListBox::DragListBox(QWidget *parent, const char *name) : QListWidget(parent)
18 {
19  if (name)
20  {
21  setObjectName(name);
22  }
23  setAcceptDrops(true);
24 }
25 
26 bool DragListBox::contains(const QString &s) const
27 {
29  if (foundList.isEmpty())
30  return false;
31  else
32  return true;
33 }
34 
35 void DragListBox::dragEnterEvent(QDragEnterEvent *evt)
36 {
37  if (evt->mimeData()->hasText())
38  {
39  evt->acceptProposedAction();
40  }
41  else
42  {
43  evt->ignore();
44  }
45 }
46 
47 void DragListBox::dragMoveEvent(QDragMoveEvent *evt)
48 {
49  if (evt->mimeData()->hasText())
50  {
51  evt->acceptProposedAction();
52  }
53  else
54  {
55  evt->ignore();
56  }
57 }
58 
59 void 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  }
93  evt->acceptProposedAction();
94 }
95 
96 void DragListBox::mousePressEvent(QMouseEvent *evt)
97 {
99 
100  if (evt->button() == Qt::LeftButton)
101  {
102  leftButtonDown = true;
103  }
104 }
105 
106 void DragListBox::mouseReleaseEvent(QMouseEvent *evt)
107 {
109 
110  if (evt->button() == Qt::LeftButton)
111  {
112  leftButtonDown = false;
113  }
114 }
115 
116 void 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 }
virtual void mouseReleaseEvent(QMouseEvent *e) override
void insertItem(int row, QListWidgetItem *item)
QListWidgetItem * item(int row) const const
Qt::MouseButton button() const const
QPoint pos() const const
virtual QMimeData * mimeData(const QList< QListWidgetItem * > items) const const
void acceptProposedAction()
bool hasText() const const
int y() const const
const QMimeData * mimeData() const const
QListWidgetItem * takeItem(int row)
LeftButton
virtual void mousePressEvent(QMouseEvent *event) override
QRect visualItemRect(const QListWidgetItem *item) const const
Qt::DropAction exec(Qt::DropActions supportedActions)
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
QListWidgetItem * itemAt(const QPoint &p) const const
QString text() const const
void setAcceptDrops(bool on)
QListWidgetItem * currentItem() const const
QObject * source() const const
void setText(const QString &text)
void setObjectName(const QString &name)
void addItem(const QString &label)
int row(const QListWidgetItem *item) const const
void setMimeData(QMimeData *data)
MatchExactly
Extension of KListWidget that allows Drag-and-Drop with other DragListBoxes.
Definition: draglistbox.h:22
void accept()
QList< QListWidgetItem * > findItems(const QString &text, Qt::MatchFlags flags) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.