From 10f0b31dd1b3193bf4c068edde5979881c82ece7 Mon Sep 17 00:00:00 2001 From: Alexis Menard Date: Fri, 3 Apr 2009 13:49:13 +0200 Subject: Fix a crash in QFileDialog with a proxy on it. The main problem is that enterDirectory received a QModelIndex that can be a proxy index and not a source index if there is a proxy set on the QFileDialog. This fix basically discover some other crashes in the completer. The problem was that we didn't apply the proxy to the completer too. So we basically messed up source and proxy indexes. Both have a proxy set now and convert the model indexes if needed. Task-number:250194 Reviewed-by:jasplin --- src/gui/dialogs/qfiledialog.cpp | 27 ++++++++++++++--- src/gui/dialogs/qfiledialog_p.h | 39 +++++++++++++------------ tests/auto/qfiledialog/tst_qfiledialog.cpp | 47 ++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/gui/dialogs/qfiledialog.cpp b/src/gui/dialogs/qfiledialog.cpp index 2ce5563..5131271 100644 --- a/src/gui/dialogs/qfiledialog.cpp +++ b/src/gui/dialogs/qfiledialog.cpp @@ -2124,6 +2124,7 @@ void QFileDialogPrivate::createWidgets() #ifndef QT_NO_COMPLETER completer = new QFSCompletor(model, q); qFileDialogUi->fileNameEdit->setCompleter(completer); + completer->sourceModel = model; QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(textChanged(QString)), q, SLOT(_q_autoCompleteFileName(QString))); #endif // QT_NO_COMPLETER @@ -2246,12 +2247,21 @@ void QFileDialog::setProxyModel(QAbstractProxyModel *proxyModel) proxyModel->setSourceModel(d->model); d->qFileDialogUi->listView->setModel(d->proxyModel); d->qFileDialogUi->treeView->setModel(d->proxyModel); +#ifndef QT_NO_COMPLETER + d->completer->setModel(d->proxyModel); + d->completer->proxyModel = d->proxyModel; +#endif connect(d->proxyModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(_q_rowsInserted(const QModelIndex &))); } else { d->proxyModel = 0; d->qFileDialogUi->listView->setModel(d->model); d->qFileDialogUi->treeView->setModel(d->model); +#ifndef QT_NO_COMPLETER + d->completer->setModel(d->model); + d->completer->sourceModel = d->model; + d->completer->proxyModel = 0; +#endif connect(d->model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(_q_rowsInserted(const QModelIndex &))); } @@ -2764,8 +2774,9 @@ void QFileDialogPrivate::_q_enterDirectory(const QModelIndex &index) { Q_Q(QFileDialog); // My Computer or a directory - QString path = index.data(QFileSystemModel::FilePathRole).toString(); - if (path.isEmpty() || model->isDir(index)) { + QModelIndex sourceIndex = mapToSource(index); + QString path = sourceIndex.data(QFileSystemModel::FilePathRole).toString(); + if (path.isEmpty() || model->isDir(sourceIndex)) { q->setDirectory(path); emit q->directoryEntered(path); if (fileMode == QFileDialog::Directory @@ -3139,7 +3150,11 @@ void QFileDialogLineEdit::keyPressEvent(QKeyEvent *e) QString QFSCompletor::pathFromIndex(const QModelIndex &index) const { - const QFileSystemModel *dirModel = static_cast(model()); + const QFileSystemModel *dirModel; + if (proxyModel) + dirModel = qobject_cast(proxyModel->sourceModel()); + else + dirModel = sourceModel; QString currentLocation = dirModel->rootPath(); QString path = index.data(QFileSystemModel::FilePathRole).toString(); if (!currentLocation.isEmpty() && path.startsWith(currentLocation)) { @@ -3185,7 +3200,11 @@ QStringList QFSCompletor::splitPath(const QString &path) const bool startsFromRoot = path[0] == sep[0]; #endif if (parts.count() == 1 || (parts.count() > 1 && !startsFromRoot)) { - const QFileSystemModel *dirModel = static_cast(model()); + const QFileSystemModel *dirModel; + if (proxyModel) + dirModel = qobject_cast(proxyModel->sourceModel()); + else + dirModel = sourceModel; QString currentLocation = QDir::toNativeSeparators(dirModel->rootPath()); if (currentLocation.contains(sep) && path != currentLocation) { QStringList currentLocationList = splitPath(currentLocation); diff --git a/src/gui/dialogs/qfiledialog_p.h b/src/gui/dialogs/qfiledialog_p.h index 8cb2cb0..dc24390 100644 --- a/src/gui/dialogs/qfiledialog_p.h +++ b/src/gui/dialogs/qfiledialog_p.h @@ -91,6 +91,26 @@ class QCompleter; class QHBoxLayout; class Ui_QFileDialog; +#ifndef QT_NO_COMPLETER +/*! + QCompleter that can deal with QFileSystemModel + */ +class QFSCompletor : public QCompleter { +public: + QFSCompletor(QAbstractItemModel *model, QObject *parent = 0) : QCompleter(model, parent), proxyModel(0), sourceModel(0) + { +#ifdef Q_OS_WIN + setCaseSensitivity(Qt::CaseInsensitive); +#endif + } + QString pathFromIndex(const QModelIndex &index) const; + QStringList splitPath(const QString& path) const; + + QAbstractProxyModel *proxyModel; + QFileSystemModel *sourceModel; +}; +#endif // QT_NO_COMPLETER + struct QFileDialogArgs { QFileDialogArgs() : parent(0), mode(QFileDialog::AnyFile) {} @@ -255,7 +275,7 @@ public: // data QStringList watching; QFileSystemModel *model; - QCompleter *completer; + QFSCompletor *completer; QFileDialog::FileMode fileMode; QFileDialog::AcceptMode acceptMode; @@ -434,23 +454,6 @@ inline QString QFileDialogPrivate::rootPath() const { inline QString QFileDialogPrivate::selectedNameFilter_sys() const { return QString(); } #endif -#ifndef QT_NO_COMPLETER -/*! - QCompleter that can deal with QFileSystemModel - */ -class QFSCompletor : public QCompleter { -public: - QFSCompletor(QAbstractItemModel *model, QObject *parent = 0) : QCompleter(model, parent) - { -#ifdef Q_OS_WIN - setCaseSensitivity(Qt::CaseInsensitive); -#endif - } - QString pathFromIndex(const QModelIndex &index) const; - QStringList splitPath(const QString& path) const; -}; -#endif // QT_NO_COMPLETER - QT_END_NAMESPACE #endif // QT_NO_FILEDIALOG diff --git a/tests/auto/qfiledialog/tst_qfiledialog.cpp b/tests/auto/qfiledialog/tst_qfiledialog.cpp index b03d459..bade586 100644 --- a/tests/auto/qfiledialog/tst_qfiledialog.cpp +++ b/tests/auto/qfiledialog/tst_qfiledialog.cpp @@ -1526,6 +1526,42 @@ private: }; +class sortProxy : public QSortFilterProxyModel +{ +public: + sortProxy(QObject *parent) : QSortFilterProxyModel(parent) + { + } +protected: + virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const + { + QFileSystemModel * const model = qobject_cast(sourceModel()); + const QFileInfo leftInfo(model->fileInfo(left)); + const QFileInfo rightInfo(model->fileInfo(right)); + + if (leftInfo.isDir() == rightInfo.isDir()) + return(leftInfo.filePath().compare(rightInfo.filePath(),Qt::CaseInsensitive) < 0); + else if (leftInfo.isDir()) + return(false); + else + return(true); + } +}; + +class CrashDialog : public QNonNativeFileDialog +{ + Q_OBJECT + +public: + CrashDialog(QWidget *parent, const QString &caption, const +QString &dir, const QString &filter) + : QNonNativeFileDialog(parent, caption, dir, filter) + { + sortProxy *proxyModel = new sortProxy(this); + setProxyModel(proxyModel); + } +}; + void tst_QFiledialog::task227304_proxyOnFileDialog() { QNonNativeFileDialog fd(0, "", QDir::currentPath(), 0); @@ -1537,6 +1573,17 @@ void tst_QFiledialog::task227304_proxyOnFileDialog() QTest::keyClick(edit, Qt::Key_S); QTest::qWait(200); QTest::keyClick(edit->completer()->popup(), Qt::Key_Down); + + CrashDialog *dialog = new CrashDialog(0, QString("crash dialog test"), QDir::homePath(), QString("*") ); + dialog->setFileMode(QFileDialog::ExistingFile); + dialog->show(); + + QListView *list = qFindChild(dialog, "listView"); + QTest::qWait(200); + QTest::keyClick(list, Qt::Key_Down); + QTest::keyClick(list, Qt::Key_Return); + QTest::qWait(200); + } void tst_QFiledialog::task227930_correctNavigationKeyboardBehavior() -- cgit v0.12