tdegraphics
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kpdocumentsaveoptions.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #define DEBUG_KP_DOCUMENT_SAVE_OPTIONS 0
  24. #include <kpdocumentsaveoptions.h>
  25. #include <tqpixmap.h>
  26. #include <tqstring.h>
  27. #include <tdeconfig.h>
  28. #include <kdebug.h>
  29. #include <tdeglobal.h>
  30. #include <kpdefs.h>
  31. struct kpDocumentSaveOptionsPrivate
  32. {
  33. TQString m_mimeType;
  34. int m_colorDepth;
  35. bool m_dither;
  36. int m_quality;
  37. };
  38. kpDocumentSaveOptions::kpDocumentSaveOptions ()
  39. : d (new kpDocumentSaveOptionsPrivate ())
  40. {
  41. d->m_mimeType = invalidMimeType ();
  42. d->m_colorDepth = invalidColorDepth ();
  43. d->m_dither = initialDither ();
  44. d->m_quality = invalidQuality ();
  45. }
  46. kpDocumentSaveOptions::kpDocumentSaveOptions (const kpDocumentSaveOptions &rhs)
  47. : d (new kpDocumentSaveOptionsPrivate ())
  48. {
  49. d->m_mimeType = rhs.mimeType ();
  50. d->m_colorDepth = rhs.colorDepth ();
  51. d->m_dither = rhs.dither ();
  52. d->m_quality = rhs.quality ();
  53. }
  54. kpDocumentSaveOptions::kpDocumentSaveOptions (TQString mimeType, int colorDepth, bool dither, int quality)
  55. : d (new kpDocumentSaveOptionsPrivate ())
  56. {
  57. d->m_mimeType = mimeType;
  58. d->m_colorDepth = colorDepth;
  59. d->m_dither = dither;
  60. d->m_quality = quality;
  61. }
  62. kpDocumentSaveOptions::~kpDocumentSaveOptions ()
  63. {
  64. delete d;
  65. }
  66. // public
  67. bool kpDocumentSaveOptions::operator== (const kpDocumentSaveOptions &rhs) const
  68. {
  69. return (mimeType () == rhs.mimeType () &&
  70. colorDepth () == rhs.colorDepth () &&
  71. dither () == rhs.dither () &&
  72. quality () == rhs.quality ());
  73. }
  74. // public
  75. bool kpDocumentSaveOptions::operator!= (const kpDocumentSaveOptions &rhs) const
  76. {
  77. return !(*this == rhs);
  78. }
  79. // public
  80. kpDocumentSaveOptions &kpDocumentSaveOptions::operator= (const kpDocumentSaveOptions &rhs)
  81. {
  82. setMimeType (rhs.mimeType ());
  83. setColorDepth (rhs.colorDepth ());
  84. setDither (rhs.dither ());
  85. setQuality (rhs.quality ());
  86. return *this;
  87. }
  88. // public
  89. void kpDocumentSaveOptions::printDebug (const TQString &prefix) const
  90. {
  91. const TQString usedPrefix = !prefix.isEmpty () ?
  92. prefix + TQString::fromLatin1 (": ") :
  93. TQString();
  94. kdDebug () << usedPrefix
  95. << "mimeType=" << mimeType ()
  96. << " colorDepth=" << colorDepth ()
  97. << " dither=" << dither ()
  98. << " quality=" << quality ()
  99. << endl;
  100. }
  101. // public
  102. TQString kpDocumentSaveOptions::mimeType () const
  103. {
  104. return d->m_mimeType;
  105. }
  106. // public
  107. void kpDocumentSaveOptions::setMimeType (const TQString &mimeType)
  108. {
  109. d->m_mimeType = mimeType;
  110. }
  111. // public static
  112. TQString kpDocumentSaveOptions::invalidMimeType ()
  113. {
  114. return TQString();
  115. }
  116. // public static
  117. bool kpDocumentSaveOptions::mimeTypeIsInvalid (const TQString &mimeType)
  118. {
  119. return (mimeType == invalidMimeType ());
  120. }
  121. // public
  122. bool kpDocumentSaveOptions::mimeTypeIsInvalid () const
  123. {
  124. return mimeTypeIsInvalid (mimeType ());
  125. }
  126. // public
  127. int kpDocumentSaveOptions::colorDepth () const
  128. {
  129. return d->m_colorDepth;
  130. }
  131. // public
  132. void kpDocumentSaveOptions::setColorDepth (int depth)
  133. {
  134. d->m_colorDepth = depth;
  135. }
  136. // public static
  137. int kpDocumentSaveOptions::invalidColorDepth ()
  138. {
  139. return -1;
  140. }
  141. // public static
  142. bool kpDocumentSaveOptions::colorDepthIsInvalid (int colorDepth)
  143. {
  144. return (colorDepth != 1 && colorDepth != 8 && colorDepth != 32);
  145. }
  146. // public
  147. bool kpDocumentSaveOptions::colorDepthIsInvalid () const
  148. {
  149. return colorDepthIsInvalid (colorDepth ());
  150. }
  151. // public
  152. bool kpDocumentSaveOptions::dither () const
  153. {
  154. return d->m_dither;
  155. }
  156. // public
  157. void kpDocumentSaveOptions::setDither (bool dither)
  158. {
  159. d->m_dither = dither;
  160. }
  161. // public static
  162. int kpDocumentSaveOptions::initialDither ()
  163. {
  164. return false; // to avoid accidental double dithering
  165. }
  166. // public
  167. int kpDocumentSaveOptions::quality () const
  168. {
  169. return d->m_quality;
  170. }
  171. // public
  172. void kpDocumentSaveOptions::setQuality (int quality)
  173. {
  174. d->m_quality = quality;
  175. }
  176. // public static
  177. int kpDocumentSaveOptions::invalidQuality ()
  178. {
  179. return -2;
  180. }
  181. // public static
  182. bool kpDocumentSaveOptions::qualityIsInvalid (int quality)
  183. {
  184. return (quality < -1 || quality > 100);
  185. }
  186. // public
  187. bool kpDocumentSaveOptions::qualityIsInvalid () const
  188. {
  189. return qualityIsInvalid (quality ());
  190. }
  191. // public static
  192. TQString kpDocumentSaveOptions::defaultMimeType (TDEConfigBase *config)
  193. {
  194. return config->readEntry (kpSettingForcedMimeType,
  195. TQString::fromLatin1 ("image/png"));
  196. }
  197. // public static
  198. void kpDocumentSaveOptions::saveDefaultMimeType (TDEConfigBase *config,
  199. const TQString &mimeType)
  200. {
  201. config->writeEntry (kpSettingForcedMimeType, mimeType);
  202. }
  203. // public static
  204. int kpDocumentSaveOptions::defaultColorDepth (TDEConfigBase *config)
  205. {
  206. int colorDepth =
  207. config->readNumEntry (kpSettingForcedColorDepth, -1);
  208. if (colorDepthIsInvalid (colorDepth))
  209. {
  210. // (not screen depth, in case of transparency)
  211. colorDepth = 32;
  212. }
  213. return colorDepth;
  214. }
  215. // public static
  216. void kpDocumentSaveOptions::saveDefaultColorDepth (TDEConfigBase *config, int colorDepth)
  217. {
  218. config->writeEntry (kpSettingForcedColorDepth, colorDepth);
  219. }
  220. // public static
  221. int kpDocumentSaveOptions::defaultDither (TDEConfigBase *config)
  222. {
  223. return config->readBoolEntry (kpSettingForcedDither, initialDither ());
  224. }
  225. // public static
  226. void kpDocumentSaveOptions::saveDefaultDither (TDEConfigBase *config, bool dither)
  227. {
  228. config->writeEntry (kpSettingForcedDither, dither);
  229. }
  230. // public static
  231. int kpDocumentSaveOptions::defaultQuality (TDEConfigBase *config)
  232. {
  233. int val = config->readNumEntry (kpSettingForcedQuality, -1);
  234. if (qualityIsInvalid (val))
  235. val = -1;
  236. return val;
  237. }
  238. // public static
  239. void kpDocumentSaveOptions::saveDefaultQuality (TDEConfigBase *config, int quality)
  240. {
  241. config->writeEntry (kpSettingForcedQuality, quality);
  242. }
  243. // public static
  244. kpDocumentSaveOptions kpDocumentSaveOptions::defaultDocumentSaveOptions (TDEConfigBase *config)
  245. {
  246. kpDocumentSaveOptions saveOptions;
  247. saveOptions.setMimeType (defaultMimeType (config));
  248. saveOptions.setColorDepth (defaultColorDepth (config));
  249. saveOptions.setDither (defaultDither (config));
  250. saveOptions.setQuality (defaultQuality (config));
  251. #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS
  252. saveOptions.printDebug ("kpDocumentSaveOptions::defaultDocumentSaveOptions()");
  253. #endif
  254. return saveOptions;
  255. }
  256. // public static
  257. bool kpDocumentSaveOptions::saveDefaultDifferences (TDEConfigBase *config,
  258. const kpDocumentSaveOptions &oldDocInfo,
  259. const kpDocumentSaveOptions &newDocInfo)
  260. {
  261. bool savedSomething = false;
  262. #if DEBUG_KP_DOCUMENT_SAVE_OPTIONS
  263. kdDebug () << "kpDocumentSaveOptions::saveDefaultDifferences()" << endl;
  264. oldDocInfo.printDebug ("\told");
  265. newDocInfo.printDebug ("\tnew");
  266. #endif
  267. if (newDocInfo.mimeType () != oldDocInfo.mimeType ())
  268. {
  269. saveDefaultMimeType (config, newDocInfo.mimeType ());
  270. savedSomething = true;
  271. }
  272. if (newDocInfo.colorDepth () != oldDocInfo.colorDepth ())
  273. {
  274. saveDefaultColorDepth (config, newDocInfo.colorDepth ());
  275. savedSomething = true;
  276. }
  277. if (newDocInfo.dither () != oldDocInfo.dither ())
  278. {
  279. saveDefaultDither (config, newDocInfo.dither ());
  280. savedSomething = true;
  281. }
  282. if (newDocInfo.quality () != oldDocInfo.quality ())
  283. {
  284. saveDefaultQuality (config, newDocInfo.quality ());
  285. savedSomething = true;
  286. }
  287. return savedSomething;
  288. }
  289. static TQStringList mimeTypesSupportingProperty (const TQString &property,
  290. const TQStringList &defaultMimeTypesWithPropertyList)
  291. {
  292. TQStringList mimeTypeList;
  293. TDEConfigGroupSaver cfgGroupSaver (TDEGlobal::config (),
  294. kpSettingsGroupMimeTypeProperties);
  295. TDEConfigBase *cfg = cfgGroupSaver.config ();
  296. if (cfg->hasKey (property))
  297. {
  298. mimeTypeList = cfg->readListEntry (property);
  299. }
  300. else
  301. {
  302. mimeTypeList = defaultMimeTypesWithPropertyList;
  303. cfg->writeEntry (property, mimeTypeList);
  304. cfg->sync ();
  305. }
  306. return mimeTypeList;
  307. }
  308. static bool mimeTypeSupportsProperty (const TQString &mimeType,
  309. const TQString &property, const TQStringList &defaultMimeTypesWithPropertyList)
  310. {
  311. const TQStringList mimeTypeList = mimeTypesSupportingProperty (
  312. property, defaultMimeTypesWithPropertyList);
  313. return mimeTypeList.contains (mimeType);
  314. }
  315. // SYNC: update mime info
  316. //
  317. // Only care about writable mimetypes.
  318. //
  319. // Run "branches/kolourpaint/control/scripts/gen_mimetype_line.sh Write" in
  320. // the version of tdelibs/kimgio/ (e.g. KDE 3.5) KolourPaint is shipped with,
  321. // to check for any new mimetypes to add info for. In the methods below,
  322. // you can specify this info (maximum color depth, whether it's lossy etc.).
  323. //
  324. // Update the below list also and bump up "kpSettingsGroupMimeTypeProperties"
  325. // in kpdefs.h.
  326. //
  327. // Currently, Depth and Quality settings are mutually exclusive with
  328. // Depth overriding Quality. I've currently favoured Quality with the
  329. // below mimetypes (i.e. all lossy mimetypes are only given Quality settings,
  330. // no Depth settings).
  331. //
  332. // Mimetypes done:
  333. // image/jp2 [UNTESTED]
  334. // image/jpeg
  335. // image/png
  336. // image/x-bmp
  337. // image/x-eps
  338. // image/x-pcx
  339. // image/x-portable-bitmap
  340. // image/x-portable-greymap
  341. // image/x-portable-pixmap
  342. // image/x-rgb
  343. // image/x-targa
  344. // image/x-xbm
  345. // image/x-xpm
  346. //
  347. // To test whether depth is configurable, write an image in the new
  348. // mimetype with all depths and read each one back. See what
  349. // kpDocument thinks the depth is when it gets TQImage to read it.
  350. // public static
  351. int kpDocumentSaveOptions::mimeTypeMaximumColorDepth (const TQString &mimeType)
  352. {
  353. TQStringList defaultList;
  354. // SYNC: update mime info here
  355. // Greyscale actually (unenforced since depth not set to configurable)
  356. defaultList << TQString::fromLatin1 ("image/x-eps:32");
  357. defaultList << TQString::fromLatin1 ("image/x-portable-bitmap:1");
  358. // Greyscale actually (unenforced since depth not set to configurable)
  359. defaultList << TQString::fromLatin1 ("image/x-portable-greymap:8");
  360. defaultList << TQString::fromLatin1 ("image/x-xbm:1");
  361. const TQStringList mimeTypeList = mimeTypesSupportingProperty (
  362. kpSettingMimeTypeMaximumColorDepth, defaultList);
  363. const TQString mimeTypeColon = mimeType + TQString::fromLatin1 (":");
  364. for (TQStringList::const_iterator it = mimeTypeList.begin ();
  365. it != mimeTypeList.end ();
  366. it++)
  367. {
  368. if ((*it).startsWith (mimeTypeColon))
  369. {
  370. int number = (*it).mid (mimeTypeColon.length ()).toInt ();
  371. if (!colorDepthIsInvalid (number))
  372. {
  373. return number;
  374. }
  375. }
  376. }
  377. return 32;
  378. }
  379. // public
  380. int kpDocumentSaveOptions::mimeTypeMaximumColorDepth () const
  381. {
  382. return mimeTypeMaximumColorDepth (mimeType ());
  383. }
  384. // public static
  385. bool kpDocumentSaveOptions::mimeTypeHasConfigurableColorDepth (const TQString &mimeType)
  386. {
  387. TQStringList defaultMimeTypes;
  388. // SYNC: update mime info here
  389. defaultMimeTypes << TQString::fromLatin1 ("image/png");
  390. defaultMimeTypes << TQString::fromLatin1 ("image/x-bmp");
  391. defaultMimeTypes << TQString::fromLatin1 ("image/x-pcx");
  392. // TODO: Only 1, 24 not 8; TQt only sees 32 but "file" cmd realises
  393. // it's either 1 or 24.
  394. defaultMimeTypes << TQString::fromLatin1 ("image/x-rgb");
  395. // TODO: Only 8 and 24 - no 1.
  396. defaultMimeTypes << TQString::fromLatin1 ("image/x-xpm");
  397. return mimeTypeSupportsProperty (mimeType,
  398. kpSettingMimeTypeHasConfigurableColorDepth,
  399. defaultMimeTypes);
  400. }
  401. // public
  402. bool kpDocumentSaveOptions::mimeTypeHasConfigurableColorDepth () const
  403. {
  404. return mimeTypeHasConfigurableColorDepth (mimeType ());
  405. }
  406. // public static
  407. bool kpDocumentSaveOptions::mimeTypeHasConfigurableQuality (const TQString &mimeType)
  408. {
  409. TQStringList defaultMimeTypes;
  410. // SYNC: update mime info here
  411. defaultMimeTypes << TQString::fromLatin1 ("image/jp2");
  412. defaultMimeTypes << TQString::fromLatin1 ("image/jpeg");
  413. return mimeTypeSupportsProperty (mimeType,
  414. kpSettingMimeTypeHasConfigurableQuality,
  415. defaultMimeTypes);
  416. }
  417. // public
  418. bool kpDocumentSaveOptions::mimeTypeHasConfigurableQuality () const
  419. {
  420. return mimeTypeHasConfigurableQuality (mimeType ());
  421. }
  422. // public
  423. int kpDocumentSaveOptions::isLossyForSaving (const TQPixmap &pixmap) const
  424. {
  425. int ret = 0;
  426. if (mimeTypeMaximumColorDepth () < pixmap.depth ())
  427. {
  428. ret |= MimeTypeMaximumColorDepthLow;
  429. }
  430. if (mimeTypeHasConfigurableColorDepth () &&
  431. !colorDepthIsInvalid () /*TODO: prevent*/ &&
  432. (colorDepth () < pixmap.depth () ||
  433. colorDepth () < 32 && pixmap.mask ()))
  434. {
  435. ret |= ColorDepthLow;
  436. }
  437. if (mimeTypeHasConfigurableQuality () &&
  438. !qualityIsInvalid ())
  439. {
  440. ret |= Quality;
  441. }
  442. return ret;
  443. }