This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloatingwidget.cpp
186 lines (149 loc) · 5.47 KB
/
floatingwidget.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
#include "floatingwidget.h"
#include "spiritsettingsform.h"
FloatingWidget::FloatingWidget(int id,QWidget *parent) :
QWidget(parent,Qt::FramelessWindowHint),
relativePos(0,0),
id(id),icon(),showshortcuts(false),shortcuts(),inuse(true)
{
// Set Basic Properties
this->setAttribute(Qt::WA_TranslucentBackground,true);
this->setWindowFlag(Qt::Tool);
this->setAcceptDrops(true);
this->resize(this->W,this->H);
this->setWindowFlag(Qt::WindowStaysOnTopHint);
// Init icon
iconPath = SettingsUtils::appDataPath()+"/"+QString::number(id,10)+".png";
QFile iconFile(iconPath);
if(!iconFile.exists()){
icon.load(":/image/ghost");
icon.save(iconPath);
}else{
icon.load(iconPath);
}
// Init Timers
mouseOutTimer = new QTimer(this);
mouseOutTimer->setSingleShot(true);
connect(mouseOutTimer,SIGNAL(timeout()),this,SLOT(mouseOut3s()));
// Init SettingsForm
form = new SpiritSettingsForm(this->id,this);
}
void FloatingWidget::paintEvent(QPaintEvent *){
QPainter iconPainter(this);
iconPainter.setOpacity(this->ALPHA);
iconPainter.setRenderHint(QPainter::Antialiasing);
iconPainter.setRenderHint(QPainter::SmoothPixmapTransform);
iconPainter.drawPixmap(this->OX-this->OW/2,this->OY-this->OH/2,this->OW,this->OH,icon);
QPainter shortcutPainter(this);
shortcutPainter.setOpacity(this->ALPHA);
shortcutPainter.setRenderHint(QPainter::Antialiasing);
shortcutPainter.setRenderHint(QPainter::SmoothPixmapTransform);
if(this->showshortcuts){
QJsonArray shortcuts = SettingsUtils::getShortcuts(this->id);
int count = shortcuts.size();
double deltaAngle = 2 * PI / count;
for(int i=0;i<count;i++){
QJsonObject shortcut = shortcuts.at(i).toObject();
double deltaX = std::sin(deltaAngle*i)*this->RADIUS;
double deltaY = 0 - std::cos(deltaAngle*i)*this->RADIUS;
qDebug()<<i<<":"<<deltaX<<","<<deltaY<<"----"<<this->RADIUS;
QPixmap shortcutIcon;
shortcutIcon.load(shortcut.value("iconPath").toString());
shortcutPainter.drawPixmap(this->OX+deltaX-this->CW/2,this->OY+deltaY-this->CH/2,this->CW,this->CH,shortcutIcon);
}
}
}
void FloatingWidget::enterEvent(QEvent *){
qDebug()<<"enter";
showShortcuts();
if(mouseOutTimer->isActive())mouseOutTimer->stop();
}
void FloatingWidget::leaveEvent(QEvent *){
qDebug()<<"leave";
mouseOutTimer->start(1000);
}
void FloatingWidget::mouseMoveEvent(QMouseEvent *event){
this->move(event->globalPos() + relativePos);
}
void FloatingWidget::mousePressEvent(QMouseEvent *event){
if(event->button()==Qt::LeftButton){
relativePos = this->pos() - event->globalPos();
}
}
void FloatingWidget::mouseReleaseEvent(QMouseEvent *event){
SettingsUtils::setPosition(this->id,event->globalPos() + relativePos);
QPoint pos = event->localPos().toPoint()-QPoint(this->OX,this->OY);
int dis2 = pos.x()*pos.x()+pos.y()*pos.y();
if(dis2>this->DIAGR2){
QJsonArray shortcuts = SettingsUtils::getShortcuts(this->id);
int count = shortcuts.size();
int angle = std::round(std::atan2(pos.y(),pos.x())/this->PI*180);
angle = (angle+450)%360;
int angleOffset =(angle+360/count/2)%360;
int offset = (double)angleOffset/(360/(double)count);
QJsonObject shortcut = shortcuts.at(offset).toObject();
QString lnkPath = shortcut.value("lnkPath").toString();
QUrl url = WinUtils::convertPath2URL(lnkPath);
QDesktopServices::openUrl(url);
}
qDebug()<<"release[ pos:"<<pos<<" dis2:"<<dis2<<" DIAGR2:"<<this->DIAGR2<<" ]";
qDebug()<<this->id;
}
void FloatingWidget::dragEnterEvent(QDragEnterEvent *event){
qDebug()<<"drag enter";
bool flag = true;
QList<QUrl> urls = event->mimeData()->urls();
for(QUrl url:urls){
QFileInfo info(url.path());
if(!info.isSymLink()&&info.suffix()!="png")
flag = false;
}
if(flag)
event->acceptProposedAction();
}
void FloatingWidget::dropEvent(QDropEvent *event){
qDebug()<<"drop";
QList<QUrl> urls = event->mimeData()->urls();
for(QUrl url:urls){
QString path = WinUtils::convertQTPath2WinPath(url.path());
QFileInfo info(path);
qDebug()<<"is symlink : "<<info.isSymLink()<<", link target : "<<info.symLinkTarget();
if(info.isSymLink()){
HWND hwnd = (HWND)this->winId();
HINSTANCE hInstance = (HINSTANCE)::GetModuleHandle(NULL);
SettingsUtils::addShortcut(this->id,path,hwnd,hInstance);
}else if(info.suffix()=="png"){
qDebug()<<"modify icon";
this->modifyIcon(path);
}
}
}
void FloatingWidget::mouseDoubleClickEvent(QMouseEvent *){
form->show();
/*
SettingsUtils::removeSpirit(this->id);
this->inuse=false;
this->hide();
*/
}
void FloatingWidget::showShortcuts(){
this->showshortcuts=true;
this->update();
}
void FloatingWidget::hideShortcuts(){
this->showshortcuts=false;
this->update();
}
void FloatingWidget::mouseOut3s(){
qDebug()<<"timer slot";
this->hideShortcuts();
}
void FloatingWidget::hideEvent(QHideEvent *){
qDebug()<<"hide "<<this->id;
}
void FloatingWidget::modifyIcon(QString path){
QString iconPath = SettingsUtils::appDataPath()+"/"+QString::number(this->id,10)+".png";
QFile(iconPath).remove();
QFile::copy(path,iconPath);
this->icon.load(iconPath);
this->update();
}