maxLibQt
BuddyLabel.h
Go to the documentation of this file.
1 /*
2  BuddyLabel
3  https://github.com/mpaperno/maxLibQt
4 
5  COPYRIGHT: (c)2019 Maxim Paperno; All Right Reserved.
6  Contact: http://www.WorldDesign.com/contact
7 
8  LICENSE:
9 
10  Commercial License Usage
11  Licensees holding valid commercial licenses may use this file in
12  accordance with the terms contained in a written agreement between
13  you and the copyright holder.
14 
15  GNU General Public License Usage
16  Alternatively, this file may be used under the terms of the GNU
17  General Public License as published by the Free Software Foundation,
18  either version 3 of the License, or (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
26 */
27 
28 #ifndef BUDDYLABEL_H
29 #define BUDDYLABEL_H
30 
31 #include <QLabel>
32 #include <QAbstractSpinBox>
33 #include <QCheckBox>
34 #include <QComboBox>
35 #include <QMouseEvent>
36 #include <QLineEdit>
37 
48 class BuddyLabel : public QLabel
49 {
50  Q_OBJECT
51  public:
52  using QLabel::QLabel;
53 
54  public slots:
57  {
58  if (this->buddy()) {
59  this->buddy()->removeEventFilter(this);
60  disconnect(this->buddy());
61  disconnectBuddy(this->buddy());
62  }
63 
64  QLabel::setBuddy(buddy);
65 
66  if (!buddy)
67  return;
68 
72  }
73 
74  signals:
76  void clicked();
78  void doubleClicked();
79 
80  protected:
82  virtual void connectBuddy(QWidget *buddy)
83  {
84  // Single clicks
85  connect(this, &BuddyLabel::clicked, buddy, QOverload<>::of(&QWidget::setFocus));
86  if (QCheckBox *cb = qobject_cast<QCheckBox*>(buddy))
88 
89  // Double clicks
90  if (QLineEdit *le = qobject_cast<QLineEdit*>(buddy))
92  else if (QAbstractSpinBox *sb = qobject_cast<QAbstractSpinBox*>(buddy))
94  else if (QComboBox *cb = qobject_cast<QComboBox*>(buddy))
95  if (cb->isEditable() && cb->lineEdit())
96  connect(this, &BuddyLabel::doubleClicked, cb->lineEdit(), &QLineEdit::selectAll);
97  }
98 
100  virtual void disconnectBuddy(QWidget *buddy) { Q_UNUSED(buddy) }
101 
103  bool eventFilter(QObject *obj, QEvent *ev)
104  {
105  if (ev->type() == QEvent::ToolTipChange && buddy() && obj == buddy())
106  setToolTip(buddy()->toolTip());
107  return false;
108  }
109 
111  {
112  if (ev->button() == Qt::LeftButton) {
113  m_pressed = true;
114  ev->accept();
115  }
117  }
118 
120  {
121  if (m_pressed && rect().contains(ev->pos()))
122  emit clicked();
123  m_pressed = false;
125  }
126 
128  {
129  if (ev->button() == Qt::LeftButton && rect().contains(ev->pos()))
130  emit doubleClicked();
132  }
133 
134  private:
135  bool m_pressed = false;
136  Q_DISABLE_COPY(BuddyLabel)
137 };
138 
139 #endif // BUDDYLABEL_H
QWidget * buddy() const const
void mouseDoubleClickEvent(QMouseEvent *ev)
Definition: BuddyLabel.h:127
QEvent::Type type() const const
LeftButton
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
bool eventFilter(QObject *obj, QEvent *ev)
The filter monitors for tool tip changes on the buddy.
Definition: BuddyLabel.h:103
void setBuddy(QWidget *buddy)
void mouseReleaseEvent(QMouseEvent *ev)
Definition: BuddyLabel.h:119
void installEventFilter(QObject *filterObj)
void setFocus()
Qt::MouseButton button() const const
void doubleClicked()
Emitted when label is double-clicked with left mouse button (or something emulating one).
void selectAll()
QRect rect() const const
void accept()
void mousePressEvent(QMouseEvent *ev)
Definition: BuddyLabel.h:110
void clicked()
Emitted when label is clicked with left mouse button (or something emulating one).
virtual void mouseDoubleClickEvent(QMouseEvent *event)
bool contains(const QRect &rectangle, bool proper) const const
QLabel(QWidget *parent, Qt::WindowFlags f)
virtual void connectBuddy(QWidget *buddy)
Override this method for custom connections.
Definition: BuddyLabel.h:82
virtual void mouseReleaseEvent(QMouseEvent *ev) override
void setBuddy(QWidget *buddy)
Overrides the QLabel::setBuddy() method, which isn't virtual. Calls the base class implementation as ...
Definition: BuddyLabel.h:56
The BuddyLabel class is a QLabel with enhanced "buddy" capabilities.
Definition: BuddyLabel.h:48
QPoint pos() const const
void setToolTip(const QString &)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void removeEventFilter(QObject *obj)
virtual void mousePressEvent(QMouseEvent *ev) override
virtual void disconnectBuddy(QWidget *buddy)
Hook for custom disconnections. We already disconnect ourselves from all slots in buddy in the main h...
Definition: BuddyLabel.h:100