ColorCombobox.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <QToolTip>
  2. #include <QPainter>
  3. #include <QColorDialog>
  4. #include "ColorCombobox.h"
  5. ColorCombobox::ColorCombobox(QWidget *parent) : QComboBox(parent)
  6. {
  7. setEditable(false);
  8. _colorDialogEnabled = false;
  9. connect(this, SIGNAL(activated(int)), SLOT(emitActivatedColor(int)));
  10. connect(this, SIGNAL(highlighted(int)), SLOT(emitHighlightedColor(int)));
  11. }
  12. void ColorCombobox::enableColorDialog(bool enabled)
  13. {
  14. if (_colorDialogEnabled == enabled)
  15. return;
  16. if (enabled)
  17. addItem(tr("More ..."));
  18. else
  19. removeItem(count() - 1);
  20. _colorDialogEnabled = enabled;
  21. }
  22. bool ColorCombobox::hasColor(const QColor &c)
  23. {
  24. int num = count();
  25. if (_colorDialogEnabled)
  26. num--;
  27. for (int i = 0; i < num; i++)
  28. if (color(i) == c)
  29. return true;
  30. return false;
  31. }
  32. void ColorCombobox::appendColor(const QColor &c, const QString &n)
  33. {
  34. if (!c.isValid() || hasColor(c))
  35. return;
  36. // 准备位图
  37. QPixmap pix(16, 16);
  38. pix.fill(Qt::transparent);
  39. QPainter painter(&pix);
  40. painter.setPen(Qt::gray);
  41. painter.setBrush(QBrush(c));
  42. painter.drawRect(1, 1, 13, 13);
  43. // 添加到列表末尾
  44. int i = count();
  45. QString tn = n.isEmpty() ? c.name().toUpper() : n;
  46. if (!_colorDialogEnabled)
  47. addItem(tn, QVariant(c.name()));
  48. else
  49. insertItem(--i, tn, QVariant(c.name()));
  50. setItemIcon(i, QIcon(pix));
  51. setCurrentIndex(i);
  52. }
  53. void ColorCombobox::appendOtherColor()
  54. {
  55. // QColor color=QColorDialog::getColor(Qt::red,this,tr("other"));
  56. // QColor color=Qt::red;
  57. QPixmap pix(16, 16);
  58. pix.fill(Qt::transparent);
  59. QPainter painter(&pix);
  60. painter.setPen(Qt::gray);
  61. QConicalGradient conicalGradient(6, 6, 0);
  62. conicalGradient.setColorAt(0, QColor(255, 0, 0));
  63. conicalGradient.setColorAt(0.166, QColor(255, 255, 0));
  64. conicalGradient.setColorAt(0.333, QColor(0, 255, 0));
  65. conicalGradient.setColorAt(0.5, QColor(0, 255, 255));
  66. conicalGradient.setColorAt(0.666, QColor(0, 0, 255));
  67. conicalGradient.setColorAt(0.833, QColor(255, 0, 255));
  68. conicalGradient.setColorAt(1, QColor(255, 0, 0));
  69. painter.setBrush(QBrush(conicalGradient));
  70. painter.drawRect(1, 1, 13, 13);
  71. int i = count();
  72. // addItem(tr("other"),3);
  73. insertItem(i, tr("Custom..."), 3);
  74. setItemIcon(i, QIcon(pix));
  75. // setCurrentIndex(i);
  76. }
  77. // Added by jingzhe tao. 2013/07/25
  78. void ColorCombobox::updateOtherColor(const QColor &color)
  79. {
  80. QPixmap pix(16, 16);
  81. pix.fill(Qt::transparent);
  82. QPainter painter(&pix);
  83. painter.setPen(Qt::gray);
  84. painter.setBrush(QBrush(color));
  85. painter.drawRect(1, 1, 13, 13);
  86. int i = count() - 1;
  87. // addItem(tr("other"),3);
  88. setItemText(i, QString(tr("Custom(%1)...")).arg(color.name()));
  89. setItemData(i, color.name());
  90. setItemIcon(i, QIcon(pix));
  91. setCurrentIndex(i);
  92. }
  93. void ColorCombobox::insertColor(const QColor &c, const QString &n, int i)
  94. {
  95. if (!c.isValid() || hasColor(c))
  96. return;
  97. int num = count();
  98. if (_colorDialogEnabled)
  99. num--;
  100. if (i >= 0 && i < num)
  101. {
  102. QPixmap pix(18, 18);
  103. QPainter painter(&pix);
  104. painter.setPen(Qt::gray);
  105. painter.setBrush(QBrush(c));
  106. painter.drawRect(1, 1, 13, 13);
  107. QString tn = n.isEmpty() ? c.name().toUpper() : n;
  108. insertItem(i, tn, QVariant(c.name()));
  109. setItemIcon(i, QIcon(pix));
  110. setCurrentIndex(i);
  111. }
  112. else
  113. appendColor(c, n);
  114. }
  115. QColor ColorCombobox::currentColor() const
  116. {
  117. int index = currentIndex();
  118. return color(index);
  119. }
  120. void ColorCombobox::setCurrentColor(const QColor &c)
  121. {
  122. bool existed = false;
  123. for (int i = 0; i < (int)count(); i++)
  124. {
  125. if (color(i) == c)
  126. {
  127. setCurrentIndex(i);
  128. _lastActivated = c;
  129. existed = true;
  130. break;
  131. }
  132. }
  133. if (!existed)
  134. {
  135. _lastActivated = c;
  136. updateOtherColor(c);
  137. }
  138. }
  139. QColor ColorCombobox::color(int index) const
  140. {
  141. if (_colorDialogEnabled && index >= count() - 1)
  142. return QColor();
  143. else if (index < 0)
  144. return QColor();
  145. else
  146. return QColor(itemData(index).toString());
  147. }
  148. void ColorCombobox::emitActivatedColor(int index)
  149. {
  150. if (index == count() - 1)
  151. {
  152. QColor color = QColorDialog::getColor(_lastActivated, this, tr("Custom Color"));
  153. if (color.isValid())
  154. {
  155. this->updateOtherColor(color);
  156. _lastActivated = QColor(itemData(index).toString());
  157. }
  158. else
  159. {
  160. setCurrentColor(_lastActivated);
  161. }
  162. }
  163. else
  164. _lastActivated = QColor(itemData(index).toString());
  165. emit activated(_lastActivated);
  166. }
  167. void ColorCombobox::emitHighlightedColor(int index)
  168. {
  169. if (!_colorDialogEnabled || index != count() - 1)
  170. emit highlighted(color(index));
  171. }
  172. QSize ColorCombobox::sizeHint() const
  173. {
  174. QFontMetrics fm(font());
  175. return QSize(fm.horizontalAdvance(tr("#RRGGBB")) + 16 + 4 + 20, fm.height() + 4);
  176. }
  177. void ColorCombobox::appendPredefinedColors()
  178. {
  179. appendColor(Qt::black, tr("Black"));
  180. appendColor(Qt::darkBlue, tr("Dark blue"));
  181. appendColor(Qt::darkGreen, tr("Dark green"));
  182. appendColor(Qt::darkCyan, tr("Dark cyan"));
  183. appendColor(Qt::darkRed, tr("Dark red"));
  184. appendColor(Qt::darkMagenta, tr("Dark magenta"));
  185. appendColor(Qt::darkYellow, tr("Dark yellow"));
  186. appendColor(Qt::darkGray, tr("Dark gray"));
  187. appendColor(Qt::gray, tr("Gray"));
  188. appendColor(Qt::blue, tr("Blue"));
  189. appendColor(Qt::green, tr("Green"));
  190. appendColor(Qt::cyan, tr("Cyan"));
  191. appendColor(Qt::red, tr("Red"));
  192. appendColor(Qt::magenta, tr("Magenta"));
  193. appendColor(Qt::yellow, tr("Yellow"));
  194. appendColor(Qt::white, tr("White"));
  195. this->appendOtherColor();
  196. }
  197. void ColorCombobox::appendBackgroundColors()
  198. {
  199. appendColor(Qt::black, tr("Black"));
  200. appendColor(Qt::gray, tr("Gray"));
  201. appendColor(Qt::white, tr("White"));
  202. this->appendOtherColor();
  203. }
  204. void ColorCombobox::clearAllColors()
  205. {
  206. clear();
  207. if (_colorDialogEnabled)
  208. addItem(tr("More ..."));
  209. }
  210. bool ColorCombobox::colorDialogEnabled() const
  211. {
  212. return _colorDialogEnabled;
  213. }