This repository has been archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqdragablewidget.cpp
126 lines (114 loc) · 3.17 KB
/
qdragablewidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "qdragablewidget.h"
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QStyleOption>
QDragableWidget::QDragableWidget(QWidget *parent) : QWidget(parent)
{
// this->setAttribute(Qt::WA_Hover, true);
m_bIsDrag = false;
m_bResize = false;
setActionCtl(true, true);
m_imgResize.load(u8":/resize.png");
m_pImgShow = new QLabel(this);
m_pImgShow->setGeometry(0,0,16,16);
m_pImgShow->setPixmap(m_imgResize);
m_pImgShow->raise();
m_pContent = NULL;
}
void QDragableWidget::setContent(QWidget *pContent)
{
m_pContent = pContent;
if( m_pContent ){
m_pContent->setParent(this);
m_pContent->setWindowFlag(Qt::Window, false);
m_pContent->move(0,0);
m_pContent->show();
m_pContent->lower();
// resize
m_pContent->setGeometry(this->rect());
}
}
void QDragableWidget::setActionCtl(bool bDragMove, bool bResize)
{
m_bEnableDrag = bDragMove;
m_bEnableResize = bResize;
}
void QDragableWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QStyleOption o;
o.initFrom(this);
QPainter p(this);
this->style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
if( m_bEnableResize ){
// draw resize pic
QRect rc = this->rect();
m_pImgShow->move(rc.bottomRight()-QPoint(17,17));
}
}
void QDragableWidget::mousePressEvent(QMouseEvent *event)
{
if( event->button() == Qt::LeftButton ){
m_pStart = event->globalPos();
m_rcStart = this->rect();
QPoint ptLT = mapToParent(QPoint(0,0));
m_rcStart.moveTo(ptLT);
QPoint curPos = event->pos();
if( (m_rcStart.width() - curPos.x()) < 16 && ( m_rcStart.height() - curPos.y() )<16 ){
if( m_bEnableResize ){
m_bResize = true;
setCursor(Qt::SizeFDiagCursor);
}
}else{
if( m_bEnableDrag ){
m_bIsDrag = true;
setCursor(Qt::SizeAllCursor);
}
}
}
}
void QDragableWidget::mouseReleaseEvent(QMouseEvent *event)
{
if( event->button() == Qt::LeftButton ){
m_bIsDrag = false;
m_bResize = false;
setCursor(Qt::ArrowCursor);
}
}
void QDragableWidget::mouseMoveEvent(QMouseEvent *event)
{
if( m_bIsDrag && m_bEnableDrag){
QPoint distance = event->globalPos() - m_pStart;
QPoint pt = m_rcStart.topLeft()+distance;
// limit min x,y
if( pt.x() < 0 ){
pt.setX(0);
}
if( pt.y() < 0 ){
pt.setY(0);
}
this->move(pt);
}
if( m_bResize && m_bEnableResize ){
QPoint distance = event->globalPos() - m_pStart;
QRect rc = m_rcStart.adjusted(0,0,distance.x(),distance.y());
// limit min width and height
if( rc.width() < 32 ){
rc.setWidth(32);
}
if( rc.height() < 32 ){
rc.setHeight(32);
}
this->setGeometry(rc);
}
}
void QDragableWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
// resize content size
if( m_pContent ){
QRect rc = this->rect();
m_pContent->setGeometry(rc);
}
}