tools/designer/src/components/widgetbox/widgetbox_dnditem.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 "widgetbox_dnditem.h"
       
    43 #include "ui4_p.h"
       
    44 
       
    45 #include <widgetfactory_p.h>
       
    46 #include <spacer_widget_p.h>
       
    47 #include <qdesigner_formbuilder_p.h>
       
    48 #include <qtresourcemodel_p.h>
       
    49 #include <formscriptrunner_p.h>
       
    50 #include <formwindowbase_p.h>
       
    51 #include <qdesigner_utils_p.h>
       
    52 #include <qdesigner_dockwidget_p.h>
       
    53 #include <qsimpleresource_p.h>
       
    54 
       
    55 #include <QtDesigner/QDesignerFormEditorInterface>
       
    56 #include <QtDesigner/QDesignerFormWindowManagerInterface>
       
    57 
       
    58 #include <QtGui/QStyle>
       
    59 #include <QtGui/QApplication>
       
    60 
       
    61 QT_BEGIN_NAMESPACE
       
    62 
       
    63 namespace qdesigner_internal {
       
    64 /*******************************************************************************
       
    65 ** WidgetBoxResource
       
    66 */
       
    67 
       
    68 static inline DeviceProfile currentDeviceProfile(const QDesignerFormEditorInterface *core)
       
    69 {
       
    70     if (QDesignerFormWindowInterface *cfw = core->formWindowManager()->activeFormWindow())
       
    71         if (const FormWindowBase *fwb = qobject_cast<const FormWindowBase *>(cfw))
       
    72             return fwb->deviceProfile();
       
    73     return DeviceProfile();
       
    74 }
       
    75 
       
    76 class WidgetBoxResource : public QDesignerFormBuilder
       
    77 {
       
    78 public:
       
    79     WidgetBoxResource(QDesignerFormEditorInterface *core);
       
    80 
       
    81     // protected->public
       
    82    QWidget *createUI(DomUI *ui, QWidget *parents) { return QDesignerFormBuilder::create(ui, parents); }
       
    83 
       
    84 protected:
       
    85 
       
    86     virtual QWidget *create(DomWidget *ui_widget, QWidget *parents);
       
    87     virtual QWidget *createWidget(const QString &widgetName, QWidget *parentWidget, const QString &name);
       
    88     virtual void createCustomWidgets(DomCustomWidgets *);
       
    89 };
       
    90 
       
    91 WidgetBoxResource::WidgetBoxResource(QDesignerFormEditorInterface *core) :
       
    92     QDesignerFormBuilder(core, DisableScripts, currentDeviceProfile(core))
       
    93 {
       
    94 }
       
    95 
       
    96 
       
    97 QWidget *WidgetBoxResource::createWidget(const QString &widgetName, QWidget *parentWidget, const QString &name)
       
    98 {
       
    99     if (widgetName == QLatin1String("Spacer")) {
       
   100         Spacer *spacer = new Spacer(parentWidget);
       
   101         spacer->setObjectName(name);
       
   102         return spacer;
       
   103     }
       
   104 
       
   105     return QDesignerFormBuilder::createWidget(widgetName, parentWidget, name);
       
   106 }
       
   107 
       
   108 QWidget *WidgetBoxResource::create(DomWidget *ui_widget, QWidget *parent)
       
   109 {
       
   110     QWidget *result = QDesignerFormBuilder::create(ui_widget, parent);
       
   111     // It is possible to have a syntax error or something in custom
       
   112     // widget XML, so, try to recover here by creating an artificial
       
   113     // top level + widget.
       
   114     if (!result) {
       
   115         const QString msg = QApplication::translate("qdesigner_internal::WidgetBox", "Warning: Widget creation failed in the widget box. This could be caused by invalid custom widget XML.");
       
   116         qdesigner_internal::designerWarning(msg);
       
   117         result = new QWidget(parent);
       
   118         new QWidget(result);
       
   119     }
       
   120     result->setFocusPolicy(Qt::NoFocus);
       
   121     result->setObjectName(ui_widget->attributeName());
       
   122     return result;
       
   123 }
       
   124 
       
   125 void WidgetBoxResource::createCustomWidgets(DomCustomWidgets *dc)
       
   126 {
       
   127     // Make a promotion entry in  case someone has a promoted widget
       
   128     // in the scratchpad.
       
   129     QSimpleResource::handleDomCustomWidgets(core(), dc);
       
   130 
       
   131 }
       
   132 
       
   133 /*******************************************************************************
       
   134 ** WidgetBoxResource
       
   135 */
       
   136 
       
   137 static QSize geometryProp(const DomWidget *dw)
       
   138 {
       
   139     const QList<DomProperty*> prop_list = dw->elementProperty();
       
   140     const QString geometry = QLatin1String("geometry");
       
   141     foreach (DomProperty *prop, prop_list) {
       
   142         if (prop->attributeName() !=  geometry)
       
   143             continue;
       
   144         DomRect *dr = prop->elementRect();
       
   145         if (dr == 0)
       
   146             continue;
       
   147         return QSize(dr->elementWidth(), dr->elementHeight());
       
   148     }
       
   149     return QSize();
       
   150 }
       
   151 
       
   152 static QSize domWidgetSize(const DomWidget *dw)
       
   153 {
       
   154     QSize size = geometryProp(dw);
       
   155     if (size.isValid())
       
   156         return size;
       
   157 
       
   158     foreach (const DomWidget *child, dw->elementWidget()) {
       
   159         size = geometryProp(child);
       
   160         if (size.isValid())
       
   161             return size;
       
   162     }
       
   163 
       
   164     foreach (const DomLayout *dl, dw->elementLayout()) {
       
   165         foreach (DomLayoutItem *item, dl->elementItem()) {
       
   166             const DomWidget *child = item->elementWidget();
       
   167             if (child == 0)
       
   168                 continue;
       
   169             size = geometryProp(child);
       
   170             if (size.isValid())
       
   171                 return size;
       
   172         }
       
   173     }
       
   174 
       
   175     return QSize();
       
   176 }
       
   177 
       
   178 static QWidget *decorationFromDomWidget(DomUI *dom_ui, QDesignerFormEditorInterface *core)
       
   179 {
       
   180     WidgetBoxResource builder(core);
       
   181     // We have the builder create the articial QWidget fake top level as a tooltip
       
   182     // because the size algorithm works better at weird DPI settings
       
   183     // if the actual widget is created as a child of a container
       
   184     QWidget *fakeTopLevel = builder.createUI(dom_ui, static_cast<QWidget*>(0));
       
   185     fakeTopLevel->setParent(0, Qt::ToolTip); // Container
       
   186     // Actual widget
       
   187     const DomWidget *domW = dom_ui->elementWidget()->elementWidget().front();
       
   188     QWidget *w = qFindChildren<QWidget*>(fakeTopLevel).front();
       
   189     Q_ASSERT(w);
       
   190     // hack begin;
       
   191     // We set _q_dockDrag dynamic property which will be detected in drag enter event of form window.
       
   192     // Dock drop is handled in special way (highlight goes to central widget of main window)
       
   193     if (qobject_cast<QDesignerDockWidget *>(w))
       
   194         fakeTopLevel->setProperty("_q_dockDrag", QVariant(true));
       
   195     // hack end;
       
   196     w->setAutoFillBackground(true); // Different style for embedded
       
   197     QSize size = domWidgetSize(domW);
       
   198     const QSize minimumSize = w->minimumSizeHint();
       
   199     if (!size.isValid())
       
   200         size = w->sizeHint();
       
   201     if (size.width() < minimumSize.width())
       
   202         size.setWidth(minimumSize.width());
       
   203     if (size.height() < minimumSize.height())
       
   204         size.setHeight(minimumSize.height());
       
   205     // A QWidget might have size -1,-1 if no geometry property is specified in the widget box.
       
   206     if (size.isEmpty())
       
   207         size = size.expandedTo(QSize(16, 16));
       
   208     w->setGeometry(QRect(QPoint(0, 0), size));
       
   209     fakeTopLevel->resize(size);
       
   210     return fakeTopLevel;
       
   211 }
       
   212 
       
   213 WidgetBoxDnDItem::WidgetBoxDnDItem(QDesignerFormEditorInterface *core,
       
   214                                    DomUI *dom_ui,
       
   215                                    const QPoint &global_mouse_pos) :
       
   216     QDesignerDnDItem(CopyDrop)
       
   217 {
       
   218     QWidget *decoration = decorationFromDomWidget(dom_ui, core);
       
   219     decoration->move(global_mouse_pos - QPoint(5, 5));
       
   220 
       
   221     init(dom_ui, 0, decoration, global_mouse_pos);
       
   222 }
       
   223 }
       
   224 
       
   225 QT_END_NAMESPACE