ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
colorcombobox.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "colorcombobox.h"
9 
10 #include <QCoreApplication>
11 #include <QPainter>
12 #include <QPixmap>
13 #include <QSettings>
14 #include <algorithm>
15 
16 namespace Widgets {
23 const QColor ColorComboBox::stColors[] = {
24  QColor(Qt::black), QColor(Qt::red), QColor(Qt::green),
25  QColor(Qt::blue), QColor(Qt::cyan), QColor(Qt::magenta),
26  QColor(Qt::yellow), QColor(Qt::darkYellow), QColor(Qt::darkBlue),
27  QColor(Qt::darkMagenta), QColor(Qt::darkRed), QColor(Qt::darkGreen),
28  QColor(Qt::darkCyan), QColor("#0000A0"), QColor("#FF8000"),
29  QColor("#8000FF"), QColor("#FF0080"), QColor(Qt::white),
30  QColor(Qt::lightGray), QColor(Qt::gray), QColor("#FFFF80"),
31  QColor("#80FFFF"), QColor("#FF80FF"), QColor(Qt::darkGray),
32 };
33 
38 ColorComboBox::ColorComboBox(QWidget* parent) : QComboBox(parent) {
39  setEditable(false);
40  init();
41 }
42 
47  QList<QColor> indexedColors = colorList();
48  QStringList color_names = colorNames();
49 
50  QPixmap icon = QPixmap(28, 16);
51  QRect r = QRect(0, 0, 27, 15);
52 
53  QPainter p;
54  p.begin(&icon);
55 
56  for (int i = 0; i < indexedColors.size(); i++) {
57  p.setBrush(QBrush(indexedColors[i]));
58  p.drawRect(r);
59  this->addItem(icon, color_names[i]);
60  }
61  p.end();
62 }
63 
68 void ColorComboBox::setColor(const QColor& c) {
69  setCurrentIndex(colorIndex(c));
70 }
71 
76 QColor ColorComboBox::color() const { return color(this->currentIndex()); }
77 
83 int ColorComboBox::colorIndex(const QColor& c) {
84  if (!isValidColor(c)) return 0;
85 
86  return colorList().indexOf(c);
87 }
88 
95 QColor ColorComboBox::color(int colorIndex) {
96  QList<QColor> colorsList = colorList();
97  if (colorIndex >= 0 && colorIndex < colorsList.size())
98  return colorsList[colorIndex];
99 
100  return Qt::black;
101 }
102 
107 QList<QColor> ColorComboBox::colorList() {
108  QSettings settings(QCoreApplication::applicationDirPath() + "\\config.ini",
109  QSettings::IniFormat);
110 
111  settings.beginGroup("/General");
112 
113  QList<QColor> indexedColors;
114  QStringList lst = settings.value("/IndexedColors").toStringList();
115  if (!lst.isEmpty()) {
116  for (int i = 0; i < lst.size(); i++) indexedColors << QColor(lst[i]);
117  } else {
118  for (int i = 0; i < stColorsCount; i++) indexedColors << stColors[i];
119  }
120  settings.endGroup();
121 
122  return indexedColors;
123 }
124 
130  QSettings settings(QCoreApplication::applicationDirPath() + "\\config.ini",
131  QSettings::IniFormat);
132 
133  settings.beginGroup("/General");
134  QStringList color_names =
135  settings.value("/IndexedColorNames", defaultColorNames())
136  .toStringList();
137  settings.endGroup();
138  return color_names;
139 }
140 
147 QColor ColorComboBox::defaultColor(int colorIndex) {
148  if (colorIndex >= 0 && colorIndex < (int)sizeof(stColors))
149  return stColors[colorIndex];
150 
151  return Qt::black;
152 }
153 
159 bool ColorComboBox::isValidColor(const QColor& color) {
160  return colorList().contains(color);
161 }
162 
168 
174  QStringList color_names = QStringList() << tr("black");
175  color_names << tr("red");
176  color_names << tr("green");
177  color_names << tr("blue");
178  color_names << tr("cyan");
179  color_names << tr("magenta");
180  color_names << tr("yellow");
181  color_names << tr("dark yellow");
182  color_names << tr("navy");
183  color_names << tr("purple");
184  color_names << tr("wine");
185  color_names << tr("olive");
186  color_names << tr("dark cyan");
187  color_names << tr("royal");
188  color_names << tr("orange");
189  color_names << tr("violet");
190  color_names << tr("pink");
191  color_names << tr("white");
192  color_names << tr("light gray");
193  color_names << tr("gray");
194  color_names << tr("light yellow");
195  color_names << tr("light cyan");
196  color_names << tr("light magenta");
197  color_names << tr("dark gray");
198  return color_names;
199 }
200 
206  QList<QColor> lst;
207  for (int i = 0; i < stColorsCount; i++) lst << stColors[i];
208 
209  return lst;
210 }
211 
212 } // namespace Widgets
math::float4 color
void setColor(const QColor &c)
设置当前颜色.
static QList< QColor > defaultColors()
获取默认颜色列表.
static QColor defaultColor(int colorIndex)
获取给定索引值的颜色.
static bool isValidColor(const QColor &color)
判断是否是一个有效的颜色.
static const int stColorsCount
Definition: colorcombobox.h:35
ColorComboBox(QWidget *parent=0)
构造颜色下拉框类, 初始化颜色下拉框.
QColor color() const
获取当前颜色.
static QStringList defaultColorNames()
获取默认颜色名列表.
static QStringList colorNames()
获取颜色下拉框的颜色名称列表.
static QList< QColor > colorList()
获取颜色下拉框的颜色列表.
static int colorIndex(const QColor &c)
获取颜色的索引值.
static int numPredefinedColors()
获取颜色下拉框内置颜色数.
static const QColor stColors[]
Definition: colorcombobox.h:36
void init()
初始化颜色下拉框.
constexpr Rgb cyan(0, MAX, MAX)
constexpr Rgb black(0, 0, 0)
constexpr Rgb magenta(MAX, 0, MAX)
constexpr Rgb darkBlue(0, 0, MAX/2)
constexpr Rgb white(MAX, MAX, MAX)
constexpr Rgb red(MAX, 0, 0)
constexpr Rgb blue(0, 0, MAX)
constexpr Rgb green(0, MAX, 0)
constexpr Rgb yellow(MAX, MAX, 0)