File indexing completed on 2024-05-19 04:36:36
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "ValuePropertyManager.h" 0021 0022 namespace tikz { 0023 namespace ui { 0024 0025 class ValuePropertyManagerPrivate 0026 { 0027 public: 0028 struct Data 0029 { 0030 tikz::Value val = 0.0_mm; 0031 tikz::Value minVal = 0.0_mm; 0032 tikz::Value maxVal = tikz::Value(INT_MAX, Unit::Millimeter); 0033 double singleStep = 0.1; 0034 int decimals = 2; 0035 bool readOnly = false; 0036 }; 0037 0038 typedef QMap<const QtProperty *, Data> PropertyValueMap; 0039 PropertyValueMap m_values; 0040 }; 0041 0042 ValuePropertyManager::ValuePropertyManager(QObject *parent) 0043 : QtAbstractPropertyManager(parent) 0044 , d_ptr(new ValuePropertyManagerPrivate()) 0045 { 0046 } 0047 0048 ValuePropertyManager::~ValuePropertyManager() 0049 { 0050 clear(); 0051 delete d_ptr; 0052 } 0053 0054 tikz::Value ValuePropertyManager::value(const QtProperty *property) const 0055 { 0056 const auto it = d_ptr->m_values.constFind(property); 0057 if (it == d_ptr->m_values.constEnd()) 0058 return 0.0_pt; 0059 return it.value().val; 0060 } 0061 0062 tikz::Value ValuePropertyManager::minimum(const QtProperty *property) const 0063 { 0064 const auto it = d_ptr->m_values.constFind(property); 0065 if (it == d_ptr->m_values.constEnd()) 0066 return 0.0_pt; 0067 return it.value().minVal; 0068 } 0069 0070 tikz::Value ValuePropertyManager::maximum(const QtProperty *property) const 0071 { 0072 const auto it = d_ptr->m_values.constFind(property); 0073 if (it == d_ptr->m_values.constEnd()) 0074 return 0.0_pt; 0075 return it.value().maxVal; 0076 } 0077 0078 double ValuePropertyManager::singleStep(const QtProperty *property) const 0079 { 0080 const auto it = d_ptr->m_values.constFind(property); 0081 if (it == d_ptr->m_values.constEnd()) 0082 return 0; 0083 return it.value().singleStep; 0084 } 0085 0086 int ValuePropertyManager::decimals(const QtProperty *property) const 0087 { 0088 const auto it = d_ptr->m_values.constFind(property); 0089 if (it == d_ptr->m_values.constEnd()) 0090 return 2; 0091 return it.value().decimals; 0092 } 0093 0094 bool ValuePropertyManager::isReadOnly(const QtProperty *property) const 0095 { 0096 const auto it = d_ptr->m_values.constFind(property); 0097 if (it == d_ptr->m_values.constEnd()) 0098 return false; 0099 return it.value().readOnly; 0100 } 0101 0102 QString ValuePropertyManager::valueText(const QtProperty *property) const 0103 { 0104 const auto it = d_ptr->m_values.constFind(property); 0105 if (it == d_ptr->m_values.constEnd()) 0106 return QString(); 0107 return it.value().val.toString(); 0108 } 0109 0110 void ValuePropertyManager::setValue(QtProperty *property, const tikz::Value & val) 0111 { 0112 const auto it = d_ptr->m_values.find(property); 0113 if (it == d_ptr->m_values.end()) 0114 return; 0115 0116 auto & data = it.value(); 0117 0118 if (data.val == val && data.val.unit() == val.unit()) 0119 return; 0120 0121 const tikz::Value oldVal = data.val; 0122 data.val = val < data.minVal ? data.minVal 0123 : val > data.maxVal ? data.maxVal 0124 : val; 0125 0126 if (data.val == oldVal && data.val.unit() == oldVal.unit()) 0127 return; 0128 0129 Q_EMIT propertyChanged(property); 0130 Q_EMIT valueChanged(property, data.val); 0131 } 0132 0133 void ValuePropertyManager::setSingleStep(QtProperty *property, double step) 0134 { 0135 const auto it = d_ptr->m_values.find(property); 0136 if (it == d_ptr->m_values.end()) 0137 return; 0138 0139 ValuePropertyManagerPrivate::Data data = it.value(); 0140 0141 if (step < 0) 0142 step = 0; 0143 0144 if (data.singleStep == step) 0145 return; 0146 0147 data.singleStep = step; 0148 0149 it.value() = data; 0150 0151 Q_EMIT singleStepChanged(property, data.singleStep); 0152 } 0153 0154 void ValuePropertyManager::setReadOnly(QtProperty *property, bool readOnly) 0155 { 0156 const auto it = d_ptr->m_values.find(property); 0157 if (it == d_ptr->m_values.end()) 0158 return; 0159 0160 ValuePropertyManagerPrivate::Data data = it.value(); 0161 0162 if (data.readOnly == readOnly) 0163 return; 0164 0165 data.readOnly = readOnly; 0166 it.value() = data; 0167 0168 Q_EMIT propertyChanged(property); 0169 Q_EMIT readOnlyChanged(property, data.readOnly); 0170 } 0171 0172 void ValuePropertyManager::setDecimals(QtProperty *property, int prec) 0173 { 0174 const auto it = d_ptr->m_values.find(property); 0175 if (it == d_ptr->m_values.end()) 0176 return; 0177 0178 ValuePropertyManagerPrivate::Data data = it.value(); 0179 0180 if (prec > 13) 0181 prec = 13; 0182 else if (prec < 0) 0183 prec = 0; 0184 0185 if (data.decimals == prec) 0186 return; 0187 0188 data.decimals = prec; 0189 0190 it.value() = data; 0191 0192 Q_EMIT decimalsChanged(property, data.decimals); 0193 } 0194 0195 void ValuePropertyManager::setMinimum(QtProperty *property, const tikz::Value & minVal) 0196 { 0197 const auto it = d_ptr->m_values.find(property); 0198 if (it == d_ptr->m_values.end()) 0199 return; 0200 0201 auto & data = it.value(); 0202 0203 data.minVal = minVal; 0204 0205 if (data.val < data.minVal) { 0206 data.val = data.minVal; 0207 0208 Q_EMIT propertyChanged(property); 0209 Q_EMIT valueChanged(property, data.val); 0210 } 0211 } 0212 0213 void ValuePropertyManager::setMaximum(QtProperty *property, const tikz::Value & maxVal) 0214 { 0215 const auto it = d_ptr->m_values.find(property); 0216 if (it == d_ptr->m_values.end()) 0217 return; 0218 0219 auto & data = it.value(); 0220 0221 data.maxVal = maxVal; 0222 0223 if (data.val > data.maxVal) { 0224 data.val = data.maxVal; 0225 0226 Q_EMIT propertyChanged(property); 0227 Q_EMIT valueChanged(property, data.val); 0228 } 0229 } 0230 0231 void ValuePropertyManager::setRange(QtProperty *property, const tikz::Value & minVal, const tikz::Value & maxVal) 0232 { 0233 Q_ASSERT(minVal <= maxVal); 0234 0235 const auto it = d_ptr->m_values.find(property); 0236 if (it == d_ptr->m_values.end()) 0237 return; 0238 0239 auto & data = it.value(); 0240 0241 data.minVal = minVal; 0242 data.maxVal = maxVal; 0243 0244 bool changed = false; 0245 0246 if (data.val < data.minVal) { 0247 data.val = data.minVal; 0248 changed = true; 0249 } 0250 0251 if (data.val > data.maxVal) { 0252 data.val = data.maxVal; 0253 changed = true; 0254 } 0255 0256 if (changed) { 0257 Q_EMIT propertyChanged(property); 0258 Q_EMIT valueChanged(property, data.val); 0259 } 0260 } 0261 0262 0263 void ValuePropertyManager::initializeProperty(QtProperty *property) 0264 { 0265 d_ptr->m_values[property] = ValuePropertyManagerPrivate::Data(); 0266 } 0267 0268 0269 void ValuePropertyManager::uninitializeProperty(QtProperty *property) 0270 { 0271 d_ptr->m_values.remove(property); 0272 } 0273 0274 } 0275 } 0276 0277 // kate: indent-width 4; replace-tabs on;