Skip to content
Permalink
Browse files

improved BookmarkInfoModel.(refs #79)

  • Loading branch information...
haraki committed Jun 15, 2019
1 parent d69968a commit 16ec5fa07b6699cb04c58becfd8b3ef91fa1cd96
Showing with 164 additions and 89 deletions.
  1. +4 −2 bookmarkinfo.h
  2. +135 −61 bookmarkinfomodel.cpp
  3. +25 −26 bookmarkinfomodel.h
@@ -23,16 +23,18 @@ class BookmarkInfo
m_path = path;
}

BookmarkInfo() = default;
~BookmarkInfo() = default;

void setType(Type type) { m_type = type; }
Type getType() const { return m_type; }
QString getTypeName() const { return (m_type == Type::System) ? "System" : (m_type == Type::User) ? "User" : "Unknown"; }
void setName(const QString& name) { m_name = name; }
QString getName() const { return m_name; }
void setPath(const QString& path) { m_path = path; }
QString getPath() const { return m_path; }

private:
BookmarkInfo() = delete;

Type m_type;
QString m_name;
QString m_path;
@@ -6,47 +6,58 @@
namespace Farman
{

BookmarkInfoModel::BookmarkInfoModel(QObject *parent)
BookmarkInfoModel::BookmarkInfoModel(bool system/* = true*/, bool showIcon/* = true*/, QObject *parent/* = Q_NULLPTR*/)
: QAbstractTableModel(parent)
, m_fileSystemModel(new QFileSystemModel(this))
, m_showIcon(showIcon)
, m_bookmarkIcon(new QIcon(":/images/bookmark_on.svg"))
{
initialize();
initialize(system);
}

int BookmarkInfoModel::initialize()
BookmarkInfoModel::~BookmarkInfoModel()
{
qDebug() << "BookmarkInfoModel::initialize()";
delete m_bookmarkIcon;
delete m_fileSystemModel;
}

int BookmarkInfoModel::initialize(bool system)
{
qDebug() << "BookmarkInfoModel::initialize() : system : " << system;

m_infos.clear();

for(const QStorageInfo& volume : QStorageInfo::mountedVolumes())
{
qDebug() << "Name : " << volume.name();
qDebug() << "Display Name : " << volume.displayName();
qDebug() << "Device:" << volume.device();
qDebug() << "Root path:" << volume.rootPath();
qDebug() << "File system type:" << volume.fileSystemType();
qDebug() << "Is valid" << volume.isValid();
qDebug() << "Is root" << volume.isRoot();
qDebug() << "Is ready" << volume.isReady();
qDebug() << "Is read only" << volume.isReadOnly();
qDebug() << QString("Bytes available: %L1 Bytes").arg(volume.bytesAvailable());
qDebug() << QString("Bytes free: %L1 Bytes").arg(volume.bytesFree());
qDebug() << QString("Bytes total: %L1 Bytes").arg(volume.bytesTotal()) << endl;

if(volume.isValid() && volume.isReady()
if(system)
{
for(const QStorageInfo& volume : QStorageInfo::mountedVolumes())
{
qDebug() << "Name : " << volume.name();
qDebug() << "Display Name : " << volume.displayName();
qDebug() << "Device:" << volume.device();
qDebug() << "Root path:" << volume.rootPath();
qDebug() << "File system type:" << volume.fileSystemType();
qDebug() << "Is valid" << volume.isValid();
qDebug() << "Is root" << volume.isRoot();
qDebug() << "Is ready" << volume.isReady();
qDebug() << "Is read only" << volume.isReadOnly();
qDebug() << QString("Bytes available: %L1 Bytes").arg(volume.bytesAvailable());
qDebug() << QString("Bytes free: %L1 Bytes").arg(volume.bytesFree());
qDebug() << QString("Bytes total: %L1 Bytes").arg(volume.bytesTotal()) << endl;

if(volume.isValid() && volume.isReady()
#ifdef Q_OS_LINUX
&& (volume.isRoot() || volume.rootPath().startsWith("/media"))
&& (volume.isRoot() || volume.rootPath().startsWith("/media"))
#endif
)
{
m_infos.push_back({BookmarkInfo::Type::System,
)
{
m_infos.push_back({BookmarkInfo::Type::System,
#ifdef Q_OS_LINUX
volume.device(),
volume.device(),
#else
volume.displayName(),
volume.displayName(),
#endif
volume.rootPath()});
volume.rootPath()});
}
}
}

@@ -76,17 +87,7 @@ QVariant BookmarkInfoModel::headerData(int section, Qt::Orientation orientation,

return QVariant();
}
#if 0
bool BookmarkInfoModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (value != headerData(section, orientation, role)) {
// FIXME: Implement me!
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
#endif

int BookmarkInfoModel::rowCount(const QModelIndex &parent) const
{
if(parent.isValid())
@@ -129,9 +130,16 @@ QVariant BookmarkInfoModel::data(const QModelIndex &index, int role) const
}
break;
case Qt::DecorationRole:
if(index.column() == 0)
if(m_showIcon && index.column() == 0)
{
return m_fileSystemModel->fileIcon(m_fileSystemModel->index(info.getPath()));
if(info.getType() == BookmarkInfo::Type::System)
{
return m_fileSystemModel->fileIcon(m_fileSystemModel->index(info.getPath()));
}
else if (info.getType() == BookmarkInfo::Type::User)
{
return *m_bookmarkIcon;
}
}
break;
case TypeRole:
@@ -144,11 +152,27 @@ QVariant BookmarkInfoModel::data(const QModelIndex &index, int role) const

return QVariant();
}
#if 0

bool BookmarkInfoModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value) {
// FIXME: Implement me!
qDebug() << "BookmarkInfoModel::setData() : row : " << index.row() << ", column : " << index.column() << ", value : " << value;

if(!index.isValid() || (index.column() > 1) || role != Qt::EditRole)
{
return false;
}

if(data(index, role) != value)
{
if(index.column() == 0)
{
m_infos[index.row()].setName(value.toString());
}
else // if(index.column() == 1)
{
m_infos[index.row()].setPath(value.toString());
}

emit dataChanged(index, index, QVector<int>() << role);
return true;
}
@@ -157,40 +181,90 @@ bool BookmarkInfoModel::setData(const QModelIndex &index, const QVariant &value,

Qt::ItemFlags BookmarkInfoModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
Qt::ItemFlags flag = QAbstractTableModel::flags(index);
if(!index.isValid())
{
flag |= Qt::ItemIsEditable;
}

return Qt::ItemIsEditable; // FIXME: Implement me!
return flag;
}

bool BookmarkInfoModel::insertRows(int row, int count, const QModelIndex &parent)
{
beginInsertRows(parent, row, row + count - 1);
// FIXME: Implement me!
Q_UNUSED(parent);

beginInsertRows(QModelIndex(), row, row + count - 1);

for(int i = 0;i < count;i++)
{
m_infos.insert(row + i, BookmarkInfo(BookmarkInfo::Type::User, "", ""));
}

endInsertRows();
}

bool BookmarkInfoModel::insertColumns(int column, int count, const QModelIndex &parent)
{
beginInsertColumns(parent, column, column + count - 1);
// FIXME: Implement me!
endInsertColumns();
return true;
}

bool BookmarkInfoModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row + count - 1);
// FIXME: Implement me!
Q_UNUSED(parent);

beginRemoveRows(QModelIndex(), row, row + count - 1);

for(;count > 0;count--)
{
m_infos.removeAt(row);
}

endRemoveRows();

return true;
}

bool BookmarkInfoModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
const QModelIndex &destinationParent, int destinationChild)
{
Q_UNUSED(sourceParent);
Q_UNUSED(destinationParent);

if(destinationChild >= sourceRow && destinationChild <= sourceRow + count - 1)
{
return false;
}

if(!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
{
return false;
}

for(int i = 0;i < count;i++)
{
m_infos.insert(destinationChild + i, m_infos[sourceRow]);

int removeIndex = (destinationChild > sourceRow) ? sourceRow : sourceRow + 1;

m_infos.removeAt(removeIndex);
}

endMoveRows();

return true;
}

bool BookmarkInfoModel::removeColumns(int column, int count, const QModelIndex &parent)
int BookmarkInfoModel::saveToSettings()
{
beginRemoveColumns(parent, column, column + count - 1);
// FIXME: Implement me!
endRemoveColumns();
QList<QPair<QString, QString>> dirPathList;
for(auto& info : m_infos)
{
dirPathList.push_back({info.getName(), info.getPath()});
}

Settings::getInstance()->setBookmarkDirPathList(dirPathList);

return 0;
}
#endif

QHash<int, QByteArray> BookmarkInfoModel::roleNames() const
{
QHash<int, QByteArray> roles;
@@ -20,42 +20,41 @@ class BookmarkInfoModel : public QAbstractTableModel
PathRole,
};

explicit BookmarkInfoModel(QObject *parent = nullptr);
explicit BookmarkInfoModel(bool system = true, bool icon = true, QObject *parent = Q_NULLPTR);
~BookmarkInfoModel() Q_DECL_OVERRIDE;

// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

#if 0
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
#endif

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

QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
#if 0
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;

// Add data:
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;

// Remove data:
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
#endif
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;

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

bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
Qt::ItemFlags flags(const QModelIndex& index) const Q_DECL_OVERRIDE;

bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;

bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;

bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
const QModelIndex &destinationParent, int destinationChild) Q_DECL_OVERRIDE;

// 自身が保持するブックマーク情報を Settings のインスタンスに反映する
// ファイル(settings.ini)への保存はアプリ終了時に行われる
int saveToSettings();

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

private:
int initialize();
int initialize(bool system);

QList<BookmarkInfo> m_infos;
QFileSystemModel* m_fileSystemModel;

bool m_showIcon;
QIcon* m_bookmarkIcon;
};

} // namespace Farman

0 comments on commit 16ec5fa

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