/**************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of a Qt Solutions component. ** ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor ** the names of its contributors may be used to endorse or promote ** products derived from this software without specific prior written ** permission. ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ****************************************************************************/ #include #include #include #include "qtpropertybrowser.h" #include "qteditorfactory.h" #include "qttreepropertybrowser.h" class DecoratedDoublePropertyManager : public QtDoublePropertyManager { Q_OBJECT public: DecoratedDoublePropertyManager(QObject *parent = 0); ~DecoratedDoublePropertyManager(); QString prefix(const QtProperty *property) const; QString suffix(const QtProperty *property) const; public Q_SLOTS: void setPrefix(QtProperty *property, const QString &prefix); void setSuffix(QtProperty *property, const QString &suffix); Q_SIGNALS: void prefixChanged(QtProperty *property, const QString &prefix); void suffixChanged(QtProperty *property, const QString &suffix); protected: QString valueText(const QtProperty *property) const; virtual void initializeProperty(QtProperty *property); virtual void uninitializeProperty(QtProperty *property); private: struct Data { QString prefix; QString suffix; }; QMap propertyToData; }; DecoratedDoublePropertyManager::DecoratedDoublePropertyManager(QObject *parent) : QtDoublePropertyManager(parent) { } DecoratedDoublePropertyManager::~DecoratedDoublePropertyManager() { } QString DecoratedDoublePropertyManager::prefix(const QtProperty *property) const { if (!propertyToData.contains(property)) return QString(); return propertyToData[property].prefix; } QString DecoratedDoublePropertyManager::suffix(const QtProperty *property) const { if (!propertyToData.contains(property)) return QString(); return propertyToData[property].suffix; } void DecoratedDoublePropertyManager::setPrefix(QtProperty *property, const QString &prefix) { if (!propertyToData.contains(property)) return; DecoratedDoublePropertyManager::Data data = propertyToData[property]; if (data.prefix == prefix) return; data.prefix = prefix; propertyToData[property] = data; emit propertyChanged(property); emit prefixChanged(property, prefix); } void DecoratedDoublePropertyManager::setSuffix(QtProperty *property, const QString &suffix) { if (!propertyToData.contains(property)) return; DecoratedDoublePropertyManager::Data data = propertyToData[property]; if (data.suffix == suffix) return; data.suffix = suffix; propertyToData[property] = data; emit propertyChanged(property); emit suffixChanged(property, suffix); } QString DecoratedDoublePropertyManager::valueText(const QtProperty *property) const { QString text = QtDoublePropertyManager::valueText(property); if (!propertyToData.contains(property)) return text; DecoratedDoublePropertyManager::Data data = propertyToData[property]; text = data.prefix + text + data.suffix; return text; } void DecoratedDoublePropertyManager::initializeProperty(QtProperty *property) { propertyToData[property] = DecoratedDoublePropertyManager::Data(); QtDoublePropertyManager::initializeProperty(property); } void DecoratedDoublePropertyManager::uninitializeProperty(QtProperty *property) { propertyToData.remove(property); QtDoublePropertyManager::uninitializeProperty(property); } class DecoratedDoubleSpinBoxFactory : public QtAbstractEditorFactory { Q_OBJECT public: DecoratedDoubleSpinBoxFactory(QObject *parent = 0); ~DecoratedDoubleSpinBoxFactory(); protected: void connectPropertyManager(DecoratedDoublePropertyManager *manager); QWidget *createEditor(DecoratedDoublePropertyManager *manager, QtProperty *property, QWidget *parent); void disconnectPropertyManager(DecoratedDoublePropertyManager *manager); private slots: void slotPrefixChanged(QtProperty *property, const QString &prefix); void slotSuffixChanged(QtProperty *property, const QString &prefix); void slotEditorDestroyed(QObject *object); private: /* We delegate responsibilities for QtDoublePropertyManager, which is a base class of DecoratedDoublePropertyManager to appropriate QtDoubleSpinBoxFactory */ QtDoubleSpinBoxFactory *originalFactory; QMap > createdEditors; QMap editorToProperty; }; DecoratedDoubleSpinBoxFactory::DecoratedDoubleSpinBoxFactory(QObject *parent) : QtAbstractEditorFactory(parent) { originalFactory = new QtDoubleSpinBoxFactory(this); } DecoratedDoubleSpinBoxFactory::~DecoratedDoubleSpinBoxFactory() { // not need to delete editors because they will be deleted by originalFactory in its destructor } void DecoratedDoubleSpinBoxFactory::connectPropertyManager(DecoratedDoublePropertyManager *manager) { originalFactory->addPropertyManager(manager); connect(manager, SIGNAL(prefixChanged(QtProperty *, const QString &)), this, SLOT(slotPrefixChanged(QtProperty *, const QString &))); connect(manager, SIGNAL(suffixChanged(QtProperty *, const QString &)), this, SLOT(slotSuffixChanged(QtProperty *, const QString &))); } QWidget *DecoratedDoubleSpinBoxFactory::createEditor(DecoratedDoublePropertyManager *manager, QtProperty *property, QWidget *parent) { QtAbstractEditorFactoryBase *base = originalFactory; QWidget *w = base->createEditor(property, parent); if (!w) return 0; QDoubleSpinBox *spinBox = qobject_cast(w); if (!spinBox) return 0; spinBox->setPrefix(manager->prefix(property)); spinBox->setSuffix(manager->suffix(property)); createdEditors[property].append(spinBox); editorToProperty[spinBox] = property; return spinBox; } void DecoratedDoubleSpinBoxFactory::disconnectPropertyManager(DecoratedDoublePropertyManager *manager) { originalFactory->removePropertyManager(manager); disconnect(manager, SIGNAL(prefixChanged(QtProperty *, const QString &)), this, SLOT(slotPrefixChanged(QtProperty *, const QString &))); disconnect(manager, SIGNAL(suffixChanged(QtProperty *, const QString &)), this, SLOT(slotSuffixChanged(QtProperty *, const QString &))); } void DecoratedDoubleSpinBoxFactory::slotPrefixChanged(QtProperty *property, const QString &prefix) { if (!createdEditors.contains(property)) return; DecoratedDoublePropertyManager *manager = propertyManager(property); if (!manager) return; QList editors = createdEditors[property]; QListIterator itEditor(editors); while (itEditor.hasNext()) { QDoubleSpinBox *editor = itEditor.next(); editor->setPrefix(prefix); } } void DecoratedDoubleSpinBoxFactory::slotSuffixChanged(QtProperty *property, const QString &prefix) { if (!createdEditors.contains(property)) return; DecoratedDoublePropertyManager *manager = propertyManager(property); if (!manager) return; QList editors = createdEditors[property]; QListIterator itEditor(editors); while (itEditor.hasNext()) { QDoubleSpinBox *editor = itEditor.next(); editor->setSuffix(prefix); } } void DecoratedDoubleSpinBoxFactory::slotEditorDestroyed(QObject *object) { QMap::ConstIterator itEditor = editorToProperty.constBegin(); while (itEditor != editorToProperty.constEnd()) { if (itEditor.key() == object) { QDoubleSpinBox *editor = itEditor.key(); QtProperty *property = itEditor.value(); editorToProperty.remove(editor); createdEditors[property].removeAll(editor); if (createdEditors[property].isEmpty()) createdEditors.remove(property); return; } itEditor++; } } int main(int argc, char **argv) { QApplication app(argc, argv); QtDoublePropertyManager *undecoratedManager = new QtDoublePropertyManager(); QtProperty *undecoratedProperty = undecoratedManager->addProperty("Undecorated"); undecoratedManager->setValue(undecoratedProperty, 123.45); DecoratedDoublePropertyManager *decoratedManager = new DecoratedDoublePropertyManager(); QtProperty *decoratedProperty = decoratedManager->addProperty("Decorated"); decoratedManager->setPrefix(decoratedProperty, "speed: "); decoratedManager->setSuffix(decoratedProperty, " km/h"); decoratedManager->setValue(decoratedProperty, 123.45); QtDoubleSpinBoxFactory *undecoratedFactory = new QtDoubleSpinBoxFactory(); DecoratedDoubleSpinBoxFactory *decoratedFactory = new DecoratedDoubleSpinBoxFactory(); QtTreePropertyBrowser *editor = new QtTreePropertyBrowser(); editor->setFactoryForManager(undecoratedManager, undecoratedFactory); editor->setFactoryForManager(decoratedManager, decoratedFactory); editor->addProperty(undecoratedProperty); editor->addProperty(decoratedProperty); editor->show(); int ret = app.exec(); delete decoratedFactory; delete decoratedManager; delete undecoratedFactory; delete undecoratedManager; delete editor; return ret; } #include "main.moc" ef='#n216'>216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517