krita

kis_icon_item.cc

00001 /*
00002  *  Copyright (c) 1999 Matthias Elter <elter@kde.org>
00003  *  Copyright (c) 2003 Patrick Julien  <freak@codepimps.org
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.g
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include <kdebug.h>
00021 
00022 #include "kis_resource.h"
00023 #include "kis_global.h"
00024 #include "kis_icon_item.h"
00025 
00026 #define THUMB_SIZE 30
00027 
00028 KisIconItem::KisIconItem(KisResource *resource)
00029 {
00030     m_resource = resource;
00031     validPixmap = false;
00032     validThumb = false;
00033     updatePixmaps();
00034 }
00035 
00036 KisIconItem::~KisIconItem()
00037 {
00038 }
00039 
00040 void KisIconItem::updatePixmaps()
00041 {
00042     validPixmap = false;
00043     validThumb = false;
00044 
00045     if (m_resource && m_resource->valid()) {
00046         QImage img = m_resource->img();
00047 
00048         if (img.isNull()) {
00049             m_resource->setValid(false);
00050             m_resource = 0;
00051             return;
00052         }
00053 
00054         if (img.width() > THUMB_SIZE || img.height() > THUMB_SIZE) {
00055             QImage thumb = img;
00056             Q_INT32 xsize = THUMB_SIZE;
00057             Q_INT32 ysize = THUMB_SIZE;
00058             Q_INT32 picW  = thumb.width();
00059             Q_INT32 picH  = thumb.height();
00060 
00061             if (picW > picH) {
00062                 float yFactor = (float)((float)(float)picH / (float)picW);
00063 
00064                 ysize = (Q_INT32)(yFactor * (float)THUMB_SIZE);
00065             
00066                 if (ysize > THUMB_SIZE) 
00067                     ysize = THUMB_SIZE;
00068             } else if (picW < picH) {
00069                 float xFactor = (float)((float)picW / (float)picH);
00070 
00071                 xsize = (Q_INT32)(xFactor * (float)THUMB_SIZE);
00072 
00073                 if (xsize > THUMB_SIZE) 
00074                     xsize = THUMB_SIZE;
00075             }
00076 
00077             thumb = thumb.smoothScale(xsize, ysize);
00078 
00079             if (!thumb.isNull()) {
00080                 m_thumb = QPixmap(thumb);
00081                 validThumb = !m_thumb.isNull();
00082             }
00083         }
00084 
00085         img = img.convertDepth(32);
00086         m_pixmap = QPixmap(img);
00087         validPixmap = true;
00088     }
00089 }
00090 
00091 QPixmap& KisIconItem::pixmap() const
00092 {
00093     return const_cast<QPixmap&>(m_pixmap);
00094 }
00095 
00096 QPixmap& KisIconItem::thumbPixmap() const
00097 {
00098     return const_cast<QPixmap&>(m_thumb);
00099 }
00100 
00101 KisResource *KisIconItem::resource() const
00102 {
00103     return m_resource;
00104 }
00105 
00106 int KisIconItem::compare(const KoIconItem *o) const
00107 {
00108     const KisIconItem *other = dynamic_cast<const KisIconItem *>(o);
00109 
00110     if (other != 0) {
00111         return m_resource->name().localeAwareCompare(other->m_resource->name());
00112     } else {
00113         return 0;
00114     }
00115 }
00116 
KDE Home | KDE Accessibility Home | Description of Access Keys