|
| 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 | +} |
0 commit comments