ctkIconEnginePlugin.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // Qt includes
  15. #include <QDebug>
  16. #include <QDir>
  17. #include <QFileInfo>
  18. #include <QImageReader>
  19. #include <QPainter>
  20. #include "ctkIconEnginePlugin.h"
  21. class ctkIconEnginePluginPrivate
  22. {
  23. public:
  24. QStringList SizeDirectories;
  25. };
  26. //------------------------------------------------------------------------------
  27. ctkIconEnginePlugin::ctkIconEnginePlugin(QObject* parentObject)
  28. :QIconEnginePluginV2(parentObject)
  29. , d_ptr(new ctkIconEnginePluginPrivate)
  30. {
  31. }
  32. //------------------------------------------------------------------------------
  33. ctkIconEnginePlugin::~ctkIconEnginePlugin()
  34. {
  35. }
  36. //------------------------------------------------------------------------------
  37. QIconEngineV2* ctkIconEnginePlugin::create(const QString& fileName)
  38. {
  39. Q_D(ctkIconEnginePlugin);
  40. Q_UNUSED(fileName);
  41. ctkIconEngine* iconEngine = new ctkIconEngine;
  42. iconEngine->setSizeDirectories(d->SizeDirectories);
  43. return iconEngine;
  44. }
  45. //------------------------------------------------------------------------------
  46. QStringList ctkIconEnginePlugin::keys()const
  47. {
  48. QStringList supportedKeys;
  49. // While ctkIconEngine supports all the image formats, it is uniquely defined
  50. // with "ctkIconEngine".
  51. supportedKeys << "ctkIconEngine";
  52. foreach(QByteArray byteArray, QImageReader::supportedImageFormats())
  53. {
  54. supportedKeys << QString(byteArray);
  55. }
  56. return supportedKeys;
  57. }
  58. //------------------------------------------------------------------------------
  59. void ctkIconEnginePlugin::setSizeDirectories(const QStringList& sizeDirectories)
  60. {
  61. Q_D(ctkIconEnginePlugin);
  62. d->SizeDirectories = sizeDirectories;
  63. }
  64. //------------------------------------------------------------------------------
  65. QStringList ctkIconEnginePlugin::sizeDirectories()const
  66. {
  67. Q_D(const ctkIconEnginePlugin);
  68. return d->SizeDirectories;
  69. }
  70. //------------------------------------------------------------------------------
  71. class ctkIconEnginePrivate
  72. {
  73. public:
  74. QStringList SizeDirectories;
  75. };
  76. //------------------------------------------------------------------------------
  77. ctkIconEngine::ctkIconEngine()
  78. : d_ptr(new ctkIconEnginePrivate)
  79. {
  80. }
  81. //------------------------------------------------------------------------------
  82. ctkIconEngine::~ctkIconEngine()
  83. {
  84. }
  85. //------------------------------------------------------------------------------
  86. void ctkIconEngine::addFile(const QString& fileName, const QSize& size,
  87. QIcon::Mode mode, QIcon::State state)
  88. {
  89. Q_D(ctkIconEngine);
  90. this->Superclass::addFile(fileName, size, mode, state);
  91. QString sizeDirectory;
  92. foreach(QString directory, d->SizeDirectories)
  93. {
  94. if (fileName.contains(directory))
  95. {
  96. sizeDirectory = directory;
  97. break;
  98. }
  99. }
  100. if (sizeDirectory.isEmpty())
  101. {
  102. return;
  103. }
  104. foreach(QString directory, d->SizeDirectories)
  105. {
  106. QString otherFileName = fileName;
  107. otherFileName.replace(sizeDirectory, directory);
  108. if (otherFileName != fileName)
  109. {
  110. this->Superclass::addFile(otherFileName, QSize(), mode, state);
  111. }
  112. }
  113. /*
  114. QFileInfo file(fileName);
  115. QDir dir = file.dir();
  116. foreach(QString subdirPath, dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs))
  117. {
  118. QDir subdir(subdirPath);
  119. QStringList matchingFiles = subdir.entryList(QStringList() <<file.fileName());
  120. if (matchingFiles.size() && matchingFiles[0] != fileName)
  121. {
  122. this->Superclass::addFile(matchingFiles[0], QSize(), mode, state);
  123. }
  124. }
  125. */
  126. }
  127. //------------------------------------------------------------------------------
  128. void ctkIconEngine::setSizeDirectories(const QStringList& sizeDirectories)
  129. {
  130. Q_D(ctkIconEngine);
  131. d->SizeDirectories = sizeDirectories;
  132. }
  133. //------------------------------------------------------------------------------
  134. QStringList ctkIconEngine::sizeDirectories()const
  135. {
  136. Q_D(const ctkIconEngine);
  137. return d->SizeDirectories;
  138. }
  139. //------------------------------------------------------------------------------
  140. QString ctkIconEngine::key() const
  141. {
  142. return QLatin1String("ctkIconEngine");
  143. }