ctkPixmapIconEngine.cpp 12 KB

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