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-or-later
10 #include <wsutil/utf8_entities.h>
12 #include <ui/qt/widgets/drag_drop_toolbar.h>
13 #include <ui/qt/widgets/drag_label.h>
14 #include <ui/qt/utils/wireshark_mime_data.h>
17 #include <QApplication>
19 #include <QToolButton>
23 #include <QMouseEvent>
26 #define drag_drop_toolbar_action_ "drag_drop_toolbar_action_"
28 DragDropToolBar::DragDropToolBar(const QString &title, QWidget *parent) :
29 QToolBar(title, parent)
34 DragDropToolBar::DragDropToolBar(QWidget *parent) :
40 void DragDropToolBar::setupToolbar()
45 // Each QToolBar has a QToolBarExtension button. Its icon looks
46 // terrible. We might want to create our own icon, but the double
47 // angle quote is a similar, nice-looking shape.
48 QToolButton *ext_button = findChild<QToolButton*>();
50 ext_button->setIcon(QIcon());
51 ext_button->setText(UTF8_RIGHT_POINTING_DOUBLE_ANGLE_QUOTATION_MARK);
55 DragDropToolBar::~DragDropToolBar()
59 void DragDropToolBar::childEvent(QChildEvent * event)
61 /* New action has been added */
62 if ( event->type() == QEvent::ChildAdded )
64 if ( event->child()->isWidgetType() )
66 /* Reset if it has moved underneath lower limit */
67 if ( childCounter < 0 )
70 ((QWidget *)event->child())->installEventFilter(this);
71 event->child()->setProperty(drag_drop_toolbar_action_, QVariant::fromValue(childCounter));
75 else if ( event->type() == QEvent::ChildRemoved )
79 else if ( event->type() == QEvent::ChildPolished )
81 /* Polish is called every time a child is added or removed. This is implemented by adding
82 * all childs again as hidden elements, and afterwards removing the existing ones. Therefore
83 * we have to reset child counter here, if a widget is being polished. If this is not being
84 * done, crashes will occur after an item has been removed and other items are moved afterwards */
85 if ( event->child()->isWidgetType() )
90 void DragDropToolBar::clear()
96 bool DragDropToolBar::eventFilter(QObject * obj, QEvent * event)
98 if ( ! obj->isWidgetType() )
99 return QToolBar::eventFilter(obj, event);
101 QWidget * elem = qobject_cast<QWidget *>(obj);
103 if ( ! elem || ( event->type() != QEvent::MouseButtonPress && event->type() != QEvent::MouseMove ) )
104 return QToolBar::eventFilter(obj, event);
106 QMouseEvent * ev = (QMouseEvent *)event;
108 if ( event->type() == QEvent::MouseButtonPress )
110 if ( ev->buttons() & Qt::LeftButton )
111 dragStartPosition = ev->pos();
113 else if ( event->type() == QEvent::MouseMove )
115 if ( ( ev->buttons() & Qt::LeftButton ) && (ev->pos() - dragStartPosition).manhattanLength()
116 > QApplication::startDragDistance())
118 ToolbarEntryMimeData * temd =
119 new ToolbarEntryMimeData(((QToolButton *)elem)->text(), elem->property(drag_drop_toolbar_action_).toInt());
120 DragLabel * lbl = new DragLabel(temd->labelText(), this);
121 QDrag * drag = new QDrag(this);
122 drag->setMimeData(temd);
124 qreal dpr = window()->windowHandle()->devicePixelRatio();
125 QPixmap pixmap(lbl->size() * dpr);
126 pixmap.setDevicePixelRatio(dpr);
128 lbl->render(&pixmap);
129 drag->setPixmap(pixmap);
131 drag->exec(Qt::CopyAction | Qt::MoveAction);
137 return QToolBar::eventFilter(obj, event);
140 void DragDropToolBar::dragEnterEvent(QDragEnterEvent *event)
145 if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
147 if (event->source() == this) {
148 event->setDropAction(Qt::MoveAction);
151 event->acceptProposedAction();
153 } else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
154 if ( event->source() != this )
156 event->setDropAction(Qt::CopyAction);
159 event->acceptProposedAction();
166 void DragDropToolBar::dragMoveEvent(QDragMoveEvent *event)
171 if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
173 QAction * actionAtPos = actionAt(event->pos() );
176 QWidget * widget = widgetForAction(actionAtPos);
179 bool success = false;
180 widget->property(drag_drop_toolbar_action_).toInt(&success);
189 if (event->source() == this) {
190 event->setDropAction(Qt::MoveAction);
193 event->acceptProposedAction();
195 } else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
196 if ( event->source() != this )
198 event->setDropAction(Qt::CopyAction);
201 event->acceptProposedAction();
208 void DragDropToolBar::dropEvent(QDropEvent *event)
213 /* Moving items around */
214 if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
216 const ToolbarEntryMimeData * data = qobject_cast<const ToolbarEntryMimeData *>(event->mimeData());
218 int oldPos = data->position();
220 QAction * action = actionAt(event->pos());
221 if ( action && actions().at(oldPos) )
223 widgetForAction(action)->setStyleSheet("QWidget { border: none; };");
224 newPos = widgetForAction(action)->property(drag_drop_toolbar_action_).toInt();
225 moveToolbarItems(oldPos, newPos);
226 QAction * moveAction = actions().at(oldPos);
228 emit actionMoved(moveAction, oldPos, newPos);
231 if (event->source() == this) {
232 event->setDropAction(Qt::MoveAction);
235 event->acceptProposedAction();
238 } else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
239 const DisplayFilterMimeData * data = qobject_cast<const DisplayFilterMimeData *>(event->mimeData());
241 if ( event->source() != this )
243 event->setDropAction(Qt::CopyAction);
246 emit newFilterDropped(data->description(), data->filter());
249 event->acceptProposedAction();
257 void DragDropToolBar::moveToolbarItems(int fromPos, int newPos)
259 if ( fromPos == newPos )
262 setUpdatesEnabled(false);
264 QList<QAction *> storedActions = actions();
269 storedActions.move(fromPos, newPos);
270 foreach ( QAction * action, storedActions )
273 setUpdatesEnabled(true);
282 * indent-tabs-mode: nil
285 * ex: set shiftwidth=4 tabstop=8 expandtab:
286 * :indentSize=4:tabSize=8:noTabs=true: