From 872e37b2ff59af2f444c191f038366281d9d70d4 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Mon, 24 Aug 2020 11:31:13 +0200 Subject: [PATCH] Implement QAbstractFileIconProvider to use theme and mime databases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This doesn't give useful defaults on all platforms; icon themes are not provided on platforms other than Linux, and the mime database doesn't always return user-friendly names based on file names. However, both icon themes and the mime database can be customized by application developers by shipping a data file side-by-side with the application, without the need for reimplementing the provider class. Change-Id: Ie4b18ac1b861e2da64f01d1f209986b27fbe6bd5 Task-number: QTBUG-66177 Reviewed-by: Tor Arne Vestbø --- src/gui/image/qabstractfileiconprovider.cpp | 50 +++++++++++++++---- src/gui/image/qabstractfileiconprovider_p.h | 3 ++ .../languagechange/tst_languagechange.cpp | 3 -- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/gui/image/qabstractfileiconprovider.cpp b/src/gui/image/qabstractfileiconprovider.cpp index aa9f5bd0f0b..7872aed4d53 100644 --- a/src/gui/image/qabstractfileiconprovider.cpp +++ b/src/gui/image/qabstractfileiconprovider.cpp @@ -40,6 +40,10 @@ #include "qabstractfileiconprovider.h" #include +#include +#include + + #include #include @@ -127,23 +131,50 @@ QAbstractFileIconProvider::Options QAbstractFileIconProvider::options() const } /*! - Returns an icon set for the given \a type. + Returns an icon set for the given \a type, using the current + icon theme. + + \sa QIcon::fromTheme */ QIcon QAbstractFileIconProvider::icon(IconType type) const { Q_UNUSED(type); - return {}; + switch (type) { + case Computer: + return QIcon::fromTheme(QLatin1String("computer")); + case Desktop: + return QIcon::fromTheme(QLatin1String("user-desktop")); + case Trashcan: + return QIcon::fromTheme(QLatin1String("user-trash")); + case Network: + return QIcon::fromTheme(QLatin1String("network-workgroup")); + case Drive: + return QIcon::fromTheme(QLatin1String("drive-harddisk")); + case Folder: + return QIcon::fromTheme(QLatin1String("folder")); + case File: + return QIcon::fromTheme(QLatin1String("text-x-generic")); + // no default on purpose; we want warnings when the type enum is extended + } + return QIcon::fromTheme(QLatin1String("text-x-generic")); } /*! - Returns an icon for the file described by \a info. + Returns an icon for the file described by \a info, using the + current icon theme. + + \sa QIcon::fromTheme */ QIcon QAbstractFileIconProvider::icon(const QFileInfo &info) const { - Q_UNUSED(info); - return {}; + Q_D(const QAbstractFileIconProvider); + if (info.isRoot()) + return icon(Drive); + if (info.isDir()) + return icon(Folder); + return QIcon::fromTheme(d->mimeDatabase.mimeTypeForFile(info).iconName()); } /*! @@ -152,6 +183,7 @@ QIcon QAbstractFileIconProvider::icon(const QFileInfo &info) const QString QAbstractFileIconProvider::type(const QFileInfo &info) const { + Q_D(const QAbstractFileIconProvider); /* ### Qt 6 These string translations being in the QFileDialog context is not ideal, but translating them in QFileDialog context only in the QFileIconProvider subclass isn't either (it basically requires a duplication of the entire function). @@ -162,12 +194,8 @@ QString QAbstractFileIconProvider::type(const QFileInfo &info) const if (QFileSystemEntry::isRootPath(info.absoluteFilePath())) return QGuiApplication::translate("QFileDialog", "Drive"); if (info.isFile()) { - // ### could use QMimeDatabase::mimeTypeForFile(const QFileInfo&) here - if (!info.suffix().isEmpty()) { - //: %1 is a file name suffix, for example txt - return QGuiApplication::translate("QFileDialog", "%1 File").arg(info.suffix()); - } - return QGuiApplication::translate("QFileDialog", "File"); + const QMimeType mimeType = d->mimeDatabase.mimeTypeForFile(info); + return mimeType.comment().isEmpty() ? mimeType.name() : mimeType.comment(); } if (info.isDir()) diff --git a/src/gui/image/qabstractfileiconprovider_p.h b/src/gui/image/qabstractfileiconprovider_p.h index db7ac6db1bd..53ad4826eb0 100644 --- a/src/gui/image/qabstractfileiconprovider_p.h +++ b/src/gui/image/qabstractfileiconprovider_p.h @@ -52,6 +52,7 @@ // #include +#include #include "qabstractfileiconprovider.h" QT_BEGIN_NAMESPACE @@ -66,6 +67,8 @@ public: QAbstractFileIconProvider *q_ptr = nullptr; QAbstractFileIconProvider::Options options = {}; + + QMimeDatabase mimeDatabase; }; QT_END_NAMESPACE diff --git a/tests/auto/other/languagechange/tst_languagechange.cpp b/tests/auto/other/languagechange/tst_languagechange.cpp index 632039ec5ab..c8b9c52a965 100644 --- a/tests/auto/other/languagechange/tst_languagechange.cpp +++ b/tests/auto/other/languagechange/tst_languagechange.cpp @@ -196,9 +196,6 @@ void tst_languageChange::retranslatability_data() << "QFileDialog::Back" << "QFileDialog::Create New Folder" << "QFileDialog::Detail View" -#if !defined(Q_OS_MAC) - << "QFileDialog::File" -#endif << "QFileDialog::Files of type:" << "QFileDialog::Forward" << "QFileDialog::List View"