Rabbit Remote Control 0.0.33
Loading...
Searching...
No Matches
Connecter.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QClipboard>
4#include <QApplication>
5#include <QDebug>
6#include <QGenericArgument>
7#include <QCheckBox>
8#include <QLoggingCategory>
9#include <QInputDialog>
10#include <QMetaMethod>
11
12#include "Connecter.h"
13#include "PluginClient.h"
14#include "RabbitCommonTools.h"
15#include "RabbitCommonDir.h"
16
17static Q_LOGGING_CATEGORY(log, "Client.Connecter")
18
19CConnecter::CConnecter(CPluginClient *plugin) : QObject(plugin),
20 m_pPluginClient(plugin),
21 m_pSettings(nullptr),
22 m_pParameter(nullptr)
23{
24 bool check = false;
25
26 if(QApplication::clipboard())
27 {
28 check = connect(QApplication::clipboard(), SIGNAL(dataChanged()),
29 this, SIGNAL(sigClipBoardChanged()));
30 Q_ASSERT(check);
31 }
32}
33
34CConnecter::~CConnecter()
35{
36 qDebug(log) << "CConnecter::~CConnecter";
37}
38
39const QString CConnecter::Id()
40{
41 QString szId = Protocol() + "_" + GetPlugClient()->Name();
42 return szId;
43}
44
45const QString CConnecter::Name()
46{
47 return GetPlugClient()->Name();
48}
49
51{
52 return tr("Name: ") + Name() + "\n"
53 + tr("Protocol: ") + Protocol()
54#ifdef DEBUG
55 + " - " + GetPlugClient()->DisplayName()
56#endif
57 + "\n"
58 + tr("Description: ") + GetPlugClient()->Description();
59}
60
61const QString CConnecter::Protocol() const
62{
63 return GetPlugClient()->Protocol();
64}
65
66const QIcon CConnecter::Icon() const
67{
68 return GetPlugClient()->Icon();
69}
70
71void CConnecter::slotSetClipboard(QMimeData* data)
72{
73 QClipboard* pClipboard = QApplication::clipboard();
74 if(pClipboard) {
75 // pClipboard->disconnect(this);
76
77 pClipboard->setMimeData(data);
78
79 // bool check = connect(pClipboard, SIGNAL(dataChanged()),
80 // this, SIGNAL(sigClipBoardChanged()));
81 // Q_ASSERT(check);
82 }
83}
84
86{
87 int nRet = QDialog::Accepted;
88 QDialog* p = OnOpenDialogSettings(parent);
89 if(p)
90 {
91 // The dialog is closed when the connect is close.
92 bool check = connect(this, SIGNAL(sigDisconnected()),
93 p, SLOT(reject()));
94 Q_ASSERT(check);
95 p->setWindowIcon(this->Icon());
96 p->setWindowTitle(tr("Set ") + GetPlugClient()->DisplayName());
97 p->setAttribute(Qt::WA_DeleteOnClose);
98 nRet = RC_SHOW_WINDOW(p);
99 } else {
100 qCritical(log) << "The Protocol [" << Protocol() << "] don't settings dialog";
101 }
102 return nRet;
103}
104
105QMenu* CConnecter::GetMenu(QWidget* parent)
106{
107 if(m_Menu.actions().isEmpty())
108 return nullptr;
109 return &m_Menu;
110}
111
112QString CConnecter::GetSettingsFile()
113{
114 if(m_szSettings.isEmpty())
115 {
116 m_szSettings = RabbitCommon::CDir::Instance()->GetDirUserData()
117 + QDir::separator()
118 + Id()
119 + ".rrc";
120 }
121 return m_szSettings;
122}
123
124int CConnecter::SetSettingsFile(const QString &szFile)
125{
126 m_szSettings = szFile;
127 return 0;
128}
129
130int CConnecter::Load(QString szFile)
131{
132 Q_ASSERT(!szFile.isEmpty());
133 if(szFile.isEmpty())
134 {
135 qCritical(log) << "The load file is empty";
136 return -1;
137 }
138 QSettings set(szFile, QSettings::IniFormat);
139 return Load(set);
140}
141
142int CConnecter::Save(QString szFile)
143{
144 Q_ASSERT(!szFile.isEmpty());
145 if(szFile.isEmpty())
146 {
147 qCritical(log) << "The load file is empty";
148 return -1;
149 }
150 QSettings set(szFile, QSettings::IniFormat);
151 return Save(set);
152}
153
154int CConnecter::Load(QSettings &set)
155{
156 int nRet = 0;
157 Q_ASSERT(m_pParameter);
158 if(m_pParameter)
159 nRet = m_pParameter->Load(set);
160 return nRet;
161}
162
163int CConnecter::Save(QSettings &set)
164{
165 int nRet = 0;
166 Q_ASSERT(m_pParameter);
167 if(m_pParameter) {
168 m_pParameter->Save(set);
169 }
170 return nRet;
171}
172
174{
175 m_Menu.setIcon(m_pPluginClient->Icon());
176 m_Menu.setTitle(m_pPluginClient->DisplayName());
177 m_Menu.setToolTip(m_pPluginClient->DisplayName());
178 m_Menu.setStatusTip(m_pPluginClient->DisplayName());
179
180 m_pSettings = new QAction(QIcon::fromTheme("system-settings"),
181 tr("Settings"), &m_Menu);
182 if(m_pSettings) {
183 bool check = connect(m_pSettings, SIGNAL(triggered()),
184 this, SLOT(slotSettings()));
185 Q_ASSERT(check);
186 }
187 return 0;
188}
189
191{
192 return 0;
193}
194
196{
197 if(!GetParameter())
198 {
199 QString szMsg = "The CConnecter is not parameters! "
200 "please first create parameters, "
201 "then call SetParameter in the ";
202 szMsg += metaObject()->className() + QString("::")
203 + metaObject()->className();
204 szMsg += QString(" or ") + metaObject()->className()
205 + QString("::") + "Initial()";
206 szMsg += " to set the parameters pointer. "
207 "Default set CParameterClient for the parameters of connecter "
208 "(CParameterConnecter or its derived classes) "
209 "See: CClient::CreateConnecter. "
210 "If you are sure the parameter of connecter "
211 "does not need CParameterClient. "
212 "Please overload the SetParameterClient in the ";
213 szMsg += QString(metaObject()->className()) + " . don't set it";
214 qCritical(log) << szMsg.toStdString().c_str();
215 Q_ASSERT(false);
216 return -1;
217 }
218 return 0;
219}
220
222{
223 if(m_pParameter)
224 m_pParameter->disconnect(this);
225
226 m_pParameter = p;
227
228 return 0;
229}
230
231CParameter* CConnecter::GetParameter()
232{
233 return m_pParameter;
234}
235
236CPluginClient* CConnecter::GetPlugClient() const
237{
238 return m_pPluginClient;
239}
240
241void CConnecter::slotShowServerName()
242{
243 emit sigUpdateName(Name());
244}
245
246void CConnecter::slotUpdateName()
247{
248 emit sigUpdateName(Name());
249}
250
251QObject* CConnecter::createObject(const QString& className, QObject* parent)
252{
253 Q_UNUSED(parent);
254 int type =
255#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
256 QMetaType::fromName(className.toStdString().c_str()).id();
257#else
258 QMetaType::type(className.toStdString().c_str());
259#endif
260 if(QMetaType::UnknownType == type)
261 {
262 qCritical(log) << className << " is QMetaType::UnknownType";
263 return nullptr;
264 }
265 QObject *obj =
266#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
267 (QObject*)QMetaType(type).create();
268#else
269 (QObject*)QMetaType::create(type);
270#endif
271 if(nullptr == obj)
272 {
273 qCritical(log) << "QMetaType::create fail: " << type;
274 return nullptr;
275 }
276 //const QMetaObject* metaObj = QMetaType::metaObjectForType(type);
277 //QObject *obj = metaObj->newInstance(Q_ARG(QObject*, parent));
278 return obj;
279}
280
281void CConnecter::slotBlockShowWidget(const QString& className, int &nRet, void* pContext)
282{
283 bool check = false;
284 QObject *obj = createObject(className);
285 Q_ASSERT_X(obj, "Connecter", QString("Create object failed: " + className).toStdString().c_str());
286 /*
287 const QMetaObject* metaObject = obj->metaObject();
288 QStringList methods;
289 for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
290 methods << QString::fromLatin1(metaObject->method(i).methodSignature());
291 qCritical(log) << methods;
292 //*/
293 if(-1 == obj->metaObject()->indexOfMethod("SetContext(void*)"))
294 {
295 QString szErr;
296 szErr = "The class " + className + " is not method SetContext. It must be SetContext(void*) method.";
297 qCritical(log) << szErr;
298 Q_ASSERT_X(false, "Connecter", szErr.toStdString().c_str());
299 }
300 obj->metaObject()->invokeMethod(obj, "SetContext", Q_ARG(void*, pContext));
301 if(-1 < obj->metaObject()->indexOfMethod("SetConnecter(CConnecter*)"))
302 {
303 obj->metaObject()->invokeMethod(obj, "SetConnecter", Q_ARG(CConnecter*, this));
304 }
305
306 if(obj->inherits("QDialog"))
307 {
308 QDialog* pDlg = qobject_cast<QDialog*>(obj);
309 pDlg->setAttribute(Qt::WA_DeleteOnClose);
310 check = connect(this, SIGNAL(sigDisconnected()),
311 pDlg, SLOT(reject()));
312 Q_ASSERT(check);
313 nRet = RC_SHOW_WINDOW(pDlg);
314 } else if(obj->inherits("QWidget")) {
315 QWidget* pWdg = qobject_cast<QWidget*>(obj);
316 pWdg->setAttribute(Qt::WA_DeleteOnClose);
317 check = connect(this, SIGNAL(sigDisconnected()),
318 pWdg, SLOT(close()));
319 Q_ASSERT(check);
320 nRet = RC_SHOW_WINDOW(pWdg);
321 }
322}
323
324void CConnecter::slotBlockShowMessageBox(const QString &szTitle, const QString &szMessage,
325 QMessageBox::StandardButtons buttons,
326 QMessageBox::StandardButton &nRet,
327 bool &checkBox,
328 QString szCheckBoxContext)
329{
330 QCheckBox* pBox = nullptr;
331 QMessageBox msg(QMessageBox::Information,
332 szTitle, szMessage, buttons, GetViewer());
333 if(!szCheckBoxContext.isEmpty())
334 {
335 pBox = new QCheckBox(szCheckBoxContext);
336 if(pBox)
337 pBox->setCheckable(true);
338 msg.setCheckBox(pBox);
339 }
340 nRet = (QMessageBox::StandardButton)RC_SHOW_WINDOW(&msg);
341 if(pBox)
342 checkBox = pBox->isChecked();
343}
344
345void CConnecter::slotBlockInputDialog(const QString &szTitle, const QString &szLable,
346 const QString &szMessage,
347 QString& szText)
348{
349 bool ok = false;
350 QString t = QInputDialog::getText(nullptr,
351 szTitle,
352 szLable,
353 QLineEdit::Normal,
354 szMessage,
355 &ok);
356 if(ok && !t.isEmpty())
357 szText = t;
358}
359
360void CConnecter::slotSettings()
361{
363}
Connecter interface.
Definition Connecter.h:62
void sigUpdateName(const QString &szName)
virtual void slotBlockShowWidget(const QString &className, int &nRet, void *pContext)
Blocks the background thread and displays the window in the foreground thread.
virtual const QIcon Icon() const
Icon.
Definition Connecter.cpp:66
virtual void slotBlockInputDialog(const QString &szTitle, const QString &szLable, const QString &szMessage, QString &szText)
Block background threads and display input dialogs in foreground threads (QInputDialog)
virtual int Load(QSettings &set)
Load parameters.
virtual Q_INVOKABLE int SetParameterClient(CParameterClient *pPara)
Set CParameterClient.
virtual Q_INVOKABLE int Clean()
Clean parameters and resource.
virtual QDialog * OnOpenDialogSettings(QWidget *parent=nullptr)=0
Open settgins dialog.
virtual int OpenDialogSettings(QWidget *parent=nullptr)
Open settings dialog.
Definition Connecter.cpp:85
virtual QWidget * GetViewer()=0
Get Viewer.
void sigDisconnected()
Successful disconnection signal.
virtual int Save(QSettings &set)
Save parameters.
virtual int SetParameter(CParameter *p)
Set parameters.
virtual const QString Description()
Description.
Definition Connecter.cpp:50
virtual const QString Protocol() const
Protocol.
Definition Connecter.cpp:61
virtual const QString Id()
Identity.
Definition Connecter.cpp:39
virtual QMenu * GetMenu(QWidget *parent=nullptr)
Get operate menu.
virtual void slotSetClipboard(QMimeData *data)
Definition Connecter.cpp:71
virtual Q_INVOKABLE int Initial()
Initial parameters and resource.
virtual const QString Name()
Name.
Definition Connecter.cpp:45
virtual void slotBlockShowMessageBox(const QString &szTitle, const QString &szMessage, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton &nRet, bool &checkBox, QString szCheckBoxContext=QString())
Block background threads and display message dialogs in foreground threads (QMessageBox)
The parameters of client.
Parameter interface.
Definition Parameter.h:169
virtual int Save(QString szFile=QString(), bool bForce=true)
Save to file.
Definition Parameter.cpp:44
virtual int Load(QString szFile=QString())
Load from file.
Definition Parameter.cpp:33
The plugin interface.
virtual const QString DisplayName() const
The plugin display name.
virtual const QString Description() const =0
Plugin description.
virtual const QString Name() const =0
This name must be the same as the project name (${PROJECT_NAME}). The translation file (${PROJECT_NAM...
virtual const QString Protocol() const =0
Plugin Protocol.