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