玉兔远程控制 0.0.31
载入中...
搜索中...
未找到
FrmListConnects.cpp
1#include "FrmListConnects.h"
2#include "RabbitCommonDir.h"
3#include <QGridLayout>
4#include <QDateTime>
5#include <QStandardItem>
6#include <QDir>
7#include <QHeaderView>
8#include <QMenu>
9
10CFrmListConnects::CFrmListConnects(CClient* pClient, bool bDock, QWidget *parent) :
11 QWidget(parent),
12 m_pToolBar(nullptr),
13 m_ptbConnect(nullptr),
14 m_pMenuNew(nullptr),
15 m_pModel(nullptr),
16 m_pClient(pClient),
17 m_nFileRow(0),
18 m_bDock(bDock),
19 m_pDockTitleBar(nullptr)
20{
21 setFocusPolicy(Qt::NoFocus);
22 setAttribute(Qt::WA_DeleteOnClose);
23 setLayout(new QGridLayout(this));
24 setWindowTitle(tr("List connections"));
25
26 m_pToolBar = new QToolBar(this);
27 m_pConnect = m_pToolBar->addAction(
28 QIcon::fromTheme("network-wired"), tr("Connect"),
29 this, SLOT(slotConnect()));
30 m_pConnect->setStatusTip(tr("Connect"));
31 m_pConnect->setToolTip(tr("Connect"));
32 m_pEditConnect = m_pToolBar->addAction(
33 QIcon::fromTheme("edit-connect"), tr("Edit and Connect"),
34 this, SLOT(slotEditConnect()));
35 m_pEditConnect->setStatusTip(tr("Edit and Connect"));
36 m_pEditConnect->setToolTip(tr("Edit and Connect"));
37 m_pToolBar->addSeparator();
38
39 m_ptbConnect = new QToolButton(m_pToolBar);
40 m_ptbConnect->setFocusPolicy(Qt::NoFocus);
41 m_ptbConnect->setPopupMode(QToolButton::InstantPopup);
42 //m_ptbConnect->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
43 m_pMenuNew = new QMenu(tr("New"), this);
44 m_pMenuNew->setIcon(QIcon::fromTheme("add"));
45 m_ptbConnect->setMenu(m_pMenuNew);
46 m_ptbConnect->setIcon(m_pMenuNew->icon());
47 m_ptbConnect->setText(tr("New"));
48 m_ptbConnect->setToolTip(tr("New"));
49 m_ptbConnect->setStatusTip(tr("New"));
50 m_pToolBar->addWidget(m_ptbConnect);
51 m_pClient->EnumPlugins(this);
52 m_pEdit = m_pToolBar->addAction(QIcon::fromTheme("edit"), tr("Edit"),
53 this, SLOT(slotEdit()));
54 m_pEdit->setStatusTip(tr("Edit"));
55 m_pEdit->setToolTip(tr("Edit"));
56 m_pCopy = m_pToolBar->addAction(QIcon::fromTheme("edit-copy"), tr("Copy"),
57 this, SLOT(slotCopy()));
58 m_pCopy->setStatusTip(tr("Copy"));
59 m_pCopy->setToolTip(tr("Copy"));
60 m_pDelete = m_pToolBar->addAction(
61 QIcon::fromTheme("edit-delete"), tr("Delete"),
62 this, SLOT(slotDelete()));
63 m_pDelete->setToolTip(tr("Delete"));
64 m_pDelete->setStatusTip(tr("Delete"));
65 m_pDetail = m_pToolBar->addAction(
66 QIcon::fromTheme("dialog-information"), tr("Detail"),
67 this, SLOT(slotDetail()));
68 m_pDetail->setCheckable(true);
69 m_pDetail->setChecked(false);
70 m_pDetail->setToolTip(tr("Detail"));
71 m_pDetail->setStatusTip(tr("Detail"));
72 m_pToolBar->addSeparator();
73 if(m_bDock) {
74 m_pRefresh = m_pToolBar->addAction(
75 QIcon::fromTheme("view-refresh"),
76 tr("Refresh"),
77 this, SLOT(slotLoadFiles()));
78 m_pRefresh->setToolTip(tr("Refresh"));
79 m_pRefresh->setStatusTip(tr("Refresh"));
80 } else {
81 QAction* pClose = m_pToolBar->addAction(
82 QIcon::fromTheme("window-close"), tr("Close"),
83 this, SLOT(close()));
84 pClose->setStatusTip(tr("Close"));
85 pClose->setToolTip(tr("Close"));
86 }
87
88 layout()->addWidget(m_pToolBar);
89
90 if(bDock) {
91 m_pDockTitleBar = new RabbitCommon::CTitleBar(parent);
92 // Create tools pushbutton in title bar
93 QMenu* pMenu = new QMenu(tr("Tools"), m_pDockTitleBar);
94 QPushButton* pTools = m_pDockTitleBar->CreateSmallPushButton(
95 QIcon::fromTheme("tools"), m_pDockTitleBar);
96 pTools->setToolTip(tr("Tools"));
97 pTools->setMenu(pMenu);
98 QList<QWidget*> lstWidget;
99 lstWidget << pTools;
100 m_pDockTitleBar->AddWidgets(lstWidget);
101
102 pMenu->addAction(m_pConnect);
103 pMenu->addAction(m_pEditConnect);
104 pMenu->addMenu(m_pMenuNew);
105 pMenu->addAction(m_pEdit);
106 pMenu->addAction(m_pCopy);
107 pMenu->addAction(m_pDelete);
108 pMenu->addAction(m_pDetail);
109 pMenu->addAction(m_pRefresh);
110 }
111
112 Q_ASSERT(m_pClient);
113 m_pTableView = new QTableView(this);
114 m_pTableView->setContextMenuPolicy(Qt::CustomContextMenu);
115 bool check = connect(m_pTableView,
116 SIGNAL(customContextMenuRequested(const QPoint &)),
117 this, SLOT(slotCustomContextMenu(const QPoint &)));
118 Q_ASSERT(check);
119 check = connect(m_pTableView, SIGNAL(doubleClicked(const QModelIndex &)),
120 this, SLOT(slotDoubleClicked(const QModelIndex&)));
121 Q_ASSERT(check);
122 layout()->addWidget(m_pTableView);;
123
124 m_pModel = new QStandardItemModel(m_pTableView);
125 m_pTableView->setModel(m_pModel);
126 m_pTableView->verticalHeader()->hide();
127 m_pTableView->setSelectionMode(QAbstractItemView::SingleSelection);
128 m_pTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
129 m_pTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
130 m_pModel->setHorizontalHeaderItem(0, new QStandardItem(tr("Name")));
131 m_pModel->setHorizontalHeaderItem(1, new QStandardItem(tr("Protocol")));
132 m_pModel->setHorizontalHeaderItem(2, new QStandardItem(tr("Date")));
133 m_pModel->setHorizontalHeaderItem(3, new QStandardItem(tr("ID")));
134 m_nFileRow = 4;
135 m_pModel->setHorizontalHeaderItem(m_nFileRow, new QStandardItem(tr("File")));
136 if(!m_pDetail->isChecked()) {
137 m_pTableView->hideColumn(3);
138 m_pTableView->hideColumn(m_nFileRow);
139 }
140
141 slotLoadFiles();
142
143 //必须在 setModel 后,才能应用
144 /*第二个参数可以为:
145 QHeaderView::Interactive :0 用户可设置,也可被程序设置成默认大小
146 QHeaderView::Fixed :2 用户不可更改列宽
147 QHeaderView::Stretch :1 根据空间,自动改变列宽,用户与程序不能改变列宽
148 QHeaderView::ResizeToContents:3 根据内容改变列宽,用户与程序不能改变列宽
149 */
150 m_pTableView->horizontalHeader()->setSectionResizeMode(
151#if defined(DEBUG) && !defined(Q_OS_ANDROID)
152 0,
153#endif
154 QHeaderView::ResizeToContents);
155 //以下设置列宽函数必须要数据加载完成后使用,才能应用
156 //See: https://blog.csdn.net/qq_40450386/article/details/86083759
157 //m_pTableView->resizeColumnsToContents(); //设置所有列宽度自适应内容
158 //m_pTableView->resizeColumnToContents(0); //设置第0列宽度自适应内容
159 //m_pTableView->resizeColumnToContents(2); //设置第1列宽度自适应内容
160
161 QItemSelectionModel* pSelect = m_pTableView->selectionModel();
162 QModelIndexList lstIndex;
163 if(pSelect)
164 lstIndex = pSelect->selectedRows();
165 if(m_pModel->rowCount() > 0 && lstIndex.isEmpty())
166 {
167 m_pTableView->selectRow(0);
168 }
169
170}
171
172CFrmListConnects::~CFrmListConnects()
173{
174}
175
176void CFrmListConnects::slotLoadFiles()
177{
178 m_pModel->removeRows(0, m_pModel->rowCount());
179 QString szPath = RabbitCommon::CDir::Instance()->GetDirUserData();
180 QDir dir(szPath);
181 QStringList files = dir.entryList(QStringList() << "*.rrc",
182 QDir::Files, QDir::Time);
183 foreach (auto fileName, files) {
184 QString szFile = dir.absoluteFilePath(fileName);
185 if(szFile.isEmpty()) continue;
186 CConnecter* c = m_pClient->LoadConnecter(szFile);
187 if(!c) continue;
188
189 QList<QStandardItem*> lstItem;
190 QStandardItem* pName = new QStandardItem(c->Icon(), c->Name());
191 pName->setToolTip(c->Description());
192 lstItem << pName;
193 QStandardItem* pProtocol = new QStandardItem(c->Protocol());
194 lstItem << pProtocol;
195 QFileInfo fi(szFile);
196 lstItem << new QStandardItem(fi.lastModified().toLocalTime().toString());
197 QStandardItem* pId = new QStandardItem(c->Id());
198 lstItem << pId;
199 QStandardItem* pFile = new QStandardItem(szFile);
200 lstItem << pFile;
201 m_pModel->appendRow(lstItem);
202
203 c->deleteLater();
204 }
205
206 if(m_pModel->rowCount() > 0)
207 {
208 m_pConnect->setEnabled(true);
209 m_pEditConnect->setEnabled(true);
210 m_pEdit->setEnabled(true);
211 m_pCopy->setEnabled(true);
212 m_pDelete->setEnabled(true);
213 } else {
214 m_pConnect->setEnabled(false);
215 m_pEditConnect->setEnabled(false);
216 m_pEdit->setEnabled(false);
217 m_pCopy->setEnabled(false);
218 m_pDelete->setEnabled(false);
219 }
220
221 return;
222}
223
224int CFrmListConnects::InsertItem(CConnecter *c, QString& szFile)
225{
226 QList<QStandardItem*> lstItem;
227 QStandardItem* pName = new QStandardItem(c->Icon(), c->Name());
228 pName->setToolTip(c->Description());
229 lstItem << pName;
230 QStandardItem* pProtocol = new QStandardItem(c->Protocol());
231 lstItem << pProtocol;
232 QFileInfo fi(szFile);
233 lstItem << new QStandardItem(fi.lastModified().toString());
234 QStandardItem* pId = new QStandardItem(c->Id());
235 lstItem << pId;
236 QStandardItem* pFile = new QStandardItem(szFile);
237 lstItem << pFile;
238 m_pModel->insertRow(0, lstItem);
239 m_pTableView->selectRow(0);
240 return 0;
241}
242
243int CFrmListConnects::onProcess(const QString &id, CPluginClient *pPlug)
244{
245 // Connect menu and toolbar
246 QAction* pAction = m_pMenuNew->addAction(pPlug->Protocol()
247 + ": " + pPlug->DisplayName(),
248 this, SLOT(slotNew()));
249 pAction->setToolTip(pPlug->Description());
250 pAction->setStatusTip(pPlug->Description());
251 pAction->setData(id);
252 pAction->setIcon(pPlug->Icon());
253 return 0;
254}
255
256void CFrmListConnects::slotNew()
257{
258 QAction* pAction = dynamic_cast<QAction*>(this->sender());
259 CConnecter* c = m_pClient->CreateConnecter(pAction->data().toString());
260 if(nullptr == c) return;
261
262 int nRet = c->OpenDialogSettings(this);
263 switch(nRet)
264 {
265 case QDialog::Rejected:
266 break;
267 case QDialog::Accepted:
268 {
269 QString szFile = RabbitCommon::CDir::Instance()->GetDirUserData()
270 + QDir::separator()
271 + c->Id()
272 + ".rrc";
273 QDir d;
274 if(d.exists(szFile)) {
275 QMessageBox::StandardButton r
276 = QMessageBox::warning(
277 this, tr("Warning"),
278 tr("File of connecter is exists. whether to overwrite it? File: %1").arg(szFile),
279 QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::No,
280 QMessageBox::StandardButton::No);
281 if(QMessageBox::StandardButton::Ok == r)
282 {
283 d.remove(szFile);
284 m_pClient->SaveConnecter(szFile, c);
285 }
286 break;
287 }
288
289 m_pClient->SaveConnecter(szFile, c);
290
291 InsertItem(c, szFile);
292
293 break;
294 }
295 }
296
297 c->deleteLater();
298}
299
300void CFrmListConnects::slotEdit()
301{
302 QItemSelectionModel* pSelect = m_pTableView->selectionModel();
303 QModelIndexList lstIndex = pSelect->selectedRows();
304 foreach(auto index, lstIndex)
305 {
306 QString szFile = m_pModel->item(index.row(), m_nFileRow)->text();
307 CConnecter* c = m_pClient->LoadConnecter(szFile);
308 int nRet = c->OpenDialogSettings(this);
309 switch(nRet)
310 {
311 case QDialog::Rejected:
312 break;
313 case QDialog::Accepted:
314 m_pClient->SaveConnecter(szFile, c);
315 break;
316 }
317 c->deleteLater();
318 }
319}
320
321void CFrmListConnects::slotEditConnect()
322{
323 QItemSelectionModel* pSelect = m_pTableView->selectionModel();
324 QModelIndexList lstIndex = pSelect->selectedRows();
325 foreach(auto index, lstIndex)
326 {
327 QString szFile = m_pModel->item(index.row(), m_nFileRow)->text();
328 CConnecter* c = m_pClient->LoadConnecter(szFile);
329 int nRet = c->OpenDialogSettings(this);
330 switch(nRet)
331 {
332 case QDialog::Rejected:
333 break;
334 case QDialog::Accepted:
335 m_pClient->SaveConnecter(szFile, c);
336 emit sigConnect(szFile);
337 break;
338 }
339 c->deleteLater();
340 }
341 if(!m_bDock)
342 close();
343}
344
345void CFrmListConnects::slotCopy()
346{
347 QItemSelectionModel* pSelect = m_pTableView->selectionModel();
348 QModelIndexList lstIndex = pSelect->selectedRows();
349 foreach(auto index, lstIndex)
350 {
351 QString szFile = m_pModel->item(index.row(), m_nFileRow)->text();
352 CConnecter* c = m_pClient->LoadConnecter(szFile);
353
354 do {
355 bool bExit = true;
356 bool bInsert = true;
357 int nRet = c->OpenDialogSettings(this);
358 switch(nRet)
359 {
360 case QDialog::Rejected:
361 break;
362 case QDialog::Accepted:
363 {
364 szFile = RabbitCommon::CDir::Instance()->GetDirUserData()
365 + QDir::separator()
366 + c->Id()
367 + ".rrc";
368 QDir d(szFile);
369 if(d.exists(szFile)) {
370 QMessageBox::StandardButton r
371 = QMessageBox::warning(
372 this,
373 tr("Warning"),
374 tr("File of connecter is exists. whether to overwrite it? "
375 "If select No, please modify the name of connecter"),
376 QMessageBox::StandardButton::Ok
377 | QMessageBox::StandardButton::No
378 | QMessageBox::StandardButton::Cancel,
379 QMessageBox::StandardButton::No);
380 if(QMessageBox::StandardButton::No == r)
381 {
382 bExit = false;
383 break;
384 }
385 if(QMessageBox::StandardButton::Cancel == r)
386 break;
387 bInsert = false;
388 }
389 m_pClient->SaveConnecter(szFile, c);
390 if(bInsert)
391 InsertItem(c, szFile);
392 break;
393 }
394 }
395
396 if(bExit)
397 break;
398 } while(1);
399
400 c->deleteLater();
401 }
402}
403
404void CFrmListConnects::slotDelete()
405{
406 QItemSelectionModel* pSelect = m_pTableView->selectionModel();
407 QModelIndexList lstIndex = pSelect->selectedRows();
408 foreach(auto index, lstIndex)
409 {
410 QString szFile = m_pModel->item(index.row(), m_nFileRow)->text();
411 QDir d(szFile);
412 if(d.remove(szFile))
413 m_pModel->removeRow(index.row());
414 }
415}
416
417void CFrmListConnects::slotConnect()
418{
419 QItemSelectionModel* pSelect = m_pTableView->selectionModel();
420 QModelIndexList lstIndex = pSelect->selectedRows();
421 foreach(auto index, lstIndex)
422 {
423 QString szFile = m_pModel->item(index.row(), m_nFileRow)->text();
424 emit sigConnect(szFile);
425 }
426 if(!m_bDock) close();
427}
428
429void CFrmListConnects::slotDetail()
430{
431 if(m_pDetail->isChecked()) {
432 m_pTableView->showColumn(3);
433 m_pTableView->showColumn(m_nFileRow);
434 } else {
435 m_pTableView->hideColumn(3);
436 m_pTableView->hideColumn(m_nFileRow);
437 }
438}
439
440void CFrmListConnects::slotCustomContextMenu(const QPoint &pos)
441{
442 QMenu menu(this);
443
444 menu.addAction(QIcon::fromTheme("network-wired"),
445 tr("Connect"), this, SLOT(slotConnect()));
446 menu.addAction(tr("Edit and Connect"), this, SLOT(slotEditConnect()));
447 menu.addSeparator();
448 menu.addMenu(m_pMenuNew);
449 menu.addAction(tr("Edit"), this, SLOT(slotEdit()));
450 menu.addAction(QIcon::fromTheme("edit-copy"), tr("Copy"),
451 this, SLOT(slotCopy()));
452 menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"),
453 this, SLOT(slotDelete()));
454 menu.addSeparator();
455 menu.addAction(QIcon::fromTheme("view-refresh"), tr("Refresh"),
456 this, SLOT(slotLoadFiles()));
457
458 menu.exec(mapToGlobal(pos));
459}
460
461void CFrmListConnects::slotDoubleClicked(const QModelIndex& index)
462{
463 QString szFile = m_pModel->item(index.row(), m_nFileRow)->text();
464 emit sigConnect(szFile);
465 if(!m_bDock) close();
466}
管理插件和连接者
Definition Client.h:49
virtual CConnecter * LoadConnecter(const QString &szFile)
从文件中新建 CConnecter 指针,所有者是调用者。 当不再使用时,调用者必须负责调用 DeleteConnecter() 删除此指针。 调用者必须连接信号 CConnecter::sigDisc...
Definition Client.cpp:273
virtual CConnecter * CreateConnecter(const QString &id)
新建 CConnecter 指针,所有者是调用者。 当不在使用时,调用者必调用 DeteleConnecter() 须释放指针。 调用者必须连接信号 CConnecter::sigDisconnecte...
Definition Client.cpp:202
virtual int SaveConnecter(QString szFile, CConnecter *pConnecter)
保存连接参数到文件
Definition Client.cpp:308
连接者应用接口。
Definition Connecter.h:62
virtual const QIcon Icon() const
Icon
Definition Connecter.cpp:74
virtual int OpenDialogSettings(QWidget *parent=nullptr)
打开设置对话框
Definition Connecter.cpp:93
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 const QString Name()
Name
Definition Connecter.cpp:53
virtual int onProcess(const QString &id, CPluginClient *pPlug) override
Process plugins
客户端插件接口。用于建立 CConnecter 实例,它由协议插件实现。
virtual const QString DisplayName() const
在界面上显示的名称
virtual const QString Description() const =0
描述
virtual const QString Protocol() const =0
协议