• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
imagecache.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2010 Daniel Laidig <laidig@kde.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "imagecache.h"
15 
16 #include <QFileInfo>
17 #include <QDataStream>
18 
19 #include <kdebug.h>
20 
21 using namespace Practice;
22 
23 const char* identifier = "parleyimagecache2";
24 
25 void ImageCache::setFilenames(const QStringList& filenames)
26 {
27  m_timestamps.clear();
28  Q_FOREACH(const QString & filename, filenames) {
29  QFileInfo info(filename);
30  m_timestamps.append(info.lastModified());
31  }
32  m_images.clear();
33  m_filenames = filenames;
34  if (!m_saveFilename.isNull()) {
35  openCache();
36  }
37 }
38 
39 void ImageCache::updateImage(const QString& id, const QImage& image)
40 {
41  m_images[id] = image;
42 }
43 
44 QSize ImageCache::imageSize(const QString& id)
45 {
46  if (!m_images.contains(id)) {
47  return QSize();
48  }
49  return m_images.value(id).size();
50 }
51 
52 QImage ImageCache::getImage(const QString& id)
53 {
54  if (!m_images.contains(id)) {
55  return QImage();
56  }
57  return m_images.value(id);
58 }
59 
60 void ImageCache::openCache()
61 {
62  QFile file(m_saveFilename);
63  file.open(QIODevice::ReadOnly);
64  QDataStream stream(&file);
65  // check identifier
66  QString temp;
67  stream >> temp;
68  if (temp != QString(identifier)) {
69  //kDebug() << "not loading cache because the identifier doesn't match";
70  return;
71  }
72  // check filename and timestamp, no need to load images for the wrong file or outdated images
73  QStringList filenames;
74  QList<QDateTime> timestamps;
75  stream >> filenames >> timestamps;
76  if (filenames != m_filenames || timestamps != m_timestamps) {
77  //kDebug() << "not loading cache because it contains the wrong theme or the timestamp has changed";
78  return;
79  }
80  // finally load data
81  stream >> m_images;
82  QHashIterator<QString, QImage> i(m_images); //TODO: do on demand
83  while (i.hasNext()) {
84  i.next();
85  m_images[i.key()] = i.value().convertToFormat(QImage::Format_ARGB32_Premultiplied);
86  }
87  //kDebug() << "opened cache:" << m_saveFilename;
88  //kDebug() << *this;
89 }
90 
91 void ImageCache::saveCache()
92 {
93  //kDebug() << "save cache to:" << m_saveFilename;
94  //kDebug() << *this;
95  QFile file(m_saveFilename);
96  file.open(QIODevice::WriteOnly);
97  QDataStream stream(&file);
98  stream << QString(identifier) << m_filenames << m_timestamps << m_images;
99 }
100 
101 QDebug Practice::operator<<(QDebug dbg, const ImageCache &c)
102 {
103  dbg.nospace() << "(ImageCache, " << c.m_filenames << ", " << c.m_timestamps << ")";
104  int pixels = 0;
105  QHashIterator<QString, QImage> i(c.m_images);
106  while (i.hasNext()) {
107  i.next();
108  dbg.nospace() << "\n\tcontains: " << qPrintable(i.key().leftJustified(35)) << ": " << i.value().size();
109  pixels += i.value().size().width() * i.value().height();
110  }
111 
112  dbg.nospace() << "\n\ttotal pixel count: " << pixels << " (approx. " << double(pixels) * 4 / 1024 / 1024 << " MiB)";
113  return dbg.space();
114 }
QList::clear
void clear()
identifier
const char * identifier
Definition: imagecache.cpp:23
QHashIterator::key
const Key & key() const
QDataStream
QHashIterator::hasNext
bool hasNext() const
QDebug::nospace
QDebug & nospace()
QFile
QString::isNull
bool isNull() const
QList::append
void append(const T &value)
imagecache.h
QHashIterator
Practice::ImageCache
Definition: imagecache.h:28
QString
Practice::operator<<
QDebug operator<<(QDebug dbg, const Practice::ImageCache &c)
Definition: imagecache.cpp:101
QList< QDateTime >
QFileInfo::lastModified
QDateTime lastModified() const
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
Practice::ImageCache::saveCache
void saveCache()
Definition: imagecache.cpp:91
Practice::ImageCache::imageSize
QSize imageSize(const QString &id)
Definition: imagecache.cpp:44
QFileInfo
QHash::clear
void clear()
QHash::value
const T value(const Key &key) const
Practice::ImageCache::updateImage
void updateImage(const QString &id, const QImage &image)
Definition: imagecache.cpp:39
QHashIterator::next
Item next()
QSize
QDebug::space
QDebug & space()
Practice::ImageCache::getImage
QImage getImage(const QString &id)
Definition: imagecache.cpp:52
QImage
Practice::ImageCache::openCache
void openCache()
Definition: imagecache.cpp:60
QDebug
QHash::contains
bool contains(const Key &key) const
QHashIterator::value
const T & value() const
Practice::ImageCache::setFilenames
void setFilenames(const QStringList &filename)
Definition: imagecache.cpp:25
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal