2017-06-27 14:36:19 +00:00
|
|
|
/* drag_drop_toolbar.cpp
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ui/qt/widgets/drag_drop_toolbar.h>
|
2017-11-07 15:51:43 +00:00
|
|
|
#include <ui/qt/utils/wireshark_mime_data.h>
|
2017-06-27 14:36:19 +00:00
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QToolBar>
|
|
|
|
#include <QToolButton>
|
|
|
|
#include <QDrag>
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QMimeData>
|
|
|
|
#include <QMouseEvent>
|
2017-08-31 06:28:02 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
2017-06-27 14:36:19 +00:00
|
|
|
#include <QWindow>
|
2017-08-31 06:28:02 +00:00
|
|
|
#endif
|
2017-06-27 14:36:19 +00:00
|
|
|
|
|
|
|
#define drag_drop_toolbar_action_ "drag_drop_toolbar_action_"
|
|
|
|
|
|
|
|
DragDropToolBar::DragDropToolBar(const QString &title, QWidget *parent) :
|
|
|
|
QToolBar(title, parent)
|
|
|
|
{
|
|
|
|
childCounter = 0;
|
|
|
|
setAcceptDrops(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
DragDropToolBar::DragDropToolBar(QWidget *parent) :
|
|
|
|
QToolBar(parent)
|
|
|
|
{
|
|
|
|
childCounter = 0;
|
|
|
|
setAcceptDrops(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
DragDropToolBar::~DragDropToolBar()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DragDropToolBar::childEvent(QChildEvent * event)
|
|
|
|
{
|
|
|
|
/* New action has been added */
|
|
|
|
if ( event->type() == QEvent::ChildAdded )
|
|
|
|
{
|
|
|
|
if ( event->child()->isWidgetType() )
|
|
|
|
{
|
2017-11-04 15:11:08 +00:00
|
|
|
/* Reset if it has moved underneath lower limit */
|
|
|
|
if ( childCounter < 0 )
|
|
|
|
childCounter = 0;
|
|
|
|
|
2017-06-27 14:36:19 +00:00
|
|
|
((QWidget *)event->child())->installEventFilter(this);
|
|
|
|
event->child()->setProperty(drag_drop_toolbar_action_, qVariantFromValue(childCounter));
|
|
|
|
childCounter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( event->type() == QEvent::ChildRemoved )
|
|
|
|
{
|
2017-11-04 15:11:08 +00:00
|
|
|
childCounter--;
|
|
|
|
}
|
|
|
|
else if ( event->type() == QEvent::ChildPolished )
|
|
|
|
{
|
|
|
|
/* Polish is called every time a child is added or removed. This is implemented by adding
|
|
|
|
* all childs again as hidden elements, and afterwards removing the existing ones. Therefore
|
|
|
|
* we have to reset child counter here, if a widget is being polished. If this is not being
|
|
|
|
* done, crashes will occur after an item has been removed and other items are moved afterwards */
|
|
|
|
if ( event->child()->isWidgetType() )
|
|
|
|
childCounter = 0;
|
2017-06-27 14:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DragDropToolBar::eventFilter(QObject * obj, QEvent * event)
|
|
|
|
{
|
|
|
|
if ( ! obj->isWidgetType() )
|
|
|
|
return QToolBar::eventFilter(obj, event);
|
|
|
|
|
|
|
|
QWidget * elem = qobject_cast<QWidget *>(obj);
|
|
|
|
|
|
|
|
if ( ! elem || ( event->type() != QEvent::MouseButtonPress && event->type() != QEvent::MouseMove ) )
|
|
|
|
return QToolBar::eventFilter(obj, event);
|
|
|
|
|
|
|
|
QMouseEvent * ev = (QMouseEvent *)event;
|
|
|
|
|
|
|
|
if ( event->type() == QEvent::MouseButtonPress )
|
|
|
|
{
|
|
|
|
if ( ev->buttons() & Qt::LeftButton )
|
|
|
|
dragStartPosition = ev->pos();
|
|
|
|
}
|
|
|
|
else if ( event->type() == QEvent::MouseMove )
|
|
|
|
{
|
|
|
|
if ( ( ev->buttons() & Qt::LeftButton ) && (ev->pos() - dragStartPosition).manhattanLength()
|
|
|
|
> QApplication::startDragDistance())
|
|
|
|
{
|
2017-11-04 15:11:08 +00:00
|
|
|
QDrag * drag = new QDrag(this);
|
2017-11-07 15:51:43 +00:00
|
|
|
drag->setMimeData(new ToolbarEntryMimeData(elem->property(drag_drop_toolbar_action_).toInt()));
|
2017-06-27 14:36:19 +00:00
|
|
|
|
2017-08-31 06:28:02 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
|
2017-06-27 14:36:19 +00:00
|
|
|
qreal dpr = window()->windowHandle()->devicePixelRatio();
|
|
|
|
QPixmap pixmap(elem->size() * dpr);
|
|
|
|
pixmap.setDevicePixelRatio(dpr);
|
2017-08-31 06:28:02 +00:00
|
|
|
#else
|
|
|
|
QPixmap pixmap(elem->size());
|
|
|
|
#endif
|
2017-06-27 14:36:19 +00:00
|
|
|
elem->render(&pixmap);
|
|
|
|
drag->setPixmap(pixmap);
|
|
|
|
|
2017-11-04 15:11:08 +00:00
|
|
|
drag->exec(Qt::CopyAction | Qt::MoveAction);
|
2017-06-27 14:36:19 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QToolBar::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DragDropToolBar::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
2017-11-07 15:51:43 +00:00
|
|
|
if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
|
2017-06-27 14:36:19 +00:00
|
|
|
{
|
|
|
|
if (event->source() == this) {
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
2017-11-07 15:51:43 +00:00
|
|
|
} else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
|
|
|
|
if ( event->source() != this )
|
|
|
|
{
|
|
|
|
event->setDropAction(Qt::CopyAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
2017-11-07 15:51:43 +00:00
|
|
|
} else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
|
|
|
|
if ( event->source() != this )
|
|
|
|
{
|
|
|
|
event->setDropAction(Qt::CopyAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
2017-06-27 14:36:19 +00:00
|
|
|
} else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DragDropToolBar::dragMoveEvent(QDragMoveEvent *event)
|
|
|
|
{
|
2017-11-07 15:51:43 +00:00
|
|
|
if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
|
2017-06-27 14:36:19 +00:00
|
|
|
{
|
|
|
|
if (event->source() == this) {
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
QAction * action = actionAt(event->pos());
|
|
|
|
if ( action )
|
|
|
|
{
|
|
|
|
foreach(QAction * act, actions())
|
|
|
|
{
|
|
|
|
if ( widgetForAction(act) )
|
|
|
|
widgetForAction(act)->setStyleSheet("QWidget { border: none; };");
|
|
|
|
}
|
|
|
|
|
|
|
|
widgetForAction(action)->setStyleSheet("QWidget { border: 2px dotted grey; };");
|
|
|
|
}
|
|
|
|
}
|
2017-11-07 15:51:43 +00:00
|
|
|
} else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
|
|
|
|
if ( event->source() != this )
|
|
|
|
{
|
|
|
|
event->setDropAction(Qt::CopyAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
2017-06-27 14:36:19 +00:00
|
|
|
} else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DragDropToolBar::dropEvent(QDropEvent *event)
|
|
|
|
{
|
2017-11-07 15:51:43 +00:00
|
|
|
/* Moving items around */
|
2017-11-07 15:51:43 +00:00
|
|
|
if (qobject_cast<const ToolbarEntryMimeData *>(event->mimeData()))
|
2017-06-27 14:36:19 +00:00
|
|
|
{
|
2017-11-07 15:51:43 +00:00
|
|
|
const ToolbarEntryMimeData * data = qobject_cast<const ToolbarEntryMimeData *>(event->mimeData());
|
|
|
|
|
|
|
|
int oldPos = data->position();
|
2017-06-27 14:36:19 +00:00
|
|
|
int newPos = -1;
|
|
|
|
QAction * action = actionAt(event->pos());
|
|
|
|
if ( action && actions().at(oldPos) )
|
|
|
|
{
|
|
|
|
widgetForAction(action)->setStyleSheet("QWidget { border: none; };");
|
|
|
|
newPos = widgetForAction(action)->property(drag_drop_toolbar_action_).toInt();
|
|
|
|
moveToolbarItems(oldPos, newPos);
|
|
|
|
emit actionMoved(actions().at(oldPos), oldPos, newPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->source() == this) {
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
2017-11-07 15:51:43 +00:00
|
|
|
} else if (qobject_cast<const DisplayFilterMimeData *>(event->mimeData())) {
|
|
|
|
const DisplayFilterMimeData * data = qobject_cast<const DisplayFilterMimeData *>(event->mimeData());
|
|
|
|
|
|
|
|
if ( event->source() != this )
|
|
|
|
{
|
|
|
|
event->setDropAction(Qt::CopyAction);
|
|
|
|
event->accept();
|
|
|
|
|
|
|
|
emit newFilterDropped(data->description(), data->filter());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
2017-06-27 14:36:19 +00:00
|
|
|
} else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DragDropToolBar::moveToolbarItems(int fromPos, int newPos)
|
|
|
|
{
|
|
|
|
if ( fromPos == newPos )
|
|
|
|
return;
|
|
|
|
|
|
|
|
setUpdatesEnabled(false);
|
|
|
|
|
|
|
|
QList<QAction *> storedActions = actions();
|
|
|
|
|
|
|
|
clear();
|
|
|
|
childCounter = 0;
|
|
|
|
|
|
|
|
storedActions.move(fromPos, newPos);
|
|
|
|
foreach ( QAction * action, storedActions )
|
|
|
|
addAction(action);
|
|
|
|
|
|
|
|
setUpdatesEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|