examples/painting/basicdrawing/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 "renderarea.h"
       
    45 #include "window.h"
       
    46 
       
    47 //! [0]
       
    48 const int IdRole = Qt::UserRole;
       
    49 //! [0]
       
    50 
       
    51 //! [1]
       
    52 Window::Window()
       
    53 {
       
    54     renderArea = new RenderArea;
       
    55 
       
    56     shapeComboBox = new QComboBox;
       
    57     shapeComboBox->addItem(tr("Polygon"), RenderArea::Polygon);
       
    58     shapeComboBox->addItem(tr("Rectangle"), RenderArea::Rect);
       
    59     shapeComboBox->addItem(tr("Rounded Rectangle"), RenderArea::RoundedRect);
       
    60     shapeComboBox->addItem(tr("Ellipse"), RenderArea::Ellipse);
       
    61     shapeComboBox->addItem(tr("Pie"), RenderArea::Pie);
       
    62     shapeComboBox->addItem(tr("Chord"), RenderArea::Chord);
       
    63     shapeComboBox->addItem(tr("Path"), RenderArea::Path);
       
    64     shapeComboBox->addItem(tr("Line"), RenderArea::Line);
       
    65     shapeComboBox->addItem(tr("Polyline"), RenderArea::Polyline);
       
    66     shapeComboBox->addItem(tr("Arc"), RenderArea::Arc);
       
    67     shapeComboBox->addItem(tr("Points"), RenderArea::Points);
       
    68     shapeComboBox->addItem(tr("Text"), RenderArea::Text);
       
    69     shapeComboBox->addItem(tr("Pixmap"), RenderArea::Pixmap);
       
    70 
       
    71     shapeLabel = new QLabel(tr("&Shape:"));
       
    72     shapeLabel->setBuddy(shapeComboBox);
       
    73 //! [1]
       
    74 
       
    75 //! [2]
       
    76     penWidthSpinBox = new QSpinBox;
       
    77     penWidthSpinBox->setRange(0, 20);
       
    78     penWidthSpinBox->setSpecialValueText(tr("0 (cosmetic pen)"));
       
    79 
       
    80     penWidthLabel = new QLabel(tr("Pen &Width:"));
       
    81     penWidthLabel->setBuddy(penWidthSpinBox);
       
    82 //! [2]
       
    83 
       
    84 //! [3]
       
    85     penStyleComboBox = new QComboBox;
       
    86     penStyleComboBox->addItem(tr("Solid"), Qt::SolidLine);
       
    87     penStyleComboBox->addItem(tr("Dash"), Qt::DashLine);
       
    88     penStyleComboBox->addItem(tr("Dot"), Qt::DotLine);
       
    89     penStyleComboBox->addItem(tr("Dash Dot"), Qt::DashDotLine);
       
    90     penStyleComboBox->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine);
       
    91     penStyleComboBox->addItem(tr("None"), Qt::NoPen);
       
    92 
       
    93     penStyleLabel = new QLabel(tr("&Pen Style:"));
       
    94     penStyleLabel->setBuddy(penStyleComboBox);
       
    95 
       
    96     penCapComboBox = new QComboBox;
       
    97     penCapComboBox->addItem(tr("Flat"), Qt::FlatCap);
       
    98     penCapComboBox->addItem(tr("Square"), Qt::SquareCap);
       
    99     penCapComboBox->addItem(tr("Round"), Qt::RoundCap);
       
   100 
       
   101     penCapLabel = new QLabel(tr("Pen &Cap:"));
       
   102     penCapLabel->setBuddy(penCapComboBox);
       
   103 
       
   104     penJoinComboBox = new QComboBox;
       
   105     penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin);
       
   106     penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin);
       
   107     penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin);
       
   108 
       
   109     penJoinLabel = new QLabel(tr("Pen &Join:"));
       
   110     penJoinLabel->setBuddy(penJoinComboBox);
       
   111 //! [3]
       
   112 
       
   113 //! [4]
       
   114     brushStyleComboBox = new QComboBox;
       
   115     brushStyleComboBox->addItem(tr("Linear Gradient"),
       
   116             Qt::LinearGradientPattern);
       
   117     brushStyleComboBox->addItem(tr("Radial Gradient"),
       
   118             Qt::RadialGradientPattern);
       
   119     brushStyleComboBox->addItem(tr("Conical Gradient"),
       
   120             Qt::ConicalGradientPattern);
       
   121     brushStyleComboBox->addItem(tr("Texture"), Qt::TexturePattern);
       
   122     brushStyleComboBox->addItem(tr("Solid"), Qt::SolidPattern);
       
   123     brushStyleComboBox->addItem(tr("Horizontal"), Qt::HorPattern);
       
   124     brushStyleComboBox->addItem(tr("Vertical"), Qt::VerPattern);
       
   125     brushStyleComboBox->addItem(tr("Cross"), Qt::CrossPattern);
       
   126     brushStyleComboBox->addItem(tr("Backward Diagonal"), Qt::BDiagPattern);
       
   127     brushStyleComboBox->addItem(tr("Forward Diagonal"), Qt::FDiagPattern);
       
   128     brushStyleComboBox->addItem(tr("Diagonal Cross"), Qt::DiagCrossPattern);
       
   129     brushStyleComboBox->addItem(tr("Dense 1"), Qt::Dense1Pattern);
       
   130     brushStyleComboBox->addItem(tr("Dense 2"), Qt::Dense2Pattern);
       
   131     brushStyleComboBox->addItem(tr("Dense 3"), Qt::Dense3Pattern);
       
   132     brushStyleComboBox->addItem(tr("Dense 4"), Qt::Dense4Pattern);
       
   133     brushStyleComboBox->addItem(tr("Dense 5"), Qt::Dense5Pattern);
       
   134     brushStyleComboBox->addItem(tr("Dense 6"), Qt::Dense6Pattern);
       
   135     brushStyleComboBox->addItem(tr("Dense 7"), Qt::Dense7Pattern);
       
   136     brushStyleComboBox->addItem(tr("None"), Qt::NoBrush);
       
   137 
       
   138     brushStyleLabel = new QLabel(tr("&Brush Style:"));
       
   139     brushStyleLabel->setBuddy(brushStyleComboBox);
       
   140 //! [4]
       
   141 
       
   142 //! [5]
       
   143     otherOptionsLabel = new QLabel(tr("Other Options:"));
       
   144 //! [5] //! [6]
       
   145     antialiasingCheckBox = new QCheckBox(tr("&Antialiasing"));
       
   146 //! [6] //! [7]
       
   147     transformationsCheckBox = new QCheckBox(tr("&Transformations"));
       
   148 //! [7]
       
   149 
       
   150 //! [8]
       
   151     connect(shapeComboBox, SIGNAL(activated(int)),
       
   152             this, SLOT(shapeChanged()));
       
   153     connect(penWidthSpinBox, SIGNAL(valueChanged(int)),
       
   154             this, SLOT(penChanged()));
       
   155     connect(penStyleComboBox, SIGNAL(activated(int)),
       
   156             this, SLOT(penChanged()));
       
   157     connect(penCapComboBox, SIGNAL(activated(int)),
       
   158             this, SLOT(penChanged()));
       
   159     connect(penJoinComboBox, SIGNAL(activated(int)),
       
   160             this, SLOT(penChanged()));
       
   161     connect(brushStyleComboBox, SIGNAL(activated(int)),
       
   162             this, SLOT(brushChanged()));
       
   163     connect(antialiasingCheckBox, SIGNAL(toggled(bool)),
       
   164             renderArea, SLOT(setAntialiased(bool)));
       
   165     connect(transformationsCheckBox, SIGNAL(toggled(bool)),
       
   166             renderArea, SLOT(setTransformed(bool)));
       
   167 //! [8]
       
   168 
       
   169 //! [9]
       
   170     QGridLayout *mainLayout = new QGridLayout;
       
   171 //! [9] //! [10]
       
   172     mainLayout->setColumnStretch(0, 1);
       
   173     mainLayout->setColumnStretch(3, 1);
       
   174     mainLayout->addWidget(renderArea, 0, 0, 1, 4);
       
   175     mainLayout->setRowMinimumHeight(1, 6);
       
   176     mainLayout->addWidget(shapeLabel, 2, 1, Qt::AlignRight);
       
   177     mainLayout->addWidget(shapeComboBox, 2, 2);
       
   178     mainLayout->addWidget(penWidthLabel, 3, 1, Qt::AlignRight);
       
   179     mainLayout->addWidget(penWidthSpinBox, 3, 2);
       
   180     mainLayout->addWidget(penStyleLabel, 4, 1, Qt::AlignRight);
       
   181     mainLayout->addWidget(penStyleComboBox, 4, 2);
       
   182     mainLayout->addWidget(penCapLabel, 5, 1, Qt::AlignRight);
       
   183     mainLayout->addWidget(penCapComboBox, 5, 2);
       
   184     mainLayout->addWidget(penJoinLabel, 6, 1, Qt::AlignRight);
       
   185     mainLayout->addWidget(penJoinComboBox, 6, 2);
       
   186     mainLayout->addWidget(brushStyleLabel, 7, 1, Qt::AlignRight);
       
   187     mainLayout->addWidget(brushStyleComboBox, 7, 2);
       
   188     mainLayout->setRowMinimumHeight(8, 6);
       
   189     mainLayout->addWidget(otherOptionsLabel, 9, 1, Qt::AlignRight);
       
   190     mainLayout->addWidget(antialiasingCheckBox, 9, 2);
       
   191     mainLayout->addWidget(transformationsCheckBox, 10, 2);
       
   192     setLayout(mainLayout);
       
   193 
       
   194     shapeChanged();
       
   195     penChanged();
       
   196     brushChanged();
       
   197     antialiasingCheckBox->setChecked(true);
       
   198 
       
   199     setWindowTitle(tr("Basic Drawing"));
       
   200 }
       
   201 //! [10]
       
   202 
       
   203 //! [11]
       
   204 void Window::shapeChanged()
       
   205 {
       
   206     RenderArea::Shape shape = RenderArea::Shape(shapeComboBox->itemData(
       
   207             shapeComboBox->currentIndex(), IdRole).toInt());
       
   208     renderArea->setShape(shape);
       
   209 }
       
   210 //! [11]
       
   211 
       
   212 //! [12]
       
   213 void Window::penChanged()
       
   214 {
       
   215     int width = penWidthSpinBox->value();
       
   216     Qt::PenStyle style = Qt::PenStyle(penStyleComboBox->itemData(
       
   217             penStyleComboBox->currentIndex(), IdRole).toInt());
       
   218     Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData(
       
   219             penCapComboBox->currentIndex(), IdRole).toInt());
       
   220     Qt::PenJoinStyle join = Qt::PenJoinStyle(penJoinComboBox->itemData(
       
   221             penJoinComboBox->currentIndex(), IdRole).toInt());
       
   222 
       
   223     renderArea->setPen(QPen(Qt::blue, width, style, cap, join));
       
   224 }
       
   225 //! [12]
       
   226 
       
   227 //! [13]
       
   228 void Window::brushChanged()
       
   229 {
       
   230     Qt::BrushStyle style = Qt::BrushStyle(brushStyleComboBox->itemData(
       
   231 //! [13]
       
   232             brushStyleComboBox->currentIndex(), IdRole).toInt());
       
   233 
       
   234 //! [14]
       
   235     if (style == Qt::LinearGradientPattern) {
       
   236         QLinearGradient linearGradient(0, 0, 100, 100);
       
   237         linearGradient.setColorAt(0.0, Qt::white);
       
   238         linearGradient.setColorAt(0.2, Qt::green);
       
   239         linearGradient.setColorAt(1.0, Qt::black);
       
   240         renderArea->setBrush(linearGradient);
       
   241 //! [14] //! [15]
       
   242     } else if (style == Qt::RadialGradientPattern) {
       
   243         QRadialGradient radialGradient(50, 50, 50, 70, 70);
       
   244         radialGradient.setColorAt(0.0, Qt::white);
       
   245         radialGradient.setColorAt(0.2, Qt::green);
       
   246         radialGradient.setColorAt(1.0, Qt::black);
       
   247         renderArea->setBrush(radialGradient);
       
   248     } else if (style == Qt::ConicalGradientPattern) {
       
   249         QConicalGradient conicalGradient(50, 50, 150);
       
   250         conicalGradient.setColorAt(0.0, Qt::white);
       
   251         conicalGradient.setColorAt(0.2, Qt::green);
       
   252         conicalGradient.setColorAt(1.0, Qt::black);
       
   253         renderArea->setBrush(conicalGradient);
       
   254 //! [15] //! [16]
       
   255     } else if (style == Qt::TexturePattern) {
       
   256         renderArea->setBrush(QBrush(QPixmap(":/images/brick.png")));
       
   257 //! [16] //! [17]
       
   258     } else {
       
   259         renderArea->setBrush(QBrush(Qt::green, style));
       
   260     }
       
   261 }
       
   262 //! [17]