Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
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}
The class only is used by CConnecterThread.
The connector interface of the plug-in, which is only used by the plug-in.
virtual CParameterBase * GetParameter()
Get parameter.
virtual QString ServerName()
Current connect server name (remote desktop name, if not present, then IP:PORT).
It starts a background thread by default.
virtual int Load(QSettings &set) override
Load parameters.
virtual QWidget * GetViewer() override
Get Viewer.
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
Current connect server name (remote desktop name, if not present, then IP:PORT).
virtual int Connect() override
Start a background thread, and create an instance of CConnect .
virtual int Save(QSettings &set) override
Save parameters.
virtual int Load(QSettings &set)
Load parameters.
virtual int Save(QSettings &set)
Save parameters.
The scroll form class.
Definition FrmScroll.h:17
A widget which displays output image from a CConnectDesktop and sends input keypresses and mouse acti...
Definition FrmViewer.h:49
ADAPT_WINDOWS
The ADAPT_WINDOWS enum.
Definition FrmViewer.h:61
@ Original
Original desktop size, the left-top of the desktop is aligned with the left-top of the window.
@ Zoom
zoom windows = desktop size * factor
@ KeepAspectRationToWindow
Keep desktop aspectration adapt to windows.
@ ZoomToWindow
Desktop adapt to windows.
double GetZoomFactor() const
Adjust the zoom factor.
The plugin interface.