1
/* This file is part of the KDE project
2
* Copyright (C) 2008-2009 Jan Hambrecht <jaham@gmx.net>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "KoResourceModel.h"
24
#include <KoResourceServerAdapter.h>
27
KoResourceModel::KoResourceModel( KoAbstractResourceServerAdapter * resourceAdapter, QObject * parent )
28
: QAbstractTableModel( parent ), m_resourceAdapter(resourceAdapter), m_columnCount(4)
30
Q_ASSERT( m_resourceAdapter );
31
m_resourceAdapter->connectToResourceServer();
32
connect(m_resourceAdapter, SIGNAL(resourceAdded(KoResource*)),
33
this, SLOT(resourceAdded(KoResource*)));
34
connect(m_resourceAdapter, SIGNAL(removingResource(KoResource*)),
35
this, SLOT(resourceRemoved(KoResource*)));
38
int KoResourceModel::rowCount( const QModelIndex &/*parent*/ ) const
40
int resourceCount = m_resourceAdapter->resources().count();
44
return static_cast<int>(ceil(static_cast<qreal>(resourceCount) / m_columnCount));
47
int KoResourceModel::columnCount ( const QModelIndex & ) const
52
QVariant KoResourceModel::data( const QModelIndex &index, int role ) const
54
if( ! index.isValid() )
61
KoResource * resource = static_cast<KoResource*>(index.internalPointer());
65
return QVariant( i18n( resource->name().toUtf8().data() ) );
67
case Qt::DecorationRole:
69
KoResource * resource = static_cast<KoResource*>(index.internalPointer());
73
return QVariant( resource->image() );
75
case KoResourceModel::LargeThumbnailRole:
77
KoResource * resource = static_cast<KoResource*>(index.internalPointer());
81
QSize imageSize = resource->image().size();
82
QSize thumbSize( 100, 100 );
83
if(imageSize.height() > thumbSize.height() || imageSize.width() > thumbSize.width()) {
84
qreal scaleW = static_cast<qreal>( thumbSize.width() ) / static_cast<qreal>( imageSize.width() );
85
qreal scaleH = static_cast<qreal>( thumbSize.height() ) / static_cast<qreal>( imageSize.height() );
87
qreal scale = qMin( scaleW, scaleH );
89
int thumbW = static_cast<int>( imageSize.width() * scale );
90
int thumbH = static_cast<int>( imageSize.height() * scale );
92
return QVariant(resource->image().scaled( thumbW, thumbH, Qt::IgnoreAspectRatio ));
95
return QVariant(resource->image());
103
QModelIndex KoResourceModel::index ( int row, int column, const QModelIndex & ) const
105
int index = row * m_columnCount + column;
106
const QList<KoResource*> resources = m_resourceAdapter->resources();
107
if( index >= resources.count() || index < 0)
108
return QModelIndex();
110
return createIndex( row, column, resources[index] );
113
void KoResourceModel::setColumnCount( int columnCount )
115
if (columnCount != m_columnCount) {
116
m_columnCount = columnCount;
121
KoAbstractResourceServerAdapter * KoResourceModel::resourceServerAdapter()
123
return m_resourceAdapter;
126
void KoResourceModel::resourceAdded(KoResource *resource)
128
int newIndex = m_resourceAdapter->resources().indexOf(resource);
135
void KoResourceModel::resourceRemoved(KoResource *resource)
141
#include <KoResourceModel.moc>