tools/designer/src/components/widgetbox/widgetbox.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.h"
       
    43 #include "widgetboxtreewidget.h"
       
    44 #include "widgetbox_dnditem.h"
       
    45 
       
    46 #include <QtDesigner/QDesignerFormEditorInterface>
       
    47 #include <QtDesigner/QDesignerFormWindowManagerInterface>
       
    48 
       
    49 #include <iconloader_p.h>
       
    50 #include <qdesigner_utils_p.h>
       
    51 #include <filterwidget_p.h>
       
    52 
       
    53 #include <QtGui/QDropEvent>
       
    54 #include <QtGui/QVBoxLayout>
       
    55 #include <QtGui/QApplication>
       
    56 #include <QtGui/QToolBar>
       
    57 #include <QtGui/QIcon>
       
    58 
       
    59 QT_BEGIN_NAMESPACE
       
    60 
       
    61 namespace qdesigner_internal {
       
    62 
       
    63 WidgetBox::WidgetBox(QDesignerFormEditorInterface *core, QWidget *parent, Qt::WindowFlags flags)
       
    64     : QDesignerWidgetBox(parent, flags),
       
    65       m_core(core),
       
    66       m_view(new WidgetBoxTreeWidget(m_core))
       
    67 {
       
    68 
       
    69     QVBoxLayout *l = new QVBoxLayout(this);
       
    70     l->setMargin(0);
       
    71     l->setSpacing(0);
       
    72 
       
    73     // Prevent the filter from grabbing focus since Our view has Qt::NoFocus
       
    74     FilterWidget *filterWidget = new FilterWidget(0, FilterWidget::LayoutAlignNone);
       
    75     filterWidget->setRefuseFocus(true);
       
    76     connect(filterWidget, SIGNAL(filterChanged(QString)), m_view, SLOT(filter(QString)));
       
    77     l->addWidget(filterWidget);
       
    78 
       
    79     // View
       
    80     connect(m_view, SIGNAL(pressed(QString,QString,QPoint)),
       
    81             this, SLOT(handleMousePress(QString,QString,QPoint)));
       
    82     l->addWidget(m_view);
       
    83 
       
    84     setAcceptDrops (true);
       
    85 }
       
    86 
       
    87 WidgetBox::~WidgetBox()
       
    88 {
       
    89 }
       
    90 
       
    91 QDesignerFormEditorInterface *WidgetBox::core() const
       
    92 {
       
    93     return m_core;
       
    94 }
       
    95 
       
    96 void WidgetBox::handleMousePress(const QString &name, const QString &xml, const QPoint &global_mouse_pos)
       
    97 {
       
    98     if (QApplication::mouseButtons() != Qt::LeftButton)
       
    99         return;
       
   100 
       
   101     DomUI *ui = xmlToUi(name, xml, true);
       
   102     if (ui == 0)
       
   103         return;
       
   104     QList<QDesignerDnDItemInterface*> item_list;
       
   105     item_list.append(new WidgetBoxDnDItem(core(), ui, global_mouse_pos));
       
   106     m_core->formWindowManager()->dragItems(item_list);
       
   107 }
       
   108 
       
   109 int WidgetBox::categoryCount() const
       
   110 {
       
   111     return m_view->categoryCount();
       
   112 }
       
   113 
       
   114 QDesignerWidgetBoxInterface::Category WidgetBox::category(int cat_idx) const
       
   115 {
       
   116     return m_view->category(cat_idx);
       
   117 }
       
   118 
       
   119 void WidgetBox::addCategory(const Category &cat)
       
   120 {
       
   121     m_view->addCategory(cat);
       
   122 }
       
   123 
       
   124 void WidgetBox::removeCategory(int cat_idx)
       
   125 {
       
   126     m_view->removeCategory(cat_idx);
       
   127 }
       
   128 
       
   129 int WidgetBox::widgetCount(int cat_idx) const
       
   130 {
       
   131     return m_view->widgetCount(cat_idx);
       
   132 }
       
   133 
       
   134 QDesignerWidgetBoxInterface::Widget WidgetBox::widget(int cat_idx, int wgt_idx) const
       
   135 {
       
   136     return m_view->widget(cat_idx, wgt_idx);
       
   137 }
       
   138 
       
   139 void WidgetBox::addWidget(int cat_idx, const Widget &wgt)
       
   140 {
       
   141     m_view->addWidget(cat_idx, wgt);
       
   142 }
       
   143 
       
   144 void WidgetBox::removeWidget(int cat_idx, int wgt_idx)
       
   145 {
       
   146     m_view->removeWidget(cat_idx, wgt_idx);
       
   147 }
       
   148 
       
   149 void WidgetBox::dropWidgets(const QList<QDesignerDnDItemInterface*> &item_list, const QPoint&)
       
   150 {
       
   151     m_view->dropWidgets(item_list);
       
   152 }
       
   153 
       
   154 void WidgetBox::setFileName(const QString &file_name)
       
   155 {
       
   156     m_view->setFileName(file_name);
       
   157 }
       
   158 
       
   159 QString WidgetBox::fileName() const
       
   160 {
       
   161     return m_view->fileName();
       
   162 }
       
   163 
       
   164 bool WidgetBox::load()
       
   165 {
       
   166     return m_view->load(loadMode());
       
   167 }
       
   168 
       
   169 bool WidgetBox::loadContents(const QString &contents)
       
   170 {
       
   171     return m_view->loadContents(contents);
       
   172 }
       
   173 
       
   174 bool WidgetBox::save()
       
   175 {
       
   176     return m_view->save();
       
   177 }
       
   178 
       
   179 static const QDesignerMimeData *checkDragEvent(QDropEvent * event,
       
   180                                                bool acceptEventsFromWidgetBox)
       
   181 {
       
   182     const QDesignerMimeData *mimeData = qobject_cast<const QDesignerMimeData *>(event->mimeData());
       
   183     if (!mimeData) {
       
   184         event->ignore();
       
   185         return 0;
       
   186     }
       
   187     // If desired, ignore a widget box drag and drop, where widget==0.
       
   188     if (!acceptEventsFromWidgetBox) {
       
   189         const bool fromWidgetBox = !mimeData->items().first()->widget();
       
   190         if (fromWidgetBox) {
       
   191             event->ignore();
       
   192             return 0;
       
   193         }
       
   194     }
       
   195 
       
   196     mimeData->acceptEvent(event);
       
   197     return mimeData;
       
   198 }
       
   199 
       
   200 void WidgetBox::dragEnterEvent (QDragEnterEvent * event)
       
   201 {
       
   202     // We accept event originating from the widget box also here,
       
   203     // because otherwise Windows will not show the DnD pixmap.
       
   204     checkDragEvent(event, true);
       
   205 }
       
   206 
       
   207 void WidgetBox::dragMoveEvent(QDragMoveEvent * event)
       
   208 {
       
   209     checkDragEvent(event, true);
       
   210 }
       
   211 
       
   212 void WidgetBox::dropEvent(QDropEvent * event)
       
   213 {
       
   214     const QDesignerMimeData *mimeData = checkDragEvent(event, false);
       
   215     if (!mimeData)
       
   216         return;
       
   217 
       
   218     dropWidgets(mimeData->items(), event->pos());
       
   219     QDesignerMimeData::removeMovedWidgetsFromSourceForm(mimeData->items());
       
   220 }
       
   221 
       
   222 QIcon WidgetBox::iconForWidget(const QString &className, const QString &category) const
       
   223 {
       
   224     Widget widgetData;
       
   225     if (!findWidget(this, className, category, &widgetData))
       
   226         return QIcon();
       
   227     return m_view->iconForWidget(widgetData.iconName());
       
   228 }
       
   229 
       
   230 }  // namespace qdesigner_internal
       
   231 
       
   232 QT_END_NAMESPACE