Skip to content

Commit 8ed2d20

Browse files
committed
App: modify file in database
1 parent 474de89 commit 8ed2d20

10 files changed

Lines changed: 162 additions & 70 deletions

File tree

App/Client/Favorite/FavoriteView.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,16 @@ void CFavoriteView::EnableAction(const QModelIndex &index)
249249
m_pDeleteAction->setEnabled(false);
250250
}
251251

252-
void CFavoriteView::slotAddToFavorite(const QString &szFile,
252+
void CFavoriteView::slotAddToFavorite(const QString &file,
253253
const QString &szName,
254254
const QString &szDescription,
255255
const QIcon &icon
256256
)
257257
{
258258
if(!m_pModel || !m_pTreeView) return;
259259

260+
QString szFile = SetFile(file);
261+
260262
int parentId = 0;
261263
QString szGroup = tr("Root");
262264
auto indexes = m_pTreeView->selectionModel()->selectedIndexes();
@@ -301,7 +303,7 @@ void CFavoriteView::slotUpdateFavorite(
301303
const QString &szDescription, const QIcon &icon)
302304
{
303305
if(!m_pModel || !m_pTreeView || szFile.isEmpty()) return;
304-
m_pModel->UpdateFavorite(szFile, szName, szDescription, icon);
306+
m_pModel->UpdateFavorite(SetFile(szFile), szName, szDescription, icon);
305307
}
306308

307309
void CFavoriteView::slotFavrtieClicked(const QModelIndex &index)
@@ -316,7 +318,7 @@ void CFavoriteView::slotFavortiedoubleClicked(const QModelIndex &index)
316318
return;
317319
QString szFile = m_pModel->data(index, CFavoriteModel::RoleFile).toString();
318320
if(!szFile.isEmpty())
319-
emit sigStart(szFile, false);
321+
emit sigStart(GetFile(szFile), false);
320322
}
321323

322324
void CFavoriteView::slotDoubleEditNode(bool bEdit)
@@ -365,7 +367,7 @@ void CFavoriteView::slotStart()
365367
{
366368
QString szFile = m_pModel->data(index, CFavoriteModel::RoleFile).toString();
367369
if(!szFile.isEmpty())
368-
emit sigStart(szFile, false);
370+
emit sigStart(GetFile(szFile), false);
369371
}
370372
}
371373

@@ -376,7 +378,7 @@ void CFavoriteView::slotOpenStart()
376378
{
377379
QString szFile = m_pModel->data(index, CFavoriteModel::RoleFile).toString();
378380
if(!szFile.isEmpty())
379-
emit sigStart(szFile, true);
381+
emit sigStart(GetFile(szFile), true);
380382
}
381383
}
382384

@@ -598,3 +600,26 @@ bool CFavoriteView::eventFilter(QObject *watched, QEvent *event)
598600
{
599601
return QWidget::eventFilter(watched, event);
600602
}
603+
604+
QString CFavoriteView::GetFile(const QString szFile)
605+
{
606+
QFileInfo fi(szFile);
607+
//qDebug(log) << szFile << fi.absolutePath();
608+
if(fi.isRelative()) {
609+
return RabbitCommon::CDir::Instance()->GetDirUserData()
610+
+ QDir::separator() + szFile;
611+
}
612+
return szFile;
613+
}
614+
615+
QString CFavoriteView::SetFile(const QString file)
616+
{
617+
QString szFile;
618+
QFileInfo fi(file);
619+
QFileInfo d(RabbitCommon::CDir::Instance()->GetDirUserData() + QDir::separator());
620+
if(fi.absolutePath() == d.absolutePath())
621+
szFile = fi.fileName();
622+
else
623+
szFile = file;
624+
return szFile;
625+
}

App/Client/Favorite/FavoriteView.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include "FavoriteModel.h"
1111
#include "ParameterApp.h"
1212

