From e4713e8745859ea5b8a8ca27a699dec1288c211b Mon Sep 17 00:00:00 2001 From: Roland Knall Date: Sun, 16 Jun 2019 12:02:37 +0200 Subject: [PATCH] Qt: Fix drag-and-drop from packet list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only initiate drag-and-drop when the left mousebutton has been clicked, and also add the filter as plain text, so to enable dragging to external applications. Change-Id: I2f6ac7f9c543d003070c2a6c0ce671ed5a215669 Reviewed-on: https://code.wireshark.org/review/33620 Petri-Dish: Roland Knall Reviewed-by: Stig Bjørlykke Tested-by: Petri Dish Buildbot Reviewed-by: Roland Knall --- ui/qt/packet_list.cpp | 41 +++++++++-------- ui/qt/packet_list.h | 1 + ui/qt/utils/wireshark_mime_data.cpp | 4 +- ui/qt/widgets/display_filter_edit.cpp | 64 +++++++++++++++++---------- ui/qt/widgets/display_filter_edit.h | 2 +- ui/qt/widgets/packet_list_header.cpp | 2 +- 6 files changed, 70 insertions(+), 44 deletions(-) diff --git a/ui/qt/packet_list.cpp b/ui/qt/packet_list.cpp index 16e1538099..e8d0039717 100644 --- a/ui/qt/packet_list.cpp +++ b/ui/qt/packet_list.cpp @@ -651,30 +651,35 @@ void PacketList::mousePressEvent (QMouseEvent *event) setAutoScroll(false); QTreeView::mousePressEvent(event); setAutoScroll(true); +} - QModelIndex curIndex = indexAt(event->pos()); - ctx_column_ = curIndex.column(); - QString filter = getFilterFromRowAndColumn(); - if ( ! filter.isEmpty() ) +void PacketList::mouseMoveEvent (QMouseEvent *event) +{ + if ( event->buttons() & Qt::LeftButton ) { - QString abbrev = filter.left(filter.indexOf(' ')); - QString name = model()->headerData(ctx_column_, header()->orientation()).toString(); + QModelIndex curIndex = indexAt(event->pos()); + ctx_column_ = curIndex.column(); + QString filter = getFilterFromRowAndColumn(); + if ( ! filter.isEmpty() ) + { + QString abbrev = filter.left(filter.indexOf(' ')); + QString name = model()->headerData(ctx_column_, header()->orientation()).toString(); - DisplayFilterMimeData * dfmd = - new DisplayFilterMimeData(name, abbrev, filter); - QDrag * drag = new QDrag(this); - drag->setMimeData(dfmd); + DisplayFilterMimeData * dfmd = + new DisplayFilterMimeData(name, abbrev, filter); + QDrag * drag = new QDrag(this); + drag->setMimeData(dfmd); - DragLabel * content = new DragLabel(dfmd->labelText(), this); - qreal dpr = window()->windowHandle()->devicePixelRatio(); - QPixmap pixmap(content->size() * dpr); - pixmap.setDevicePixelRatio(dpr); - content->render(&pixmap); - drag->setPixmap(pixmap); + DragLabel * content = new DragLabel(dfmd->labelText(), this); + qreal dpr = window()->windowHandle()->devicePixelRatio(); + QPixmap pixmap(content->size() * dpr); + pixmap.setDevicePixelRatio(dpr); + content->render(&pixmap); + drag->setPixmap(pixmap); - drag->exec(Qt::CopyAction); + drag->exec(Qt::CopyAction); + } } - } void PacketList::resizeEvent(QResizeEvent *event) diff --git a/ui/qt/packet_list.h b/ui/qt/packet_list.h index 48fede5c08..e05bd5a3b4 100644 --- a/ui/qt/packet_list.h +++ b/ui/qt/packet_list.h @@ -74,6 +74,7 @@ protected: void timerEvent(QTimerEvent *event); void paintEvent(QPaintEvent *event); virtual void mousePressEvent (QMouseEvent *event); + virtual void mouseMoveEvent (QMouseEvent *event); virtual void resizeEvent(QResizeEvent *event); protected slots: diff --git a/ui/qt/utils/wireshark_mime_data.cpp b/ui/qt/utils/wireshark_mime_data.cpp index a18592faee..7ea3214b0a 100644 --- a/ui/qt/utils/wireshark_mime_data.cpp +++ b/ui/qt/utils/wireshark_mime_data.cpp @@ -14,7 +14,9 @@ QMimeData(), description_(description), filter_(filter), field_(field) -{} +{ + setText(filter); +} QString DisplayFilterMimeData::description() const { diff --git a/ui/qt/widgets/display_filter_edit.cpp b/ui/qt/widgets/display_filter_edit.cpp index 9f985b1784..879c3d41e9 100644 --- a/ui/qt/widgets/display_filter_edit.cpp +++ b/ui/qt/widgets/display_filter_edit.cpp @@ -586,16 +586,18 @@ void DisplayFilterEdit::dropEvent(QDropEvent *event) else filterText = data->filter(); - if ( text().length() > 0 && QApplication::keyboardModifiers() & Qt::MetaModifier) + bool prepare = QApplication::keyboardModifiers() & Qt::ShiftModifier; + + if ( text().length() > 0 || QApplication::keyboardModifiers() & Qt::MetaModifier) { - createFilterTextDropMenu(event, filterText); + createFilterTextDropMenu(event, prepare, filterText); return; } setText(filterText); // Holding down the Shift key will only prepare filter. - if (!(QApplication::keyboardModifiers() & Qt::ShiftModifier)) { + if ( ! prepare ) { applyDisplayFilter(); } @@ -608,33 +610,45 @@ void DisplayFilterEdit::dropEvent(QDropEvent *event) } } -void DisplayFilterEdit::createFilterTextDropMenu(QDropEvent *event, QString filterText) +void DisplayFilterEdit::createFilterTextDropMenu(QDropEvent *event, bool prepare, QString filterText) { if ( filterText.isEmpty() ) return; QMenu applyMenu(this); - QAction * andAction = new QAction(tr(UTF8_HORIZONTAL_ELLIPSIS "and Selected"), this); - andAction->setData(QString("&& %1").arg(filterText)); - connect(andAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); + QAction * selAction = applyMenu.addAction(tr("Selected")); + selAction->setData(QString("%1").arg(filterText)); + selAction->setProperty("clear", qVariantFromValue(true)); + connect(selAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - QAction * orAction = new QAction(tr(UTF8_HORIZONTAL_ELLIPSIS "or Selected"), this); - orAction->setData(QString("|| %1").arg(filterText)); - connect(orAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); + QAction * notSelAction = applyMenu.addAction(tr("Not Selected")); + notSelAction->setData(QString("!(%1)").arg(filterText)); + notSelAction->setProperty("clear", qVariantFromValue(true)); + connect(notSelAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - QAction * andNotAction = new QAction(tr(UTF8_HORIZONTAL_ELLIPSIS "and not Selected"), this); - andNotAction->setData(QString("&& !(%1)").arg(filterText)); - connect(andNotAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); + if ( this->text().length() > 0 ) + { + QAction * andAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "and Selected")); + andAction->setData(QString("&& %1").arg(filterText)); + connect(andAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); - QAction * orNotAction = new QAction(tr(UTF8_HORIZONTAL_ELLIPSIS "or not Selected"), this); - orNotAction->setData(QString("|| !(%1)").arg(filterText)); - connect(orNotAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); + QAction * orAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "or Selected")); + orAction->setData(QString("|| %1").arg(filterText)); + connect(orAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); + + QAction * andNotAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "and not Selected")); + andNotAction->setData(QString("&& !(%1)").arg(filterText)); + connect(andNotAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); + + QAction * orNotAction = applyMenu.addAction(tr(UTF8_HORIZONTAL_ELLIPSIS "or not Selected")); + orNotAction->setData(QString("|| !(%1)").arg(filterText)); + connect(orNotAction, &QAction::triggered, this, &DisplayFilterEdit::dropActionMenuEvent); + } + + foreach ( QAction * action, applyMenu.actions() ) + action->setProperty("prepare", qVariantFromValue(prepare)); - applyMenu.addAction(andAction); - applyMenu.addAction(orAction); - applyMenu.addAction(andNotAction); - applyMenu.addAction(orNotAction); applyMenu.exec(this->mapToGlobal(event->pos())); } @@ -646,14 +660,18 @@ void DisplayFilterEdit::dropActionMenuEvent() return; QString value = sendAction->data().toString(); + bool prepare = sendAction->property("prepare").toBool(); - QString filterText = QString("((%1) %2)").arg(this->text()).arg(value); + QString filterText; + if ( sendAction->property("clear").toBool() ) + filterText = value; + else + filterText = QString("((%1) %2)").arg(this->text()).arg(value); setText(filterText); // Holding down the Shift key will only prepare filter. - if (!(QApplication::keyboardModifiers() & Qt::ShiftModifier)) { + if ( ! prepare ) applyDisplayFilter(); - } } /* diff --git a/ui/qt/widgets/display_filter_edit.h b/ui/qt/widgets/display_filter_edit.h index 2ecf693b56..85ea801433 100644 --- a/ui/qt/widgets/display_filter_edit.h +++ b/ui/qt/widgets/display_filter_edit.h @@ -69,7 +69,7 @@ private: void setDefaultPlaceholderText(); void buildCompletionList(const QString& field_word); - void createFilterTextDropMenu(QDropEvent *event, QString filterText = QString()); + void createFilterTextDropMenu(QDropEvent *event, bool prepare, QString filterText = QString()); signals: void pushFilterSyntaxStatus(const QString&); diff --git a/ui/qt/widgets/packet_list_header.cpp b/ui/qt/widgets/packet_list_header.cpp index 660ae64c1b..8750672efe 100644 --- a/ui/qt/widgets/packet_list_header.cpp +++ b/ui/qt/widgets/packet_list_header.cpp @@ -41,7 +41,7 @@ void PacketListHeader::dragEnterEvent(QDragEnterEvent *event) if ( ! event ) return; - if (qobject_cast(event->mimeData())) + if ( qobject_cast(event->mimeData()) && event->source() != this->parent() ) { if ( event->source() != this ) {