Skip to content

Commit 77c0f57

Browse files
committed
Plugins::FileTransfer: add CRemoteFileModel
1 parent 5ef4c25 commit 77c0f57

8 files changed

Lines changed: 265 additions & 10 deletions

File tree

Plugins/FileTransfer/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SET(FileTransfer_SOURCE_FILES
1010
ParameterFileTransfer.cpp
1111
DlgFileTransfer.cpp
1212
ListFileModel.cpp
13+
RemoteFileModel.cpp
1314
)
1415
SET(FileTransfer_HEADER_FILES
1516
PluginFileTransfer.h
@@ -18,6 +19,7 @@ SET(FileTransfer_HEADER_FILES
1819
ParameterFileTransfer.h
1920
DlgFileTransfer.h
2021
ListFileModel.h
22+
RemoteFileModel.h
2123
)
2224
SET(FileTransfer_UI_FILES
2325
FrmFileTransfer.ui

Plugins/FileTransfer/FrmFileTransfer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ CFrmFileTransfer::CFrmFileTransfer(QWidget *parent)
1414
, ui(new Ui::CFrmFileTransfer)
1515
, m_pModelLocalDir(new QFileSystemModel(this))
1616
, m_pModelLocalFile(new QFileSystemModel(this))
17+
, m_pRemoteModelDir(new CRemoteFileModel(this))
18+
, m_pRemoteModelFile(new CRemoteFileModel(this))
1719
, m_pListFileModel(new CListFileModel(this))
1820
{
1921
bool check = false;
@@ -45,9 +47,11 @@ CFrmFileTransfer::CFrmFileTransfer(QWidget *parent)
4547
ui->tabLocal->verticalHeader()->hide();
4648
ui->tabLocal->horizontalHeader()->setSectionResizeMode(
4749
QHeaderView::ResizeToContents);
48-
50+
51+
ui->treeRemote->setModel(m_pRemoteModelDir);
4952
ui->treeRemote->setContextMenuPolicy(Qt::CustomContextMenu);
5053
ui->treeRemote->setSelectionBehavior(QAbstractItemView::SelectRows);
54+
ui->tabRemote->setModel(m_pRemoteModelFile);
5155
ui->tabRemote->setContextMenuPolicy(Qt::CustomContextMenu);
5256
ui->tabRemote->setSelectionBehavior(QAbstractItemView::SelectRows);
5357

Plugins/FileTransfer/FrmFileTransfer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QWidget>
66
#include <QFileSystemModel>
77
#include "ListFileModel.h"
8+
#include "RemoteFileModel.h"
89

910
namespace Ui {
1011
class CFrmFileTransfer;
@@ -70,5 +71,7 @@ private Q_SLOTS:
7071
QFileSystemModel* m_pModelLocalDir;
7172
QFileSystemModel* m_pModelLocalFile;
7273

74+
CRemoteFileModel* m_pRemoteModelDir;
75+
CRemoteFileModel* m_pRemoteModelFile;
7376
CListFileModel* m_pListFileModel;
7477
};

Plugins/FileTransfer/FrmFileTransfer.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<enum>Qt::Orientation::Vertical</enum>
107107
</property>
108108
<widget class="QTreeView" name="treeRemote"/>
109-
<widget class="QTableWidget" name="tabRemote"/>
109+
<widget class="QTableView" name="tabRemote"/>
110110
</widget>
111111
</item>
112112
</layout>

Plugins/FileTransfer/ListFileModel.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ static Q_LOGGING_CATEGORY(log, "FileTransfer.ListFileModel")
55

66
CListFileModel::CListFileModel(QObject *parent)
77
: QAbstractTableModel(parent)
8-
, m_Columns((int)ColumnValue::End)
9-
, m_Rows(0)
108
{}
119

1210
QVariant CListFileModel::headerData(int section, Qt::Orientation orientation, int role) const
@@ -77,7 +75,7 @@ int CListFileModel::rowCount(const QModelIndex &parent) const
7775
if (parent.isValid())
7876
return 0;
7977

80-
return m_Rows;
78+
return 0;
8179
}
8280

8381
int CListFileModel::columnCount(const QModelIndex &parent) const
@@ -86,7 +84,7 @@ int CListFileModel::columnCount(const QModelIndex &parent) const
8684
if (parent.isValid())
8785
return 0;
8886

89-
return m_Columns;
87+
return (int)ColumnValue::End;
9088
}
9189

9290
QVariant CListFileModel::data(const QModelIndex &index, int role) const

Plugins/FileTransfer/ListFileModel.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class CListFileModel : public QAbstractTableModel
3434
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
3535

3636
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
37-
38-
private:
39-
int m_Columns;
40-
int m_Rows;
4137
};
4238

