Krita Source Code Documentation
Loading...
Searching...
No Matches
KisTextPropertiesManager.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
10#include <KoSelection.h>
11#include <KoSvgTextShape.h>
13
14#include <KisView.h>
15#include <kis_canvas2.h>
17
18
29
31 : QObject(parent)
32 , d(new Private)
33{
34}
35
39
41{
42 d->providerConnectionStore.clear();
43 d->provider = provider;
44 if (d->provider) {
45 d->providerConnectionStore.addUniqueConnection(d->provider, SIGNAL(sigTextPropertiesChanged()), this, SLOT(slotTextPropertiesChanged()));
46 d->providerConnectionStore.addUniqueConnection(d->provider, SIGNAL(sigCharacterPropertiesChanged()), this, SLOT(slotCharacterPropertiesChanged()));
47 }
48}
49
51{
52 d->interfaceConnectionStore.clear();
53 d->interface = interface;
54 if (d->interface) {
55 d->interfaceConnectionStore.addUniqueConnection(d->interface, SIGNAL(textSelectionChanged()), this, SLOT(slotInterfaceSelectionChanged()));
56 d->interfaceConnectionStore.addUniqueConnection(d->interface, SIGNAL(textCharacterSelectionChanged()), this, SLOT(slotCharacterInterfaceSelectionChanged()));
59 }
60}
61
62KoSvgTextPropertyData textDataProperties(QList<KoSvgTextProperties> props, QSet<KoSvgTextProperties::PropertyId> propIds) {
63 KoSvgTextPropertyData textData;
64 textData.commonProperties = props.first();
65
66 for (auto it = props.begin(); it != props.end(); it++) {
67 for (int i = 0; i < propIds.size(); i++) {
68 KoSvgTextProperties::PropertyId p = propIds.values().at(i);
69 if (it->hasProperty(p)) {
70 if (textData.commonProperties.property(p) != it->property(p)) {
72 textData.tristate.insert(p);
73 }
74 } else {
76 textData.tristate.insert(p);
77 }
78 }
79 }
80 return textData;
81}
82
83QSet<KoSvgTextProperties::PropertyId> getTristate(QList<KoSvgTextProperties> props) {
84 QSet<KoSvgTextProperties::PropertyId> propIds;
85
86 for (auto it = props.begin(); it != props.end(); it++) {
88 for (int i = 0; i < p.properties().size(); i++) {
89 propIds.insert(p.properties().at(i));
90 }
91 }
92 return propIds;
93}
94
96 if (!d->interface || !d->provider) return;
97
98 QList<KoSvgTextProperties> props = d->interface->getSelectedProperties();
99 if (props.isEmpty()) return;
100
101 QSet<KoSvgTextProperties::PropertyId> propIds = getTristate(props);
102
103 KoSvgTextPropertyData textData = textDataProperties(props, propIds);
105 textData.enabled = true;
106
107 d->lastSetTextData = textData;
108 d->provider->setTextPropertyData(textData);
109
110}
111
113{
114 if (!d->interface || !d->provider) return;
115 KoSvgTextPropertyData charData;
116 if (d->interface->characterPropertiesEnabled()) {
117 QList<KoSvgTextProperties> charProps = d->interface->getCharacterProperties();
118 if (!charProps.isEmpty()) {
119 QSet<KoSvgTextProperties::PropertyId> charPropIds = getTristate(charProps);
120 charData = textDataProperties(charProps, charPropIds);
121 }
122 charData.inheritedProperties = d->interface->getInheritedProperties();
123 charData.spanSelection = d->interface->spanSelection();
124 charData.enabled = true;
125 }
126 d->lastSetCharacterData = charData;
127 d->provider->setCharacterPropertyData(charData);
128}
129
130QSet<KoSvgTextProperties::PropertyId> removedProps(KoSvgTextPropertyData textData, KoSvgTextPropertyData oldProps) {
131 QSet<KoSvgTextProperties::PropertyId> removeProperties;
133 oldPropIds.append(oldProps.tristate.values());
134 for (auto it = oldPropIds.begin(); it != oldPropIds.end(); it++) {
136 if (!textData.commonProperties.hasProperty(p) && !textData.tristate.contains(p)) {
137 removeProperties.insert(p);
138 }
139 }
140 return removeProperties;
141}
142
144{
145 if (!d->interface || !d->provider) return;
146 if (d->lastSetTextData == d->provider->textPropertyData()) return;
147
148 KoSvgTextPropertyData textData = d->provider->textPropertyData();
149
150 KoSvgTextProperties newProps = textData.commonProperties.ownProperties(d->lastSetTextData.commonProperties);
151 QSet<KoSvgTextProperties::PropertyId> removeProperties = removedProps(textData, d->lastSetTextData);
152
153 if (newProps.isEmpty() && removeProperties.isEmpty()) return;
154 d->interface->setPropertiesOnSelected(newProps, removeProperties);
155}
156
158{
159 if (!d->interface || !d->provider) return;
160 if (!d->interface->characterPropertiesEnabled()) return;
161 KoSvgTextPropertyData textData = d->provider->characterTextPropertyData();
162 if (textData == d->lastSetCharacterData) return;
163
164 KoSvgTextProperties newProps = textData.commonProperties.ownProperties(d->lastSetCharacterData.commonProperties);
165 QSet<KoSvgTextProperties::PropertyId> removeProperties = removedProps(textData, d->lastSetCharacterData);
166
167 if (newProps.isEmpty() && removeProperties.isEmpty()) return;
168 d->interface->setCharacterPropertiesOnSelected(newProps, removeProperties);
169}
const Params2D p
QSet< KoSvgTextProperties::PropertyId > getTristate(QList< KoSvgTextProperties > props)
QSet< KoSvgTextProperties::PropertyId > removedProps(KoSvgTextPropertyData textData, KoSvgTextPropertyData oldProps)
KoSvgTextPropertyData textDataProperties(QList< KoSvgTextProperties > props, QSet< KoSvgTextProperties::PropertyId > propIds)
KisTextPropertiesManager(QObject *parent=nullptr)
void setTextPropertiesInterface(KoSvgTextPropertiesInterface *interface)
setTextPropertiesInterface set the text properties interface. This should be done on tool activation....
QScopedPointer< Private > d
void setCanvasResourceProvider(KisCanvasResourceProvider *provider)
setCanvasResourceProvider set the canvas resource provider.
The KoSvgTextPropertiesInterface class.
void removeProperty(PropertyId id)
QList< PropertyId > properties() const
QVariant property(PropertyId id, const QVariant &defaultValue=QVariant()) const
bool hasProperty(PropertyId id) const
KoSvgTextProperties ownProperties(const KoSvgTextProperties &parentProperties, bool keepFontSize=false) const
KisSignalAutoConnectionsStore interfaceConnectionStore
KisSignalAutoConnectionsStore providerConnectionStore
The KoSvgTextPropertyData struct.
KoSvgTextProperties commonProperties
The properties common between all the selected text.
QSet< KoSvgTextProperties::PropertyId > tristate
The properties that are not common (tri-state) between the selected text.
bool spanSelection
Whether we're currently selecting a span of text as opposed to editing the whole paragraph.
KoSvgTextProperties inheritedProperties
The properties that are inherited, so that widgets may be set correctly.