TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musiccolordialog.cpp
Go to the documentation of this file.
1 #include "musiccolordialog.h"
2 #include "ui_musiccolordialog.h"
3 
4 #include <QToolTip>
5 #include <QButtonGroup>
6 #include <QPainterPath>
7 
9  : QWidget(parent),
10  m_dblSaturation(1.0)
11 {
12  setMouseTracking(true);
13  setMinimumSize(QSize(360, 120));
14 }
15 
16 QColor MusicHlPalette::color() const
17 {
18  return m_color;
19 }
20 
21 void MusicHlPalette::setColor(const QColor &color)
22 {
23  m_color = color;
24 #if TTK_QT_VERSION_CHECK(6,0,0)
25  float vh, vs, vl = -100.0f;
26 #else
27  qreal vh, vs, vl = -100.0f;
28 #endif
29  m_color.getHslF(&vh, &vs, &vl);
30  m_dblSaturation = vs;
31  m_ptfVernierPercentPos.setX(vh);
32  m_ptfVernierPercentPos.setY(1 - vl);
33  Q_EMIT colorChanged(m_color);
34 }
35 
37 {
38  m_ptVernierPos = rect().center();
40  update();
41 }
42 
43 void MusicHlPalette::setSaturation(double dblsaturation)
44 {
45  m_dblSaturation = dblsaturation;
47  update();
48 }
49 
50 void MusicHlPalette::paintEvent(QPaintEvent *event)
51 {
52  Q_UNUSED(event);
53  QPainter painter(this);
54  painter.setRenderHint(QPainter::Antialiasing, true);
55  painter.setBrush(Qt::NoBrush);
56 
57  const int ntRight = rect().right();
58  const int ntBottm = rect().bottom();
59  QColor colorStart, colorDatum, colorFinal;
60 
61  for(int it = 0; it < ntRight + 1; ++it)
62  {
63  colorStart.setHslF(it / double(ntRight), m_dblSaturation, 1);
64  colorDatum.setHslF(it / double(ntRight), m_dblSaturation, 0.5);
65  colorFinal.setHslF(it / double(ntRight), m_dblSaturation, 0);
66 
67  QLinearGradient linearGradient;
68  linearGradient.setStart(QPointF(it, 0));
69  linearGradient.setFinalStop(QPointF(it, ntBottm));
70  linearGradient.setColorAt(0.0, colorStart);
71  linearGradient.setColorAt(0.5, colorDatum);
72  linearGradient.setColorAt(1.0, colorFinal);
73 
74  painter.setPen(QPen(linearGradient, 1));
75  painter.drawLine(QPointF(it, 0), QPointF(it, ntBottm));
76  }
77 
78  painter.setPen(QPen(Qt::black, 2));
79  painter.drawEllipse(m_ptVernierPos, 5, 5);
80 
81  painter.setPen(QPen(Qt::white, 2));
82  painter.drawEllipse(m_ptVernierPos, 7, 7);
83 }
84 
85 void MusicHlPalette::resizeEvent(QResizeEvent *event)
86 {
87  Q_UNUSED(event);
88  m_ptVernierPos.setX(rect().right() * m_ptfVernierPercentPos.rx());
89  m_ptVernierPos.setY(rect().bottom() * m_ptfVernierPercentPos.ry());
90  update();
91 }
92 
93 void MusicHlPalette::mousePressEvent(QMouseEvent *event)
94 {
95  if(event->button() == Qt::LeftButton)
96  {
97  m_ptVernierPos = event->pos();
99  update();
100  }
101 }
102 
103 void MusicHlPalette::mouseMoveEvent(QMouseEvent *event)
104 {
105  if(event->buttons() & Qt::LeftButton && !(event->buttons() & Qt::RightButton))
106  {
107  m_ptVernierPos = event->pos();
108  if(rect().contains(m_ptVernierPos))
109  {
110  calculateColor();
111  update();
112  }
113  }
114  else
115  {
116  QPainterPath path;
117  path.addEllipse(m_ptVernierPos, 7, 7);
118 
119  if(path.contains(event->pos()))
120  {
121  QToolTip::showText(mapToGlobal(event->pos()) + QPoint(0, 5), tr("Adjust hue and brightness"), this, QRect(m_ptVernierPos - QPoint(8, 8), QSize(16, 16)));
122  }
123  }
124 }
125 
127 {
128  m_ptfVernierPercentPos.setX(m_ptVernierPos.x() / TTKStaticCast(double, rect().right()));
129  m_ptfVernierPercentPos.setY(m_ptVernierPos.y() / TTKStaticCast(double, rect().bottom()));
131  Q_EMIT colorChanged(m_color);
132 }
133 
134 
135 
137  : QWidget(parent),
138  m_color(Qt::red),
139  m_dblVernierX(0),
140  m_dblVernierPercentX(0),
141  m_dblSaturation(0)
142 {
143  setMouseTracking(true);
144  setMinimumWidth(360);
145  setMinimumHeight(16);
146  setMaximumHeight(24);
147 }
148 
150 {
151  return m_dblSaturation;
152 }
153 
154 void MusicHlSaturationPalette::setSaturation(double dblsaturation)
155 {
156  m_dblSaturation = dblsaturation;
158  m_dblVernierX = m_dblVernierPercentX * rect().right();
159 }
160 
161 void MusicHlSaturationPalette::setBaseColor(const QColor &color)
162 {
163  m_color = color;
164  update();
165 }
166 
167 void MusicHlSaturationPalette::paintEvent(QPaintEvent *event)
168 {
169  Q_UNUSED(event);
170  QPainter painter(this);
171  painter.setRenderHint(QPainter::Antialiasing, true);
172  painter.setBrush(Qt::NoBrush);
173 
174  const int ntRight = rect().right();
175  const int ntBottm = rect().bottom();
176 
177 #if TTK_QT_VERSION_CHECK(6,0,0)
178  float vh, vs, vl = -100.0f;
179 #else
180  qreal vh, vs, vl = -100.0f;
181 #endif
182  m_color.getHslF(&vh, &vs, &vl);
183 
184  QColor colorCenter, colorStart, colorFinal;
185  colorCenter.setHslF(vh, 0.5, vl);
186  colorStart.setHslF(vh, 1, vl);
187  colorFinal.setHslF(vh, 0, vl);
188 
189  QLinearGradient linearGradient;
190  linearGradient.setStart(QPointF(0, 0));
191  linearGradient.setFinalStop(QPointF(ntRight, 0));
192  linearGradient.setColorAt(0.0, colorStart);
193  linearGradient.setColorAt(1.0, colorFinal);
194 
195  painter.fillRect(rect(), linearGradient);
196 
197  const QPointF ptfCenter(m_dblVernierX, ntBottm / 2.0);
198  painter.setPen(QPen(Qt::black, 2));
199  painter.drawEllipse(ptfCenter, 5, 5);
200 
201  painter.setPen(QPen(Qt::white, 2));
202  painter.drawEllipse(ptfCenter, 7, 7);
203 }
204 
205 void MusicHlSaturationPalette::resizeEvent(QResizeEvent *event)
206 {
207  Q_UNUSED(event);
208  m_dblVernierX = rect().right() * m_dblVernierPercentX;
209  update();
210 }
211 
213 {
214  if(event->button() == Qt::LeftButton)
215  {
216  m_dblVernierX = event->pos().x();
218  update();
219  }
220 }
221 
223 {
224  if(event->buttons() & Qt::LeftButton && !(event->buttons() & Qt::RightButton))
225  {
226  m_dblVernierX = event->pos().x();
227  if(rect().contains(event->pos()))
228  {
230  update();
231  }
232  }
233  else
234  {
235  QPainterPath path;
236  path.addEllipse(QPointF(m_dblVernierX, rect().bottom() / 2.0), 7, 7);
237 
238  if(path.contains(event->pos()))
239  {
240  QToolTip::showText(mapToGlobal(event->pos()) + QPoint(0, 5), tr("Adjust hue and brightness"), this, QRect(event->pos() - QPoint(8, 8), QSize(16, 16)));
241  }
242  }
243 }
244 
246 {
247  m_dblVernierPercentX = m_dblVernierX / rect().right();
249  m_color.setHslF(m_color.hslHueF(), m_dblSaturation, m_color.lightnessF());
251 }
252 
253 
254 
256  : MusicAbstractMoveDialog(parent),
257  m_ui(new Ui::MusicColorDialog)
258 {
259  m_ui->setupUi(this);
260  setFixedSize(size());
261  setBackgroundLabel(m_ui->background);
262 
263  m_ui->topTitleCloseButton->setIcon(QIcon(":/functions/btn_close_hover"));
264  m_ui->topTitleCloseButton->setStyleSheet(TTK::UI::ToolButtonStyle04);
265  m_ui->topTitleCloseButton->setCursor(QCursor(Qt::PointingHandCursor));
266  m_ui->topTitleCloseButton->setToolTip(tr("Close"));
267  connect(m_ui->topTitleCloseButton, SIGNAL(clicked()), SLOT(close()));
268 
269  m_ui->confirmButton->setStyleSheet(TTK::UI::PushButtonStyle04);
270  m_ui->cancelButton->setStyleSheet(TTK::UI::PushButtonStyle04);
271  m_ui->confirmButton->setCursor(QCursor(Qt::PointingHandCursor));
272  m_ui->cancelButton->setCursor(QCursor(Qt::PointingHandCursor));
273 
274 #ifdef Q_OS_UNIX
275  m_ui->confirmButton->setFocusPolicy(Qt::NoFocus);
276  m_ui->cancelButton->setFocusPolicy(Qt::NoFocus);
277 #endif
278 
279  connect(m_ui->wgtPalette, SIGNAL(colorChanged(QColor)), m_ui->wgtSaturationIndicator, SLOT(setBaseColor(QColor)));
280  connect(m_ui->wgtPalette, SIGNAL(colorChanged(QColor)), SLOT(colorChanged(QColor)));
281  connect(m_ui->wgtSaturationIndicator, SIGNAL(saturationChanged(double)), m_ui->wgtPalette, SLOT(setSaturation(double)));
282 
283  m_ui->wgtPalette->initialize();
284 
285  QButtonGroup *buttonGroup = new QButtonGroup(this);
286  buttonGroup->addButton(m_ui->topTitleCloseButton, 0);
287  buttonGroup->addButton(m_ui->confirmButton, 1);
288  buttonGroup->addButton(m_ui->cancelButton, 2);
289  QtButtonGroupConnect(buttonGroup, this, buttonClicked, TTK_SLOT);
290 }
291 
292 MusicColorDialog::MusicColorDialog(const QColor &color, QWidget *parent)
293  : MusicColorDialog(parent)
294 {
295  if(color.isValid())
296  {
297  setColor(color);
298  }
299 }
300 
302 {
303  delete m_ui;
304 }
305 
306 QColor MusicColorDialog::popup(QWidget *parent, const QColor &color)
307 {
308  MusicColorDialog dialog(color, parent);
309  return dialog.exec() ? dialog.color() : QColor();
310 }
311 
313 {
314  return m_color;
315 }
316 
317 void MusicColorDialog::setColor(const QColor &color)
318 {
319  m_color = color;
320  m_ui->wgtPalette->setColor(color);
321  m_ui->wgtSaturationIndicator->setSaturation(color.hslSaturationF());
322 }
323 
325 {
326  switch(index)
327  {
328  case 0:
329  case 2: reject(); break;
330  case 1: accept(); break;
331  default: break;
332  }
333 }
334 
335 void MusicColorDialog::colorChanged(const QColor &color)
336 {
337  m_color = color;
338 }
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:159
void setBackgroundLabel(QLabel *label)
MusicHlPalette(QWidget *parent=nullptr)
MusicHlSaturationPalette(QWidget *parent=nullptr)
virtual void mousePressEvent(QMouseEvent *event) overridefinal
QColor color() const
void saturationChanged(double dblSaturation)
void colorChanged(const QColor &color)
Ui::MusicColorDialog * m_ui
void setSaturation(double dblsaturation)
virtual void resizeEvent(QResizeEvent *event) overridefinal
voidpf void uLong size
Definition: ioapi.h:136
void setBaseColor(const QColor &color)
virtual void mousePressEvent(QMouseEvent *event) overridefinal
QColor color() const
void setColor(const QColor &color)
void setColor(const QColor &color)
void buttonClicked(int index)
virtual void mouseMoveEvent(QMouseEvent *event) overridefinal
virtual void resizeEvent(QResizeEvent *event) overridefinal
static const QString PushButtonStyle04
#define TTK_SLOT
Definition: ttkqtglobal.h:165
void colorChanged(const QColor &color)
virtual void paintEvent(QPaintEvent *event) overridefinal
static QColor popup(QWidget *parent=nullptr, const QColor &color={})
The class of the moving dialog base.
#define QtButtonGroupConnect(p, q, f, s)
Button group connect.
Definition: ttkqtcompat.h:101
QPointF m_ptfVernierPercentPos
MusicColorDialog(QWidget *parent=nullptr)
static const QString ToolButtonStyle04
The class of the get color table widget.
virtual void paintEvent(QPaintEvent *event) overridefinal
void setSaturation(double dblsaturation)
virtual void mouseMoveEvent(QMouseEvent *event) overridefinal