4339
#endif // LISTFILEMODEL_H
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2+
// Author Kang Lin <kl222@126.com>
3+
4+
#include <QLoggingCategory>
5+
#include "RemoteFileModel.h"
6+
7+
static Q_LOGGING_CATEGORY(log, "RemoteFileModel")
8+
9+
CRemoteFileSystemItem::CRemoteFileSystemItem(
10+
const QString &path, CRemoteFileSystemItem *parent)
11+
: QObject(parent)
12+
{
13+
m_path = path;
14+
m_parentItem = parent;
15+
}
16+
17+
CRemoteFileSystemItem::~CRemoteFileSystemItem() {
18+
qDeleteAll(m_childItems);
19+
}
20+
21+
void CRemoteFileSystemItem::AppendChild(CRemoteFileSystemItem *child) {
22+
m_childItems.append(child);
23+
}
24+
25+
void CRemoteFileSystemItem::RemoveChild(int index) {
26+
m_childItems.removeAt(index);
27+
}
28+
29+
CRemoteFileSystemItem *CRemoteFileSystemItem::Child(int index) {
30+
return m_childItems.value(index);
31+
}
32+
33+
int CRemoteFileSystemItem::ChildCount() const {
34+
return m_childItems.count();
35+
}
36+
37+
int CRemoteFileSystemItem::ColumnCount() const {
38+
return 4;
39+
}
40+
41+
QVariant CRemoteFileSystemItem::Data() const {
42+
return m_path;
43+
}
44+
45+
int CRemoteFileSystemItem::Index() const {
46+
if (m_parentItem)
47+
return m_parentItem->m_childItems.indexOf(const_cast<CRemoteFileSystemItem*>(this));
48+
return 0;
49+
}
50+
51+
CRemoteFileSystemItem *CRemoteFileSystemItem::Parent() {
52+
return m_parentItem;
53+
}
54+
55+
CRemoteFileModel::CRemoteFileModel(QObject *parent)
56+
: QAbstractItemModel(parent)
57+
{
58+
qDebug(log) << Q_FUNC_INFO;
59+
}
60+
61+
CRemoteFileModel::~CRemoteFileModel()
62+
{
63+
qDebug(log) << Q_FUNC_INFO;
64+
}
65+
66+
QVariant CRemoteFileModel::headerData(int section, Qt::Orientation orientation, int role) const
67+
{
68+
switch((ColumnValue)section) {
69+
case ColumnValue::Name:
70+
switch(role) {
71+
case Qt::DisplayRole:
72+
return tr("File name");
73+
}
74+
case ColumnValue::Size: {
75+
switch(role) {
76+
case Qt::DisplayRole:
77+
return tr("File size");
78+
}
79+
}
80+
case ColumnValue::Type: {
81+
switch(role) {
82+
case Qt::DisplayRole:
83+
return tr("File type");
84+
}
85+
}
86+
case ColumnValue::LastModified: {
87+
switch(role) {
88+
case Qt::DisplayRole:
89+
return tr("Last modified time");
90+
}
91+
}
92+
case ColumnValue::Privileges: {
93+
switch(role) {
94+
case Qt::DisplayRole:
95+
return tr("Privileges");
96+
}
97+
}
98+
case ColumnValue::Ower: {
99+
switch(role) {
100+
case Qt::DisplayRole:
101+
return tr("Ower/Group");
102+
}
103+
}
104+
}
105+
return QVariant();
106+
}
107+
108+
QModelIndex CRemoteFileModel::index(int row, int column, const QModelIndex &parent) const
109+
{
110+
qDebug(log) << Q_FUNC_INFO << parent;
111+
if (!hasIndex(row, column, parent))
112+
return QModelIndex();
113+
CRemoteFileSystemItem *parentItem;
114+
if (!parent.isValid() || !indexValid(parent))
115+
parentItem = m_rootItem;
116+
else
117+
parentItem = static_cast<CRemoteFileSystemItem*>(parent.internalPointer());
118+
if(parentItem == nullptr)
119+
return QModelIndex();
120+
CRemoteFileSystemItem *childItem = parentItem->Child(row);
121+
if (childItem)
122+
return createIndex(row, column, childItem);
123+
else
124+
return QModelIndex();
125+
}
126+
127+
QModelIndex CRemoteFileModel::parent(const QModelIndex &index) const
128+
{
129+
qDebug(log) << Q_FUNC_INFO << index;
130+
if(m_rootItem == nullptr)
131+
return QModelIndex();
132+
if (!index.isValid())
133+
return QModelIndex();
134+
if(!indexValid(index))
135+
return QModelIndex();
136+
CRemoteFileSystemItem *childItem = static_cast<CRemoteFileSystemItem*>(index.internalPointer());
137+
CRemoteFileSystemItem *parentItem = childItem->Parent();
138+
if(parentItem == nullptr)
139+
return QModelIndex();
140+
if (parentItem == m_rootItem)
141+
return QModelIndex();
142+
return createIndex(parentItem->Index(), 0, parentItem);
143+
}
144+
145+
int CRemoteFileModel::rowCount(const QModelIndex &parent) const
146+
{
147+
qDebug(log) << Q_FUNC_INFO << parent;
148+
CRemoteFileSystemItem *parentItem;
149+
if (parent.column() > 0)
150+
return 0;
151+
if (!parent.isValid() || !indexValid(parent))
152+
parentItem = m_rootItem;
153+
else
154+
parentItem = static_cast<CRemoteFileSystemItem*>(parent.internalPointer());
155+
if(parentItem == nullptr)
156+
return 0;
157+
return parentItem->ChildCount();
158+
}
159+
160+
int CRemoteFileModel::columnCount(const QModelIndex &parent) const
161+
{
162+
qDebug(log) << Q_FUNC_INFO << parent;
163+
if (!parent.isValid())
164+
return 0;
165+
166+
return (int)ColumnValue::End;
167+
}
168+
169+
QVariant CRemoteFileModel::data(const QModelIndex &index, int role) const
170+
{
171+
qDebug(log) << Q_FUNC_INFO << index << role;
172+
if (!index.isValid())
173+
return QVariant();
174+
175+
// FIXME: Implement me!
176+
return QVariant();
177+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2+
// Author Kang Lin <kl222@126.com>
3+
4+
#pragma once
5+
6+
#include <QAbstractItemModel>
7+
#include <QDateTime>
8+
9+
class CRemoteFileSystemItem : public QObject {
10+
Q_OBJECT
11+
public:
12+
explicit CRemoteFileSystemItem(const QString &path,
13+
CRemoteFileSystemItem *parent = nullptr);
14+
virtual ~CRemoteFileSystemItem();
15+
16+
void AppendChild(CRemoteFileSystemItem *child);
17+
void RemoveChild(int index);
18+
19+
CRemoteFileSystemItem *Child(int index);
20+
int ChildCount() const;
21+
int ColumnCount() const;
22+
QVariant Data() const;
23+
int Index() const;
24+
CRemoteFileSystemItem *Parent();
25+
void SetSize(uint64_t size) { m_size = size; }
26+
uint64_t Size() const { return m_size; }
27+
void SetLastModified(QDateTime lastModified) { m_lastModified = lastModified; }
28+
QDateTime LastModified() const { return m_lastModified; }
29+
void SetIsDir(bool isDir) { m_isDir = isDir; }
30+
bool IsDir() const { return m_isDir; }
31+
32+
private:
33+
QList<CRemoteFileSystemItem*> m_childItems;
34+
QString m_path;
35+
uint64_t m_size = 0;
36+
bool m_isDir = false;
37+
QDateTime m_lastModified;
38+
CRemoteFileSystemItem *m_parentItem;
39+
};
40+
41+
class CRemoteFileModel : public QAbstractItemModel
42+
{
43+
Q_OBJECT
44+
45+
public:
46+
explicit CRemoteFileModel(QObject *parent = nullptr);
47+
virtual ~CRemoteFileModel();
48+
49+
enum class ColumnValue {
50+
Name = 0,
51+
Type,
52+
Size,
53+
LastModified,
54+
Privileges,
55+
Ower,
56+
End
57+
};
58+
Q_ENUM(ColumnValue)
59+
60+
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
61+
62+
inline bool indexValid(const QModelIndex &index) const {
63+
return (index.row() >= 0) && (index.column() >= 0) && (index.model() == this);
64+
}
65+
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
66+
QModelIndex parent(const QModelIndex &index) const override;
67+
68+
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
69+
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
70+
71+
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
72+
73+
private:
74+
CRemoteFileSystemItem *m_rootItem = nullptr;
75+
};

0 commit comments

Comments
 (0)