Seamly2D
Code documentation
color_combobox.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file color_combobox.cpp *
3  *****************************************************************************/
4 #include <QColor>
5 #include <QPixmap>
6 #include <QMap>
7 #include <QAbstractItemView>
8 #include <QComboBox>
9 
10 #include "../vtools/tools/vabstracttool.h"
11 #include "../vmisc/logging.h"
12 #include "../ifc/ifcdef.h"
13 
14 #include "color_combobox.h"
15 
16 Q_LOGGING_CATEGORY(colorComboBox, "color_combobox")
17 
18 /*
19  * Default Constructor.
20  */
21 ColorComboBox::ColorComboBox(QWidget *parent, const char *name)
22  : QComboBox(parent)
23  , m_currentColor("black")
24  , m_iconWidth(40)
25  , m_iconHeight(14)
26 {
27  setObjectName(name);
28  setEditable (false);
29  init();
30 }
31 
32 /*
33  * Constructor that provides width and height for icon.
34  */
35 ColorComboBox::ColorComboBox(int width, int height, QWidget *parent, const char *name)
36  : QComboBox(parent)
37  , m_currentColor("black")
38  , m_iconWidth(width)
39  , m_iconHeight(height)
40 {
41  qCDebug(colorComboBox, "ColorComboBox Constructor 2 used");
42  setObjectName(name);
43  setEditable (false);
44  init();
45 }
46 
47 /**
48  * Destructor
49  */
51 
52 /*
53  * Initialisation called from constructor or manually but only once.
54  */
56 {
58  connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ColorComboBox::colorChanged);
59 }
60 
61 /*
62  * Sets the items shown in the combobox and sets index to the 1st color.
63  */
65 {
66  this->blockSignals(true);
67 
68  clear();
69 
70 #if defined(Q_OS_MAC)
71  // Mac pixmap should be little bit smaller
72  setIconSize(QSize(m_iconWidth-= 2 ,m_iconHeight-= 2));
73 #else
74  // Windows
75  setIconSize(QSize(m_iconWidth, m_iconHeight));
76 #endif
77 
78  this->view()->setTextElideMode(Qt::ElideNone);
79  map.remove(ColorByGroup);
80  QMap<QString, QString>::const_iterator i = map.constBegin();
81  while (i != map.constEnd())
82  {
83  QPixmap pixmap = VAbstractTool::createColorIcon(m_iconWidth, m_iconHeight, i.key());
84  addItem(QIcon(pixmap), i.value(), QVariant(i.key()));
85  ++i;
86  }
87 
88  setMaxVisibleItems(map.size());
89  this->model()->sort(1, Qt::AscendingOrder);
90  setCurrentIndex(0);
91  colorChanged(currentIndex());
92 
93  this->blockSignals(false);
94 }
95 /*
96  * Sets the color shown in the combobox to the given color.
97  */
98 void ColorComboBox::setColor(const QString &color)
99 {
100  qCDebug(colorComboBox, "ColorComboBox::setColor");
101  m_currentColor = color;
102 
103  setCurrentIndex(findData(color));
104 
105  if (currentIndex()!= count() -1 )
106  {
107  colorChanged(currentIndex());
108  }
109 }
110 
111 /*
112  * Called when the color has changed. This method sets the current color to the
113  * value chosen.
114  */
116 {
117  qCDebug(colorComboBox, "ColorComboBox::colorChanged");
118 
119  QVariant color = itemData(index);
120  if(color != QVariant::Invalid )
121  {
122  m_currentColor = QVariant(color).toString();
123  }
124 
126 }
127 
128 QString ColorComboBox::getColor() const
129 {
130  return m_currentColor;
131 }
132 
134 {
135  return m_iconWidth;
136 }
137 
139 {
140  return m_iconHeight;
141 }
void setColor(const QString &color)
void colorChangedSignal(const QString &color)
virtual ~ColorComboBox()
void colorChanged(int index)
ColorComboBox(QWidget *parent=nullptr, const char *name=nullptr)
QString getColor() const
void setItems(QMap< QString, QString > map)
QString m_currentColor
static QMap< QString, QString > ColorsList()
static QPixmap createColorIcon(const int w, const int h, const QString &color)
const QString ColorByGroup
Definition: ifcdef.cpp:371