-
Notifications
You must be signed in to change notification settings - Fork 2
/
widget.cpp
147 lines (138 loc) · 6.21 KB
/
widget.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
//
// Created by lsk on 7/25/22.
//
#include "widget.h"
using namespace std;
Widget::Widget(configuration configuration, QWidget *parent) : QWidget(parent) {
// 初始化窗口,设置鼠标事件穿透
this->setGeometry(0,
0,
QApplication::desktop()->width(),
QApplication::desktop()->height());
this->setWindowOpacity(1);
this->setWindowFlags(Qt::FramelessWindowHint
|Qt::WindowStaysOnTopHint
|Qt::Tool);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setAttribute(Qt::WA_TransparentForMouseEvents);
XShapeCombineRectangles(QX11Info::display(),
this->winId(),
ShapeInput, 0, 0, nullptr, 0, ShapeSet, YXBanded);
this->currentConfiguration = configuration;
// 菜单初始化
auto *icon = new QSystemTrayIcon(this);
icon->setToolTip("Live2d");
icon->setIcon(QIcon::fromTheme("show_table_row"));
auto *trayIconMenu = new QMenu(this);
auto *enableHideOnHoverAction = new QAction("悬浮隐藏", this);
enableHideOnHoverAction->setCheckable(true);
enableHideOnHoverAction->setChecked(true);
connect(enableHideOnHoverAction, &QAction::toggled, this, [this](bool checked) {
this->currentConfiguration.setHideOnHover(checked);
});
trayIconMenu->addAction(enableHideOnHoverAction);
auto *showAction = new QAction("显示", this);
showAction->setCheckable(true);
showAction->setChecked(true);
connect(showAction, &QAction::toggled, this, [this, trayIconMenu](bool checked){
if (checked) {
this->show();
trayIconMenu->setIcon(QIcon::fromTheme("show_table_row"));
} else {
this->hide();
trayIconMenu->setIcon(QIcon::fromTheme("hide_table_row"));
}
});
trayIconMenu->addAction(showAction);
this->configDialog = new ConfigDialog(this->currentConfiguration, false, nullptr);
connect(this->configDialog, &ConfigDialog::okPressed, this, [this](class configuration conf) {
this->currentConfiguration = conf;
this->th->setMouseSensibility(conf.getMouseSensibility());
delete this->widget;
this->widget = new QLive2dWidget(this);
this->widget->resize(currentConfiguration.getWidgetSize());
this->widget->move(0, this->height() - this->widget->height());
connect(this->widget, SIGNAL(initialized(QLive2dWidget*)), this, SLOT(live2dInitialized(QLive2dWidget*)));
});
auto *configOption = new QAction("设置", this);
connect(configOption, &QAction::triggered, this, [this]() {
this->configDialog->show();
});
trayIconMenu->addAction(configOption);
auto *quitAction = new QAction("退出", this);
quitAction->setIcon(QIcon::fromTheme("application-exit"));
connect(quitAction, &QAction::triggered, this, &QCoreApplication::quit, Qt::QueuedConnection);
trayIconMenu->addAction(quitAction);
icon->setContextMenu(trayIconMenu);
icon->setVisible(true);
icon->show();
// Live2d组件初始化
this->widget = new QLive2dWidget(this);
qDebug() << this->currentConfiguration.getWidgetSize();
this->widget->resize(currentConfiguration.getWidgetSize());
this->widget->move(0, this->height() - this->widget->height());
connect(this->widget, SIGNAL(initialized(QLive2dWidget*)), this, SLOT(live2dInitialized(QLive2dWidget*)));
// 开启鼠标事件监听线程
this->th = new MouseEventThread(this->geometry(), this->winId(), this->currentConfiguration.getMouseSensibility(), this);
connect(this->th, SIGNAL(mouseEvent(QPoint,QPoint)), this, SLOT(mouseEvent(QPoint,QPoint)), Qt::QueuedConnection);
connect(this->th, &MouseEventThread::mousePress, this, &Widget::mousePress, Qt::QueuedConnection);
connect(this->th, &MouseEventThread::mouseRelease, this, &Widget::mouseRelease, Qt::QueuedConnection);
this->th->start();
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this, [this]() {this->th->exit();});
}
Widget::~Widget() {
delete this->widget;
delete this->th;
delete this->configDialog;
this->widget = nullptr;
this->th = nullptr;
this->configDialog = nullptr;
}
void Widget::live2dInitialized(QLive2dWidget *wid) {
cout << "Starting Live2D Widget with configuration: " << endl;
cout << QTS(this->currentConfiguration.toString());
wid->setResDir(this->currentConfiguration.getResourceDir().toStdString());
wid->setModel(this->currentConfiguration.getModelName().toStdString());
this->initialized = true;
}
void Widget::mouseEvent(QPoint rel, QPoint raw) {
widget->mouseMove(rel);
//widget->mouseMove(rel);
if (this->currentConfiguration.isHideOnHover()) {
if (widget->geometry().contains(raw) && widget->isVisible()) {
widget->hide();
}
if (!widget->geometry().contains(raw) && !widget->isVisible()) {
widget->show();
}
}
}
void Widget::setModel(std::string resourceDir, std::string modelName) {
this->currentConfiguration.setResourceDir(QString::fromStdString(resourceDir));
this->currentConfiguration.setModelName(QString::fromStdString(modelName));
}
void Widget::setWidgetPosition(bool widgetOnLeft) {
if (!widgetOnLeft) {
this->widget->move(this->width() - 300, this->height() - 300);
}
}
void Widget::mousePress(QPoint rel, QPoint raw) {
if (this->widget->geometry().contains(rel)) {
if (this->currentConfiguration.isHideOnHover()) {
this->widget->mousePress(QPoint(rel.x(), this->height() - rel.y()));
} else {
int spaceBetweenLeftAndWidget = this->width() - this->widget->width();
this->widget->mousePress(QPoint(rel.x() - spaceBetweenLeftAndWidget, this->height() - rel.y()));
}
}
}
void Widget::mouseRelease(QPoint rel, QPoint raw) {
if (this->widget->geometry().contains(rel)) {
if (this->currentConfiguration.isHideOnHover()) {
this->widget->mouseRelease(QPoint(rel.x(), this->height() - rel.y()));
} else {
int spaceBetweenLeftAndWidget = this->width() - this->widget->width();
this->widget->mouseRelease(QPoint(rel.x() - spaceBetweenLeftAndWidget, this->height() - rel.y()));
}
}
}