Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Switch color history in the popup palette to use last-used sorting
Dmitry Kazakov418457f188e9
diff --git a/libs/global/KisHistoryList.h b/libs/global/KisHistoryList.hnew file mode 100644index 0000000000..0c2e5da1f5--- /dev/null+++ b/libs/global/KisHistoryList.h@@ -0,0 +1,85 @@+/*+ * SPDX-FileCopyrightText: 2024 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef KISHISTORYLIST_H+#define KISHISTORYLIST_H++#include "kis_debug.h"+#include "kis_assert.h"+#include <deque>++template <typename T>+class KisHistoryList+{+public:+ KisHistoryList(int size)+ : m_maxSize(size)+ {+ }++ /**+ * @return the number of elements in the list+ */+ int size() const {+ return m_values.size();+ }++ /**+ * @return @pos's element of the history list+ */+ T at(int pos) const {+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(pos < size(), T());+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(pos >= 0, T());++ return m_values.at(pos);+ }++ /**+ * Add an element to the history list+ *+ * If an element already exists in the list, then this+ * existing element is put into the top of the list (as+ * recently used).+ *+ * If the number of elements exceeds the maxSize() value,+ * the oldest element of the history list is removed.+ */+ int append(const T& value) {+ auto it = std::find(m_values.begin(), m_values.end(), value);++ if (it != m_values.end()) {+ std::rotate(m_values.begin(), it, std::next(it));+ } else {+ m_values.push_front(value);+ if (int(m_values.size()) > m_maxSize) {+ m_values.pop_back();+ }+ }++ // return the index of just added color (always 0)+ return 0;+ }++ /**+ * Clear all historical elements from the list+ */+ void clear() {+ m_values.clear();+ }++ /**+ * @return the maximum possible number of elements in the list+ */+ int maxSize() const {+ return m_maxSize;+ }++private:+ int m_maxSize = 0;+ std::deque<T> m_values;+};++#endif // KISHISTORYLIST_Hdiff --git a/libs/global/tests/CMakeLists.txt b/libs/global/tests/CMakeLists.txtindex e1958ad7ea..0941c8ec8b 100644--- a/libs/global/tests/CMakeLists.txt+++ b/libs/global/tests/CMakeLists.txt@@ -7,6 +7,7 @@ kis_add_tests(KisSignalAutoConnectionTest.cppKisRectsGridTest.cppKisLazyStorageTest.cppKisValueCacheTest.cpp+ KisHistoryListTest.cppNAME_PREFIX "libs-global-"LINK_LIBRARIES kritaglobal kritatestsdk)diff --git a/libs/global/tests/KisHistoryListTest.cpp b/libs/global/tests/KisHistoryListTest.cppnew file mode 100644index 0000000000..0a9c0b0868--- /dev/null+++ b/libs/global/tests/KisHistoryListTest.cpp@@ -0,0 +1,76 @@+/*+ * SPDX-FileCopyrightText: 2024 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "KisHistoryListTest.h"++#include <simpletest.h>+#include "KisHistoryList.h"+++namespace {+auto refState = [](std::initializer_list<int> list) -> std::vector<int> {+ return list;+};++auto getState = [] (const KisHistoryList<int> &list) -> std::vector<int> {+ std::vector<int> state;+ state.reserve(list.size());+ for (int i = 0; i < list.size(); i++) {+ state.push_back(list.at(i));+ }+ return state;+};+}++void KisHistoryListTest::testRotation()+{+ KisHistoryList<int> list(5);++ QCOMPARE(getState(list), refState({}));++ list.append(1);+ QCOMPARE(getState(list), refState({1}));++ list.append(2);+ QCOMPARE(getState(list), refState({2, 1}));++ list.append(3);+ QCOMPARE(getState(list), refState({3, 2, 1}));++ list.append(4);+ QCOMPARE(getState(list), refState({4, 3, 2, 1}));++ list.append(5);+ QCOMPARE(getState(list), refState({5, 4, 3, 2, 1}));++ list.append(6);+ QCOMPARE(getState(list), refState({6, 5, 4, 3, 2}));++ list.clear();+ QCOMPARE(list.size(), 0);+ QCOMPARE(getState(list), refState({}));+}++void KisHistoryListTest::testBubbleUp()+{+ KisHistoryList<int> list(5);++ QCOMPARE(getState(list), refState({}));++ list.append(1);+ list.append(2);+ list.append(3);+ list.append(4);+ list.append(5);+ QCOMPARE(getState(list), refState({5, 4, 3, 2, 1}));++ list.append(3);++ QCOMPARE(getState(list), refState({3, 5, 4, 2, 1}));+}+++SIMPLE_TEST_MAIN(KisHistoryListTest)diff --git a/libs/global/tests/KisHistoryListTest.h b/libs/global/tests/KisHistoryListTest.hnew file mode 100644index 0000000000..d70d5a57f7--- /dev/null+++ b/libs/global/tests/KisHistoryListTest.h@@ -0,0 +1,21 @@+/*+ * SPDX-FileCopyrightText: 2024 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef KISHISTORYLISTTEST_H+#define KISHISTORYLISTTEST_H++#include <QObject>++class KisHistoryListTest : public QObject+{+ Q_OBJECT++private Q_SLOTS:+ void testRotation();+ void testBubbleUp();+};++#endif // KISHISTORYLISTTEST_Hdiff --git a/libs/ui/kis_favorite_resource_manager.cpp b/libs/ui/kis_favorite_resource_manager.cppindex cbcc122768..6cfd9ffe8b 100644--- a/libs/ui/kis_favorite_resource_manager.cpp+++ b/libs/ui/kis_favorite_resource_manager.cpp@@ -19,152 +19,21 @@#include "kis_paintop_box.h"#include "KisViewManager.h"#include "KisResourceServerProvider.h"-#include "kis_min_heap.h"#include "kis_config.h"#include "kis_config_notifier.h"#include <kis_paintop_preset.h>+#include <KisHistoryList.h>--class KisFavoriteResourceManager::ColorDataList-{-public:- static const int MAX_RECENT_COLOR = 12;-- ColorDataList() {- m_key = 0;- }-- ~ColorDataList() {- qDeleteAll(m_guiList);- }-- int size() {- return m_guiList.size();- }-- int leastUsedGuiPos() {- return findPos(m_priorityList.valueAt(0));- }-- const KoColor& guiColor(int pos) {- Q_ASSERT_X(pos < size(), "ColorDataList::guiColor", "index out of bounds");- Q_ASSERT_X(pos >= 0, "ColorDataList::guiColor", "negative index");-- return m_guiList.at(pos)->data;- }-- void append(const KoColor& data) {- int pos = findPos(data);- if (pos > -1) updateKey(pos);- else appendNew(data);- }-- void appendNew(const KoColor& data) {- if (size() >= ColorDataList::MAX_RECENT_COLOR) removeLeastUsed();-- PriorityNode<KoColor> * node;- node = new PriorityNode <KoColor>();- node->data = data;- node->key = m_key++;- m_priorityList.append(node);-- int pos = guiInsertPos(data);- pos >= m_guiList.size() ? m_guiList.append(node)- : m_guiList.insert(pos, node);- node = 0;- }-- void removeLeastUsed() {- Q_ASSERT_X(size() >= 0, "ColorDataList::removeLeastUsed", "index out of bounds");- if (size() <= 0) return;-- int pos = findPos(m_priorityList.valueAt(0));- m_guiList.removeAt(pos);- m_priorityList.remove(0);- }-- void clearHistory() {- Q_ASSERT_X(size() >= 0, "ColorDataList::clearHistory", "index out of bounds");- if (size() <= 0 ) return;- while (size() > 0){- removeLeastUsed();- }- }-- void updateKey(int guiPos) {- if (m_guiList.at(guiPos)->key == m_key - 1) return;- m_priorityList.changeKey(m_guiList.at(guiPos)->pos, m_key++);- }-- /*find position of the color on the gui list*/- int findPos(const KoColor& color) {-- int low = 0, high = size(), mid = 0;- while (low < high) {- mid = (low + high) / 2;- if (hsvComparison(color, m_guiList.at(mid)->data) == 0) return mid;- else if (hsvComparison(color, m_guiList.at(mid)->data) < 0) high = mid;- else low = mid + 1;- }-- return -1;- }---private:-- int m_key;-- int guiInsertPos(const KoColor& color) {- int low = 0, high = size() - 1, mid = (low + high) / 2;- while (low < high) {-- hsvComparison(color, m_guiList[mid]->data) == -1 ? high = mid- : low = mid + 1;- mid = (low + high) / 2;- }-- if (m_guiList.size() > 0) {- if (hsvComparison(color, m_guiList[mid]->data) == 1) ++mid;- }- return mid;- }-- /*compares c1 and c2 based on HSV.- c1 < c2, returns -1- c1 = c2, returns 0- c1 > c2, returns 1 */- int hsvComparison(const KoColor& c1, const KoColor& c2) {- QColor qc1 = c1.toQColor();- QColor qc2 = c2.toQColor();-- if (qc1.hue() < qc2.hue()) return -1;- if (qc1.hue() > qc2.hue()) return 1;-- // hue is the same, ok let's compare saturation- if (qc1.saturation() < qc2.saturation()) return -1;- if (qc1.saturation() > qc2.saturation()) return 1;-- // oh, also saturation is same?- if (qc1.value() < qc2.value()) return -1;- if (qc1.value() > qc2.value()) return 1;-- // user selected two similar colors- return 0;- }-- KisMinHeap <KoColor, MAX_RECENT_COLOR> m_priorityList;- QList <PriorityNode <KoColor>*> m_guiList;-};-+const int KisFavoriteResourceManager::MAX_RECENT_COLOR = 12;KisFavoriteResourceManager::KisFavoriteResourceManager(KisPaintopBox *paintopBox): m_paintopBox(paintopBox)+ , m_colorHistoryList(new KisHistoryList<KoColor>(MAX_RECENT_COLOR))+{KisConfig cfg(true);m_maxPresets = cfg.favoritePresets();- m_colorList = new ColorDataList();connect(KisConfigNotifier::instance(), SIGNAL(configChanged()), SLOT(configChanged()));KisPaintOpPresetResourceServer * rServer = KisResourceServerProvider::instance()->paintOpPresetServer();rServer->addObserver(this);@@ -174,7 +43,6 @@ KisFavoriteResourceManager::~KisFavoriteResourceManager(){KisPaintOpPresetResourceServer *rServer = KisResourceServerProvider::instance()->paintOpPresetServer();rServer->removeObserver(this);- delete m_colorList;}void KisFavoriteResourceManager::unsetResourceServer()@@ -246,19 +114,14 @@ int KisFavoriteResourceManager::numFavoritePresets()//Recent Colorsvoid KisFavoriteResourceManager::slotUpdateRecentColor(int pos){- // Do not update the key, the color might be selected but it is not used yet. So we are not supposed- // to update the color priority when we select it.- m_colorList->updateKey(pos);-Q_EMIT setSelectedColor(pos);- Q_EMIT sigSetFGColor(m_colorList->guiColor(pos));+ Q_EMIT sigSetFGColor(m_colorHistoryList->at(pos));Q_EMIT hidePalettes();}void KisFavoriteResourceManager::slotAddRecentColor(const KoColor& color){- m_colorList->append(color);- int pos = m_colorList->findPos(color);+ int pos = m_colorHistoryList->append(color);Q_EMIT setSelectedColor(pos);}@@ -284,17 +147,17 @@ void KisFavoriteResourceManager::resourceChanged(QSharedPointer<KisPaintOpPresetint KisFavoriteResourceManager::recentColorsTotal(){- return m_colorList->size();+ return m_colorHistoryList->size();}void KisFavoriteResourceManager::slotClearHistory(){- m_colorList->clearHistory();+ m_colorHistoryList->clear();}-const KoColor& KisFavoriteResourceManager::recentColorAt(int pos)+KoColor KisFavoriteResourceManager::recentColorAt(int pos){- return m_colorList->guiColor(pos);+ return m_colorHistoryList->at(pos);}void KisFavoriteResourceManager::slotSetBGColor(const KoColor c)diff --git a/libs/ui/kis_favorite_resource_manager.h b/libs/ui/kis_favorite_resource_manager.hindex 0062fe6e47..6a481935cd 100644--- a/libs/ui/kis_favorite_resource_manager.h+++ b/libs/ui/kis_favorite_resource_manager.h@@ -21,6 +21,8 @@class QString;class KisPaintopBox;class KisPaintOpPreset;+template <typename T>+class KisHistoryList;class KisFavoriteResourceManager : public QObject, public KoResourceServerObserver<KisPaintOpPreset>{@@ -43,7 +45,7 @@ public:void updateFavoritePresets();int recentColorsTotal();- const KoColor& recentColorAt(int pos);+ KoColor recentColorAt(int pos);// Reimplemented from KoResourceServerObservervoid removingResource(QSharedPointer<KisPaintOpPreset> resource) override;@@ -90,14 +92,12 @@ private Q_SLOTS:void presetsChanged();private:+ static const int MAX_RECENT_COLOR;void init();KisPaintopBox *m_paintopBox {nullptr};- class ColorDataList;- ColorDataList *m_colorList {nullptr};-void saveFavoritePresets();KoColor m_bgColor;@@ -110,6 +110,7 @@ private:KisTagModel* m_tagModel {nullptr};KisTagFilterResourceProxyModel* m_resourcesProxyModel {nullptr};KisResourceModel* m_resourceModel {nullptr};+ QScopedPointer<KisHistoryList<KoColor>> m_colorHistoryList;};diff --git a/libs/ui/kis_min_heap.h b/libs/ui/kis_min_heap.hdeleted file mode 100644index fcd512de36..0000000000--- a/libs/ui/kis_min_heap.h+++ /dev/null@@ -1,155 +0,0 @@-/* This file is part of the KDE project- SPDX-FileCopyrightText: 2009 Vera Lukman <[email protected]>-- SPDX-License-Identifier: LGPL-2.0-only-*/--#ifndef KIS_MIN_HEAP_H-#define KIS_MIN_HEAP_H--#include <kis_debug.h>-#include <QList>--template <typename T> struct PriorityNode {- T data;- int key;- int pos;-};--template <typename T, int N> class KisMinHeap-{-public:-- KisMinHeap()- : m_last(0),- m_size(N),- m_list(new PriorityNode <T>* [N])- {- }-- inline KisMinHeap(const T& data, int key)- : KisMinHeap()- {- append(data, key);- }-- inline KisMinHeap(PriorityNode<T>* node)- : KisMinHeap()- {- append(node);- }-- ~KisMinHeap() {- delete[] m_list;- }-- inline void changeKey(int pos, int newKey) {- m_list[pos]->key = newKey;- heapifyUp(pos);- heapifyDown(pos);- }-- inline int size() {- return m_last;- }-- inline T valueAt(int pos) {- return m_list[pos]->data;- }-- void append(PriorityNode<T>* node) {- node->pos = m_last;- m_list[m_last] = node;- ++m_last;- heapifyUp(node->pos);-- node = 0;- }-- void append(const T& data, int key) {- if (m_last >= m_size) return;- PriorityNode <T>* node = new PriorityNode<T>;- node->data = data;- node->key = key;-- append(node);- }-- void remove(int pos) {- if (pos < 0) return;- swap(pos, m_last - 1);- --m_last;- delete m_list[m_last];- m_list[m_last] = 0;- heapifyUp(pos);- heapifyDown(pos);- }-- void remove(const T& data) {- int pos = find(data);- if (pos >= 0) remove(pos);- }-- int find(const T& data) {- for (int pos = 0; pos < m_last; pos++) {- if (m_list[pos]->data == data) return pos;- }- return -1;- }--private:-- int m_last;- int m_size;- PriorityNode <T>* *m_list;-- void swap(int pos1, int pos2) {- PriorityNode <T>* temp(m_list[pos1]);- m_list[pos1] = m_list[pos2];- m_list[pos1]->pos = pos1;- m_list[pos2] = temp;- m_list[pos2]->pos = pos2;- temp = 0;- }-- void heapifyUp(int pos) {- while (pos > 0 && m_list[pos]->key < m_list[parentPos(pos)]->key) {- swap(pos, parentPos(pos));- pos = parentPos(pos);- }- }-- void heapifyDown(int pos) {- if (leftChildPos(pos) >= m_last) return; //no children- else {- int childPos = 0;-- if (rightChildPos(pos) >= m_last) { //1 child- childPos = leftChildPos(pos);- } else { //2 children- m_list[leftChildPos(pos)]->key < m_list[rightChildPos(pos)]->key ? childPos = leftChildPos(pos) :- childPos = rightChildPos(pos);- }-- if (m_list[childPos]->key < m_list[pos]->key) {- swap(pos, childPos);- heapifyDown(childPos);- } else return;- }- }-- inline int leftChildPos(int x) {- return 2 * x + 1;- }-- inline int rightChildPos(int x) {- return 2 * x + 2;- }-- inline int parentPos(int x) {- return (x - 1) / 2;- }--};--#endif // HEAP_Hdiff --git a/libs/ui/kis_popup_palette.cpp b/libs/ui/kis_popup_palette.cppindex 5e75c70c61..c619b117bc 100644--- a/libs/ui/kis_popup_palette.cpp+++ b/libs/ui/kis_popup_palette.cpp@@ -666,7 +666,10 @@ void KisPopupPalette::paintEvent(QPaintEvent* e)if (m_showColorHistory) {// paint recent colors area.painter.setPen(Qt::NoPen);- qreal rotationAngle = -360.0 / m_resourceManager->recentColorsTotal();+ const qreal rotationAngle = 360.0 / m_resourceManager->recentColorsTotal();+ const qreal rotationOffset = 180.0;++ painter.rotate(rotationOffset);// there might be no recent colors at the start, so paint a placeholderif (m_resourceManager->recentColorsTotal() == 0) {@@ -676,7 +679,6 @@ void KisPopupPalette::paintEvent(QPaintEvent* e)painter.setPen(QPen(palette().color(QPalette::Window).lighter(150), 2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));painter.drawPath(emptyRecentColorsPath);} else {-for (int pos = 0; pos < m_resourceManager->recentColorsTotal(); pos++) {QPainterPath recentColorsPath(drawDonutPathAngle(m_colorHistoryInnerRadius, m_colorHistoryOuterRadius, m_resourceManager->recentColorsTotal()));@@ -695,7 +697,7 @@ void KisPopupPalette::paintEvent(QPaintEvent* e)QPainterPath path_ColorDonut(drawDonutPathFull(0, 0, m_colorHistoryInnerRadius, m_colorHistoryOuterRadius));painter.drawPath(path_ColorDonut);} else {- painter.rotate((m_resourceManager->recentColorsTotal() + hoveredColor()) *rotationAngle);+ painter.rotate((m_resourceManager->recentColorsTotal() + hoveredColor()) * rotationAngle);QPainterPath path(drawDonutPathAngle(m_colorHistoryInnerRadius, m_colorHistoryOuterRadius, m_resourceManager->recentColorsTotal()));painter.drawPath(path);painter.rotate(hoveredColor() * -1 * rotationAngle);@@ -710,7 +712,7 @@ void KisPopupPalette::paintEvent(QPaintEvent* e)QPainterPath path_ColorDonut(drawDonutPathFull(0, 0, m_colorHistoryInnerRadius, m_colorHistoryOuterRadius));painter.drawPath(path_ColorDonut);} else {- painter.rotate((m_resourceManager->recentColorsTotal() + selectedColor()) *rotationAngle);+ painter.rotate((m_resourceManager->recentColorsTotal() + selectedColor()) * rotationAngle);QPainterPath path(drawDonutPathAngle(m_colorHistoryInnerRadius, m_colorHistoryOuterRadius, m_resourceManager->recentColorsTotal()));painter.drawPath(path);painter.rotate(selectedColor() * -1 * rotationAngle);@@ -1147,13 +1149,10 @@ int KisPopupPalette::calculateColorIndex(QPointF position, int numColors) const// relative to palette centerQPointF relPosition = position - QPointF(0.5 * m_popupPaletteSize, 0.5 * m_popupPaletteSize);- qreal angle = qAtan2(relPosition.x(), relPosition.y()) + M_PI/numColors;- if (angle < 0) {- angle += 2 * M_PI;- }+ qreal angle = M_PI - qAtan2(relPosition.x(), relPosition.y()) + M_PI / numColors;+ angle = normalizeAngle(angle);int index = floor(angle * numColors / (2 * M_PI));-return qBound(0, index, numColors - 1);}