tools/designer/src/lib/shared/qdesigner_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 "qdesigner_dnditem_p.h"
       
    43 #include "formwindowbase_p.h"
       
    44 #include "ui4_p.h"
       
    45 
       
    46 #include <QtGui/QPainter>
       
    47 #include <QtGui/QBitmap>
       
    48 #include <QtGui/QPixmap>
       
    49 #include <QtGui/QImage>
       
    50 #include <QtGui/QLabel>
       
    51 #include <QtGui/QDrag>
       
    52 #include <QtGui/QCursor>
       
    53 #include <QtGui/QDropEvent>
       
    54 #include <QtGui/QRgb>
       
    55 
       
    56 #include <QtCore/QMultiMap>
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 namespace qdesigner_internal {
       
    61 
       
    62 QDesignerDnDItem::QDesignerDnDItem(DropType type, QWidget *source) :
       
    63     m_source(source),
       
    64     m_type(type),
       
    65     m_dom_ui(0),
       
    66     m_widget(0),
       
    67     m_decoration(0)
       
    68 {
       
    69 }
       
    70 
       
    71 void QDesignerDnDItem::init(DomUI *ui, QWidget *widget, QWidget *decoration,
       
    72                                     const QPoint &global_mouse_pos)
       
    73 {
       
    74     Q_ASSERT(widget != 0 || ui != 0);
       
    75     Q_ASSERT(decoration != 0);
       
    76 
       
    77     m_dom_ui = ui;
       
    78     m_widget = widget;
       
    79     m_decoration = decoration;
       
    80 
       
    81     const QRect geometry = m_decoration->geometry();
       
    82     m_hot_spot = global_mouse_pos - m_decoration->geometry().topLeft();
       
    83 }
       
    84 
       
    85 QDesignerDnDItem::~QDesignerDnDItem()
       
    86 {
       
    87     if (m_decoration != 0)
       
    88         m_decoration->deleteLater();
       
    89     delete m_dom_ui;
       
    90 }
       
    91 
       
    92 DomUI *QDesignerDnDItem::domUi() const
       
    93 {
       
    94     return m_dom_ui;
       
    95 }
       
    96 
       
    97 QWidget *QDesignerDnDItem::decoration() const
       
    98 {
       
    99     return m_decoration;
       
   100 }
       
   101 
       
   102 QPoint QDesignerDnDItem::hotSpot() const
       
   103 {
       
   104     return m_hot_spot;
       
   105 }
       
   106 
       
   107 QWidget *QDesignerDnDItem::widget() const
       
   108 {
       
   109     return m_widget;
       
   110 }
       
   111 
       
   112 QDesignerDnDItem::DropType QDesignerDnDItem::type() const
       
   113 {
       
   114     return m_type;
       
   115 }
       
   116 
       
   117 QWidget *QDesignerDnDItem::source() const
       
   118 {
       
   119     return m_source;
       
   120 }
       
   121 
       
   122 void QDesignerDnDItem::setDomUi(DomUI *dom_ui)
       
   123 {
       
   124     delete m_dom_ui;
       
   125     m_dom_ui = dom_ui;
       
   126 }
       
   127 
       
   128 // ---------- QDesignerMimeData
       
   129 
       
   130 // Make pixmap transparent on Windows only. Mac is transparent by default, Unix usually does not work.
       
   131 #ifdef Q_WS_WIN
       
   132 #  define TRANSPARENT_DRAG_PIXMAP
       
   133 #endif
       
   134 
       
   135 QDesignerMimeData::QDesignerMimeData(const QDesignerDnDItems &items, QDrag *drag) :
       
   136     m_items(items)
       
   137 {
       
   138     enum { Alpha = 200 };
       
   139     QPoint decorationTopLeft;
       
   140     switch (m_items.size()) {
       
   141     case 0:
       
   142         break;
       
   143     case 1: {
       
   144         QWidget *deco = m_items.first()->decoration();
       
   145         decorationTopLeft = deco->pos();
       
   146         const QPixmap widgetPixmap = QPixmap::grabWidget(deco);
       
   147 #ifdef TRANSPARENT_DRAG_PIXMAP
       
   148         QImage image(widgetPixmap.size(), QImage::Format_ARGB32);
       
   149         image.fill(QColor(Qt::transparent).rgba());
       
   150         QPainter painter(&image);
       
   151         painter.drawPixmap(QPoint(0, 0), widgetPixmap);
       
   152         painter.end();
       
   153         setImageTransparency(image, Alpha);
       
   154         drag->setPixmap(QPixmap::fromImage(image));
       
   155 #else
       
   156         drag->setPixmap(widgetPixmap);
       
   157 #endif
       
   158     }
       
   159         break;
       
   160     default: {
       
   161         // determine size of drag decoration by uniting all geometries
       
   162         const QDesignerDnDItems::const_iterator cend = m_items.constEnd();
       
   163         QDesignerDnDItems::const_iterator it =m_items.constBegin();
       
   164         QRect unitedGeometry = (*it)->decoration()->geometry();
       
   165         for (++it; it != cend; ++it )
       
   166             unitedGeometry  = unitedGeometry .united((*it)->decoration()->geometry());
       
   167 
       
   168         // paint with offset. At the same time, create a mask bitmap, containing widget rectangles.
       
   169         QImage image(unitedGeometry.size(), QImage::Format_ARGB32);
       
   170         image.fill(QColor(Qt::transparent).rgba());
       
   171         QBitmap mask(unitedGeometry.size());
       
   172         mask.clear();
       
   173         // paint with offset, determine action
       
   174         QPainter painter(&image);
       
   175         QPainter maskPainter(&mask);
       
   176         decorationTopLeft = unitedGeometry.topLeft();
       
   177         for (it = m_items.constBegin() ; it != cend; ++it ) {
       
   178             QWidget *w = (*it)->decoration();
       
   179             const QPixmap wp = QPixmap::grabWidget(w);
       
   180             const QPoint pos = w->pos() - decorationTopLeft;
       
   181             painter.drawPixmap(pos, wp);
       
   182             maskPainter.fillRect(QRect(pos, wp.size()), Qt::color1);
       
   183         }
       
   184         painter.end();
       
   185         maskPainter.end();
       
   186 #ifdef TRANSPARENT_DRAG_PIXMAP
       
   187         setImageTransparency(image, Alpha);
       
   188 #endif
       
   189         QPixmap pixmap = QPixmap::fromImage(image);
       
   190         pixmap.setMask(mask);
       
   191         drag->setPixmap(pixmap);
       
   192     }
       
   193         break;
       
   194     }
       
   195     // determine hot spot and reconstruct the exact starting position as form window
       
   196     // introduces some offset when detecting DnD
       
   197     m_globalStartPos =  m_items.first()->decoration()->pos() +  m_items.first()->hotSpot();
       
   198     m_hotSpot = m_globalStartPos - decorationTopLeft;
       
   199     drag->setHotSpot(m_hotSpot);
       
   200 
       
   201     drag->setMimeData(this);
       
   202 }
       
   203 
       
   204 QDesignerMimeData::~QDesignerMimeData()
       
   205 {
       
   206     const QDesignerDnDItems::const_iterator cend = m_items.constEnd();
       
   207     for (QDesignerDnDItems::const_iterator it = m_items.constBegin(); it != cend; ++it )
       
   208         delete *it;
       
   209 }
       
   210 
       
   211 Qt::DropAction QDesignerMimeData::proposedDropAction() const
       
   212 {
       
   213    return m_items.first()->type() == QDesignerDnDItemInterface::CopyDrop ? Qt::CopyAction : Qt::MoveAction;
       
   214 }
       
   215 
       
   216 Qt::DropAction QDesignerMimeData::execDrag(const QDesignerDnDItems &items, QWidget * dragSource)
       
   217 {
       
   218     if (items.empty())
       
   219         return Qt::IgnoreAction;
       
   220 
       
   221     QDrag *drag = new QDrag(dragSource);
       
   222     QDesignerMimeData *mimeData = new QDesignerMimeData(items, drag);
       
   223 
       
   224     // Store pointers to widgets that are to be re-shown if a move operation is canceled
       
   225     QWidgetList reshowWidgets;
       
   226     const QDesignerDnDItems::const_iterator cend = items.constEnd();
       
   227     for (QDesignerDnDItems::const_iterator it = items.constBegin(); it != cend; ++it )
       
   228         if (QWidget *w = (*it)->widget())
       
   229             if ((*it)->type() ==  QDesignerDnDItemInterface::MoveDrop)
       
   230                 reshowWidgets.push_back(w);
       
   231 
       
   232     const Qt::DropAction executedAction = drag->exec(Qt::CopyAction|Qt::MoveAction, mimeData->proposedDropAction());
       
   233 
       
   234     if (executedAction == Qt::IgnoreAction && !reshowWidgets.empty())
       
   235         foreach (QWidget *w, reshowWidgets)
       
   236             w->show();
       
   237 
       
   238     return executedAction;
       
   239 }
       
   240 
       
   241 
       
   242 void QDesignerMimeData::moveDecoration(const QPoint &globalPos) const
       
   243 {
       
   244     const QPoint relativeDistance = globalPos - m_globalStartPos;
       
   245     const QDesignerDnDItems::const_iterator cend = m_items.constEnd();
       
   246     for (QDesignerDnDItems::const_iterator it =m_items.constBegin(); it != cend; ++it ) {
       
   247         QWidget *w = (*it)->decoration();
       
   248         w->move(w->pos() + relativeDistance);
       
   249     }
       
   250 }
       
   251 
       
   252 void QDesignerMimeData::removeMovedWidgetsFromSourceForm(const QDesignerDnDItems &items)
       
   253 {
       
   254     typedef QMultiMap<FormWindowBase *, QWidget *> FormWidgetMap;
       
   255     FormWidgetMap formWidgetMap;
       
   256     // Find moved widgets per form
       
   257     const QDesignerDnDItems::const_iterator cend = items.constEnd();
       
   258     for (QDesignerDnDItems::const_iterator it = items.constBegin(); it != cend; ++it )
       
   259         if ((*it)->type() ==  QDesignerDnDItemInterface::MoveDrop)
       
   260             if (QWidget *w = (*it)->widget())
       
   261                 if (FormWindowBase *fb = qobject_cast<FormWindowBase *>((*it)->source()))
       
   262                     formWidgetMap.insert(fb, w);
       
   263     if (formWidgetMap.empty())
       
   264         return;
       
   265 
       
   266     foreach (FormWindowBase * fb, formWidgetMap.keys())
       
   267         fb->deleteWidgetList(formWidgetMap.values(fb));
       
   268 }
       
   269 
       
   270 void QDesignerMimeData::acceptEventWithAction(Qt::DropAction desiredAction, QDropEvent *e)
       
   271 {
       
   272     if (e->proposedAction() == desiredAction) {
       
   273         e->acceptProposedAction();
       
   274     } else {
       
   275         e->setDropAction(desiredAction);
       
   276         e->accept();
       
   277     }
       
   278 }
       
   279 
       
   280 void QDesignerMimeData::acceptEvent(QDropEvent *e) const
       
   281 {
       
   282     acceptEventWithAction(proposedDropAction(), e);
       
   283 }
       
   284 
       
   285 void QDesignerMimeData::setImageTransparency(QImage &image, int alpha)
       
   286 {
       
   287     const int height = image.height();
       
   288     for (int l = 0; l < height; l++) {
       
   289         QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(l));
       
   290         QRgb *lineEnd = line + image.width();
       
   291         for ( ; line < lineEnd; line++) {
       
   292             const QRgb rgba = *line;
       
   293             *line = qRgba(qRed(rgba), qGreen(rgba), qBlue(rgba), alpha);
       
   294         }
       
   295     }
       
   296 }
       
   297 
       
   298 } // namespace qdesigner_internal
       
   299 
       
   300 QT_END_NAMESPACE