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