Skip to content
Snippets Groups Projects
Commit c72d9dc6 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

implement automatic color generation for new layer

parent 6aa82dfe
No related branches found
No related tags found
1 merge request!417Layer editor improvements
......@@ -55,8 +55,43 @@ void SampleEditorController::addLayer(LayerItem* before)
const int rowInMultiLayer = (before != nullptr) ? m_multiLayerItem->layers().indexOf(before)
: m_multiLayerItem->layers().size();
// -- find a color for the new layer
QColor color;
auto unusedColors = LayerEditorUtils::predefinedLayerColors();
for (auto l : m_multiLayerItem->layers())
unusedColors.removeAll(l->color());
if (!unusedColors.isEmpty())
color = unusedColors.first();
else {
// search for a color which has been used the less, and which is not the same as in the
// layers above and below
QMap<QString, int> usage;
for (auto l : m_multiLayerItem->layers())
usage[l->color().name()] += 1;
auto sortedByUsage = LayerEditorUtils::predefinedLayerColors();
std::stable_sort(
sortedByUsage.begin(), sortedByUsage.end(),
[&](const QColor& a, const QColor& b) { return usage[a.name()] < usage[b.name()]; });
const QColor above = (rowInMultiLayer > 0)
? m_multiLayerItem->layers()[rowInMultiLayer - 1]->color()
: QColor();
const QColor below = (rowInMultiLayer < m_multiLayerItem->layers().size())
? m_multiLayerItem->layers()[rowInMultiLayer]->color()
: QColor();
for (auto col : sortedByUsage)
if (col != above && col != below) {
color = col;
break;
}
}
// - create new layer
LayerItem* layer = m_multiLayerItem->addLayer(rowInMultiLayer);
// #baLayerEditor set color of layer
layer->setColor(color);
ASSERT(m_multiLayerForm);
m_multiLayerForm->onLayerAdded(layer);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment