1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "detailsbutton.h"
6
7#include <QPainter>
8#include <QStyleOption>
9#include <QPalette>
10#include <QDebug>
11#include <QPaintEvent>
12#include <QPropertyAnimation>
13
14class DetailsButtonPrivate
15{
16 friend class DetailsButton;
17 bool hover = false;
18};
19
20DetailsButton::DetailsButton(QWidget *parent)
21 : QAbstractButton (parent)
22 , d(new DetailsButtonPrivate)
23{
24 setCheckable(true);
25 setText(tr("Details"));
26 setFocusPolicy(Qt::FocusPolicy::StrongFocus);
27}
28
29DetailsButton::~DetailsButton()
30{
31 if (d)
32 delete d;
33}
34
35void DetailsButton::paintEvent(QPaintEvent *event)
36{
37 QPainter p(this);
38 int arrowsize = 15;
39 auto pen = p.pen();
40
41 QRect rect = DetailsButton::rect();
42 p.drawLine(rect.topLeft(), rect.topRight());
43 p.drawLine(rect.topLeft(), rect.bottomLeft());
44 p.drawLine(rect.topRight(), rect.bottomRight());
45 p.drawLine(rect.bottomLeft(), rect.bottomRight());
46
47 if (!d->hover) {
48 p.setPen(Qt::NoPen);
49 p.setBrush(palette().window().color());
50 p.drawRoundedRect(rect.adjusted(1, 1, -1, -1), 0, 0);
51 } else {
52 p.setPen(Qt::NoPen);
53 p.setBrush(QColor(0, 0, 0, 255));
54 p.drawRoundedRect(rect.adjusted(1, 1, -1, -1), 0, 0 );
55 }
56
57 p.setPen(pen);
58 QFont font(text());
59 QFontMetrics fontMetrics(this->font());
60 QRect textBoundingRect = fontMetrics.boundingRect(text());
61 QSize fontSize = textBoundingRect.size();
62 int fontPosX = textBoundingRect.x();
63 int fontPosY = textBoundingRect.y();
64 int textPosX = (size().width() - fontSize.width() - arrowsize) / 2
65 + ( fontPosX > 0 ? fontPosX : - fontPosX);
66 int textPosY = (size().height() - fontSize.height()) / 2
67 + ( fontPosY > 0 ? fontPosY: - fontPosY);
68 p.drawText(QPoint{textPosX, textPosY}, this->text());
69
70 bool checked = isChecked();
71 QStyleOption arrowOpt;
72 arrowOpt.initFrom(this);
73 QPalette pal = arrowOpt.palette;
74 pal.setBrush(QPalette::All, QPalette::Text, QColor(0, 0, 0));
75 arrowOpt.rect = QRect{size().width() - arrowsize - 6, (height() - arrowsize) / 2 , arrowsize, arrowsize};
76 arrowOpt.palette = pal;
77 style()->drawPrimitive(checked ? QStyle::PE_IndicatorArrowUp : QStyle::PE_IndicatorArrowDown, &arrowOpt, &p, this);
78}
79
80void DetailsButton::enterEvent(QEvent *event)
81{
82 d->hover = true;
83 return QAbstractButton::enterEvent(event);
84}
85
86void DetailsButton::leaveEvent(QEvent *event)
87{
88 d->hover = false;
89 return QAbstractButton::enterEvent(event);
90}
91