ctkPixmapIconEngine.cpp 11 KB

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