KoResourceModel.cpp 9.61 KB
Newer Older
1
/* This file is part of the KDE project
2
 * Copyright (C) 2008-2009 Jan Hambrecht <jaham@gmx.net>
3
 * Copyright (c) 2013 Sascha Suelzer <s.suelzer@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */

#include "KoResourceModel.h"

23
#include <klocalizedstring.h>
24
#include <kconfiggroup.h>
25
#include <ksharedconfig.h>
26

27
#include <KoResourceServerAdapter.h>
28
#include <math.h>
29

30
KoResourceModel::KoResourceModel(QSharedPointer<KoAbstractResourceServerAdapter> resourceAdapter, QObject * parent)
31
    : QAbstractTableModel(parent)
32 33
    , m_resourceAdapter(resourceAdapter)
    , m_columnCount(4)
34
{
35
    Q_ASSERT(m_resourceAdapter);
36
    m_resourceAdapter->connectToResourceServer();
37
    connect(m_resourceAdapter.data(), SIGNAL(resourceAdded(KoResource*)),
38
            this, SLOT(resourceAdded(KoResource*)));
39
    connect(m_resourceAdapter.data(), SIGNAL(removingResource(KoResource*)),
40
            this, SLOT(resourceRemoved(KoResource*)));
41
    connect(m_resourceAdapter.data(), SIGNAL(resourceChanged(KoResource*)),
42
            this, SLOT(resourceChanged(KoResource*)));
43
    connect(m_resourceAdapter.data(), SIGNAL(tagsWereChanged()),
44
            this, SLOT(tagBoxEntryWasModified()));
45
    connect(m_resourceAdapter.data(), SIGNAL(tagCategoryWasAdded(QString)),
46
            this, SLOT(tagBoxEntryWasAdded(QString)));
47
    connect(m_resourceAdapter.data(), SIGNAL(tagCategoryWasRemoved(QString)),
48
            this, SLOT(tagBoxEntryWasRemoved(QString)));
49 50
}

51 52
KoResourceModel::~KoResourceModel()
{
53
    if (!m_currentTag.isEmpty()) {
54
        KConfigGroup group =  KSharedConfig::openConfig()->group("SelectedTags");
55 56 57
        group.writeEntry(serverType(), m_currentTag);
    }

58 59
}

60 61
int KoResourceModel::rowCount( const QModelIndex &/*parent*/ ) const
{
62 63 64 65 66
    int resourceCount = m_resourceAdapter->resources().count();
    if (!resourceCount)
        return 0;

    return static_cast<int>(ceil(static_cast<qreal>(resourceCount) / m_columnCount));
67 68 69 70 71 72 73 74 75 76 77 78 79 80
}

int KoResourceModel::columnCount ( const QModelIndex & ) const
{
    return m_columnCount;
}

