examples/painting/painterpaths/window.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 examples 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 <QtGui>
       
    43 
       
    44 #include <math.h>
       
    45 
       
    46 #include "renderarea.h"
       
    47 #include "window.h"
       
    48 
       
    49 //! [0]
       
    50 const float Pi = 3.14159f;
       
    51 //! [0]
       
    52 
       
    53 //! [1]
       
    54 Window::Window()
       
    55 {
       
    56     QPainterPath rectPath;
       
    57     rectPath.moveTo(20.0, 30.0);
       
    58     rectPath.lineTo(80.0, 30.0);
       
    59     rectPath.lineTo(80.0, 70.0);
       
    60     rectPath.lineTo(20.0, 70.0);
       
    61     rectPath.closeSubpath();
       
    62 //! [1]
       
    63 
       
    64 //! [2]
       
    65     QPainterPath roundRectPath;
       
    66     roundRectPath.moveTo(80.0, 35.0);
       
    67     roundRectPath.arcTo(70.0, 30.0, 10.0, 10.0, 0.0, 90.0);
       
    68     roundRectPath.lineTo(25.0, 30.0);
       
    69     roundRectPath.arcTo(20.0, 30.0, 10.0, 10.0, 90.0, 90.0);
       
    70     roundRectPath.lineTo(20.0, 65.0);
       
    71     roundRectPath.arcTo(20.0, 60.0, 10.0, 10.0, 180.0, 90.0);
       
    72     roundRectPath.lineTo(75.0, 70.0);
       
    73     roundRectPath.arcTo(70.0, 60.0, 10.0, 10.0, 270.0, 90.0);
       
    74     roundRectPath.closeSubpath();
       
    75 //! [2]
       
    76 
       
    77 //! [3]
       
    78     QPainterPath ellipsePath;
       
    79     ellipsePath.moveTo(80.0, 50.0);
       
    80     ellipsePath.arcTo(20.0, 30.0, 60.0, 40.0, 0.0, 360.0);
       
    81 //! [3]
       
    82 
       
    83 //! [4]
       
    84     QPainterPath piePath;
       
    85     piePath.moveTo(50.0, 50.0);
       
    86     piePath.arcTo(20.0, 30.0, 60.0, 40.0, 60.0, 240.0);
       
    87     piePath.closeSubpath();
       
    88 //! [4]
       
    89 
       
    90 //! [5]
       
    91     QPainterPath polygonPath;
       
    92     polygonPath.moveTo(10.0, 80.0);
       
    93     polygonPath.lineTo(20.0, 10.0);
       
    94     polygonPath.lineTo(80.0, 30.0);
       
    95     polygonPath.lineTo(90.0, 70.0);
       
    96     polygonPath.closeSubpath();
       
    97 //! [5]
       
    98 
       
    99 //! [6]
       
   100     QPainterPath groupPath;
       
   101     groupPath.moveTo(60.0, 40.0);
       
   102     groupPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0);
       
   103     groupPath.moveTo(40.0, 40.0);
       
   104     groupPath.lineTo(40.0, 80.0);
       
   105     groupPath.lineTo(80.0, 80.0);
       
   106     groupPath.lineTo(80.0, 40.0);
       
   107     groupPath.closeSubpath();
       
   108 //! [6]
       
   109 
       
   110 //! [7]
       
   111     QPainterPath textPath;
       
   112     QFont timesFont("Times", 50);
       
   113     timesFont.setStyleStrategy(QFont::ForceOutline);
       
   114     textPath.addText(10, 70, timesFont, tr("Qt"));
       
   115 //! [7]
       
   116 
       
   117 //! [8]
       
   118     QPainterPath bezierPath;
       
   119     bezierPath.moveTo(20, 30);
       
   120     bezierPath.cubicTo(80, 0, 50, 50, 80, 80);
       
   121 //! [8]
       
   122 
       
   123 //! [9]
       
   124     QPainterPath starPath;
       
   125     starPath.moveTo(90, 50);
       
   126     for (int i = 1; i < 5; ++i) {
       
   127         starPath.lineTo(50 + 40 * cos(0.8 * i * Pi),
       
   128                         50 + 40 * sin(0.8 * i * Pi));
       
   129     }
       
   130     starPath.closeSubpath();
       
   131 //! [9]
       
   132 
       
   133 //! [10]
       
   134     renderAreas[0] = new RenderArea(rectPath);
       
   135     renderAreas[1] = new RenderArea(roundRectPath);
       
   136     renderAreas[2] = new RenderArea(ellipsePath);
       
   137     renderAreas[3] = new RenderArea(piePath);
       
   138     renderAreas[4] = new RenderArea(polygonPath);
       
   139     renderAreas[5] = new RenderArea(groupPath);
       
   140     renderAreas[6] = new RenderArea(textPath);
       
   141     renderAreas[7] = new RenderArea(bezierPath);
       
   142     renderAreas[8] = new RenderArea(starPath);
       
   143     Q_ASSERT(NumRenderAreas == 9);
       
   144 //! [10]
       
   145 
       
   146 //! [11]
       
   147     fillRuleComboBox = new QComboBox;
       
   148     fillRuleComboBox->addItem(tr("Odd Even"), Qt::OddEvenFill);
       
   149     fillRuleComboBox->addItem(tr("Winding"), Qt::WindingFill);
       
   150 
       
   151     fillRuleLabel = new QLabel(tr("Fill &Rule:"));
       
   152     fillRuleLabel->setBuddy(fillRuleComboBox);
       
   153 //! [11]
       
   154 
       
   155 //! [12]
       
   156     fillColor1ComboBox = new QComboBox;
       
   157     populateWithColors(fillColor1ComboBox);
       
   158     fillColor1ComboBox->setCurrentIndex(
       
   159             fillColor1ComboBox->findText("mediumslateblue"));
       
   160 
       
   161     fillColor2ComboBox = new QComboBox;
       
   162     populateWithColors(fillColor2ComboBox);
       
   163     fillColor2ComboBox->setCurrentIndex(
       
   164             fillColor2ComboBox->findText("cornsilk"));
       
   165 
       
   166     fillGradientLabel = new QLabel(tr("&Fill Gradient:"));
       
   167     fillGradientLabel->setBuddy(fillColor1ComboBox);
       
   168 
       
   169     fillToLabel = new QLabel(tr("to"));
       
   170     fillToLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
   171 
       
   172     penWidthSpinBox = new QSpinBox;
       
   173     penWidthSpinBox->setRange(0, 20);
       
   174 
       
   175     penWidthLabel = new QLabel(tr("&Pen Width:"));
       
   176     penWidthLabel->setBuddy(penWidthSpinBox);
       
   177 
       
   178     penColorComboBox = new QComboBox;
       
   179     populateWithColors(penColorComboBox);
       
   180     penColorComboBox->setCurrentIndex(
       
   181             penColorComboBox->findText("darkslateblue"));
       
   182 
       
   183     penColorLabel = new QLabel(tr("Pen &Color:"));
       
   184     penColorLabel->setBuddy(penColorComboBox);
       
   185 
       
   186     rotationAngleSpinBox = new QSpinBox;
       
   187     rotationAngleSpinBox->setRange(0, 359);
       
   188     rotationAngleSpinBox->setWrapping(true);
       
   189     rotationAngleSpinBox->setSuffix("\xB0");
       
   190 
       
   191     rotationAngleLabel = new QLabel(tr("&Rotation Angle:"));
       
   192     rotationAngleLabel->setBuddy(rotationAngleSpinBox);
       
   193 //! [12]
       
   194 
       
   195 //! [16]
       
   196     connect(fillRuleComboBox, SIGNAL(activated(int)),
       
   197             this, SLOT(fillRuleChanged()));
       
   198     connect(fillColor1ComboBox, SIGNAL(activated(int)),
       
   199             this, SLOT(fillGradientChanged()));
       
   200     connect(fillColor2ComboBox, SIGNAL(activated(int)),
       
   201             this, SLOT(fillGradientChanged()));
       
   202     connect(penColorComboBox, SIGNAL(activated(int)),
       
   203             this, SLOT(penColorChanged()));
       
   204 
       
   205     for (int i = 0; i < NumRenderAreas; ++i) {
       
   206         connect(penWidthSpinBox, SIGNAL(valueChanged(int)),
       
   207                 renderAreas[i], SLOT(setPenWidth(int)));
       
   208         connect(rotationAngleSpinBox, SIGNAL(valueChanged(int)),
       
   209                 renderAreas[i], SLOT(setRotationAngle(int)));
       
   210     }
       
   211 
       
   212 //! [16] //! [17]
       
   213     QGridLayout *topLayout = new QGridLayout;
       
   214     for (int i = 0; i < NumRenderAreas; ++i)
       
   215         topLayout->addWidget(renderAreas[i], i / 3, i % 3);
       
   216 
       
   217     QGridLayout *mainLayout = new QGridLayout;
       
   218     mainLayout->addLayout(topLayout, 0, 0, 1, 4);
       
   219     mainLayout->addWidget(fillRuleLabel, 1, 0);
       
   220     mainLayout->addWidget(fillRuleComboBox, 1, 1, 1, 3);
       
   221     mainLayout->addWidget(fillGradientLabel, 2, 0);
       
   222     mainLayout->addWidget(fillColor1ComboBox, 2, 1);
       
   223     mainLayout->addWidget(fillToLabel, 2, 2);
       
   224     mainLayout->addWidget(fillColor2ComboBox, 2, 3);
       
   225     mainLayout->addWidget(penWidthLabel, 3, 0);
       
   226     mainLayout->addWidget(penWidthSpinBox, 3, 1, 1, 3);
       
   227     mainLayout->addWidget(penColorLabel, 4, 0);
       
   228     mainLayout->addWidget(penColorComboBox, 4, 1, 1, 3);
       
   229     mainLayout->addWidget(rotationAngleLabel, 5, 0);
       
   230     mainLayout->addWidget(rotationAngleSpinBox, 5, 1, 1, 3);
       
   231     setLayout(mainLayout);
       
   232 //! [17]
       
   233 
       
   234 //! [18]
       
   235     fillRuleChanged();
       
   236     fillGradientChanged();
       
   237     penColorChanged();
       
   238     penWidthSpinBox->setValue(2);
       
   239 
       
   240     setWindowTitle(tr("Painter Paths"));
       
   241 }
       
   242 //! [18]
       
   243 
       
   244 //! [19]
       
   245 void Window::fillRuleChanged()
       
   246 {
       
   247     Qt::FillRule rule = (Qt::FillRule)currentItemData(fillRuleComboBox).toInt();
       
   248 
       
   249     for (int i = 0; i < NumRenderAreas; ++i)
       
   250         renderAreas[i]->setFillRule(rule);
       
   251 }
       
   252 //! [19]
       
   253 
       
   254 //! [20]
       
   255 void Window::fillGradientChanged()
       
   256 {
       
   257     QColor color1 = qvariant_cast<QColor>(currentItemData(fillColor1ComboBox));
       
   258     QColor color2 = qvariant_cast<QColor>(currentItemData(fillColor2ComboBox));
       
   259 
       
   260     for (int i = 0; i < NumRenderAreas; ++i)
       
   261         renderAreas[i]->setFillGradient(color1, color2);
       
   262 }
       
   263 //! [20]
       
   264 
       
   265 //! [21]
       
   266 void Window::penColorChanged()
       
   267 {
       
   268     QColor color = qvariant_cast<QColor>(currentItemData(penColorComboBox));
       
   269 
       
   270     for (int i = 0; i < NumRenderAreas; ++i)
       
   271         renderAreas[i]->setPenColor(color);
       
   272 }
       
   273 //! [21]
       
   274 
       
   275 //! [22]
       
   276 void Window::populateWithColors(QComboBox *comboBox)
       
   277 {
       
   278     QStringList colorNames = QColor::colorNames();
       
   279     foreach (QString name, colorNames)
       
   280         comboBox->addItem(name, QColor(name));
       
   281 }
       
   282 //! [22]
       
   283 
       
   284 //! [23]
       
   285 QVariant Window::currentItemData(QComboBox *comboBox)
       
   286 {
       
   287     return comboBox->itemData(comboBox->currentIndex());
       
   288 }
       
   289 //! [23]