From 3f7a8b63cb4105d4ac64b5564afdcb6f00620453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C3=A9cureuil?= Date: Thu, 14 May 2009 19:27:25 +0000 Subject: Branch for 2009.1 and handle trunk --- imageloader.cpp | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 imageloader.cpp (limited to 'imageloader.cpp') diff --git a/imageloader.cpp b/imageloader.cpp new file mode 100644 index 0000000..69d60c5 --- /dev/null +++ b/imageloader.cpp @@ -0,0 +1,223 @@ +/* + * Copyright 2008 Arthur Renato Mello + * Copyright 2008 Gustavo Pichorim Boiko + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "imageloader.h" + +ImageLoader::ImageLoader() +{ +} + +ImageLoader::~ImageLoader() +{ +} + +void ImageLoader::setCachePrefix(const QString &cachePrefix) +{ + m_cachePath = cachePrefix + + "/" + + QString("%1X%2/").arg(m_targetSize.width()) + .arg(m_targetSize.height()) + + "/"; +} + +QSize ImageLoader::getSVGSize(const QString &p_file) +{ + QSvgRenderer svg(p_file); + + return svg.defaultSize(); +} + +void ImageLoader::setBaseSize(const QSize &s) +{ + m_baseSize = s; +} + +void ImageLoader::setTargetSize(const QSize &s) +{ + m_targetSize = s; +} + +loadedPixmap ImageLoader::loadPixmap(const QString &p_filename, const QSize &p_size, const QPoint &p_pos) +{ + loadedPixmap ret; + + QString file; + + QStringList list = p_filename.split(":", QString::SkipEmptyParts); + + if(list.isEmpty()) + return ret; + + file = list.at(0); + + QString item; + + if(list.size() > 1) + item = list.at(1); + + QFileInfo fileInfo(file); + + QString fileCache = p_filename.right((p_filename.size() + - p_filename.lastIndexOf("/")) - 1); + fileCache += ".png"; + + QString confFile = KGlobal::dirs()->findResource("data", m_cachePath + + "Cache.rc"); + if(!confFile.isEmpty()) + { + KConfig cfg(confFile, KConfig::SimpleConfig); + + if(cfg.hasGroup(fileCache)) + { + KConfigGroup group = cfg.group(fileCache); + + QString pixmap = group.readEntry("Path", QString()); + QPoint pos = group.readEntry("Position", QPoint()); + QDateTime modified = group.readEntry("Modified", QDateTime()); + + if( !pixmap.isEmpty() && + !pos.isNull() && + modified.isValid() && + fileInfo.lastModified() <= modified) + { + ret.pixmap.load(pixmap); + ret.pos = pos; + + return ret; + } + } + } + + if(p_size.isValid()) + ret.pixmap = QPixmap(p_size); + + ret.pos = p_pos; + + if(file.endsWith(".svg")) //.svgz not working correctly + { + QSvgRenderer svg(file); + + QSize svgSize = svg.defaultSize(); + + if(!item.isEmpty()) + { + QRect rect = svg.boundsOnElement(item).toRect(); + + if(!m_targetSize.isEmpty()) + { + QMatrix matrix; + matrix.scale(((double)m_targetSize.width() / svgSize.width()), + ((double)m_targetSize.height() / svgSize.height())); + + rect = matrix.mapRect(rect); + } + + if(!p_size.isValid()) + ret.pixmap = QPixmap(rect.size()); + if(p_pos.isNull()) + ret.pos = rect.topLeft(); + } + else if(!p_size.isValid()) + ret.pixmap = QPixmap(svgSize); + + ret.pixmap.fill(Qt::transparent); + + QPainter p(&ret.pixmap); + + if(!item.isEmpty()) + svg.render(&p, item); + else + svg.render(&p); + } + else + { + ret.pixmap.load(file); + + if(p_size.isValid() && p_size != ret.pixmap.size()) + { + if(p_size.isValid()) + ret.pixmap = ret.pixmap.scaled(p_size, Qt::KeepAspectRatio, + Qt::SmoothTransformation); + } + } + + QString cache = KGlobal::dirs()->locateLocal("data", m_cachePath); + + KConfig cfg(cache + "Cache.rc", KConfig::SimpleConfig); + KConfigGroup group = cfg.group(fileCache); + group.writeEntry("Path", cache + fileCache); + group.writeEntry("Position", ret.pos); + group.writeEntry("Modified", fileInfo.lastModified()); + + ret.pixmap.save(cache + fileCache, "PNG"); + + return ret; +} + +QPixmap ImageLoader::loadBackground(const QString &p_background) +{ + return loadPixmap(p_background, m_targetSize).pixmap; +} + +loadedPixmap ImageLoader::loadItem(const QString &p_item, QPoint p_pos, const bool &p_useIconSet) +{ + loadedPixmap ret; + + if(!p_pos.isNull()) + { + if(!m_baseSize.isEmpty() && !m_targetSize.isEmpty()) + { + QMatrix matrix; + matrix.scale(((double)m_targetSize.width() / m_baseSize.width()), + ((double)m_targetSize.height() / m_baseSize.height())); + + p_pos = matrix.map(p_pos); + } + } + + if(p_useIconSet) + { + ret.pixmap = DesktopIcon(p_item); + ret.pos = p_pos; + + return ret; + } + + ret = loadPixmap(p_item, QSize(), p_pos); + + return ret; +} + +QRect ImageLoader::loadStatusRect(const QString &p_status) +{ + loadedPixmap pix = loadPixmap(p_status); + + return(QRect(pix.pos, pix.pixmap.size())); +} -- cgit v1.2.1