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