tools/designer/src/lib/shared/qdesigner_command2.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_command2_p.h"
       
    43 #include "formwindowbase_p.h"
       
    44 #include "layoutinfo_p.h"
       
    45 #include "qdesigner_command_p.h"
       
    46 #include "widgetfactory_p.h"
       
    47 #include "qlayout_widget_p.h"
       
    48 
       
    49 #include <QtDesigner/QDesignerFormEditorInterface>
       
    50 #include <QtDesigner/QDesignerMetaDataBaseInterface>
       
    51 
       
    52 #include <QtGui/QApplication>
       
    53 #include <QtGui/QLayout>
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 namespace qdesigner_internal {
       
    58 
       
    59 MorphLayoutCommand::MorphLayoutCommand(QDesignerFormWindowInterface *formWindow) :
       
    60     QDesignerFormWindowCommand(QString(), formWindow),
       
    61     m_breakLayoutCommand(new BreakLayoutCommand(formWindow)),
       
    62     m_layoutCommand(new LayoutCommand(formWindow)),
       
    63     m_newType(LayoutInfo::VBox),
       
    64     m_layoutBase(0)
       
    65 {
       
    66 }
       
    67 
       
    68 MorphLayoutCommand::~MorphLayoutCommand()
       
    69 {
       
    70     delete m_layoutCommand;
       
    71     delete m_breakLayoutCommand;
       
    72 }
       
    73 
       
    74 bool MorphLayoutCommand::init(QWidget *w, int newType)
       
    75 {
       
    76     int oldType;
       
    77     QDesignerFormWindowInterface *fw = formWindow();
       
    78     if (!canMorph(fw, w, &oldType) || oldType == newType)
       
    79         return false;
       
    80     m_layoutBase = w;
       
    81     m_newType = newType;
       
    82     // Find all managed widgets
       
    83     m_widgets.clear();
       
    84     const QLayout *layout = LayoutInfo::managedLayout(fw->core(), w);
       
    85     const int count = layout->count();
       
    86     for (int i = 0; i < count ; i++) {
       
    87         if (QWidget *w = layout->itemAt(i)->widget())
       
    88             if (fw->isManaged(w))
       
    89                 m_widgets.push_back(w);
       
    90     }
       
    91     const bool reparentLayoutWidget = false; // leave QLayoutWidget intact
       
    92     m_breakLayoutCommand->init(m_widgets, m_layoutBase, reparentLayoutWidget);
       
    93     m_layoutCommand->init(m_layoutBase, m_widgets, static_cast<LayoutInfo::Type>(m_newType), m_layoutBase, reparentLayoutWidget);
       
    94     setText(formatDescription(core(), m_layoutBase, oldType, newType));
       
    95     return true;
       
    96 }
       
    97 
       
    98 bool MorphLayoutCommand::canMorph(const QDesignerFormWindowInterface *formWindow, QWidget *w, int *ptrToCurrentType)
       
    99 {
       
   100     if (ptrToCurrentType)
       
   101         *ptrToCurrentType = LayoutInfo::NoLayout;
       
   102     // We want a managed widget or a container page
       
   103     // with a level-0 managed layout
       
   104     QDesignerFormEditorInterface *core = formWindow->core();
       
   105     QLayout *layout = LayoutInfo::managedLayout(core, w);
       
   106     if (!layout)
       
   107         return false;
       
   108     const LayoutInfo::Type type = LayoutInfo::layoutType(core, layout);
       
   109     if (ptrToCurrentType)
       
   110         *ptrToCurrentType = type;
       
   111     switch (type) {
       
   112     case LayoutInfo::HBox:
       
   113     case LayoutInfo::VBox:
       
   114     case LayoutInfo::Grid:
       
   115     case LayoutInfo::Form:
       
   116         return true;
       
   117         break;
       
   118     case LayoutInfo::NoLayout:
       
   119     case LayoutInfo::HSplitter: // Nothing doing
       
   120     case LayoutInfo::VSplitter:
       
   121     case LayoutInfo::UnknownLayout:
       
   122         break;
       
   123     }
       
   124     return false;
       
   125 }
       
   126 
       
   127 void MorphLayoutCommand::redo()
       
   128 {
       
   129     m_breakLayoutCommand->redo();
       
   130     m_layoutCommand->redo();
       
   131     /* Transfer applicable properties which is a cross-section of the modified
       
   132      * properties except object name. */
       
   133     if (const LayoutProperties *properties = m_breakLayoutCommand->layoutProperties()) {
       
   134         const int oldMask = m_breakLayoutCommand->propertyMask();
       
   135         QLayout *newLayout = LayoutInfo::managedLayout(core(), m_layoutBase);
       
   136         const int newMask = LayoutProperties::visibleProperties(newLayout);
       
   137         const int applicableMask = (oldMask & newMask) & ~LayoutProperties::ObjectNameProperty;
       
   138         if (applicableMask)
       
   139             properties->toPropertySheet(core(), newLayout, applicableMask);
       
   140     }
       
   141 }
       
   142 
       
   143 void MorphLayoutCommand::undo()
       
   144 {
       
   145     m_layoutCommand->undo();
       
   146     m_breakLayoutCommand->undo();
       
   147 }
       
   148 
       
   149 QString MorphLayoutCommand::formatDescription(QDesignerFormEditorInterface * /* core*/, const QWidget *w, int oldType, int newType)
       
   150 {
       
   151     const QString oldName = LayoutInfo::layoutName(static_cast<LayoutInfo::Type>(oldType));
       
   152     const QString newName = LayoutInfo::layoutName(static_cast<LayoutInfo::Type>(newType));
       
   153     const QString widgetName = qobject_cast<const QLayoutWidget*>(w) ? w->layout()->objectName() : w->objectName();
       
   154     return QApplication::translate("Command", "Change layout of '%1' from %2 to %3").arg(widgetName, oldName, newName);
       
   155 }
       
   156 
       
   157 } // namespace qdesigner_internal
       
   158 
       
   159 QT_END_NAMESPACE