Skip to content
Permalink
Browse files

implemented StorageFavoriteInfoModel.(refs #77)

  • Loading branch information...
haraki committed Jun 8, 2019
1 parent bbefac5 commit 7d34a50a5f6946be86d77592b8e3a308dbc210c2
Showing with 150 additions and 0 deletions.
  1. +2 −0 farman.pro
  2. +77 −0 storagefavoriteinfomodel.cpp
  3. +71 −0 storagefavoriteinfomodel.h
@@ -31,6 +31,7 @@ SOURCES += main.cpp\
selectstoragefavoritedialog.cpp \
sortdialog.cpp \
filterdialog.cpp \
storagefavoriteinfomodel.cpp \
workingdialog.cpp \
worker.cpp \
copyworker.cpp \
@@ -59,6 +60,7 @@ HEADERS += mainwindow.h \
folderview.h \
doublefolderpanel.h \
selectstoragefavoritedialog.h \
storagefavoriteinfomodel.h \
types.h \
sortdialog.h \
filterdialog.h \
@@ -0,0 +1,77 @@
#include <QAbstractListModel>
#include <QStorageInfo>
#include <QDebug>
#include "storagefavoriteinfomodel.h"

namespace Farman
{

StorageFavoriteInfoModel::StorageFavoriteInfoModel(QObject *parent/*= Q_NULLPTR*/)
: QAbstractListModel(parent)
{
initialize();
}

int StorageFavoriteInfoModel::initialize()
{
qDebug() << "StorageFavoriteInfoModel::initialize()";

m_infos.clear();

for(const QStorageInfo& storage : QStorageInfo::mountedVolumes())
{
if(storage.isValid())
{
m_infos.push_back({StorageFavoriteInfo::Type::Storage, storage.displayName(), storage.rootPath()});

qDebug() << "name : " << m_infos.back().getName() << ", path : " << m_infos.back().getPath();
}
}

return 0;
}

int StorageFavoriteInfoModel::rowCount(const QModelIndex &parent/* = Q_NULLPTR*/) const
{
if(parent.isValid())
{
return 0;
}

return m_infos.count();
}

QHash<int, QByteArray> StorageFavoriteInfoModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TypeRole] = "type";
roles[NameRole] = "name";
roles[PathRole] = "path";
return roles;
}

QVariant StorageFavoriteInfoModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid() || index.row() < 0 || index.row() >= m_infos.count())
{
return QVariant();
}

const StorageFavoriteInfo& info = m_infos[index.row()];

switch (role)
{
case Qt::DisplayRole:
return info.getName() + " [" + info.getPath() + "]";
case TypeRole:
return info.getTypeName();
case NameRole:
return info.getName();
case PathRole:
return info.getPath();
}

return QVariant();
}

} // namespace Farman
@@ -0,0 +1,71 @@
#ifndef STORAGEFAVORITEINFOMODEL_H
#define STORAGEFAVORITEINFOMODEL_H

#include <QAbstractListModel>
#include <QStorageInfo>

namespace Farman
{

class StorageFavoriteInfo
{
public:
enum class Type : int
{
Storage,
Favorite,
};

StorageFavoriteInfo(Type type, const QString& name, const QString& path)
{
m_type = type;
m_name = name;
m_path = path;
}

~StorageFavoriteInfo() = default;

Type getType() const { return m_type; }
QString getTypeName() const { return (m_type == Type::Storage) ? "Storage" : (m_type == Type::Favorite) ? "Favorite" : "Unknown"; }
QString getName() const { return m_name; }
QString getPath() const { return m_path; }

private:
StorageFavoriteInfo() = delete;

Type m_type;
QString m_name;
QString m_path;
};

class StorageFavoriteInfoModel : public QAbstractListModel
{
Q_OBJECT

public:
enum Roles
{
TypeRole = Qt::UserRole + 1,
NameRole,
PathRole,
};

explicit StorageFavoriteInfoModel(QObject *parent = Q_NULLPTR);

// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;

protected:
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;

private:
int initialize();

QList<StorageFavoriteInfo> m_infos;
};

} // namespace Farman

#endif // STORAGEFAVORITEINFOMODEL_H

0 comments on commit 7d34a50

Please sign in to comment.
You can’t perform that action at this time.