tools/designer/src/components/propertyeditor/propertyeditor.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the Qt Designer of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "propertyeditor.h"
00025 #include "findicondialog_p.h"
00026 #include "qpropertyeditor_model_p.h"
00027 #include "qpropertyeditor_items_p.h"
00028 
00029 // sdk
00030 #include <QtDesigner/QtDesigner>
00031 #include <QtDesigner/QExtensionManager>
00032 
00033 // shared
00034 #include <iconloader_p.h>
00035 #include <qdesigner_promotedwidget_p.h>
00036 #include <qdesigner_utils_p.h>
00037 #include <qdesigner_command_p.h>
00038 #include <metadatabase_p.h>
00039 
00040 #include "paletteeditorbutton.h"
00041 #include <QtGui/QtGui>
00042 
00043 #ifndef Q_MOC_RUN
00044 using namespace qdesigner_internal;
00045 #endif
00046 
00047 IProperty *PropertyEditor::createSpecialProperty(const QVariant &value, const QString &name)
00048 {
00049     Q_UNUSED(value);
00050     Q_UNUSED(name);
00051 
00052     return 0;
00053 }
00054 
00055 // ---------------------------------------------------------------------------------
00056 
00057 namespace qdesigner_internal {
00058 
00059 class IconProperty : public AbstractProperty<QIcon>
00060 {
00061 public:
00062     IconProperty(QDesignerFormEditorInterface *core, const QIcon &value, const QString &name);
00063 
00064     void setValue(const QVariant &value);
00065     QString toString() const;
00066     QVariant decoration() const;
00067 
00068     QWidget *createEditor(QWidget *parent, const QObject *target, const char *receiver) const;
00069     void updateEditorContents(QWidget *editor);
00070     void updateValue(QWidget *editor);
00071 private:
00072     QDesignerFormEditorInterface *m_core;
00073 };
00074 
00075 class PixmapProperty : public AbstractProperty<QPixmap>
00076 {
00077 public:
00078     PixmapProperty(QDesignerFormEditorInterface *core, const QPixmap &pixmap, const QString &name);
00079 
00080     void setValue(const QVariant &value);
00081     QString toString() const;
00082     QVariant decoration() const;
00083 
00084     QWidget *createEditor(QWidget *parent, const QObject *target, const char *receiver) const;
00085     void updateEditorContents(QWidget *editor);
00086     void updateValue(QWidget *editor);
00087 private:
00088     QDesignerFormEditorInterface *m_core;
00089 };
00090 
00091 class PaletteProperty : public AbstractProperty<QPalette>
00092 {
00093 public:
00094     PaletteProperty(QDesignerFormEditorInterface *core, const QPalette &value,
00095                 QWidget *selectedWidget, const QString &name);
00096 
00097     void setValue(const QVariant &value);
00098     QString toString() const;
00099 
00100     QWidget *createEditor(QWidget *parent, const QObject *target, const char *receiver) const;
00101     void updateEditorContents(QWidget *editor);
00102     void updateValue(QWidget *editor);
00103 
00104 private:
00105     QDesignerFormEditorInterface *m_core;
00106     QWidget *m_selectedWidget;
00107 };
00108 
00109 // This handles editing of pixmap and icon properties
00110 class GraphicsPropertyEditor : public QWidget
00111 {
00112     Q_OBJECT
00113 public:
00114     GraphicsPropertyEditor(QDesignerFormEditorInterface *core, const QIcon &pm, QWidget *parent);
00115     GraphicsPropertyEditor(QDesignerFormEditorInterface *core, const QPixmap &pixmap, QWidget *parent);
00116     ~GraphicsPropertyEditor();
00117 
00118     void setIcon(const QIcon &pm);
00119     void setPixmap(const QPixmap &pm);
00120     QIcon icon() const { return m_mode == Icon ? m_icon : QIcon(); }
00121     QPixmap pixmap() const { return m_mode == Pixmap ? m_pixmap : QPixmap(); }
00122 
00123 signals:
00124     void iconChanged(const QIcon &pm);
00125     void pixmapChanged(const QPixmap &pm);
00126 
00127 private slots:
00128     void showDialog();
00129     void comboActivated(int idx);
00130 
00131 private:
00132     void init();
00133     void populateCombo();
00134     int indexOfIcon(const QIcon &icon);
00135     int indexOfPixmap(const QPixmap &pixmap);
00136 
00137     enum Mode { Icon, Pixmap };
00138     Mode m_mode;
00139 
00140     QDesignerFormEditorInterface *m_core;
00141     QComboBox *m_combo;
00142     QToolButton *m_button;
00143     QIcon m_icon;
00144     QPixmap m_pixmap;
00145 };
00146 
00147 GraphicsPropertyEditor::~GraphicsPropertyEditor()
00148 {
00149 }
00150 
00151 void GraphicsPropertyEditor::init()
00152 {
00153     QHBoxLayout *layout = new QHBoxLayout(this);
00154     layout->setMargin(0);
00155     layout->setSpacing(0);
00156 
00157     m_combo = new QComboBox(this);
00158     m_combo->setFrame(0);
00159     m_combo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00160     m_combo->setEditable(false);
00161     layout->addWidget(m_combo);
00162     m_button = new QToolButton(this);
00163     m_button->setIcon(createIconSet(QLatin1String("fileopen.png")));
00164     m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding);
00165     m_button->setFixedWidth(20);
00166     layout->addWidget(m_button);
00167     connect(m_button, SIGNAL(clicked()), this, SLOT(showDialog()));
00168     connect(m_combo, SIGNAL(activated(int)), this, SLOT(comboActivated(int)));
00169 
00170     populateCombo();
00171 }
00172 
00173 void GraphicsPropertyEditor::comboActivated(int idx)
00174 {
00175     if (m_mode == Icon) {
00176         setIcon(qvariant_cast<QIcon>(m_combo->itemData(idx)));
00177     } else {
00178         setPixmap(qvariant_cast<QPixmap>(m_combo->itemData(idx)));
00179     }
00180 }
00181 
00182 int GraphicsPropertyEditor::indexOfIcon(const QIcon &icon)
00183 {
00184     if (m_mode == Pixmap)
00185         return -1;
00186 
00187     if (icon.isNull())
00188         return 0;
00189 
00190     for (int i = 1; i < m_combo->count(); ++i) {
00191         if (qvariant_cast<QIcon>(m_combo->itemData(i)).serialNumber() == icon.serialNumber())
00192             return i;
00193     }
00194 
00195     populateCombo();
00196 
00197     for (int i = 1; i < m_combo->count(); ++i) {
00198         if (qvariant_cast<QIcon>(m_combo->itemData(i)).serialNumber() == icon.serialNumber())
00199             return i;
00200     }
00201 
00202     return -1;
00203 }
00204 
00205 int GraphicsPropertyEditor::indexOfPixmap(const QPixmap &pixmap)
00206 {
00207     if (m_mode == Icon)
00208         return -1;
00209 
00210     if (pixmap.isNull())
00211         return 0;
00212 
00213     for (int i = 1; i < m_combo->count(); ++i) {
00214         if (qvariant_cast<QPixmap>(m_combo->itemData(i)).serialNumber() == pixmap.serialNumber())
00215             return i;
00216     }
00217 
00218     populateCombo();
00219 
00220     for (int i = 1; i < m_combo->count(); ++i) {
00221         if (qvariant_cast<QPixmap>(m_combo->itemData(i)).serialNumber() == pixmap.serialNumber())
00222             return i;
00223     }
00224 
00225     return -1;
00226 }
00227 
00228 void GraphicsPropertyEditor::populateCombo()
00229 {
00230     QDesignerFormWindowInterface *form = m_core->formWindowManager()->activeFormWindow();
00231     if (form == 0)
00232         return;
00233     QStringList qrc_list = form->resourceFiles();
00234 
00235     m_combo->clear();
00236 
00237     QDesignerIconCacheInterface *cache = m_core->iconCache();
00238     if (m_mode == Icon) {
00239         m_combo->addItem(tr("<no icon>"));
00240         QList<QIcon> icon_list = cache->iconList();
00241         foreach (QIcon icon, icon_list) {
00242             QString qrc_path = cache->iconToQrcPath(icon);
00243             if (!qrc_path.isEmpty() && !qrc_list.contains(qrc_path))
00244                 continue;
00245             m_combo->addItem(icon, QFileInfo(cache->iconToFilePath(icon)).fileName(),
00246                                 QVariant(icon));
00247         }
00248     } else {
00249         m_combo->addItem(tr("<no pixmap>"));
00250         QList<QPixmap> pixmap_list = cache->pixmapList();
00251         foreach (QPixmap pixmap, pixmap_list) {
00252             QString qrc_path = cache->iconToQrcPath(pixmap);
00253             if (!qrc_path.isEmpty() && !qrc_list.contains(qrc_path))
00254                 continue;
00255             m_combo->addItem(QIcon(pixmap),
00256                                 QFileInfo(cache->pixmapToFilePath(pixmap)).fileName(),
00257                                 QVariant(pixmap));
00258         }
00259     }
00260     bool blocked = m_combo->blockSignals(true);
00261     m_combo->setCurrentIndex(0);
00262     m_combo->blockSignals(blocked);
00263 }
00264 
00265 GraphicsPropertyEditor::GraphicsPropertyEditor(QDesignerFormEditorInterface *core, const QIcon &pm,
00266                                                 QWidget *parent)
00267     : QWidget(parent)
00268 {
00269     m_mode = Icon;
00270     m_core = core;
00271     init();
00272     setIcon(pm);
00273 }
00274 
00275 GraphicsPropertyEditor::GraphicsPropertyEditor(QDesignerFormEditorInterface *core, const QPixmap &pm,
00276                                                 QWidget *parent)
00277     : QWidget(parent)
00278 {
00279     m_mode = Pixmap;
00280     m_core = core;
00281     init();
00282     setPixmap(pm);
00283 }
00284 
00285 void GraphicsPropertyEditor::showDialog()
00286 {
00287     QDesignerFormWindowInterface *form = m_core->formWindowManager()->activeFormWindow();
00288     if (form == 0)
00289         return;
00290 
00291     QString file_path;
00292     QString qrc_path;
00293 
00294     if (m_mode == Icon && !m_icon.isNull()) {
00295         file_path = m_core->iconCache()->iconToFilePath(m_icon);
00296         qrc_path = m_core->iconCache()->iconToQrcPath(m_icon);
00297     } else if (!m_pixmap.isNull()) {
00298         file_path = m_core->iconCache()->pixmapToFilePath(m_pixmap);
00299         qrc_path = m_core->iconCache()->pixmapToQrcPath(m_pixmap);
00300     }
00301 
00302     FindIconDialog dialog(form, 0);
00303     dialog.setPaths(qrc_path, file_path);
00304     if (dialog.exec()) {
00305         file_path = dialog.filePath();
00306         qrc_path = dialog.qrcPath();
00307         if (!file_path.isEmpty()) {
00308             populateCombo();
00309             if (m_mode == Icon) {
00310                 QIcon icon = m_core->iconCache()->nameToIcon(file_path, qrc_path);
00311                 populateCombo();
00312                 setIcon(icon);
00313             } else {
00314                 QPixmap pixmap = m_core->iconCache()->nameToPixmap(file_path, qrc_path);
00315                 populateCombo();
00316                 setPixmap(pixmap);
00317             }
00318         }
00319     }
00320 }
00321 
00322 void GraphicsPropertyEditor::setIcon(const QIcon &pm)
00323 {
00324     if (m_mode == Pixmap)
00325         return;
00326 
00327     if (pm.isNull() && m_icon.isNull())
00328         return;
00329     if (pm.serialNumber() == m_icon.serialNumber())
00330         return;
00331 
00332     m_icon = pm;
00333 
00334     bool blocked = m_combo->blockSignals(true);
00335     m_combo->setCurrentIndex(indexOfIcon(m_icon));
00336     m_combo->blockSignals(blocked);
00337 
00338     emit iconChanged(m_icon);
00339 }
00340 
00341 void GraphicsPropertyEditor::setPixmap(const QPixmap &pm)
00342 {
00343     if (m_mode == Icon)
00344         return;
00345 
00346     if (pm.isNull() && m_pixmap.isNull())
00347         return;
00348     if (pm.serialNumber() == m_pixmap.serialNumber())
00349         return;
00350 
00351     m_pixmap = pm;
00352 
00353     bool blocked = m_combo->blockSignals(true);
00354     m_combo->setCurrentIndex(indexOfPixmap(m_pixmap));
00355     m_combo->blockSignals(blocked);
00356 
00357     emit pixmapChanged(m_pixmap);
00358 }
00359 
00360 }  // namespace qdesigner_internal
00361 
00362 IconProperty::IconProperty(QDesignerFormEditorInterface *core, const QIcon &value, const QString &name)
00363     : AbstractProperty<QIcon>(value, name)
00364 {
00365     m_core = core;
00366 }
00367 
00368 void IconProperty::setValue(const QVariant &value)
00369 {
00370     m_value = qvariant_cast<QIcon>(value);
00371 }
00372 
00373 QString IconProperty::toString() const
00374 {
00375     QString path = m_core->iconCache()->iconToFilePath(m_value);
00376     return QFileInfo(path).fileName();
00377 }
00378 
00379 QVariant IconProperty::decoration() const
00380 {
00381     static QIcon empty_icon;
00382     if (empty_icon.isNull())
00383         empty_icon = QIcon(QLatin1String(":/trolltech/formeditor/images/emptyicon.png"));
00384 
00385     if (m_value.isNull())
00386         return qVariantFromValue(empty_icon);
00387     return qVariantFromValue(m_value);
00388 }
00389 
00390 QWidget *IconProperty::createEditor(QWidget *parent, const QObject *target,
00391                                         const char *receiver) const
00392 {
00393     GraphicsPropertyEditor *editor = new GraphicsPropertyEditor(m_core, m_value, parent);
00394 
00395     QObject::connect(editor, SIGNAL(iconChanged(QIcon)), target, receiver);
00396 
00397     return editor;
00398 }
00399 
00400 void IconProperty::updateEditorContents(QWidget *editor)
00401 {
00402     if (GraphicsPropertyEditor *ed = qobject_cast<GraphicsPropertyEditor*>(editor)) {
00403         ed->setIcon(m_value);
00404     }
00405 }
00406 
00407 void IconProperty::updateValue(QWidget *editor)
00408 {
00409     if (GraphicsPropertyEditor *ed = qobject_cast<GraphicsPropertyEditor*>(editor)) {
00410         QIcon newValue = ed->icon();
00411 
00412         if (newValue.serialNumber() != m_value.serialNumber()) {
00413             m_value = newValue;
00414             setChanged(true);
00415         }
00416     }
00417 }
00418 
00419 PixmapProperty::PixmapProperty(QDesignerFormEditorInterface *core, const QPixmap &pixmap, const QString &name)
00420     : AbstractProperty<QPixmap>(pixmap, name)
00421 {
00422     m_core = core;
00423 }
00424 
00425 void PixmapProperty::setValue(const QVariant &value)
00426 {
00427     m_value = qvariant_cast<QPixmap>(value);
00428 }
00429 
00430 QString PixmapProperty::toString() const
00431 {
00432     QString path = m_core->iconCache()->pixmapToFilePath(m_value);
00433     return QFileInfo(path).fileName();
00434 }
00435 
00436 QVariant PixmapProperty::decoration() const
00437 {
00438     static QIcon empty_icon;
00439     if (empty_icon.isNull())
00440         empty_icon = QIcon(QLatin1String(":/trolltech/formeditor/images/emptyicon.png"));
00441 
00442     if (m_value.isNull())
00443         return qVariantFromValue(empty_icon);
00444     return qVariantFromValue(QIcon(m_value));
00445 }
00446 
00447 QWidget *PixmapProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const
00448 {
00449     GraphicsPropertyEditor *editor = new GraphicsPropertyEditor(m_core, m_value, parent);
00450 
00451     QObject::connect(editor, SIGNAL(pixmapChanged(QPixmap)), target, receiver);
00452 
00453     return editor;
00454 }
00455 
00456 void PixmapProperty::updateEditorContents(QWidget *editor)
00457 {
00458     if (GraphicsPropertyEditor *ed = qobject_cast<GraphicsPropertyEditor*>(editor)) {
00459         ed->setPixmap(m_value);
00460     }
00461 }
00462 
00463 void PixmapProperty::updateValue(QWidget *editor)
00464 {
00465     if (GraphicsPropertyEditor *ed = qobject_cast<GraphicsPropertyEditor*>(editor)) {
00466         QPixmap newValue = ed->pixmap();
00467 
00468         if (newValue.serialNumber() != m_value.serialNumber()) {
00469             m_value = newValue;
00470             setChanged(true);
00471         }
00472     }
00473 }
00474 
00475 // -------------------------------------------------------------------------
00476 PaletteProperty::PaletteProperty(QDesignerFormEditorInterface *core, const QPalette &value, QWidget *selectedWidget,
00477                 const QString &name)
00478     : AbstractProperty<QPalette>(value, name)
00479 {
00480     m_selectedWidget = selectedWidget;
00481     m_core = core;
00482 }
00483 
00484 void PaletteProperty::setValue(const QVariant &value)
00485 {
00486     m_value = qvariant_cast<QPalette>(value);
00487     QPalette parentPalette = QPalette();
00488     if (m_selectedWidget) {
00489         if (m_selectedWidget->isWindow())
00490             parentPalette = QApplication::palette(m_selectedWidget);
00491         else {
00492             if (m_selectedWidget->parentWidget())
00493                 parentPalette = m_selectedWidget->parentWidget()->palette();
00494         }
00495     }
00496     uint mask = m_value.resolve();
00497     m_value = m_value.resolve(parentPalette);
00498     m_value.resolve(mask);
00499 }
00500 
00501 QString PaletteProperty::toString() const
00502 {
00503     return QString(); // ### implement me
00504 }
00505 
00506 QWidget *PaletteProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const
00507 {
00508     PaletteEditorButton *btn = new PaletteEditorButton(m_core, m_value, m_selectedWidget, parent);
00509     QObject::connect(btn, SIGNAL(changed()), target, receiver);
00510     return btn;
00511 }
00512 
00513 void PaletteProperty::updateEditorContents(QWidget *editor)
00514 {
00515     if (PaletteEditorButton *btn = qobject_cast<PaletteEditorButton*>(editor)) {
00516         btn->setPalette(m_value);
00517     }
00518 }
00519 
00520 void PaletteProperty::updateValue(QWidget *editor)
00521 {
00522     if (PaletteEditorButton *btn = qobject_cast<PaletteEditorButton*>(editor)) {
00523         QPalette newValue = btn->palette();
00524 
00525         if (newValue.resolve() != m_value.resolve() || newValue != m_value) {
00526             m_value = newValue;
00527             setChanged(true);
00528         }
00529     }
00530 }
00531 
00532 
00533 // -------------------------------------------------------------------------------------
00534 
00535 struct Group
00536 {
00537     QString name;
00538     QList<IProperty*> properties;
00539 
00540     inline Group() {}
00541     inline Group(const QString &n): name(n) {}
00542 
00543     inline bool operator == (const Group &other) const
00544     { return name == other.name; }
00545 };
00546 
00547 MetaDataBaseItem* PropertyEditor::metaDataBaseItem() const 
00548 {
00549     QObject *o = object();
00550     if (!o) 
00551         return 0;
00552     if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(o))
00553         o = promoted->child();
00554 
00555     MetaDataBase* db = qobject_cast<MetaDataBase*>(core()->metaDataBase());
00556     if (!db) return 0;
00557     return static_cast<MetaDataBaseItem*>(db->item(o));
00558 }
00559 
00560 void PropertyEditor::createPropertySheet(PropertyCollection *root, QObject *object)
00561 {
00562     QList<Group> groups;
00563 
00564     QExtensionManager *m = m_core->extensionManager();
00565     QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*> (m, m_core);
00566 
00567     bool isMainContainer = false;
00568     if (QWidget *widget = qobject_cast<QWidget*>(object)) {
00569         if (QDesignerFormWindowInterface *fw = QDesignerFormWindowInterface::findFormWindow(widget)) {
00570             isMainContainer = (fw->mainContainer() == widget);
00571         }
00572     }
00573     m_prop_sheet = qobject_cast<QDesignerPropertySheetExtension*>(m->extension(object, Q_TYPEID(QDesignerPropertySheetExtension)));
00574     for (int i=0; i<m_prop_sheet->count(); ++i) {
00575         if (!m_prop_sheet->isVisible(i))
00576             continue;
00577 
00578         QString pname = m_prop_sheet->propertyName(i);
00579         QVariant value = m_prop_sheet->property(i);
00580 
00581         IProperty *p = 0;
00582         if (qVariantCanConvert<FlagType>(value)) {
00583             FlagType f = qvariant_cast<FlagType>(value);
00584 
00585             if (pname == QLatin1String("alignment")) {
00586                 // ### fixme!!!
00587                 if (qobject_cast<QLineEdit *>(object)) {
00588                     QStringList align_keys = QStringList()
00589                                              << QString::fromUtf8("Qt::AlignLeft")
00590                                              << QString::fromUtf8("Qt::AlignHCenter")
00591                                              << QString::fromUtf8("Qt::AlignRight");
00592                     QMap<QString, QVariant> align_map;
00593                     foreach (QString align, align_keys) {
00594                         align_map.insert(align, f.items.value(align));
00595                     }
00596                     p = new MapProperty(align_map, uint(f.value.toInt() & Qt::AlignHorizontal_Mask),
00597                                         QLatin1String("alignment"));
00598                 } else {
00599                     p = new AlignmentProperty(f.items, Qt::Alignment(f.value.toInt()), pname);
00600                 }
00601             } else {
00602                 if (lang) {
00603                     QMap<QString, QVariant> items;
00604                     QMapIterator<QString, QVariant> it (f.items);
00605                     while (it.hasNext()) {
00606                         it.next();
00607                         QString id = lang->enumerator(it.key());
00608                         items.insert(id, it.value());
00609                     }
00610                     f.items = items;
00611                 }
00612 
00613                 p = new FlagsProperty(f.items, f.value.toInt(), pname);
00614             }
00615         } else if (qVariantCanConvert<EnumType>(value)) {
00616             EnumType e = qvariant_cast<EnumType>(value);
00617 
00618             if (lang) {
00619                 QMap<QString, QVariant> items;
00620                 QMapIterator<QString, QVariant> it (e.items);
00621                 e.names.clear();
00622                 while (it.hasNext()) {
00623                     it.next();
00624                     QString id = lang->enumerator(it.key());
00625                     items.insert(id, it.value());
00626                     e.names.append(id);
00627                 }
00628                 e.items = items;
00629             }
00630 
00631             p = new MapProperty(e.items, e.value, pname, e.names);
00632         }
00633 
00634         if (!p) {
00635             switch (value.type()) {
00636             case 0:
00637                 p = createSpecialProperty(value, pname);
00638                 break;
00639             case QVariant::Int:
00640                 p = new IntProperty(value.toInt(), pname);
00641                 break;
00642             case QVariant::UInt:
00643                 p = new IntProperty(value.toUInt(), pname);
00644                 break;
00645             case QVariant::LongLong:
00646                 p = new LongLongProperty(value.toLongLong(), pname);
00647                 break;
00648             case QVariant::ULongLong:
00649                 p = new LongLongProperty(value.toULongLong(), pname);
00650                 break;
00651             case QVariant::Double:
00652                 p = new DoubleProperty(value.toDouble(), pname);
00653                 break;
00654             case QVariant::Char:
00655                 p = new CharProperty(value.toChar(), pname);
00656                 break;
00657             case QVariant::Bool:
00658                 p = new BoolProperty(value.toBool(), pname);
00659                 break;
00660             case QVariant::ByteArray:
00661                 p = new StringProperty(QString::fromUtf8(value.toByteArray()), pname);
00662                 break;
00663             case QVariant::String: {
00664                 MetaDataBaseItem *item = metaDataBaseItem();
00665                 if (item && pname != QLatin1String("objectName")) {
00666                     p = new StringProperty(value.toString(), pname, true, item->propertyComment(pname));
00667                 } else {
00668                     StringProperty *sprop = new StringProperty(value.toString(), pname);
00669                     p = sprop;
00670 
00671                     if (pname == QLatin1String("objectName")) {
00672                         sprop->setCheckValidObjectName(true);
00673                         sprop->setAllowScope(isMainContainer);
00674                     }
00675                 }
00676             } break;
00677             case QVariant::Size:
00678                 p = new SizeProperty(value.toSize(), pname);
00679                 break;
00680             case QVariant::SizeF:
00681                 p = new SizeFProperty(value.toSizeF(), pname);
00682                 break;
00683             case QVariant::Point:
00684                 p = new PointProperty(value.toPoint(), pname);
00685                 break;
00686             case QVariant::PointF:
00687                 p = new PointFProperty(value.toPointF(), pname);
00688                 break;
00689             case QVariant::Rect:
00690                 p = new RectProperty(value.toRect(), pname);
00691                 break;
00692             case QVariant::RectF:
00693                 p = new RectFProperty(value.toRectF(), pname);
00694                 break;
00695             case QVariant::Icon:
00696                 p = new IconProperty(m_core, qvariant_cast<QIcon>(value), pname);
00697                 break;
00698             case QVariant::Pixmap:
00699                 p = new PixmapProperty(m_core, qvariant_cast<QPixmap>(value), pname);
00700                 break;
00701             case QVariant::Font:
00702                 p = new FontProperty(qvariant_cast<QFont>(value), pname, qobject_cast<QWidget *>(object));
00703                 break;
00704             case QVariant::Color:
00705                 p = new ColorProperty(qvariant_cast<QColor>(value), pname);
00706                 break;
00707             case QVariant::SizePolicy:
00708                 p = new SizePolicyProperty(qvariant_cast<QSizePolicy>(value), pname);
00709                 break;
00710             case QVariant::DateTime:
00711                 p = new DateTimeProperty(value.toDateTime(), pname);
00712                 break;
00713             case QVariant::Date:
00714                 p = new DateProperty(value.toDate(), pname);
00715                 break;
00716             case QVariant::Time:
00717                 p = new TimeProperty(value.toTime(), pname);
00718                 break;
00719             case QVariant::Cursor:
00720                 p = new CursorProperty(qvariant_cast<QCursor>(value), pname);
00721                 break;
00722             case QVariant::KeySequence:
00723                 p = new StringProperty(qvariant_cast<QKeySequence>(value), pname);
00724                 break;
00725             case QVariant::Palette:
00726                 p = new PaletteProperty(m_core, qvariant_cast<QPalette>(value),
00727                                 qobject_cast<QWidget *>(object), pname);
00728                 break;
00729             case QVariant::Url:
00730                 p = new UrlProperty(value.toUrl(), pname);
00731                 break;
00732             case QVariant::StringList:
00733                 p = new StringListProperty(qvariant_cast<QStringList>(value), pname);
00734                 break;
00735             default:
00736                 // ### qWarning() << "property" << pname << "with type" << value.type() << "not supported yet!";
00737                 break;
00738             } // end switch
00739         }
00740 
00741         if (p != 0) {
00742             p->setHasReset(m_prop_sheet->hasReset(i));
00743             p->setChanged(m_prop_sheet->isChanged(i));
00744             p->setDirty(false);
00745 
00746             QString pgroup = m_prop_sheet->propertyGroup(i);
00747             int groupIndex = groups.indexOf(pgroup);
00748             if (groupIndex == -1) {
00749                 groupIndex = groups.count();
00750                 groups.append(Group(pgroup));
00751             }
00752 
00753             QList<IProperty*> &groupProperties = groups[groupIndex].properties;
00754             groupProperties.append(p);
00755         }
00756     }
00757 
00758     foreach (Group g, groups) {
00759         root->addProperty(new SeparatorProperty(QString(), g.name));
00760         foreach (IProperty *p, g.properties) {
00761             root->addProperty(p);
00762         }
00763     }
00764 }
00765 
00766 PropertyEditor::PropertyEditor(QDesignerFormEditorInterface *core,
00767             QWidget *parent, Qt::WindowFlags flags)
00768     : QDesignerPropertyEditorInterface(parent, flags),
00769       m_core(core),
00770       m_properties(0)
00771 {
00772     QVBoxLayout *lay = new QVBoxLayout(this);
00773     lay->setMargin(0);
00774     m_editor = new QPropertyEditor(this);
00775     lay->addWidget(m_editor);
00776     m_prop_sheet = 0;
00777 
00778     connect(m_editor, SIGNAL(propertyChanged(IProperty*)),
00779         this, SLOT(firePropertyChanged(IProperty*)));
00780     connect(m_editor->editorModel(), SIGNAL(resetProperty(QString)),
00781                 this, SLOT(resetProperty(QString)));
00782 }
00783 
00784 PropertyEditor::~PropertyEditor()
00785 {
00786 }
00787 
00788 bool PropertyEditor::isReadOnly() const
00789 {
00790     return m_editor->isReadOnly();
00791 }
00792 
00793 void PropertyEditor::setReadOnly(bool readOnly)
00794 {
00795     m_editor->setReadOnly(readOnly);
00796 }
00797 
00798 QDesignerFormEditorInterface *PropertyEditor::core() const
00799 {
00800     return m_core;
00801 }
00802 
00803 IProperty *PropertyEditor::propertyByName(IProperty *p, const QString &name)
00804 {
00805     if (p->propertyName() == name)
00806         return p;
00807 
00808     if (p->kind() == IProperty::Property_Group) {
00809         IPropertyGroup *g = static_cast<IPropertyGroup*>(p);
00810         for (int i=0; i<g->propertyCount(); ++i)
00811             if (IProperty *c = propertyByName(g->propertyAt(i), name))
00812                 return c;
00813     }
00814 
00815     return 0;
00816 }
00817 
00818 void PropertyEditor::setPropertyValue(const QString &name, const QVariant &value, bool changed)
00819 {
00820     if (isReadOnly())
00821         return;
00822 
00823     if (IProperty *p = propertyByName(m_editor->initialInput(), name)) {
00824         if (p->value() != value)
00825             p->setValue(value);
00826 
00827         p->setChanged(changed);
00828         p->setDirty(false);
00829 
00830         m_editor->editorModel()->refresh(p);
00831     }
00832 }
00833 
00834 void PropertyEditor::firePropertyChanged(IProperty *p)
00835 {
00836     if (isReadOnly())
00837         return;
00838 
00839     if (object()) {     
00840         if (p->parent() && p->propertyName() == QLatin1String("comment")) {
00841             QString parentProperty = p->parent()->propertyName();
00842             if (MetaDataBaseItem *item = metaDataBaseItem()) {
00843                 item->setPropertyComment(parentProperty, p->value().toString());
00844                 emit propertyChanged(parentProperty, p->parent()->value());
00845             }
00846             return;
00847         }
00848     }
00849     emit propertyChanged(p->propertyName(), p->value());
00850 }
00851 
00852 void PropertyEditor::clearDirty(IProperty *p)
00853 {
00854     p->setDirty(false);
00855 
00856     if (p->kind() == IProperty::Property_Normal)
00857         return;
00858 
00859     IPropertyGroup *g = static_cast<IPropertyGroup*>(p);
00860     for (int i=0; i<g->propertyCount(); ++i)
00861         clearDirty(g->propertyAt(i));
00862 }
00863 
00864 void PropertyEditor::setObject(QObject *object)
00865 {
00866     if (m_editor->initialInput())
00867         clearDirty(m_editor->initialInput());
00868 
00869     m_object = object;
00870     if (QAction *action = qobject_cast<QAction*>(m_object)) {
00871         if (action->menu())
00872             m_object = action->menu();
00873     }
00874 
00875     IPropertyGroup *old_properties = m_properties;
00876     m_properties = 0;
00877     m_prop_sheet = 0;
00878 
00879     if (m_object) {
00880         PropertyCollection *collection = new PropertyCollection(QLatin1String("<root>"));
00881         createPropertySheet(collection, object);
00882         m_properties = collection;
00883     }
00884 
00885     m_editor->setInitialInput(m_properties);
00886 
00887     delete old_properties;
00888 }
00889 
00890 void PropertyEditor::resetProperty(const QString &prop_name)
00891 {
00892     int idx = m_prop_sheet->indexOf(prop_name);
00893 
00894     if (idx == -1) {
00895         qWarning("PropertyEditor::resetProperty(): no property \"%s\"",
00896                     prop_name.toUtf8().constData());
00897         return;
00898     }
00899 
00900     QDesignerFormWindowInterface *form = m_core->formWindowManager()->activeFormWindow();
00901     if (form == 0) {
00902         qWarning("PropertyEditor::resetProperty(): widget does not belong to any form");
00903         return;
00904     }
00905 
00906     ResetPropertyCommand *cmd = new ResetPropertyCommand(form);
00907     cmd->init(m_object, prop_name);
00908     form->commandHistory()->push(cmd);
00909 }
00910 
00911 QString PropertyEditor::currentPropertyName() const
00912 {
00913     QModelIndex index = m_editor->selectionModel()->currentIndex();
00914     if (index.isValid()) {
00915         IProperty *property = static_cast<IProperty*>(index.internalPointer());
00916 
00917         while (property && property->isFake())
00918             property = property->parent();
00919 
00920         if (property)
00921             return property->propertyName();
00922     }
00923 
00924     return QString();
00925 }
00926 
00927 #include "propertyeditor.moc"

Generated on Thu Mar 15 12:01:08 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1