FEATURE: sourceFilter to exclude certain sources

sourceFilter to exclude certain from the model with a regexp
This commit is contained in:
Marco Martin 2011-04-09 20:14:22 +02:00
parent 6f9f911c68
commit 98396208dc
2 changed files with 40 additions and 3 deletions

View File

@ -156,13 +156,19 @@ DataModel::~DataModel()
void DataModel::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
{
if (!m_keyRoleFilter.isEmpty()) {
QRegExp sourceRegExp(m_sourceFilter);
//a key that matches the one we want exists and is a list of DataEngine::Data
if (data.contains(m_keyRoleFilter) && data.value(m_keyRoleFilter).canConvert<QVariantList>()) {
if (data.contains(m_keyRoleFilter) &&
data.value(m_keyRoleFilter).canConvert<QVariantList>() &&
//matches the source filter regexp?
(m_sourceFilter.isEmpty() || (sourceRegExp.isValid() && sourceRegExp.exactMatch(sourceName)))) {
setItems(sourceName, data.value(m_keyRoleFilter).value<QVariantList>());
//try to match the key we want with a regular expression if set
} else {
QRegExp regExp(m_keyRoleFilter);
if (regExp.isValid()) {
if (regExp.isValid() &&
//matches the source filter regexp?
(m_sourceFilter.isEmpty() || (sourceRegExp.isValid() && sourceRegExp.exactMatch(sourceName)))) {
QHash<QString, QVariant>::const_iterator i;
QVariantList list;
for (i = data.constBegin(); i != data.constEnd(); ++i) {
@ -179,9 +185,14 @@ void DataModel::dataUpdated(const QString &sourceName, const Plasma::DataEngine:
if (!m_dataSource->data().isEmpty()) {
QVariantMap::const_iterator i = m_dataSource->data().constBegin();
QRegExp sourceRegExp(m_sourceFilter);
while (i != m_dataSource->data().constEnd()) {
QVariant value = i.value();
if (value.isValid() && value.canConvert<Plasma::DataEngine::Data>()) {
if (value.isValid() &&
value.canConvert<Plasma::DataEngine::Data>() &&
//matches the source filter regexp?
(m_sourceFilter.isEmpty() || (sourceRegExp.isValid() && sourceRegExp.exactMatch(i.key())))) {
Plasma::DataEngine::Data data = value.value<Plasma::DataEngine::Data>();
data["DataEngineSource"] = i.key();
list.append(data);
@ -226,6 +237,20 @@ void DataModel::setKeyRoleFilter(const QString& key)
m_keyRoleFilter = key;
}
void DataModel::setSourceFilter(const QString& key)
{
if (m_sourceFilter == key) {
return;
}
m_sourceFilter = key;
}
QString DataModel::sourceFilter() const
{
return m_sourceFilter;
}
QString DataModel::keyRoleFilter() const
{
return m_keyRoleFilter;

View File

@ -88,6 +88,7 @@ class DataModel : public QAbstractItemModel
Q_OBJECT
Q_PROPERTY(QObject *dataSource READ dataSource WRITE setDataSource)
Q_PROPERTY(QString keyRoleFilter READ keyRoleFilter WRITE setKeyRoleFilter)
Q_PROPERTY(QString sourceFilter READ sourceFilter WRITE setSourceFilter)
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
@ -97,9 +98,19 @@ public:
void setDataSource(QObject *source);
QObject *dataSource() const;
/**
* Include only items with a key that matches this regexp in the model
*/
void setKeyRoleFilter(const QString& key);
QString keyRoleFilter() const;
/**
* Include only items with a source name that matches this regexp in the model
* @since 4.7
*/
void setSourceFilter(const QString& key);
QString sourceFilter() const;
int roleNameToId(const QString &name);
//Reimplemented
@ -131,6 +142,7 @@ private Q_SLOTS:
private:
DataSource *m_dataSource;
QString m_keyRoleFilter;
QString m_sourceFilter;
QTimer *m_roleNamesTimer;
QMap<QString, QVector<QVariant> > m_items;
QHash<int, QByteArray> m_roleNames;