tools/designer/src/components/lib/qdesigner_components.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 <QtDesigner/QDesignerComponents>
       
    43 
       
    44 #include <actioneditor_p.h>
       
    45 #include <widgetdatabase_p.h>
       
    46 #include <widgetfactory_p.h>
       
    47 
       
    48 #include <formeditor/formeditor.h>
       
    49 #include <widgetbox/widgetbox.h>
       
    50 #include <propertyeditor/propertyeditor.h>
       
    51 #include <objectinspector/objectinspector.h>
       
    52 #include <taskmenu/taskmenu_component.h>
       
    53 #include "qtresourceview_p.h"
       
    54 #include <qdesigner_integration_p.h>
       
    55 #include <signalsloteditor/signalsloteditorwindow.h>
       
    56 
       
    57 #include <buddyeditor/buddyeditor_plugin.h>
       
    58 #include <signalsloteditor/signalsloteditor_plugin.h>
       
    59 #include <tabordereditor/tabordereditor_plugin.h>
       
    60 
       
    61 #include <QtDesigner/QDesignerLanguageExtension>
       
    62 #include <QtDesigner/QExtensionManager>
       
    63 #include <QtDesigner/QDesignerResourceBrowserInterface>
       
    64 
       
    65 #include <QtCore/qplugin.h>
       
    66 #include <QtCore/QDir>
       
    67 #include <QtCore/QTextStream>
       
    68 #include <QtCore/QDebug>
       
    69 #include <QtCore/QFile>
       
    70 #include <QtCore/QFileInfo>
       
    71 
       
    72 // ### keep it in sync with Q_IMPORT_PLUGIN in qplugin.h
       
    73 #define DECLARE_PLUGIN_INSTANCE(PLUGIN) \
       
    74     extern QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance_##PLUGIN(); \
       
    75     class Static##PLUGIN##PluginInstance { public: \
       
    76         Static##PLUGIN##PluginInstance() {                      \
       
    77             QT_PREPEND_NAMESPACE(qRegisterStaticPluginInstanceFunction) \
       
    78                 (&qt_plugin_instance_##PLUGIN); \
       
    79         } \
       
    80     };
       
    81 
       
    82 #define INIT_PLUGIN_INSTANCE(PLUGIN) \
       
    83     do { \
       
    84         Static##PLUGIN##PluginInstance instance; \
       
    85         Q_UNUSED(instance); \
       
    86     } while (0)
       
    87 
       
    88 DECLARE_PLUGIN_INSTANCE(SignalSlotEditorPlugin)
       
    89 DECLARE_PLUGIN_INSTANCE(BuddyEditorPlugin)
       
    90 DECLARE_PLUGIN_INSTANCE(TabOrderEditorPlugin)
       
    91 
       
    92 static void initResources()
       
    93 {
       
    94     // Q_INIT_RESOURCE only usable in functions in global namespace
       
    95     Q_INIT_RESOURCE(formeditor);
       
    96     Q_INIT_RESOURCE(widgetbox);
       
    97     Q_INIT_RESOURCE(propertyeditor);
       
    98 }
       
    99 
       
   100 
       
   101 static void initInstances()
       
   102 {
       
   103     static bool plugins_initialized = false;
       
   104 
       
   105     if (!plugins_initialized) {
       
   106         INIT_PLUGIN_INSTANCE(SignalSlotEditorPlugin);
       
   107         INIT_PLUGIN_INSTANCE(BuddyEditorPlugin);
       
   108         INIT_PLUGIN_INSTANCE(TabOrderEditorPlugin);
       
   109         plugins_initialized = true;
       
   110     }
       
   111 }
       
   112 
       
   113 QT_BEGIN_NAMESPACE
       
   114 
       
   115 /*!
       
   116     \class QDesignerComponents
       
   117     \brief The QDesignerComponents class provides a central resource for the various components
       
   118     used in the \QD user interface.
       
   119     \inmodule QtDesigner
       
   120     \internal
       
   121 
       
   122     The QDesignerComponents class is a factory for each of the standard components present
       
   123     in the \QD user interface. It is mostly useful for developers who want to implement
       
   124     a standalone form editing environment using \QD's components, or who need to integrate
       
   125     \QD's components into an existing integrated development environment (IDE).
       
   126 
       
   127     \sa QDesignerFormEditorInterface, QDesignerObjectInspectorInterface,
       
   128         QDesignerPropertyEditorInterface, QDesignerWidgetBoxInterface
       
   129 */
       
   130 
       
   131 /*!
       
   132     Initializes the resources used by the components.*/
       
   133 void QDesignerComponents::initializeResources()
       
   134 {
       
   135     initResources();
       
   136 }
       
   137 
       
   138 /*!
       
   139     Initializes the plugins used by the components.*/
       
   140 void QDesignerComponents::initializePlugins(QDesignerFormEditorInterface *core)
       
   141 {
       
   142     qdesigner_internal::QDesignerIntegration::initializePlugins(core);
       
   143 }
       
   144 
       
   145 /*!
       
   146     Constructs a form editor interface with the given \a parent.*/
       
   147 QDesignerFormEditorInterface *QDesignerComponents::createFormEditor(QObject *parent)
       
   148 {
       
   149     initInstances();
       
   150     return new qdesigner_internal::FormEditor(parent);
       
   151 }
       
   152 
       
   153 /*!
       
   154     Returns a new task menu with the given \a parent for the \a core interface.*/
       
   155 QObject *QDesignerComponents::createTaskMenu(QDesignerFormEditorInterface *core, QObject *parent)
       
   156 {
       
   157     return new qdesigner_internal::TaskMenuComponent(core, parent);
       
   158 }
       
   159 
       
   160 static inline int qtMajorVersion(int qtVersion) { return qtVersion >> 16; }
       
   161 static inline int qtMinorVersion(int qtVersion) { return (qtVersion >> 8) & 0xFF; }
       
   162 static inline void setMinorVersion(int minorVersion, int *qtVersion)
       
   163 {
       
   164     *qtVersion &= ~0xFF00;
       
   165     *qtVersion |= minorVersion << 8;
       
   166 }
       
   167 
       
   168 // Build the version-dependent name of the user widget box file, '$HOME.designer/widgetbox4.4.xml'
       
   169 static inline QString widgetBoxFileName(int qtVersion, const QDesignerLanguageExtension *lang = 0)
       
   170 {
       
   171     QString rc; {
       
   172         const QChar dot = QLatin1Char('.');
       
   173         QTextStream str(&rc);
       
   174         str << QDir::homePath() << QDir::separator() << QLatin1String(".designer") << QDir::separator()
       
   175             << QLatin1String("widgetbox");
       
   176         // The naming convention using the version was introduced with 4.4
       
   177         const int major = qtMajorVersion(qtVersion);
       
   178         const int minor = qtMinorVersion(qtVersion);
       
   179         if (major >= 4 &&  minor >= 4)
       
   180             str << major << dot << minor;
       
   181         if (lang)
       
   182             str << dot << lang->uiExtension();
       
   183         str << QLatin1String(".xml");
       
   184     }
       
   185     return rc;
       
   186 }
       
   187 
       
   188 /*!
       
   189     Returns a new widget box interface with the given \a parent for the \a core interface.*/
       
   190 QDesignerWidgetBoxInterface *QDesignerComponents::createWidgetBox(QDesignerFormEditorInterface *core, QWidget *parent)
       
   191 {
       
   192     qdesigner_internal::WidgetBox *widgetBox = new qdesigner_internal::WidgetBox(core, parent);
       
   193 
       
   194     const QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core);
       
   195 
       
   196     do {
       
   197         if (lang) {
       
   198             const QString languageWidgetBox = lang->widgetBoxContents();
       
   199             if (!languageWidgetBox.isEmpty()) {
       
   200                 widgetBox->loadContents(lang->widgetBoxContents());
       
   201                 break;
       
   202             }
       
   203         }
       
   204 
       
   205         widgetBox->setFileName(QLatin1String(":/trolltech/widgetbox/widgetbox.xml"));
       
   206         widgetBox->load();
       
   207     } while (false);
       
   208 
       
   209     const QString userWidgetBoxFile = widgetBoxFileName(QT_VERSION, lang);
       
   210 
       
   211     widgetBox->setFileName(userWidgetBoxFile);
       
   212     if (!QFileInfo(userWidgetBoxFile).exists()) {
       
   213         // check previous version, that is, are we running the new version for the first time
       
   214         // If so, try to copy the old widget box file
       
   215         if (const int minv = qtMinorVersion(QT_VERSION)) {
       
   216             int oldVersion = QT_VERSION;
       
   217             setMinorVersion(minv - 1, &oldVersion);
       
   218             const QString oldWidgetBoxFile = widgetBoxFileName(oldVersion, lang);
       
   219             if (QFileInfo(oldWidgetBoxFile).exists())
       
   220                 QFile::copy(oldWidgetBoxFile, userWidgetBoxFile);
       
   221         }
       
   222     }
       
   223     widgetBox->load();
       
   224 
       
   225     return widgetBox;
       
   226 }
       
   227 
       
   228 /*!
       
   229     Returns a new property editor interface with the given \a parent for the \a core interface.*/
       
   230 QDesignerPropertyEditorInterface *QDesignerComponents::createPropertyEditor(QDesignerFormEditorInterface *core, QWidget *parent)
       
   231 {
       
   232     return new qdesigner_internal::PropertyEditor(core, parent);
       
   233 }
       
   234 
       
   235 /*!
       
   236     Returns a new object inspector interface with the given \a parent for the \a core interface.*/
       
   237 QDesignerObjectInspectorInterface *QDesignerComponents::createObjectInspector(QDesignerFormEditorInterface *core, QWidget *parent)
       
   238 {
       
   239     return new qdesigner_internal::ObjectInspector(core, parent);
       
   240 }
       
   241 
       
   242 /*!
       
   243     Returns a new action editor interface with the given \a parent for the \a core interface.*/
       
   244 QDesignerActionEditorInterface *QDesignerComponents::createActionEditor(QDesignerFormEditorInterface *core, QWidget *parent)
       
   245 {
       
   246     return new qdesigner_internal::ActionEditor(core, parent);
       
   247 }
       
   248 
       
   249 /*!
       
   250     Returns a new resource editor with the given \a parent for the \a core interface.*/
       
   251 QWidget *QDesignerComponents::createResourceEditor(QDesignerFormEditorInterface *core, QWidget *parent)
       
   252 {
       
   253     if (QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core)) {
       
   254         QWidget *w = lang->createResourceBrowser(parent);
       
   255         if (w)
       
   256             return w;
       
   257     }
       
   258     QtResourceView *resourceView = new QtResourceView(core, parent);
       
   259     resourceView->setResourceModel(core->resourceModel());
       
   260     resourceView->setSettingsKey(QLatin1String("ResourceBrowser"));
       
   261     qdesigner_internal::QDesignerIntegration *designerIntegration = qobject_cast<qdesigner_internal::QDesignerIntegration *>(core->integration());
       
   262     // Note for integrators: make sure you call createResourceEditor() after you instantiated your subclass of designer integration
       
   263     // (designer doesn't do that since by default editing resources is enabled)
       
   264     if (designerIntegration)
       
   265         resourceView->setResourceEditingEnabled(designerIntegration->isResourceEditingEnabled());
       
   266     return resourceView;
       
   267 }
       
   268 
       
   269 /*!
       
   270     Returns a new signal-slot editor with the given \a parent for the \a core interface.*/
       
   271 QWidget *QDesignerComponents::createSignalSlotEditor(QDesignerFormEditorInterface *core, QWidget *parent)
       
   272 {
       
   273     return new qdesigner_internal::SignalSlotEditorWindow(core, parent);
       
   274 }
       
   275 
       
   276 QT_END_NAMESPACE
       
   277