QVariant KoResourceModel::data( const QModelIndex &index, int role ) const
{
    if( ! index.isValid() )
         return QVariant();

    switch( role )
    {
81 82 83 84 85
        case Qt::DisplayRole:
        {
            KoResource * resource = static_cast<KoResource*>(index.internalPointer());
            if( ! resource )
                return QVariant();
86
            QString resName = i18n( resource->name().toUtf8().data());
87

Bernhard Liebl's avatar
Bernhard Liebl committed
88 89 90 91 92 93 94
            return QVariant( resName );
        }
        case KoResourceModel::TagsRole:
        {
            KoResource * resource = static_cast<KoResource*>(index.internalPointer());
            if( ! resource )
                return QVariant();
95
            if (m_resourceAdapter->assignedTagsList(resource).count()) {
Bernhard Liebl's avatar
Bernhard Liebl committed
96 97 98 99
                QString taglist = m_resourceAdapter->assignedTagsList(resource).join("</li><li>");
                return QString("<li>%2</li>").arg(taglist);
            } else {
                return QString();
100
            }
101
        }
102 103 104 105 106 107
        case Qt::DecorationRole:
        {
            KoResource * resource = static_cast<KoResource*>(index.internalPointer());
            if( ! resource )
                return QVariant();

Boudewijn Rempt's avatar
Boudewijn Rempt committed
108
            return QVariant( resource->image() );
109
        }
110
        case KoResourceModel::LargeThumbnailRole:
111 112 113 114 115
        {
            KoResource * resource = static_cast<KoResource*>(index.internalPointer());
            if( ! resource )
                return QVariant();

Boudewijn Rempt's avatar
Boudewijn Rempt committed
116
            QSize imageSize = resource->image().size();
117
            QSize thumbSize( 100, 100 );
Boudewijn Rempt's avatar
Boudewijn Rempt committed
118 119 120
            if(imageSize.height() > thumbSize.height() || imageSize.width() > thumbSize.width()) {
                qreal scaleW = static_cast<qreal>( thumbSize.width() ) / static_cast<qreal>( imageSize.width() );
                qreal scaleH = static_cast<qreal>( thumbSize.height() ) / static_cast<qreal>( imageSize.height() );
121 122 123

                qreal scale = qMin( scaleW, scaleH );

Boudewijn Rempt's avatar
Boudewijn Rempt committed
124 125
                int thumbW = static_cast<int>( imageSize.width() * scale );
                int thumbH = static_cast<int>( imageSize.height() * scale );
126

127
                return QVariant(resource->image().scaled( thumbW, thumbH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
128 129
            }
            else
Boudewijn Rempt's avatar
Boudewijn Rempt committed
130
                return QVariant(resource->image());
131 132 133 134 135 136 137 138 139 140
        }

        default:
            return QVariant();
    }
}

QModelIndex KoResourceModel::index ( int row, int column, const QModelIndex & ) const
{
    int index = row * m_columnCount + column;
141 142
    const QList<KoResource*> resources = m_resourceAdapter->resources();
    if( index >= resources.count() || index < 0)
143 144
        return QModelIndex();

145
    return createIndex( row, column, resources[index] );
146 147
}

148 149 150
void KoResourceModel::doSafeLayoutReset(KoResource *activateAfterReformat)
{
    emit beforeResourcesLayoutReset(activateAfterReformat);
151 152
    beginResetModel();
    endResetModel();
153 154 155
    emit afterResourcesLayoutReset();
}

156 157
void KoResourceModel::setColumnCount( int columnCount )
{
158
    if (columnCount != m_columnCount) {
159
        emit beforeResourcesLayoutReset(0);
160
        m_columnCount = columnCount;
161 162
        beginResetModel();
        endResetModel();
163
        emit afterResourcesLayoutReset();
164
    }
165 166
}

167 168 169
void KoResourceModel::resourceAdded(KoResource *resource)
{
    int newIndex = m_resourceAdapter->resources().indexOf(resource);
170 171 172
    if (newIndex >= 0) {
        doSafeLayoutReset(0);
    }
173 174 175 176
}

void KoResourceModel::resourceRemoved(KoResource *resource)
{
177
    Q_UNUSED(resource);
178
    doSafeLayoutReset(0);
179 180
}

181 182 183
void KoResourceModel::resourceChanged(KoResource* resource)
{
    int resourceIndex = m_resourceAdapter->resources().indexOf(resource);
184 185
    int row = resourceIndex / columnCount();
    int column = resourceIndex % columnCount();
186 187 188 189 190

    QModelIndex modelIndex = index(row, column);
    if (!modelIndex.isValid()) {
        return;
    }
191

192 193 194
    emit dataChanged(modelIndex, modelIndex);
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
void KoResourceModel::tagBoxEntryWasModified()
{
    m_resourceAdapter->updateServer();
    emit tagBoxEntryModified();
}

void KoResourceModel::tagBoxEntryWasAdded(const QString& tag)
{
    emit tagBoxEntryAdded(tag);
}

void KoResourceModel::tagBoxEntryWasRemoved(const QString& tag)
{
    emit tagBoxEntryRemoved(tag);
}

211
QModelIndex KoResourceModel::indexFromResource(KoResource* resource) const
212 213
{
    int resourceIndex = m_resourceAdapter->resources().indexOf(resource);
214 215 216 217 218 219
    if (columnCount() > 0) {
        int row = resourceIndex / columnCount();
        int column = resourceIndex % columnCount();
        return index(row, column);
    }
    return QModelIndex();
220 221
}

222
QString KoResourceModel::extensions() const
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
{
    return m_resourceAdapter->extensions();
}

void KoResourceModel::importResourceFile(const QString &filename)
{
    m_resourceAdapter->importResourceFile(filename);
}

void KoResourceModel::importResourceFile(const QString & filename, bool fileCreation)
{
    m_resourceAdapter->importResourceFile(filename, fileCreation);
}

bool KoResourceModel::removeResource(KoResource* resource)
{
    return m_resourceAdapter->removeResource(resource);
}

void KoResourceModel::removeResourceFile(const QString &filename)
{
    m_resourceAdapter->removeResourceFile(filename);
}

247
QStringList KoResourceModel::assignedTagsList(KoResource *resource) const
248
{
249
    return m_resourceAdapter->assignedTagsList(resource);
250 251
}

252
void KoResourceModel::addTag(KoResource* resource, const QString& tag)
253 254
{
    m_resourceAdapter->addTag(resource, tag);
255
    emit tagBoxEntryAdded(tag);
256 257 258 259 260 261 262
}

void KoResourceModel::deleteTag(KoResource *resource, const QString &tag)
{
    m_resourceAdapter->deleteTag(resource, tag);
}

263
QStringList KoResourceModel::tagNamesList() const
264
{
265
    return m_resourceAdapter->tagNamesList();
266 267 268 269 270 271 272
}

QStringList KoResourceModel::searchTag(const QString& lineEditText)
{
    return m_resourceAdapter->searchTag(lineEditText);
}

273
void KoResourceModel::searchTextChanged(const QString& searchString)
274
{
275 276 277 278 279 280
    m_resourceAdapter->searchTextChanged(searchString);
}

void KoResourceModel::enableResourceFiltering(bool enable)
{
    m_resourceAdapter->enableResourceFiltering(enable);
281 282
}

283
void KoResourceModel::setCurrentTag(const QString& currentTag)
284
{
285
    m_currentTag = currentTag;
286
    m_resourceAdapter->setCurrentTag(currentTag);
287 288 289 290 291 292 293
}

void KoResourceModel::updateServer()
{
    m_resourceAdapter->updateServer();
}

294 295 296 297 298
int KoResourceModel::resourcesCount() const
{
    return m_resourceAdapter->resources().count();
}

299
QList<KoResource *> KoResourceModel::currentlyVisibleResources() const
300 301 302
{
  return m_resourceAdapter->resources();
}
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

void KoResourceModel::tagCategoryMembersChanged()
{
    m_resourceAdapter->tagCategoryMembersChanged();
}

void KoResourceModel::tagCategoryAdded(const QString& tag)
{
    m_resourceAdapter->tagCategoryAdded(tag);
}

void KoResourceModel::tagCategoryRemoved(const QString& tag)
{
    m_resourceAdapter->tagCategoryRemoved(tag);
}

319 320 321 322 323
QString KoResourceModel::serverType() const
{
    return m_resourceAdapter->serverType();
}

324 325 326 327
QList< KoResource* > KoResourceModel::serverResources() const
{
    return m_resourceAdapter->serverResources();
}