13+
/*!
14+
* \brief The CFavoriteView class
15+
* \note The file is exist in RabbitCommon::CDir::Instance()->GetDirUserData(),
16+
* then remove the path, only save file name. other save the file path.
17+
*/
1318
class CFavoriteView : public QWidget
1419
{
1520
Q_OBJECT
@@ -65,6 +70,8 @@ private slots:
6570
void setupToolBar(QLayout* layout);
6671
void setupTreeView(QLayout* layout);
6772
void EnableAction(const QModelIndex& index = QModelIndex());
73+
QString GetFile(const QString szFile);
74+
QString SetFile(const QString szFile);
6875

6976
private:
7077
CParameterApp* m_pParaApp;

App/Client/Recent/FrmRecent.cpp

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
static Q_LOGGING_CATEGORY(log, "App.FrmListRecent")
1717

1818
CFrmRecent::CFrmRecent(
19-
MainWindow *pMainWindow, CManager *pManager, CRecentDatabase *pDb,
19+
MainWindow *pMainWindow, CManager *pManager,
2020
CParameterApp &parameterApp, bool bDock, QWidget *parent)
2121
: QWidget(parent)
2222
, m_pMainWindow(pMainWindow)
2323
, m_ParameterApp(parameterApp)
2424
, m_pToolBar(nullptr)
2525
, m_ptbOperate(nullptr)
2626
, m_pMenuNew(nullptr)
27-
, m_pDatabase(pDb)
2827
, m_pModel(nullptr)
2928
, m_pManager(pManager)
3029
, m_bDock(bDock)
@@ -167,8 +166,6 @@ CFrmRecent::CFrmRecent(
167166
Q_ASSERT(check);
168167
layout()->addWidget(m_pTableView);
169168

170-
m_pModel = new CRecentModel(&m_ParameterApp, m_pDatabase, m_pTableView);
171-
m_pTableView->setModel(m_pModel);
172169
m_pTableView->verticalHeader()->hide();
173170
m_pTableView->setSelectionMode(QAbstractItemView::SingleSelection);
174171
m_pTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
@@ -177,43 +174,62 @@ CFrmRecent::CFrmRecent(
177174
m_pTableView->hideColumn(CRecentModel::ColumnNo::ID);
178175
m_pTableView->hideColumn(CRecentModel::ColumnNo::File);
179176
}
180-
181-
check = connect(m_pDatabase, &CRecentDatabase::sigChanged,
182-
this, &CFrmRecent::slotRefresh);
183-
Q_ASSERT(check);
177+
}
178+
179+
CFrmRecent::~CFrmRecent()
180+
{
181+
}
184182

185-
// if(!bDock)
186-
// slotLoadFiles();
187-
188-
//必须在 setModel 后,才能应用
189-
/*第二个参数可以为:
190-
QHeaderView::Interactive :0 用户可设置,也可被程序设置成默认大小
191-
QHeaderView::Fixed :2 用户不可更改列宽
192-
QHeaderView::Stretch :1 根据空间,自动改变列宽,用户与程序不能改变列宽
193-
QHeaderView::ResizeToContents:3 根据内容改变列宽,用户与程序不能改变列宽
194-
*/
195-
m_pTableView->horizontalHeader()->setSectionResizeMode(
183+
int CFrmRecent::Init()
184+
{
185+
if(!m_Database.IsOpen()) {
186+
m_Database.OpenDatabase(&m_ParameterApp.m_Database, "recent_connection");
187+
}
188+
m_pModel = new CRecentModel(&m_ParameterApp, &m_Database, m_pTableView);
189+
m_pTableView->setModel(m_pModel);
190+
if(m_pTableView->horizontalHeader()) {
191+
// check = connect(&m_Database, &CRecentDatabase::sigChanged,
192+
// this, &CFrmRecent::slotRefresh);
193+
// Q_ASSERT(check);
194+
//必须在 setModel 后,才能应用
195+
/*第二个参数可以为:
196+
QHeaderView::Interactive :0 用户可设置,也可被程序设置成默认大小
197+
QHeaderView::Fixed :2 用户不可更改列宽
198+
QHeaderView::Stretch :1 根据空间,自动改变列宽,用户与程序不能改变列宽
199+
QHeaderView::ResizeToContents:3 根据内容改变列宽,用户与程序不能改变列宽
200+
*/
201+
m_pTableView->horizontalHeader()->setSectionResizeMode(
196202
#if defined(DEBUG) && !defined(Q_OS_ANDROID)
197-
0,
203+
0,
198204
#endif
199-
QHeaderView::Interactive);
205+
QHeaderView::Interactive);
206+
}
200207
//以下设置列宽函数必须要数据加载完成后使用,才能应用
201208
//See: https://blog.csdn.net/qq_40450386/article/details/86083759
202209
//m_pTableView->resizeColumnsToContents(); //设置所有列宽度自适应内容
203-
//m_pTableView->resizeColumnToContents(0); //设置第0列宽度自适应内容
204-
//m_pTableView->resizeColumnToContents(2); //设置第1列宽度自适应内容
205-
//m_pTableView->resizeColumnToContents(3); //设置第1列宽度自适应内容
210+
211+
m_pManager->EnumPlugins(this);
212+
slotRefresh();
213+
return 0;
206214
}
207215

208-
CFrmRecent::~CFrmRecent()
216+
int CFrmRecent::AddRecent(const CRecentDatabase::RecentItem &item)
209217
{
218+
int nRet = m_Database.AddRecent(item);
219+
slotRefresh();
220+
return nRet;
210221
}
211222

212-
int CFrmRecent::Init()
223+
bool CFrmRecent::UpdateRecent(const CRecentDatabase::RecentItem &item)
213224
{
214-
m_pManager->EnumPlugins(this);
225+
bool bRet = m_Database.UpdateRecent(item);
215226
slotRefresh();
216-
return 0;
227+
return bRet;
228+
}
229+
230+
QList<CRecentDatabase::RecentItem> CFrmRecent::GetRecents(int limit, int offset)
231+
{
232+
return m_Database.GetRecents(limit, offset);
217233
}
218234

219235
void CFrmRecent::slotRefresh()
@@ -243,7 +259,7 @@ int CFrmRecent::InsertItem(COperate *c, QString& szFile)
243259
item.szName = c->Name();
244260
item.szProtocol = c->Protocol();
245261
item.szType = c->GetTypeName();
246-
item.szFile = szFile;
262+
item.SetFile(szFile);
247263
m_pModel->addItem(item);
248264

249265
m_pTableView->selectRow(0);
@@ -341,7 +357,7 @@ void CFrmRecent::slotEdit()
341357
foreach(auto index, lstIndex)
342358
{
343359
auto item = m_pModel->getItem(index);
344-
QString szFile = item.szFile;
360+
QString szFile = item.GetFile();
345361
if(szFile.isEmpty()) continue;
346362
COperate* pOperate = m_pManager->LoadOperate(szFile);
347363
int nRet = pOperate->OpenDialogSettings(this);
@@ -364,7 +380,7 @@ void CFrmRecent::slotEditConnect()
364380
foreach(auto index, lstIndex)
365381
{
366382
auto item = m_pModel->getItem(index);
367-
QString szFile = item.szFile;
383+
QString szFile = item.GetFile();
368384
if(szFile.isEmpty()) continue;
369385
COperate* c = m_pManager->LoadOperate(szFile);
370386
int nRet = c->OpenDialogSettings(this);
@@ -390,7 +406,7 @@ void CFrmRecent::slotCopy()
390406
foreach(auto index, lstIndex)
391407
{
392408
auto item = m_pModel->getItem(index);
393-
QString szFile = item.szFile;
409+
QString szFile = item.GetFile();
394410
if(szFile.isEmpty()) continue;
395411
COperate* pOperate = m_pManager->LoadOperate(szFile);
396412

@@ -477,7 +493,7 @@ void CFrmRecent::slotStart()
477493
foreach(auto index, lstIndex)
478494
{
479495
auto item = m_pModel->getItem(index);
480-
QString szFile = item.szFile;
496+
QString szFile = item.GetFile();
481497
if(szFile.isEmpty()) continue;
482498
emit sigStart(szFile);
483499
}
@@ -523,7 +539,7 @@ void CFrmRecent::slotDoubleClicked(const QModelIndex& index)
523539
if(!m_pModel) return;
524540
if(!index.isValid()) return;
525541
auto item = m_pModel->getItem(index);
526-
QString szFile = item.szFile;
542+
QString szFile = item.GetFile();
527543
if(szFile.isEmpty()) return;
528544
emit sigStart(szFile);
529545
if(!m_bDock) close();
@@ -537,12 +553,12 @@ void CFrmRecent::slotAddToFavorite()
537553
{
538554
if(!index.isValid()) continue;
539555
auto item = m_pModel->getItem(index);
540-
QFileInfo fi(item.szFile);
541-
if(0 == item.id || item.szFile.isEmpty() || !fi.exists()) continue;
556+
QFileInfo fi(item.GetFile());
557+
if(0 == item.id || item.GetFile().isEmpty() || !fi.exists()) continue;
542558
QString szName = item.szName;
543559
QIcon icon = item.icon;
544560
QString szDescription = item.szDescription;
545-
QString szFile = item.szFile;
561+
QString szFile = item.GetFile();
546562
emit sigAddToFavorite(szFile, szName, szDescription, icon);
547563
}
548564
}
@@ -557,7 +573,7 @@ void CFrmRecent::slotImport()
557573
if (!filename.isEmpty()) {
558574
QFileInfo fi(filename);
559575
if(0 == fi.suffix().compare("json", Qt::CaseInsensitive)) {
560-
if (m_pDatabase->ImportFromJsonFile(filename)) {
576+
if (m_Database.ImportFromJsonFile(filename)) {
561577
slotRefresh();
562578
QMessageBox::information(
563579
this, tr("Success"),
@@ -582,7 +598,7 @@ void CFrmRecent::slotExport()
582598
if (!filename.isEmpty()) {
583599
QFileInfo fi(filename);
584600
if(0 == fi.suffix().compare("json", Qt::CaseInsensitive)) {
585-
if (m_pDatabase->ExportToJsonFile(filename)) {
601+
if (m_Database.ExportToJsonFile(filename)) {
586602
QMessageBox::information(
587603
this, tr("Success"),
588604
tr("Successfully exported recent to json file"));

App/Client/Recent/FrmRecent.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CFrmRecent : public QWidget, CManager::Handle
2525

2626
public:
2727
explicit CFrmRecent(MainWindow* pMainWindow, CManager* pManager,
28-
CRecentDatabase* pDb, CParameterApp &parameterApp,
28+
CParameterApp &parameterApp,
2929
bool bDock = false, QWidget *parent = nullptr);
3030
virtual ~CFrmRecent();
3131

@@ -35,6 +35,9 @@ class CFrmRecent : public QWidget, CManager::Handle
3535
RabbitCommon::CTitleBar* m_pDockTitleBar;
3636

3737
int Init();
38+
int AddRecent(const CRecentDatabase::RecentItem &item);
39+
bool UpdateRecent(const CRecentDatabase::RecentItem &item);
40+
QList<CRecentDatabase::RecentItem> GetRecents(int limit = -1, int offset = 0);
3841

3942
public Q_SLOTS:
4043
void slotRefresh();
@@ -84,7 +87,7 @@ private slots:
8487
QAction* m_pImport;
8588

8689
QTableView* m_pTableView;
87-
CRecentDatabase* m_pDatabase;
90+
CRecentDatabase m_Database;
8891
CRecentModel* m_pModel;
8992
CManager* m_pManager;
9093
bool m_bDock;

App/Client/Recent/RecentDatabase.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515
#include "RabbitCommonDir.h"
1616

1717
static Q_LOGGING_CATEGORY(log, "App.Recent.Db")
18+
19+
CRecentDatabase::RecentItem::RecentItem() : id(0) {
20+
}
21+
22+
int CRecentDatabase::RecentItem::SetFile(const QString& file) {
23+
QFileInfo fi(file);
24+
QFileInfo d(RabbitCommon::CDir::Instance()->GetDirUserData() + QDir::separator());
25+
//qDebug(log) << "\n" << fi.path() << "\n" << d.path() << "\n" << fi.absoluteFilePath() << "\n" << d.absoluteFilePath();
26+
if(fi.absolutePath() == d.absolutePath())
27+
szFile = fi.fileName();
28+
else
29+
szFile = file;
30+
return 0;
31+
}
32+
33+
QString CRecentDatabase::RecentItem::GetFile() {
34+
QFileInfo fi(szFile);
35+
//qDebug(log) << szFile << fi.absolutePath();
36+
if(fi.isRelative()) {
37+
return RabbitCommon::CDir::Instance()->GetDirUserData()
38+
+ QDir::separator() + szFile;
39+
}
40+
return szFile;
41+
}
42+
1843
CRecentDatabase::CRecentDatabase(QObject *parent)
1944
: CDatabase{parent}
2045
{
@@ -226,6 +251,11 @@ int CRecentDatabase::AddRecent(const RecentItem &item)
226251
return query.lastInsertId().toInt();
227252
}
228253

254+
bool CRecentDatabase::UpdateRecent(const RecentItem& item)
255+
{
256+
return UpdateRecent(item.szFile, item.szName, item.szDescription);
257+
}
258+
229259
bool CRecentDatabase::UpdateRecent(
230260
const QString &szFile, const QString& szName, const QString& szDescription)
231261
{
@@ -243,7 +273,8 @@ bool CRecentDatabase::UpdateRecent(
243273

244274
bool success = query.exec();
245275
if (!success) {
246-
qCritical(log) << "Failed to update recent:" << query.lastError().text();
276+
qCritical(log) << "Failed to update recent:" << query.lastError().text()
277+
<< "Sql:" << query.executedQuery();
247278
}
248279
emit sigChanged();
249280
return success;

0 commit comments

Comments
 (0)