~ubuntu-branches/ubuntu/trusty/digikam/trusty

« back to all changes in this revision

Viewing changes to core/utilities/imageeditor/core/undocache.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2012-09-27 21:41:30 UTC
  • mfrom: (1.2.43)
  • mto: This revision was merged to the branch mainline in revision 86.
  • Revision ID: package-import@ubuntu.com-20120927214130-i8v3ufr21nesp29i
Tags: 4:3.0.0~beta1a-1
* New upstream release

* Fix "wrongly conflicts phonon-backend-vlc" dropped (Closes: #688142)
* debian/watch include download.kde.org

* digikam 3.0.0 uses features from unreleased kdegraphics >=4.10 & ships 
a private version of the kdegraphics libs - this is not the Debian way :-(
* Unsatisfactory Conflicts: libkipi8, libkexiv2-10, libkdcraw20, libksane0
* Suspend digikam-dbg >130Mb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2005-02-05
 
7
 * Description : undo cache manager for image editor
 
8
 *
 
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>
 
12
 *
 
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)
 
17
 * any later version.
 
18
 *
 
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.
 
23
 *
 
24
 * ============================================================ */
 
25
 
 
26
#include "undocache.h"
 
27
 
 
28
// Qt includes
 
29
 
 
30
#include <QByteArray>
 
31
#include <QCoreApplication>
 
32
#include <QDataStream>
 
33
#include <QDir>
 
34
#include <QFile>
 
35
#include <QFileInfo>
 
36
#include <QStringList>
 
37
 
 
38
// KDE includes
 
39
 
 
40
#include <kstandarddirs.h>
 
41
#include <kaboutdata.h>
 
42
#include <kcomponentdata.h>
 
43
#include <kdebug.h>
 
44
#include <kglobal.h>
 
45
 
 
46
namespace Digikam
 
47
{
 
48
 
 
49
class UndoCache::Private
 
50
{
 
51
public:
 
52
 
 
53
    Private()
 
54
    {
 
55
    }
 
56
 
 
57
    QString cacheFile(int level) const
 
58
    {
 
59
        return QString("%1-%2.bin").arg(cachePrefix).arg(level);
 
60
    }
 
61
 
 
62
    QString   cachePrefix;
 
63
    QSet<int> cachedLevels;
 
64
};
 
65
 
 
66
UndoCache::UndoCache()
 
67
    : d(new Private)
 
68
{
 
69
    QString cacheDir = KStandardDirs::locateLocal("cache", KGlobal::mainComponent().aboutData()->programName() + '/');
 
70
 
 
71
    d->cachePrefix   = QString("%1undocache-%2")
 
72
                       .arg(cacheDir)
 
73
                       .arg(QCoreApplication::applicationPid());
 
74
 
 
75
    // remove any remnants
 
76
    QDir dir(cacheDir);
 
77
 
 
78
    foreach(const QFileInfo& info, dir.entryInfoList(QStringList() << (d->cachePrefix + '*')))
 
79
    {
 
80
        QFile(info.filePath()).remove();
 
81
    }
 
82
}
 
83
 
 
84
UndoCache::~UndoCache()
 
85
{
 
86
    clear();
 
87
    delete d;
 
88
}
 
89
 
 
90
void UndoCache::clear()
 
91
{
 
92
    foreach(int level, d->cachedLevels)
 
93
    {
 
94
        QFile(d->cacheFile(level)).remove();
 
95
    }
 
96
 
 
97
    d->cachedLevels.clear();
 
98
}
 
99
 
 
100
void UndoCache::clearFrom(int fromLevel)
 
101
{
 
102
    foreach(int level, d->cachedLevels)
 
103
    {
 
104
        if (level >= fromLevel)
 
105
        {
 
106
            QFile(d->cacheFile(level)).remove();
 
107
            d->cachedLevels.remove(level);
 
108
        }
 
109
    }
 
110
}
 
111
 
 
112
bool UndoCache::putData(int level, const DImg& img) const
 
113
{
 
114
    QFile file(d->cacheFile(level));
 
115
 
 
116
    if (file.exists() || !file.open(QIODevice::WriteOnly))
 
117
    {
 
118
        return false;
 
119
    }
 
120
 
 
121
    QDataStream ds(&file);
 
122
    ds << img.width();
 
123
    ds << img.height();
 
124
    ds << img.sixteenBit();
 
125
    ds << img.hasAlpha();
 
126
 
 
127
    QByteArray ba((const char*)img.bits(), img.numBytes());
 
128
    ds << ba;
 
129
 
 
130
    file.close();
 
131
 
 
132
    d->cachedLevels << level;
 
133
 
 
134
    return true;
 
135
}
 
136
 
 
137
DImg UndoCache::getData(int level) const
 
138
{
 
139
    int  w          = 0;
 
140
    int  h          = 0;
 
141
    bool sixteenBit = false;
 
142
    bool hasAlpha   = false;
 
143
 
 
144
    QFile file(d->cacheFile(level));
 
145
 
 
146
    if (!file.open(QIODevice::ReadOnly))
 
147
    {
 
148
        return DImg();
 
149
    }
 
150
 
 
151
    QDataStream ds(&file);
 
152
    ds >> w;
 
153
    ds >> h;
 
154
    ds >> sixteenBit;
 
155
    ds >> hasAlpha;
 
156
 
 
157
    QByteArray ba;
 
158
    ds >> ba;
 
159
 
 
160
    DImg img(w, h, sixteenBit, hasAlpha, (uchar*)ba.data(), true);
 
161
 
 
162
    file.close();
 
163
 
 
164
    return img;
 
165
}
 
166
 
 
167
}  // namespace Digikam