18#ifndef TOOLPROPERTIES_H
19#define TOOLPROPERTIES_H
43 : mValueType(INTEGER) {
46 mDefaultValue = defaultValue;
47 setBaseValue(defaultValue);
53 mDefaultValue = defaultValue;
54 setBaseValue(defaultValue);
61 mDefaultValue = defaultValue;
69 void setBaseValue(
int newValue) {
70 Q_ASSERT(mValueType == INTEGER);
71 mBaseValue = qBound(mMinValue.
toInt(), newValue, mMaxValue.
toInt());
74 void setBaseValue(qreal newValue) {
75 Q_ASSERT(mValueType == REAL);
76 mBaseValue = qBound(mMinValue.
toReal(), newValue, mMaxValue.
toReal());
79 void setBaseValue(
bool newValue) {
80 Q_ASSERT(mValueType == BOOL);
81 mBaseValue = newValue;
97 int intValue()
const {
98 if (mValueType != INTEGER) {
103 return mBaseValue.
toInt();
106 qreal realValue()
const {
107 if (mValueType != REAL) {
112 return mBaseValue.
toReal();
115 bool boolValue()
const {
116 if (mValueType != BOOL) {
120 return mBaseValue.
toBool();
123 qreal minReal()
const {
124 if (mValueType != REAL) {
127 return mMinValue.
toReal();
130 qreal maxReal()
const {
131 if (mValueType != REAL) {
135 return mMaxValue.
toReal();
139 if (mValueType != INTEGER) {
142 return mMinValue.
toInt();
146 if (mValueType != INTEGER) {
150 return mMaxValue.
toInt();
153 void resetBaseValue() {
154 switch (mValueType) {
156 mBaseValue = mDefaultValue;
159 mBaseValue = mDefaultValue;
162 mBaseValue = mDefaultValue;
171 if (mValueType != INTEGER) {
174 return mDefaultValue.
toInt();
179 if (mValueType != REAL) {
182 return mDefaultValue.
toReal();
187 if (mValueType != BOOL) {
190 return mDefaultValue.
toBool();
193 ValueType type()
const {
return mValueType; }
196 ValueType mValueType;
216 mProps.
insert(properties);
228 mIdentifier = toolIdentifier;
231 Q_ASSERT(mProps.
count() > 0);
232 for (
auto it = mProps.
begin(); it != mProps.
end(); ++it) {
235 const QString& settingName = identifier(it.key());
236 loadProperty(settingName, info, settings);
243 settings.
setValue(mVersionKey, mVersion);
246 for (
auto it = mProps.
begin(); it != mProps.
end(); ++it) {
248 QString propertyId = identifier(it.key());
249 if (it.value().type() == PropertyInfo::INVALID) {
250 Q_ASSERT_X(
false, __func__,
"Wrong state, expected a value type INTEGER|REAL|BOOL but got INVALID. You've probably misconfigured the property. "
251 "Ensure the property has been setup correctly and try again.");
254 settings.
setValue(propertyId, it.value().toVariant());
265 void setBaseValue(
int rawType,
const PropertyInfo& info)
267 if (!isValidType(rawType)) {
return; }
271 case PropertyInfo::INTEGER:
272 mProps[rawType].setBaseValue(info.intValue());
274 case PropertyInfo::REAL:
275 mProps[rawType].setBaseValue(info.realValue());
277 case PropertyInfo::BOOL:
278 mProps[rawType].setBaseValue(info.boolValue());
280 case PropertyInfo::INVALID:
281 Q_ASSERT_X(
false, __func__,
"Expected value but got INVALID. Make sure the property has been setup properly before trying to set its base value.");
286 void setVersion(Version version) { mVersion = version; }
290 return mProps[rawPropertyType];
293 void restoreProperties() {
294 for (
auto it = mProps.
begin(); it != mProps.
end(); ++it) {
295 it.
value().resetBaseValue();
300 bool requireMigration(
QSettings& settings, ToolProperties::Version version) {
302 if (hasLegacySettings(settings) && !settings.
contains(mVersionKey)) {
307 int migrationNumber =
static_cast<int>(version);
309 return settings.
contains(mVersionKey) && migrationNumber < mVersion && migrationNumber > settings.
value(mVersionKey).
toInt();
312 bool hasLegacySettings(
QSettings& settings)
const {
317 bool isValidType(
int rawType)
const {
319 for (
const auto& r : mTypeRanges) {
322 rangeCheck +=
QString(
"[%1...%2]").
arg(r.first).
arg(r.second);
324 if (rawType >= r.first && rawType < r.second) {
328 qWarning() << __FUNCTION__ <<
":" <<
QString(
"Expected a valid type in range of %1 but instead got: %2. Make sure the input value matches one of the ranges of the tool setting").
arg(rangeCheck).
arg(rawType);
333 this->mIdentifiers.
insert(identifiers);
336 QString identifier(
int rawKey)
const {
337 auto it = mIdentifiers.
find(rawKey);
338 Q_ASSERT_X(it != mIdentifiers.
end(),
339 "ToolSettings::identifier",
340 QString(
"No identifier matching the key %1 found").arg(rawKey).toUtf8().constData());
351 switch (info.type()) {
352 case PropertyInfo::INTEGER: {
354 info.setBaseValue(value.
toInt());
357 case PropertyInfo::REAL: {
359 info.setBaseValue(value.
toReal());
362 case PropertyInfo::BOOL: {
364 info.setBaseValue(value.
toBool());
367 case PropertyInfo::INVALID: {
368 Q_ASSERT_X(
false, __func__,
"Wrong state, expected a value type but got INVALID. You've probably misconfigured the property. "
369 "Ensure the property has been setup correctly and try again.");
381 QString mIdentifier =
"undefined";
382 Version mVersion = VERSION_1;
383 QString mVersionKey =
"ToolProperties_Version";
391 virtual PropertyInfo getInfo(
int rawPropertyType)
const = 0;
402 STABILIZATION_VALUE = 102,
403 PRESSURE_ENABLED = 103,
404 INVISIBILITY_ENABLED = 104,
405 FEATHER_ENABLED = 105,
406 ANTI_ALIASING_ENABLED = 106,
407 FILLCONTOUR_ENABLED = 107,
413 mToolProperties.setRanges({ { START, END } });
415 mToolProperties.insertIdentifiers({
416 { WIDTH_VALUE,
"Width" },
417 { FEATHER_VALUE,
"Feather" },
418 { FEATHER_ENABLED,
"FeatherEnabled" },
419 { STABILIZATION_VALUE,
"LineStabilization" },
420 { PRESSURE_ENABLED,
"PressureEnabled" },
421 { INVISIBILITY_ENABLED,
"InvisibilityEnabled" },
422 { ANTI_ALIASING_ENABLED,
"AntiAliasingEnabled"},
423 { FILLCONTOUR_ENABLED,
"FillContourEnabled" }
427 ToolProperties& toolProperties()
override {
return mToolProperties; }
430 mToolProperties.addRange(range);
433 PropertyInfo getInfo(
int rawPropertyType)
const override {
434 return mToolProperties.getInfo(rawPropertyType);
437 qreal width()
const {
return getInfo(WIDTH_VALUE).realValue(); }
438 qreal feather()
const {
return getInfo(FEATHER_VALUE).realValue(); }
439 int stabilizerLevel()
const {
return getInfo(STABILIZATION_VALUE).intValue(); }
440 bool pressureEnabled()
const {
return getInfo(PRESSURE_ENABLED).boolValue(); }
441 bool invisibilityEnabled()
const {
return getInfo(INVISIBILITY_ENABLED).boolValue(); }
442 bool featherEnabled()
const {
return getInfo(FEATHER_ENABLED).boolValue(); }
443 bool AntiAliasingEnabled()
const {
return getInfo(ANTI_ALIASING_ENABLED).boolValue(); }
444 bool fillContourEnabled()
const {
return getInfo(FILLCONTOUR_ENABLED).boolValue(); }
458 CLOSEDPATH_ENABLED = START,
459 BEZIERPATH_ENABLED = 201,
465 toolProperties().addRange({START, END});
467 toolProperties().insertIdentifiers({
468 { CLOSEDPATH_ENABLED,
"ClosedPathEnabled"},
469 { BEZIERPATH_ENABLED,
"BezierPathEnabled" }
473 ToolProperties& toolProperties()
override {
return mStrokeToolProperties.toolProperties(); }
476 PropertyInfo getInfo(
int rawPropertyType)
const override {
477 return mStrokeToolProperties.getInfo(rawPropertyType);
480 qreal width()
const {
return getInfo(StrokeToolProperties::WIDTH_VALUE).realValue(); }
481 bool closedPathEnabled()
const {
return getInfo(CLOSEDPATH_ENABLED).boolValue(); }
482 bool bezierPathEnabled()
const {
return getInfo(BEZIERPATH_ENABLED).boolValue(); }
483 bool AntiAliasingEnabled()
const {
return getInfo(StrokeToolProperties::ANTI_ALIASING_ENABLED).boolValue(); }
493 FILLTHICKNESS_VALUE = START,
495 COLORTOLERANCE_VALUE = 301,
496 FILLEXPAND_VALUE = 302,
497 FILLLAYERREFERENCEMODE_VALUE = 303,
498 FILLMODE_VALUE = 304,
499 COLORTOLERANCE_ENABLED = 305,
500 FILLEXPAND_ENABLED = 306,
506 mToolProperties.setRanges({ { START, END } });
508 mToolProperties.insertIdentifiers({
509 { FILLTHICKNESS_VALUE,
"FillThickness"},
510 { COLORTOLERANCE_VALUE,
"ColorTolerance"},
511 { COLORTOLERANCE_ENABLED,
"ColorToleranceEnabled"},
512 { FILLEXPAND_VALUE,
"FillExpand"},
513 { FILLEXPAND_ENABLED,
"FillExpandEnabled"},
514 { COLORTOLERANCE_ENABLED,
"ColorToleranceEnabled"},
515 { FILLLAYERREFERENCEMODE_VALUE,
"FillReferenceMode"},
516 { FILLMODE_VALUE,
"FillMode"}
520 ToolProperties& toolProperties()
override {
return mToolProperties; }
522 PropertyInfo getInfo(
int rawPropertyType)
const override {
523 return mToolProperties.getInfo(rawPropertyType);
526 qreal fillThickness()
const {
return getInfo(FILLTHICKNESS_VALUE).realValue(); }
527 int tolerance()
const {
return getInfo(COLORTOLERANCE_VALUE).intValue(); }
528 int fillExpandAmount()
const {
return getInfo(FILLEXPAND_VALUE).intValue(); }
529 int fillReferenceMode()
const {
return getInfo(FILLLAYERREFERENCEMODE_VALUE).intValue(); }
530 int fillMode()
const {
return getInfo(FILLMODE_VALUE).intValue(); }
531 bool colorToleranceEnabled()
const {
return getInfo(COLORTOLERANCE_ENABLED).boolValue(); }
532 bool fillExpandEnabled()
const {
return getInfo(FILLEXPAND_ENABLED).boolValue(); }
542 SHOWPATH_ENABLED = START,
544 PATH_DOTCOLOR_TYPE = 401,
550 mToolProperties.setRanges({ { START, END }});
552 mToolProperties.insertIdentifiers({
553 { SHOWPATH_ENABLED,
"ShowPathEnabled"},
554 { PATH_DOTCOLOR_TYPE,
"PathDotColorType"},
558 ToolProperties& toolProperties()
override {
return mToolProperties; }
560 PropertyInfo getInfo(
int rawPropertyType)
const override {
561 return mToolProperties.getInfo(rawPropertyType);
564 bool showPathEnabled()
const {
return getInfo(SHOWPATH_ENABLED).boolValue(); }
565 DotColorType dotColorType()
const {
return static_cast<DotColorType
>(getInfo(PATH_DOTCOLOR_TYPE).intValue()); }
576 SHOWSELECTIONINFO_ENABLED = START,
577 ANTI_ALIASING_ENABLED = 501,
582 mProperties.setRanges({ { START, END } });
584 mProperties.insertIdentifiers({
585 { SHOWSELECTIONINFO_ENABLED,
"ShowSelectionInfoEnabled" },
586 { ANTI_ALIASING_ENABLED,
"AntiAliasingEnabled" }
592 PropertyInfo getInfo(
int rawPropertyType)
const override {
593 return mProperties.getInfo(rawPropertyType);
596 bool showSelectionInfoEnabled()
const {
return getInfo(SHOWSELECTIONINFO_ENABLED).boolValue(); }
597 bool antiAliasingEnabled()
const {
return getInfo(ANTI_ALIASING_ENABLED).boolValue(); }
int count(const Key &key) const const
QHash::iterator find(const Key &key)
QHash::iterator insert(const Key &key, const T &value)
const T value(const Key &key) const const
void beginGroup(const QString &prefix)
bool contains(const QString &key) const const
void setValue(const QString &key, const QVariant &value)
QVariant value(const QString &key, const QVariant &defaultValue) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool toBool() const const
int toInt(bool *ok) const const
qreal toReal(bool *ok) const const
void append(const T &value)
qreal defaultReal()
Returns the default value as an real, otherwise -1.0 if it hasn't been specified or the type doesn't ...
int defaultInt()
Returns the default value as an real, otherwise -1 if it hasn't been specified or the type doesn't ma...
bool defaultBool()
Returns the default value as an bool, otherwise false if it hasn't been specified or the type doesn't...