diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2017-09-20 11:36:42 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2017-09-20 12:19:39 +0000 |
commit | 29b38bea45712c85a0c9f07bfa26524f3a35668f (patch) | |
tree | 0d8fc2d67444cc4dfe8cb1e12156059cf357b895 /examples/widgets/itemviews/storageview/storagemodel.cpp | |
parent | bf41fbb233d65b6cc19837da038e2672bb34f562 (diff) |
Polish the StorageView example
Introduce nullptr and override. Change the ready/valid columns to
use check marks. Right-align the numerical columns. Add a shortcut
to refresh. Sort by path, making sure the root volume is first.
Task-number: QTBUG-60635
Change-Id: I74cda7647f544902aaf4d2a0ab76986f1523aa6f
Reviewed-by: Gatis Paeglis <gatis.paeglis@qt.io>
Diffstat (limited to 'examples/widgets/itemviews/storageview/storagemodel.cpp')
-rw-r--r-- | examples/widgets/itemviews/storageview/storagemodel.cpp | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/examples/widgets/itemviews/storageview/storagemodel.cpp b/examples/widgets/itemviews/storageview/storagemodel.cpp index 1395c9f208..063f126d86 100644 --- a/examples/widgets/itemviews/storageview/storagemodel.cpp +++ b/examples/widgets/itemviews/storageview/storagemodel.cpp @@ -54,14 +54,27 @@ #include <QDir> #include <QLocale> #include <qmath.h> +#include <algorithm> #include <cmath> StorageModel::StorageModel(QObject *parent) : - QAbstractTableModel(parent), - m_volumes(QStorageInfo::mountedVolumes()) + QAbstractTableModel(parent) { } +void StorageModel::refresh() +{ + beginResetModel(); + m_volumes = QStorageInfo::mountedVolumes(); + std::sort(m_volumes.begin(), m_volumes.end(), + [](const QStorageInfo &st1, const QStorageInfo &st2) { + static const QString rootSortString = QStringLiteral(" "); + return (st1.isRoot() ? rootSortString : st1.rootPath()) + < (st2.isRoot() ? rootSortString : st2.rootPath()); + }); + endResetModel(); +} + int StorageModel::columnCount(const QModelIndex &/*parent*/) const { return ColumnCount; @@ -74,6 +87,22 @@ int StorageModel::rowCount(const QModelIndex &parent) const return m_volumes.count(); } +Qt::ItemFlags StorageModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags result = QAbstractTableModel::flags(index); + switch (index.column()) { + case ColumnAvailable: + case ColumnIsReady: + case ColumnIsReadOnly: + case ColumnIsValid: + result |= Qt::ItemIsUserCheckable; + break; + default: + break; + } + return result; +} + QVariant StorageModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) @@ -96,6 +125,12 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const return QLocale().formattedDataSize(volume.bytesFree()); case ColumnAvailable: return QLocale().formattedDataSize(volume.bytesAvailable()); + default: + break; + } + } else if (role == Qt::CheckStateRole) { + const QStorageInfo &volume = m_volumes.at(index.row()); + switch (index.column()) { case ColumnIsReady: return volume.isReady(); case ColumnIsReadOnly: @@ -105,6 +140,16 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const default: break; } + } else if (role == Qt::TextAlignmentRole) { + switch (index.column()) { + case ColumnTotal: + case ColumnFree: + case ColumnAvailable: + return Qt::AlignTrailing; + default: + break; + } + return Qt::AlignLeading; } else if (role == Qt::ToolTipRole) { QLocale locale; const QStorageInfo &volume = m_volumes.at(index.row()); @@ -147,13 +192,13 @@ QVariant StorageModel::headerData(int section, Qt::Orientation orientation, int switch (section) { case ColumnRootPath: - return tr("Root path"); + return tr("Root Path"); case ColumnName: return tr("Volume Name"); case ColumnDevice: return tr("Device"); case ColumnFileSystemName: - return tr("File system"); + return tr("File System"); case ColumnTotal: return tr("Total"); case ColumnFree: |