Commit 28e6f71e authored by Urs Fleisch's avatar Urs Fleisch
Browse files

Filter file extensions in proxy model instead of file system model.

parent 1ad5e433
......@@ -42,7 +42,7 @@
* @param parent parent object
*/
FileProxyModel::FileProxyModel(QObject* parent) : QSortFilterProxyModel(parent),
m_iconProvider(new TaggedFileIconProvider)
m_iconProvider(new TaggedFileIconProvider), m_fsModel(0)
{
connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)),
this, SLOT(updateInsertedRows(QModelIndex,int,int)));
......@@ -54,10 +54,9 @@ FileProxyModel::FileProxyModel(QObject* parent) : QSortFilterProxyModel(parent),
*/
QFileInfo FileProxyModel::fileInfo(const QModelIndex& index) const
{
if (const QFileSystemModel* fsModel =
qobject_cast<QFileSystemModel*>(sourceModel())) {
if (m_fsModel) {
QModelIndex sourceIndex(mapToSource(index));
return fsModel->fileInfo(sourceIndex);
return m_fsModel->fileInfo(sourceIndex);
}
return QFileInfo();
}
......@@ -68,10 +67,9 @@ QFileInfo FileProxyModel::fileInfo(const QModelIndex& index) const
*/
QString FileProxyModel::filePath(const QModelIndex& index) const
{
if (const QFileSystemModel* fsModel =
qobject_cast<QFileSystemModel*>(sourceModel())) {
if (m_fsModel) {
QModelIndex sourceIndex(mapToSource(index));
return fsModel->filePath(sourceIndex);
return m_fsModel->filePath(sourceIndex);
}
return QString();
}
......@@ -82,10 +80,9 @@ QString FileProxyModel::filePath(const QModelIndex& index) const
*/
bool FileProxyModel::isDir(const QModelIndex& index) const
{
if (const QFileSystemModel* fsModel =
qobject_cast<QFileSystemModel*>(sourceModel())) {
if (m_fsModel) {
QModelIndex sourceIndex(mapToSource(index));
return fsModel->isDir(sourceIndex);
return m_fsModel->isDir(sourceIndex);
}
return false;
}
......@@ -96,10 +93,9 @@ bool FileProxyModel::isDir(const QModelIndex& index) const
*/
bool FileProxyModel::remove(const QModelIndex& index) const
{
if (const QFileSystemModel* fsModel =
qobject_cast<QFileSystemModel*>(sourceModel())) {
if (m_fsModel) {
QModelIndex sourceIndex(mapToSource(index));
return fsModel->remove(sourceIndex);
return m_fsModel->remove(sourceIndex);
}
return false;
}
......@@ -110,10 +106,9 @@ bool FileProxyModel::remove(const QModelIndex& index) const
*/
bool FileProxyModel::rmdir(const QModelIndex& index) const
{
if (const QFileSystemModel* fsModel =
qobject_cast<QFileSystemModel*>(sourceModel())) {
if (m_fsModel) {
QModelIndex sourceIndex(mapToSource(index));
return fsModel->rmdir(sourceIndex);
return m_fsModel->rmdir(sourceIndex);
}
return false;
}
......@@ -186,7 +181,16 @@ bool FileProxyModel::filterAcceptsRow(
return false;
}
QString item(srcIndex.data().toString());
return item != "." && item != "..";
if (item == "." || item == "..")
return false;
if (m_extensions.isEmpty() || !m_fsModel || m_fsModel->isDir(srcIndex))
return true;
for (QStringList::const_iterator it = m_extensions.begin();
it != m_extensions.end();
++it) {
if (item.endsWith(*it, Qt::CaseInsensitive))
return true;
}
}
return false;
}
......@@ -235,6 +239,41 @@ bool FileProxyModel::setData(const QModelIndex& index, const QVariant& value,
return QSortFilterProxyModel::setData(index, value, role);
}
/**
* Set source model.
* @param sourceModel source model, must be QFileSystemModel
*/
void FileProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
{
m_fsModel = qobject_cast<QFileSystemModel*>(sourceModel);
Q_ASSERT_X(m_fsModel != 0 , "setSourceModel",
"sourceModel is not QFileSystemModel");
QSortFilterProxyModel::setSourceModel(sourceModel);
}
/**
* Sets the name filters to apply against the existing files.
* @param filters list of strings containing wildcards like "*.mp3"
*/
void FileProxyModel::setNameFilters(const QStringList& filters)
{
QRegExp wildcardRe("\\.\\w+");
QSet<QString> exts;
foreach (QString filter, filters) {
int pos = 0;
while ((pos = wildcardRe.indexIn(filter, pos)) != -1) {
int len = wildcardRe.matchedLength();
exts.insert(filter.mid(pos, len).toLower());
pos += len;
}
}
QStringList oldExtensions(m_extensions);
m_extensions = exts.toList();
if (m_extensions != oldExtensions) {
invalidateFilter();
}
}
/**
* Filter out a model index.
* @param index model index which has to be filtered out
......
......@@ -31,6 +31,7 @@
#include <QHash>
#include <QSet>
#include <QFileInfo>
#include <QStringList>
class QFileSystemModel;
class TaggedFile;
......@@ -96,6 +97,18 @@ public:
virtual bool setData(const QModelIndex& index, const QVariant& value,
int role=Qt::EditRole);
/**
* Set source model.
* @param sourceModel source model, must be QFileSystemModel
*/
virtual void setSourceModel(QAbstractItemModel* sourceModel);
/**
* Sets the name filters to apply against the existing files.
* @param filters list of strings containing wildcards like "*.mp3"
*/
void setNameFilters(const QStringList& filters);
/**
* Filter out a model index.
* @param index model index which has to be filtered out
......@@ -264,6 +277,8 @@ private:
QHash<QPersistentModelIndex, TaggedFile*> m_taggedFiles;
QSet<QPersistentModelIndex> m_filteredOut;
TaggedFileIconProvider* m_iconProvider;
QFileSystemModel* m_fsModel;
QStringList m_extensions;
};
Q_DECLARE_METATYPE(TaggedFile*)
......
......@@ -166,13 +166,9 @@ Id3Form::Id3Form(QWidget* parent)
m_vSplitter = new QSplitter(Qt::Vertical, this);
m_fileListBox = new FileList(m_vSplitter, theApp);
FileProxyModel* fileProxyModel = new FileProxyModel(m_vSplitter);
fileProxyModel->setSourceModel(theApp->getFileSystemModel());
m_fileListBox->setModel(fileProxyModel);
m_fileListBox->setModel(theApp->getFileProxyModel());
m_dirListBox = new DirList(m_vSplitter);
DirProxyModel* dirProxyModel = new DirProxyModel(m_vSplitter);
dirProxyModel->setSourceModel(theApp->getFileSystemModel());
m_dirListBox->setModel(dirProxyModel);
m_dirListBox->setModel(theApp->getDirProxyModel());
m_rightHalfVBox = new QWidget;
QScrollArea* scrollView = new QScrollArea(this);
......
......@@ -93,6 +93,7 @@
#include "playlistdialog.h"
#include "playlistcreator.h"
#include "fileproxymodel.h"
#include "dirproxymodel.h"
#include "modeliterator.h"
#include "dirlist.h"
#include "pictureframe.h"
......@@ -142,6 +143,8 @@ QString Kid3App::s_dirName;
*/
Kid3App::Kid3App() :
m_fileSystemModel(new QFileSystemModel(this)),
m_fileProxyModel(new FileProxyModel(this)),
m_dirProxyModel(new DirProxyModel(this)),
m_downloadToAllFilesInDir(false),
m_importDialog(0), m_browseCoverArtDialog(0),
m_exportDialog(0), m_renDirDialog(0),
......@@ -152,6 +155,8 @@ Kid3App::Kid3App() :
#endif
{
m_fileSystemModel->setFilter(QDir::AllEntries | QDir::AllDirs);
m_fileProxyModel->setSourceModel(m_fileSystemModel);
m_dirProxyModel->setSourceModel(m_fileSystemModel);
#ifdef CONFIG_USE_KDE
m_config = new KConfig;
#else
......@@ -850,8 +855,7 @@ bool Kid3App::openDirectory(QString dir, bool confirm, bool fileCheck)
slotStatusMsg(i18n("Opening directory..."));
QStringList nameFilters(s_miscCfg.m_nameFilter.split(' '));
m_fileSystemModel->setNameFilters(nameFilters);
m_fileSystemModel->setNameFilterDisables(false);
m_fileProxyModel->setNameFilters(nameFilters);
QModelIndex rootIndex = m_fileSystemModel->setRootPath(dir);
QModelIndex fileIndex = m_fileSystemModel->index(filePath);
bool ok = m_view->readFileList(rootIndex, fileIndex);
......
......@@ -80,6 +80,7 @@ class PlayToolBar;
class DirContents;
class QFileSystemModel;
class FileProxyModel;
class DirProxyModel;
/** Kid3 application */
class Kid3App : public Kid3AppBaseClass
......@@ -281,6 +282,18 @@ public:
*/
QFileSystemModel* getFileSystemModel() { return m_fileSystemModel; }
/**
* Get file proxy model.
* @return file proxy model.
*/
FileProxyModel* getFileProxyModel() { return m_fileProxyModel; }
/**
* Get directory proxy model.
* @return directory proxy model.
*/
DirProxyModel* getDirProxyModel() { return m_dirProxyModel; }
/**
* Display help for a topic.
*
......@@ -791,6 +804,8 @@ private:
Id3Form* m_view;
/** model of filesystem */
QFileSystemModel* m_fileSystemModel;
FileProxyModel* m_fileProxyModel;
DirProxyModel* m_dirProxyModel;
/** true if any file was modified */
bool m_modified;
/** true if list is filtered */
......
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