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