chalk

kis_icon_item.cpp
1/*
2 * Copyright (c) 1999 Matthias Elter <elter@kde.org>
3 * Copyright (c) 2003 Patrick Julien <freak@codepimps.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.g
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include <kdebug.h>
21
22#include "kis_resource.h"
23#include "kis_global.h"
24#include "kis_icon_item.h"
25
26#define THUMB_SIZE 30
27
28KisIconItem::KisIconItem(KisResource *resource)
29{
30 m_resource = resource;
31 validPixmap = false;
32 validThumb = false;
33 updatePixmaps();
34}
35
36KisIconItem::~KisIconItem()
37{
38}
39
40void KisIconItem::updatePixmaps()
41{
42 validPixmap = false;
43 validThumb = false;
44
45 if (m_resource && m_resource->valid()) {
46 TQImage img = m_resource->img();
47
48 if (img.isNull()) {
49 m_resource->setValid(false);
50 m_resource = 0;
51 return;
52 }
53
54 if (img.width() > THUMB_SIZE || img.height() > THUMB_SIZE) {
55 TQImage thumb = img;
56 TQ_INT32 xsize = THUMB_SIZE;
57 TQ_INT32 ysize = THUMB_SIZE;
58 TQ_INT32 picW = thumb.width();
59 TQ_INT32 picH = thumb.height();
60
61 if (picW > picH) {
62 float yFactor = (float)((float)(float)picH / (float)picW);
63
64 ysize = (TQ_INT32)(yFactor * (float)THUMB_SIZE);
65
66 if (ysize > THUMB_SIZE)
67 ysize = THUMB_SIZE;
68 } else if (picW < picH) {
69 float xFactor = (float)((float)picW / (float)picH);
70
71 xsize = (TQ_INT32)(xFactor * (float)THUMB_SIZE);
72
73 if (xsize > THUMB_SIZE)
74 xsize = THUMB_SIZE;
75 }
76
77 thumb = thumb.smoothScale(xsize, ysize);
78
79 if (!thumb.isNull()) {
80 m_thumb = TQPixmap(thumb);
81 validThumb = !m_thumb.isNull();
82 }
83 }
84
85 img = img.convertDepth(32);
86 m_pixmap = TQPixmap(img);
87 validPixmap = true;
88 }
89}
90
91TQPixmap& KisIconItem::pixmap() const
92{
93 return const_cast<TQPixmap&>(m_pixmap);
94}
95
96TQPixmap& KisIconItem::thumbPixmap() const
97{
98 return const_cast<TQPixmap&>(m_thumb);
99}
100
101KisResource *KisIconItem::resource() const
102{
103 return m_resource;
104}
105
106int KisIconItem::compare(const KoIconItem *o) const
107{
108 const KisIconItem *other = dynamic_cast<const KisIconItem *>(o);
109
110 if (other != 0) {
111 return m_resource->name().localeAwareCompare(other->m_resource->name());
112 } else {
113 return 0;
114 }
115}
116
The KisResource class provides a representation of Chalk image resources.