TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkabstractmoveresizewidget.cpp
Go to the documentation of this file.
2 #include "ttklibrary.h"
3 
4 #include <QApplication>
5 
6 static constexpr int DISTANCE = 5;
7 
8 #if !TTK_QT_VERSION_CHECK(5,0,0) && defined(Q_OS_UNIX)
9 # define GEOMETRY(x, y, w, h) move(x, y); resize(w, h);
10 #else
11 # define GEOMETRY(x, y, w, h) setGeometry(x, y, w, h);
12 #endif
13 
15  : TTKAbstractMoveResizeWidget(true, parent)
16 {
17 
18 }
19 
21  : QWidget(parent),
22  m_direction(TTK::Direction::No)
23 {
24  m_struct.m_mouseLeftPress = false;
25  m_struct.m_isPressBorder = false;
26 
27  setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
28  setAttribute(Qt::WA_TranslucentBackground, transparent);
29  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
30  setMouseTracking(true);
31 }
32 
33 bool TTKAbstractMoveResizeWidget::eventFilter(QObject *object, QEvent *event)
34 {
35  QWidget::eventFilter(object, event);
36  if(QEvent::MouseMove == event->type())
37  {
38  QMouseEvent *mouseEvent = TTKStaticCast(QMouseEvent*, event);
39  QApplication::sendEvent(this, mouseEvent);
40  }
41  return false;
42 }
43 
45 {
46  QWidget::paintEvent(event);
48  {
49  return;
50  }
51 
52  const QPoint &point = mapFromGlobal(QCursor::pos());
53  if(point.y() > DISTANCE && point.y() < height() - DISTANCE && point.x() > DISTANCE && point.x() < width() - DISTANCE)
54  {
55  setCursor(Qt::ArrowCursor);
57  }
58 }
59 
61 {
62  QWidget::mousePressEvent(event);
63 
65  m_struct.m_isPressBorder = false;
66  setFocus();
67 
68  if(event->button() == Qt::LeftButton)
69  {
70  m_struct.m_windowPos = pos();
71  if(QRect(DISTANCE + 1, DISTANCE + 1, width() - (DISTANCE + 1) * 2, height() - (DISTANCE + 1) * 2).contains(event->pos()))
72  {
75  }
76  else
77  {
79  }
80  }
81 }
82 
84 {
85  QWidget::mouseMoveEvent(event);
87 
89  {
91  }
92 }
93 
95 {
96  QWidget::mouseReleaseEvent(event);
97  m_struct.m_isPressBorder = false;
98  m_struct.m_mouseLeftPress = false;
99  setCursor(QCursor(Qt::ArrowCursor));
101 }
102 
104 {
105  const QPoint &point = mapFromGlobal(QCursor::pos());
106  if(point.x() < DISTANCE && point.y() < height() - DISTANCE && point.y() > DISTANCE)
107  {
108  setCursor(Qt::SizeHorCursor);
110  }
111  else if(point.x() > width() - DISTANCE && point.y() < height() - DISTANCE && point.y() > DISTANCE)
112  {
113  setCursor(Qt::SizeHorCursor);
115  }
116  else if(point.y() < DISTANCE && point.x() > DISTANCE && point.x() < width() - DISTANCE)
117  {
118  setCursor(Qt::SizeVerCursor);
120  }
121  else if(point.y() > height() - DISTANCE && point.x() > DISTANCE && point.x() < width() - DISTANCE)
122  {
123  setCursor(Qt::SizeVerCursor);
125  }
126  else if(point.y() < DISTANCE && point.x() < DISTANCE)
127  {
128  setCursor(Qt::SizeFDiagCursor);
130  }
131  else if(point.y() < DISTANCE && point.x() > width() - DISTANCE)
132  {
133  setCursor(Qt::SizeBDiagCursor);
135  }
136  else if(point.x() < DISTANCE && point.y() > height() - DISTANCE)
137  {
138  setCursor(Qt::SizeBDiagCursor);
140  }
141  else if(point.x() > DISTANCE && point.y() > height() - DISTANCE)
142  {
143  setCursor(Qt::SizeFDiagCursor);
145  }
146  else
147  {
148  setCursor(Qt::ArrowCursor);
150  }
151 }
152 
154 {
155  const QPoint &point = QCursor::pos();
156  switch(m_direction)
157  {
159  {
160  const int wValue = x() + width() - point.x();
161  if(minimumWidth() <= wValue && wValue <= maximumWidth())
162  {
163  GEOMETRY(point.x(), y(), wValue, height());
164  }
165  break;
166  }
168  {
169  const int wValue = point.x() - x();
170  if(minimumWidth() <= wValue && wValue <= maximumWidth())
171  {
172  GEOMETRY(x(), y(), wValue, height());
173  }
174  break;
175  }
176  case TTK::Direction::Top:
177  {
178  const int hValue = y() - point.y() + height();
179  if(minimumHeight() <= hValue && hValue <= maximumHeight())
180  {
181  GEOMETRY(x(), point.y(), width(), hValue);
182  }
183  break;
184  }
186  {
187  const int hValue = point.y() - y();
188  if(minimumHeight() <= hValue && hValue <= maximumHeight())
189  {
190  GEOMETRY(x(), y(), width(), hValue);
191  }
192  break;
193  }
195  {
196  int yValue = point.y();
197  int xValue = point.x();
198 
199  int wValue = pos().x() + width() - xValue;
200  int hValue = pos().y() + height() - yValue;
201 
202  const int twValue = m_struct.m_windowPos.x() + m_struct.m_pressedSize.width();
203  const int thValue = m_struct.m_windowPos.y() + m_struct.m_pressedSize.height();
204 
205  if(twValue - xValue >= maximumWidth())
206  {
207  xValue = twValue - maximumWidth();
208  wValue = maximumWidth();
209  }
210 
211  if(twValue - xValue <= minimumWidth())
212  {
213  xValue = twValue - minimumWidth();
214  wValue = minimumWidth();
215  }
216 
217  if(thValue - yValue >= maximumHeight())
218  {
219  yValue = thValue - maximumHeight();
220  hValue = maximumHeight();
221  }
222 
223  if(thValue - yValue <= minimumHeight())
224  {
225  yValue = thValue - minimumHeight();
226  hValue = minimumHeight();
227  }
228 
229  GEOMETRY(xValue, yValue, wValue, hValue);
230  break;
231  }
233  {
234  int hValue = y() + height() - point.y();
235  const int wValue = point.x() - x();
236  int yValue = point.y();
237 
238  if(hValue >= maximumHeight())
239  {
240  yValue = m_struct.m_windowPos.y() + m_struct.m_pressedSize.height() - height();
241  hValue = maximumHeight();
242  }
243 
244  if(hValue <= minimumHeight())
245  {
246  yValue = m_struct.m_windowPos.y() + m_struct.m_pressedSize.height() - height();
247  hValue = minimumHeight();
248  }
249 
250  GEOMETRY(m_struct.m_windowPos.x(), yValue, wValue, hValue);
251  break;
252  }
254  {
255  int wValue = x() + width() - point.x();
256  const int hValue = point.y() - m_struct.m_windowPos.y();
257  int xValue = point.x();
258  const int twValue = m_struct.m_windowPos.x() + m_struct.m_pressedSize.width();
259 
260  if(twValue - xValue >= maximumWidth())
261  {
262  xValue = twValue - maximumWidth();
263  wValue = maximumWidth();
264  }
265 
266  if(twValue - xValue <= minimumWidth())
267  {
268  xValue = twValue - minimumWidth();
269  wValue = minimumWidth();
270  }
271 
272  GEOMETRY(xValue, m_struct.m_windowPos.y(), wValue, hValue);
273  break;
274  }
276  {
277  const int wValue = point.x() - x();
278  const int hValue = point.y() - y();
279  GEOMETRY(m_struct.m_windowPos.x(), m_struct.m_windowPos.y(), wValue, hValue);
280  break;
281  }
282  default: break;
283  }
284 }
285 
286 void TTKAbstractMoveResizeWidget::setObjectsTracking(const QWidgetList &objects)
287 {
288  for(QWidget *object : objects)
289  {
290  object->installEventFilter(this);
291  object->setMouseTracking(true);
292  }
293 }
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:159
TTKAbstractMoveResizeWidget(QWidget *parent=nullptr)
void setObjectsTracking(const QWidgetList &objects)
The class of the ttk moving resize widget base.
voidpf void uLong size
Definition: ioapi.h:136
virtual void mouseMoveEvent(QMouseEvent *event) override
Direction
Definition: ttklibrary.h:27
virtual bool eventFilter(QObject *object, QEvent *event) override
#define GEOMETRY(x, y, w, h)
static constexpr int DISTANCE
#define QtMouseGlobalPos(p)
Definition: ttkqtcompat.h:148
The namespace of the process utils.
Definition: ttkcompat.h:24
virtual void paintEvent(QPaintEvent *event) override
virtual void mouseReleaseEvent(QMouseEvent *event) override
virtual void mousePressEvent(QMouseEvent *event) override