1   #include "renderarea.h"
2   #include "mainwindow.h"
3   #include <QtWidgets>

4   const int IdRole = Qt::UserRole;


5   MainWindow::MainWindow()
6   {
7       renderArea = new RenderArea;

8       shapeComboBox = new QComboBox;
9       shapeComboBox->addItem(tr("Polygon"), RenderArea::Polygon);
10      shapeComboBox->addItem(tr("Rectangle"), RenderArea::Rect);
11      shapeComboBox->addItem(tr("Rounded Rectangle"), RenderArea::RoundedRect);
12      shapeComboBox->addItem(tr("Ellipse"), RenderArea::Ellipse);
13      shapeComboBox->addItem(tr("Pie"), RenderArea::Pie);
14      shapeComboBox->addItem(tr("Chord"), RenderArea::Chord);
15      shapeComboBox->addItem(tr("Path"), RenderArea::Path);
16      shapeComboBox->addItem(tr("Line"), RenderArea::Line);
17      shapeComboBox->addItem(tr("Polyline"), RenderArea::Polyline);
18      shapeComboBox->addItem(tr("Arc"), RenderArea::Arc);
19      shapeComboBox->addItem(tr("Points"), RenderArea::Points);
20      shapeComboBox->addItem(tr("Text"), RenderArea::Text);
21      shapeComboBox->addItem(tr("Pixmap"), RenderArea::Pixmap);

22      shapeLabel = new QLabel(tr("&Shape:"));
23      shapeLabel->setBuddy(shapeComboBox);



24      penWidthSpinBox = new QSpinBox;
25      penWidthSpinBox->setRange(0, 20);
26      penWidthSpinBox->setSpecialValueText(tr("0 (cosmetic pen)"));

27      penWidthLabel = new QLabel(tr("Pen &Width:"));
28      penWidthLabel->setBuddy(penWidthSpinBox);


29      penStyleComboBox = new QComboBox;
30      penStyleComboBox->addItem(tr("Solid"), static_cast<int>(Qt::SolidLine));
31      penStyleComboBox->addItem(tr("Dash"), static_cast<int>(Qt::DashLine));
32      penStyleComboBox->addItem(tr("Dot"), static_cast<int>(Qt::DotLine));
33      penStyleComboBox->addItem(tr("Dash Dot"), static_cast<int>(Qt::DashDotLine));
34      penStyleComboBox->addItem(tr("Dash Dot Dot"), static_cast<int>(Qt::DashDotDotLine));
35      penStyleComboBox->addItem(tr("None"), static_cast<int>(Qt::NoPen));

36      penStyleLabel = new QLabel(tr("&Pen Style:"));
37      penStyleLabel->setBuddy(penStyleComboBox);

38      penCapComboBox = new QComboBox;
39      penCapComboBox->addItem(tr("Flat"), Qt::FlatCap);
40      penCapComboBox->addItem(tr("Square"), Qt::SquareCap);
41      penCapComboBox->addItem(tr("Round"), Qt::RoundCap);

42      penCapLabel = new QLabel(tr("Pen &Cap:"));
43      penCapLabel->setBuddy(penCapComboBox);

44      penJoinComboBox = new QComboBox;
45      penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin);
46      penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin);
47      penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin);

48      penJoinLabel = new QLabel(tr("Pen &Join:"));
49      penJoinLabel->setBuddy(penJoinComboBox);



50      brushStyleComboBox = new QComboBox;
51      brushStyleComboBox->addItem(tr("Linear Gradient"),
            static_cast<int>(Qt::LinearGradientPattern));
52      brushStyleComboBox->addItem(tr("Radial Gradient"),
            static_cast<int>(Qt::RadialGradientPattern));
53      brushStyleComboBox->addItem(tr("Conical Gradient"),
54             static_cast<int>(Qt::ConicalGradientPattern));
55      brushStyleComboBox->addItem(tr("Texture"), static_cast<int>(Qt::TexturePattern));
56      brushStyleComboBox->addItem(tr("Solid"), static_cast<int>(Qt::SolidPattern));
57      brushStyleComboBox->addItem(tr("Horizontal"), static_cast<int>(Qt::HorPattern));
58      brushStyleComboBox->addItem(tr("Vertical"), static_cast<int>(Qt::VerPattern));
59      brushStyleComboBox->addItem(tr("Cross"), static_cast<int>(Qt::CrossPattern));
60      brushStyleComboBox->addItem(tr("Backward Diagonal"), static_cast<int>(Qt::BDiagPattern));
61      brushStyleComboBox->addItem(tr("Forward Diagonal"), static_cast<int>(Qt::FDiagPattern));
62      brushStyleComboBox->addItem(tr("Diagonal Cross"), static_cast<int>(Qt::DiagCrossPattern));
63      brushStyleComboBox->addItem(tr("Dense 1"), static_cast<int>(Qt::Dense1Pattern));
64      brushStyleComboBox->addItem(tr("Dense 2"), static_cast<int>(Qt::Dense2Pattern));
65      brushStyleComboBox->addItem(tr("Dense 3"), static_cast<int>(Qt::Dense3Pattern));
66      brushStyleComboBox->addItem(tr("Dense 4"), static_cast<int>(Qt::Dense4Pattern));
67      brushStyleComboBox->addItem(tr("Dense 5"), static_cast<int>(Qt::Dense5Pattern));
68      brushStyleComboBox->addItem(tr("Dense 6"), static_cast<int>(Qt::Dense6Pattern));
69      brushStyleComboBox->addItem(tr("Dense 7"), static_cast<int>(Qt::Dense7Pattern));
70      brushStyleComboBox->addItem(tr("None"), static_cast<int>(Qt::NoBrush));

