Refactor QFileSystemModel: use named values for columns
Hardcoded 0, 1, 2, 3 make it hard to follow the logic of this code. Replace those values with values in the already existing unnamed enum, and use everywhere. Pick-to: 6.5 6.2 Task-number: QTBUG-110632 Change-Id: I325ab9edb5f3f996e87c83be1ec7226d5453f2cc Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
This commit is contained in:
parent
7191b8fe38
commit
41a349f004
@ -710,15 +710,15 @@ QVariant QFileSystemModel::data(const QModelIndex &index, int role) const
|
|||||||
|
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
if (index.column() == 0)
|
if (index.column() == QFileSystemModelPrivate::NameColumn)
|
||||||
return d->name(index);
|
return d->name(index);
|
||||||
Q_FALLTHROUGH();
|
Q_FALLTHROUGH();
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case 0: return d->displayName(index);
|
case QFileSystemModelPrivate::NameColumn: return d->displayName(index);
|
||||||
case 1: return d->size(index);
|
case QFileSystemModelPrivate::SizeColumn: return d->size(index);
|
||||||
case 2: return d->type(index);
|
case QFileSystemModelPrivate::TypeColumn: return d->type(index);
|
||||||
case 3: return d->time(index);
|
case QFileSystemModelPrivate::TimeColumn: return d->time(index);
|
||||||
default:
|
default:
|
||||||
qWarning("data: invalid display value column %d", index.column());
|
qWarning("data: invalid display value column %d", index.column());
|
||||||
break;
|
break;
|
||||||
@ -729,7 +729,7 @@ QVariant QFileSystemModel::data(const QModelIndex &index, int role) const
|
|||||||
case FileNameRole:
|
case FileNameRole:
|
||||||
return d->name(index);
|
return d->name(index);
|
||||||
case Qt::DecorationRole:
|
case Qt::DecorationRole:
|
||||||
if (index.column() == 0) {
|
if (index.column() == QFileSystemModelPrivate::NameColumn) {
|
||||||
QIcon icon = d->icon(index);
|
QIcon icon = d->icon(index);
|
||||||
#if QT_CONFIG(filesystemwatcher)
|
#if QT_CONFIG(filesystemwatcher)
|
||||||
if (icon.isNull()) {
|
if (icon.isNull()) {
|
||||||
@ -743,7 +743,7 @@ QVariant QFileSystemModel::data(const QModelIndex &index, int role) const
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Qt::TextAlignmentRole:
|
case Qt::TextAlignmentRole:
|
||||||
if (index.column() == 1)
|
if (index.column() == QFileSystemModelPrivate::SizeColumn)
|
||||||
return QVariant(Qt::AlignTrailing | Qt::AlignVCenter);
|
return QVariant(Qt::AlignTrailing | Qt::AlignVCenter);
|
||||||
break;
|
break;
|
||||||
case FilePermissions:
|
case FilePermissions:
|
||||||
@ -937,23 +937,27 @@ QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation,
|
|||||||
|
|
||||||
QString returnValue;
|
QString returnValue;
|
||||||
switch (section) {
|
switch (section) {
|
||||||
case 0: returnValue = tr("Name");
|
case QFileSystemModelPrivate::NameColumn:
|
||||||
break;
|
returnValue = tr("Name");
|
||||||
case 1: returnValue = tr("Size");
|
break;
|
||||||
break;
|
case QFileSystemModelPrivate::SizeColumn:
|
||||||
case 2: returnValue =
|
returnValue = tr("Size");
|
||||||
|
break;
|
||||||
|
case QFileSystemModelPrivate::TypeColumn:
|
||||||
|
returnValue =
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
tr("Kind", "Match OS X Finder");
|
tr("Kind", "Match OS X Finder");
|
||||||
#else
|
#else
|
||||||
tr("Type", "All other platforms");
|
tr("Type", "All other platforms");
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
// Windows - Type
|
// Windows - Type
|
||||||
// OS X - Kind
|
// OS X - Kind
|
||||||
// Konqueror - File Type
|
// Konqueror - File Type
|
||||||
// Nautilus - Type
|
// Nautilus - Type
|
||||||
case 3: returnValue = tr("Date Modified");
|
case QFileSystemModelPrivate::TimeColumn:
|
||||||
break;
|
returnValue = tr("Date Modified");
|
||||||
|
break;
|
||||||
default: return QVariant();
|
default: return QVariant();
|
||||||
}
|
}
|
||||||
return returnValue;
|
return returnValue;
|
||||||
@ -1017,7 +1021,7 @@ public:
|
|||||||
const QFileSystemModelPrivate::QFileSystemNode *r) const
|
const QFileSystemModelPrivate::QFileSystemNode *r) const
|
||||||
{
|
{
|
||||||
switch (sortColumn) {
|
switch (sortColumn) {
|
||||||
case 0: {
|
case QFileSystemModelPrivate::NameColumn: {
|
||||||
#ifndef Q_OS_MAC
|
#ifndef Q_OS_MAC
|
||||||
// place directories before files
|
// place directories before files
|
||||||
bool left = l->isDir();
|
bool left = l->isDir();
|
||||||
@ -1027,7 +1031,7 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
return naturalCompare.compare(l->fileName, r->fileName) < 0;
|
return naturalCompare.compare(l->fileName, r->fileName) < 0;
|
||||||
}
|
}
|
||||||
case 1:
|
case QFileSystemModelPrivate::SizeColumn:
|
||||||
{
|
{
|
||||||
// Directories go first
|
// Directories go first
|
||||||
bool left = l->isDir();
|
bool left = l->isDir();
|
||||||
@ -1041,7 +1045,7 @@ public:
|
|||||||
|
|
||||||
return sizeDifference < 0;
|
return sizeDifference < 0;
|
||||||
}
|
}
|
||||||
case 2:
|
case QFileSystemModelPrivate::TypeColumn:
|
||||||
{
|
{
|
||||||
int compare = naturalCompare.compare(l->type(), r->type());
|
int compare = naturalCompare.compare(l->type(), r->type());
|
||||||
if (compare == 0)
|
if (compare == 0)
|
||||||
@ -1049,7 +1053,7 @@ public:
|
|||||||
|
|
||||||
return compare < 0;
|
return compare < 0;
|
||||||
}
|
}
|
||||||
case 3:
|
case QFileSystemModelPrivate::TimeColumn:
|
||||||
{
|
{
|
||||||
const QDateTime left = l->lastModified(QTimeZone::UTC);
|
const QDateTime left = l->lastModified(QTimeZone::UTC);
|
||||||
const QDateTime right = r->lastModified(QTimeZone::UTC);
|
const QDateTime right = r->lastModified(QTimeZone::UTC);
|
||||||
@ -1181,7 +1185,7 @@ QMimeData *QFileSystemModel::mimeData(const QModelIndexList &indexes) const
|
|||||||
QList<QUrl> urls;
|
QList<QUrl> urls;
|
||||||
QList<QModelIndex>::const_iterator it = indexes.begin();
|
QList<QModelIndex>::const_iterator it = indexes.begin();
|
||||||
for (; it != indexes.end(); ++it)
|
for (; it != indexes.end(); ++it)
|
||||||
if ((*it).column() == 0)
|
if ((*it).column() == QFileSystemModelPrivate::NameColumn)
|
||||||
urls << QUrl::fromLocalFile(filePath(*it));
|
urls << QUrl::fromLocalFile(filePath(*it));
|
||||||
QMimeData *data = new QMimeData();
|
QMimeData *data = new QMimeData();
|
||||||
data->setUrls(urls);
|
data->setUrls(urls);
|
||||||
@ -1991,8 +1995,8 @@ void QFileSystemModelPrivate::_q_fileSystemChanged(const QString &path,
|
|||||||
&& visibleMin < parentNode->visibleChildren.size()
|
&& visibleMin < parentNode->visibleChildren.size()
|
||||||
&& parentNode->visibleChildren.at(visibleMin) == min
|
&& parentNode->visibleChildren.at(visibleMin) == min
|
||||||
&& visibleMax >= 0) {
|
&& visibleMax >= 0) {
|
||||||
QModelIndex bottom = q->index(translateVisibleLocation(parentNode, visibleMin), 0, parentIndex);
|
QModelIndex bottom = q->index(translateVisibleLocation(parentNode, visibleMin), QFileSystemModelPrivate::NameColumn, parentIndex);
|
||||||
QModelIndex top = q->index(translateVisibleLocation(parentNode, visibleMax), 3, parentIndex);
|
QModelIndex top = q->index(translateVisibleLocation(parentNode, visibleMax), QFileSystemModelPrivate::NumColumns - 1, parentIndex);
|
||||||
emit q->dataChanged(bottom, top);
|
emit q->dataChanged(bottom, top);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,13 @@ class Q_GUI_EXPORT QFileSystemModelPrivate : public QAbstractItemModelPrivate
|
|||||||
Q_DECLARE_PUBLIC(QFileSystemModel)
|
Q_DECLARE_PUBLIC(QFileSystemModel)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum { NumColumns = 4 };
|
enum {
|
||||||
|
NameColumn,
|
||||||
|
SizeColumn,
|
||||||
|
TypeColumn,
|
||||||
|
TimeColumn,
|
||||||
|
NumColumns = 4
|
||||||
|
};
|
||||||
|
|
||||||
class QFileSystemNode
|
class QFileSystemNode
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user