Changeset View
Changeset View
Standalone View
Standalone View
src/widgets/axiscontrol.cpp
Show All 13 Lines | 1 | /* Atelier KDE Printer Host for 3D Printing | |||
---|---|---|---|---|---|
14 | GNU General Public License for more details. | 14 | GNU General Public License for more details. | ||
15 | 15 | | |||
16 | You should have received a copy of the GNU General Public License | 16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | 18 | */ | ||
19 | #include "axiscontrol.h" | 19 | #include "axiscontrol.h" | ||
20 | #include <algorithm> | 20 | #include <algorithm> | ||
21 | 21 | | |||
22 | PieButton::PieButton(QLatin1Char &axis, int value, int size, int angle) : _axis(axis), _value(value) | 22 | PieButton::PieButton(QLatin1Char &axis, double value, int size, int angle) : _axis(axis), _value(value) | ||
23 | { | 23 | { | ||
24 | const int delta = 16; // Qt Docs: angle is 16th of a degree. | 24 | const int delta = 16; // Qt Docs: angle is 16th of a degree. | ||
25 | setBrush(_palette.button()); | 25 | setBrush(_palette.button()); | ||
26 | setStartAngle(angle * delta); | 26 | setStartAngle(angle * delta); | ||
27 | setSpanAngle(90 * delta); | 27 | setSpanAngle(90 * delta); | ||
28 | setRect(QRect(QPoint(size * -1, size * -1), QPoint(size, size))); | 28 | setRect(QRect(QPoint(size * -1, size * -1), QPoint(size, size))); | ||
29 | setZValue(size * -1); | 29 | setZValue(size * -1); | ||
30 | setAcceptHoverEvents(true); | 30 | setAcceptHoverEvents(true); | ||
Show All 15 Lines | 45 | { | |||
46 | setBrush(_palette.highlight()); | 46 | setBrush(_palette.highlight()); | ||
47 | } | 47 | } | ||
48 | 48 | | |||
49 | void PieButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *) | 49 | void PieButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *) | ||
50 | { | 50 | { | ||
51 | setBrush(_palette.button()); | 51 | setBrush(_palette.button()); | ||
52 | } | 52 | } | ||
53 | 53 | | |||
54 | RectButton::RectButton(QLatin1Char &axis, int value, int size) : _axis(axis), _value(value) | 54 | RectButton::RectButton(QLatin1Char &axis, double value, int size) : _axis(axis), _value(value) | ||
55 | { | 55 | { | ||
56 | setBrush(_palette.button()); | 56 | setBrush(_palette.button()); | ||
57 | setRect(QRect(QPoint(0, 0), QPoint(size, size))); | 57 | setRect(QRect(QPoint(0, 0), QPoint(size, size))); | ||
58 | setAcceptHoverEvents(true); | 58 | setAcceptHoverEvents(true); | ||
59 | setZValue(size * -1); | 59 | setZValue(size * -1); | ||
60 | if (axis != QLatin1Char('E')) { | 60 | if (axis != QLatin1Char('E')) { | ||
61 | setToolTip(tr("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); | 61 | setToolTip(tr("Move the hotend to the %1 by %2 units").arg(axis).arg(value)); | ||
62 | } else { | 62 | } else { | ||
Show All 23 Lines | |||||
86 | /* About the Magic Numbers | 86 | /* About the Magic Numbers | ||
87 | I don't have experience programming with QGraphicsScene, | 87 | I don't have experience programming with QGraphicsScene, | ||
88 | Tomaz is helping me, but until we have a better solution, all the values | 88 | Tomaz is helping me, but until we have a better solution, all the values | ||
89 | that are dividing or multiplying the items is based only in tests and errors. | 89 | that are dividing or multiplying the items is based only in tests and errors. | ||
90 | Those values was chosen because it fit better on the alignment of the items | 90 | Those values was chosen because it fit better on the alignment of the items | ||
91 | in the scene. If you have a better solution, please share with us. | 91 | in the scene. If you have a better solution, please share with us. | ||
92 | Lays Rodrigues - Jan/2017 | 92 | Lays Rodrigues - Jan/2017 | ||
93 | */ | 93 | */ | ||
94 | AxisControl::AxisControl(const QList<int> &movementValues, QWidget *parent) : | 94 | AxisControl::AxisControl(const QList<double> &movementValues, QWidget *parent) : | ||
95 | QGraphicsView(parent) | 95 | QGraphicsView(parent) | ||
96 | { | 96 | { | ||
97 | setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); | 97 | setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); | ||
98 | 98 | | |||
99 | setScene(new QGraphicsScene()); | 99 | setScene(new QGraphicsScene()); | ||
100 | 100 | | |||
101 | const int listSize = movementValues.size(); | 101 | const int listSize = movementValues.size(); | ||
102 | int maxValue = *std::max_element(movementValues.begin(), movementValues.end()); | 102 | double maxValue = *std::max_element(movementValues.begin(), movementValues.end()); | ||
103 | QList<int> lessList = movementValues; | 103 | QList<double> lessList = movementValues; | ||
104 | std::sort(lessList.begin(), lessList.end(), std::less<int>()); | 104 | std::sort(lessList.begin(), lessList.end(), std::less<double>()); | ||
105 | QList<int> greaterList = movementValues; | 105 | QList<double> greaterList = movementValues; | ||
106 | std::sort(greaterList.begin(), greaterList.end(), std::greater<int>()); | 106 | std::sort(greaterList.begin(), greaterList.end(), std::greater<double>()); | ||
107 | 107 | | |||
108 | auto createPie = [ this, maxValue ](QLatin1Char & axis, int value, int size, int angle) { | 108 | auto createPie = [ this, maxValue ](QLatin1Char & axis, double value, int size, int angle) { | ||
109 | auto pie = new PieButton(axis, value, size, angle); | 109 | auto pie = new PieButton(axis, value, size, angle); | ||
110 | pie->setPalette(this->palette()); | 110 | pie->setPalette(this->palette()); | ||
111 | connect(pie, &PieButton::clicked, this, &AxisControl::clicked); | 111 | connect(pie, &PieButton::clicked, this, &AxisControl::clicked); | ||
112 | if (abs(value) == maxValue) { | 112 | if (QString::number(abs(value)) == QString::number(maxValue)) { | ||
113 | setLabels(pie, axis, value); | 113 | setLabels(pie, axis, value); | ||
114 | } | 114 | } | ||
115 | scene()->addItem(pie); | 115 | scene()->addItem(pie); | ||
116 | }; | 116 | }; | ||
117 | 117 | | |||
118 | auto createRect = [ this, maxValue ](QLatin1Char & axis, int value, int size, int xPos, int yPos) { | 118 | auto createRect = [ this, maxValue ](QLatin1Char & axis, double value, int size, int xPos, int yPos) { | ||
119 | auto z = new RectButton(axis, value, size); | 119 | auto z = new RectButton(axis, value, size); | ||
120 | z->setPalette(this->palette()); | 120 | z->setPalette(this->palette()); | ||
121 | z->setPos(xPos, yPos); | 121 | z->setPos(xPos, yPos); | ||
122 | connect(z, &RectButton::clicked, this, &AxisControl::clicked); | 122 | connect(z, &RectButton::clicked, this, &AxisControl::clicked); | ||
123 | if (abs(value) == maxValue) { | 123 | if (QString::number(abs(value)) == QString::number(maxValue)) { | ||
124 | setLabels(z, axis, value); | 124 | setLabels(z, axis, value); | ||
125 | } | 125 | } | ||
126 | scene()->addItem(z); | 126 | scene()->addItem(z); | ||
127 | }; | 127 | }; | ||
128 | 128 | | |||
129 | int currPieSize = 25; | 129 | int currPieSize = 25; | ||
130 | auto xchar = QLatin1Char('X'); | 130 | auto xchar = QLatin1Char('X'); | ||
131 | auto ychar = QLatin1Char('Y'); | 131 | auto ychar = QLatin1Char('Y'); | ||
132 | auto zchar = QLatin1Char('Z'); | 132 | auto zchar = QLatin1Char('Z'); | ||
133 | auto echar = QLatin1Char('E'); | 133 | auto echar = QLatin1Char('E'); | ||
134 | for (const int &value : lessList) { | 134 | for (const double &value : lessList) { | ||
135 | createPie(xchar, value, currPieSize, -45); // Left | 135 | createPie(xchar, value, currPieSize, -45); // Left | ||
136 | createPie(xchar, value * -1, currPieSize, 135); // Right | 136 | createPie(xchar, value * -1, currPieSize, 135); // Right | ||
137 | createPie(ychar, value, currPieSize, 45); // Top | 137 | createPie(ychar, value, currPieSize, 45); // Top | ||
138 | createPie(ychar, value * -1, currPieSize, 225); // Bottom | 138 | createPie(ychar, value * -1, currPieSize, 225); // Bottom | ||
139 | currPieSize += 25; | 139 | currPieSize += 25; | ||
140 | } | 140 | } | ||
141 | 141 | | |||
142 | int currSize = 25; | 142 | int currSize = 25; | ||
143 | int xPos = sceneRect().width() - 50; | 143 | int xPos = sceneRect().width() - 50; | ||
144 | int yPos = -(listSize * 25); //Align with the origin | 144 | int yPos = -(listSize * 25); //Align with the origin | ||
145 | 145 | | |||
146 | // Z+ | 146 | // Z+ | ||
147 | for (const int &value : greaterList) { | 147 | for (const double &value : greaterList) { | ||
148 | createRect(zchar, value, currSize, xPos, yPos); | 148 | createRect(zchar, value, currSize, xPos, yPos); | ||
149 | yPos += currSize; | 149 | yPos += currSize; | ||
150 | } | 150 | } | ||
151 | 151 | | |||
152 | // Z- | 152 | // Z- | ||
153 | for (const int &value : lessList) { | 153 | for (const double &value : lessList) { | ||
154 | createRect(zchar, -value, currSize, xPos, yPos); | 154 | createRect(zchar, -value, currSize, xPos, yPos); | ||
155 | yPos += currSize; | 155 | yPos += currSize; | ||
156 | } | 156 | } | ||
157 | 157 | | |||
158 | currSize = 25; | 158 | currSize = 25; | ||
159 | xPos = sceneRect().width() - 50; | 159 | xPos = sceneRect().width() - 50; | ||
160 | yPos = -(listSize * 25); //Align with the origin | 160 | yPos = -(listSize * 25); //Align with the origin | ||
161 | 161 | | |||
162 | // E- | 162 | // E- | ||
163 | for (const int &value : greaterList) { | 163 | for (const double &value : greaterList) { | ||
164 | createRect(echar, -value, currSize, xPos, yPos); | 164 | createRect(echar, -value, currSize, xPos, yPos); | ||
165 | yPos += currSize; | 165 | yPos += currSize; | ||
166 | } | 166 | } | ||
167 | 167 | | |||
168 | // E+ | 168 | // E+ | ||
169 | for (const int &value : lessList) { | 169 | for (const double &value : lessList) { | ||
170 | createRect(echar, value, currSize, xPos, yPos); | 170 | createRect(echar, value, currSize, xPos, yPos); | ||
171 | yPos += currSize; | 171 | yPos += currSize; | ||
172 | } | 172 | } | ||
173 | setSceneRect(scene()->itemsBoundingRect()); | 173 | setSceneRect(scene()->itemsBoundingRect()); | ||
174 | } | 174 | } | ||
175 | 175 | | |||
176 | void AxisControl::resizeEvent(QResizeEvent *) | 176 | void AxisControl::resizeEvent(QResizeEvent *) | ||
177 | { | 177 | { | ||
178 | fitInView(sceneRect(), Qt::KeepAspectRatio); | 178 | fitInView(sceneRect(), Qt::KeepAspectRatio); | ||
179 | } | 179 | } | ||
180 | 180 | | |||
181 | void AxisControl::setLabels(QGraphicsItem *item, QLatin1Char &axis, int value) | 181 | void AxisControl::setLabels(QGraphicsItem *item, QLatin1Char &axis, double value) | ||
182 | { | 182 | { | ||
183 | auto *lb = new QGraphicsSimpleTextItem(); | 183 | auto *lb = new QGraphicsSimpleTextItem(); | ||
184 | lb->setBrush(palette().buttonText()); | 184 | lb->setBrush(palette().buttonText()); | ||
185 | 185 | | |||
186 | if (this->logicalDpiX() <= 96) { | 186 | if (this->logicalDpiX() <= 96) { | ||
187 | lb->setText((value < 0) ? QStringLiteral(" -") + axis : QStringLiteral(" ") + axis); | 187 | lb->setText((value < 0) ? QStringLiteral(" -") + axis : QStringLiteral(" ") + axis); | ||
188 | } else { | 188 | } else { | ||
189 | lb->setText((value < 0) ? QStringLiteral("-") + axis : QStringLiteral(" ") + axis); | 189 | lb->setText((value < 0) ? QStringLiteral("-") + axis : QStringLiteral(" ") + axis); | ||
Show All 28 Lines |