25 |
25 |
26 GuiUtilities::GuiUtilities (QObject* parent) : |
26 GuiUtilities::GuiUtilities (QObject* parent) : |
27 QObject (parent), |
27 QObject (parent), |
28 HierarchyElement (parent) {} |
28 HierarchyElement (parent) {} |
29 |
29 |
30 QIcon GuiUtilities::makeColorIcon (LDColor ldcolor, int size) |
30 /* |
|
31 * makeColorIcon |
|
32 * |
|
33 * Creates an icon to represent an LDraw color. |
|
34 */ |
|
35 QIcon GuiUtilities::makeColorIcon (LDColor ldColor, int size) |
31 { |
36 { |
32 // Create an image object and link a painter to it. |
37 QImage image {size, size, QImage::Format_ARGB32}; |
33 QImage img (size, size, QImage::Format_ARGB32); |
38 QPainter painter {&image}; |
34 QPainter painter (&img); |
39 QColor color; |
35 QColor color = ldcolor.faceColor(); |
|
36 |
40 |
37 if (ldcolor == MainColor) |
41 if (ldColor == MainColor) |
38 { |
42 { |
39 // Use the user preferences for main color here |
43 // Use the user preferences for the main color. |
40 color = m_config->mainColor(); |
44 color = m_config->mainColor(); |
41 color.setAlphaF (m_config->mainColorAlpha()); |
45 color.setAlphaF(m_config->mainColorAlpha()); |
42 } |
46 } |
|
47 else |
|
48 color = ldColor.faceColor(); |
43 |
49 |
44 // Paint the icon border |
50 // Paint the icon border |
45 painter.fillRect (QRect (0, 0, size, size), ldcolor.edgeColor()); |
51 painter.fillRect(QRect {0, 0, size, size}, ldColor.edgeColor()); |
46 |
52 |
47 // Paint the checkerboard background, visible with translucent icons |
53 // Paint the checkerboard background, visible with translucent icons |
48 painter.drawPixmap (QRect (1, 1, size - 2, size - 2), GetIcon ("checkerboard"), QRect (0, 0, 8, 8)); |
54 painter.drawPixmap(QRect {1, 1, size - 2, size - 2}, GetIcon("checkerboard"), QRect {0, 0, 8, 8}); |
49 |
55 |
50 // Paint the color above the checkerboard |
56 // Paint the color above the checkerboard |
51 painter.fillRect (QRect (1, 1, size - 2, size - 2), color); |
57 painter.fillRect (QRect {1, 1, size - 2, size - 2}, color); |
52 return QIcon (QPixmap::fromImage (img)); |
58 return QIcon {QPixmap::fromImage(image)}; |
53 } |
59 } |
54 |
60 |
|
61 /* |
|
62 * fillUsedColorsToComboBox |
|
63 * |
|
64 * Fills the provided combo box with the colors used in the current document. |
|
65 */ |
55 void GuiUtilities::fillUsedColorsToComboBox (QComboBox* box) |
66 void GuiUtilities::fillUsedColorsToComboBox (QComboBox* box) |
56 { |
67 { |
57 QMap<LDColor, int> counts; |
68 QMap<LDColor, int> frequencies; |
58 |
69 |
59 for (LDObject* obj : currentDocument()->objects()) |
70 for (LDObject* object : currentDocument()->objects()) |
60 { |
71 { |
61 if (not obj->isColored() or not obj->color().isValid()) |
72 if (object->isColored() and object->color().isValid()) |
62 continue; |
73 { |
63 |
74 if (not frequencies.contains(object->color())) |
64 if (not counts.contains (obj->color())) |
75 frequencies[object->color()] = 1; |
65 counts[obj->color()] = 1; |
76 else |
66 else |
77 frequencies[object->color()] += 1; |
67 counts[obj->color()] += 1; |
78 } |
68 } |
79 } |
69 |
80 |
70 box->clear(); |
81 box->clear(); |
71 int row = 0; |
82 int row = 0; |
|
83 QMapIterator<LDColor, int> iterator {frequencies}; |
72 |
84 |
73 QMapIterator<LDColor, int> it (counts); |
85 while (iterator.hasNext()) |
74 while (it.hasNext()) |
|
75 { |
86 { |
76 it.next(); |
87 iterator.next(); |
77 QIcon ico = makeColorIcon (it.key(), 16); |
88 LDColor color = iterator.key(); |
78 box->addItem (ico, format ("[%1] %2 (%3 object%4)", |
89 int frequency = iterator.value(); |
79 it.key(), it.key().name(), it.value(), plural (it.value()))); |
90 QIcon icon = makeColorIcon(color, 16); |
80 box->setItemData (row, it.key().index()); |
91 box->addItem(icon, format("[%1] %2 (%3 object%4)", color.index(), color.name(), frequency, plural(frequency))); |
|
92 box->setItemData(row, color.index()); |
81 ++row; |
93 ++row; |
82 } |
94 } |
83 } |
95 } |
84 |
96 |
|
97 /* |
|
98 * mainColorRepresentation |
|
99 * |
|
100 * Returns the user-preferred appearance for the main color. |
|
101 */ |
85 QColor GuiUtilities::mainColorRepresentation() |
102 QColor GuiUtilities::mainColorRepresentation() |
86 { |
103 { |
87 QColor col (m_config->mainColor()); |
104 QColor result = {m_config->mainColor()}; |
88 |
105 |
89 if (not col.isValid()) |
106 if (result.isValid()) |
90 return QColor (0, 0, 0); |
107 { |
91 |
108 result.setAlpha(m_config->mainColorAlpha() * 255.f); |
92 col.setAlpha (m_config->mainColorAlpha() * 255.f); |
109 return result; |
93 return col; |
110 } |
|
111 else |
|
112 { |
|
113 return QColor {0, 0, 0}; |
|
114 } |
94 } |
115 } |
95 |
116 |
96 // |
117 /* |
97 // Returns a list of quick colors based on the configuration entry. |
118 * loadQuickColorList |
98 // |
119 * |
99 QList<ColorToolbarItem> GuiUtilities::loadQuickColorList() |
120 * Returns a list of contents for the color toolbar, based on configuration. |
|
121 */ |
|
122 QVector<ColorToolbarItem> GuiUtilities::loadQuickColorList() |
100 { |
123 { |
101 QList<ColorToolbarItem> colors; |
124 QVector<ColorToolbarItem> colors; |
102 |
125 |
103 for (QString colorname : m_config->quickColorToolbar().split (":")) |
126 for (QString colorName : m_config->quickColorToolbar().split(":")) |
104 { |
127 { |
105 if (colorname == "|") |
128 if (colorName == "|") |
|
129 { |
106 colors << ColorToolbarItem::makeSeparator(); |
130 colors << ColorToolbarItem::makeSeparator(); |
|
131 } |
107 else |
132 else |
108 { |
133 { |
109 LDColor color = colorname.toInt(); |
134 LDColor color = colorName.toInt(); |
110 |
135 |
111 if (color.isValid()) |
136 if (color.isValid()) |
112 colors << ColorToolbarItem (color, nullptr); |
137 colors << ColorToolbarItem {color, nullptr}; |
113 } |
138 } |
114 } |
139 } |
115 |
140 |
116 return colors; |
141 return colors; |
117 } |
142 } |