Qt 5.10 Book Examples
TurtleWorkArea.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // TurtleWorkArea.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include <QJSEngine>
13 #include "TurtleWorkArea.h"
14 #include "Turtle.h"
15 
16 // ----------------------------------------------------------------------
17 TurtleWorkArea::TurtleWorkArea(QWidget* pwgt/*=0*/) : QWidget(pwgt)
18 {
19  m_pTurtle = new Turtle;
20  m_pTurtle->setFixedSize(400, 400);
21  m_ptxt = new QTextEdit;
22 
23  QComboBox* pcbo = new QComboBox;
24  QStringList lst;
25 
26  lst << "Haus vom Nikolaus" << "Curly" << "Circle" << "Fantasy";
27  pcbo->addItems(lst);
28  connect(pcbo, SIGNAL(activated(int)), SLOT(slotApplyCode(int)));
29  slotApplyCode(0);
30 
31  QJSValue scriptTurtle =
32  m_scriptEngine.newQObject(m_pTurtle);
33  m_scriptEngine.globalObject().setProperty("turtle", scriptTurtle);
34 
35  QPushButton* pcmd = new QPushButton("&Evaluate");
36  connect(pcmd, SIGNAL(clicked()), SLOT(slotEvaluate()));
37 
38  QGridLayout* pgrdLayout = new QGridLayout;
39 
40  pgrdLayout->addWidget(pcbo, 0, 0);
41  pgrdLayout->addWidget(m_ptxt, 1, 0);
42  pgrdLayout->addWidget(m_pTurtle, 0, 1, 2, 1);
43  pgrdLayout->addWidget(pcmd, 2, 0, 1, 2);
44  setLayout(pgrdLayout);
45 }
46 
47 // ----------------------------------------------------------------------
48 void TurtleWorkArea::slotEvaluate()
49 {
50  QJSValue result =
51  m_scriptEngine.evaluate(m_ptxt->toPlainText());
52  if (result.isError()) {
53  QMessageBox::critical(0,
54  "Evaluating error",
55  result.toString(),
56  QMessageBox::Yes
57  );
58  }
59 }
60 
61 // ----------------------------------------------------------------------
62 void TurtleWorkArea::slotApplyCode(int n)
63 {
64  QString strCode;
65  switch (n) {
66  case 0:
67  strCode = "var k = 100;\n"
68  "turtle.reset();\n"
69  "turtle.right(90);\n"
70  "turtle.back(-k);\n"
71  "turtle.left(90);\n"
72  "turtle.forward(k);\n"
73  "turtle.left(30);\n"
74  "turtle.forward(k);\n"
75  "turtle.left(120);\n"
76  "turtle.forward(k);\n"
77  "turtle.left(30);\n"
78  "turtle.forward(k);\n"
79  "turtle.left(135);\n"
80  "turtle.forward(Math.sqrt(2)*k);\n"
81  "turtle.left(135);\n"
82  "turtle.forward(k);\n"
83  "turtle.left(135);\n"
84  "turtle.forward(Math.sqrt(2)*k);\n";
85  break;
86  case 1:
87  strCode = "turtle.reset();\n"
88  "for (i = 0; i < 2; ++i) {\n"
89  " for(j = 0; j < 100; ++j) {\n"
90  " turtle.forward(15);\n"
91  " turtle.left(100 - j);\n"
92  " }\n"
93  " turtle.right(180);\n"
94  "}";
95  break;
96  case 2:
97  strCode = "turtle.circle = function()\n"
98  "{\n"
99  " for (var i = 0; i < 360; ++i) {\n"
100  " this.forward(1);\n"
101  " this.left(1);\n"
102  " }\n"
103  "}\n"
104  "turtle.reset();\n"
105  "turtle.circle();\n";
106  break;
107  default:
108  strCode = "turtle.reset();\n"
109  "for (i = 0; i < 200; ++i) {\n"
110  " turtle.forward(i * 2);\n"
111  " turtle.left(91);\n"
112  "}";
113  }
114  m_ptxt->setPlainText(strCode);
115 }
TurtleWorkArea(QWidget *pwgt=0)
Definition: Turtle.h:16