Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Implement optional sorting in the "Last Used Colors" wheel in the popup

Dmitry Kazakov96d629d059d1
diff --git a/libs/global/KisHistoryList.h b/libs/global/KisHistoryList.h
index 0c2e5da1f5..177fcad6b7 100644
--- a/libs/global/KisHistoryList.h
+++ b/libs/global/KisHistoryList.h
@@ -14,6 +14,9 @@
template <typename T>
class KisHistoryList
{
+public:
+ using const_iterator = typename std::deque<T>::const_iterator;
+
public:
KisHistoryList(int size)
: m_maxSize(size)
@@ -77,6 +80,15 @@ public:
return m_maxSize;
}
+ const_iterator cbegin() const {
+ return m_values.cbegin();
+ }
+
+ const_iterator cend() const {
+ return m_values.cend();
+ }
+
+
private:
int m_maxSize = 0;
std::deque<T> m_values;
diff --git a/libs/global/KisSortedHistoryList.h b/libs/global/KisSortedHistoryList.h
new file mode 100644
index 0000000000..ce2cc86bdf
--- /dev/null
+++ b/libs/global/KisSortedHistoryList.h
@@ -0,0 +1,114 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef KISSORTEDHISTORYLIST_H
+#define KISSORTEDHISTORYLIST_H
+
+#include <vector>
+#include <KisHistoryList.h>
+
+template <typename T>
+class KisSortedHistoryList
+{
+public:
+ using const_iterator = typename std::vector<T>::const_iterator;
+ using compare_less = std::function<bool(const T&, const T&)>;
+
+public:
+ KisSortedHistoryList(int size)
+ : m_list(size)
+ {
+ }
+
+ /**
+ * @return the number of elements in the list
+ */
+ int size() const {
+ return m_list.size();
+ }
+
+ /**
+ * @return @pos's element of the sorted 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_sortedList.at(pos);
+ }
+
+ /**
+ * Add an element to the history list
+ *
+ * @return the position of the inserted element in the sorted list
+ */
+ int append(const T& value) {
+ int newElementIndex = m_list.append(value);;
+ if (resortList()) {
+ auto it = std::find(m_sortedList.begin(), m_sortedList.end(), value);
+ KIS_SAFE_ASSERT_RECOVER(it != m_sortedList.end()) {
+ it = m_sortedList.begin();
+ }
+ newElementIndex = std::distance(m_sortedList.begin(), it);
+ }
+
+ return newElementIndex;
+ }
+
+ /**
+ * Clear all historical elements from the list
+ */
+ void clear() {
+ m_list.clear();
+ m_sortedList.clear();
+ }
+
+ /**
+ * @return the maximum possible number of elements in the list
+ */
+ int maxSize() const {
+ return m_list.maxSize();
+ }
+
+ /**
+ * Set the comparison function for sorting the elements.
+ *
+ * Set to empty function `compare_less{}` to disable sorting completely
+ */
+ void setCompareLess(compare_less func) {
+ m_compareLess = func;
+ resortList();
+ }
+
+ const_iterator cbegin() const {
+ return m_sortedList.cbegin();
+ }
+
+ const_iterator cend() const {
+ return m_sortedList.cend();
+ }
+
+private:
+ bool resortList() {
+ m_sortedList.clear();
+ m_sortedList.reserve(m_list.size());
+ std::copy(m_list.cbegin(), m_list.cend(), std::back_inserter(m_sortedList));
+
+ if (m_compareLess) {
+ std::sort(m_sortedList.begin(), m_sortedList.end(), m_compareLess);
+ }
+
+ return bool(m_compareLess);
+ }
+
+private:
+ KisHistoryList<T> m_list;
+ std::vector<T> m_sortedList;
+ compare_less m_compareLess;
+};
+
+
+#endif // KISSORTEDHISTORYLIST_H
diff --git a/libs/global/tests/KisHistoryListTest.cpp b/libs/global/tests/KisHistoryListTest.cpp
index 0a9c0b0868..154549107f 100644
--- a/libs/global/tests/KisHistoryListTest.cpp
+++ b/libs/global/tests/KisHistoryListTest.cpp
@@ -8,6 +8,7 @@
#include <simpletest.h>
#include "KisHistoryList.h"
+#include "KisSortedHistoryList.h"
namespace {
@@ -15,14 +16,20 @@ auto refState = [](std::initializer_list<int> list) -> std::vector<int> {
return list;
};
-auto getState = [] (const KisHistoryList<int> &list) -> std::vector<int> {
+std::vector<int> getState(const KisHistoryList<int> &list) {
std::vector<int> state;
state.reserve(list.size());
- for (int i = 0; i < list.size(); i++) {
- state.push_back(list.at(i));
- }
+ std::copy(list.cbegin(), list.cend(), std::back_inserter(state));
return state;
-};
+}
+
+std::vector<int> getState(const KisSortedHistoryList<int> &list) {
+ std::vector<int> state;
+ state.reserve(list.size());
+ std::copy(list.cbegin(), list.cend(), std::back_inserter(state));
+ return state;
+}
+
}
void KisHistoryListTest::testRotation()
@@ -72,5 +79,48 @@ void KisHistoryListTest::testBubbleUp()
QCOMPARE(getState(list), refState({3, 5, 4, 2, 1}));
}
+void KisHistoryListTest::testSortedList()
+{
+ KisSortedHistoryList<int> list(5);
+
+ list.append(1);
+ list.append(2);
+ list.append(3);
+ list.append(4);
+ list.append(5);
+ list.append(3);
+ list.append(10);
+ list.append(11);
+ list.append(3);
+
+ // first, check if unsorted version works correctly
+ QCOMPARE(getState(list), refState({3, 11, 10, 5, 4}));
+
+ list.setCompareLess(std::less{});
+
+ // set up sorting, the values should be sorted now
+ QCOMPARE(getState(list), refState({3, 4, 5, 10, 11}));
+
+ list.append(12);
+
+ // the oldest values are dropped from the middle of the sorted list
+ QCOMPARE(getState(list), refState({3, 5, 10, 11, 12}));
+
+ list.append(13);
+
+ // more oldest values are dropped from the middle
+ QCOMPARE(getState(list), refState({3, 10, 11, 12, 13}));
+
+ list.setCompareLess(std::greater{});
+
+ // reverse sorting order
+ QCOMPARE(getState(list), refState({13, 12, 11, 10, 3}));
+
+ list.setCompareLess(KisSortedHistoryList<int>::compare_less{});
+
+ // disable sorting, show the original historical order
+ QCOMPARE(getState(list), refState({13, 12, 3, 11, 10}));
+}
+
SIMPLE_TEST_MAIN(KisHistoryListTest)
diff --git a/libs/global/tests/KisHistoryListTest.h b/libs/global/tests/KisHistoryListTest.h
index d70d5a57f7..ec231237c7 100644
--- a/libs/global/tests/KisHistoryListTest.h
+++ b/libs/global/tests/KisHistoryListTest.h
@@ -16,6 +16,7 @@ class KisHistoryListTest : public QObject
private Q_SLOTS:
void testRotation();
void testBubbleUp();
+ void testSortedList();
};
#endif // KISHISTORYLISTTEST_H
diff --git a/libs/ui/kis_favorite_resource_manager.cpp b/libs/ui/kis_favorite_resource_manager.cpp
index 6cfd9ffe8b..e0196a0fdb 100644
--- a/libs/ui/kis_favorite_resource_manager.cpp
+++ b/libs/ui/kis_favorite_resource_manager.cpp
@@ -22,19 +22,19 @@
#include "kis_config.h"
#include "kis_config_notifier.h"
#include <kis_paintop_preset.h>
-#include <KisHistoryList.h>
+#include <KisSortedHistoryList.h>
const int KisFavoriteResourceManager::MAX_RECENT_COLOR = 12;
KisFavoriteResourceManager::KisFavoriteResourceManager(KisPaintopBox *paintopBox)
: m_paintopBox(paintopBox)
- , m_colorHistoryList(new KisHistoryList<KoColor>(MAX_RECENT_COLOR))
+ , m_colorHistoryList(new KisSortedHistoryList<KoColor>(MAX_RECENT_COLOR))
{
- KisConfig cfg(true);
- m_maxPresets = cfg.favoritePresets();
connect(KisConfigNotifier::instance(), SIGNAL(configChanged()), SLOT(configChanged()));
+ configChanged();
+
KisPaintOpPresetResourceServer * rServer = KisResourceServerProvider::instance()->paintOpPresetServer();
rServer->addObserver(this);
}
@@ -185,6 +185,23 @@ void KisFavoriteResourceManager::configChanged()
KisConfig cfg(true);
m_maxPresets = cfg.favoritePresets();
updateFavoritePresets();
+
+ using compare_less = KisSortedHistoryList<KoColor>::compare_less;
+
+ compare_less sortingFunc;
+ const QString sortingType = cfg.readEntry("popuppalette/colorHistorySorting", QString("hsv"));
+ if (sortingType == "hsv") {
+ sortingFunc = [] (const KoColor &lhs, const KoColor &rhs) {
+ auto makeHsvTuple = [] (const KoColor &color) {
+ int h, s, v;
+ color.toQColor().getHsv(&h, &s, &v);
+ return std::make_tuple(h, s, v);
+ };
+ return makeHsvTuple(lhs) < makeHsvTuple(rhs);
+ };
+ }
+
+ m_colorHistoryList->setCompareLess(sortingFunc);
}
void KisFavoriteResourceManager::presetsChanged()
diff --git a/libs/ui/kis_favorite_resource_manager.h b/libs/ui/kis_favorite_resource_manager.h
index 6a481935cd..735092bf58 100644
--- a/libs/ui/kis_favorite_resource_manager.h
+++ b/libs/ui/kis_favorite_resource_manager.h
@@ -22,7 +22,7 @@ class QString;
class KisPaintopBox;
class KisPaintOpPreset;
template <typename T>
-class KisHistoryList;
+class KisSortedHistoryList;
class KisFavoriteResourceManager : public QObject, public KoResourceServerObserver<KisPaintOpPreset>
{
@@ -110,7 +110,7 @@ private:
KisTagModel* m_tagModel {nullptr};
KisTagFilterResourceProxyModel* m_resourcesProxyModel {nullptr};
KisResourceModel* m_resourceModel {nullptr};
- QScopedPointer<KisHistoryList<KoColor>> m_colorHistoryList;
+ QScopedPointer<KisSortedHistoryList<KoColor>> m_colorHistoryList;
};