Rabbit Remote Control 0.0.32
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_pZoomToWindow(nullptr)
22 , m_pZoomAspectRatio(nullptr)
23 , m_pZoomOriginal(nullptr)
24 , m_pZoomIn(nullptr)
25 , m_pZoomOut(nullptr)
26 , m_psbZoomFactor(nullptr)
27 , m_pScreenShot(nullptr)
28#if HAVE_QT6_RECORD
29 , m_pRecord(nullptr)
30 , m_pRecordPause(nullptr)
31#endif
32{
33 qDebug(log) << Q_FUNC_INFO;
34}
35
36CConnecterThread::~CConnecterThread()
37{
38 qDebug(log) << Q_FUNC_INFO;
39}
40
42{
43 qDebug(log) << Q_FUNC_INFO;
44 int nRet = 0;
45
47 if(nRet)
48 return nRet;
49
50 Q_ASSERT(!(m_pFrmViewer && m_pScroll));
51 m_pFrmViewer = new CFrmViewer();
52 m_pScroll = new CFrmScroll(m_pFrmViewer);
53
54 nRet = InitialMenu();
55 return nRet;
56}
57
59{
60 qDebug(log) << Q_FUNC_INFO;
61 int nRet = 0;
62 if(m_pScroll)
63 {
64 delete m_pScroll;
65 m_pScroll = nullptr;
66 }
67
69 return nRet;
70}
71
72int CConnecterThread::InitialMenu()
73{
74 int nRet = 0;
75 bool check = false;
76
77 QMenu* pMenuZoom = new QMenu(&m_Menu);
78 pMenuZoom->setTitle(tr("Zoom"));
79 pMenuZoom->setIcon(QIcon::fromTheme("zoom"));
80 m_pMenuZoom = m_Menu.addMenu(pMenuZoom);
81 m_pZoomToWindow = pMenuZoom->addAction(
82 QIcon::fromTheme("zoom-fit-best"), tr("Zoom to window"));
83 m_pZoomToWindow->setCheckable(true);
84 check = connect(m_pZoomToWindow, &QAction::toggled, this,
85 [&](){
86 m_pScroll->slotSetAdaptWindows(
88 });
89 Q_ASSERT(check);
90 m_pZoomAspectRatio = pMenuZoom->addAction(
91 QIcon::fromTheme("zoom-aspect-ratio"),
92 tr("Keep aspect ration to windows"));
93 m_pZoomAspectRatio->setCheckable(true);
94 check = connect(m_pZoomAspectRatio, &QAction::toggled, this,
95 [&](){
96 m_pScroll->slotSetAdaptWindows(
98 });
99 Q_ASSERT(check);
100 m_pZoomOriginal = pMenuZoom->addAction(
101 QIcon::fromTheme("zoom-original"), tr("Original"));
102 m_pZoomOriginal->setCheckable(true);
103 check = connect(m_pZoomOriginal, &QAction::toggled, this,
104 [&](){
105 m_pScroll->slotSetAdaptWindows(
107 });
108 Q_ASSERT(check);
109 m_pZoomIn = pMenuZoom->addAction(QIcon::fromTheme("zoom-in"), tr("Zoom in"));
110 m_pZoomIn->setCheckable(true);
111 check = connect(
112 m_pZoomIn, &QAction::toggled, this,
113 [&](){
114 if(m_psbZoomFactor)
115 m_psbZoomFactor->setValue((m_pFrmViewer->GetZoomFactor() + 0.1) * 100);
116 });
117 Q_ASSERT(check);
118 m_pZoomOut = pMenuZoom->addAction(
119 QIcon::fromTheme("zoom-out"), tr("Zoom out"));
120 m_pZoomOut->setCheckable(true);
121 check = connect(
122 m_pZoomOut, &QAction::toggled, this,
123 [&](){
124 if(m_psbZoomFactor)
125 m_psbZoomFactor->setValue((m_pFrmViewer->GetZoomFactor() - 0.1) * 100);
126 });
127 Q_ASSERT(check);
128 QActionGroup* pGBViewZoom = new QActionGroup(this);
129 if(pGBViewZoom) {
130 pGBViewZoom->addAction(m_pZoomToWindow);
131 pGBViewZoom->addAction(m_pZoomAspectRatio);
132 pGBViewZoom->addAction(m_pZoomOriginal);
133 pGBViewZoom->addAction(m_pZoomIn);
134 pGBViewZoom->addAction(m_pZoomOut);
135 }
136 m_psbZoomFactor = new QSpinBox(pMenuZoom);
137 m_psbZoomFactor->setRange(0, 9999999);
138 m_psbZoomFactor->setValue(100);
139 m_psbZoomFactor->setSuffix("%");
140 m_psbZoomFactor->setEnabled(false);
141 m_psbZoomFactor->setFocusPolicy(Qt::NoFocus);
142 check = connect(
143 m_psbZoomFactor, SIGNAL(valueChanged(int)),
144 this, SLOT(slotValueChanged(int)));
145 Q_ASSERT(check);
146 QWidgetAction* pFactor = new QWidgetAction(pMenuZoom);
147 pFactor->setDefaultWidget(m_psbZoomFactor);
148 pMenuZoom->insertAction(m_pZoomOut, pFactor);
149
150 m_Menu.addSeparator();
151 m_pScreenShot = new QAction(QIcon::fromTheme("camera-photo"),
152 tr("ScreenShot"), &m_Menu);
153 check = connect(m_pScreenShot, SIGNAL(triggered()),
154 this, SLOT(slotScreenShot()));
155 Q_ASSERT(check);
156 m_Menu.addAction(m_pScreenShot);
157#if HAVE_QT6_RECORD
158 m_pRecord = new QAction(
159 QIcon::fromTheme("media-record"), tr("Record"), &m_Menu);
160 m_pRecord->setCheckable(true);
161 check = connect(m_pRecord, SIGNAL(toggled(bool)),
162 this, SLOT(slotRecord(bool)));
163 Q_ASSERT(check);
164 m_Menu.addAction(m_pRecord);
165 m_pRecordPause = new QAction(
166 QIcon::fromTheme("media-playback-pause"), tr("Record pause"), &m_Menu);
167 m_pRecordPause->setCheckable(true);
168 m_pRecordPause->setEnabled(false);
169 check = connect(m_pRecordPause, SIGNAL(toggled(bool)),
170 this, SIGNAL(sigRecordPause(bool)));
171 Q_ASSERT(check);
172 m_Menu.addAction(m_pRecordPause);
173#endif
174
175 m_Menu.addSeparator();
176 if(m_pSettings)
177 m_Menu.addAction(m_pSettings);
178
179 return nRet;
180}
181
183{
184 return m_pScroll;
185}
186
188{
189 qDebug(log) << Q_FUNC_INFO;
190 int nRet = 0;
191 m_pThread = new CConnectThread(this);
192 if(!m_pThread) {
193 qCritical(log) << "new CConnectThread fail";
194 return -2;
195 }
196
197 m_pThread->start();
198
199 return nRet;
200}
201
203{
204 qDebug(log) << Q_FUNC_INFO;
205 int nRet = 0;
206 if(m_pThread)
207 {
208 m_pThread->quit();
209 //Don't delete m_pThread, See CConnectThread
210 m_pThread = nullptr;
211 }
212 return nRet;
213}
214
216{
217 if(GetParameter())
218 if(!GetParameter()->GetShowServerName()
219 || CConnecterConnect::ServerName().isEmpty())
220 {
221 if(!GetParameter()->m_Net.GetHost().isEmpty())
222 return GetParameter()->m_Net.GetHost() + ":"
223 + QString::number(GetParameter()->m_Net.GetPort());
224 }
226}
227
228int CConnecterThread::Load(QSettings &set)
229{
230 int nRet = 0;
231 Q_ASSERT(m_pFrmViewer);
232 nRet = CConnecterConnect::Load(set);
233 if(m_pFrmViewer && GetParameter())
234 {
235 m_pFrmViewer->slotSetZoomFactor(GetParameter()->GetZoomFactor());
236 if(m_psbZoomFactor)
237 m_psbZoomFactor->setValue(m_pFrmViewer->GetZoomFactor() * 100);
238 CFrmViewer::ADAPT_WINDOWS aw = GetParameter()->GetAdaptWindows();
240 if(m_pZoomToWindow)
241 m_pZoomToWindow->setChecked(true);
243 if(m_pZoomAspectRatio)
244 m_pZoomAspectRatio->setChecked(true);
245 } else if(CFrmViewer::ADAPT_WINDOWS::Original == aw) {
246 if(m_pZoomOriginal)
247 m_pZoomOriginal->setChecked(true);
248 } else if(CFrmViewer::ADAPT_WINDOWS::Zoom == aw) {
249 if(m_pZoomIn)
250 m_pZoomIn->setChecked(true);
251 }
252 }
253 return nRet;
254}
255
256int CConnecterThread::Save(QSettings &set)
257{
258 int nRet = 0;
259 Q_ASSERT(GetParameter());
260 if(GetParameter() && m_pFrmViewer)
261 {
262 GetParameter()->SetAdaptWindows(m_pFrmViewer->GetAdaptWindows());
263 GetParameter()->SetZoomFactor(m_pFrmViewer->GetZoomFactor());
264 }
265 nRet = CConnecterConnect::Save(set);
266 return nRet;
267}
268
269#if HAVE_QT6_RECORD
270void CConnecterThread::slotRecord(bool checked)
271{
272 qDebug(log) << Q_FUNC_INFO << checked;
273 QAction* pRecord = qobject_cast<QAction*>(sender());
274 if(pRecord)
275 {
276 if(checked) {
277 pRecord->setIcon(QIcon::fromTheme("media-playback-stop"));
278 pRecord->setText(tr("Stop record"));
279 }
280 else {
281 pRecord->setIcon(QIcon::fromTheme("media-playback-start"));
282 pRecord->setText(tr("Start record"));
283 }
284 m_pRecordPause->setEnabled(checked);
285 emit sigRecord(checked);
286 }
287}
288
289void CConnecterThread::slotRecorderStateChanged(
290 QMediaRecorder::RecorderState state)
291{
292 if(QMediaRecorder::StoppedState == state)
293 {
294 slotRecord(false);
295 m_pRecordPause->setChecked(false);
296 }
297}
298#endif
299
301{
302 if(!m_pScroll || !m_pFrmViewer) return;
303 m_pFrmViewer->slotSetZoomFactor(((double)v) / 100);
304 m_pScroll->slotSetAdaptWindows(CFrmViewer::ADAPT_WINDOWS::Zoom);
305}
306
307void CConnecterThread::slotScreenShot()
308{
309 if(!GetParameter() || !m_pFrmViewer)
310 return;
311 auto &record = GetParameter()->m_Record;
312 QString szFile = record.GetImageFile(true);
313 bool bRet = m_pFrmViewer->GrabImage().save(szFile);
314 if(bRet)
315 qDebug(log) << "Success: save screenshot to" << szFile;
316 else
317 qCritical(log) << "Fail: save screenshot to" << szFile;
318 if(record.GetEndAction() != CParameterRecord::ENDACTION::No)
319 QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
320}
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 Clean()
Clean parameters and resource.
virtual int Save(QSettings &set)
Save parameters.
virtual int Initial()
Initial parameters and resource.
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.