00001
00002
00003
00004
00005
00006
00007
00008 #include <QImageReader>
00009 #include <QMapIterator>
00010 #include <QObject>
00011
00012 #include "commonstrings.h"
00013 #include "util_formats.h"
00014
00015 FormatsManager* FormatsManager::_instance = 0;
00016
00017 FormatsManager::FormatsManager()
00018 {
00019 m_fmts.insert(FormatsManager::EPS, QStringList() << "eps" << "epsf" << "epsi");
00020 m_fmts.insert(FormatsManager::GIF, QStringList() << "gif");
00021 m_fmts.insert(FormatsManager::JPEG, QStringList() << "jpg" << "jpeg");
00022 m_fmts.insert(FormatsManager::PAT, QStringList() << "pat");
00023 m_fmts.insert(FormatsManager::PDF, QStringList() << "pdf");
00024 m_fmts.insert(FormatsManager::PNG, QStringList() << "png");
00025 m_fmts.insert(FormatsManager::PS, QStringList() << "ps");
00026 m_fmts.insert(FormatsManager::PSD, QStringList() << "psd");
00027 m_fmts.insert(FormatsManager::TIFF, QStringList() << "tif" << "tiff");
00028 m_fmts.insert(FormatsManager::XPM, QStringList() << "xpm");
00029 m_fmts.insert(FormatsManager::WMF, QStringList() << "wmf");
00030 m_fmts.insert(FormatsManager::SVG, QStringList() << "svg" << "svgz");
00031 m_fmts.insert(FormatsManager::AI, QStringList() << "ai");
00032
00033 m_fmtNames[FormatsManager::EPS] = QObject::tr("Encapsulated PostScript");
00034 m_fmtNames[FormatsManager::GIF] = QObject::tr("GIF");
00035 m_fmtNames[FormatsManager::JPEG] = QObject::tr("JPEG");
00036 m_fmtNames[FormatsManager::PAT] = QObject::tr("Pattern Files");
00037 m_fmtNames[FormatsManager::PDF] = QObject::tr("PDF Document");
00038 m_fmtNames[FormatsManager::PNG] = QObject::tr("PNG");
00039 m_fmtNames[FormatsManager::PS] = QObject::tr("PostScript");
00040 m_fmtNames[FormatsManager::PSD] = QObject::tr("Adobe Photoshop");
00041 m_fmtNames[FormatsManager::TIFF] = QObject::tr("TIFF");
00042 m_fmtNames[FormatsManager::XPM] = QObject::tr("XPM");
00043 m_fmtNames[FormatsManager::WMF] = QObject::tr("Windows Meta File");
00044 m_fmtNames[FormatsManager::SVG] = QObject::tr("Scalable Vector Graphics");
00045 m_fmtNames[FormatsManager::AI] = QObject::tr("Adobe Illustrator");
00046
00047 m_fmtMimeTypes.insert(FormatsManager::EPS, QStringList() << "application/postscript");
00048 m_fmtMimeTypes.insert(FormatsManager::GIF, QStringList() << "image/gif");
00049 m_fmtMimeTypes.insert(FormatsManager::JPEG, QStringList() << "image/jpeg");
00050 m_fmtMimeTypes.insert(FormatsManager::PAT, QStringList() << "");
00051 m_fmtMimeTypes.insert(FormatsManager::PDF, QStringList() << "application/pdf");
00052 m_fmtMimeTypes.insert(FormatsManager::PNG, QStringList() << "image/png");
00053 m_fmtMimeTypes.insert(FormatsManager::PS, QStringList() << "application/postscript");
00054 m_fmtMimeTypes.insert(FormatsManager::PSD, QStringList() << "application/photoshop");
00055 m_fmtMimeTypes.insert(FormatsManager::TIFF, QStringList() << "image/tiff");
00056 m_fmtMimeTypes.insert(FormatsManager::XPM, QStringList() << "image/xpm ");
00057 m_fmtMimeTypes.insert(FormatsManager::WMF, QStringList() << "image/wmf");
00058 m_fmtMimeTypes.insert(FormatsManager::SVG, QStringList() << "image/svg+xml");
00059 m_fmtMimeTypes.insert(FormatsManager::AI, QStringList() << "application/illustrator");
00060
00061 QMapIterator<int, QStringList> i(m_fmts);
00062 while (i.hasNext())
00063 {
00064 i.next();
00065 m_fmtList << i.value().first().toUpper();
00066 }
00067
00068 m_qtSupportedImageFormats=QImageReader::supportedImageFormats();
00069 m_supportedImageFormats=m_qtSupportedImageFormats;
00070 updateSupportedImageFormats(m_supportedImageFormats);
00071 }
00072
00073 FormatsManager::~FormatsManager()
00074 {
00075 }
00076
00077 FormatsManager* FormatsManager::instance()
00078 {
00079 if (_instance == 0)
00080 _instance = new FormatsManager();
00081
00082 return _instance;
00083 }
00084
00085 void FormatsManager::deleteInstance()
00086 {
00087 if (_instance)
00088 delete _instance;
00089 _instance = 0;
00090 }
00091
00092 void FormatsManager::imageFormatSupported(const QString& ext)
00093 {
00094
00095 }
00096
00097 void FormatsManager::updateSupportedImageFormats(QList<QByteArray>& supportedImageFormats)
00098 {
00099 QMapIterator<int, QStringList> it(m_fmts);
00100 while (it.hasNext())
00101 {
00102 it.next();
00103 QStringListIterator itSL(it.value());
00104 while (itSL.hasNext())
00105 {
00106 QString t(itSL.next());
00107 supportedImageFormats.append(t.toLocal8Bit());
00108 }
00109 }
00110 }
00111
00112 QString FormatsManager::nameOfFormat(int type)
00113 {
00114 QMapIterator<int, QString> it(m_fmtNames);
00115 while (it.hasNext())
00116 {
00117 it.next();
00118 if (type & it.key())
00119 return it.value();
00120 }
00121 return QString::null;
00122 }
00123
00124 QStringList FormatsManager::mimetypeOfFormat(int type)
00125 {
00126 QMapIterator<int, QStringList> it(m_fmtMimeTypes);
00127 while (it.hasNext())
00128 {
00129 it.next();
00130 if (type & it.key())
00131 return it.value();
00132 }
00133 return QStringList();
00134 }
00135
00136 QString FormatsManager::extensionsForFormat(int type)
00137 {
00138 QString a,b,c;
00139 fileTypeStrings(type, a, b, c);
00140 return b;
00141 }
00142
00143 QString FormatsManager::fileDialogFormatList(int type)
00144 {
00145 QString a,b,c;
00146 fileTypeStrings(type, a, b, c);
00147 return a + b + ";;" +c;
00148 }
00149
00150 QString FormatsManager::extensionListForFormat(int type, int listType)
00151 {
00152 QString nameMatch;
00153 QString separator(listType==0 ? " *." : "|");
00154 QMapIterator<int, QStringList> it(m_fmts);
00155 bool first=true;
00156 int n=0;
00157 while (it.hasNext())
00158 {
00159 it.next();
00160 if (type & it.key())
00161 {
00162
00163 if ((JPEG & it.key()) && !m_supportedImageFormats.contains(QByteArray("jpg")))
00164 continue;
00165 if ((GIF & it.key()) && !m_supportedImageFormats.contains(QByteArray("gif")))
00166 continue;
00167 if (first)
00168 first=false;
00169 QStringListIterator itSL(it.value());
00170 while (itSL.hasNext())
00171 {
00172 if (listType==0)
00173 nameMatch += separator;
00174 nameMatch += itSL.next();
00175 if (listType==1 && itSL.hasNext())
00176 nameMatch += separator;
00177 }
00178 }
00179 ++n;
00180 if (listType==1 && it.hasNext() && nameMatch.length()>0 && !nameMatch.endsWith(separator))
00181 nameMatch += separator;
00182 }
00183 if (listType==0 && nameMatch.startsWith(" "))
00184 nameMatch.remove(0,1);
00185 if (listType==1 && nameMatch.endsWith("|"))
00186 nameMatch.chop(1);
00187 return nameMatch;
00188 }
00189
00190 void FormatsManager::fileTypeStrings(int type, QString& formatList, QString& formatText, QString& formatAll, bool lowerCaseOnly)
00191 {
00192 QString fmtList = QObject::tr("All Supported Formats")+" (";
00193 QString fmtText;
00194 QMapIterator<int, QStringList> it(m_fmts);
00195 bool first=true;
00196 int n=0;
00197 while (it.hasNext())
00198 {
00199 it.next();
00200 if (type & it.key())
00201 {
00202
00203 if ((JPEG & it.key()) && !m_supportedImageFormats.contains(QByteArray("jpg")))
00204 continue;
00205 if ((GIF & it.key()) && !m_supportedImageFormats.contains(QByteArray("gif")))
00206 continue;
00207 if (first)
00208 first=false;
00209 else
00210 {
00211 fmtList += " ";
00212 fmtText += ";;";
00213 }
00214 QString text=m_fmtNames[it.key()] + " (";
00215 QStringListIterator itSL(it.value());
00216 while (itSL.hasNext())
00217 {
00218 QString t("*." + itSL.next());
00219 fmtList += t;
00220 text += t;
00221 if(!lowerCaseOnly)
00222 {
00223 fmtList += " " + t.toUpper();
00224 text += " " + t.toUpper();
00225 }
00226 if (itSL.hasNext())
00227 {
00228 fmtList += " ";
00229 text += " ";
00230 }
00231 }
00232 text += ")";
00233 fmtText += text;
00234 }
00235 ++n;
00236 }
00237 formatList+=fmtList + ");;";
00238 formatText+=fmtText;
00239 formatAll=QObject::tr("All Files (*)");
00240 }
00241
00242 bool extensionIndicatesPDF(const QString &ext)
00243 {
00244
00245
00246
00247 return (QString::compare("pdf", ext, Qt::CaseInsensitive) == 0);
00248 }
00249
00250 bool extensionIndicatesEPS(const QString &ext)
00251 {
00252 QStringList strl;
00253 strl << "eps" << "epsf" << "epsi";
00254 return strl.contains(ext, Qt::CaseInsensitive);
00255 }
00256
00257 bool extensionIndicatesEPSorPS(const QString &ext)
00258 {
00259 QStringList strl;
00260 strl << "eps" << "epsf" << "epsi" << "ps";
00261 return strl.contains(ext, Qt::CaseInsensitive);
00262 }
00263
00264 bool extensionIndicatesTIFF(const QString &ext)
00265 {
00266 QStringList strl;
00267 strl << "tif" << "tiff";
00268 return strl.contains(ext, Qt::CaseInsensitive);
00269 }
00270
00271 bool extensionIndicatesPSD(const QString &ext)
00272 {
00273
00274
00275
00276 return (QString::compare("psd", ext, Qt::CaseInsensitive) == 0);
00277 }
00278
00279 bool extensionIndicatesJPEG(const QString &ext)
00280 {
00281 QStringList strl;
00282 strl << "jpg" << "jpeg";
00283 return strl.contains(ext, Qt::CaseInsensitive);
00284 }
00285
00286 bool extensionIndicatesPattern(const QString &ext)
00287 {
00288 QStringList strl;
00289 strl << "pat";
00290 return strl.contains(ext, Qt::CaseInsensitive);
00291 }
00292
00293 QString getImageType(QString filename)
00294 {
00295 QString ret = "";
00296 QFile f(filename);
00297 QFileInfo fi(f);
00298 if (fi.exists())
00299 {
00300 QByteArray buf(20, ' ');
00301 if (f.open(QIODevice::ReadOnly))
00302 {
00303 f.read(buf.data(), 20);
00304 if ((buf[0] == '%') && (buf[1] == '!') && (buf[2] == 'P') && (buf[3] == 'S') && (buf[4] == '-') && (buf[5] == 'A'))
00305 ret = "eps";
00306 else if ((buf[0] == '\xC5') && (buf[1] == '\xD0') && (buf[2] == '\xD3') && (buf[3] == '\xC6'))
00307 ret = "eps";
00308 else if ((buf[0] == 'G') && (buf[1] == 'I') && (buf[2] == 'F') && (buf[3] == '8'))
00309 ret = "gif";
00310 else if ((buf[0] == '\xFF') && (buf[1] == '\xD8') && (buf[2] == '\xFF'))
00311 ret = "jpg";
00312 else if ((buf[0] == '%') && (buf[1] == 'P') && (buf[2] == 'D') && (buf[3] == 'F'))
00313 ret = "pdf";
00314 else if ((buf[0] == '\x89') && (buf[1] == 'P') && (buf[2] == 'N') && (buf[3] == 'G'))
00315 ret = "png";
00316 else if ((buf[0] == '8') && (buf[1] == 'B') && (buf[2] == 'P') && (buf[3] == 'S'))
00317 ret = "psd";
00318 else if (((buf[0] == 'I') && (buf[1] == 'I') && (buf[2] == '\x2A')) || ((buf[0] == 'M') && (buf[1] == 'M') && (buf[3] == '\x2A')))
00319 ret = "tif";
00320 else if ((buf[0] == '/') && (buf[1] == '*') && (buf[2] == ' ') && (buf[3] == 'X') && (buf[4] == 'P') && (buf[5] == 'M'))
00321 ret = "xpm";
00322
00323 f.close();
00324 }
00325 }
00326 return ret;
00327 }