tools/designer/src/lib/shared/spacer_widget.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 "spacer_widget_p.h"
       
    43 #include "layoutinfo_p.h"
       
    44 
       
    45 #include <QtDesigner/abstractformwindow.h>
       
    46 #include <QtDesigner/QDesignerFormWindowInterface>
       
    47 #include <QtDesigner/QDesignerFormEditorInterface>
       
    48 #include <QtDesigner/QDesignerPropertySheetExtension>
       
    49 #include <QtDesigner/QExtensionManager>
       
    50 
       
    51 #include <QtGui/QLayout>
       
    52 #include <QtGui/QPainter>
       
    53 #include <QtGui/qevent.h>
       
    54 #include <QtCore/qdebug.h>
       
    55 
       
    56 QT_BEGIN_NAMESPACE
       
    57 
       
    58 // The Spacer widget is Designer representation of  QLayoutItem.
       
    59 // It uses QLayoutItem's sizeHint property as QWidget
       
    60 // sizeHint and the QLayoutItem's sizeType property as QWidget size policy.
       
    61 // If it is not within a layout, it adds a margin (m_SizeOffset) around it
       
    62 // to avoid being shrunk to an invisible state when the sizeHint is reset to 0,0
       
    63 // and enables sizeHandle-resizing. In a layout, however, this m_SizeOffset
       
    64 // should not be applied for  pixel-exact design.
       
    65 
       
    66 Spacer::Spacer(QWidget *parent) :
       
    67     QWidget(parent),
       
    68     m_SizeOffset(3, 3), // A small offset to ensure the spacer is still visible when reset to size 0,0
       
    69     m_orientation(Qt::Vertical),
       
    70     m_interactive(true),
       
    71     m_layoutState(UnknownLayoutState),
       
    72     m_sizeHint(0, 0)
       
    73 {
       
    74     setAttribute(Qt::WA_MouseNoMask);
       
    75     m_formWindow = QDesignerFormWindowInterface::findFormWindow(this);
       
    76     setSizeType(QSizePolicy::Expanding);
       
    77 }
       
    78 
       
    79 bool Spacer::event(QEvent *e)
       
    80 {
       
    81     switch (e->type()) {
       
    82     case QEvent::ToolTip:
       
    83         updateToolTip(); // Tooltip includes size, so, refresh on demand
       
    84         break;
       
    85     case QEvent::ParentChange: // Cache information about 'being in layout' which is expensive to calculate.
       
    86         m_layoutState = UnknownLayoutState;
       
    87         break;
       
    88     default:
       
    89         break;
       
    90     }
       
    91     return QWidget::event(e);
       
    92 }
       
    93 
       
    94 bool Spacer::isInLayout() const
       
    95 {
       
    96     if (m_layoutState == UnknownLayoutState) {
       
    97         m_layoutState = OutsideLayout;
       
    98         if (m_formWindow)
       
    99             if (const QWidget *parent = parentWidget())
       
   100                 if (qdesigner_internal::LayoutInfo::managedLayoutType(m_formWindow->core(), parent) != qdesigner_internal::LayoutInfo::NoLayout)
       
   101                     m_layoutState = InLayout;
       
   102     }
       
   103     return m_layoutState == InLayout;
       
   104 }
       
   105 
       
   106 void Spacer::paintEvent(QPaintEvent *)
       
   107 {
       
   108     // Only draw spacers when we're editting widgets
       
   109     if (m_formWindow != 0 && m_formWindow->currentTool() != 0)
       
   110         return;
       
   111 
       
   112     QPainter p(this);
       
   113     p.setPen(Qt::blue);
       
   114     const int w = width();
       
   115     const int h = height();
       
   116     if (w * h == 0)
       
   117         return;
       
   118 
       
   119     if (w <= m_SizeOffset.width() || h <= m_SizeOffset.height()) {
       
   120         const int lw = w - 1;
       
   121         const int lh = h - 1;
       
   122         switch (m_orientation) {
       
   123         case Qt::Horizontal:
       
   124             p.drawLine(0,  0, 0,  lh);
       
   125             p.drawLine(lw, 0, lw, lh);
       
   126             break;
       
   127         case Qt::Vertical:
       
   128             p.drawLine(0, 0,  lw, 0);
       
   129             p.drawLine(0, lh, lw, lh);
       
   130             break;
       
   131         }
       
   132         return;
       
   133     }
       
   134     if (m_orientation == Qt::Horizontal) {
       
   135         const int dist = 3;
       
   136         const int amplitude = qMin(3, h / 3);
       
   137         const int base = h / 2;
       
   138         int i = 0;
       
   139         p.setPen(Qt::white);
       
   140         for (i = 0; i < w / 3 +2; ++i)
       
   141             p.drawLine(i * dist, base - amplitude, i * dist + dist / 2, base + amplitude);
       
   142         p.setPen(Qt::blue);
       
   143         for (i = 0; i < w / 3 +2; ++i)
       
   144             p.drawLine(i * dist + dist / 2, base + amplitude, i * dist + dist, base - amplitude);
       
   145         const int y = h/2;
       
   146         p.drawLine(0, y-10, 0, y+10);
       
   147         p.drawLine(w - 1, y-10, w - 1, y+10);
       
   148     } else {
       
   149         const int dist = 3;
       
   150         const int amplitude = qMin(3, w / 3);
       
   151         const int base = w / 2;
       
   152         int i = 0;
       
   153         p.setPen(Qt::white);
       
   154         for (i = 0; i < h / 3 +2; ++i)
       
   155             p.drawLine(base - amplitude, i * dist, base + amplitude,i * dist + dist / 2);
       
   156         p.setPen(Qt::blue);
       
   157         for (i = 0; i < h / 3 +2; ++i)
       
   158             p.drawLine(base + amplitude, i * dist + dist / 2, base - amplitude, i * dist + dist);
       
   159         const int x = w/2;
       
   160         p.drawLine(x-10, 0, x+10, 0);
       
   161         p.drawLine(x-10, h - 1, x+10, h - 1);
       
   162     }
       
   163 }
       
   164 
       
   165 void Spacer::resizeEvent(QResizeEvent* e)
       
   166 {
       
   167     QWidget::resizeEvent(e);
       
   168     // When resized by widget handle dragging after a reset (QSize(0, 0)):
       
   169     // Mark the property as changed (geometry and sizeHint are in sync except for 'changed').
       
   170     if (m_formWindow) {
       
   171         const QSize oldSize = e->oldSize();
       
   172         if (oldSize.isNull() || oldSize.width() <= m_SizeOffset.width() || oldSize.height() <= m_SizeOffset.height())
       
   173             if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(m_formWindow->core()->extensionManager(), this))
       
   174                 sheet->setChanged(sheet->indexOf(QLatin1String("sizeHint")), true);
       
   175     }
       
   176 
       
   177     updateMask();
       
   178 
       
   179     if (!m_interactive)
       
   180         return;
       
   181 
       
   182     if (!isInLayout()) { // Allow size-handle resize only if not in layout
       
   183         const QSize currentSize = size();
       
   184         if (currentSize.width() >= m_SizeOffset.width() && currentSize.height() >= m_SizeOffset.height())
       
   185             m_sizeHint = currentSize - m_SizeOffset;
       
   186     }
       
   187 }
       
   188 
       
   189 void Spacer::updateMask()
       
   190 {
       
   191     QRegion r(rect());
       
   192     const int w = width();
       
   193     const int h = height();
       
   194     if (w > 1 && h > 1) {
       
   195         if (m_orientation == Qt::Horizontal) {
       
   196             const int amplitude = qMin(3, h / 3);
       
   197             const int base = h / 2;
       
   198             r = r.subtract(QRect(1, 0, w - 2, base - amplitude));
       
   199             r = r.subtract(QRect(1, base + amplitude, w - 2, h - base - amplitude));
       
   200         } else {
       
   201             const int amplitude = qMin(3, w / 3);
       
   202             const int base = w / 2;
       
   203             r = r.subtract(QRect(0, 1, base - amplitude, h - 2));
       
   204             r = r.subtract(QRect(base + amplitude, 1, w - base - amplitude, h - 2));
       
   205         }
       
   206     }
       
   207     setMask(r);
       
   208 }
       
   209 
       
   210 void Spacer::setSizeType(QSizePolicy::Policy t)
       
   211 {
       
   212     const QSizePolicy sizeP = m_orientation == Qt::Vertical ? QSizePolicy(QSizePolicy::Minimum, t) : QSizePolicy(t, QSizePolicy::Minimum);
       
   213     setSizePolicy(sizeP);
       
   214 }
       
   215 
       
   216 
       
   217 QSizePolicy::Policy Spacer::sizeType() const
       
   218 {
       
   219     return m_orientation == Qt::Vertical ? sizePolicy().verticalPolicy() : sizePolicy().horizontalPolicy();
       
   220 }
       
   221 
       
   222 Qt::Alignment Spacer::alignment() const
       
   223 {
       
   224     // For grid layouts
       
   225     return m_orientation == Qt::Vertical ?  Qt::AlignHCenter : Qt::AlignVCenter;
       
   226 }
       
   227 
       
   228 QSize Spacer::sizeHint() const
       
   229 {
       
   230     return isInLayout() ? m_sizeHint : m_sizeHint + m_SizeOffset;
       
   231 }
       
   232 
       
   233 QSize Spacer::sizeHintProperty() const
       
   234 {
       
   235     return m_sizeHint;
       
   236 }
       
   237 
       
   238 void Spacer::setSizeHintProperty(const QSize &s)
       
   239 {
       
   240     m_sizeHint = s;
       
   241 
       
   242     if (!isInLayout()) // Visible resize only if not in layout
       
   243         resize(s + m_SizeOffset);
       
   244 
       
   245     updateGeometry();
       
   246 }
       
   247 
       
   248 Qt::Orientation Spacer::orientation() const
       
   249 {
       
   250     return m_orientation;
       
   251 }
       
   252 
       
   253 void Spacer::setOrientation(Qt::Orientation o)
       
   254 {
       
   255     if (m_orientation == o)
       
   256         return;
       
   257 
       
   258     const QSizePolicy::Policy st = sizeType(); // flip size type
       
   259     m_orientation = o;
       
   260     setSizeType(st);
       
   261 
       
   262     if (m_interactive) {
       
   263         m_sizeHint = QSize(m_sizeHint.height(), m_sizeHint.width());
       
   264         if (!isInLayout())
       
   265             resize(m_sizeHint + m_SizeOffset);
       
   266     }
       
   267 
       
   268     updateMask();
       
   269     update();
       
   270     updateGeometry();
       
   271 }
       
   272 
       
   273 void Spacer::updateToolTip()
       
   274 {
       
   275     const QString format = m_orientation == Qt::Horizontal ? tr("Horizontal Spacer '%1', %2 x %3") : tr("Vertical Spacer '%1', %2 x %3");
       
   276     QString msg = format.arg(objectName()).arg(m_sizeHint.width()).arg(m_sizeHint.height());
       
   277     setToolTip(msg);
       
   278 }
       
   279 
       
   280 QT_END_NAMESPACE