tools/designer/src/lib/shared/shared_settings.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
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 "shared_settings_p.h"
       
    43 #include "grid_p.h"
       
    44 #include "previewmanager_p.h"
       
    45 #include "qdesigner_utils_p.h"
       
    46 #include <QtDesigner/abstractformeditor.h>
       
    47 #include <QtDesigner/private/abstractsettings_p.h>
       
    48 #include <QtCore/QStringList>
       
    49 #include <QtCore/QDir>
       
    50 #include <QtCore/QVariantMap>
       
    51 #include <QtCore/QCoreApplication>
       
    52 #include <QtCore/QSize>
       
    53 
       
    54 QT_BEGIN_NAMESPACE
       
    55 
       
    56 static const char *designerPath = "/.designer";
       
    57 static const char *defaultGridKey = "defaultGrid";
       
    58 static const char *previewKey = "Preview";
       
    59 static const char *enabledKey = "Enabled";
       
    60 static const char *userDeviceSkinsKey= "UserDeviceSkins";
       
    61 static const char *zoomKey = "zoom";
       
    62 static const char *zoomEnabledKey = "zoomEnabled";
       
    63 static const char *deviceProfileIndexKey = "DeviceProfileIndex";
       
    64 static const char *deviceProfilesKey = "DeviceProfiles";
       
    65 static const char *formTemplatePathsKey = "FormTemplatePaths";
       
    66 static const char *formTemplateKey = "FormTemplate";
       
    67 static const char *newFormSizeKey = "NewFormSize";
       
    68 
       
    69 using namespace qdesigner_internal;
       
    70 
       
    71 static bool checkTemplatePath(const QString &path, bool create)
       
    72 {
       
    73     QDir current(QDir::current());
       
    74     if (current.exists(path))
       
    75         return true;
       
    76 
       
    77     if (!create)
       
    78         return false;
       
    79 
       
    80     if (current.mkpath(path))
       
    81         return true;
       
    82 
       
    83     qdesigner_internal::designerWarning(QCoreApplication::translate("QDesignerSharedSettings", "The template path %1 could not be created.").arg(path));
       
    84     return false;
       
    85 }
       
    86 
       
    87 namespace qdesigner_internal {
       
    88 
       
    89 QDesignerSharedSettings::QDesignerSharedSettings(QDesignerFormEditorInterface *core)
       
    90         : m_settings(core->settingsManager())
       
    91 {
       
    92 }
       
    93 
       
    94 Grid QDesignerSharedSettings::defaultGrid() const
       
    95 {
       
    96     Grid grid;
       
    97     const QVariantMap defaultGridMap
       
    98             = m_settings->value(QLatin1String(defaultGridKey), QVariantMap()).toMap();
       
    99     if (!defaultGridMap.empty())
       
   100         grid.fromVariantMap(defaultGridMap);
       
   101     return grid;
       
   102 }
       
   103 
       
   104 void QDesignerSharedSettings::setDefaultGrid(const Grid &grid)
       
   105 {
       
   106     m_settings->setValue(QLatin1String(defaultGridKey), grid.toVariantMap());
       
   107 }
       
   108 
       
   109 const QStringList &QDesignerSharedSettings::defaultFormTemplatePaths()
       
   110 {
       
   111     static QStringList rc;
       
   112     if (rc.empty()) {
       
   113         // Ensure default form template paths
       
   114         const QString templatePath = QLatin1String("/templates");
       
   115         // home
       
   116         QString path = QDir::homePath();
       
   117         path += QLatin1String(designerPath);
       
   118         path += templatePath;
       
   119         if (checkTemplatePath(path, true))
       
   120             rc += path;
       
   121 
       
   122         // designer/bin: Might be owned by root in some installations, do not force it.
       
   123         path = qApp->applicationDirPath();
       
   124         path += templatePath;
       
   125         if (checkTemplatePath(path, false))
       
   126             rc += path;
       
   127     }
       
   128     return rc;
       
   129 }
       
   130 
       
   131 QStringList QDesignerSharedSettings::formTemplatePaths() const
       
   132 {
       
   133     return m_settings->value(QLatin1String(formTemplatePathsKey),
       
   134                             defaultFormTemplatePaths()).toStringList();
       
   135 }
       
   136 
       
   137 void QDesignerSharedSettings::setFormTemplatePaths(const QStringList &paths)
       
   138 {
       
   139     m_settings->setValue(QLatin1String(formTemplatePathsKey), paths);
       
   140 }
       
   141 
       
   142 QString  QDesignerSharedSettings::formTemplate() const
       
   143 {
       
   144     return m_settings->value(QLatin1String(formTemplateKey)).toString();
       
   145 }
       
   146 
       
   147 void QDesignerSharedSettings::setFormTemplate(const QString &t)
       
   148 {
       
   149     m_settings->setValue(QLatin1String(formTemplateKey), t);
       
   150 }
       
   151 
       
   152 void QDesignerSharedSettings::setAdditionalFormTemplatePaths(const QStringList &additionalPaths)
       
   153 {
       
   154     // merge template paths
       
   155     QStringList templatePaths = defaultFormTemplatePaths();
       
   156     templatePaths += additionalPaths;
       
   157     setFormTemplatePaths(templatePaths);
       
   158 }
       
   159 
       
   160 QStringList QDesignerSharedSettings::additionalFormTemplatePaths() const
       
   161 {
       
   162     // get template paths excluding internal ones
       
   163     QStringList rc = formTemplatePaths();
       
   164     foreach (QString internalTemplatePath, defaultFormTemplatePaths()) {
       
   165         const int index = rc.indexOf(internalTemplatePath);
       
   166         if (index != -1)
       
   167             rc.removeAt(index);
       
   168     }
       
   169     return rc;
       
   170 }
       
   171 
       
   172 QSize QDesignerSharedSettings::newFormSize() const
       
   173 {
       
   174     return m_settings->value(QLatin1String(newFormSizeKey), QSize(0, 0)).toSize();
       
   175 }
       
   176 
       
   177 void  QDesignerSharedSettings::setNewFormSize(const QSize &s)
       
   178 {
       
   179     if (s.isNull()) {
       
   180         m_settings->remove(QLatin1String(newFormSizeKey));
       
   181     } else {
       
   182         m_settings->setValue(QLatin1String(newFormSizeKey), s);
       
   183     }
       
   184 }
       
   185 
       
   186 
       
   187 PreviewConfiguration QDesignerSharedSettings::customPreviewConfiguration() const
       
   188 {
       
   189     PreviewConfiguration configuration;
       
   190     configuration.fromSettings(QLatin1String(previewKey), m_settings);
       
   191     return configuration;
       
   192 }
       
   193 
       
   194 void QDesignerSharedSettings::setCustomPreviewConfiguration(const PreviewConfiguration &configuration)
       
   195 {
       
   196     configuration.toSettings(QLatin1String(previewKey), m_settings);
       
   197 }
       
   198 
       
   199 bool QDesignerSharedSettings::isCustomPreviewConfigurationEnabled() const
       
   200 {
       
   201     m_settings->beginGroup(QLatin1String(previewKey));
       
   202     bool isEnabled = m_settings->value(QLatin1String(enabledKey), false).toBool();
       
   203     m_settings->endGroup();
       
   204     return isEnabled;
       
   205 }
       
   206 
       
   207 void QDesignerSharedSettings::setCustomPreviewConfigurationEnabled(bool enabled)
       
   208 {
       
   209     m_settings->beginGroup(QLatin1String(previewKey));
       
   210     m_settings->setValue(QLatin1String(enabledKey), enabled);
       
   211     m_settings->endGroup();
       
   212 }
       
   213 
       
   214 QStringList QDesignerSharedSettings::userDeviceSkins() const
       
   215 {
       
   216     m_settings->beginGroup(QLatin1String(previewKey));
       
   217     QStringList userDeviceSkins
       
   218             = m_settings->value(QLatin1String(userDeviceSkinsKey), QStringList()).toStringList();
       
   219     m_settings->endGroup();
       
   220     return userDeviceSkins;
       
   221 }
       
   222 
       
   223 void QDesignerSharedSettings::setUserDeviceSkins(const QStringList &userDeviceSkins)
       
   224 {
       
   225     m_settings->beginGroup(QLatin1String(previewKey));
       
   226     m_settings->setValue(QLatin1String(userDeviceSkinsKey), userDeviceSkins);
       
   227     m_settings->endGroup();
       
   228 }
       
   229 
       
   230 int QDesignerSharedSettings::zoom() const
       
   231 {
       
   232     return m_settings->value(QLatin1String(zoomKey), 100).toInt();
       
   233 }
       
   234 
       
   235 void QDesignerSharedSettings::setZoom(int z)
       
   236 {
       
   237     m_settings->setValue(QLatin1String(zoomKey), QVariant(z));
       
   238 }
       
   239 
       
   240 bool QDesignerSharedSettings::zoomEnabled() const
       
   241 {
       
   242     return m_settings->value(QLatin1String(zoomEnabledKey), false).toBool();
       
   243 }
       
   244 
       
   245 void QDesignerSharedSettings::setZoomEnabled(bool v)
       
   246 {
       
   247      m_settings->setValue(QLatin1String(zoomEnabledKey), v);
       
   248 }
       
   249 
       
   250 DeviceProfile QDesignerSharedSettings::currentDeviceProfile() const
       
   251 {
       
   252     return deviceProfileAt(currentDeviceProfileIndex());
       
   253 }
       
   254 
       
   255 void QDesignerSharedSettings::setCurrentDeviceProfileIndex(int i)
       
   256 {
       
   257     m_settings->setValue(QLatin1String(deviceProfileIndexKey), i);
       
   258 }
       
   259 
       
   260 int QDesignerSharedSettings::currentDeviceProfileIndex() const
       
   261 {
       
   262      return m_settings->value(QLatin1String(deviceProfileIndexKey), -1).toInt();
       
   263 }
       
   264 
       
   265 static inline QString msgWarnDeviceProfileXml(const QString &msg)
       
   266 {
       
   267     return QCoreApplication::translate("QDesignerSharedSettings", "An error has been encountered while parsing device profile XML: %1").arg(msg);
       
   268 }
       
   269 
       
   270 DeviceProfile QDesignerSharedSettings::deviceProfileAt(int idx) const
       
   271 {
       
   272     DeviceProfile rc;
       
   273     if (idx < 0)
       
   274         return rc;
       
   275     const QStringList xmls = deviceProfileXml();
       
   276     if (idx >= xmls.size())
       
   277         return rc;
       
   278     QString errorMessage;
       
   279     if (!rc.fromXml(xmls.at(idx), &errorMessage)) {
       
   280         rc.clear();
       
   281         designerWarning(msgWarnDeviceProfileXml(errorMessage));
       
   282     }
       
   283     return rc;
       
   284 }
       
   285 
       
   286 QStringList QDesignerSharedSettings::deviceProfileXml() const
       
   287 {
       
   288     return m_settings->value(QLatin1String(deviceProfilesKey), QStringList()).toStringList();
       
   289 }
       
   290 
       
   291 QDesignerSharedSettings::DeviceProfileList QDesignerSharedSettings::deviceProfiles() const
       
   292 {
       
   293     DeviceProfileList rc;
       
   294     const QStringList xmls = deviceProfileXml();
       
   295     if (xmls.empty())
       
   296         return rc;
       
   297     // De-serialize
       
   298     QString errorMessage;
       
   299     DeviceProfile dp;
       
   300     const QStringList::const_iterator scend = xmls.constEnd();
       
   301     for (QStringList::const_iterator it = xmls.constBegin(); it != scend; ++it) {
       
   302         if (dp.fromXml(*it, &errorMessage)) {
       
   303             rc.push_back(dp);
       
   304         } else {
       
   305             designerWarning(msgWarnDeviceProfileXml(errorMessage));
       
   306         }
       
   307     }
       
   308     return rc;
       
   309 }
       
   310 
       
   311 void QDesignerSharedSettings::setDeviceProfiles(const DeviceProfileList &dp)
       
   312 {
       
   313     QStringList l;
       
   314     const DeviceProfileList::const_iterator dcend = dp.constEnd();
       
   315     for (DeviceProfileList::const_iterator it = dp.constBegin(); it != dcend; ++it)
       
   316         l.push_back(it->toXml());
       
   317     m_settings->setValue(QLatin1String(deviceProfilesKey), l);
       
   318 }
       
   319 }
       
   320 
       
   321 QT_END_NAMESPACE