1 /* drag_drop_toolbar.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0+*/
9 #include <ui/qt/widgets/drag_drop_toolbar.h>
10 #include <ui/qt/widgets/drag_label.h>
11 #include <ui/qt/utils/wireshark_mime_data.h>
14 #include <QApplication>
16 #include <QToolButton>
20 #include <QMouseEvent>
21 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
25 #define drag_drop_toolbar_action_ "drag_drop_toolbar_action_"
27 DragDropToolBar::DragDropToolBar(const QString &title, QWidget *parent) :
28 QToolBar(title, parent)
34 DragDropToolBar::DragDropToolBar(QWidget *parent) :
41 DragDropToolBar::~DragDropToolBar()
45 void DragDropToolBar::childEvent(QChildEvent * event)
47 /* New action has been added */
48 if ( event->type() == QEvent::ChildAdded )
50 if ( event->child()->isWidgetType() )
52 /* Reset if it has moved underneath lower limit */
53 if ( childCounter < 0 )
56 ((QWidget *)event->child())->installEventFilter(this);
57 event->child()->setProperty(drag_drop_toolbar_action_, QVariant::fromValue(childCounter));
61 else if ( event->type() == QEvent::ChildRemoved )
65 else if ( event->type() == QEvent::ChildPolished )
67 /* Polish is called every time a child is added or removed. This is implemented by adding
68 * all childs again as hidden elements, and afterwards removing the existing ones. Therefore
69 * we have to reset child counter here, if a widget is being polished. If this is not being
70 * done, crashes will occur after an item has been removed and other items are moved afterwards */
71 if ( event->child()->isWidgetType() )
76 void DragDropToolBar::clear()
82 bool DragDropToolBar::eventFilter(QObject * obj, QEvent * event)
84 if ( ! obj->isWidgetType() )
85 return QToolBar::eventFilter(obj, event);
87 QWidget * elem = qobject_cast<QWidget *>(obj);
89 if ( ! elem || ( event->type() != QEvent::MouseButtonPress && event->type() != QEvent::MouseMove ) )
90 return QToolBar::eventFilter(obj, event);
92 QMouseEvent * ev = (QMouseEvent *)event;
94 if ( event->type() == QEvent::MouseButtonPress )
96 if ( ev->buttons() & Qt::LeftButton )
97 dragStartPosition = ev->pos();
99 else if ( event->type() == QEvent::MouseMove )
101 if ( ( ev->buttons() & Qt::LeftButton ) && (ev->pos() - dragStartPosition).manhattanLength()
102 > QApplication::startDragDistance())
104 ToolbarEntryMimeData * temd =
105 new ToolbarEntryMimeData(((QToolButton *)elem)->text(), elem->property(drag_drop_toolbar_action_).toInt());
106 DragLabel * lbl = new DragLabel(temd->labelText(), this);
107 QDrag * drag = new QDrag(this);
108 drag->setMimeData(temd);
110 #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
111 qreal dpr = window()->windowHandle()->devicePixelRatio();
112 QPixmap pixmap(lbl->size() * dpr);
113 pixmap.setDevicePixelRatio(dpr);
115 QPixmap pixmap(lbl->size());
117 lbl->render(&pixmap);
118 drag->setPixmap(pixmap);
120 drag->exec(Qt::CopyAction | Qt::MoveAction);
126 return QToolBar::eventFilter(obj, event);
129 void DragDropToolBar::dragEnterEvent(QDragEnterEvent *event)
134 if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
136 if (event->source() == this) {
137 event->setDropAction(Qt::MoveAction);
140 event->acceptProposedAction();
142 } else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
143 if ( event->source() != this )
145 event->setDropAction(Qt::CopyAction);
148 event->acceptProposedAction();
155 void DragDropToolBar::dragMoveEvent(QDragMoveEvent *event)
160 if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
162 QAction * actionAtPos = actionAt(event->pos() );
165 QWidget * widget = widgetForAction(actionAtPos);
168 bool success = false;
169 widget->property(drag_drop_toolbar_action_).toInt(&success);
178 if (event->source() == this) {
179 event->setDropAction(Qt::MoveAction);
182 event->acceptProposedAction();
184 } else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
185 if ( event->source() != this )
187 event->setDropAction(Qt::CopyAction);
190 event->acceptProposedAction();
197 void DragDropToolBar::dropEvent(QDropEvent *event)
202 /* Moving items around */
203 if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
205 const ToolbarEntryMimeData * data = qobject_cast<const ToolbarEntryMimeData *>(event->mimeData());
207 int oldPos = data->position();
209 QAction * action = actionAt(event->pos());
210 if ( action && actions().at(oldPos) )
212 widgetForAction(action)->setStyleSheet("QWidget { border: none; };");
213 newPos = widgetForAction(action)->property(drag_drop_toolbar_action_).toInt();
214 moveToolbarItems(oldPos, newPos);
215 QAction * moveAction = actions().at(oldPos);
217 emit actionMoved(moveAction, oldPos, newPos);
220 if (event->source() == this) {
221 event->setDropAction(Qt::MoveAction);
224 event->acceptProposedAction();
227 } else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
228 const DisplayFilterMimeData * data = qobject_cast<const DisplayFilterMimeData *>(event->mimeData());
230 if ( event->source() != this )
232 event->setDropAction(Qt::CopyAction);
235 emit newFilterDropped(data->description(), data->filter());
238 event->acceptProposedAction();
246 void DragDropToolBar::moveToolbarItems(int fromPos, int newPos)
248 if ( fromPos == newPos )
251 setUpdatesEnabled(false);
253 QList<QAction *> storedActions = actions();
258 storedActions.move(fromPos, newPos);
259 foreach ( QAction * action, storedActions )
262 setUpdatesEnabled(true);
271 * indent-tabs-mode: nil
274 * ex: set shiftwidth=4 tabstop=8 expandtab:
275 * :indentSize=4:tabSize=8:noTabs=true: