-
Notifications
You must be signed in to change notification settings - Fork 1
/
splitter.cpp
232 lines (208 loc) · 5.92 KB
/
splitter.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
#include "splitter.h"
#include <QtX11Extras/QX11Info>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <QSettings>
#include <QDebug>
#include <QPoint>
#include <QCursor>
#include <QApplication>
#include <QSize>
#define MARGIN 10
#define MINWIDTH 200
#define MINHEIGHT 150
void Splitter::setTitleBar (TitleBar *bar) {
mTitleBar = bar;
}
Splitter::Splitter(int id, QString title)
{
mId = id;
mTitle = title;
//window flags
setMouseTracking(true);
setWindowFlags (Qt::Window
| Qt::FramelessWindowHint
| Qt::WindowStaysOnBottomHint
| Qt::Tool);
//follow the current workspace
Atom net_wm_state_sticky=XInternAtom (QX11Info::display(),
"_NET_WM_STATE_STICKY",
True);
Atom net_wm_state = XInternAtom (QX11Info::display(),
"_NET_WM_STATE",
False);
XChangeProperty (QX11Info::display(),
winId(),
net_wm_state,
XA_ATOM,
32,
PropModeAppend,
(unsigned char *)&net_wm_state_sticky,
1);
readSettings();
//showNormal();
}
void Splitter::readSettings() {
QSettings settings;
settings.beginGroup("iconview/id_geometry_table");
restoreGeometry(settings.value("geometry"+QString::number(mId)).toByteArray());
qDebug()<<settings.value("geometry"+QString::number(mId));
qDebug()<<settings.allKeys()<<endl<<settings.childKeys()<<endl<<settings.children();
settings.endGroup();
}
void Splitter::writeSettings() {
QSettings settings;
settings.beginGroup("iconview/id_geometry_table");
settings.setValue("geometry"+QString::number(mId),saveGeometry());
settings.endGroup();
}
void Splitter::closeEvent(QCloseEvent *e) {
writeSettings();
QSplitter::closeEvent(e);
}
void Splitter::mousePressEvent(QMouseEvent *e) {
if(e->button() == Qt::LeftButton)
{
this->isLeftPressed = true;
QPoint temp = e->globalPos();
//qDebug()<<"Press globalpos: "<<temp<<"Press pos: "<<e->pos();
pLast = temp;
curPos = countFlag(e->pos(), countRow(e->pos()));
if(curPos != 22) //移动窗口
{
if (mTitleBar != NULL) {
mTitleBar->setEnabled(false);
}
}
e->ignore();
}
}
void Splitter::mouseMoveEvent(QMouseEvent *e) {
//qDebug()<<"move event splitter";
int poss = countFlag(e->pos(), countRow(e->pos()));
setCursorType(poss);
//是否左击
if(isLeftPressed)
{
QPoint ptemp = e->globalPos();
//ptemp = ptemp - pLast;
if(curPos == 22) //移动窗口
{
//ptemp = ptemp + e->pos();
//ptemp = ptemp + pos();
//move(ptemp);
}
else
{
QRect wid = geometry();
qDebug()<<wid;
//改变窗口的大小
switch(curPos)
{
//左上角
case 11:
//wid.setTopLeft(wid.topLeft() + ptemp);
wid.setTopLeft(e->globalPos());
break;
//右上角
case 13:
wid.setTopRight(wid.topRight() + ptemp);
wid.setTopRight(e->globalPos());
break;
//左下角
case 31:
//wid.setBottomLeft(wid.bottomLeft() + ptemp);
wid.setBottomLeft(e->globalPos());
break;
//右下角
case 33:
//wid.setBottomRight(wid.bottomRight() + ptemp);
wid.setBottomRight(e->globalPos());
break;
//中上角
case 12:
//wid.setTop(wid.top() + ptemp.y());
wid.setTop(e->globalY());
break;
//中左角
case 21:
//wid.setLeft(wid.left() + ptemp.x());
wid.setLeft(e->globalX());
break;
//中右角
case 23:
//wid.setRight(wid.right() + ptemp.x());
wid.setRight(e->globalX());
break;
//中下角
case 32:
//wid.setBottom(wid.bottom() + ptemp.y());
wid.setBottom(e->globalY());
break;
}
setGeometry(wid);
e->accept();
}
//更新位置
pLast = e->globalPos();
} else {
QApplication::restoreOverrideCursor();
}
e->ignore();
}
void Splitter::mouseReleaseEvent(QMouseEvent *e) {
writeSettings();
if (mTitleBar!=NULL) {
mTitleBar->setEnabled(true);
}
qDebug()<<"splitter mouse release";
setCursor(Qt::ArrowCursor);
isLeftPressed = false;
}
//计算鼠标在哪一行那一列
int Splitter::countFlag(QPoint p, int row)
{
if(p.y() < MARGIN)
return 10+row;
else if(p.y() > this->height()-MARGIN)
return 30+row;
else
return 20+row;
}
//根据鼠标所在位置改变鼠标指针形状
void Splitter::setCursorType(int flag)
{
//Qt::CursorShape cursor;
QCursor cursor;
switch(flag)
{
case 11:
case 33:
cursor = Qt::SizeFDiagCursor;
break;
case 13:
case 31:
cursor = Qt::SizeBDiagCursor;
break;
case 21:
case 23:
cursor = Qt::SizeHorCursor;
break;
case 12:
case 32:
cursor = Qt::SizeVerCursor;
break;
default:
//恢复鼠标指针形状
QApplication::restoreOverrideCursor();
break;
}
//qDebug()<<flag<<endl<<cursor;
setCursor(cursor);
}
//计算在哪一列
int Splitter::countRow(QPoint p)
{
return (p.x()<MARGIN) ? 1:( (p.x()>(this->width()-MARGIN) ? 3:2) );
}