Warning, file /office/calligra/libs/widgets/KoResourceModel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2008-2009 Jan Hambrecht <jaham@gmx.net>
0003  * Copyright (c) 2013 Sascha Suelzer <s.suelzer@gmail.com>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoResourceModel.h"
0022 
0023 #include <klocalizedstring.h>
0024 #include <kconfiggroup.h>
0025 #include <ksharedconfig.h>
0026 
0027 #include <KoResourceServerAdapter.h>
0028 #include <math.h>
0029 
0030 KoResourceModel::KoResourceModel(QSharedPointer<KoAbstractResourceServerAdapter> resourceAdapter, QObject * parent)
0031     : KoResourceModelBase(parent)
0032     , m_resourceAdapter(resourceAdapter)
0033     , m_columnCount(4)
0034 {
0035     Q_ASSERT(m_resourceAdapter);
0036     m_resourceAdapter->connectToResourceServer();
0037     connect(m_resourceAdapter.data(), SIGNAL(resourceAdded(KoResource*)),
0038             this, SLOT(resourceAdded(KoResource*)));
0039     connect(m_resourceAdapter.data(), SIGNAL(removingResource(KoResource*)),
0040             this, SLOT(resourceRemoved(KoResource*)));
0041     connect(m_resourceAdapter.data(), SIGNAL(resourceChanged(KoResource*)),
0042             this, SLOT(resourceChanged(KoResource*)));
0043     connect(m_resourceAdapter.data(), SIGNAL(tagsWereChanged()),
0044             this, SLOT(tagBoxEntryWasModified()));
0045     connect(m_resourceAdapter.data(), SIGNAL(tagCategoryWasAdded(QString)),
0046             this, SLOT(tagBoxEntryWasAdded(QString)));
0047     connect(m_resourceAdapter.data(), SIGNAL(tagCategoryWasRemoved(QString)),
0048             this, SLOT(tagBoxEntryWasRemoved(QString)));
0049 }
0050 
0051 KoResourceModel::~KoResourceModel()
0052 {
0053     if (!m_currentTag.isEmpty()) {
0054         KConfigGroup group =  KSharedConfig::openConfig()->group("SelectedTags");
0055         group.writeEntry(serverType(), m_currentTag);
0056     }
0057 
0058 }
0059 
0060 int KoResourceModel::rowCount( const QModelIndex &/*parent*/ ) const
0061 {
0062     int resourceCount = m_resourceAdapter->resources().count();
0063     if (!resourceCount)
0064         return 0;
0065 
0066     return static_cast<int>(ceil(static_cast<qreal>(resourceCount) / m_columnCount));
0067 }
0068 
0069 int KoResourceModel::columnCount ( const QModelIndex & ) const
0070 {
0071     return m_columnCount;
0072 }
0073 
0074 QVariant KoResourceModel::data( const QModelIndex &index, int role ) const
0075 {
0076     if( ! index.isValid() )
0077          return QVariant();
0078 
0079     switch( role )
0080     {
0081         case Qt::DisplayRole:
0082         {
0083             KoResource * resource = static_cast<KoResource*>(index.internalPointer());
0084             if( ! resource )
0085                 return QVariant();
0086             QString resName = i18n( resource->name().toUtf8().data());
0087 
0088             if (m_resourceAdapter->assignedTagsList(resource).count()) {
0089                 QString taglist = m_resourceAdapter->assignedTagsList(resource).join("] , [");
0090                 QString tagListToolTip = QString(" - %1: [%2]").arg(i18n("Tags"), taglist);
0091                 return QVariant( resName + tagListToolTip );
0092             }
0093             return QVariant( resName );
0094         }
0095         case Qt::DecorationRole:
0096         {
0097             KoResource * resource = static_cast<KoResource*>(index.internalPointer());
0098             if( ! resource )
0099                 return QVariant();
0100 
0101             return QVariant( resource->image() );
0102         }
0103         case KoResourceModel::LargeThumbnailRole:
0104         {
0105             KoResource * resource = static_cast<KoResource*>(index.internalPointer());
0106             if( ! resource )
0107                 return QVariant();
0108 
0109             QSize imageSize = resource->image().size();
0110             QSize thumbSize( 100, 100 );
0111             if(imageSize.height() > thumbSize.height() || imageSize.width() > thumbSize.width()) {
0112                 qreal scaleW = static_cast<qreal>( thumbSize.width() ) / static_cast<qreal>( imageSize.width() );
0113                 qreal scaleH = static_cast<qreal>( thumbSize.height() ) / static_cast<qreal>( imageSize.height() );
0114 
0115                 qreal scale = qMin( scaleW, scaleH );
0116 
0117                 int thumbW = static_cast<int>( imageSize.width() * scale );
0118                 int thumbH = static_cast<int>( imageSize.height() * scale );
0119 
0120                 return QVariant(resource->image().scaled( thumbW, thumbH, Qt::IgnoreAspectRatio ));
0121             }
0122             else
0123                 return QVariant(resource->image());
0124         }
0125 
0126         default:
0127             return QVariant();
0128     }
0129 }
0130 
0131 QModelIndex KoResourceModel::index ( int row, int column, const QModelIndex & ) const
0132 {
0133     int index = row * m_columnCount + column;
0134     const QList<KoResource*> resources = m_resourceAdapter->resources();
0135     if( index >= resources.count() || index < 0)
0136         return QModelIndex();
0137 
0138     return createIndex( row, column, resources[index] );
0139 }
0140 
0141 void KoResourceModel::doSafeLayoutReset(KoResource *activateAfterReformat)
0142 {
0143     emit beforeResourcesLayoutReset(activateAfterReformat);
0144     beginResetModel();
0145     endResetModel();
0146     emit afterResourcesLayoutReset();
0147 }
0148 
0149 void KoResourceModel::setColumnCount( int columnCount )
0150 {
0151     if (columnCount != m_columnCount) {
0152         emit beforeResourcesLayoutReset(0);
0153         beginResetModel();
0154         m_columnCount = columnCount;
0155         endResetModel();
0156         emit afterResourcesLayoutReset();
0157     }
0158 }
0159 
0160 void KoResourceModel::resourceAdded(KoResource *resource)
0161 {
0162     int newIndex = m_resourceAdapter->resources().indexOf(resource);
0163     if (newIndex >= 0) {
0164         doSafeLayoutReset(0);
0165     }
0166 }
0167 
0168 void KoResourceModel::resourceRemoved(KoResource *resource)
0169 {
0170     Q_UNUSED(resource);
0171 
0172     KoResource *first = !m_resourceAdapter->resources().isEmpty() ? m_resourceAdapter->resources().first() : 0;
0173     doSafeLayoutReset(first);
0174 }
0175 
0176 void KoResourceModel::resourceChanged(KoResource* resource)
0177 {
0178     int resourceIndex = m_resourceAdapter->resources().indexOf(resource);
0179     int row = resourceIndex / columnCount();
0180     int column = resourceIndex % columnCount();
0181 
0182     QModelIndex modelIndex = index(row, column);
0183     if (!modelIndex.isValid()) {
0184         return;
0185     }
0186 
0187     emit dataChanged(modelIndex, modelIndex);
0188 }
0189 
0190 void KoResourceModel::tagBoxEntryWasModified()
0191 {
0192     m_resourceAdapter->updateServer();
0193     emit tagBoxEntryModified();
0194 }
0195 
0196 void KoResourceModel::tagBoxEntryWasAdded(const QString& tag)
0197 {
0198     emit tagBoxEntryAdded(tag);
0199 }
0200 
0201 void KoResourceModel::tagBoxEntryWasRemoved(const QString& tag)
0202 {
0203     emit tagBoxEntryRemoved(tag);
0204 }
0205 
0206 QModelIndex KoResourceModel::indexFromResource(KoResource* resource) const
0207 {
0208     int resourceIndex = m_resourceAdapter->resources().indexOf(resource);
0209     if (columnCount() > 0) {
0210         int row = resourceIndex / columnCount();
0211         int column = resourceIndex % columnCount();
0212         return index(row, column);
0213     }
0214     return QModelIndex();
0215 }
0216 
0217 QString KoResourceModel::extensions() const
0218 {
0219     return m_resourceAdapter->extensions();
0220 }
0221 
0222 void KoResourceModel::importResourceFile(const QString &filename)
0223 {
0224     m_resourceAdapter->importResourceFile(filename);
0225 }
0226 
0227 void KoResourceModel::importResourceFile(const QString & filename, bool fileCreation)
0228 {
0229     m_resourceAdapter->importResourceFile(filename, fileCreation);
0230 }
0231 
0232 bool KoResourceModel::removeResource(KoResource* resource)
0233 {
0234     return m_resourceAdapter->removeResource(resource);
0235 }
0236 
0237 void KoResourceModel::removeResourceFile(const QString &filename)
0238 {
0239     m_resourceAdapter->removeResourceFile(filename);
0240 }
0241 
0242 QStringList KoResourceModel::assignedTagsList(KoResource *resource) const
0243 {
0244     return m_resourceAdapter->assignedTagsList(resource);
0245 }
0246 
0247 void KoResourceModel::addTag(KoResource* resource, const QString& tag)
0248 {
0249     m_resourceAdapter->addTag(resource, tag);
0250     emit tagBoxEntryAdded(tag);
0251 }
0252 
0253 void KoResourceModel::deleteTag(KoResource *resource, const QString &tag)
0254 {
0255     m_resourceAdapter->deleteTag(resource, tag);
0256 }
0257 
0258 QStringList KoResourceModel::tagNamesList() const
0259 {
0260     return m_resourceAdapter->tagNamesList();
0261 }
0262 
0263 QStringList KoResourceModel::searchTag(const QString& lineEditText)
0264 {
0265     return m_resourceAdapter->searchTag(lineEditText);
0266 }
0267 
0268 void KoResourceModel::searchTextChanged(const QString& searchString)
0269 {
0270     m_resourceAdapter->searchTextChanged(searchString);
0271 }
0272 
0273 void KoResourceModel::enableResourceFiltering(bool enable)
0274 {
0275     m_resourceAdapter->enableResourceFiltering(enable);
0276 }
0277 
0278 void KoResourceModel::setCurrentTag(const QString& currentTag)
0279 {
0280     m_currentTag = currentTag;
0281     m_resourceAdapter->setCurrentTag(currentTag);
0282 }
0283 
0284 void KoResourceModel::updateServer()
0285 {
0286     m_resourceAdapter->updateServer();
0287 }
0288 
0289 int KoResourceModel::resourcesCount() const
0290 {
0291     return m_resourceAdapter->resources().count();
0292 }
0293 
0294 QList<KoResource *> KoResourceModel::currentlyVisibleResources() const
0295 {
0296   return m_resourceAdapter->resources();
0297 }
0298 
0299 void KoResourceModel::tagCategoryMembersChanged()
0300 {
0301     m_resourceAdapter->tagCategoryMembersChanged();
0302 }
0303 
0304 void KoResourceModel::tagCategoryAdded(const QString& tag)
0305 {
0306     m_resourceAdapter->tagCategoryAdded(tag);
0307 }
0308 
0309 void KoResourceModel::tagCategoryRemoved(const QString& tag)
0310 {
0311     m_resourceAdapter->tagCategoryRemoved(tag);
0312 }
0313 
0314 QString KoResourceModel::serverType() const
0315 {
0316     return m_resourceAdapter->serverType();
0317 }
0318 
0319 QList< KoResource* > KoResourceModel::serverResources() const
0320 {
0321     return m_resourceAdapter->serverResources();
0322 }