tools/designer/src/lib/shared/qdesigner_utils.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Designer of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qdesigner_utils_p.h"
       
    43 #include "qdesigner_propertycommand_p.h"
       
    44 #include "abstractformbuilder.h"
       
    45 #include "formwindowbase_p.h"
       
    46 
       
    47 #include <QtDesigner/QDesignerFormEditorInterface>
       
    48 #include <QtDesigner/QDesignerFormWindowInterface>
       
    49 #include <QtDesigner/QDesignerIconCacheInterface>
       
    50 #include <QtDesigner/QDesignerResourceBrowserInterface>
       
    51 #include <QtDesigner/QDesignerLanguageExtension>
       
    52 #include <QtDesigner/QDesignerTaskMenuExtension>
       
    53 #include <QtDesigner/QExtensionManager>
       
    54 
       
    55 #include <QtGui/QIcon>
       
    56 #include <QtGui/QPixmap>
       
    57 #include <QtCore/QDir>
       
    58 
       
    59 #include <QtGui/QApplication>
       
    60 #include <QtCore/QProcess>
       
    61 #include <QtCore/QLibraryInfo>
       
    62 #include <QtCore/QDebug>
       
    63 #include <QtCore/QQueue>
       
    64 #include <QtGui/QListWidget>
       
    65 #include <QtGui/QTreeWidget>
       
    66 #include <QtGui/QTableWidget>
       
    67 #include <QtGui/QComboBox>
       
    68 
       
    69 QT_BEGIN_NAMESPACE
       
    70 
       
    71 namespace qdesigner_internal
       
    72 {
       
    73     QDESIGNER_SHARED_EXPORT void designerWarning(const QString &message)
       
    74     {
       
    75         qWarning("Designer: %s", qPrintable(message));
       
    76     }
       
    77 
       
    78     void reloadTreeItem(DesignerIconCache *iconCache, QTreeWidgetItem *item)
       
    79     {
       
    80         if (!item)
       
    81             return;
       
    82 
       
    83         for (int c = 0; c < item->columnCount(); c++) {
       
    84             const QVariant v = item->data(c, Qt::DecorationPropertyRole);
       
    85             if (qVariantCanConvert<PropertySheetIconValue>(v))
       
    86                 item->setIcon(c, iconCache->icon(qVariantValue<PropertySheetIconValue>(v)));
       
    87         }
       
    88     }
       
    89 
       
    90     void reloadListItem(DesignerIconCache *iconCache, QListWidgetItem *item)
       
    91     {
       
    92         if (!item)
       
    93             return;
       
    94 
       
    95         const QVariant v = item->data(Qt::DecorationPropertyRole);
       
    96         if (qVariantCanConvert<PropertySheetIconValue>(v))
       
    97             item->setIcon(iconCache->icon(qVariantValue<PropertySheetIconValue>(v)));
       
    98     }
       
    99 
       
   100     void reloadTableItem(DesignerIconCache *iconCache, QTableWidgetItem *item)
       
   101     {
       
   102         if (!item)
       
   103             return;
       
   104 
       
   105         const QVariant v = item->data(Qt::DecorationPropertyRole);
       
   106         if (qVariantCanConvert<PropertySheetIconValue>(v))
       
   107             item->setIcon(iconCache->icon(qVariantValue<PropertySheetIconValue>(v)));
       
   108     }
       
   109 
       
   110     void reloadIconResources(DesignerIconCache *iconCache, QObject *object)
       
   111     {
       
   112         if (QListWidget *listWidget = qobject_cast<QListWidget *>(object)) {
       
   113             for (int i = 0; i < listWidget->count(); i++)
       
   114                 reloadListItem(iconCache, listWidget->item(i));
       
   115         } else if (QComboBox *comboBox = qobject_cast<QComboBox *>(object)) {
       
   116             for (int i = 0; i < comboBox->count(); i++) {
       
   117                 const QVariant v = comboBox->itemData(i, Qt::DecorationPropertyRole);
       
   118                 if (qVariantCanConvert<PropertySheetIconValue>(v)) {
       
   119                     QIcon icon = iconCache->icon(qVariantValue<PropertySheetIconValue>(v));
       
   120                     comboBox->setItemIcon(i, icon);
       
   121                     comboBox->setItemData(i, icon);
       
   122                 }
       
   123             }
       
   124         } else if (QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>(object)) {
       
   125             reloadTreeItem(iconCache, treeWidget->headerItem());
       
   126             QQueue<QTreeWidgetItem *> itemsQueue;
       
   127             for (int i = 0; i < treeWidget->topLevelItemCount(); i++)
       
   128                 itemsQueue.enqueue(treeWidget->topLevelItem(i));
       
   129             while (!itemsQueue.isEmpty()) {
       
   130                 QTreeWidgetItem *item = itemsQueue.dequeue();
       
   131                 for (int i = 0; i < item->childCount(); i++)
       
   132                     itemsQueue.enqueue(item->child(i));
       
   133                 reloadTreeItem(iconCache, item);
       
   134             }
       
   135         } else if (QTableWidget *tableWidget = qobject_cast<QTableWidget *>(object)) {
       
   136             const int columnCount = tableWidget->columnCount();
       
   137             const int rowCount = tableWidget->rowCount();
       
   138             for (int c = 0; c < columnCount; c++)
       
   139                 reloadTableItem(iconCache, tableWidget->horizontalHeaderItem(c));
       
   140             for (int r = 0; r < rowCount; r++)
       
   141                 reloadTableItem(iconCache, tableWidget->verticalHeaderItem(r));
       
   142             for (int c = 0; c < columnCount; c++)
       
   143                 for (int r = 0; r < rowCount; r++)
       
   144                     reloadTableItem(iconCache, tableWidget->item(r, c));
       
   145         }
       
   146     }
       
   147 
       
   148     // ------------- DesignerMetaEnum
       
   149     DesignerMetaEnum::DesignerMetaEnum(const QString &name, const QString &scope, const QString &separator) :
       
   150         MetaEnum<int>(name, scope, separator)
       
   151     {
       
   152     }
       
   153 
       
   154 
       
   155     QString DesignerMetaEnum::toString(int value, SerializationMode sm, bool *ok) const
       
   156     {
       
   157         // find value
       
   158         bool valueOk;
       
   159         const QString item = valueToKey(value, &valueOk);
       
   160         if (ok)
       
   161             *ok = valueOk;
       
   162 
       
   163         if (!valueOk || sm == NameOnly)
       
   164             return item;
       
   165 
       
   166         QString qualifiedItem;
       
   167         appendQualifiedName(item,  qualifiedItem);
       
   168         return qualifiedItem;
       
   169     }
       
   170 
       
   171     QString DesignerMetaEnum::messageToStringFailed(int value) const
       
   172     {
       
   173         return QCoreApplication::translate("DesignerMetaEnum", "%1 is not a valid enumeration value of '%2'.").arg(value).arg(name());
       
   174     }
       
   175 
       
   176     QString DesignerMetaEnum::messageParseFailed(const QString &s) const
       
   177     {
       
   178         return QCoreApplication::translate("DesignerMetaEnum", "'%1' could not be converted to an enumeration value of type '%2'.").arg(s).arg(name());
       
   179     }
       
   180     // -------------- DesignerMetaFlags
       
   181     DesignerMetaFlags::DesignerMetaFlags(const QString &name, const QString &scope, const QString &separator) :
       
   182        MetaEnum<uint>(name, scope, separator)
       
   183     {
       
   184     }
       
   185 
       
   186     QStringList DesignerMetaFlags::flags(int ivalue) const
       
   187     {
       
   188         typedef MetaEnum<uint>::KeyToValueMap::const_iterator KeyToValueMapIterator;
       
   189         QStringList rc;
       
   190         const uint v = static_cast<uint>(ivalue);
       
   191         const KeyToValueMapIterator cend = keyToValueMap().constEnd();
       
   192         for (KeyToValueMapIterator it = keyToValueMap().constBegin();it != cend; ++it )  {
       
   193             const uint itemValue = it.value();
       
   194             // Check for equality first as flag values can be 0 or -1, too. Takes preference over a bitwise flag
       
   195             if (v == itemValue) {
       
   196                 rc.clear();
       
   197                 rc.push_back(it.key());
       
   198                 return rc;
       
   199             }
       
   200             // Do not add 0-flags (None-flags)
       
   201             if (itemValue)
       
   202                 if ((v & itemValue) == itemValue)
       
   203                     rc.push_back(it.key());
       
   204         }
       
   205         return rc;
       
   206     }
       
   207 
       
   208 
       
   209     QString DesignerMetaFlags::toString(int value, SerializationMode sm) const
       
   210     {
       
   211         const QStringList flagIds = flags(value);
       
   212         if (flagIds.empty())
       
   213             return QString();
       
   214 
       
   215         const QChar delimiter = QLatin1Char('|');
       
   216         QString rc;
       
   217         const QStringList::const_iterator cend = flagIds.constEnd();
       
   218         for (QStringList::const_iterator it = flagIds.constBegin(); it != cend; ++it) {
       
   219             if (!rc.isEmpty())
       
   220                 rc += delimiter ;
       
   221             if (sm == FullyQualified)
       
   222                 appendQualifiedName(*it, rc);
       
   223             else
       
   224                 rc += *it;
       
   225         }
       
   226         return rc;
       
   227     }
       
   228 
       
   229 
       
   230     int DesignerMetaFlags::parseFlags(const QString &s, bool *ok) const
       
   231     {
       
   232         if (s.isEmpty()) {
       
   233             if (ok)
       
   234                 *ok = true;
       
   235             return 0;
       
   236         }
       
   237         uint flags = 0;
       
   238         bool valueOk = true;
       
   239         QStringList keys = s.split(QString(QLatin1Char('|')));
       
   240         const QStringList::iterator cend = keys.end();
       
   241         for (QStringList::iterator it = keys.begin(); it != cend; ++it) {
       
   242             const uint flagValue = keyToValue(*it, &valueOk);
       
   243             if (!valueOk) {
       
   244                 flags = 0;
       
   245                 break;
       
   246             }
       
   247             flags |= flagValue;
       
   248         }
       
   249         if (ok)
       
   250             *ok = valueOk;
       
   251         return static_cast<int>(flags);
       
   252     }
       
   253 
       
   254     QString DesignerMetaFlags::messageParseFailed(const QString &s) const
       
   255     {
       
   256         return QCoreApplication::translate("DesignerMetaFlags", "'%1' could not be converted to a flag value of type '%2'.").arg(s).arg(name());
       
   257     }
       
   258 
       
   259     // ---------- PropertySheetEnumValue
       
   260 
       
   261     PropertySheetEnumValue::PropertySheetEnumValue(int v, const DesignerMetaEnum &me) :
       
   262        value(v),
       
   263        metaEnum(me)
       
   264     {
       
   265     }
       
   266     PropertySheetEnumValue::PropertySheetEnumValue() :
       
   267        value(0)
       
   268     {
       
   269     }
       
   270 
       
   271     // ---------------- PropertySheetFlagValue
       
   272     PropertySheetFlagValue::PropertySheetFlagValue(int v, const DesignerMetaFlags &mf) :
       
   273         value(v),
       
   274         metaFlags(mf)
       
   275     {
       
   276     }
       
   277 
       
   278     PropertySheetFlagValue::PropertySheetFlagValue() :
       
   279         value(0)
       
   280     {
       
   281     }
       
   282 
       
   283     // ---------------- PropertySheetPixmapValue
       
   284     PropertySheetPixmapValue::PropertySheetPixmapValue(const QString &path) : m_path(path)
       
   285     {
       
   286     }
       
   287 
       
   288     PropertySheetPixmapValue::PropertySheetPixmapValue()
       
   289     {
       
   290     }
       
   291 
       
   292     PropertySheetPixmapValue::PixmapSource PropertySheetPixmapValue::getPixmapSource(QDesignerFormEditorInterface *core, const QString & path)
       
   293     {
       
   294         if (const QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension *>(core->extensionManager(), core))
       
   295             return lang->isLanguageResource(path) ?  LanguageResourcePixmap : FilePixmap;
       
   296         return path.startsWith(QLatin1Char(':')) ? ResourcePixmap : FilePixmap;
       
   297     }
       
   298 
       
   299     int PropertySheetPixmapValue::compare(const PropertySheetPixmapValue &other) const
       
   300     {
       
   301         return m_path.compare(other.m_path);
       
   302     }
       
   303 
       
   304     QString PropertySheetPixmapValue::path() const
       
   305     {
       
   306         return m_path;
       
   307     }
       
   308 
       
   309     void PropertySheetPixmapValue::setPath(const QString &path)
       
   310     {
       
   311         if (m_path == path)
       
   312             return;
       
   313         m_path = path;
       
   314     }
       
   315 
       
   316     // ---------- PropertySheetIconValue
       
   317     PropertySheetIconValue::PropertySheetIconValue(const PropertySheetPixmapValue &pixmap)
       
   318     {
       
   319         setPixmap(QIcon::Normal, QIcon::Off, pixmap);
       
   320     }
       
   321 
       
   322     PropertySheetIconValue::PropertySheetIconValue()
       
   323     {
       
   324     }
       
   325 
       
   326     bool PropertySheetIconValue::equals(const PropertySheetIconValue &rhs) const
       
   327     {
       
   328         return m_paths == rhs.m_paths;
       
   329     }
       
   330 
       
   331     bool PropertySheetIconValue::operator<(const PropertySheetIconValue &other) const
       
   332     {
       
   333         QMapIterator<ModeStateKey, PropertySheetPixmapValue> itThis(m_paths);
       
   334         QMapIterator<ModeStateKey, PropertySheetPixmapValue> itOther(other.m_paths);
       
   335         while (itThis.hasNext() && itOther.hasNext()) {
       
   336             const ModeStateKey thisPair = itThis.next().key();
       
   337             const ModeStateKey otherPair = itOther.next().key();
       
   338             if (thisPair < otherPair)
       
   339                 return true;
       
   340             else if (otherPair < thisPair)
       
   341                 return false;
       
   342             const int crc = itThis.value().compare(itOther.value());
       
   343             if (crc < 0)
       
   344                 return true;
       
   345             if (crc > 0)
       
   346                 return false;
       
   347         }
       
   348         if (itOther.hasNext())
       
   349             return true;
       
   350         return false;
       
   351     }
       
   352 
       
   353     PropertySheetPixmapValue PropertySheetIconValue::pixmap(QIcon::Mode mode, QIcon::State state) const
       
   354     {
       
   355         const ModeStateKey pair = qMakePair(mode, state);
       
   356         return m_paths.value(pair);
       
   357     }
       
   358 
       
   359     void PropertySheetIconValue::setPixmap(QIcon::Mode mode, QIcon::State state, const PropertySheetPixmapValue &pixmap)
       
   360     {
       
   361         const ModeStateKey pair = qMakePair(mode, state);
       
   362         if (pixmap.path().isEmpty())
       
   363             m_paths.remove(pair);
       
   364         else
       
   365             m_paths.insert(pair, pixmap);
       
   366     }
       
   367 
       
   368     QPixmap DesignerPixmapCache::pixmap(const PropertySheetPixmapValue &value) const
       
   369     {
       
   370         QMap<PropertySheetPixmapValue, QPixmap>::const_iterator it = m_cache.constFind(value);
       
   371         if (it != m_cache.constEnd())
       
   372             return it.value();
       
   373 
       
   374         QPixmap pix = QPixmap(value.path());
       
   375         m_cache.insert(value, pix);
       
   376         return pix;
       
   377     }
       
   378 
       
   379     void DesignerPixmapCache::clear()
       
   380     {
       
   381         m_cache.clear();
       
   382     }
       
   383 
       
   384     DesignerPixmapCache::DesignerPixmapCache(QObject *parent)
       
   385         : QObject(parent)
       
   386     {
       
   387     }
       
   388 
       
   389     QIcon DesignerIconCache::icon(const PropertySheetIconValue &value) const
       
   390     {
       
   391         QMap<PropertySheetIconValue, QIcon>::const_iterator it = m_cache.constFind(value);
       
   392         if (it != m_cache.constEnd())
       
   393             return it.value();
       
   394 
       
   395         QIcon icon;
       
   396         QMap<QPair<QIcon::Mode, QIcon::State>, PropertySheetPixmapValue> paths = value.paths();
       
   397         QMapIterator<QPair<QIcon::Mode, QIcon::State>, PropertySheetPixmapValue> itPath(paths);
       
   398         while (itPath.hasNext()) {
       
   399             QPair<QIcon::Mode, QIcon::State> pair = itPath.next().key();
       
   400             icon.addFile(itPath.value().path(), QSize(), pair.first, pair.second);
       
   401         }
       
   402         m_cache.insert(value, icon);
       
   403         return icon;
       
   404     }
       
   405 
       
   406     void DesignerIconCache::clear()
       
   407     {
       
   408         m_cache.clear();
       
   409     }
       
   410 
       
   411     DesignerIconCache::DesignerIconCache(DesignerPixmapCache *pixmapCache, QObject *parent)
       
   412         : QObject(parent),
       
   413         m_pixmapCache(pixmapCache)
       
   414     {
       
   415 
       
   416     }
       
   417 
       
   418     PropertySheetStringValue::PropertySheetStringValue(const QString &value,
       
   419                     bool translatable, const QString &disambiguation, const QString &comment)
       
   420         : m_value(value), m_translatable(translatable), m_disambiguation(disambiguation), m_comment(comment)
       
   421     {  }
       
   422 
       
   423     QString PropertySheetStringValue::value() const
       
   424     {
       
   425         return m_value;
       
   426     }
       
   427 
       
   428     void PropertySheetStringValue::setValue(const QString &value)
       
   429     {
       
   430         m_value = value;
       
   431     }
       
   432 
       
   433     bool PropertySheetStringValue::translatable() const
       
   434     {
       
   435         return m_translatable;
       
   436     }
       
   437 
       
   438     void PropertySheetStringValue::setTranslatable(bool translatable)
       
   439     {
       
   440         m_translatable = translatable;
       
   441     }
       
   442 
       
   443     QString PropertySheetStringValue::disambiguation() const
       
   444     {
       
   445         return m_disambiguation;
       
   446     }
       
   447 
       
   448     void PropertySheetStringValue::setDisambiguation(const QString &disambiguation)
       
   449     {
       
   450         m_disambiguation = disambiguation;
       
   451     }
       
   452 
       
   453     QString PropertySheetStringValue::comment() const
       
   454     {
       
   455         return m_comment;
       
   456     }
       
   457 
       
   458     void PropertySheetStringValue::setComment(const QString &comment)
       
   459     {
       
   460         m_comment = comment;
       
   461     }
       
   462 
       
   463     bool PropertySheetStringValue::equals(const PropertySheetStringValue &rhs) const
       
   464     {
       
   465         return (m_value == rhs.m_value) && (m_translatable == rhs.m_translatable)
       
   466             && (m_disambiguation == rhs.m_disambiguation) && (m_comment == rhs.m_comment);
       
   467     }
       
   468 
       
   469     PropertySheetKeySequenceValue::PropertySheetKeySequenceValue(const QKeySequence &value,
       
   470                     bool translatable, const QString &disambiguation, const QString &comment)
       
   471         : m_value(value),
       
   472           m_standardKey(QKeySequence::UnknownKey),
       
   473           m_translatable(translatable),
       
   474           m_disambiguation(disambiguation),
       
   475           m_comment(comment)
       
   476     {  }
       
   477 
       
   478     PropertySheetKeySequenceValue::PropertySheetKeySequenceValue(const QKeySequence::StandardKey &standardKey,
       
   479                     bool translatable, const QString &disambiguation, const QString &comment)
       
   480         : m_value(QKeySequence(standardKey)),
       
   481           m_standardKey(standardKey),
       
   482           m_translatable(translatable),
       
   483           m_disambiguation(disambiguation),
       
   484           m_comment(comment)
       
   485     {  }
       
   486 
       
   487     QKeySequence PropertySheetKeySequenceValue::value() const
       
   488     {
       
   489         return m_value;
       
   490     }
       
   491 
       
   492     void PropertySheetKeySequenceValue::setValue(const QKeySequence &value)
       
   493     {
       
   494         m_value = value;
       
   495         m_standardKey = QKeySequence::UnknownKey;
       
   496     }
       
   497 
       
   498     QKeySequence::StandardKey PropertySheetKeySequenceValue::standardKey() const
       
   499     {
       
   500         return m_standardKey;
       
   501     }
       
   502 
       
   503     void PropertySheetKeySequenceValue::setStandardKey(const QKeySequence::StandardKey &standardKey)
       
   504     {
       
   505         m_value = QKeySequence(standardKey);
       
   506         m_standardKey = standardKey;
       
   507     }
       
   508 
       
   509     bool PropertySheetKeySequenceValue::isStandardKey() const
       
   510     {
       
   511         return m_standardKey != QKeySequence::UnknownKey;
       
   512     }
       
   513 
       
   514     QString PropertySheetKeySequenceValue::comment() const
       
   515     {
       
   516         return m_comment;
       
   517     }
       
   518 
       
   519     void PropertySheetKeySequenceValue::setComment(const QString &comment)
       
   520     {
       
   521         m_comment = comment;
       
   522     }
       
   523 
       
   524     QString PropertySheetKeySequenceValue::disambiguation() const
       
   525     {
       
   526         return m_disambiguation;
       
   527     }
       
   528 
       
   529     void PropertySheetKeySequenceValue::setDisambiguation(const QString &disambiguation)
       
   530     {
       
   531         m_disambiguation = disambiguation;
       
   532     }
       
   533 
       
   534     bool PropertySheetKeySequenceValue::translatable() const
       
   535     {
       
   536         return m_translatable;
       
   537     }
       
   538 
       
   539     void PropertySheetKeySequenceValue::setTranslatable(bool translatable)
       
   540     {
       
   541         m_translatable = translatable;
       
   542     }
       
   543 
       
   544     bool PropertySheetKeySequenceValue::equals(const PropertySheetKeySequenceValue &rhs) const
       
   545     {
       
   546         return (m_value == rhs.m_value) && (m_standardKey == rhs.m_standardKey)
       
   547             && (m_translatable == rhs.m_translatable) && (m_disambiguation == rhs.m_disambiguation) && (m_comment == rhs.m_comment);
       
   548     }
       
   549 
       
   550     class StateMap
       
   551     {
       
   552     public:
       
   553         StateMap()
       
   554         {
       
   555             m_stateToFlag.insert(qMakePair(QIcon::Normal, QIcon::Off), 0x01);
       
   556             m_stateToFlag.insert(qMakePair(QIcon::Normal, QIcon::On), 0x02);
       
   557             m_stateToFlag.insert(qMakePair(QIcon::Disabled, QIcon::Off), 0x04);
       
   558             m_stateToFlag.insert(qMakePair(QIcon::Disabled, QIcon::On), 0x08);
       
   559             m_stateToFlag.insert(qMakePair(QIcon::Active, QIcon::Off), 0x10);
       
   560             m_stateToFlag.insert(qMakePair(QIcon::Active, QIcon::On), 0x20);
       
   561             m_stateToFlag.insert(qMakePair(QIcon::Selected, QIcon::Off), 0x40);
       
   562             m_stateToFlag.insert(qMakePair(QIcon::Selected, QIcon::On), 0x80);
       
   563 
       
   564             m_flagToState.insert(0x01, qMakePair(QIcon::Normal, QIcon::Off));
       
   565             m_flagToState.insert(0x02, qMakePair(QIcon::Normal, QIcon::On));
       
   566             m_flagToState.insert(0x04, qMakePair(QIcon::Disabled, QIcon::Off));
       
   567             m_flagToState.insert(0x08, qMakePair(QIcon::Disabled, QIcon::On));
       
   568             m_flagToState.insert(0x10, qMakePair(QIcon::Active, QIcon::Off));
       
   569             m_flagToState.insert(0x20, qMakePair(QIcon::Active, QIcon::On));
       
   570             m_flagToState.insert(0x40, qMakePair(QIcon::Selected, QIcon::Off));
       
   571             m_flagToState.insert(0x80, qMakePair(QIcon::Selected, QIcon::On));
       
   572         }
       
   573         uint flag(const QPair<QIcon::Mode, QIcon::State> &pair) const
       
   574         {
       
   575             return m_stateToFlag.value(pair);
       
   576         }
       
   577         QPair<QIcon::Mode, QIcon::State> state(uint flag) const
       
   578         {
       
   579             return m_flagToState.value(flag);
       
   580         }
       
   581     private:
       
   582         QMap<QPair<QIcon::Mode, QIcon::State>, uint > m_stateToFlag;
       
   583         QMap<uint, QPair<QIcon::Mode, QIcon::State> > m_flagToState;
       
   584     };
       
   585 
       
   586     Q_GLOBAL_STATIC(StateMap, stateMap)
       
   587 
       
   588     uint PropertySheetIconValue::mask() const
       
   589     {
       
   590         uint flags = 0;
       
   591         QMapIterator<ModeStateKey, PropertySheetPixmapValue> itPath(m_paths);
       
   592         while (itPath.hasNext())
       
   593             flags |= stateMap()->flag(itPath.next().key());
       
   594         return flags;
       
   595     }
       
   596 
       
   597     uint PropertySheetIconValue::compare(const PropertySheetIconValue &other) const
       
   598     {
       
   599         uint diffMask = mask() | other.mask();
       
   600         for (int i = 0; i < 8; i++) {
       
   601             uint flag = 1 << i;
       
   602             if (diffMask & flag) { // if state is set in both icons, compare the values
       
   603                 const ModeStateKey state = stateMap()->state(flag);
       
   604                 if (pixmap(state.first, state.second) == other.pixmap(state.first, state.second))
       
   605                     diffMask &= ~flag;
       
   606             }
       
   607         }
       
   608         return diffMask;
       
   609     }
       
   610 
       
   611     void PropertySheetIconValue::assign(const PropertySheetIconValue &other, uint mask)
       
   612     {
       
   613         for (int i = 0; i < 8; i++) {
       
   614             uint flag = 1 << i;
       
   615             if (mask & flag) {
       
   616                 const ModeStateKey state = stateMap()->state(flag);
       
   617                 setPixmap(state.first, state.second, other.pixmap(state.first, state.second));
       
   618             }
       
   619         }
       
   620     }
       
   621 
       
   622     PropertySheetIconValue::ModeStateToPixmapMap PropertySheetIconValue::paths() const
       
   623     {
       
   624         return m_paths;
       
   625     }
       
   626 
       
   627     QDESIGNER_SHARED_EXPORT QDesignerFormWindowCommand *createTextPropertyCommand(const QString &propertyName, const QString &text, QObject *object, QDesignerFormWindowInterface *fw)
       
   628     {
       
   629         if (text.isEmpty()) {
       
   630             ResetPropertyCommand *cmd = new ResetPropertyCommand(fw);
       
   631             cmd->init(object, propertyName);
       
   632             return cmd;
       
   633         }
       
   634         SetPropertyCommand *cmd = new SetPropertyCommand(fw);
       
   635         cmd->init(object, propertyName, text);
       
   636         return cmd;
       
   637     }
       
   638 
       
   639     QDESIGNER_SHARED_EXPORT QAction *preferredEditAction(QDesignerFormEditorInterface *core, QWidget *managedWidget)
       
   640     {
       
   641         QAction *action = 0;
       
   642         if (const QDesignerTaskMenuExtension *taskMenu = qt_extension<QDesignerTaskMenuExtension*>(core->extensionManager(), managedWidget)) {
       
   643             action = taskMenu->preferredEditAction();
       
   644             if (!action) {
       
   645                 const QList<QAction *> actions = taskMenu->taskActions();
       
   646                 if (!actions.isEmpty())
       
   647                     action = actions.first();
       
   648             }
       
   649         }
       
   650         if (!action) {
       
   651             if (const QDesignerTaskMenuExtension *taskMenu = qobject_cast<QDesignerTaskMenuExtension *>(
       
   652                         core->extensionManager()->extension(managedWidget, QLatin1String("QDesignerInternalTaskMenuExtension")))) {
       
   653                 action = taskMenu->preferredEditAction();
       
   654                 if (!action) {
       
   655                     const QList<QAction *> actions = taskMenu->taskActions();
       
   656                     if (!actions.isEmpty())
       
   657                         action = actions.first();
       
   658                 }
       
   659             }
       
   660         }
       
   661         return action;
       
   662     }
       
   663 
       
   664     QDESIGNER_SHARED_EXPORT bool runUIC(const QString &fileName, UIC_Mode mode, QByteArray& ba, QString &errorMessage)
       
   665     {
       
   666         QStringList argv;
       
   667         QString binary = QLibraryInfo::location(QLibraryInfo::BinariesPath);
       
   668         binary += QDir::separator();
       
   669         switch (mode) {
       
   670         case UIC_GenerateCode:
       
   671             binary += QLatin1String("uic");
       
   672             break;
       
   673         case UIC_ConvertV3:
       
   674             binary += QLatin1String("uic3");
       
   675             argv += QLatin1String("-convert");
       
   676             break;
       
   677         }
       
   678         argv += fileName;
       
   679         QProcess uic;
       
   680         uic.start(binary, argv);
       
   681         if (!uic.waitForStarted()) {
       
   682             errorMessage = QApplication::translate("Designer", "Unable to launch %1.").arg(binary);
       
   683             return false;
       
   684         }
       
   685         if (!uic.waitForFinished()) {
       
   686             errorMessage = QApplication::translate("Designer", "%1 timed out.").arg(binary);
       
   687             return false;
       
   688         }
       
   689         if (uic.exitCode()) {
       
   690             errorMessage =  QString::fromAscii(uic.readAllStandardError());
       
   691             return false;
       
   692         }
       
   693         ba = uic.readAllStandardOutput();
       
   694         return true;
       
   695     }
       
   696 
       
   697     QDESIGNER_SHARED_EXPORT QString qtify(const QString &name)
       
   698     {
       
   699         QString qname = name;
       
   700 
       
   701         Q_ASSERT(qname.isEmpty() == false);
       
   702 
       
   703 
       
   704         if (qname.count() > 1 && qname.at(1).isUpper()) {
       
   705             const QChar first = qname.at(0);
       
   706             if (first == QLatin1Char('Q') || first == QLatin1Char('K'))
       
   707                 qname.remove(0, 1);
       
   708         }
       
   709 
       
   710         const int len = qname.count();
       
   711         for (int i = 0; i < len && qname.at(i).isUpper(); i++)
       
   712             qname[i] = qname.at(i).toLower();
       
   713 
       
   714         return qname;
       
   715     }
       
   716 
       
   717     // --------------- UpdateBlocker
       
   718     UpdateBlocker::UpdateBlocker(QWidget *w) :
       
   719         m_widget(w),
       
   720         m_enabled(w->updatesEnabled() && w->isVisible())
       
   721     {
       
   722         if (m_enabled)
       
   723             m_widget->setUpdatesEnabled(false);
       
   724     }
       
   725 
       
   726     UpdateBlocker::~UpdateBlocker()
       
   727     {
       
   728         if (m_enabled)
       
   729             m_widget->setUpdatesEnabled(true);
       
   730     }
       
   731 
       
   732 } // namespace qdesigner_internal
       
   733 
       
   734 QT_END_NAMESPACE