/
mainframe.cpp
195 lines (176 loc) · 5.03 KB
/
mainframe.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "mainframe.h"
#include "ui_mainframe.h"
MainFrame::MainFrame(QWidget *parent) :
QFrame(parent),
ui(new Ui::MainFrame)
{
ui->setupUi(this);
initWindow();
setWindowCircle();
connectEvent();
}
void MainFrame::initWindow()
{
this->setWindowFlags(Qt::FramelessWindowHint);
m_pContentWidget = new ContentWidget(this);
m_pStatusBar = new StatusBar(this);
m_pTitleBar = new TitleBar(this);
m_pToolBar = new ToolBar(this);
m_pMainLayout = new QVBoxLayout(this);
m_pMainLayout->addWidget(m_pTitleBar);
m_pMainLayout->addWidget(m_pToolBar);
m_pMainLayout->addWidget(m_pContentWidget);
m_pMainLayout->addWidget(m_pStatusBar);
m_pMainLayout->setSpacing(0);
m_pMainLayout->setContentsMargins(0,0,0,0);
this->setStyleSheet("QFrame#MainFrame {border-image:url(:/res/images/frame.jpg); border:1px solid black;}");
this->setMouseTracking(true);
m_bLeftBtnPress = false;
setMinimumWidth(850);
setMinimumHeight(600);
m_bMaxWin = false;
this->m_pTitleBar->setMouseTracking(true);
this->m_pContentWidget->setMouseTracking(true);
m_bLeftBtnPress = false;
}
void MainFrame::setWindowCircle()
{
//生成一张位图
QBitmap objBitmap(size());
//QPainter用于在位图上绘画
QPainter painter(&objBitmap);
//填充位图矩形框(用白色填充)
painter.fillRect(rect(),Qt::white);
painter.setBrush(QColor(0,0,0));
//在位图上画圆角矩形(用黑色填充)
painter.drawRoundedRect(this->rect(),10,10);
//使用setmask过滤即可
setMask(objBitmap);
}
MainFrame::~MainFrame()
{
delete ui;
}
void MainFrame::connectEvent()
{
connect(this->m_pTitleBar, SIGNAL(signal_close()), this, SLOT(close()));
}
void MainFrame::paintEvent(QPaintEvent *event)
{
setWindowCircle();
}
void MainFrame::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_ptPressGlobal = event->globalPos();
m_bLeftBtnPress = true;
}
}
void MainFrame::mouseMoveEvent(QMouseEvent *event)
{
if(!m_bLeftBtnPress)
{
m_eDirection = PointValid(event->x(),event->y());
SetCursorStyle(m_eDirection);
}
else
{
int nXGlobal = event->globalX();
int nYGlobal = event->globalY();
SetDrayMove(nXGlobal,nYGlobal,m_eDirection);
m_ptPressGlobal =QPoint(nXGlobal,nYGlobal);
}
}
void MainFrame::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_bLeftBtnPress = false;
m_eDirection = eNone;
}
}
void MainFrame::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && event->y() <= m_pTitleBar->height())
{
if(!m_bMaxWin)
{
m_rectRestoreWindow = geometry();
setGeometry(qApp->desktop()->availableGeometry());
}
else
{
setGeometry(m_rectRestoreWindow);
}
m_bMaxWin = !m_bMaxWin;
}
}
//确定点的位置方向
MainFrame::enum_Direction MainFrame::PointValid(int nXRelative,int nYRelative)
{
int nTop = qAbs(nYRelative)<VALUE_DIS ?eTop:eNone;
int nRight = qAbs(nXRelative-rect().right())<VALUE_DIS ? eRight:eNone;
int nBottom = qAbs(nYRelative-rect().bottom())<VALUE_DIS ? eBottom:eNone;
int nLeft = qAbs(nXRelative)<VALUE_DIS ? eLeft:eNone;
return enum_Direction(nTop + nRight + nBottom + nLeft);
}
//设置鼠标样式
void MainFrame::SetCursorStyle(enum_Direction direction)
{
//设置上下左右以及右上、右下、左上、坐下的鼠标形状
switch(direction)
{
case eTop:
case eBottom:
setCursor(Qt::SizeVerCursor);
break;
case eRight:
case eLeft:
setCursor(Qt::SizeHorCursor);
break;
case eTopRight:
case eBottomLeft:
setCursor(Qt::SizeBDiagCursor);
break;
case eRightBottom:
case eLeftTop:
setCursor(Qt::SizeFDiagCursor);
break;
default:
setCursor(Qt::ArrowCursor);
break;
}
}
//设置鼠标拖动的窗口位置信息
void MainFrame::SetDrayMove(int nXGlobal,int nYGlobal,enum_Direction direction)
{
//计算偏差
int ndX = nXGlobal - m_ptPressGlobal.x();
int ndY = nYGlobal - m_ptPressGlobal.y();
//获得主窗口位置信息
QRect rectWindow = geometry();
//判别方向
if(direction & eTop)
{
rectWindow.setTop(rectWindow.top()+ndY);
}
if(direction & eRight)
{
rectWindow.setRight(rectWindow.right()+ndX);
}
if(direction & eBottom)
{
rectWindow.setBottom(rectWindow.bottom()+ndY);
}
if(direction & eLeft)
{
rectWindow.setLeft(rectWindow.left()+ndX);
}
if(rectWindow.width()< minimumWidth() || rectWindow.height()<minimumHeight())
{
return;
}
//重新设置窗口位置为新位置信息
setGeometry(rectWindow);
}