source: trunk/tools/designer/src/components/propertyeditor/brushpropertymanager.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 11.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the Qt Designer of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "brushpropertymanager.h"
43#include "qtpropertymanager.h"
44#include "qtvariantproperty.h"
45#include "qtpropertybrowserutils_p.h"
46
47#include <QtCore/QCoreApplication>
48#include <QtCore/QVariant>
49#include <QtCore/QString>
50
51static const char *brushStyles[] = {
52QT_TRANSLATE_NOOP("BrushPropertyManager", "No brush"),
53QT_TRANSLATE_NOOP("BrushPropertyManager", "Solid"),
54QT_TRANSLATE_NOOP("BrushPropertyManager", "Dense 1"),
55QT_TRANSLATE_NOOP("BrushPropertyManager", "Dense 2"),
56QT_TRANSLATE_NOOP("BrushPropertyManager", "Dense 3"),
57QT_TRANSLATE_NOOP("BrushPropertyManager", "Dense 4"),
58QT_TRANSLATE_NOOP("BrushPropertyManager", "Dense 5"),
59QT_TRANSLATE_NOOP("BrushPropertyManager", "Dense 6"),
60QT_TRANSLATE_NOOP("BrushPropertyManager", "Dense 7"),
61QT_TRANSLATE_NOOP("BrushPropertyManager", "Horizontal"),
62QT_TRANSLATE_NOOP("BrushPropertyManager", "Vertical"),
63QT_TRANSLATE_NOOP("BrushPropertyManager", "Cross"),
64QT_TRANSLATE_NOOP("BrushPropertyManager", "Backward diagonal"),
65QT_TRANSLATE_NOOP("BrushPropertyManager", "Forward diagonal"),
66QT_TRANSLATE_NOOP("BrushPropertyManager", "Crossing diagonal"),
67};
68
69QT_BEGIN_NAMESPACE
70
71namespace qdesigner_internal {
72
73BrushPropertyManager::BrushPropertyManager()
74{
75}
76
77int BrushPropertyManager::brushStyleToIndex(Qt::BrushStyle st)
78{
79 switch (st) {
80 case Qt::NoBrush: return 0;
81 case Qt::SolidPattern: return 1;
82 case Qt::Dense1Pattern: return 2;
83 case Qt::Dense2Pattern: return 3;
84 case Qt::Dense3Pattern: return 4;
85 case Qt::Dense4Pattern: return 5;
86 case Qt::Dense5Pattern: return 6;
87 case Qt::Dense6Pattern: return 7;
88 case Qt::Dense7Pattern: return 8;
89 case Qt::HorPattern: return 9;
90 case Qt::VerPattern: return 10;
91 case Qt::CrossPattern: return 11;
92 case Qt::BDiagPattern: return 12;
93 case Qt::FDiagPattern: return 13;
94 case Qt::DiagCrossPattern: return 14;
95 default: break;
96 }
97 return 0;
98}
99
100Qt::BrushStyle BrushPropertyManager::brushStyleIndexToStyle(int brushStyleIndex)
101{
102 switch (brushStyleIndex) {
103 case 0: return Qt::NoBrush;
104 case 1: return Qt::SolidPattern;
105 case 2: return Qt::Dense1Pattern;
106 case 3: return Qt::Dense2Pattern;
107 case 4: return Qt::Dense3Pattern;
108 case 5: return Qt::Dense4Pattern;
109 case 6: return Qt::Dense5Pattern;
110 case 7: return Qt::Dense6Pattern;
111 case 8: return Qt::Dense7Pattern;
112 case 9: return Qt::HorPattern;
113 case 10: return Qt::VerPattern;
114 case 11: return Qt::CrossPattern;
115 case 12: return Qt::BDiagPattern;
116 case 13: return Qt::FDiagPattern;
117 case 14: return Qt::DiagCrossPattern;
118 }
119 return Qt::NoBrush;
120}
121
122const BrushPropertyManager::EnumIndexIconMap &BrushPropertyManager::brushStyleIcons()
123{
124 // Create a map of icons for the brush style editor
125 static EnumIndexIconMap rc;
126 if (rc.empty()) {
127 const int brushStyleCount = sizeof(brushStyles)/sizeof(const char *);
128 QBrush brush(Qt::black);
129 const QIcon solidIcon = QtPropertyBrowserUtils::brushValueIcon(brush);
130 for (int i = 0; i < brushStyleCount; i++) {
131 const Qt::BrushStyle style = brushStyleIndexToStyle(i);
132 brush.setStyle(style);
133 rc.insert(i, QtPropertyBrowserUtils::brushValueIcon(brush));
134 }
135 }
136 return rc;
137}
138
139QString BrushPropertyManager::brushStyleIndexToString(int brushStyleIndex)
140{
141 const int brushStyleCount = sizeof(brushStyles)/sizeof(const char *);
142 return brushStyleIndex < brushStyleCount ? QCoreApplication::translate("BrushPropertyManager", brushStyles[brushStyleIndex]) : QString();
143}
144
145void BrushPropertyManager::initializeProperty(QtVariantPropertyManager *vm, QtProperty *property, int enumTypeId)
146{
147 m_brushValues.insert(property, QBrush());
148 // style
149 QtVariantProperty *styleSubProperty = vm->addProperty(enumTypeId, QCoreApplication::translate("BrushPropertyManager", "Style"));
150 property->addSubProperty(styleSubProperty);
151 QStringList styles;
152 const int brushStyleCount = sizeof(brushStyles)/sizeof(const char *);
153 for (int i = 0; i < brushStyleCount; i++)
154 styles.push_back(QCoreApplication::translate("BrushPropertyManager", brushStyles[i]));
155 styleSubProperty->setAttribute(QLatin1String("enumNames"), styles);
156 styleSubProperty->setAttribute(QLatin1String("enumIcons"), qVariantFromValue(brushStyleIcons()));
157 m_brushPropertyToStyleSubProperty.insert(property, styleSubProperty);
158 m_brushStyleSubPropertyToProperty.insert(styleSubProperty, property);
159 // color
160 QtVariantProperty *colorSubProperty = vm->addProperty(QVariant::Color, QCoreApplication::translate("BrushPropertyManager", "Color"));
161 property->addSubProperty(colorSubProperty);
162 m_brushPropertyToColorSubProperty.insert(property, colorSubProperty);
163 m_brushColorSubPropertyToProperty.insert(colorSubProperty, property);
164}
165
166bool BrushPropertyManager::uninitializeProperty(QtProperty *property)
167{
168 const PropertyBrushMap::iterator brit = m_brushValues.find(property); // Brushes
169 if (brit == m_brushValues.end())
170 return false;
171 m_brushValues.erase(brit);
172 // style
173 PropertyToPropertyMap::iterator subit = m_brushPropertyToStyleSubProperty.find(property);
174 if (subit != m_brushPropertyToStyleSubProperty.end()) {
175 QtProperty *styleProp = subit.value();
176 m_brushStyleSubPropertyToProperty.remove(styleProp);
177 m_brushPropertyToStyleSubProperty.erase(subit);
178 delete styleProp;
179 }
180 // color
181 subit = m_brushPropertyToColorSubProperty.find(property);
182 if (subit != m_brushPropertyToColorSubProperty.end()) {
183 QtProperty *colorProp = subit.value();
184 m_brushColorSubPropertyToProperty.remove(colorProp);
185 m_brushPropertyToColorSubProperty.erase(subit);
186 delete colorProp;
187 }
188 return true;
189}
190
191void BrushPropertyManager::slotPropertyDestroyed(QtProperty *property)
192{
193 PropertyToPropertyMap::iterator subit = m_brushStyleSubPropertyToProperty.find(property);
194 if (subit != m_brushStyleSubPropertyToProperty.end()) {
195 m_brushPropertyToStyleSubProperty[subit.value()] = 0;
196 m_brushStyleSubPropertyToProperty.erase(subit);
197 }
198 subit = m_brushColorSubPropertyToProperty.find(property);
199 if (subit != m_brushColorSubPropertyToProperty.end()) {
200 m_brushPropertyToColorSubProperty[subit.value()] = 0;
201 m_brushColorSubPropertyToProperty.erase(subit);
202 }
203}
204
205
206BrushPropertyManager::ValueChangedResult BrushPropertyManager::valueChanged(QtVariantPropertyManager *vm, QtProperty *property, const QVariant &value)
207{
208 switch (value.type()) {
209 case QVariant::Int: // Style subproperty?
210 if (QtProperty *brushProperty = m_brushStyleSubPropertyToProperty.value(property, 0)) {
211 const QBrush oldValue = m_brushValues.value(brushProperty);
212 QBrush newBrush = oldValue;
213 const int index = value.toInt();
214 newBrush.setStyle(brushStyleIndexToStyle(index));
215 if (newBrush == oldValue)
216 return Unchanged;
217 vm->variantProperty(brushProperty)->setValue(newBrush);
218 return Changed;
219 }
220 break;
221 case QVariant::Color: // Color subproperty?
222 if (QtProperty *brushProperty = m_brushColorSubPropertyToProperty.value(property, 0)) {
223 const QBrush oldValue = m_brushValues.value(brushProperty);
224 QBrush newBrush = oldValue;
225 newBrush.setColor(qvariant_cast<QColor>(value));
226 if (newBrush == oldValue)
227 return Unchanged;
228 vm->variantProperty(brushProperty)->setValue(newBrush);
229 return Changed;
230 }
231 break;
232 default:
233 break;
234 }
235 return NoMatch;
236}
237
238BrushPropertyManager::ValueChangedResult BrushPropertyManager::setValue(QtVariantPropertyManager *vm, QtProperty *property, const QVariant &value)
239{
240 if (value.type() != QVariant::Brush)
241 return NoMatch;
242 const PropertyBrushMap::iterator brit = m_brushValues.find(property);
243 if (brit == m_brushValues.end())
244 return NoMatch;
245
246 const QBrush newBrush = qvariant_cast<QBrush>(value);
247 if (newBrush == brit.value())
248 return Unchanged;
249 brit.value() = newBrush;
250 if (QtProperty *styleProperty = m_brushPropertyToStyleSubProperty.value(property))
251 vm->variantProperty(styleProperty)->setValue(brushStyleToIndex(newBrush.style()));
252 if (QtProperty *colorProperty = m_brushPropertyToColorSubProperty.value(property))
253 vm->variantProperty(colorProperty)->setValue(newBrush.color());
254
255 return Changed;
256}
257
258bool BrushPropertyManager::valueText(const QtProperty *property, QString *text) const
259{
260 const PropertyBrushMap::const_iterator brit = m_brushValues.constFind(const_cast<QtProperty *>(property));
261 if (brit == m_brushValues.constEnd())
262 return false;
263 const QBrush &brush = brit.value();
264 const QString styleName = brushStyleIndexToString(brushStyleToIndex(brush.style()));
265 *text = QCoreApplication::translate("BrushPropertyManager", "[%1, %2]").arg(styleName).arg(QtPropertyBrowserUtils::colorValueText(brush.color()));
266 return true;
267}
268
269bool BrushPropertyManager::valueIcon(const QtProperty *property, QIcon *icon) const
270{
271 const PropertyBrushMap::const_iterator brit = m_brushValues.constFind(const_cast<QtProperty *>(property));
272 if (brit == m_brushValues.constEnd())
273 return false;
274 *icon = QtPropertyBrowserUtils::brushValueIcon(brit.value());
275 return true;
276}
277
278bool BrushPropertyManager::value(const QtProperty *property, QVariant *v) const
279{
280 const PropertyBrushMap::const_iterator brit = m_brushValues.constFind(const_cast<QtProperty *>(property));
281 if (brit == m_brushValues.constEnd())
282 return false;
283 qVariantSetValue(*v, brit.value());
284 return true;
285}
286}
287
288QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.