1 | #include "ClickableFrame.h" |
---|---|
2 | |
3 | #include <QMouseEvent> |
4 | #include <QVBoxLayout> |
5 | #include <QLabel> |
6 | #include <QStyle> |
7 | |
8 | ClickableFrame::ClickableFrame(QWidget *parent) |
9 | : QFrame(parent) |
10 | { |
11 | setAttribute(Qt::WA_DeleteOnClose); |
12 | } |
13 | |
14 | ClickableFrame::ClickableFrame(const QString &text, Qt::Alignment alignment, QWidget *parent) |
15 | : QFrame(parent) |
16 | { |
17 | const auto layout = new QVBoxLayout(this); |
18 | layout->setContentsMargins(2, 2, 2, 2); |
19 | layout->setSpacing(0); |
20 | layout->addWidget(mText = new QLabel(text)); |
21 | layout->setAlignment(alignment); |
22 | |
23 | mText->setTextFormat(Qt::RichText); |
24 | } |
25 | |
26 | void ClickableFrame::mousePressEvent(QMouseEvent *e) |
27 | { |
28 | mPressed = rect().contains(e->pos()) && e->button() == Qt::LeftButton; |
29 | |
30 | QFrame::mousePressEvent(e); |
31 | } |
32 | |
33 | void ClickableFrame::mouseReleaseEvent(QMouseEvent *e) |
34 | { |
35 | if (mPressed && rect().contains(e->pos()) && e->button() == Qt::LeftButton) |
36 | emit clicked(); |
37 | |
38 | QFrame::mouseReleaseEvent(e); |
39 | } |
40 | |
41 | void ClickableFrame::enterEvent(QEvent *event) |
42 | { |
43 | if (mHasLinkStyles) |
44 | { |
45 | QFont f = mText->font(); |
46 | f.setUnderline(true); |
47 | mText->setFont(f); |
48 | } |
49 | |
50 | QFrame::enterEvent(event); |
51 | } |
52 | |
53 | void ClickableFrame::leaveEvent(QEvent *event) |
54 | { |
55 | if (mHasLinkStyles) |
56 | { |
57 | QFont f = mText->font(); |
58 | f.setUnderline(false); |
59 | mText->setFont(f); |
60 | } |
61 | |
62 | QFrame::leaveEvent(event); |
63 | } |
64 |