ctkPixmapIconEngine.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QFileInfo>
  17. #include <QPainter>
  18. #include <QPixmapCache>
  19. #include <QStyleOption>
  20. #include "ctkPixmapIconEngine.h"
  21. ctkPixmapIconEngine::ctkPixmapIconEngine()
  22. {
  23. }
  24. ctkPixmapIconEngine::ctkPixmapIconEngine(const ctkPixmapIconEngine &other)
  25. : QIconEngineV2(other), pixmaps(other.pixmaps)
  26. {
  27. }
  28. ctkPixmapIconEngine::~ctkPixmapIconEngine()
  29. {
  30. }
  31. void ctkPixmapIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
  32. {
  33. QSize pixmapSize = rect.size();
  34. #if defined(Q_WS_MAC)
  35. pixmapSize *= qt_mac_get_scalefactor();
  36. #endif
  37. painter->drawPixmap(rect, pixmap(pixmapSize, mode, state));
  38. }
  39. static inline int area(const QSize &s) { return s.width() * s.height(); }
  40. // returns the smallest of the two that is still larger than or equal to size.
  41. static ctkPixmapIconEngineEntry *bestSizeMatch( const QSize &size, ctkPixmapIconEngineEntry *pa, ctkPixmapIconEngineEntry *pb)
  42. {
  43. int s = area(size);
  44. if (pa->size == QSize() && pa->pixmap.isNull()) {
  45. pa->pixmap = QPixmap(pa->fileName);
  46. pa->size = pa->pixmap.size();
  47. }
  48. int a = area(pa->size);
  49. if (pb->size == QSize() && pb->pixmap.isNull()) {
  50. pb->pixmap = QPixmap(pb->fileName);
  51. pb->size = pb->pixmap.size();
  52. }
  53. int b = area(pb->size);
  54. int res = a;
  55. if (qMin(a,b) >= s)
  56. res = qMin(a,b);
  57. else
  58. res = qMax(a,b);
  59. if (res == a)
  60. return pa;
  61. return pb;
  62. }
  63. ctkPixmapIconEngineEntry *ctkPixmapIconEngine::tryMatch(const QSize &size, QIcon::Mode mode, QIcon::State state)
  64. {
  65. ctkPixmapIconEngineEntry *pe = 0;
  66. for (int i = 0; i < pixmaps.count(); ++i)
  67. if (pixmaps.at(i).mode == mode && pixmaps.at(i).state == state) {
  68. if (pe)
  69. pe = bestSizeMatch(size, &pixmaps[i], pe);
  70. else
  71. pe = &pixmaps[i];
  72. }
  73. return pe;
  74. }
  75. ctkPixmapIconEngineEntry *ctkPixmapIconEngine::bestMatch(const QSize &size, QIcon::Mode mode, QIcon::State state, bool sizeOnly)
  76. {
  77. ctkPixmapIconEngineEntry *pe = tryMatch(size, mode, state);
  78. while (!pe){
  79. QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off : QIcon::On;
  80. if (mode == QIcon::Disabled || mode == QIcon::Selected) {
  81. QIcon::Mode oppositeMode = (mode == QIcon::Disabled) ? QIcon::Selected : QIcon::Disabled;
  82. if ((pe = tryMatch(size, QIcon::Normal, state)))
  83. break;
  84. if ((pe = tryMatch(size, QIcon::Active, state)))
  85. break;
  86. if ((pe = tryMatch(size, mode, oppositeState)))
  87. break;
  88. if ((pe = tryMatch(size, QIcon::Normal, oppositeState)))
  89. break;
  90. if ((pe = tryMatch(size, QIcon::Active, oppositeState)))
  91. break;
  92. if ((pe = tryMatch(size, oppositeMode, state)))
  93. break;
  94. if ((pe = tryMatch(size, oppositeMode, oppositeState)))
  95. break;
  96. } else {
  97. QIcon::Mode oppositeMode = (mode == QIcon::Normal) ? QIcon::Active : QIcon::Normal;
  98. if ((pe = tryMatch(size, oppositeMode, state)))
  99. break;
  100. if ((pe = tryMatch(size, mode, oppositeState)))
  101. break;
  102. if ((pe = tryMatch(size, oppositeMode, oppositeState)))
  103. break;
  104. if ((pe = tryMatch(size, QIcon::Disabled, state)))
  105. break;
  106. if ((pe = tryMatch(size, QIcon::Selected, state)))
  107. break;
  108. if ((pe = tryMatch(size, QIcon::Disabled, oppositeState)))
  109. break;
  110. if ((pe = tryMatch(size, QIcon::Selected, oppositeState)))
  111. break;
  112. }
  113. if (!pe)
  114. return pe;
  115. }
  116. if (sizeOnly ? (pe->size.isNull() || !pe->size.isValid()) : pe->pixmap.isNull()) {
  117. pe->pixmap = QPixmap(pe->fileName);
  118. if (!pe->pixmap.isNull())
  119. pe->size = pe->pixmap.size();
  120. }
  121. return pe;
  122. }
  123. QPixmap ctkPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
  124. {
  125. QPixmap pm;
  126. ctkPixmapIconEngineEntry *pe = bestMatch(size, mode, state, false);
  127. if (pe)
  128. pm = pe->pixmap;
  129. if (pm.isNull()) {
  130. int idx = pixmaps.count();
  131. while (--idx >= 0) {
  132. if (pe == &pixmaps[idx]) {
  133. pixmaps.remove(idx);
  134. break;
  135. }
  136. }
  137. if (pixmaps.isEmpty())
  138. return pm;
  139. else
  140. return pixmap(size, mode, state);
  141. }
  142. QSize actualSize = pm.size();
  143. if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
  144. actualSize.scale(size, Qt::KeepAspectRatio);
  145. QString key = QLatin1String("$qt_icon_")
  146. + QString::number(pm.cacheKey())
  147. + QString::number(static_cast<int>(pe->mode))
  148. + QString::number(QApplication::palette().cacheKey())
  149. + QLatin1Char('_')
  150. + QString::number(actualSize.width())
  151. + QLatin1Char('_')
  152. + QString::number(actualSize.height())
  153. + QLatin1Char('_');
  154. if (mode == QIcon::Active) {
  155. if (QPixmapCache::find(key + QString::number(static_cast<int>(mode)), pm))
  156. return pm; // horray
  157. if (QPixmapCache::find(key + QString::number(static_cast<int>(QIcon::Normal)), pm)) {
  158. QStyleOption opt(0);
  159. opt.palette = QApplication::palette();
  160. QPixmap active = QApplication::style()->generatedIconPixmap(QIcon::Active, pm, &opt);
  161. if (pm.cacheKey() == active.cacheKey())
  162. return pm;
  163. }
  164. }
  165. if (!QPixmapCache::find(key + QString::number(static_cast<int>(mode)), pm)) {
  166. if (pm.size() != actualSize)
  167. pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  168. if (pe->mode != mode && mode != QIcon::Normal) {
  169. QStyleOption opt(0);
  170. opt.palette = QApplication::palette();
  171. QPixmap generated = QApplication::style()->generatedIconPixmap(mode, pm, &opt);
  172. if (!generated.isNull())
  173. pm = generated;
  174. }
  175. QPixmapCache::insert(key + QString::number(static_cast<int>(mode)), pm);
  176. }
  177. return pm;
  178. }
  179. QSize ctkPixmapIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
  180. {
  181. QSize actualSize;
  182. if (ctkPixmapIconEngineEntry *pe = bestMatch(size, mode, state, true))
  183. actualSize = pe->size;
  184. if (actualSize.isNull())
  185. return actualSize;
  186. if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
  187. actualSize.scale(size, Qt::KeepAspectRatio);
  188. return actualSize;
  189. }
  190. void ctkPixmapIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
  191. {
  192. if (!pixmap.isNull()) {
  193. ctkPixmapIconEngineEntry *pe = tryMatch(pixmap.size(), mode, state);
  194. if(pe && pe->size == pixmap.size()) {
  195. pe->pixmap = pixmap;
  196. pe->fileName.clear();
  197. } else {
  198. pixmaps += ctkPixmapIconEngineEntry(pixmap, mode, state);
  199. }
  200. }
  201. }
  202. void ctkPixmapIconEngine::addFile(const QString &fileName, const QSize &_size, QIcon::Mode mode, QIcon::State state)
  203. {
  204. if (!fileName.isEmpty()) {
  205. QSize size = _size;
  206. QPixmap pixmap;
  207. QString abs = fileName;
  208. if (fileName.at(0) != QLatin1Char(':'))
  209. abs = QFileInfo(fileName).absoluteFilePath();
  210. for (int i = 0; i < pixmaps.count(); ++i) {
  211. if (pixmaps.at(i).mode == mode && pixmaps.at(i).state == state) {
  212. ctkPixmapIconEngineEntry *pe = &pixmaps[i];
  213. if(size == QSize()) {
  214. pixmap = QPixmap(abs);
  215. size = pixmap.size();
  216. }
  217. if (pe->size == QSize() && pe->pixmap.isNull()) {
  218. pe->pixmap = QPixmap(pe->fileName);
  219. pe->size = pe->pixmap.size();
  220. }
  221. if(pe->size == size) {
  222. pe->pixmap = pixmap;
  223. pe->fileName = abs;
  224. return;
  225. }
  226. }
  227. }
  228. ctkPixmapIconEngineEntry e(abs, size, mode, state);
  229. e.pixmap = pixmap;
  230. pixmaps += e;
  231. }
  232. }
  233. QString ctkPixmapIconEngine::key() const
  234. {
  235. return QLatin1String("ctkPixmapIconEngine");
  236. }
  237. QIconEngineV2 *ctkPixmapIconEngine::clone() const
  238. {
  239. return new ctkPixmapIconEngine(*this);
  240. }
  241. bool ctkPixmapIconEngine::read(QDataStream &in)
  242. {
  243. int num_entries;
  244. QPixmap pm;
  245. QString fileName;
  246. QSize sz;
  247. uint mode;
  248. uint state;
  249. in >> num_entries;
  250. for (int i=0; i < num_entries; ++i) {
  251. if (in.atEnd()) {
  252. pixmaps.clear();
  253. return false;
  254. }
  255. in >> pm;
  256. in >> fileName;
  257. in >> sz;
  258. in >> mode;
  259. in >> state;
  260. if (pm.isNull()) {
  261. addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state));
  262. } else {
  263. ctkPixmapIconEngineEntry pe(fileName, sz, QIcon::Mode(mode), QIcon::State(state));
  264. pe.pixmap = pm;
  265. pixmaps += pe;
  266. }
  267. }
  268. return true;
  269. }
  270. bool ctkPixmapIconEngine::write(QDataStream &out) const
  271. {
  272. int num_entries = pixmaps.size();
  273. out << num_entries;
  274. for (int i=0; i < num_entries; ++i) {
  275. if (pixmaps.at(i).pixmap.isNull())
  276. out << QPixmap(pixmaps.at(i).fileName);
  277. else
  278. out << pixmaps.at(i).pixmap;
  279. out << pixmaps.at(i).fileName;
  280. out << pixmaps.at(i).size;
  281. out << static_cast<uint>(pixmaps.at(i).mode);
  282. out << static_cast<uint>(pixmaps.at(i).state);
  283. }
  284. return true;
  285. }
  286. void ctkPixmapIconEngine::virtual_hook(int id, void *data)
  287. {
  288. switch (id) {
  289. case QIconEngineV2::AvailableSizesHook: {
  290. QIconEngineV2::AvailableSizesArgument &arg =
  291. *reinterpret_cast<QIconEngineV2::AvailableSizesArgument*>(data);
  292. arg.sizes.clear();
  293. for (int i = 0; i < pixmaps.size(); ++i) {
  294. ctkPixmapIconEngineEntry &pe = pixmaps[i];
  295. if (pe.size == QSize() && pe.pixmap.isNull()) {
  296. pe.pixmap = QPixmap(pe.fileName);
  297. pe.size = pe.pixmap.size();
  298. }
  299. if (pe.mode == arg.mode && pe.state == arg.state && !pe.size.isEmpty())
  300. arg.sizes.push_back(pe.size);
  301. }
  302. break;
  303. }
  304. default:
  305. QIconEngineV2::virtual_hook(id, data);
  306. }
  307. }