Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_favorite_resource_manager.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2009 Vera Lukman <shicmap@gmail.com>
3 SPDX-FileCopyrightText: 2011 Sven Langkamp <sven.langkamp@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#include <kis_debug.h>
9#include <QPoint>
10#include <QString>
11#include <QColor>
14#include <KoID.h>
15#include <kconfig.h>
17#include "kis_popup_palette.h"
18#include "kis_paintop_box.h"
19#include "KisViewManager.h"
21#include "kis_config.h"
22#include "kis_config_notifier.h"
23#include <kis_paintop_preset.h>
25
27
28
30 : m_paintopBox(paintopBox)
31 , m_colorHistoryList(new KisSortedHistoryList<KoColor>(MAX_RECENT_COLOR))
32
33{
36
38 rServer->addObserver(this);
39}
40
46
52{
53 init();
54
55 QVector<QString> names;
56 for (int i = 0; i < m_maxPresets; i++) {
57 QModelIndex index = m_resourcesProxyModel->index(i, 0);
58 if (index.isValid()) {
59 QString name = m_resourcesProxyModel->data(index, Qt::UserRole + KisAbstractResourceModel::Name).toString();
60 names << name;
61 } else {
62 break; // no more valid indices
63 }
64 }
65
66 return names;
67}
68
70{
71 init();
72 QList<QImage> images;
73 for (int i = 0; i < m_maxPresets; i++) {
74 QModelIndex index = m_resourcesProxyModel->index(i, 0);
75 if (index.isValid()) {
76 QVariant tmp = m_resourcesProxyModel->data(index, Qt::UserRole + KisAbstractResourceModel::Thumbnail);
77 QImage image = tmp.value<QImage>();
78 images << image;
79 } else {
80 break; // no more valid indices
81 }
82 }
83 return images;
84}
85
87{
88 m_currentTag = tag;
90 KisConfig(false).writeEntry<QString>("favoritePresetsTag", tag->url());
92}
93
95{
96 //ENTER_FUNCTION() << ppVar(pos) << ppVar(numFavoritePresets());
97 if (pos < 0 || pos >= numFavoritePresets()) return;
98
99 QModelIndex index = m_resourcesProxyModel->index(pos, 0);
101
103
104 Q_EMIT hidePalettes();
105}
106
112
113//Recent Colors
115{
116 Q_EMIT setSelectedColor(pos);
117 Q_EMIT sigSetFGColor(m_colorHistoryList->at(pos));
118 Q_EMIT hidePalettes();
119}
120
122{
123 int pos = m_colorHistoryList->append(color);
124 Q_EMIT setSelectedColor(pos);
125}
126
131
136
141
146
151
156
161
166
171
173{
174 return preset1->name() < preset2->name();
175}
176
181
183{
184 KisConfig cfg(true);
187
189
190 compare_less sortingFunc;
191 const QString sortingType = cfg.readEntry("popuppalette/colorHistorySorting", QString("hsv"));
192 if (sortingType == "hsv") {
193 sortingFunc = [] (const KoColor &lhs, const KoColor &rhs) {
194 auto makeHsvTuple = [] (const KoColor &color) {
195 int h, s, v;
196 color.toQColor().getHsv(&h, &s, &v);
197 return std::make_tuple(h, s, v);
198 };
199 return makeHsvTuple(lhs) < makeHsvTuple(rhs);
200 };
201 }
202
203 m_colorHistoryList->setCompareLess(sortingFunc);
204}
205
210
212{
213 if (!m_initialized) {
214 m_initialized = true;
215
218
219 connect(m_resourcesProxyModel, SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(presetsChanged()));
220 connect(m_resourcesProxyModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(presetsChanged()));
221
223
225 QString currentTag = KisConfig(true).readEntry<QString>("favoritePresetsTag", "★ My Favorites");
226
228 KisTagSP currentTagSP = tagModel.tagForUrl(currentTag);
229 if (!currentTagSP.isNull()) {
230 m_currentTag = currentTagSP;
231 }
232 if (m_currentTag.isNull() && tagModel.rowCount() > 0) {
233 // safety measure to have at least *some* tag chosen
234 QModelIndex idx = tagModel.index(0, 0);
235 currentTagSP = tagModel.tagForIndex(idx);
236 if (currentTagSP && !m_currentTag) {
237 m_currentTag = currentTagSP;
238 }
239 }
242
244 }
245}
246
247
qreal v
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
static KisConfigNotifier * instance()
void writeEntry(const QString &name, const T &value)
Definition kis_config.h:779
int favoritePresets(bool defaultValue=false) const
T readEntry(const QString &name, const T &defaultValue=T())
Definition kis_config.h:789
void resourceChanged(QSharedPointer< KisPaintOpPreset > resource) override
void resourceAdded(QSharedPointer< KisPaintOpPreset > resource) override
void sigChangeFGColorSelector(const KoColor &)
KisFavoriteResourceManager(KisPaintopBox *paintopBox)
void removingResource(QSharedPointer< KisPaintOpPreset > resource) override
KisTagFilterResourceProxyModel * m_resourcesProxyModel
QScopedPointer< KisSortedHistoryList< KoColor > > m_colorHistoryList
void sigSetFGColor(const KoColor &c)
void resourceSelected(KoResourceSP resource)
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
static KisResourceServerProvider * instance()
KisPaintOpPresetResourceServer * paintOpPresetServer()
std::function< bool(const T &, const T &)> compare_less
The KisTagFilterResourceProxyModel class filters the resources by tag or resource name.
KoResourceSP resourceForIndex(QModelIndex index=QModelIndex()) const override
resourceForIndex returns a properly versioned and id'ed resource object
void setTagFilter(const KisTagSP tag)
setTagFilter sets the tag to filter with
void addObserver(ObserverType *observer)
void removeObserver(ObserverType *observer)
bool sortPresetByName(KisPaintOpPresetSP preset1, KisPaintOpPresetSP preset2)
const QString PaintOpPresets