QFileSystemModel: fix updating QFileInfo for a node after a file rename
Use the correct parent path for the renamed file when creating QFileInfo. It must be a path to the parent directory, not to the root directory of the model. Modify tst_QFileSystemModel::setData() to test renames in subdirs of the root directory of the model. Change-Id: I69d9e3a937616857a81617791ed118af3f6eea05 Task-number: QTBUG-52561 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>bb10
parent
ea64dc9a11
commit
e27fae353c
|
|
@ -849,9 +849,11 @@ bool QFileSystemModel::setData(const QModelIndex &idx, const QVariant &value, in
|
|||
if (newName == idx.data().toString())
|
||||
return true;
|
||||
|
||||
const QString parentPath = filePath(parent(idx));
|
||||
|
||||
if (newName.isEmpty()
|
||||
|| QDir::toNativeSeparators(newName).contains(QDir::separator())
|
||||
|| !QDir(filePath(parent(idx))).rename(oldName, newName)) {
|
||||
|| !QDir(parentPath).rename(oldName, newName)) {
|
||||
#ifndef QT_NO_MESSAGEBOX
|
||||
QMessageBox::information(0, QFileSystemModel::tr("Invalid filename"),
|
||||
QFileSystemModel::tr("<b>The name \"%1\" can not be used.</b><p>Try using another name, with fewer characters or no punctuations marks.")
|
||||
|
|
@ -878,7 +880,7 @@ bool QFileSystemModel::setData(const QModelIndex &idx, const QVariant &value, in
|
|||
parentNode->visibleChildren.removeAt(visibleLocation);
|
||||
QFileSystemModelPrivate::QFileSystemNode * oldValue = parentNode->children.value(oldName);
|
||||
parentNode->children[newName] = oldValue;
|
||||
QFileInfo info(d->rootDir, newName);
|
||||
QFileInfo info(parentPath, newName);
|
||||
oldValue->fileName = newName;
|
||||
oldValue->parent = parentNode;
|
||||
#ifndef QT_NO_FILESYSTEMWATCHER
|
||||
|
|
@ -890,7 +892,7 @@ bool QFileSystemModel::setData(const QModelIndex &idx, const QVariant &value, in
|
|||
parentNode->visibleChildren.insert(visibleLocation, newName);
|
||||
|
||||
d->delayedSort();
|
||||
emit fileRenamed(filePath(idx.parent()), oldName, newName);
|
||||
emit fileRenamed(parentPath, oldName, newName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -697,6 +697,7 @@ void tst_QFileSystemModel::nameFilters()
|
|||
}
|
||||
void tst_QFileSystemModel::setData_data()
|
||||
{
|
||||
QTest::addColumn<QString>("subdirName");
|
||||
QTest::addColumn<QStringList>("files");
|
||||
QTest::addColumn<QString>("oldFileName");
|
||||
QTest::addColumn<QString>("newFileName");
|
||||
|
|
@ -706,7 +707,15 @@ void tst_QFileSystemModel::setData_data()
|
|||
<< QDir::temp().absolutePath() + '/' + "a"
|
||||
<< false;
|
||||
*/
|
||||
QTest::newRow("in current dir") << (QStringList() << "a" << "b" << "c")
|
||||
QTest::newRow("in current dir")
|
||||
<< QString()
|
||||
<< (QStringList() << "a" << "b" << "c")
|
||||
<< "a"
|
||||
<< "d"
|
||||
<< true;
|
||||
QTest::newRow("in subdir")
|
||||
<< "s"
|
||||
<< (QStringList() << "a" << "b" << "c")
|
||||
<< "a"
|
||||
<< "d"
|
||||
<< true;
|
||||
|
|
@ -715,15 +724,25 @@ void tst_QFileSystemModel::setData_data()
|
|||
void tst_QFileSystemModel::setData()
|
||||
{
|
||||
QSignalSpy spy(model, SIGNAL(fileRenamed(QString,QString,QString)));
|
||||
QString tmp = flatDirTestPath;
|
||||
QFETCH(QString, subdirName);
|
||||
QFETCH(QStringList, files);
|
||||
QFETCH(QString, oldFileName);
|
||||
QFETCH(QString, newFileName);
|
||||
QFETCH(bool, success);
|
||||
|
||||
QString tmp = flatDirTestPath;
|
||||
if (!subdirName.isEmpty()) {
|
||||
QDir dir(tmp);
|
||||
QVERIFY(dir.mkdir(subdirName));
|
||||
tmp.append('/' + subdirName);
|
||||
}
|
||||
QVERIFY(createFiles(tmp, files));
|
||||
QModelIndex root = model->setRootPath(tmp);
|
||||
QTRY_COMPARE(model->rowCount(root), files.count());
|
||||
QModelIndex tmpIdx = model->setRootPath(flatDirTestPath);
|
||||
if (!subdirName.isEmpty()) {
|
||||
tmpIdx = model->index(tmp);
|
||||
model->fetchMore(tmpIdx);
|
||||
}
|
||||
QTRY_COMPARE(model->rowCount(tmpIdx), files.count());
|
||||
|
||||
QModelIndex idx = model->index(tmp + '/' + oldFileName);
|
||||
QCOMPARE(idx.isValid(), true);
|
||||
|
|
@ -731,16 +750,21 @@ void tst_QFileSystemModel::setData()
|
|||
|
||||
model->setReadOnly(false);
|
||||
QCOMPARE(model->setData(idx, newFileName), success);
|
||||
model->setReadOnly(true);
|
||||
if (success) {
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QList<QVariant> arguments = spy.takeFirst();
|
||||
QCOMPARE(model->data(idx, QFileSystemModel::FileNameRole).toString(), newFileName);
|
||||
QCOMPARE(model->fileInfo(idx).filePath(), tmp + '/' + newFileName);
|
||||
QCOMPARE(model->index(arguments.at(0).toString()), model->index(tmp));
|
||||
QCOMPARE(arguments.at(1).toString(), oldFileName);
|
||||
QCOMPARE(arguments.at(2).toString(), newFileName);
|
||||
QCOMPARE(QFile::rename(tmp + '/' + newFileName, tmp + '/' + oldFileName), true);
|
||||
}
|
||||
QTRY_COMPARE(model->rowCount(root), files.count());
|
||||
QTRY_COMPARE(model->rowCount(tmpIdx), files.count());
|
||||
// cleanup
|
||||
if (!subdirName.isEmpty())
|
||||
QVERIFY(QDir(tmp).removeRecursively());
|
||||
}
|
||||
|
||||
void tst_QFileSystemModel::sortPersistentIndex()
|
||||
|
|
|
|||
Loading…
Reference in New Issue