Commit e590cd04 authored by Urs Fleisch's avatar Urs Fleisch
Browse files

KDE 5: Fix name filters used in file dialogs.

parent 4bcea5da
......@@ -132,6 +132,9 @@ QIcon KdePlatformTools::iconFromTheme(const QString& name) const
QString KdePlatformTools::fileDialogNameFilter(
const QList<QPair<QString, QString> >& nameFilters) const
{
#if QT_VERSION >= 0x050000
return ICorePlatformTools::qtFileDialogNameFilter(nameFilters);
#else
QString filter;
for (QList<QPair<QString, QString> >::const_iterator it =
nameFilters.constBegin();
......@@ -150,6 +153,7 @@ QString KdePlatformTools::fileDialogNameFilter(
}
}
return filter;
#endif
}
/**
......@@ -159,12 +163,16 @@ QString KdePlatformTools::fileDialogNameFilter(
*/
QString KdePlatformTools::getNameFilterPatterns(const QString& nameFilter) const
{
#if QT_VERSION >= 0x050000
return ICorePlatformTools::qtNameFilterPatterns(nameFilter);
#else
if (nameFilter.startsWith(QLatin1Char('*'))) {
int end = nameFilter.indexOf(QLatin1Char('|'));
return end != -1 ? nameFilter.left(end) : nameFilter;
} else {
return QString();
}
#endif
}
/**
......
......@@ -261,20 +261,7 @@ bool CorePlatformTools::moveToTrash(const QString& path) const
QString CorePlatformTools::fileDialogNameFilter(
const QList<QPair<QString, QString> >& nameFilters) const
{
QString filter;
for (QList<QPair<QString, QString> >::const_iterator it =
nameFilters.constBegin();
it != nameFilters.constEnd();
++it) {
if (!filter.isEmpty()) {
filter += QLatin1String(";;");
}
filter += it->first;
filter += QLatin1String(" (");
filter += it->second;
filter += QLatin1Char(')');
}
return filter;
return ICorePlatformTools::qtFileDialogNameFilter(nameFilters);
}
/**
......@@ -284,9 +271,5 @@ QString CorePlatformTools::fileDialogNameFilter(
*/
QString CorePlatformTools::getNameFilterPatterns(const QString& nameFilter) const
{
int start = nameFilter.indexOf(QLatin1Char('(')),
end = nameFilter.indexOf(QLatin1Char(')'));
return start != -1 && end != -1 && end > start
? nameFilter.mid(start + 1, end - start - 1)
: QString();
return ICorePlatformTools::qtNameFilterPatterns(nameFilter);
}
......@@ -25,6 +25,7 @@
*/
#include "icoreplatformtools.h"
#include <QString>
/**
* Destructor.
......@@ -32,3 +33,46 @@
ICorePlatformTools::~ICorePlatformTools()
{
}
/**
* Construct a name filter string suitable for file dialogs.
* This function can be used to implement fileDialogNameFilter()
* for QFileDialog.
* @param nameFilters list of description, filter pairs, e.g.
* [("Images", "*.jpg *.jpeg *.png"), ("All Files", "*")].
* @return name filter string.
*/
QString ICorePlatformTools::qtFileDialogNameFilter(
const QList<QPair<QString, QString> >& nameFilters)
{
QString filter;
for (QList<QPair<QString, QString> >::const_iterator it =
nameFilters.constBegin();
it != nameFilters.constEnd();
++it) {
if (!filter.isEmpty()) {
filter += QLatin1String(";;");
}
filter += it->first;
filter += QLatin1String(" (");
filter += it->second;
filter += QLatin1Char(')');
}
return filter;
}
/**
* Get file pattern part of m_nameFilter.
* This function can be used to implement getNameFilterPatterns()
* for QFileDialog.
* @param nameFilter name filter string
* @return file patterns, e.g. "*.mp3".
*/
QString ICorePlatformTools::qtNameFilterPatterns(const QString& nameFilter)
{
int start = nameFilter.indexOf(QLatin1Char('(')),
end = nameFilter.indexOf(QLatin1Char(')'));
return start != -1 && end != -1 && end > start
? nameFilter.mid(start + 1, end - start - 1)
: QString();
}
......@@ -74,6 +74,27 @@ public:
* @return file patterns, e.g. "*.mp3".
*/
virtual QString getNameFilterPatterns(const QString& nameFilter) const = 0;
protected:
/**
* Construct a name filter string suitable for file dialogs.
* This function can be used to implement fileDialogNameFilter()
* for QFileDialog.
* @param nameFilters list of description, filter pairs, e.g.
* [("Images", "*.jpg *.jpeg *.png"), ("All Files", "*")].
* @return name filter string.
*/
static QString qtFileDialogNameFilter(
const QList<QPair<QString, QString> >& nameFilters);
/**
* Get file pattern part of m_nameFilter.
* This function can be used to implement getNameFilterPatterns()
* for QFileDialog.
* @param nameFilter name filter string
* @return file patterns, e.g. "*.mp3".
*/
static QString qtNameFilterPatterns(const QString& nameFilter);
};
#endif // ICOREPLATFORMTOOLS_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment