ctkPixmapIconEngine.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.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. // ctkPixmapIconEngine is based on QPixmapIconEngine located in qicon_p.h/cpp
  15. /****************************************************************************
  16. **
  17. ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  18. ** All rights reserved.
  19. ** Contact: Nokia Corporation (qt-info@nokia.com)
  20. **
  21. ** This file is part of the QtGui module of the Qt Toolkit.
  22. **
  23. ** $QT_BEGIN_LICENSE:LGPL$
  24. ** Commercial Usage
  25. ** Licensees holding valid Qt Commercial licenses may use this file in
  26. ** accordance with the Qt Commercial License Agreement provided with the
  27. ** Software or, alternatively, in accordance with the terms contained in
  28. ** a written agreement between you and Nokia.
  29. **
  30. ** GNU Lesser General Public License Usage
  31. ** Alternatively, this file may be used under the terms of the GNU Lesser
  32. ** General Public License version 2.1 as published by the Free Software
  33. ** Foundation and appearing in the file LICENSE.LGPL included in the
  34. ** packaging of this file. Please review the following information to
  35. ** ensure the GNU Lesser General Public License version 2.1 requirements
  36. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  37. **
  38. ** In addition, as a special exception, Nokia gives you certain additional
  39. ** rights. These rights are described in the Nokia Qt LGPL Exception
  40. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  41. **
  42. ** GNU General Public License Usage
  43. ** Alternatively, this file may be used under the terms of the GNU
  44. ** General Public License version 3.0 as published by the Free Software
  45. ** Foundation and appearing in the file LICENSE.GPL included in the
  46. ** packaging of this file. Please review the following information to
  47. ** ensure the GNU General Public License version 3.0 requirements will be
  48. ** met: http://www.gnu.org/copyleft/gpl.html.
  49. **
  50. ** If you have questions regarding the use of this file, please contact
  51. ** Nokia at qt-info@nokia.com.
  52. ** $QT_END_LICENSE$
  53. **
  54. ****************************************************************************/
  55. // Qt includes
  56. #include <QApplication>
  57. #include <QFileInfo>
  58. #include <QPainter>
  59. #include <QPixmapCache>
  60. #include <QStyleOption>
  61. #include "ctkPixmapIconEngine.h"
  62. ctkPixmapIconEngine::ctkPixmapIconEngine()
  63. {
  64. }
  65. ctkPixmapIconEngine::ctkPixmapIconEngine(const ctkPixmapIconEngine &other)
  66. : QIconEngineV2(other), pixmaps(other.pixmaps)
  67. {
  68. }
  69. ctkPixmapIconEngine::~ctkPixmapIconEngine()
  70. {
  71. }
  72. void ctkPixmapIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
  73. {
  74. QSize pixmapSize = rect.size();
  75. #if defined(Q_WS_MAC)
  76. //pixmapSize *= qt_mac_get_scalefactor();
  77. #endif
  78. painter->drawPixmap(rect, pixmap(pixmapSize, mode, state));
  79. }
  80. static inline int area(const QSize &s) { return s.width() * s.height(); }
  81. // returns the smallest of the two that is still larger than or equal to size.
  82. static ctkPixmapIconEngineEntry *bestSizeMatch( const QSize &size, ctkPixmapIconEngineEntry *pa, ctkPixmapIconEngineEntry *pb)
  83. {
  84. int s = area(size);
  85. if (pa->size == QSize() && pa->pixmap.isNull()) {
  86. pa->pixmap = QPixmap(pa->fileName);
  87. pa->size = pa->pixmap.size();
  88. }
  89. int a = area(pa->size);
  90. if (pb->size == QSize() && pb->pixmap.isNull()) {
  91. pb->pixmap = QPixmap(pb->fileName);
  92. pb->size = pb->pixmap.size();
  93. }
  94. int b = area(pb->size);
  95. int res = a;
  96. if (qMin(a,b) >= s)
  97. res = qMin(a,b);
  98. else
  99. res = qMax(a,b);
  100. if (res == a)
  101. return pa;
  102. return pb;
  103. }
  104. ctkPixmapIconEngineEntry *ctkPixmapIconEngine::tryMatch(const QSize &size, QIcon::Mode mode, QIcon::State state)
  105. {
  106. ctkPixmapIconEngineEntry *pe = 0;
  107. for (int i = 0; i < pixmaps.count(); ++i)
  108. if (pixmaps.at(i).mode == mode && pixmaps.at(i).state == state) {
  109. if (pe)
  110. pe = bestSizeMatch(size, &pixmaps[i], pe);
  111. else
  112. pe = &pixmaps[i];
  113. }
  114. return pe;
  115. }
  116. ctkPixmapIconEngineEntry *ctkPixmapIconEngine::bestMatch(const QSize &size, QIcon::Mode mode, QIcon::State state, bool sizeOnly)
  117. {
  118. ctkPixmapIconEngineEntry *pe = tryMatch(size, mode, state);
  119. while (!pe){
  120. QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off : QIcon::On;
  121. if (mode == QIcon::Disabled || mode == QIcon::Selected) {
  122. QIcon::Mode oppositeMode = (mode == QIcon::Disabled) ? QIcon::Selected : QIcon::Disabled;
  123. if ((pe = tryMatch(size, QIcon::Normal, state)))
  124. break;
  125. if ((pe = tryMatch(size, QIcon::Active, state)))
  126. break;
  127. if ((pe = tryMatch(size, mode, oppositeState)))
  128. break;
  129. if ((pe = tryMatch(size, QIcon::Normal, oppositeState)))
  130. break;
  131. if ((pe = tryMatch(size, QIcon::Active, oppositeState)))
  132. break;
  133. if ((pe = tryMatch(size, oppositeMode, state)))
  134. break;
  135. if ((pe = tryMatch(size, oppositeMode, oppositeState)))
  136. break;
  137. } else {
  138. QIcon::Mode oppositeMode = (mode == QIcon::Normal) ? QIcon::Active : QIcon::Normal;
  139. if ((pe = tryMatch(size, oppositeMode, state)))
  140. break;
  141. if ((pe = tryMatch(size, mode, oppositeState)))
  142. break;
  143. if ((pe = tryMatch(size, oppositeMode, oppositeState)))
  144. break;
  145. if ((pe = tryMatch(size, QIcon::Disabled, state)))
  146. break;
  147. if ((pe = tryMatch(size, QIcon::Selected, state)))
  148. break;
  149. if ((pe = tryMatch(size, QIcon::Disabled, oppositeState)))
  150. break;
  151. if ((pe = tryMatch(size, QIcon::Selected, oppositeState)))
  152. break;
  153. }
  154. if (!pe)
  155. return pe;
  156. }
  157. if (sizeOnly ? (pe->size.isNull() || !pe->size.isValid()) : pe->pixmap.isNull()) {
  158. pe->pixmap = QPixmap(pe->fileName);
  159. if (!pe->pixmap.isNull())
  160. pe->size = pe->pixmap.size();
  161. }
  162. return pe;
  163. }
  164. QPixmap ctkPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
  165. {
  166. QPixmap pm;
  167. ctkPixmapIconEngineEntry *pe = bestMatch(size, mode, state, false);
  168. if (pe)
  169. pm = pe->pixmap;
  170. if (pm.isNull()) {
  171. int idx = pixmaps.count();
  172. while (--idx >= 0) {
  173. if (pe == &pixmaps[idx]) {
  174. pixmaps.remove(idx);
  175. break;
  176. }
  177. }
  178. if (pixmaps.isEmpty())
  179. return pm;
  180. else
  181. return pixmap(size, mode, state);
  182. }
  183. QSize actualSize = pm.size();
  184. if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
  185. actualSize.scale(size, Qt::KeepAspectRatio);
  186. QString key = QLatin1String("$qt_icon_")
  187. + QString::number(pm.cacheKey())
  188. + QString::number(static_cast<int>(pe->mode))
  189. + QString::number(QApplication::palette().cacheKey())
  190. + QLatin1Char('_')
  191. + QString::number(actualSize.width())
  192. + QLatin1Char('_')
  193. + QString::number(actualSize.height())
  194. + QLatin1Char('_');
  195. if (mode == QIcon::Active) {
  196. if (QPixmapCache::find(key + QString::number(static_cast<int>(mode)), pm))
  197. return pm; // horray
  198. if (QPixmapCache::find(key + QString::number(static_cast<int>(QIcon::Normal)), pm)) {
  199. QStyleOption opt(0);
  200. opt.palette = QApplication::palette();
  201. QPixmap active = QApplication::style()->generatedIconPixmap(QIcon::Active, pm, &opt);
  202. if (pm.cacheKey() == active.cacheKey())
  203. return pm;
  204. }
  205. }
  206. if (!QPixmapCache::find(key + QString::number(static_cast<int>(mode)), pm)) {
  207. if (pm.size() != actualSize)
  208. pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  209. if (pe->mode != mode && mode != QIcon::Normal) {
  210. QStyleOption opt(0);
  211. opt.palette = QApplication::palette();
  212. QPixmap generated = QApplication::style()->generatedIconPixmap(mode, pm, &opt);
  213. if (!generated.isNull())
  214. pm = generated;
  215. }
  216. QPixmapCache::insert(key + QString::number(static_cast<int>(mode)), pm);
  217. }
  218. return pm;
  219. }
  220. QSize ctkPixmapIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
  221. {
  222. QSize actualSize;
  223. if (ctkPixmapIconEngineEntry *pe = bestMatch(size, mode, state, true))
  224. actualSize = pe->size;
  225. if (actualSize.isNull())
  226. return actualSize;
  227. if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
  228. actualSize.scale(size, Qt::KeepAspectRatio);
  229. return actualSize;
  230. }
  231. void ctkPixmapIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
  232. {
  233. if (!pixmap.isNull()) {
  234. ctkPixmapIconEngineEntry *pe = tryMatch(pixmap.size(), mode, state);
  235. if(pe && pe->size == pixmap.size()) {
  236. pe->pixmap = pixmap;
  237. pe->fileName.clear();
  238. } else {
  239. pixmaps += ctkPixmapIconEngineEntry(pixmap, mode, state);
  240. }
  241. }
  242. }
  243. void ctkPixmapIconEngine::addFile(const QString &fileName, const QSize &_size, QIcon::Mode mode, QIcon::State state)
  244. {
  245. if (!fileName.isEmpty()) {
  246. QSize size = _size;
  247. QPixmap pixmap;
  248. QString abs = fileName;
  249. if (fileName.at(0) != QLatin1Char(':'))
  250. abs = QFileInfo(fileName).absoluteFilePath();
  251. for (int i = 0; i < pixmaps.count(); ++i) {
  252. if (pixmaps.at(i).mode == mode && pixmaps.at(i).state == state) {
  253. ctkPixmapIconEngineEntry *pe = &pixmaps[i];
  254. if(size == QSize()) {
  255. pixmap = QPixmap(abs);
  256. size = pixmap.size();
  257. }
  258. if (pe->size == QSize() && pe->pixmap.isNull()) {
  259. pe->pixmap = QPixmap(pe->fileName);
  260. pe->size = pe->pixmap.size();
  261. }
  262. if(pe->size == size) {
  263. pe->pixmap = pixmap;
  264. pe->fileName = abs;
  265. return;
  266. }
  267. }
  268. }
  269. ctkPixmapIconEngineEntry e(abs, size, mode, state);
  270. e.pixmap = pixmap;
  271. pixmaps += e;
  272. }
  273. }
  274. QString ctkPixmapIconEngine::key() const
  275. {
  276. return QLatin1String("ctkPixmapIconEngine");
  277. }
  278. QIconEngineV2 *ctkPixmapIconEngine::clone() const
  279. {
  280. return new ctkPixmapIconEngine(*this);
  281. }
  282. bool ctkPixmapIconEngine::read(QDataStream &in)
  283. {
  284. int num_entries;
  285. QPixmap pm;
  286. QString fileName;
  287. QSize sz;
  288. uint mode;
  289. uint state;
  290. in >> num_entries;
  291. for (int i=0; i < num_entries; ++i) {
  292. if (in.atEnd()) {
  293. pixmaps.clear();
  294. return false;
  295. }
  296. in >> pm;
  297. in >> fileName;
  298. in >> sz;
  299. in >> mode;
  300. in >> state;
  301. if (pm.isNull()) {
  302. addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state));
  303. } else {
  304. ctkPixmapIconEngineEntry pe(fileName, sz, QIcon::Mode(mode), QIcon::State(state));
  305. pe.pixmap = pm;
  306. pixmaps += pe;
  307. }
  308. }
  309. return true;
  310. }
  311. bool ctkPixmapIconEngine::write(QDataStream &out) const
  312. {
  313. int num_entries = pixmaps.size();
  314. out << num_entries;
  315. for (int i=0; i < num_entries; ++i) {
  316. if (pixmaps.at(i).pixmap.isNull())
  317. out << QPixmap(pixmaps.at(i).fileName);
  318. else
  319. out << pixmaps.at(i).pixmap;
  320. out << pixmaps.at(i).fileName;
  321. out << pixmaps.at(i).size;
  322. out << static_cast<uint>(pixmaps.at(i).mode);
  323. out << static_cast<uint>(pixmaps.at(i).state);
  324. }
  325. return true;
  326. }
  327. void ctkPixmapIconEngine::virtual_hook(int id, void *data)
  328. {
  329. switch (id) {
  330. case QIconEngineV2::AvailableSizesHook: {
  331. QIconEngineV2::AvailableSizesArgument &arg =
  332. *reinterpret_cast<QIconEngineV2::AvailableSizesArgument*>(data);
  333. arg.sizes.clear();
  334. for (int i = 0; i < pixmaps.size(); ++i) {
  335. ctkPixmapIconEngineEntry &pe = pixmaps[i];
  336. if (pe.size == QSize() && pe.pixmap.isNull()) {
  337. pe.pixmap = QPixmap(pe.fileName);
  338. pe.size = pe.pixmap.size();
  339. }
  340. if (pe.mode == arg.mode && pe.state == arg.state && !pe.size.isEmpty())
  341. arg.sizes.push_back(pe.size);
  342. }
  343. break;
  344. }
  345. default:
  346. QIconEngineV2::virtual_hook(id, data);
  347. }
  348. }