Debian Codegraph @

src/album/widgets/expansionmenu.cpp

raw
1 #include "expansionmenu.h"
2 #include "expansionpanel.h"
3
4 #include <QHBoxLayout>
5 #include <DCommandLinkButton>
6 #include <DFontSizeManager>
7 #include <DGuiApplicationHelper>
8 #include <DHiDPIHelper>
9 #include <DLabel>
10 #include <QAbstractButton>
11 #include <QMouseEvent>
12
13 FilterWidget::FilterWidget(QWidget *parent): QWidget(parent)
14 {
15 QHBoxLayout *hb = new QHBoxLayout(this);
16 hb->setSpacing(4);
17 hb->setContentsMargins(0, 0, 0, 0);
18 this->setContentsMargins(0, 0, 10, 0);
19 this->setLayout(hb);
20
21 m_leftLabel = new FilterLabel(this);
22 m_leftLabel->setFixedSize(QSize(16, 16));
23 DFontSizeManager::instance()->bind(m_leftLabel, DFontSizeManager::T5, QFont::Normal);
24 hb->addWidget(m_leftLabel);
25
26 m_btn = new FilterLabel(this);
27 DFontSizeManager::instance()->bind(m_btn, DFontSizeManager::T5, QFont::Normal);
28 hb->addWidget(m_btn);
29
30 m_rightLabel = new FilterLabel(this);
31 DFontSizeManager::instance()->bind(m_rightLabel, DFontSizeManager::T5, QFont::Normal);
32 hb->addWidget(m_rightLabel);
33 m_rightLabel->setPixmap(QIcon::fromTheme("album_arrowdown").pixmap(QSize(8, 6)));
34
35 connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged,
36 this, &FilterWidget::themeTypeChanged);
37 themeTypeChanged(DGuiApplicationHelper::instance()->themeType());
38
39 m_btn->installEventFilter(this);
40 m_rightLabel->installEventFilter(this);
41 }
42
43 FilterWidget::~FilterWidget()
44 {
45
46 }
47
48 void FilterWidget::setIcon(QIcon icon)
49 {
50 m_leftLabel->setPixmap(icon.pixmap(QSize(14, 14)));
51 }
52
53 void FilterWidget::setText(QString text)
54 {
55 m_btn->setText(text);
56 }
57
58 void FilterWidget::setFilteData(ExpansionPanel::FilteData &data)
59 {
60 m_data = data;
61 themeTypeChanged(DGuiApplicationHelper::instance()->themeType());
62 m_btn->setText(data.text);
63 }
64
65 ItemType FilterWidget::getFilteType()
66 {
67 return m_data.type;
68 }
69
70 void FilterWidget::onClicked()
71 {
72 emit clicked();
73 }
74
75 void FilterWidget::themeTypeChanged(int type)
76 {
77 if (type == 1) {
78 QString path = ":/icons/deepin/builtin/icons/light/";
79 path += m_data.icon_r_path;
80 path += "_16px.svg";
81 QPixmap pix = DHiDPIHelper::loadNxPixmap(path);
82 const qreal ratio = devicePixelRatioF();
83 pix.setDevicePixelRatio(ratio);
84 m_leftLabel->setPixmap(pix);
85 m_rightLabel->setPixmap(DHiDPIHelper::loadNxPixmap(":/icons/deepin/builtin/icons/light/album_arrowdown_10px.svg").scaled(10, 10));
86 m_btn->setText(m_data.text);
87 } else {
88 QString path = ":/icons/deepin/builtin/icons/dark/";
89 path += m_data.icon_r_path;
90 path += "_16px.svg";
91 QPixmap pix = DHiDPIHelper::loadNxPixmap(path);
92 const qreal ratio = devicePixelRatioF();
93 pix.setDevicePixelRatio(ratio);
94 m_leftLabel->setPixmap(pix);
95 m_rightLabel->setPixmap(DHiDPIHelper::loadNxPixmap(":/icons/deepin/builtin/icons/dark/album_arrowdown_10px.svg").scaled(10, 10));
96 m_btn->setText(m_data.text);
97 }
98 }
99
100 bool FilterWidget::eventFilter(QObject *obj, QEvent *event)
101 {
102 if (obj == m_btn || obj == m_rightLabel) {
103 QMouseEvent *e = dynamic_cast<QMouseEvent *>(event);
104 if (e && (e->type() == QEvent::MouseMove)) {
105 //解决触屏上点击后移动整个应用问题
106 return true;
107 } else if (e && e->type() == QEvent::MouseButtonPress) {
108 onClicked();
109 return true;
110 }
111 }
112 return QWidget::eventFilter(obj, event);
113 }
114
115
116 ExpansionMenu::ExpansionMenu(QWidget *parent)
117 : QObject(parent)
118 {
119 panel = new ExpansionPanel();
120 panel->setVisible(false);
121 mainButton = new FilterWidget(parent);
122 connect(panel, &ExpansionPanel::currentItemChanged, this, &ExpansionMenu::onCurrentItemChanged);
123 connect(mainButton, &FilterWidget::clicked, this, &ExpansionMenu::onMainButtonClicked);
124 }
125
126 FilterWidget *ExpansionMenu::mainWidget()
127 {
128 return mainButton;
129 }
130
131 void ExpansionMenu::onCurrentItemChanged(ExpansionPanel::FilteData &data)
132 {
133 mainButton->setFilteData(data);
134 emit mainButton->currentItemChanged(data);
135 }
136
137 void ExpansionMenu::onMainButtonClicked()
138 {
139 qDebug() << __FUNCTION__ << "---" << panel->isHidden();
140 panel->isHidden() ? panel->show() : panel->hide();
141 panel->setGeometry(QCursor().pos().x(), QCursor().pos().y() + 15, 0, 0);
142 }
143
144 void ExpansionMenu::addNewButton(ExpansionPanel::FilteData &data)
145 {
146 panel->addNewButton(data);
147 }
148
149 void ExpansionMenu::setDefaultFilteData(ExpansionPanel::FilteData &data)
150 {
151 mainButton->setFilteData(data);
152 }
153
154
155 FilterLabel::FilterLabel(QWidget *parent)
156 {
157
158 }
159
160 FilterLabel::~FilterLabel()
161 {
162
163 }
164
165 void FilterLabel::mouseReleaseEvent(QMouseEvent *ev)
166 {
167 emit clicked();
168 QLabel::mouseReleaseEvent(ev);
169 }