1 /* clickable_label.cpp 2 * 3 * Taken from https://wiki.qt.io/Clickable_QLabel and adapted for usage 4 * 5 * Wireshark - Network traffic analyzer 6 * By Gerald Combs <gerald@wireshark.org> 7 * Copyright 1998 Gerald Combs 8 * 9 * SPDX-License-Identifier: GPL-2.0-or-later 10 */ 11 12 #include <ui/qt/widgets/clickable_label.h> 13 14 #include <QMouseEvent> 15 ClickableLabel(QWidget * parent)16ClickableLabel::ClickableLabel(QWidget* parent) 17 : QLabel(parent) 18 { 19 setMinimumWidth(0); 20 setText(QString()); 21 22 setStyleSheet(QString( 23 "QLabel {" 24 " margin-left: 0.5em;" 25 " }" 26 )); 27 } 28 mouseReleaseEvent(QMouseEvent * event)29void ClickableLabel::mouseReleaseEvent(QMouseEvent * event) 30 { 31 /* It has to be ensured, that if the user clicks on the label and then moves away out of 32 * the scope of the widget, the event does not fire. Otherwise this behavior differs from 33 * the way, the toolbar buttons work for instance */ 34 if (event->pos().x() < 0 || event->pos().x() > size().width()) 35 return; 36 if (event->pos().y() < 0 || event->pos().y() > size().height()) 37 return; 38 39 emit clicked(); 40 } 41 mousePressEvent(QMouseEvent * event)42void ClickableLabel::mousePressEvent(QMouseEvent *event) 43 { 44 if (event->button() == Qt::LeftButton) 45 emit clickedAt(QPoint(event->globalPos()), Qt::LeftButton); 46 } 47 contextMenuEvent(QContextMenuEvent * event)48void ClickableLabel::contextMenuEvent(QContextMenuEvent *event) 49 { 50 emit clickedAt(QPoint(event->globalPos()), Qt::RightButton); 51 } 52