71      brushStyleLabel = new QLabel(tr("&Brush:"));
72      brushStyleLabel->setBuddy(brushStyleComboBox);



73      otherOptionsLabel = new QLabel(tr("Options:"));

74      antialiasingCheckBox = new QCheckBox(tr("&Antialiasing"));

75      transformationsCheckBox = new QCheckBox(tr("&Transformations"));



76      connect(shapeComboBox, SIGNAL(activated(int)),
            this, SLOT(shapeChanged()));
77      connect(penWidthSpinBox, SIGNAL(valueChanged(int)),
            this, SLOT(penChanged()));
78      connect(penStyleComboBox, SIGNAL(activated(int)),
            this, SLOT(penChanged()));
79      connect(penCapComboBox, SIGNAL(activated(int)),
            this, SLOT(penChanged()));
80      connect(penJoinComboBox, SIGNAL(activated(int)),
            this, SLOT(penChanged()));
81      connect(brushStyleComboBox, SIGNAL(activated(int)),
            this, SLOT(brushChanged()));
82      connect(antialiasingCheckBox, SIGNAL(toggled(bool)),
            renderArea, SLOT(setAntialiased(bool)));
83      connect(transformationsCheckBox, SIGNAL(toggled(bool)),
            renderArea, SLOT(setTransformed(bool)));



84      QGridLayout *mainLayout = new QGridLayout;

85      mainLayout->setColumnStretch(0, 1);
86      mainLayout->setColumnStretch(3, 1);
87      mainLayout->addWidget(renderArea, 0, 0, 1, 4);
88      mainLayout->addWidget(shapeLabel, 2, 0, Qt::AlignRight);
89      mainLayout->addWidget(shapeComboBox, 2, 1);
90      mainLayout->addWidget(penWidthLabel, 3, 0, Qt::AlignRight);
91      mainLayout->addWidget(penWidthSpinBox, 3, 1);
92      mainLayout->addWidget(penStyleLabel, 4, 0, Qt::AlignRight);
93      mainLayout->addWidget(penStyleComboBox, 4, 1);
94      mainLayout->addWidget(penCapLabel, 3, 2, Qt::AlignRight);
95      mainLayout->addWidget(penCapComboBox, 3, 3);
96      mainLayout->addWidget(penJoinLabel, 2, 2, Qt::AlignRight);
97      mainLayout->addWidget(penJoinComboBox, 2, 3);
98      mainLayout->addWidget(brushStyleLabel, 4, 2, Qt::AlignRight);
99      mainLayout->addWidget(brushStyleComboBox, 4, 3);
100     mainLayout->addWidget(otherOptionsLabel, 5, 0, Qt::AlignRight);
101     mainLayout->addWidget(antialiasingCheckBox, 5, 1, 1, 1, Qt::AlignRight);
102     mainLayout->addWidget(transformationsCheckBox, 5, 2, 1, 2, Qt::AlignRight);
103     setLayout(mainLayout);

104     shapeChanged();
105     penChanged();
106     brushChanged();
107     antialiasingCheckBox->setChecked(true);

108     setWindowTitle(tr("Basic Drawing"));
109 }



110 void MainWindow::shapeChanged()
11  {
112     RenderArea::Shape shape = RenderArea::Shape(shapeComboBox->itemData(
113             shapeComboBox->currentIndex(), IdRole).toInt());
114     renderArea->setShape(shape);
115 }



116 void MainWindow::penChanged()
117 {
118     int width = penWidthSpinBox->value();
119     Qt::PenStyle style = Qt::PenStyle(penStyleComboBox->itemData(
            penStyleComboBox->currentIndex(), IdRole).toInt());
120     Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData(
            penCapComboBox->currentIndex(), IdRole).toInt());
121     Qt::PenJoinStyle join = Qt::PenJoinStyle(penJoinComboBox->itemData(
            penJoinComboBox->currentIndex(), IdRole).toInt());

121     renderArea->setPen(QPen(Qt::blue, width, style, cap, join));
123 }



124 void MainWindow::brushChanged()
125 {
126     Qt::BrushStyle style = Qt::BrushStyle(brushStyleComboBox->itemData(

            brushStyleComboBox->currentIndex(), IdRole).toInt());


127    if (style == Qt::LinearGradientPattern) {
128        QLinearGradient linearGradient(0, 0, 100, 100);
129        linearGradient.setColorAt(0.0, Qt::white);
130        linearGradient.setColorAt(0.2, Qt::green);
131        linearGradient.setColorAt(1.0, Qt::black);
132        renderArea->setBrush(linearGradient);

133    } else if (style == Qt::RadialGradientPattern) {
134        QRadialGradient radialGradient(50, 50, 50, 70, 70);
135        radialGradient.setColorAt(0.0, Qt::white);
136        radialGradient.setColorAt(0.2, Qt::green);
137        radialGradient.setColorAt(1.0, Qt::black);
138        renderArea->setBrush(radialGradient);
139    } else if (style == Qt::ConicalGradientPattern) {
140        QConicalGradient conicalGradient(50, 50, 150);
141        conicalGradient.setColorAt(0.0, Qt::white);
142        conicalGradient.setColorAt(0.2, Qt::green);
143        conicalGradient.setColorAt(1.0, Qt::black);
144        renderArea->setBrush(conicalGradient);

145    } else if (style == Qt::TexturePattern) {
         renderArea->setBrush(QBrush(QPixmap(":/images/brick.png")));

146    } else {
147        renderArea->setBrush(QBrush(Qt::green, style));
148    }
149 }
