TTKMusicPlayer  4.3.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 <QPainter>
5 #include <QMouseEvent>
6 
7 static constexpr int PAINT_BUTTON_WIDTH = 20;
8 static constexpr int PAINT_BUTTON_HEIGHT = 20;
9 static constexpr int PAINT_SLIDER_HEIGHT = 10;
10 static constexpr int PAINT_HANDER = 6;
11 
13  : QPushButton(parent)
14 {
15  setIcon(QIcon(":/toolSets/btn_arrow"));
16  setStyleSheet(TTK::UI::PushButtonStyle02);
17  setCursor(QCursor(Qt::PointingHandCursor));
18 #ifdef Q_OS_UNIX
19  setFocusPolicy(Qt::NoFocus);
20 #endif
21 }
22 
23 void MusicMoveButton::mousePressEvent(QMouseEvent *event)
24 {
25  if(event->button() == Qt::LeftButton)
26  {
27  m_mouseLeftPressed = true;
29  }
30 }
31 
32 void MusicMoveButton::mouseMoveEvent(QMouseEvent *event)
33 {
35  {
36  event->ignore();
37  return;
38  }
39 
40  const int xpos = QtGlobalPositionX(event) - m_pressedPos.x();
42 
43  move(x() + xpos, y());
44  Q_EMIT moveChanged();
45 }
46 
47 void MusicMoveButton::mouseReleaseEvent(QMouseEvent *event)
48 {
50  m_mouseLeftPressed = false;
51  Q_EMIT buttonRelease();
52 }
53 
54 
56  : QWidget(parent),
57  m_duration(0),
58  m_position(0)
59 {
60  m_leftControl = new MusicMoveButton(this);
61  m_rightControl = new MusicMoveButton(this);
64 
65  connect(m_leftControl, SIGNAL(moveChanged()), SLOT(buttonMoveUpdate()));
66  connect(m_rightControl, SIGNAL(moveChanged()), SLOT(buttonMoveUpdate()));
67  connect(m_leftControl, SIGNAL(buttonRelease()), SLOT(buttonReleaseLeft()));
68  connect(m_rightControl, SIGNAL(buttonRelease()), SLOT(buttonReleaseRight()));
69 
70  resizeGeometry(width(), height());
71 }
72 
74 {
75  delete m_leftControl;
76  delete m_rightControl;
77 }
78 
79 void MusicCutSliderWidget::setPosition(qint64 position)
80 {
81  if(m_duration <= 0)
82  {
83  m_duration = 1;
84  }
85  m_position = m_width*position/m_duration;
86  update();
87 }
88 
89 void MusicCutSliderWidget::setDuration(qint64 duration)
90 {
91  m_duration = duration;
92 }
93 
94 void MusicCutSliderWidget::resizeGeometry(int width, int height)
95 {
96  m_width = width;
97  m_height = height;
98 
99  if(height < 30)
100  {
101  return;
102  }
103 
104  int lineStartHeight = (m_height - (PAINT_SLIDER_HEIGHT + PAINT_BUTTON_WIDTH)) / 2;
105  m_leftControl->move(-PAINT_BUTTON_WIDTH / 2, lineStartHeight + PAINT_SLIDER_HEIGHT);
106  m_rightControl->move(-PAINT_BUTTON_WIDTH / 2, lineStartHeight + PAINT_SLIDER_HEIGHT);
107 }
108 
110 {
111  int leftX = m_leftControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
112  int rightX = m_rightControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
113 
114  if(leftX < 0)
115  {
116  m_leftControl->move(-PAINT_BUTTON_WIDTH / 2, m_leftControl->geometry().y());
117  return;
118  }
119  else if(rightX < 0)
120  {
121  m_rightControl->move(-PAINT_BUTTON_WIDTH / 2, m_rightControl->geometry().y());
122  return;
123  }
124  else if(leftX > m_width)
125  {
126  m_leftControl->move(m_width - PAINT_BUTTON_WIDTH / 2, m_leftControl->geometry().y());
127  return;
128  }
129  else if(rightX > m_width)
130  {
131  m_rightControl->move(m_width - PAINT_BUTTON_WIDTH / 2, m_leftControl->geometry().y());
132  return;
133  }
134 
135  leftX = leftX*m_duration/m_width;
136  rightX = rightX*m_duration/m_width;
137 
138  if(leftX < rightX)
139  {
140  Q_EMIT posChanged(leftX, rightX);
141  }
142  else
143  {
144  Q_EMIT posChanged(rightX, leftX);
145  }
146  update();
147 }
148 
150 {
151  int leftX = m_leftControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
152  leftX = leftX*m_duration/m_width;
153  Q_EMIT buttonReleaseChanged(leftX);
154 }
155 
157 {
158  int rightX = m_rightControl->geometry().x() + PAINT_BUTTON_WIDTH / 2;
159  rightX = rightX*m_duration/m_width;
160  Q_EMIT buttonReleaseChanged(rightX);
161 }
162 
163 void MusicCutSliderWidget::paintEvent(QPaintEvent *event)
164 {
165  QWidget::paintEvent(event);
166  QPainter painter(this);
167  painter.setRenderHint(QPainter::Antialiasing);
168 
169  const int lineStartHeight = (m_height - (PAINT_SLIDER_HEIGHT + PAINT_BUTTON_WIDTH)) / 2;
170  painter.fillRect(0, lineStartHeight, m_width, PAINT_SLIDER_HEIGHT, QColor(220, 220, 220));
171  painter.fillRect(0, lineStartHeight, m_position, PAINT_SLIDER_HEIGHT, QColor(150, 150, 150));
172 
173  const int leftX = m_leftControl->geometry().x();
174  const int rightX = m_rightControl->geometry().x();
175  painter.fillRect(leftX < rightX ? leftX + PAINT_BUTTON_WIDTH / 2 : rightX + PAINT_BUTTON_WIDTH / 2, lineStartHeight, std::abs(leftX -rightX), PAINT_SLIDER_HEIGHT, QColor(TTK::UI::Color01));
176  painter.fillRect(m_position - PAINT_HANDER / 2, lineStartHeight + (PAINT_SLIDER_HEIGHT - PAINT_HANDER) / 2, PAINT_HANDER, PAINT_HANDER, QColor(0, 0, 0));
177 }
178 
180 {
181  Q_UNUSED(event);
182 }
183 
184 void MusicCutSliderWidget::mouseMoveEvent(QMouseEvent *event)
185 {
186  Q_UNUSED(event);
187 }
188 
190 {
191  Q_UNUSED(event);
192 }
void setPosition(qint64 position)
virtual void mouseReleaseEvent(QMouseEvent *event) overridefinal
#define QtGlobalPosition(p)
Definition: ttkqtcompat.h:161
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
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
#define QtGlobalPositionX(p)
Definition: ttkqtcompat.h:163
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