玉兔远程控制 0.0.31
载入中...
搜索中...
未找到
ConnecterThread.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QLoggingCategory>
4#include <QDesktopServices>
5#include <QUrl>
6
7#include <QWidgetAction>
8#include <QActionGroup>
9
10#include "ConnecterThread.h"
11#include "ConnectThread.h"
12#include "PluginClient.h"
13
14static Q_LOGGING_CATEGORY(log, "Client.Connecter.Thread")
15
17 : CConnecterConnect(plugin)
18 , m_pThread(nullptr)
19 , m_pFrmViewer(nullptr)
20 , m_pScroll(nullptr)
21 , m_psbZoomFactor(nullptr)
22{
23 qDebug(log) << Q_FUNC_INFO;
24}
25
26CConnecterThread::~CConnecterThread()
27{
28 qDebug(log) << Q_FUNC_INFO;
29}
30
32{
33 qDebug(log) << Q_FUNC_INFO;
34 int nRet = 0;
35 bool check = false;
36
37 Q_ASSERT(!(m_pFrmViewer && m_pScroll));
38 m_pFrmViewer = new CFrmViewer();
39 m_pScroll = new CFrmScroll(m_pFrmViewer);
40
41 QMenu* pMenuZoom = new QMenu(&m_Menu);
42 pMenuZoom->setTitle(tr("Zoom"));
43 pMenuZoom->setIcon(QIcon::fromTheme("zoom"));
44 m_pMenuZoom = m_Menu.addMenu(pMenuZoom);
45 m_pZoomToWindow = pMenuZoom->addAction(
46 QIcon::fromTheme("zoom-fit-best"), tr("Zoom to window"));
47 m_pZoomToWindow->setCheckable(true);
48 check = connect(m_pZoomToWindow, &QAction::toggled, this,
49 [&](){
50 m_pScroll->slotSetAdaptWindows(
52 });
53 Q_ASSERT(check);
54 m_pZoomAspectRatio = pMenuZoom->addAction(
55 QIcon::fromTheme("zoom-aspect-ratio"),
56 tr("Keep aspect ration to windows"));
57 m_pZoomAspectRatio->setCheckable(true);
58 check = connect(m_pZoomAspectRatio, &QAction::toggled, this,
59 [&](){
60 m_pScroll->slotSetAdaptWindows(
62 });
63 Q_ASSERT(check);
64 m_pZoomOriginal = pMenuZoom->addAction(
65 QIcon::fromTheme("zoom-original"), tr("Original"));
66 m_pZoomOriginal->setCheckable(true);
67 check = connect(m_pZoomOriginal, &QAction::toggled, this,
68 [&](){
69 m_pScroll->slotSetAdaptWindows(
71 });
72 Q_ASSERT(check);
73 m_pZoomIn = pMenuZoom->addAction(QIcon::fromTheme("zoom-in"), tr("Zoom in"));
74 m_pZoomIn->setCheckable(true);
75 check = connect(
76 m_pZoomIn, &QAction::toggled, this,
77 [&](){
78 if(m_psbZoomFactor)
79 m_psbZoomFactor->setValue((m_pFrmViewer->GetZoomFactor() + 0.1) * 100);
80 });
81 Q_ASSERT(check);
82 m_pZoomOut = pMenuZoom->addAction(
83 QIcon::fromTheme("zoom-out"), tr("Zoom out"));
84 m_pZoomOut->setCheckable(true);
85 check = connect(
86 m_pZoomOut, &QAction::toggled, this,
87 [&](){
88 if(m_psbZoomFactor)
89 m_psbZoomFactor->setValue((m_pFrmViewer->GetZoomFactor() - 0.1) * 100);
90 });
91 Q_ASSERT(check);
92 QActionGroup* pGBViewZoom = new QActionGroup(this);
93 if(pGBViewZoom) {
94 pGBViewZoom->addAction(m_pZoomToWindow);
95 pGBViewZoom->addAction(m_pZoomAspectRatio);
96 pGBViewZoom->addAction(m_pZoomOriginal);
97 pGBViewZoom->addAction(m_pZoomIn);
98 pGBViewZoom->addAction(m_pZoomOut);
99 }
100 m_psbZoomFactor = new QSpinBox(pMenuZoom);
101 m_psbZoomFactor->setRange(0, 9999999);
102 m_psbZoomFactor->setValue(100);
103 m_psbZoomFactor->setSuffix("%");
104 m_psbZoomFactor->setEnabled(false);
105 m_psbZoomFactor->setFocusPolicy(Qt::NoFocus);
106 check = connect(
107 m_psbZoomFactor, SIGNAL(valueChanged(int)),
108 this, SLOT(slotValueChanged(int)));
109 Q_ASSERT(check);
110 QWidgetAction* pFactor = new QWidgetAction(pMenuZoom);
111 pFactor->setDefaultWidget(m_psbZoomFactor);
112 pMenuZoom->insertAction(m_pZoomOut, pFactor);
113
114 m_Menu.addSeparator();
115 m_pScreenShot = new QAction(QIcon::fromTheme("camera-photo"),
116 tr("ScreenShot"));
117 check = connect(m_pScreenShot, SIGNAL(triggered()),
118 this, SLOT(slotScreenShot()));
119 Q_ASSERT(check);
120 m_Menu.addAction(m_pScreenShot);
121#if HAVE_QT6_RECORD
122 m_pRecord = new QAction(QIcon::fromTheme("media-record"), tr("Record"), this);
123 m_pRecord->setCheckable(true);
124 check = connect(m_pRecord, SIGNAL(toggled(bool)),
125 this, SLOT(slotRecord(bool)));
126 Q_ASSERT(check);
127 m_Menu.addAction(m_pRecord);
128 m_pRecordPause = new QAction(QIcon::fromTheme("media-playback-pause"),
129 tr("Record pause"), this);
130 m_pRecordPause->setCheckable(true);
131 m_pRecordPause->setEnabled(false);
132 check = connect(m_pRecordPause, SIGNAL(toggled(bool)),
133 this, SIGNAL(sigRecordPause(bool)));
134 Q_ASSERT(check);
135 m_Menu.addAction(m_pRecordPause);
136#endif
137
138 m_Menu.addSeparator();
139 if(m_pSettings)
140 m_Menu.addAction(m_pSettings);
141
142 return nRet;
143}
144
146{
147 qDebug(log) << Q_FUNC_INFO;
148 int nRet = 0;
149 if(m_pScroll)
150 {
151 delete m_pScroll;
152 m_pScroll = nullptr;
153 }
154 return nRet;
155}
156
158{
159 return m_pScroll;
160}
161
163{
164 qDebug(log) << Q_FUNC_INFO;
165 int nRet = 0;
166 m_pThread = new CConnectThread(this);
167 if(!m_pThread) {
168 qCritical(log) << "new CConnectThread fail";
169 return -2;
170 }
171
172 m_pThread->start();
173
174 return nRet;
175}
176
178{
179 qDebug(log) << Q_FUNC_INFO;
180 int nRet = 0;
181 if(m_pThread)
182 {
183 m_pThread->quit();
184 //Don't delete m_pThread, See CConnectThread
185 m_pThread = nullptr;
186 }
187 return nRet;
188}
189
191{
192 if(GetParameter())
193 if(!GetParameter()->GetShowServerName()
194 || CConnecterConnect::ServerName().isEmpty())
195 {
196 if(!GetParameter()->m_Net.GetHost().isEmpty())
197 return GetParameter()->m_Net.GetHost() + ":"
198 + QString::number(GetParameter()->m_Net.GetPort());
199 }
201}
202
203int CConnecterThread::Load(QSettings &set)
204{
205 int nRet = 0;
206 Q_ASSERT(m_pFrmViewer);
207 nRet = CConnecterConnect::Load(set);
208 if(m_pFrmViewer && GetParameter())
209 {
210 m_pFrmViewer->slotSetZoomFactor(GetParameter()->GetZoomFactor());
211 if(m_psbZoomFactor)
212 m_psbZoomFactor->setValue(m_pFrmViewer->GetZoomFactor() * 100);
213 CFrmViewer::ADAPT_WINDOWS aw = GetParameter()->GetAdaptWindows();
215 m_pZoomToWindow->setChecked(true);
217 m_pZoomAspectRatio->setChecked(true);
219 m_pZoomOriginal->setChecked(true);
221 m_pZoomIn->setChecked(true);
222 }
223 return nRet;
224}
225
226int CConnecterThread::Save(QSettings &set)
227{
228 int nRet = 0;
229 Q_ASSERT(GetParameter());
230 if(GetParameter() && m_pFrmViewer)
231 {
232 GetParameter()->SetAdaptWindows(m_pFrmViewer->GetAdaptWindows());
233 GetParameter()->SetZoomFactor(m_pFrmViewer->GetZoomFactor());
234 }
235 nRet = CConnecterConnect::Save(set);
236 return nRet;
237}
238
239#if HAVE_QT6_RECORD
240void CConnecterThread::slotRecord(bool checked)
241{
242 qDebug(log) << Q_FUNC_INFO << checked;
243 QAction* pRecord = qobject_cast<QAction*>(sender());
244 if(pRecord)
245 {
246 if(checked) {
247 pRecord->setIcon(QIcon::fromTheme("media-playback-stop"));
248 pRecord->setText(tr("Stop record"));
249 }
250 else {
251 pRecord->setIcon(QIcon::fromTheme("media-playback-start"));
252 pRecord->setText(tr("Start record"));
253 }
254 m_pRecordPause->setEnabled(checked);
255 emit sigRecord(checked);
256 }
257}
258
259void CConnecterThread::slotRecorderStateChanged(
260 QMediaRecorder::RecorderState state)
261{
262 if(QMediaRecorder::StoppedState == state)
263 {
264 slotRecord(false);
265 m_pRecordPause->setChecked(false);
266 }
267}
268#endif
269
271{
272 if(!m_pScroll || !m_pFrmViewer) return;
273 m_pFrmViewer->slotSetZoomFactor(((double)v) / 100);
274 m_pScroll->slotSetAdaptWindows(CFrmViewer::ADAPT_WINDOWS::Zoom);
275}
276
277void CConnecterThread::slotScreenShot()
278{
279 if(!GetParameter() || !m_pFrmViewer)
280 return;
281 auto &record = GetParameter()->m_Record;
282 QString szFile = record.GetImageFile(true);
283 bool bRet = m_pFrmViewer->GrabImage().save(szFile);
284 if(bRet)
285 qDebug(log) << "Success: save screenshot to" << szFile;
286 else
287 qCritical(log) << "Fail: save screenshot to" << szFile;
288 if(record.GetEndAction() != CParameterRecord::ENDACTION::No)
289 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
290}
此类仅用于 CConnecterThread 。 一个线程只处理一个连接
插件的连接者接口,此接口仅由插件使用。 所有的需要 CConnect 的插件连接者均需要从此接口派生。
virtual CParameterBase * GetParameter()
Get parameter
virtual QString ServerName()
当前连接名(远程桌面的名称,如果没有,则是 IP:端口)。例如:服务名或 IP:端口
默认启动一个后台线程。实现一个后台线程处理一个连接。 可与插件接口从 CPluginClient 派生的插件一起使用,用于连接是阻塞模型的。
virtual int Load(QSettings &set) override
加载参数
virtual QWidget * GetViewer() override
得到显示视图
virtual int Initial() override
Initial parameters and resource
void slotValueChanged(int v)
emit by zoom menu in the class
virtual int Clean() override
Clean parameters and resource
virtual int DisConnect() override
emit sigConnected() in CConnectThread::run()
virtual QString ServerName() override
当前连接名(远程桌面的名称,如果没有,则是 IP:端口)。例如:服务名或 IP:端口
virtual int Connect() override
启动一个后台线程,并建立 CConnect 实例。在 CConnect 中触发 sigConnected()
virtual int Save(QSettings &set) override
保存参数
virtual int Load(QSettings &set)
加载参数
virtual int Save(QSettings &set)
保存参数
The scroll form class
Definition FrmScroll.h:17
用于显示从 CConnectDesktop 输出的图像,和向 CConnectDesktop 发送键盘、鼠标事件。
Definition FrmViewer.h:49
ADAPT_WINDOWS
窗口适配枚举常量
Definition FrmViewer.h:61
@ Original
原始桌面大小,桌面的左上点与窗口的左上点对齐
@ Zoom
缩放窗口大小等于桌面大小 * 系数
@ KeepAspectRationToWindow
保持长宽比缩放到窗口大小,窗口是固定的
@ ZoomToWindow
桌面缩放到窗口大小,窗口是固定的
double GetZoomFactor() const
调整缩放系数。 调整完成后需要调用 SetAdaptWindows(FrmViewer::Zoom) 缩放窗口大小。
客户端插件接口。用于建立 CConnecter 实例,它由协议插件实现。