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