diff options
author | Alexis Menard <alexis.menard@nokia.com> | 2009-04-21 08:17:56 (GMT) |
---|---|---|
committer | Alexis Menard <alexis.menard@nokia.com> | 2009-04-21 08:21:23 (GMT) |
commit | b108e0479c6ec872ab767b8b81420b28ca1886cf (patch) | |
tree | f61d0d4b9147d87debd8685a4b76ea7674493e57 | |
parent | 292a37301950a64f211e3a2909ff64884b73e1cc (diff) | |
download | Qt-b108e0479c6ec872ab767b8b81420b28ca1886cf.zip Qt-b108e0479c6ec872ab767b8b81420b28ca1886cf.tar.gz Qt-b108e0479c6ec872ab767b8b81420b28ca1886cf.tar.bz2 |
There is no way to delete a invalid entry in the sidebar of QFileDialog.
We can't remove an item in the sidebar if the bookmark is not valid
(i.e. link to a non existing directory). ItemViews doesn't allow you
to have disabled items and to select them at the same time, so i have
implemented a delegate that paint in gray if the bookmark is invalid.
So you can click on it and delete it.
Task-number: 251341
Reviewed-by: jasplin
-rw-r--r-- | src/gui/dialogs/qsidebar.cpp | 21 | ||||
-rw-r--r-- | src/gui/dialogs/qsidebar_p.h | 13 | ||||
-rw-r--r-- | tests/auto/qfiledialog/tst_qfiledialog.cpp | 82 |
3 files changed, 112 insertions, 4 deletions
diff --git a/src/gui/dialogs/qsidebar.cpp b/src/gui/dialogs/qsidebar.cpp index 1bd2b7d..bfdb37e 100644 --- a/src/gui/dialogs/qsidebar.cpp +++ b/src/gui/dialogs/qsidebar.cpp @@ -55,6 +55,18 @@ QT_BEGIN_NAMESPACE +void QSideBarDelegate::initStyleOption(QStyleOptionViewItem *option, + const QModelIndex &index) const +{ + QStyledItemDelegate::initStyleOption(option,index); + QVariant value = index.data(QUrlModel::EnabledRole); + if (value.isValid()) { + //If the bookmark/entry is not enabled then we paint it in gray + if (!qvariant_cast<bool>(value)) + option->state &= ~QStyle::State_Enabled; + } +} + /*! QUrlModel lets you have indexes from a QFileSystemModel to a list. When QFileSystemModel changes them QUrlModel will automatically update. @@ -88,9 +100,6 @@ Qt::ItemFlags QUrlModel::flags(const QModelIndex &index) const if (index.data(Qt::DecorationRole).isNull()) flags &= ~Qt::ItemIsEnabled; - if (invalidUrls.contains(index.data(UrlRole).toUrl())) - flags &= ~Qt::ItemIsEnabled; - return flags; } @@ -193,6 +202,11 @@ void QUrlModel::setUrl(const QModelIndex &index, const QUrl &url, const QModelIn newName = QFileInfo(url.toLocalFile()).fileName(); if (!invalidUrls.contains(url)) invalidUrls.append(url); + //The bookmark is invalid then we set to false the EnabledRole + setData(index, false, EnabledRole); + } else { + //The bookmark is valid then we set to true the EnabledRole + setData(index, true, EnabledRole); } // Make sure that we have at least 32x32 images @@ -356,6 +370,7 @@ void QSidebar::init(QFileSystemModel *model, const QList<QUrl> &newUrls) urlModel = new QUrlModel(this); urlModel->setFileSystemModel(model); setModel(urlModel); + setItemDelegate(new QSideBarDelegate(this)); connect(selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(clicked(const QModelIndex &))); diff --git a/src/gui/dialogs/qsidebar_p.h b/src/gui/dialogs/qsidebar_p.h index ecbbb37..56fd6d4 100644 --- a/src/gui/dialogs/qsidebar_p.h +++ b/src/gui/dialogs/qsidebar_p.h @@ -55,6 +55,7 @@ #include <qlistwidget.h> #include <qstandarditemmodel.h> +#include <qstyleditemdelegate.h> #include <qurl.h> #ifndef QT_NO_FILEDIALOG @@ -62,13 +63,23 @@ QT_BEGIN_NAMESPACE class QFileSystemModel; + +class QSideBarDelegate : public QStyledItemDelegate +{ + public: + QSideBarDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {} + void initStyleOption(QStyleOptionViewItem *option, + const QModelIndex &index) const; +}; + class Q_AUTOTEST_EXPORT QUrlModel : public QStandardItemModel { Q_OBJECT public: enum Roles { - UrlRole = Qt::UserRole + 1 + UrlRole = Qt::UserRole + 1, + EnabledRole = Qt::UserRole + 2 }; QUrlModel(QObject *parent = 0); diff --git a/tests/auto/qfiledialog/tst_qfiledialog.cpp b/tests/auto/qfiledialog/tst_qfiledialog.cpp index 981d445..13b13d6 100644 --- a/tests/auto/qfiledialog/tst_qfiledialog.cpp +++ b/tests/auto/qfiledialog/tst_qfiledialog.cpp @@ -158,6 +158,7 @@ private slots: void task239706_editableFilterCombo(); void task218353_relativePaths(); void task251321_sideBarHiddenEntries(); + void task251341_sideBarRemoveEntries(); private: QByteArray userSettings; @@ -1886,5 +1887,86 @@ void tst_QFiledialog::task251321_sideBarHiddenEntries() current.rmdir(".hidden"); } +class MyQSideBar : public QSidebar +{ +public : + MyQSideBar(QWidget *parent = 0) : QSidebar(parent) + {} + + void removeSelection() { + QList<QModelIndex> idxs = selectionModel()->selectedIndexes(); + QList<QPersistentModelIndex> indexes; + for (int i = 0; i < idxs.count(); i++) + indexes.append(idxs.at(i)); + + for (int i = 0; i < indexes.count(); ++i) + if (!indexes.at(i).data(Qt::UserRole + 1).toUrl().path().isEmpty()) + model()->removeRow(indexes.at(i).row()); + } +}; + +void tst_QFiledialog::task251341_sideBarRemoveEntries() +{ + QNonNativeFileDialog fd; + + QDir current = QDir::currentPath(); + current.mkdir("testDir"); + QDir testSubDir = QDir("testDir"); + + QList<QUrl> urls; + urls << QUrl::fromLocalFile(testSubDir.absolutePath()); + urls << QUrl::fromLocalFile("NotFound"); + fd.setSidebarUrls(urls); + fd.show(); + QTest::qWait(250); + + QSidebar *sidebar = qFindChild<QSidebar*>(&fd, "sidebar"); + sidebar->setFocus(); + //We enter in the first bookmark + sidebar->selectUrl(QUrl::fromLocalFile(testSubDir.absolutePath())); + QTest::mouseClick(sidebar->viewport(), Qt::LeftButton, 0, sidebar->visualRect(sidebar->model()->index(0, 0)).center()); + QTest::qWait(250); + + QFileSystemModel *model = qFindChild<QFileSystemModel*>(&fd, "qt_filesystem_model"); + //There is no file + QCOMPARE(model->rowCount(model->index(testSubDir.absolutePath())), 0); + //Icon is not enabled QUrlModel::EnabledRole + QVariant value = sidebar->model()->index(0, 0).data(Qt::UserRole + 2); + QCOMPARE(qvariant_cast<bool>(value), true); + + sidebar->setFocus(); + //We enter in the second bookmark which is invalid + sidebar->selectUrl(QUrl::fromLocalFile("NotFound")); + QTest::mouseClick(sidebar->viewport(), Qt::LeftButton, 0, sidebar->visualRect(sidebar->model()->index(1, 0)).center()); + QTest::qWait(250); + + //We fallback to root because the entry in the bookmark is invalid + QCOMPARE(model->rowCount(model->index("NotFound")), model->rowCount(model->index(model->rootPath()))); + //Icon is not enabled QUrlModel::EnabledRole + value = sidebar->model()->index(1, 0).data(Qt::UserRole + 2); + QCOMPARE(qvariant_cast<bool>(value), false); + + MyQSideBar mySideBar; + mySideBar.init(model, urls); + mySideBar.show(); + mySideBar.selectUrl(QUrl::fromLocalFile(testSubDir.absolutePath())); + QTest::qWait(1000); + mySideBar.removeSelection(); + + //We remove the first entry + QList<QUrl> expected; + expected << QUrl::fromLocalFile("NotFound"); + QCOMPARE(mySideBar.urls(), expected); + + mySideBar.selectUrl(QUrl::fromLocalFile("NotFound")); + mySideBar.removeSelection(); + + //We remove the second entry + expected.clear(); + QCOMPARE(mySideBar.urls(), expected); + + current.rmdir("testDir"); +} + QTEST_MAIN(tst_QFiledialog) #include "tst_qfiledialog.moc" |