Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

One DataModel to filter them all

int childCount(const QVariantList &indexPath);
bool hasChildren(const QVariantList &indexPath);
QString itemType(const QVariantList &indexPath);
QVariant data(const QVariantList &indexPath);
ListView1         ListView2           ListView3
       ^                 ^                    ^
       |                 |                    |
sourceModel -> ownerFilterModel -> fileTypeFilterModel
void FileBrowser::scanDirectory()
{
    m_fileModel->clear();

    QDirIterator it(m_directory, QDir::NoDotAndDotDot);
    while (it.hasNext()) {
        it.next();

        const QFileInfo info = it.fileInfo();

        QVariantMap entry;
        entry["name"] = info.fileName();
        entry["extension"] = info.suffix();
        entry["owner"] = info.owner();
        // add more information here

        m_fileModel->insert(entry);
    }
}
FileBrowser::FileBrowser(QObject *parent)
    : QObject(parent)
    , m_fileModel(new GroupDataModel(QStringList() << "name", this))
{
    m_ownerFilterModel = new OwnerFilterModel(this);
    m_onwerFilterModel->setSourceModel(m_fileModel);
    m_ownerFilterModel->setOwner("tokoe");

    m_fileTypeFilterModel = new FileTypeFilterModel(this);
    m_fileTypeFilterModel->setSourceModel(m_ownerModel);
    m_fileTypeFilterModel->setFileType("ogg");
}
class OwnerFilterModel : public FilterProxyModel
{
    Q_OBJECT

public:
    OwnerFilterModel(QObject *parent = 0)
        : FilterProxyModel(parent)
    {
    }

    void setOwner(const QString &owner)
    {
        m_owner = owner;
        invalidateFilter();
    }

protected:
    virtual bool acceptItem(const QVariantList &indexPath,
            const QString &itemType, const QVariant &data) const
    {
        if (itemType == "header")
            return true;//we don't care about headers

        if (m_owner.isEmpty())//if no owner is given, we accept all
            return true;

        const QVariantMap entry = data.toMap();
        return (m_owner == entry["owner"].toString());
    }
}

3 Comments

26 - Jun - 2013

mathieu hamel

28 - Aug - 2013

Jeff

23 - Jan - 2018

okan