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>
This commit is contained in:
Friedemann Kleint 2017-09-20 11:36:42 +02:00
parent bf41fbb233
commit 29b38bea45
3 changed files with 67 additions and 10 deletions

View File

@ -51,6 +51,7 @@
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QtWidgets/QHeaderView> #include <QtWidgets/QHeaderView>
#include <QtWidgets/QShortcut>
#include <QtWidgets/QTreeView> #include <QtWidgets/QTreeView>
#include "storagemodel.h" #include "storagemodel.h"
@ -60,9 +61,16 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
QTreeView view; QTreeView view;
view.setModel(new StorageModel(&view));
view.resize(640, 480); view.resize(640, 480);
view.setWindowTitle("Storage View");
view.setSelectionBehavior(QAbstractItemView::SelectRows); view.setSelectionBehavior(QAbstractItemView::SelectRows);
StorageModel *model = new StorageModel(&view);
model->refresh();
QShortcut *refreshShortcut = new QShortcut(Qt::CTRL + Qt::Key_R, &view);
QObject::connect(refreshShortcut, &QShortcut::activated, model, &StorageModel::refresh);
view.setModel(model);
int columnCount = view.model()->columnCount(); int columnCount = view.model()->columnCount();
for (int c = 0; c < columnCount; ++c) for (int c = 0; c < columnCount; ++c)
view.resizeColumnToContents(c); view.resizeColumnToContents(c);

View File

@ -54,14 +54,27 @@
#include <QDir> #include <QDir>
#include <QLocale> #include <QLocale>
#include <qmath.h> #include <qmath.h>
#include <algorithm>
#include <cmath> #include <cmath>
StorageModel::StorageModel(QObject *parent) : StorageModel::StorageModel(QObject *parent) :
QAbstractTableModel(parent), QAbstractTableModel(parent)
m_volumes(QStorageInfo::mountedVolumes())
{ {
} }
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 int StorageModel::columnCount(const QModelIndex &/*parent*/) const
{ {
return ColumnCount; return ColumnCount;
@ -74,6 +87,22 @@ int StorageModel::rowCount(const QModelIndex &parent) const
return m_volumes.count(); 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 QVariant StorageModel::data(const QModelIndex &index, int role) const
{ {
if (!index.isValid()) if (!index.isValid())
@ -96,6 +125,12 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const
return QLocale().formattedDataSize(volume.bytesFree()); return QLocale().formattedDataSize(volume.bytesFree());
case ColumnAvailable: case ColumnAvailable:
return QLocale().formattedDataSize(volume.bytesAvailable()); 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: case ColumnIsReady:
return volume.isReady(); return volume.isReady();
case ColumnIsReadOnly: case ColumnIsReadOnly:
@ -105,6 +140,16 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const
default: default:
break; 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) { } else if (role == Qt::ToolTipRole) {
QLocale locale; QLocale locale;
const QStorageInfo &volume = m_volumes.at(index.row()); const QStorageInfo &volume = m_volumes.at(index.row());
@ -147,13 +192,13 @@ QVariant StorageModel::headerData(int section, Qt::Orientation orientation, int
switch (section) { switch (section) {
case ColumnRootPath: case ColumnRootPath:
return tr("Root path"); return tr("Root Path");
case ColumnName: case ColumnName:
return tr("Volume Name"); return tr("Volume Name");
case ColumnDevice: case ColumnDevice:
return tr("Device"); return tr("Device");
case ColumnFileSystemName: case ColumnFileSystemName:
return tr("File system"); return tr("File System");
case ColumnTotal: case ColumnTotal:
return tr("Total"); return tr("Total");
case ColumnFree: case ColumnFree:

View File

@ -74,13 +74,17 @@ public:
ColumnCount ColumnCount
}; };
explicit StorageModel(QObject *parent = 0); explicit StorageModel(QObject *parent = nullptr);
int columnCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const override;
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const; Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
public slots:
void refresh();
private: private:
QList<QStorageInfo> m_volumes; QList<QStorageInfo> m_volumes;