diff options
Diffstat (limited to 'src/nepomuk/utils')
-rw-r--r-- | src/nepomuk/utils/nepomukutils_export.h | 40 | ||||
-rw-r--r-- | src/nepomuk/utils/resourcemodel.cpp | 230 | ||||
-rw-r--r-- | src/nepomuk/utils/resourcemodel.h | 177 | ||||
-rw-r--r-- | src/nepomuk/utils/simpleresourcemodel.cpp | 170 | ||||
-rw-r--r-- | src/nepomuk/utils/simpleresourcemodel.h | 144 |
5 files changed, 761 insertions, 0 deletions
diff --git a/src/nepomuk/utils/nepomukutils_export.h b/src/nepomuk/utils/nepomukutils_export.h new file mode 100644 index 00000000..718bd7c3 --- /dev/null +++ b/src/nepomuk/utils/nepomukutils_export.h @@ -0,0 +1,40 @@ +/* This file is part of the KDE project + Copyright (C) 2007 David Faure <faure@kde.org> + Copyright (C) 2010 Sebastian Trueg <trueg@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef NEPOMUKUTILS_EXPORT_H +#define NEPOMUKUTILS_EXPORT_H + +/* needed for KDE_EXPORT and KDE_IMPORT macros */ +#include <kdemacros.h> + +#ifndef NEPOMUKUTILS_EXPORT +# if defined(KDELIBS_STATIC_LIBS) + /* No export/import for static libraries */ +# define NEPOMUKUTILS_EXPORT +# elif defined(MAKE_NEPOMUKUTILS_LIB) + /* We are building this library */ +# define NEPOMUKUTILS_EXPORT KDE_EXPORT +# else + /* We are using this library */ +# define NEPOMUKUTILS_EXPORT KDE_IMPORT +# endif +#endif + +#endif diff --git a/src/nepomuk/utils/resourcemodel.cpp b/src/nepomuk/utils/resourcemodel.cpp new file mode 100644 index 00000000..72c71e9b --- /dev/null +++ b/src/nepomuk/utils/resourcemodel.cpp @@ -0,0 +1,230 @@ +/* + This file is part of the Nepomuk KDE project. + Copyright (C) 2007-2010 Sebastian Trueg <trueg@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "resourcemodel.h" + +#include <QtCore/QUrl> +#include <QtCore/QList> +#include <QtCore/QMimeData> + +#include "kurl.h" +#include "kdebug.h" +#include "kcategorizedsortfilterproxymodel.h" +#include "kicon.h" +#include "klocale.h" + +#include <Nepomuk2/Resource> +#include <Nepomuk2/Types/Class> +#include <Nepomuk2/Variant> + +#include <Soprano/Vocabulary/RDFS> +#include <Soprano/Vocabulary/NAO> + + +Q_DECLARE_METATYPE(Nepomuk2::Types::Class) + + +class Nepomuk2::Utils::ResourceModel::Private +{ +public: +}; + + +Nepomuk2::Utils::ResourceModel::ResourceModel( QObject* parent ) + : QAbstractItemModel( parent ), + d( new Private() ) +{ +} + + +Nepomuk2::Utils::ResourceModel::~ResourceModel() +{ + delete d; +} + + +QModelIndex Nepomuk2::Utils::ResourceModel::parent( const QModelIndex& child ) const +{ + Q_UNUSED(child); + return QModelIndex(); +} + + +int Nepomuk2::Utils::ResourceModel::columnCount( const QModelIndex& parent ) const +{ + Q_UNUSED(parent); + return ResourceModelColumnCount; +} + + +QVariant Nepomuk2::Utils::ResourceModel::data( const QModelIndex& index, int role ) const +{ + Nepomuk2::Resource res = resourceForIndex( index ); + if( !res.isValid() ) { + return QVariant(); + } + + // + // Part 1: column specific data + // + switch( index.column() ) { + case ResourceColumn: + switch( role ) { + case Qt::DisplayRole: + case Qt::EditRole: + return res.genericLabel(); + + case Qt::DecorationRole: { + QString iconName = res.genericIcon(); + if( !iconName.isEmpty() ) { + return KIcon( iconName ); + } + else { + QIcon icon = Types::Class(res.type()).icon(); + if( !icon.isNull() ) + return icon; + else + return QVariant(); + } + } + + case Qt::ToolTipRole: + return KUrl( res.uri() ).prettyUrl(); + + } + + case ResourceTypeColumn: + switch( role ) { + case Qt::DisplayRole: + case Qt::EditRole: + return Types::Class( res.type() ).label(); + + case Qt::DecorationRole: { + QIcon icon = Types::Class(res.type()).icon(); + if( !icon.isNull() ) + return icon; + else + return QVariant(); + } + + case Qt::ToolTipRole: + return KUrl(res.type()).prettyUrl(); + } + } + + + // + // Part 2: column-independant data + // + switch( role ) { + case KCategorizedSortFilterProxyModel::CategorySortRole: + // FIXME: sort files before other stuff and so on + + case KCategorizedSortFilterProxyModel::CategoryDisplayRole: { + Q_ASSERT( !res.type().isEmpty() ); + Nepomuk2::Types::Class c( res.type() ); + QString cat = c.label(); + if ( cat.isEmpty() ) { + cat = c.name(); + } + if ( c.uri() == Soprano::Vocabulary::RDFS::Resource() || cat.isEmpty() ) { + cat = i18nc( "@title KCategorizedSortFilterProxyModel grouping for all Nepomukj resources that are of type rdfs:Resource", "Miscellaneous" ); + } + + return cat; + } + + case ResourceRole: + return QVariant::fromValue( res ); + + case ResourceTypeRole: + return QVariant::fromValue( Nepomuk2::Types::Class(res.type()) ); + + case ResourceCreationDateRole: + return res.property( Soprano::Vocabulary::NAO::created() ).toDateTime(); + } + + // fallback + return QVariant(); +} + + +QVariant Nepomuk2::Utils::ResourceModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if( role == Qt::DisplayRole ) { + switch( section ) { + case ResourceColumn: + return i18nc("@title:column The Nepomuk resource label and icon", "Resource"); + case ResourceTypeColumn: + return i18nc("@title:column The Nepomuk resource's RDF type", "Resource Type"); + } + } + + return QAbstractItemModel::headerData(section, orientation, role); +} + + +Qt::ItemFlags Nepomuk2::Utils::ResourceModel::flags( const QModelIndex& index ) const +{ + if ( index.isValid() ) { + return QAbstractItemModel::flags( index ) | Qt::ItemIsDragEnabled; + } + else { + return QAbstractItemModel::flags( index ); + } +} + + +QMimeData* Nepomuk2::Utils::ResourceModel::mimeData( const QModelIndexList& indexes ) const +{ + KUrl::List uris; + foreach ( const QModelIndex& index, indexes ) { + if (index.isValid()) { + uris << index.data( ResourceRole ).value<Resource>().uri(); + } + } + + QMimeData* mimeData = new QMimeData(); + uris.populateMimeData( mimeData ); + + QByteArray data; + QDataStream s( &data, QIODevice::WriteOnly ); + s << uris; + mimeData->setData( mimeTypes().first(), data ); + + return mimeData; +} + + +QStringList Nepomuk2::Utils::ResourceModel::mimeTypes() const +{ + return( QStringList() + << QLatin1String( "application/x-nepomuk-resource-uri" ) + << KUrl::List::mimeDataTypes() ); +} + + +bool Nepomuk2::Utils::ResourceModel::setData( const QModelIndex& index, const QVariant& value, int role ) +{ + return QAbstractItemModel::setData(index, value, role); +} + +#include "resourcemodel.moc" diff --git a/src/nepomuk/utils/resourcemodel.h b/src/nepomuk/utils/resourcemodel.h new file mode 100644 index 00000000..526c7ac0 --- /dev/null +++ b/src/nepomuk/utils/resourcemodel.h @@ -0,0 +1,177 @@ +/* + This file is part of the Nepomuk KDE project. + Copyright (C) 2007-2010 Sebastian Trueg <trueg@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _NEPOMUK_RESOUCE_MODEL_H_ +#define _NEPOMUK_RESOUCE_MODEL_H_ + +#include <QtCore/QAbstractItemModel> + +#include "nepomukutils_export.h" + +namespace Nepomuk2 { + + class Resource; + + namespace Utils { + /** + * \class ResourceModel resourcemodel.h Nepomuk/Utils/ResourceModel + * + * \brief Base class for all models providing a plain list of resources. + * + * The %ResourceModel is a base class for models that handle a set of + * %Nepomuk Resource instances. This can be a simple list as in SimpleResourceModel + * or a set of query results as in ResultModel. It could also be a dynamic list + * which is updated while the user scrolls it. + * + * %ResourceModel cannot be instanciated by itself. Use one of the subclasses + * or derive your own subclass from it. + * + * At least the following methods need to be implemented in a subclass: + * \li resourceForIndex() + * \li indexForResource(), + * \li QAbstractItemModel::rowCount() + * \li QAbstractItemModel::index() + * + * \author Sebastian Trueg <trueg@kde.org> + * + * \since 4.6 + */ + class NEPOMUKUTILS_EXPORT ResourceModel : public QAbstractItemModel + { + Q_OBJECT + + public: + /** + * Constructor. + */ + ResourceModel( QObject* parent = 0 ); + + /** + * Destructor + */ + virtual ~ResourceModel(); + + /** + * The columns supported by ResourceModel are identified + * by this enumeration. + */ + enum ResourceModelColumns { + /// The first column displays the label of the resource and its icon + ResourceColumn = 0, + + /// The second column displays the resource's type + ResourceTypeColumn = 1, + + /// The number of columns + ResourceModelColumnCount = 2 + }; + + /** + * Custom roles that can be accessed for example in delegates. + */ + enum ResourceRoles { + /** + * The resource itself, provided as a Nepomuk::Resource instance. + */ + ResourceRole = 7766897, + + /** + * The type of the resource, provided as a Nepomuk::Types::Class + * instance. + */ + ResourceTypeRole = 687585, + + /** + * The creation date of the resource. + */ + ResourceCreationDateRole = 7766898 + }; + + /** + * Get the Resource which corresponds to \p index. + * + * \return The Resource which corresponds to \p index or an invalid Resource + * if \p index is invalid. + */ + virtual Resource resourceForIndex( const QModelIndex& index ) const = 0; + + /** + * Get the index for a resource. + * + * \return The index which corresponds to \p res of an invalid QModelIndex + * if \p res is not part of this model. + */ + virtual QModelIndex indexForResource( const Resource& res ) const = 0; + + /** + * The default implementation returns an invalid QModelIndex, thus providing + * a plain list. + */ + virtual QModelIndex parent( const QModelIndex& child ) const; + + /** + * The default implementation returns 2 with the first column representing the resource + * itself and the second one showing the type. + */ + virtual int columnCount( const QModelIndex& parent ) const; + + /** + * Handles most roles typically used in applications like Qt::DisplayRole, Qt::ToolTipRole, + * and Qt::DecorationRole. Additionally KCategorizedSortFilterProxyModel roles are supported + * categorizing by resource types. + */ + virtual QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const; + + /** + * Provides header data for the supported columns. + */ + virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; + + /** + * Reimplemented to support dragging of resources out of the model. + */ + virtual Qt::ItemFlags flags( const QModelIndex& index ) const; + + /** + * Stores the resource URIs via KUrl::List::populateMimeData() and as a specific + * "application/x-nepomuk-resource-uri" mime type to indicate that these are URIs + * corresponding to actual %Nepomuk resources. + */ + virtual QMimeData* mimeData( const QModelIndexList& indexes ) const; + + /** + * \return The KUrl mime types and "application/x-nepomuk-resource-uri". + */ + virtual QStringList mimeTypes() const; + + /** + * Provided for future extensions. + */ + virtual bool setData( const QModelIndex& index, const QVariant& value, int role ); + + private: + class Private; + Private* const d; + }; + } +} + +#endif diff --git a/src/nepomuk/utils/simpleresourcemodel.cpp b/src/nepomuk/utils/simpleresourcemodel.cpp new file mode 100644 index 00000000..f500f648 --- /dev/null +++ b/src/nepomuk/utils/simpleresourcemodel.cpp @@ -0,0 +1,170 @@ +/* + This file is part of the Nepomuk KDE project. + Copyright (C) 2008-2010 Sebastian Trueg <trueg@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "simpleresourcemodel.h" + +#include <QtCore/QUrl> +#include <QtCore/QList> + +#include <Nepomuk2/Resource> +#include <Nepomuk2/Query/Result> + +#include "kdebug.h" +#include "kurl.h" + + +class Nepomuk2::Utils::SimpleResourceModel::Private +{ +public: + QList<Nepomuk2::Resource> resources; +}; + + +Nepomuk2::Utils::SimpleResourceModel::SimpleResourceModel( QObject* parent ) + : ResourceModel( parent ), + d( new Private() ) +{ +} + + +Nepomuk2::Utils::SimpleResourceModel::~SimpleResourceModel() +{ + delete d; +} + + +QModelIndex Nepomuk2::Utils::SimpleResourceModel::indexForResource( const Resource& res ) const +{ + Q_ASSERT( res.isValid() ); + // FIXME: performance + int i = 0; + QList<Nepomuk2::Resource>::const_iterator end = d->resources.constEnd(); + for ( QList<Nepomuk2::Resource>::const_iterator it = d->resources.constBegin(); it != end; ++it ) { + if ( *it == res ) { + return index( i, 0 ); + } + ++i; + } + + return QModelIndex(); +} + + +Nepomuk2::Resource Nepomuk2::Utils::SimpleResourceModel::resourceForIndex( const QModelIndex& index ) const +{ + if ( index.isValid() && index.row() < d->resources.count() ) { + return d->resources[index.row()]; + } + else { + return Resource(); + } +} + + +int Nepomuk2::Utils::SimpleResourceModel::rowCount( const QModelIndex& parent ) const +{ + if ( parent.isValid() ) { + return 0; + } + else { + return d->resources.count(); + } +} + + +QModelIndex Nepomuk2::Utils::SimpleResourceModel::index( int row, int column, const QModelIndex& parent ) const +{ + if ( !parent.isValid() && row < d->resources.count() ) { + return createIndex( row, column, 0 ); + } + else { + return QModelIndex(); + } +} + + +bool Nepomuk2::Utils::SimpleResourceModel::removeRows(int row, int count, const QModelIndex& parent) +{ + if( count < 1 || row < 0 || (row + count) > d->resources.size() || parent.isValid() ) + return false; + + beginRemoveRows( parent, row, row + count -1 ); + + QList<Resource>::iterator begin, end; + begin = end = d->resources.begin(); + begin += row; + end += row + count; + d->resources.erase( begin, end ); + + endRemoveRows(); + return true; +} + + +void Nepomuk2::Utils::SimpleResourceModel::setResources( const QList<Nepomuk2::Resource>& resources ) +{ + d->resources = resources; + reset(); +} + + +void Nepomuk2::Utils::SimpleResourceModel::addResources( const QList<Nepomuk2::Resource>& resources ) +{ + if(!resources.isEmpty()) { + beginInsertRows( QModelIndex(), d->resources.count(), d->resources.count() + resources.count() - 1 ); + d->resources << resources; + endInsertRows(); + } +} + + +void Nepomuk2::Utils::SimpleResourceModel::addResource( const Nepomuk2::Resource& resource ) +{ + addResources( QList<Resource>() << resource ); +} + + +void Nepomuk2::Utils::SimpleResourceModel::setResults( const QList<Nepomuk2::Query::Result>& results) +{ + clear(); + addResults( results ); +} + +void Nepomuk2::Utils::SimpleResourceModel::addResults( const QList<Nepomuk2::Query::Result>& results ) +{ + Q_FOREACH( const Query::Result& result, results ) { + addResource( result.resource() ); + } +} + +void Nepomuk2::Utils::SimpleResourceModel::addResult( const Nepomuk2::Query::Result result ) +{ + addResource( result.resource() ); +} + + +void Nepomuk2::Utils::SimpleResourceModel::clear() +{ + d->resources.clear(); + reset(); +} + +#include "simpleresourcemodel.moc" diff --git a/src/nepomuk/utils/simpleresourcemodel.h b/src/nepomuk/utils/simpleresourcemodel.h new file mode 100644 index 00000000..2f48d930 --- /dev/null +++ b/src/nepomuk/utils/simpleresourcemodel.h @@ -0,0 +1,144 @@ +/* + This file is part of the Nepomuk KDE project. + Copyright (C) 2008-2010 Sebastian Trueg <trueg@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _NEPOMUK_SIMPLE_RESOURCE_MODEL_H_ +#define _NEPOMUK_SIMPLE_RESOURCE_MODEL_H_ + +#include "resourcemodel.h" + +#include "nepomukutils_export.h" + +#include <Nepomuk2/Resource> +#include <Nepomuk2/Query/Result> + +#include <QtCore/QList> + +namespace Nepomuk2 { + namespace Utils { + /** + * \class SimpleResourceModel simpleresourcemodel.h Nepomuk/Utils/SimpleResourceModel + * + * A simple ResourceModel that handles a list of Resource instances which + * can be managed via the setResources(), addResource(), addResources(), and + * clear() methods. + * + * \author Sebastian Trueg <trueg@kde.org> + * + * \since 4.6 + */ + class NEPOMUKUTILS_EXPORT SimpleResourceModel : public ResourceModel + { + Q_OBJECT + + public: + /** + * Creates an empty resource model. + */ + SimpleResourceModel( QObject* parent = 0 ); + + /** + * Destructor + */ + ~SimpleResourceModel(); + + /** + * Get the Resource which corresponds to \p index. + * + * \return The Resource which corresponds to \p index or an invalid Resource + * if \p index is invalid. + */ + QModelIndex indexForResource( const Resource& res ) const; + + /** + * Get the index for a resource. + * + * \return The index which corresponds to \p res of an invalid QModelIndex + * if \p res is not part of this model. + */ + Resource resourceForIndex( const QModelIndex& index ) const; + + /** + * \return The number of resources added to the model for an invalid parent index. + */ + int rowCount( const QModelIndex& parent = QModelIndex() ) const; + + /** + * Creates an index for the cell at \p row and \p column. + */ + QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const; + + /** + * Removes those resources from the model. + */ + bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()); + + public Q_SLOTS: + /** + * Set the resources to be provided by the model to \p resources. + */ + void setResources( const QList<Nepomuk2::Resource>& resources ); + + /** + * Add \p resources to the list of resources being provided by the + * model. + */ + void addResources( const QList<Nepomuk2::Resource>& resources ); + + /** + * Add \p resource to the list of resources being provided by the + * model. + */ + void addResource( const Nepomuk2::Resource& resource ); + + /** + * This method is similar to setResources(). It is provided for + * allowing convenient connections from signals that provide + * Query::Result objects. + */ + void setResults( const QList<Nepomuk2::Query::Result>& results ); + + /** + * This method is similar to addResources(). It is provided for + * allowing convenient connections from signals that provide + * Query::Result objects like Query::QueryServiceClient::newResults(). + */ + void addResults( const QList<Nepomuk2::Query::Result>& results ); + + /** + * This method is similar to addResource(). It is provided for + * allowing convenient connections from signals that provide + * Query::Result objects. + */ + void addResult( const Nepomuk2::Query::Result result ); + + /** + * Clear the model by removing all resources added via setResources() and friends. + */ + void clear(); + + private: + class Private; + Private* const d; + }; + } +} + +#endif |