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