1
/* ============================================================
3
* This file is a part of digiKam project
4
* http://www.digikam.org
7
* Description : undo cache manager for image editor
9
* Copyright (C) 2005 by Renchi Raju <renchi dot raju at gmail dot com>
10
* Copyright (C) 2005 by Joern Ahrens <joern.ahrens@kdemail.net>
11
* Copyright (C) 2006-2012 by Gilles Caulier <caulier dot gilles at gmail dot com>
13
* This program is free software; you can redistribute it
14
* and/or modify it under the terms of the GNU General
15
* Public License as published by the Free Software Foundation;
16
* either version 2, or (at your option)
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* ============================================================ */
26
#include "undocache.h"
31
#include <QCoreApplication>
32
#include <QDataStream>
36
#include <QStringList>
40
#include <kstandarddirs.h>
41
#include <kaboutdata.h>
42
#include <kcomponentdata.h>
49
class UndoCache::Private
57
QString cacheFile(int level) const
59
return QString("%1-%2.bin").arg(cachePrefix).arg(level);
63
QSet<int> cachedLevels;
66
UndoCache::UndoCache()
69
QString cacheDir = KStandardDirs::locateLocal("cache", KGlobal::mainComponent().aboutData()->programName() + '/');
71
d->cachePrefix = QString("%1undocache-%2")
73
.arg(QCoreApplication::applicationPid());
75
// remove any remnants
78
foreach(const QFileInfo& info, dir.entryInfoList(QStringList() << (d->cachePrefix + '*')))
80
QFile(info.filePath()).remove();
84
UndoCache::~UndoCache()
90
void UndoCache::clear()
92
foreach(int level, d->cachedLevels)
94
QFile(d->cacheFile(level)).remove();
97
d->cachedLevels.clear();
100
void UndoCache::clearFrom(int fromLevel)
102
foreach(int level, d->cachedLevels)
104
if (level >= fromLevel)
106
QFile(d->cacheFile(level)).remove();
107
d->cachedLevels.remove(level);
112
bool UndoCache::putData(int level, const DImg& img) const
114
QFile file(d->cacheFile(level));
116
if (file.exists() || !file.open(QIODevice::WriteOnly))
121
QDataStream ds(&file);
124
ds << img.sixteenBit();
125
ds << img.hasAlpha();
127
QByteArray ba((const char*)img.bits(), img.numBytes());
132
d->cachedLevels << level;
137
DImg UndoCache::getData(int level) const
141
bool sixteenBit = false;
142
bool hasAlpha = false;
144
QFile file(d->cacheFile(level));
146
if (!file.open(QIODevice::ReadOnly))
151
QDataStream ds(&file);
160
DImg img(w, h, sixteenBit, hasAlpha, (uchar*)ba.data(), true);
167
} // namespace Digikam