TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musiccutsliderwidget.cpp
Go to the documentation of this file.
1 #include "musiccutsliderwidget.h"
2 #include "musicuiobject.h"
3 
4 #include <qmath.h>
5 #include <QPainter>
6 #include <QMouseEvent>
7 
8 static constexpr int PAINT_BUTTON_WIDTH = 20;
9 static constexpr int PAINT_BUTTON_HEIGHT = 20;
10 static constexpr int PAINT_SLIDER_HEIGHT = 10;
11 static constexpr int PAINT_HANDER = 6;
12 
14  : QPushButton(parent)
15 {
16  setIcon(QIcon(":/toolSets/btn_arrow"));
17  setStyleSheet(TTK::UI::PushButtonStyle02);
18  setCursor(QCursor(Qt::PointingHandCursor));
19 #ifdef Q_OS_UNIX
20  setFocusPolicy(Qt::NoFocus);
21 #endif
22 }
23 
24 void MusicMoveButton::mousePressEvent(QMouseEvent *event)
25 {
26  if(event->button() == Qt::LeftButton)
27  {
28  m_leftButtonPress = true;
29  }
30 
31  m_pressAt = QtMouseGlobalPos(event);
32 }
33 
34 void MusicMoveButton::mouseMoveEvent(QMouseEvent *event)
35 {
37  {
38  event->ignore();
39  return;
40  }
41 
42  const int xpos = QtMouseGlobalX(event) - m_pressAt.x();
43  m_pressAt = QtMouseGlobalPos(event);
44 
45  move(x() + xpos, y());
46  Q_EMIT moveChanged();
47 }
48 
49 void MusicMoveButton::mouseReleaseEvent(QMouseEvent *event)
50 {
51  m_pressAt = QtMouseGlobalPos(event);
52  m_leftButtonPress = false;
53  Q_EMIT buttonRelease();
54 }
55 
56 
58  : QWidget(parent),
59  m_duration(0),
60  m_position(0)
61 {
62  m_leftControl = new MusicMoveButton(this);
63  m_rightControl = new MusicMoveButton(this);
66 
67  connect(m_leftControl, SIGNAL(moveChanged()), SLOT(buttonMoveUpdate()));
68  connect(m_rightControl, SIGNAL(moveChanged()), SLOT(buttonMoveUpdate()));
69  connect(m_leftControl, SIGNAL(buttonRelease()), SLOT(buttonReleaseLeft()));
70  connect(m_rightControl, SIGNAL(buttonRelease()), SLOT(buttonReleaseRight()));
71 
72  resizeGeometry(width(), height());
73 }
74 
76 {
77  delete m_leftControl;
78  delete m_rightControl;
79 }
80 
81 void MusicCutSliderWidget::setPosition(qint64 position)
82 {
83  if(m_duration <= 0)
84  {
85  m_duration = 1;
86  }
87  m_position = m_width*position/m_duration;
88  update();
89 }
90 
91 void MusicCutSliderWidget::setDuration(qint64 duration)
92 {
93  m_duration = duration;
94 }
95 
96 void MusicCutSliderWidget::resizeGeometry(int width, int height)
97 {
98  m_width = width;
99  m_height = height;
100 
101  if(height < 30)
102  {
103  return;
104  }
105 
106  int lineStartHeight = (m_height - (PAINT_SLIDER_HEIGHT + PAINT_BUTTON_WIDTH)) / 2;
107  m_leftControl->move(-PAINT_BUTTON_WIDTH / 2, lineStartHeight + PAINT_SLIDER_HEIGHT);
108  m_rightControl->move(-PAINT_BUTTON_WIDTH / 2, lineStartHeight + PAINT_SLIDER_HEIGHT);
109 }
110 
112 {
113  int leftX = m_leftControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
114  int rightX = m_rightControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
115 
116  if(leftX < 0)
117  {
118  m_leftControl->move(-PAINT_BUTTON_WIDTH / 2, m_leftControl->geometry().y());
119  return;
120  }
121  else if(rightX < 0)
122  {
123  m_rightControl->move(-PAINT_BUTTON_WIDTH / 2, m_rightControl->geometry().y());
124  return;
125  }
126  else if(leftX > m_width)
127  {
128  m_leftControl->move(m_width - PAINT_BUTTON_WIDTH / 2, m_leftControl->geometry().y());
129  return;
130  }
131  else if(rightX > m_width)
132  {
133  m_rightControl->move(m_width - PAINT_BUTTON_WIDTH / 2, m_leftControl->geometry().y());
134  return;
135  }
136 
137  leftX = leftX*m_duration/m_width;
138  rightX = rightX*m_duration/m_width;
139 
140  if(leftX < rightX)
141  {
142  Q_EMIT posChanged(leftX, rightX);
143  }
144  else
145  {
146  Q_EMIT posChanged(rightX, leftX);
147  }
148  update();
149 }
150 
152 {
153  int leftX = m_leftControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
154  leftX = leftX*m_duration/m_width;
155  Q_EMIT buttonReleaseChanged(leftX);
156 }
157 
159 {
160  int rightX = m_rightControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
161  rightX = rightX*m_duration/m_width;
162  Q_EMIT buttonReleaseChanged(rightX);
163 }
164 
165 void MusicCutSliderWidget::paintEvent(QPaintEvent *event)
166 {
167  QWidget::paintEvent(event);
168  QPainter painter(this);
169 
170  const int lineStartHeight = (m_height - (PAINT_SLIDER_HEIGHT + PAINT_BUTTON_WIDTH)) / 2;
171  painter.fillRect(0, lineStartHeight, m_width, PAINT_SLIDER_HEIGHT, QColor(220, 220, 220));
172  painter.fillRect(0, lineStartHeight, m_position, PAINT_SLIDER_HEIGHT, QColor(150, 150, 150));
173 
174  const int leftX = m_leftControl->geometry().x();
175  const int rightX = m_rightControl->geometry().x();
176  painter.fillRect(leftX < rightX ? leftX + PAINT_BUTTON_WIDTH / 2 : rightX + PAINT_BUTTON_WIDTH / 2, lineStartHeight, abs(leftX -rightX), PAINT_SLIDER_HEIGHT, QColor(TTK::UI::Color01));
177  painter.fillRect(m_position - PAINT_HANDER / 2, lineStartHeight + (PAINT_SLIDER_HEIGHT - PAINT_HANDER) / 2, PAINT_HANDER, PAINT_HANDER, QColor(0, 0, 0));
178 
179 }
180 
182 {
183  Q_UNUSED(event);
184 }
185 
186 void MusicCutSliderWidget::mouseMoveEvent(QMouseEvent *event)
187 {
188  Q_UNUSED(event);
189 }
190 
192 {
193  Q_UNUSED(event);
194 }
void setPosition(qint64 position)
virtual void mouseReleaseEvent(QMouseEvent *event) overridefinal
#define QtMouseGlobalX(p)
Definition: ttkqtcompat.h:146
MusicMoveButton * m_leftControl
static constexpr int PAINT_SLIDER_HEIGHT
void buttonReleaseChanged(qint64 pos)
void buttonRelease()
virtual void mousePressEvent(QMouseEvent *event) overridefinal
static const QString PushButtonStyle02
#define QtMouseGlobalPos(p)
Definition: ttkqtcompat.h:148
MusicMoveButton * m_rightControl
The class of the move button.
void setDuration(qint64 duration)
void posChanged(qint64 start, qint64 end)
MusicCutSliderWidget(QWidget *parent=nullptr)
MusicMoveButton(QWidget *parent=nullptr)
virtual void mousePressEvent(QMouseEvent *event) overridefinal
static constexpr int PAINT_BUTTON_HEIGHT
static constexpr unsigned int Color01
Color QRgb.
Definition: musicuiobject.h:32
static constexpr int PAINT_BUTTON_WIDTH
static constexpr int PAINT_HANDER
virtual void mouseReleaseEvent(QMouseEvent *event) overridefinal
virtual void paintEvent(QPaintEvent *event) overridefinal
void resizeGeometry(int width, int height)
virtual void mouseMoveEvent(QMouseEvent *event) overridefinal
virtual void mouseMoveEvent(QMouseEvent *event) overridefinal