Commit 77afe223 authored by Mathias Wein's avatar Mathias Wein

Implement a strategy to bubble up presets in history

parent c4ce32ec
......@@ -108,24 +108,30 @@ void PresetHistoryDock::unsetCanvas()
void PresetHistoryDock::presetSelected(QListWidgetItem *item)
{
if (item) {
int oldPosition = m_presetHistory->currentRow();
int newPosition = bubblePreset(oldPosition);
QVariant v = item->data(Qt::UserRole);
KisPaintOpPresetSP preset = v.value<KisPaintOpPresetSP>();
m_block = true;
m_canvas->viewManager()->paintOpBox()->resourceSelected(preset.data());
m_block = false;
if (oldPosition != newPosition) {
m_presetHistory->setCurrentRow(newPosition);
}
}
}
void PresetHistoryDock::canvasResourceChanged(int key, const QVariant& /*v*/)
void PresetHistoryDock::canvasResourceChanged(int key, const QVariant& v)
{
if (m_block) return;
if (m_canvas && key == KisCanvasResourceProvider::CurrentPaintOpPreset) {
KisPaintOpPresetSP preset = m_canvas->resourceManager()->resource(KisCanvasResourceProvider::CurrentPaintOpPreset).value<KisPaintOpPresetSP>();
KisPaintOpPresetSP preset = v.value<KisPaintOpPresetSP>();
if (preset) {
for (int i = 0; i < m_presetHistory->count(); ++i) {
if (preset->name() == m_presetHistory->item(i)->text()) {
m_presetHistory->setCurrentRow(i);
int newPosition = bubblePreset(i);
m_presetHistory->setCurrentRow(newPosition);
return;
}
}
......@@ -134,12 +140,54 @@ void PresetHistoryDock::canvasResourceChanged(int key, const QVariant& /*v*/)
}
}
int PresetHistoryDock::bubblePreset(int position)
{
QListWidgetItem *item = m_presetHistory->item(position);
if (position == 0) {
// topmost item cannot bubble, its bubble state stays until
// below item tries to bubble, so state can be set unconditionally
item->setData(Qt::UserRole+1, QVariant(true));
return position;
}
if (!item->data(Qt::UserRole+1).toBool()) {
// first activation effectively makes the entry share the position with the above
// entry (unless it's already marked to bubble too) but it won't raise in position
// until it gets activated again so its position is clearly defined as above
item->setData(Qt::UserRole+1, QVariant(true));
return position;
}
else {
item->setData(Qt::UserRole+1, QVariant(false));
int topPosition = position - 1;
for (; topPosition >= 0; --topPosition) {
QListWidgetItem *topItem = m_presetHistory->item(topPosition);
if (topItem->data(Qt::UserRole+1).toBool()) {
topItem->setData(Qt::UserRole+1, QVariant(false));
}
else {
break;
}
}
// if all above items want to bubble too, nothing happens besides resetting bubble state
if (topPosition >= 0) {
// since a group of items may bubble together, the net effect is
// that the item above this range moves below that group
QListWidgetItem *topItem = m_presetHistory->takeItem(topPosition);
m_presetHistory->insertItem(position, topItem);
return position - 1;
}
}
return position;
}
void PresetHistoryDock::addPreset(KisPaintOpPresetSP preset)
{
if (preset) {
QListWidgetItem *item = new QListWidgetItem(QPixmap::fromImage(preset->image()), preset->name());
QVariant v = QVariant::fromValue<KisPaintOpPresetSP>(preset);
item->setData(Qt::UserRole, v);
item->setData(Qt::UserRole+1, QVariant(false));
m_presetHistory->insertItem(0, item);
m_presetHistory->setCurrentRow(0);
if (m_presetHistory->count() > 10) {
......
......@@ -44,6 +44,7 @@ private Q_SLOTS:
void presetSelected(QListWidgetItem* item);
void canvasResourceChanged(int key, const QVariant& v);
private:
int bubblePreset(int position);
void addPreset(KisPaintOpPresetSP preset);
private:
QPointer<KisCanvas2> m_canvas;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment