-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplitter.cpp
323 lines (285 loc) · 8.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* splitter.cpp: splitter is an frameless window contain a titlebar and a view.
*
* Copyright (C) 2019, Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Yue Lan<[email protected]>
*
*/
#include "splitter.h"
#include "titlewidget.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QSettings>
#include <QPoint>
#include <QCursor>
#include <QApplication>
#include <QGuiApplication>
#include <QScreen>
#include <QDesktopWidget>
#include <QSize>
#include <QtX11Extras/QX11Info>
#include <QStandardPaths>
#include <QPainter>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#define MARGIN 10
#define MINWIDTH 200
#define MINHEIGHT 150
Splitter::Splitter(int id, QString title) {
mId = id;
mTitle = title;
mLayout = new QVBoxLayout(this);
mLayout->setSpacing(0);
mTitleWidget = new TitleWidget(id,title);
mLayout->addWidget(mTitleWidget);
mDirPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QString("/.desktop_file_memos/")+QString::number(id);
mTitleWidget->setDirPath(mDirPath);
mIconView = new IconView(id, mDirPath);
mLayout->addWidget(mIconView);
setMouseTracking(true);
setMinimumSize(MINWIDTH,MINHEIGHT);
setContentsMargins(0,0,0,0);
//set window states
setWindowFlags (Qt::Window
| Qt::FramelessWindowHint
| Qt::WindowStaysOnBottomHint
| Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground, true);
setStyleSheet("background-color: transparent;"
"border: 0 transparent;");
//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();
connect(this, &Splitter::writeFlagRequest, mTitleWidget, &TitleWidget::onWriteFlagRequest);
connect(mTitleWidget, &TitleWidget::viewHideRequest, [=](){
mIconView->hide();
mLayout->removeWidget(mIconView);
mLastSize = rect().size();
//qDebug()<<rect();
mLayout->removeWidget(mTitleWidget);
setMinimumHeight(50);
resize(rect().width(),0);
Q_EMIT writeFlagRequest();
});
connect(mTitleWidget, &TitleWidget::viewShowRequest, [=](){
mLayout->addWidget(mTitleWidget);
mLayout->addWidget(mIconView);
mIconView->show();
setMinimumHeight(MINHEIGHT);
resize(mLastSize);
Q_EMIT writeFlagRequest();
//Don't comment out this paragraph
writeSettings();
});
}
QSize Splitter::getLastWritingSize() {
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
QRect tmpRect = settings.value("geometry"+QString::number(mId)).toRect();
return tmpRect.size();
}
void Splitter::ensureLastSettings(QSettings *settings) {
if (mTitleWidget != nullptr) {
mTitleWidget->ensureLastSettings(settings);
}
}
void Splitter::readSettings() {
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
settings.beginGroup("iconview/id_geometry_table");
//qDebug()<<"read settings";
//qDebug()<<settings.value("geometry"+QString::number(mId)).toByteArray();
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(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
settings.beginGroup("iconview/id_geometry_table");
settings.setValue("geometry"+QString::number(mId),saveGeometry());
settings.endGroup();
}
void Splitter::paintEvent(QPaintEvent *e) {
/*
QPainter painter(this);
if (mIconView->isVisible())
painter.fillRect(this->rect(), QColor(0,0,0,80));
*/
return QWidget::paintEvent(e);
}
void Splitter::closeEvent(QCloseEvent *e) {
writeSettings();
QWidget::closeEvent(e);
}
void Splitter::mousePressEvent(QMouseEvent *e) {
if(e->button() == Qt::LeftButton)
{
this->isLeftPressed = true;
QPoint temp = e->globalPos();
pLast = temp;
mLastRect = geometry();
//qDebug()<<mLastRect.top()<<" "<<mLastRect.left()<<" "<<mLastRect.bottom()<<" "<<mLastRect.right();
curPos = countFlag(e->pos(), countRow(e->pos()));
if(curPos != 22)
{
if (mTitleWidget != nullptr) {
mTitleWidget->setEnabled(false);
}
}
//e->ignore();
e->accept();
}
return QWidget::mousePressEvent(e);
}
void Splitter::mouseMoveEvent(QMouseEvent *e) {
if (!mIconView->isVisible()) {
e->ignore();
return QWidget::mouseMoveEvent(e);
}
int poss = countFlag(e->pos(), countRow(e->pos()));
setCursorType(poss);
if(isLeftPressed)
{
if(curPos == 22)
{
}
else
{
QRect wid = geometry();
/*
qDebug()<<mLastRect<<e->globalPos();
qDebug()<<"dx:"<<mLastRect.left()-e->globalX();
qDebug()<<"dy:"<<mLastRect.top()-e->globalY();
qDebug()<<wid;
*/
switch(curPos)
{
case 11:
if (true) {
wid.setTopLeft(e->globalPos());
}
break;
case 13:
wid.setTopRight(e->globalPos());
break;
case 31:
wid.setBottomLeft(e->globalPos());
break;
case 33:
wid.setBottomRight(e->globalPos());
break;
case 12:
if (true) {
wid.setTop(e->globalY());
}
break;
case 21:
if (true) {
wid.setLeft(e->globalX());
}
break;
case 23:
if (true) {
wid.setRight(e->globalX());
}
break;
case 32:
wid.setBottom(e->globalY());
break;
}
setGeometry(wid);
/*
if (QGuiApplication::screens().constFirst()->availableGeometry().contains(e->pos())) {
setGeometry(wid);
}
*/
e->accept();
}
pLast = e->globalPos();
} else {
QApplication::restoreOverrideCursor();
}
e->ignore();
QWidget::mouseMoveEvent(e);
}
void Splitter::mouseReleaseEvent(QMouseEvent *) {
if (mTitleWidget!=nullptr) {
if (mIconView->isVisible()) {
writeSettings();
}
mTitleWidget->setEnabled(true);
}
if (mTitleWidget!=nullptr) {
}
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;
}
setCursor(cursor);
}
int Splitter::countRow(QPoint p)
{
return (p.x()<MARGIN) ? 1:( (p.x()>(this->width()-MARGIN) ? 3:2) );
}