-
Notifications
You must be signed in to change notification settings - Fork 4
/
titlebar.cpp
279 lines (264 loc) · 8.53 KB
/
titlebar.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <QtGui>
#include "titlebar.h"
#include "mainframe.h"
const int TITLE_H=32;
TitleBar::TitleBar(QWidget *parent) :
QWidget(parent)
{
this->setFixedHeight(32);
CreateWidget();
CreateLayout();
SetWidgetStyle();
m_bLeftButtonPressed = false;
}
void TitleBar::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
if(event->y()<VALUE_DIS || event->x()<VALUE_DIS || rect().width() - event->x() < VALUE_DIS)
{
event->ignore();
return;
}
m_ptPress = event->globalPos();
m_bLeftButtonPressed = true;
}
event->ignore();
}
void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
if(m_bLeftButtonPressed)
{
m_ptMove = event->globalPos();
MainFrame *pMainFrame = (qobject_cast<MainFrame*>(parent()));
pMainFrame->move(pMainFrame->pos() + m_ptMove - m_ptPress);
m_ptPress = m_ptMove;
}
event->ignore();
}
void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_bLeftButtonPressed = false;
}
event->ignore();
}
//创建子部件
void TitleBar::CreateWidget()
{
//图像标签--logo
m_pLabelIcon = new QLabel(this);
QPixmap objPixmap(":/res/images/360AboutLogo.png");
m_pLabelIcon->setPixmap(objPixmap.scaled(TITLE_H,TITLE_H));
//文本标签--标题
m_pLabelTitle = new QLabel(this);
m_pLabelTitle->setText(QString("360 Safe Guard V8.5"));
//文本标签--样式版本
m_pLabelVersion = new QLabel(this);
m_pLabelVersion->setText(QString("Use Class Style"));
//设置鼠标形状
m_pLabelVersion->setCursor(Qt::PointingHandCursor);
//按钮--更换皮肤
m_pBtnSkin = new QToolButton(this);
//设置初始图片
SetBtnIcon(m_pBtnSkin,eBtnStateDefault,true);
//按钮--菜单
m_pBtnMenu = new QToolButton(this);
SetBtnIcon(m_pBtnMenu,eBtnStateDefault,true);
//按钮--最小化
m_pBtnMin = new QToolButton(this);
SetBtnIcon(m_pBtnMin,eBtnStateDefault,true);
//按钮--最大化/还原
m_pBtnMax = new QToolButton(this);
SetBtnIcon(m_pBtnMax,eBtnStateDefault,true);
//按钮--关闭
m_pBtnClose = new QToolButton(this);
SetBtnIcon(m_pBtnClose,eBtnStateDefault,true);
//获得子部件
const QObjectList &objList = children();
for(int nIndex=0; nIndex < objList.count(); ++nIndex)
{
//设置子部件的MouseTracking属性
((QWidget*)(objList.at(nIndex)))->setMouseTracking(true);
//如果是QToolButton部件
if(0==qstrcmp(objList.at(nIndex)->metaObject()->className(),"QToolButton"))
{
//连接pressed信号为slot_btnpress
connect(((QToolButton*)(objList.at(nIndex))),SIGNAL(pressed()),this,SLOT(slot_btnpress()));
//连接clicked信号为slot_btnclick
connect(((QToolButton*)(objList.at(nIndex))),SIGNAL(clicked()),this,SLOT(slot_btnclick()));
// connect(((QToolButton*)(objList.at(nIndex))),SIGNAL(enterEvent()),this,SLOT(slot_btnclick()));
//设置顶部间距
((QToolButton*)(objList.at(nIndex)))->setContentsMargins(0,VALUE_DIS,0,0);
}
}
}
//设置子部件样式(qss)
void TitleBar::SetWidgetStyle()
{
//设置标签的文本颜色,大小等以及按钮的边框
setStyleSheet("QLabel{color:#CCCCCC;font-size:12px;font-weight:bold;border:0px;} QToolButton{border:0px;}");
//设置左边距
m_pLabelTitle->setStyleSheet("margin-left:6px;");
//设置右边距以及鼠标移上去时的文本颜色
m_pLabelVersion->setStyleSheet("QLabel{margin-right:10px;}QLabel:hover{color:#00AA00;}");
}
//创建设置布局
void TitleBar::CreateLayout()
{
//水平布局
m_pLayout = new QHBoxLayout(this);
//添加部件
m_pLayout->addWidget(m_pLabelIcon);
m_pLayout->addWidget(m_pLabelTitle);
//添加伸缩项
m_pLayout->addStretch(1);
//添加部件
m_pLayout->addWidget(m_pLabelVersion);
m_pLayout->addWidget(m_pBtnSkin);
m_pLayout->addWidget(m_pBtnMenu);
m_pLayout->addWidget(m_pBtnMin);
m_pLayout->addWidget(m_pBtnMax);
m_pLayout->addWidget(m_pBtnClose);
//设置Margin
m_pLayout->setContentsMargins(0,0,VALUE_DIS,0);
//设置部件之间的space
m_pLayout->setSpacing(0);
setLayout(m_pLayout);
}
//设置按钮不同状态下的图标
void TitleBar::SetBtnIcon(QToolButton *pBtn,eBtnMoustState state,bool bInit/*=false*/)
{
//获得图片路径
QString strImagePath = GetBtnImagePath(pBtn,bInit);
//创建QPixmap对象
QPixmap objPixmap(strImagePath);
//得到图像宽和高
int nPixWidth = objPixmap.width();
int nPixHeight = objPixmap.height();
//如果状态不是无效值
if(state!=eBtnStateNone)
{
if(pBtn == m_pBtnSkin)
{
pBtn->setIcon(objPixmap.copy((nPixWidth/3)*(state-1),0,nPixWidth/3,nPixHeight));
//设置按钮图片大小
pBtn->setIconSize(QSize(nPixWidth/3,nPixHeight));
}else
{
/*设置按钮图片
按钮的图片是连续在一起的,如前1/4部分表示默认状态下的图片部分,接后的1/4部分表示鼠标移到按钮状态下的图片部分
*/
pBtn->setIcon(objPixmap.copy((nPixWidth/4)*(state-1),0,nPixWidth/4,nPixHeight));
//设置按钮图片大小
pBtn->setIconSize(QSize(nPixWidth/4,nPixHeight));
}
}
}
//获得图片路径(固定值)
const QString TitleBar::GetBtnImagePath(QToolButton *pBtn,bool bInit/*=false*/)
{
QString strImagePath;
//皮肤按钮
if(m_pBtnSkin==pBtn)
{
strImagePath = ":/res/images/SkinButtom.png";
}
//菜单按钮
if(m_pBtnMenu==pBtn)
{
strImagePath = ":/res/images/title_bar_menu.png";
}
//最小化
if(m_pBtnMin==pBtn)
{
strImagePath = ":/res/images/sys_button_min.png";
}
//最大化/还原按钮,所以包括最大化和还原两张图片
if(m_pBtnMax==pBtn)
{
//如果是初始设置或者主界面的最大化标志不为真(其中MainWindow::Instance()使用单例设计模式)
// if(bInit==true || MainFrame::Instance()->GetMaxWin()==false)
if(bInit == true)
{
//最大化按钮图片路径
strImagePath = ":/res/images/sys_button_max.png";
}
else
{
//还原按钮图片路径
strImagePath = ":/res/images/sys_button_restore.png";
}
}
//关闭按钮
if(m_pBtnClose==pBtn)
{
strImagePath = ":/res/images/sys_button_close.png";
}
return strImagePath;
}
//创建事件过滤器
void TitleBar::CreateEventFiter()
{
m_pBtnSkin->installEventFilter(this);
m_pBtnMenu->installEventFilter(this);
m_pBtnMin->installEventFilter(this);
m_pBtnMax->installEventFilter(this);
m_pBtnClose->installEventFilter(this);
}
//事件过滤
bool TitleBar::eventFilter(QObject *obj, QEvent *event)
{
//按钮状态
eBtnMoustState eState = eBtnStateNone;
//判断事件类型--QEvent::Enter
if (event->type() == QEvent::Enter)
{
eState = eBtnStateHover;
}
//判断事件类型--QEvent::Leave
if (event->type() == QEvent::Leave)
{
eState = eBtnStateDefault;
}
//判断事件类型--QEvent::MouseButtonPress
if (event->type() == QEvent::MouseButtonPress && ((QMouseEvent*)(event))->button()== Qt::LeftButton)
{
eState = eBtnStatePress;
}
//判断目标
if(m_pBtnSkin==obj || m_pBtnMenu==obj || m_pBtnMin==obj || m_pBtnMax==obj || m_pBtnClose==obj)
{
//如果状态有效
if(eState != eBtnStateNone)
{
//根据状态设置按钮图标
SetBtnIcon((QToolButton *)obj,eState, false);
return false;
}
}
return QWidget::eventFilter(obj,event);
}
//槽函数--slot_btnclick
void TitleBar::slot_btnclick()
{
QToolButton *pBtn = (QToolButton*)(sender());
if(pBtn==m_pBtnMin)
{
emit signal_min();
}
if(pBtn==m_pBtnMax)
{
emit signal_maxrestore();
}
if(pBtn==m_pBtnClose)
{
emit signal_close();
}
}
void TitleBar::slot_btnpress()
{
QToolButton *pBtn = (QToolButton*)(sender());
SetBtnIcon(pBtn, eBtnStatePress, false);
}