summaryrefslogtreecommitdiffstats
path: root/src/imagefactory.h
blob: 8f90e02b53d7876f22f9a0a477c824fabc4357f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/***************************************************************************
    copyright            : (C) 2003-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#ifndef IMAGEFACTORY_H
#define IMAGEFACTORY_H

#include "stringset.h"

#include <kurl.h>

#include <tqcolor.h>
#include <tqcache.h>

class KTempDir;

namespace Tellico {
  namespace Data {
    class Image;
    class ImageInfo;
  }

class StyleOptions {
public:
  TQString fontFamily;
  int fontSize;
  TQColor baseColor;
  TQColor textColor;
  TQColor highlightedBaseColor;
  TQColor highlightedTextColor;
  TQString imgDir;
};

/**
 * @author Robby Stephenson
 */
class ImageFactory {
public:
  enum CacheDir {
    TempDir,
    DataDir,
    LocalDir
  };

  /**
   * setup some of the static members
   */
  static void init();

  /**
   * Returns the temporary directory where image files are saved
   *
   * @return The full path
   */
  static TQString tempDir();
  static TQString dataDir();

  /**
   * Add an image, reading it from a URL, which is the case when adding a new image from the
   * @ref ImageWidget.
   *
   * @param url The URL of the image, anything TDEIO can handle
   * @param quiet If any error should not be reported.
   * @return The image id, empty if null
   */
  static TQString addImage(const KURL& url, bool quiet=false,
                          const KURL& referrer = KURL(), bool linkOnly=false);
  /**
   * Add an image, reading it from a regular TQImage, which is the case when dragging and dropping
   * an image in the @ref ImageWidget. The format has to be included, since the TQImage doesn't
   * 'know' what format it came from.
   *
   * @param image The qimage
   * @param format The image format, probably "PNG"
   * @return The image id, empty if null
   */
  static TQString addImage(const TQImage& image, const TQString& format);
  static TQString addImage(const TQPixmap& image, const TQString& format);
  /**
   * Add an image, reading it from data, which is the case when reading from the data file. The
   * @p id isn't strictly needed, since it can be reconstructed from the image data and format, but
   * since it's already known, go ahead and use it.
   *
   * @param data The image data
   * @param format The image format, from TQt's output format list
   * @param id The internal id of the image
   * @return The image id, empty if null
   */
  static TQString addImage(const TQByteArray& data, const TQString& format, const TQString& id);

  /**
   * Writes an image to a file. ImageFactory keeps track of which images were already written
   * if the location is the same as the tempdir.
   *
   * @param id The ID of the image to be written
   * @param targetDir The directory to write the image to, if empty, the tempdir is used.
   * @param force Force the image to be written, even if it already has been
   * @return Whether the save was successful
   */
  static bool writeImage(const TQString& id, const KURL& targetDir, bool force=false);
  static bool writeCachedImage(const TQString& id, CacheDir dir, bool force = false);

  /**
   * Returns an image reference given its id. If none is found, a null image
   * is returned.
   *
   * @param id The image id
   * @return The image referencenter
   */
  static const Data::Image& imageById(const TQString& id);
  static Data::ImageInfo imageInfo(const TQString& id);
  static void cacheImageInfo(const Data::ImageInfo& info);
  // basically returns !imageById().isNull()
  static bool validImage(const TQString& id);

  static TQPixmap pixmap(const TQString& id, int w, int h);

  /**
   * Clear the image cache and dict
   * if deleteTempDirectory = true, then clean the temp dir and remove all temporary image files
   */
  static void clean(bool deleteTempDirectory);
  /**
   * Creates the gradient images used in the entry view.
   */
  static void createStyleImages(const StyleOptions& options = StyleOptions());

  static void removeImage(const TQString& id_, bool deleteImage);
  static StringSet imagesNotInCache();

  static void setLocalDirectory(const KURL& url);
  // local save directory
  static TQString localDir();

private:
  /**
   * Add an image, reading it from a URL, which is the case when adding a new image from the
   * @ref ImageWidget.
   *
   * @param url The URL of the image, anything TDEIO can handle
   * @param quiet If any error should not be reported.
   * @return The image
   */
  static const Data::Image& addImageImpl(const KURL& url, bool quiet=false,
                                         const KURL& referrer = KURL(), bool linkOnly = false);
  /**
   * Add an image, reading it from a regular TQImage, which is the case when dragging and dropping
   * an image in the @ref ImageWidget. The format has to be included, since the TQImage doesn't
   * 'know' what format it came from.
   *
   * @param image The qimage
   * @param format The image format, probably "PNG"
   * @return The image
   */
  static const Data::Image& addImageImpl(const TQImage& image, const TQString& format);
  /**
   * Add an image, reading it from data, which is the case when reading from the data file. The
   * @p id isn't strictly needed, since it can be reconstructed from the image data and format, but
   * since it's already known, go ahead and use it.
   *
   * @param data The image data
   * @param format The image format, from TQt's output format list
   * @param id The internal id of the image
   * @return The image
   */
  static const Data::Image& addImageImpl(const TQByteArray& data, const TQString& format, const TQString& id);

  static const Data::Image& addCachedImageImpl(const TQString& id, CacheDir dir);
  static bool hasImage(const TQString& id);
  static void releaseImages();

  static bool s_needInit;
  static TQDict<Data::Image> s_imageDict;
  static TQCache<Data::Image> s_imageCache;
  static TQCache<TQPixmap> s_pixmapCache;
  static TQMap<TQString, Data::ImageInfo> s_imageInfoMap;
  static StringSet s_imagesInTmpDir; // the id's of the images written to tmp directory
  static StringSet s_imagesToRelease;
  static KTempDir* s_tmpDir;
  static TQString s_localDir;
  static const Data::Image s_null;
};

} // end namespace

#endif