Rev 13957 | Rev 13975 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10124 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | |||
10181 | cbradney | 8 | #include <QImageReader> |
9 | #include <QMapIterator> |
||
10292 | cbradney | 10 | #include <QObject> |
10124 | cbradney | 11 | |
10292 | cbradney | 12 | #include "commonstrings.h" |
10212 | cbradney | 13 | #include "util_formats.h" |
10124 | cbradney | 14 | |
10181 | cbradney | 15 | FormatsManager* FormatsManager::_instance = 0; |
16 | |||
17 | FormatsManager::FormatsManager() |
||
18 | { |
||
11678 | cbradney | 19 | m_fmts.insert(FormatsManager::EPS, QStringList() << "eps" << "epsf" << "epsi" << "eps2" << "eps3" << "epi" << "ept"); |
10292 | cbradney | 20 | m_fmts.insert(FormatsManager::GIF, QStringList() << "gif"); |
21 | m_fmts.insert(FormatsManager::JPEG, QStringList() << "jpg" << "jpeg"); |
||
22 | m_fmts.insert(FormatsManager::PAT, QStringList() << "pat"); |
||
23 | m_fmts.insert(FormatsManager::PDF, QStringList() << "pdf"); |
||
24 | m_fmts.insert(FormatsManager::PNG, QStringList() << "png"); |
||
25 | m_fmts.insert(FormatsManager::PS, QStringList() << "ps"); |
||
26 | m_fmts.insert(FormatsManager::PSD, QStringList() << "psd"); |
||
27 | m_fmts.insert(FormatsManager::TIFF, QStringList() << "tif" << "tiff"); |
||
28 | m_fmts.insert(FormatsManager::XPM, QStringList() << "xpm"); |
||
29 | m_fmts.insert(FormatsManager::WMF, QStringList() << "wmf"); |
||
30 | m_fmts.insert(FormatsManager::SVG, QStringList() << "svg" << "svgz"); |
||
31 | m_fmts.insert(FormatsManager::AI, QStringList() << "ai"); |
||
11615 | fschmid | 32 | m_fmts.insert(FormatsManager::XFIG, QStringList() << "fig"); |
13957 | herm | 33 | m_fmts.insert(FormatsManager::CVG, QStringList() << "cvg"); |
34 | m_fmts.insert(FormatsManager::WPG, QStringList() << "wpg"); |
||
13963 | fschmid | 35 | #ifdef GMAGICK_FOUND |
13957 | herm | 36 | m_fmts.insert(FormatsManager::GMAGICK, QStringList() << "xbm" << "wpg" << "tga" << "ptif" << "ppm" << "pnm" << "pict" << "pgm" << "pcds" << "pcd" << "pbm" << "mng" << "ico" << "gif" << "fax" << "dpx" << "bmp" << "xcf"); |
13963 | fschmid | 37 | #endif |
13957 | herm | 38 | m_fmts.insert(FormatsManager::UNICONV, QStringList() << "cdr" << "cdt" << "ccx" << "cmx" << "cgm" << "aff" << "sk" << "sk1"); |
10271 | cbradney | 39 | |
10292 | cbradney | 40 | m_fmtNames[FormatsManager::EPS] = QObject::tr("Encapsulated PostScript"); |
41 | m_fmtNames[FormatsManager::GIF] = QObject::tr("GIF"); |
||
42 | m_fmtNames[FormatsManager::JPEG] = QObject::tr("JPEG"); |
||
43 | m_fmtNames[FormatsManager::PAT] = QObject::tr("Pattern Files"); |
||
44 | m_fmtNames[FormatsManager::PDF] = QObject::tr("PDF Document"); |
||
45 | m_fmtNames[FormatsManager::PNG] = QObject::tr("PNG"); |
||
46 | m_fmtNames[FormatsManager::PS] = QObject::tr("PostScript"); |
||
47 | m_fmtNames[FormatsManager::PSD] = QObject::tr("Adobe Photoshop"); |
||
48 | m_fmtNames[FormatsManager::TIFF] = QObject::tr("TIFF"); |
||
49 | m_fmtNames[FormatsManager::XPM] = QObject::tr("XPM"); |
||
50 | m_fmtNames[FormatsManager::WMF] = QObject::tr("Windows Meta File"); |
||
51 | m_fmtNames[FormatsManager::SVG] = QObject::tr("Scalable Vector Graphics"); |
||
52 | m_fmtNames[FormatsManager::AI] = QObject::tr("Adobe Illustrator"); |
||
11615 | fschmid | 53 | m_fmtNames[FormatsManager::XFIG] = QObject::tr("Xfig File"); |
13957 | herm | 54 | m_fmtNames[FormatsManager::CVG] = QObject::tr("Calamus Cvg File"); |
55 | m_fmtNames[FormatsManager::WPG] = QObject::tr("Word Perfect Wpg File"); |
||
13963 | fschmid | 56 | #ifdef GMAGICK_FOUND |
13957 | herm | 57 | m_fmtNames[FormatsManager::GMAGICK] = QObject::tr("GraphicsMagick File"); |
13963 | fschmid | 58 | #endif |
13957 | herm | 59 | m_fmtNames[FormatsManager::UNICONV] = QObject::tr("UniConvertor File"); |
10181 | cbradney | 60 | |
10292 | cbradney | 61 | m_fmtMimeTypes.insert(FormatsManager::EPS, QStringList() << "application/postscript"); |
62 | m_fmtMimeTypes.insert(FormatsManager::GIF, QStringList() << "image/gif"); |
||
63 | m_fmtMimeTypes.insert(FormatsManager::JPEG, QStringList() << "image/jpeg"); |
||
64 | m_fmtMimeTypes.insert(FormatsManager::PAT, QStringList() << ""); |
||
65 | m_fmtMimeTypes.insert(FormatsManager::PDF, QStringList() << "application/pdf"); |
||
66 | m_fmtMimeTypes.insert(FormatsManager::PNG, QStringList() << "image/png"); |
||
67 | m_fmtMimeTypes.insert(FormatsManager::PS, QStringList() << "application/postscript"); |
||
68 | m_fmtMimeTypes.insert(FormatsManager::PSD, QStringList() << "application/photoshop"); |
||
69 | m_fmtMimeTypes.insert(FormatsManager::TIFF, QStringList() << "image/tiff"); |
||
70 | m_fmtMimeTypes.insert(FormatsManager::XPM, QStringList() << "image/xpm "); |
||
71 | m_fmtMimeTypes.insert(FormatsManager::WMF, QStringList() << "image/wmf"); |
||
72 | m_fmtMimeTypes.insert(FormatsManager::SVG, QStringList() << "image/svg+xml"); |
||
73 | m_fmtMimeTypes.insert(FormatsManager::AI, QStringList() << "application/illustrator"); |
||
11615 | fschmid | 74 | m_fmtMimeTypes.insert(FormatsManager::XFIG, QStringList() << "image/x-xfig"); |
13838 | fschmid | 75 | m_fmtMimeTypes.insert(FormatsManager::CVG, QStringList() << ""); |
13885 | fschmid | 76 | m_fmtMimeTypes.insert(FormatsManager::WPG, QStringList() << ""); |
10292 | cbradney | 77 | |
10271 | cbradney | 78 | QMapIterator<int, QStringList> i(m_fmts); |
79 | while (i.hasNext()) |
||
80 | { |
||
81 | i.next(); |
||
10398 | cbradney | 82 | m_fmtList << i.value().first().toUpper(); |
10271 | cbradney | 83 | } |
84 | |||
85 | m_qtSupportedImageFormats=QImageReader::supportedImageFormats(); |
||
10272 | cbradney | 86 | m_supportedImageFormats=m_qtSupportedImageFormats; |
10181 | cbradney | 87 | updateSupportedImageFormats(m_supportedImageFormats); |
88 | } |
||
89 | |||
90 | FormatsManager::~FormatsManager() |
||
91 | { |
||
92 | } |
||
93 | |||
94 | FormatsManager* FormatsManager::instance() |
||
95 | { |
||
96 | if (_instance == 0) |
||
97 | _instance = new FormatsManager(); |
||
98 | |||
99 | return _instance; |
||
100 | } |
||
101 | |||
102 | void FormatsManager::deleteInstance() |
||
103 | { |
||
104 | if (_instance) |
||
105 | delete _instance; |
||
106 | _instance = 0; |
||
107 | } |
||
108 | |||
109 | void FormatsManager::imageFormatSupported(const QString& ext) |
||
110 | { |
||
10273 | cbradney | 111 | // return m_supportedImageFormats.contains(QByteArray(ext).toLatin1()); |
10181 | cbradney | 112 | } |
113 | |||
114 | void FormatsManager::updateSupportedImageFormats(QList<QByteArray>& supportedImageFormats) |
||
115 | { |
||
116 | QMapIterator<int, QStringList> it(m_fmts); |
||
117 | while (it.hasNext()) |
||
118 | { |
||
119 | it.next(); |
||
120 | QStringListIterator itSL(it.value()); |
||
121 | while (itSL.hasNext()) |
||
122 | { |
||
123 | QString t(itSL.next()); |
||
124 | supportedImageFormats.append(t.toLocal8Bit()); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
10292 | cbradney | 129 | QString FormatsManager::nameOfFormat(int type) |
130 | { |
||
131 | QMapIterator<int, QString> it(m_fmtNames); |
||
132 | while (it.hasNext()) |
||
133 | { |
||
134 | it.next(); |
||
135 | if (type & it.key()) |
||
136 | return it.value(); |
||
137 | } |
||
138 | return QString::null; |
||
139 | } |
||
140 | |||
141 | QStringList FormatsManager::mimetypeOfFormat(int type) |
||
142 | { |
||
143 | QMapIterator<int, QStringList> it(m_fmtMimeTypes); |
||
144 | while (it.hasNext()) |
||
145 | { |
||
146 | it.next(); |
||
147 | if (type & it.key()) |
||
148 | return it.value(); |
||
149 | } |
||
150 | return QStringList(); |
||
151 | } |
||
152 | |||
153 | QString FormatsManager::extensionsForFormat(int type) |
||
154 | { |
||
155 | QString a,b,c; |
||
156 | fileTypeStrings(type, a, b, c); |
||
157 | return b; |
||
158 | } |
||
159 | |||
160 | QString FormatsManager::fileDialogFormatList(int type) |
||
161 | { |
||
162 | QString a,b,c; |
||
163 | fileTypeStrings(type, a, b, c); |
||
164 | return a + b + ";;" +c; |
||
165 | } |
||
166 | |||
167 | QString FormatsManager::extensionListForFormat(int type, int listType) |
||
168 | { |
||
169 | QString nameMatch; |
||
170 | QString separator(listType==0 ? " *." : "|"); |
||
171 | QMapIterator<int, QStringList> it(m_fmts); |
||
172 | bool first=true; |
||
173 | int n=0; |
||
174 | while (it.hasNext()) |
||
175 | { |
||
176 | it.next(); |
||
177 | if (type & it.key()) |
||
178 | { |
||
179 | //Just in case the Qt used doesn't support jpeg or gif |
||
180 | if ((JPEG & it.key()) && !m_supportedImageFormats.contains(QByteArray("jpg"))) |
||
181 | continue; |
||
182 | if ((GIF & it.key()) && !m_supportedImageFormats.contains(QByteArray("gif"))) |
||
183 | continue; |
||
184 | if (first) |
||
185 | first=false; |
||
186 | QStringListIterator itSL(it.value()); |
||
187 | while (itSL.hasNext()) |
||
188 | { |
||
189 | if (listType==0) |
||
190 | nameMatch += separator; |
||
191 | nameMatch += itSL.next(); |
||
192 | if (listType==1 && itSL.hasNext()) |
||
193 | nameMatch += separator; |
||
194 | } |
||
195 | } |
||
196 | ++n; |
||
197 | if (listType==1 && it.hasNext() && nameMatch.length()>0 && !nameMatch.endsWith(separator)) |
||
198 | nameMatch += separator; |
||
199 | } |
||
200 | if (listType==0 && nameMatch.startsWith(" ")) |
||
201 | nameMatch.remove(0,1); |
||
202 | if (listType==1 && nameMatch.endsWith("|")) |
||
203 | nameMatch.chop(1); |
||
204 | return nameMatch; |
||
205 | } |
||
206 | |||
10271 | cbradney | 207 | void FormatsManager::fileTypeStrings(int type, QString& formatList, QString& formatText, QString& formatAll, bool lowerCaseOnly) |
10181 | cbradney | 208 | { |
10271 | cbradney | 209 | QString fmtList = QObject::tr("All Supported Formats")+" ("; |
10181 | cbradney | 210 | QString fmtText; |
211 | QMapIterator<int, QStringList> it(m_fmts); |
||
212 | bool first=true; |
||
213 | int n=0; |
||
214 | while (it.hasNext()) |
||
215 | { |
||
216 | it.next(); |
||
217 | if (type & it.key()) |
||
218 | { |
||
10271 | cbradney | 219 | //Just in case the Qt used doesn't support jpeg or gif |
220 | if ((JPEG & it.key()) && !m_supportedImageFormats.contains(QByteArray("jpg"))) |
||
10292 | cbradney | 221 | continue; |
10271 | cbradney | 222 | if ((GIF & it.key()) && !m_supportedImageFormats.contains(QByteArray("gif"))) |
10292 | cbradney | 223 | continue; |
10181 | cbradney | 224 | if (first) |
225 | first=false; |
||
226 | else |
||
10292 | cbradney | 227 | { |
10181 | cbradney | 228 | fmtList += " "; |
10292 | cbradney | 229 | fmtText += ";;"; |
230 | } |
||
231 | QString text=m_fmtNames[it.key()] + " ("; |
||
10181 | cbradney | 232 | QStringListIterator itSL(it.value()); |
233 | while (itSL.hasNext()) |
||
234 | { |
||
235 | QString t("*." + itSL.next()); |
||
236 | fmtList += t; |
||
237 | text += t; |
||
238 | if(!lowerCaseOnly) |
||
239 | { |
||
10398 | cbradney | 240 | fmtList += " " + t.toUpper(); |
241 | text += " " + t.toUpper(); |
||
10181 | cbradney | 242 | } |
243 | if (itSL.hasNext()) |
||
244 | { |
||
245 | fmtList += " "; |
||
246 | text += " "; |
||
247 | } |
||
248 | } |
||
10292 | cbradney | 249 | text += ")"; |
10181 | cbradney | 250 | fmtText += text; |
251 | } |
||
252 | ++n; |
||
253 | } |
||
10292 | cbradney | 254 | formatList+=fmtList + ");;"; |
10181 | cbradney | 255 | formatText+=fmtText; |
10271 | cbradney | 256 | formatAll=QObject::tr("All Files (*)"); |
10181 | cbradney | 257 | } |
258 | |||
10124 | cbradney | 259 | bool extensionIndicatesPDF(const QString &ext) |
260 | { |
||
10136 | cbradney | 261 | // QStringList strl; |
262 | // strl << "pdf"; |
||
263 | // return strl.contains(ext, Qt::CaseInsensitive); |
||
264 | return (QString::compare("pdf", ext, Qt::CaseInsensitive) == 0); |
||
265 | } |
||
266 | |||
267 | bool extensionIndicatesEPS(const QString &ext) |
||
268 | { |
||
10124 | cbradney | 269 | QStringList strl; |
11678 | cbradney | 270 | strl << "eps" << "epsf" << "epsi" << "eps2" << "eps3" << "epi" << "ept"; |
10124 | cbradney | 271 | return strl.contains(ext, Qt::CaseInsensitive); |
272 | } |
||
273 | |||
10136 | cbradney | 274 | bool extensionIndicatesEPSorPS(const QString &ext) |
10124 | cbradney | 275 | { |
276 | QStringList strl; |
||
11678 | cbradney | 277 | strl << "eps" << "epsf" << "epsi" << "ps" << "eps2" << "eps3" << "epi" << "ept"; |
10124 | cbradney | 278 | return strl.contains(ext, Qt::CaseInsensitive); |
279 | } |
||
10136 | cbradney | 280 | |
281 | bool extensionIndicatesTIFF(const QString &ext) |
||
282 | { |
||
283 | QStringList strl; |
||
284 | strl << "tif" << "tiff"; |
||
285 | return strl.contains(ext, Qt::CaseInsensitive); |
||
286 | } |
||
287 | |||
288 | bool extensionIndicatesPSD(const QString &ext) |
||
289 | { |
||
290 | // QStringList strl; |
||
291 | // strl << "psd"; |
||
292 | // return strl.contains(ext, Qt::CaseInsensitive); |
||
293 | return (QString::compare("psd", ext, Qt::CaseInsensitive) == 0); |
||
294 | } |
||
295 | |||
296 | bool extensionIndicatesJPEG(const QString &ext) |
||
297 | { |
||
298 | QStringList strl; |
||
299 | strl << "jpg" << "jpeg"; |
||
300 | return strl.contains(ext, Qt::CaseInsensitive); |
||
301 | } |
||
10181 | cbradney | 302 | |
303 | bool extensionIndicatesPattern(const QString &ext) |
||
304 | { |
||
305 | QStringList strl; |
||
306 | strl << "pat"; |
||
307 | return strl.contains(ext, Qt::CaseInsensitive); |
||
308 | } |
||
10303 | cbradney | 309 | |
310 | QString getImageType(QString filename) |
||
311 | { |
||
312 | QString ret = ""; |
||
313 | QFile f(filename); |
||
314 | QFileInfo fi(f); |
||
315 | if (fi.exists()) |
||
316 | { |
||
10398 | cbradney | 317 | QByteArray buf(20, ' '); |
10303 | cbradney | 318 | if (f.open(QIODevice::ReadOnly)) |
319 | { |
||
10398 | cbradney | 320 | f.read(buf.data(), 20); |
10303 | cbradney | 321 | if ((buf[0] == '%') && (buf[1] == '!') && (buf[2] == 'P') && (buf[3] == 'S') && (buf[4] == '-') && (buf[5] == 'A')) |
322 | ret = "eps"; |
||
323 | else if ((buf[0] == '\xC5') && (buf[1] == '\xD0') && (buf[2] == '\xD3') && (buf[3] == '\xC6')) |
||
324 | ret = "eps"; |
||
325 | else if ((buf[0] == 'G') && (buf[1] == 'I') && (buf[2] == 'F') && (buf[3] == '8')) |
||
326 | ret = "gif"; |
||
327 | else if ((buf[0] == '\xFF') && (buf[1] == '\xD8') && (buf[2] == '\xFF')) |
||
328 | ret = "jpg"; |
||
329 | else if ((buf[0] == '%') && (buf[1] == 'P') && (buf[2] == 'D') && (buf[3] == 'F')) |
||
330 | ret = "pdf"; |
||
331 | else if ((buf[0] == '\x89') && (buf[1] == 'P') && (buf[2] == 'N') && (buf[3] == 'G')) |
||
332 | ret = "png"; |
||
333 | else if ((buf[0] == '8') && (buf[1] == 'B') && (buf[2] == 'P') && (buf[3] == 'S')) |
||
334 | ret = "psd"; |
||
335 | else if (((buf[0] == 'I') && (buf[1] == 'I') && (buf[2] == '\x2A')) || ((buf[0] == 'M') && (buf[1] == 'M') && (buf[3] == '\x2A'))) |
||
336 | ret = "tif"; |
||
337 | else if ((buf[0] == '/') && (buf[1] == '*') && (buf[2] == ' ') && (buf[3] == 'X') && (buf[4] == 'P') && (buf[5] == 'M')) |
||
338 | ret = "xpm"; |
||
339 | |||
340 | f.close(); |
||
341 | } |
||
342 | } |
||
343 | return ret; |
||
344 | } |