~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to libs/widgets/KoResourceModel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
 * Copyright (C) 2008-2009 Jan Hambrecht <jaham@gmx.net>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
#include "KoResourceModel.h"
 
21
 
 
22
#include <klocale.h>
 
23
 
 
24
#include <KoResourceServerAdapter.h>
 
25
#include <math.h>
 
26
 
 
27
KoResourceModel::KoResourceModel( KoAbstractResourceServerAdapter * resourceAdapter, QObject * parent )
 
28
    : QAbstractTableModel( parent ), m_resourceAdapter(resourceAdapter), m_columnCount(4)
 
29
{
 
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*)));
 
36
}
 
37
 
 
38
int KoResourceModel::rowCount( const QModelIndex &/*parent*/ ) const
 
39
{
 
40
    int resourceCount = m_resourceAdapter->resources().count();
 
41
    if (!resourceCount)
 
42
        return 0;
 
43
 
 
44
    return static_cast<int>(ceil(static_cast<qreal>(resourceCount) / m_columnCount));
 
45
}
 
46
 
 
47
int KoResourceModel::columnCount ( const QModelIndex & ) const
 
48
{
 
49
    return m_columnCount;
 
50
}
 
51
 
 
52
QVariant KoResourceModel::data( const QModelIndex &index, int role ) const
 
53
{
 
54
    if( ! index.isValid() )
 
55
         return QVariant();
 
56
 
 
57
    switch( role )
 
58
    {
 
59
        case Qt::DisplayRole:
 
60
        {
 
61
            KoResource * resource = static_cast<KoResource*>(index.internalPointer());
 
62
            if( ! resource )
 
63
                return QVariant();
 
64
 
 
65
            return QVariant( i18n( resource->name().toUtf8().data() ) );
 
66
        }
 
67
        case Qt::DecorationRole:
 
68
        {
 
69
            KoResource * resource = static_cast<KoResource*>(index.internalPointer());
 
70
            if( ! resource )
 
71
                return QVariant();
 
72
 
 
73
            return QVariant( resource->image() );
 
74
        }
 
75
        case KoResourceModel::LargeThumbnailRole:
 
76
        {
 
77
            KoResource * resource = static_cast<KoResource*>(index.internalPointer());
 
78
            if( ! resource )
 
79
                return QVariant();
 
80
 
 
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() );
 
86
 
 
87
                qreal scale = qMin( scaleW, scaleH );
 
88
 
 
89
                int thumbW = static_cast<int>( imageSize.width() * scale );
 
90
                int thumbH = static_cast<int>( imageSize.height() * scale );
 
91
 
 
92
                return QVariant(resource->image().scaled( thumbW, thumbH, Qt::IgnoreAspectRatio ));
 
93
            }
 
94
            else
 
95
                return QVariant(resource->image());
 
96
        }
 
97
 
 
98
        default:
 
99
            return QVariant();
 
100
    }
 
101
}
 
102
 
 
103
QModelIndex KoResourceModel::index ( int row, int column, const QModelIndex & ) const
 
104
{
 
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();
 
109
 
 
110
    return createIndex( row, column, resources[index] );
 
111
}
 
112
 
 
113
void KoResourceModel::setColumnCount( int columnCount )
 
114
{
 
115
    if (columnCount != m_columnCount) {
 
116
        m_columnCount = columnCount;
 
117
        reset();
 
118
    }
 
119
}
 
120
 
 
121
KoAbstractResourceServerAdapter * KoResourceModel::resourceServerAdapter()
 
122
{
 
123
    return m_resourceAdapter;
 
124
}
 
125
 
 
126
void KoResourceModel::resourceAdded(KoResource *resource)
 
127
{
 
128
    int newIndex = m_resourceAdapter->resources().indexOf(resource);
 
129
    if (newIndex < 0)
 
130
        return;
 
131
    
 
132
    reset();
 
133
}
 
134
 
 
135
void KoResourceModel::resourceRemoved(KoResource *resource)
 
136
{
 
137
    Q_UNUSED(resource);
 
138
    reset();
 
139
}
 
140
 
 
141
#include <KoResourceModel.moc>