| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "pathstroke.h" |
| 5 |
#include "arthurstyle.h" |
| 6 |
#include "arthurwidgets.h" |
| 7 |
|
| 8 |
extern void draw_round_rect(QPainter *p, const QRect &bounds, int radius); |
| 9 |
|
| 10 |
|
| 11 |
PathStrokeControls::PathStrokeControls(QWidget* parent, PathStrokeRenderer* renderer, bool smallScreen) |
| 12 |
: QWidget(parent) |
| 13 |
{ |
| 14 |
m_renderer = renderer; |
| 15 |
|
| 16 |
if (smallScreen) |
| 17 |
layoutForSmallScreens(); |
| 18 |
else |
| 19 |
layoutForDesktop(); |
| 20 |
} |
| 21 |
|
| 22 |
void PathStrokeControls::createCommonControls(QWidget* parent) |
| 23 |
{ |
| 24 |
m_capGroup = new QGroupBox(parent); |
| 25 |
m_capGroup->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
| 26 |
QRadioButton *flatCap = new QRadioButton(m_capGroup); |
| 27 |
QRadioButton *squareCap = new QRadioButton(m_capGroup); |
| 28 |
QRadioButton *roundCap = new QRadioButton(m_capGroup); |
| 29 |
m_capGroup->setTitle(tr("Cap Style")); |
| 30 |
flatCap->setText(tr("Flat")); |
| 31 |
squareCap->setText(tr("Square")); |
| 32 |
roundCap->setText(tr("Round")); |
| 33 |
flatCap->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 34 |
squareCap->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 35 |
roundCap->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 36 |
|
| 37 |
m_joinGroup = new QGroupBox(parent); |
| 38 |
m_joinGroup->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
| 39 |
QRadioButton *bevelJoin = new QRadioButton(m_joinGroup); |
| 40 |
QRadioButton *miterJoin = new QRadioButton(m_joinGroup); |
| 41 |
QRadioButton *svgMiterJoin = new QRadioButton(m_joinGroup); |
| 42 |
QRadioButton *roundJoin = new QRadioButton(m_joinGroup); |
| 43 |
m_joinGroup->setTitle(tr("Join Style")); |
| 44 |
bevelJoin->setText(tr("Bevel")); |
| 45 |
miterJoin->setText(tr("Miter")); |
| 46 |
svgMiterJoin->setText(tr("SvgMiter")); |
| 47 |
roundJoin->setText(tr("Round")); |
| 48 |
|
| 49 |
m_styleGroup = new QGroupBox(parent); |
| 50 |
m_styleGroup->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
| 51 |
QRadioButton *solidLine = new QRadioButton(m_styleGroup); |
| 52 |
QRadioButton *dashLine = new QRadioButton(m_styleGroup); |
| 53 |
QRadioButton *dotLine = new QRadioButton(m_styleGroup); |
| 54 |
QRadioButton *dashDotLine = new QRadioButton(m_styleGroup); |
| 55 |
QRadioButton *dashDotDotLine = new QRadioButton(m_styleGroup); |
| 56 |
QRadioButton *customDashLine = new QRadioButton(m_styleGroup); |
| 57 |
m_styleGroup->setTitle(tr("Pen Style")); |
| 58 |
|
| 59 |
QPixmap line_solid(":res/images/line_solid.png"); |
| 60 |
solidLine->setIcon(line_solid); |
| 61 |
solidLine->setIconSize(line_solid.size()); |
| 62 |
QPixmap line_dashed(":res/images/line_dashed.png"); |
| 63 |
dashLine->setIcon(line_dashed); |
| 64 |
dashLine->setIconSize(line_dashed.size()); |
| 65 |
QPixmap line_dotted(":res/images/line_dotted.png"); |
| 66 |
dotLine->setIcon(line_dotted); |
| 67 |
dotLine->setIconSize(line_dotted.size()); |
| 68 |
QPixmap line_dash_dot(":res/images/line_dash_dot.png"); |
| 69 |
dashDotLine->setIcon(line_dash_dot); |
| 70 |
dashDotLine->setIconSize(line_dash_dot.size()); |
| 71 |
QPixmap line_dash_dot_dot(":res/images/line_dash_dot_dot.png"); |
| 72 |
dashDotDotLine->setIcon(line_dash_dot_dot); |
| 73 |
dashDotDotLine->setIconSize(line_dash_dot_dot.size()); |
| 74 |
customDashLine->setText(tr("Custom")); |
| 75 |
|
| 76 |
int fixedHeight = bevelJoin->sizeHint().height(); |
| 77 |
solidLine->setFixedHeight(fixedHeight); |
| 78 |
dashLine->setFixedHeight(fixedHeight); |
| 79 |
dotLine->setFixedHeight(fixedHeight); |
| 80 |
dashDotLine->setFixedHeight(fixedHeight); |
| 81 |
dashDotDotLine->setFixedHeight(fixedHeight); |
| 82 |
|
| 83 |
m_pathModeGroup = new QGroupBox(parent); |
| 84 |
m_pathModeGroup->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
| 85 |
QRadioButton *curveMode = new QRadioButton(m_pathModeGroup); |
| 86 |
QRadioButton *lineMode = new QRadioButton(m_pathModeGroup); |
| 87 |
m_pathModeGroup->setTitle(tr("Line Style")); |
| 88 |
curveMode->setText(tr("Curves")); |
| 89 |
lineMode->setText(tr("Lines")); |
| 90 |
|
| 91 |
|
| 92 |
// Layouts |
| 93 |
QVBoxLayout *capGroupLayout = new QVBoxLayout(m_capGroup); |
| 94 |
capGroupLayout->addWidget(flatCap); |
| 95 |
capGroupLayout->addWidget(squareCap); |
| 96 |
capGroupLayout->addWidget(roundCap); |
| 97 |
|
| 98 |
QVBoxLayout *joinGroupLayout = new QVBoxLayout(m_joinGroup); |
| 99 |
joinGroupLayout->addWidget(bevelJoin); |
| 100 |
joinGroupLayout->addWidget(miterJoin); |
| 101 |
joinGroupLayout->addWidget(svgMiterJoin); |
| 102 |
joinGroupLayout->addWidget(roundJoin); |
| 103 |
|
| 104 |
QVBoxLayout *styleGroupLayout = new QVBoxLayout(m_styleGroup); |
| 105 |
styleGroupLayout->addWidget(solidLine); |
| 106 |
styleGroupLayout->addWidget(dashLine); |
| 107 |
styleGroupLayout->addWidget(dotLine); |
| 108 |
styleGroupLayout->addWidget(dashDotLine); |
| 109 |
styleGroupLayout->addWidget(dashDotDotLine); |
| 110 |
styleGroupLayout->addWidget(customDashLine); |
| 111 |
|
| 112 |
QVBoxLayout *pathModeGroupLayout = new QVBoxLayout(m_pathModeGroup); |
| 113 |
pathModeGroupLayout->addWidget(curveMode); |
| 114 |
pathModeGroupLayout->addWidget(lineMode); |
| 115 |
|
| 116 |
|
| 117 |
// Connections |
| 118 |
connect(flatCap, &QAbstractButton::clicked, |
| 119 |
m_renderer, &PathStrokeRenderer::setFlatCap); |
| 120 |
connect(squareCap, &QAbstractButton::clicked, |
| 121 |
m_renderer, &PathStrokeRenderer::setSquareCap); |
| 122 |
connect(roundCap, &QAbstractButton::clicked, |
| 123 |
m_renderer, &PathStrokeRenderer::setRoundCap); |
| 124 |
|
| 125 |
connect(bevelJoin, &QAbstractButton::clicked, |
| 126 |
m_renderer, &PathStrokeRenderer::setBevelJoin); |
| 127 |
connect(miterJoin, &QAbstractButton::clicked, |
| 128 |
m_renderer, &PathStrokeRenderer::setMiterJoin); |
| 129 |
connect(svgMiterJoin, &QAbstractButton::clicked, |
| 130 |
m_renderer, &PathStrokeRenderer::setSvgMiterJoin); |
| 131 |
connect(roundJoin, &QAbstractButton::clicked, |
| 132 |
m_renderer, &PathStrokeRenderer::setRoundJoin); |
| 133 |
|
| 134 |
connect(curveMode, &QAbstractButton::clicked, |
| 135 |
m_renderer, &PathStrokeRenderer::setCurveMode); |
| 136 |
connect(lineMode, &QAbstractButton::clicked, |
| 137 |
m_renderer, &PathStrokeRenderer::setLineMode); |
| 138 |
|
| 139 |
connect(solidLine, &QAbstractButton::clicked, |
| 140 |
m_renderer, &PathStrokeRenderer::setSolidLine); |
| 141 |
connect(dashLine, &QAbstractButton::clicked, |
| 142 |
m_renderer, &PathStrokeRenderer::setDashLine); |
| 143 |
connect(dotLine, &QAbstractButton::clicked, |
| 144 |
m_renderer, &PathStrokeRenderer::setDotLine); |
| 145 |
connect(dashDotLine, &QAbstractButton::clicked, |
| 146 |
m_renderer, &PathStrokeRenderer::setDashDotLine); |
| 147 |
connect(dashDotDotLine, &QAbstractButton::clicked, |
| 148 |
m_renderer, &PathStrokeRenderer::setDashDotDotLine); |
| 149 |
connect(customDashLine, &QAbstractButton::clicked, |
| 150 |
m_renderer, &PathStrokeRenderer::setCustomDashLine); |
| 151 |
|
| 152 |
// Set the defaults: |
| 153 |
flatCap->setChecked(true); |
| 154 |
bevelJoin->setChecked(true); |
| 155 |
curveMode->setChecked(true); |
| 156 |
solidLine->setChecked(true); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
void PathStrokeControls::layoutForDesktop() |
| 161 |
{ |
| 162 |
QGroupBox *mainGroup = new QGroupBox(this); |
| 163 |
mainGroup->setFixedWidth(180); |
| 164 |
mainGroup->setTitle(tr("Path Stroking")); |
| 165 |
|
| 166 |
createCommonControls(mainGroup); |
| 167 |
|
| 168 |
QGroupBox* penWidthGroup = new QGroupBox(mainGroup); |
| 169 |
QSlider *penWidth = new QSlider(Qt::Horizontal, penWidthGroup); |
| 170 |
penWidth->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); |
| 171 |
penWidthGroup->setTitle(tr("Pen Width")); |
| 172 |
penWidth->setRange(0, 500); |
| 173 |
|
| 174 |
QPushButton *animated = new QPushButton(mainGroup); |
| 175 |
animated->setText(tr("Animate")); |
| 176 |
animated->setCheckable(true); |
| 177 |
|
| 178 |
QPushButton *showSourceButton = new QPushButton(mainGroup); |
| 179 |
showSourceButton->setText(tr("Show Source")); |
| 180 |
QPushButton *whatsThisButton = new QPushButton(mainGroup); |
| 181 |
whatsThisButton->setText(tr("What's This?")); |
| 182 |
whatsThisButton->setCheckable(true); |
| 183 |
|
| 184 |
|
| 185 |
// Layouts: |
| 186 |
QVBoxLayout *penWidthLayout = new QVBoxLayout(penWidthGroup); |
| 187 |
penWidthLayout->addWidget(penWidth); |
| 188 |
|
| 189 |
QVBoxLayout * mainLayout = new QVBoxLayout(this); |
| 190 |
mainLayout->setContentsMargins(QMargins()); |
| 191 |
mainLayout->addWidget(mainGroup); |
| 192 |
|
| 193 |
QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup); |
| 194 |
mainGroupLayout->setContentsMargins(3, 3, 3, 3); |
| 195 |
mainGroupLayout->addWidget(m_capGroup); |
| 196 |
mainGroupLayout->addWidget(m_joinGroup); |
| 197 |
mainGroupLayout->addWidget(m_styleGroup); |
| 198 |
mainGroupLayout->addWidget(penWidthGroup); |
| 199 |
mainGroupLayout->addWidget(m_pathModeGroup); |
| 200 |
mainGroupLayout->addWidget(animated); |
| 201 |
mainGroupLayout->addStretch(1); |
| 202 |
mainGroupLayout->addWidget(showSourceButton); |
| 203 |
mainGroupLayout->addWidget(whatsThisButton); |
| 204 |
|
| 205 |
|
| 206 |
// Set up connections |
| 207 |
connect(animated, &QAbstractButton::toggled, |
| 208 |
m_renderer, &PathStrokeRenderer::setAnimation); |
| 209 |
|
| 210 |
connect(penWidth, &QAbstractSlider::valueChanged, |
| 211 |
m_renderer, &PathStrokeRenderer::setPenWidth); |
| 212 |
|
| 213 |
connect(showSourceButton, &QAbstractButton::clicked, |
| 214 |
m_renderer, &ArthurFrame::showSource); |
| 215 |
connect(whatsThisButton, &QAbstractButton::clicked, |
| 216 |
m_renderer, &ArthurFrame::setDescriptionEnabled); |
| 217 |
connect(m_renderer, &ArthurFrame::descriptionEnabledChanged, |
| 218 |
whatsThisButton, &QAbstractButton::setChecked); |
| 219 |
|
| 220 |
|
| 221 |
// Set the defaults |
| 222 |
animated->setChecked(true); |
| 223 |
penWidth->setValue(50); |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
void PathStrokeControls::layoutForSmallScreens() |
| 228 |
{ |
| 229 |
createCommonControls(this); |
| 230 |
|
| 231 |
m_capGroup->layout()->setContentsMargins(QMargins()); |
| 232 |
m_joinGroup->layout()->setContentsMargins(QMargins()); |
| 233 |
m_styleGroup->layout()->setContentsMargins(QMargins()); |
| 234 |
m_pathModeGroup->layout()->setContentsMargins(QMargins()); |
| 235 |
|
| 236 |
QPushButton* okBtn = new QPushButton(tr("OK"), this); |
| 237 |
okBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 238 |
okBtn->setMinimumSize(100,okBtn->minimumSize().height()); |
| 239 |
|
| 240 |
QPushButton* quitBtn = new QPushButton(tr("Quit"), this); |
| 241 |
quitBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 242 |
quitBtn->setMinimumSize(100, okBtn->minimumSize().height()); |
| 243 |
|
| 244 |
QLabel *penWidthLabel = new QLabel(tr(" Width:")); |
| 245 |
QSlider *penWidth = new QSlider(Qt::Horizontal, this); |
| 246 |
penWidth->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); |
| 247 |
penWidth->setRange(0, 500); |
| 248 |
|
| 249 |
// Layouts: |
| 250 |
QHBoxLayout *penWidthLayout = new QHBoxLayout; |
| 251 |
penWidthLayout->addWidget(penWidthLabel, 0, Qt::AlignRight); |
| 252 |
penWidthLayout->addWidget(penWidth); |
| 253 |
|
| 254 |
QVBoxLayout *leftLayout = new QVBoxLayout; |
| 255 |
leftLayout->addWidget(m_capGroup); |
| 256 |
leftLayout->addWidget(m_joinGroup); |
| 257 |
leftLayout->addLayout(penWidthLayout); |
| 258 |
|
| 259 |
QVBoxLayout *rightLayout = new QVBoxLayout; |
| 260 |
rightLayout->addWidget(m_styleGroup); |
| 261 |
rightLayout->addWidget(m_pathModeGroup); |
| 262 |
|
| 263 |
QGridLayout *mainLayout = new QGridLayout(this); |
| 264 |
mainLayout->setContentsMargins(QMargins()); |
| 265 |
|
| 266 |
// Add spacers around the form items so we don't look stupid at higher resolutions |
| 267 |
mainLayout->addItem(new QSpacerItem(0,0), 0, 0, 1, 4); |
| 268 |
mainLayout->addItem(new QSpacerItem(0,0), 1, 0, 2, 1); |
| 269 |
mainLayout->addItem(new QSpacerItem(0,0), 1, 3, 2, 1); |
| 270 |
mainLayout->addItem(new QSpacerItem(0,0), 3, 0, 1, 4); |
| 271 |
|
| 272 |
mainLayout->addLayout(leftLayout, 1, 1); |
| 273 |
mainLayout->addLayout(rightLayout, 1, 2); |
| 274 |
mainLayout->addWidget(quitBtn, 2, 1, Qt::AlignHCenter | Qt::AlignTop); |
| 275 |
mainLayout->addWidget(okBtn, 2, 2, Qt::AlignHCenter | Qt::AlignTop); |
| 276 |
|
| 277 |
connect(penWidth, &QAbstractSlider::valueChanged, m_renderer, &PathStrokeRenderer::setPenWidth); |
| 278 |
connect(quitBtn, &QAbstractButton::clicked, this, &PathStrokeControls::emitQuitSignal); |
| 279 |
connect(okBtn, &QAbstractButton::clicked, this, &PathStrokeControls::emitOkSignal); |
| 280 |
|
| 281 |
m_renderer->setAnimation(true); |
| 282 |
penWidth->setValue(50); |
| 283 |
} |
| 284 |
|
| 285 |
void PathStrokeControls::emitQuitSignal() |
| 286 |
{ |
| 287 |
emit quitPressed(); |
| 288 |
} |
| 289 |
|
| 290 |
void PathStrokeControls::emitOkSignal() |
| 291 |
{ |
| 292 |
emit okPressed(); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
PathStrokeWidget::PathStrokeWidget(bool smallScreen) |
| 297 |
{ |
| 298 |
setWindowTitle(tr("Path Stroking")); |
| 299 |
|
| 300 |
// Widget construction and property setting |
| 301 |
m_renderer = new PathStrokeRenderer(this, smallScreen); |
| 302 |
|
| 303 |
m_controls = new PathStrokeControls(nullptr, m_renderer, smallScreen); |
| 304 |
|
| 305 |
// Layouting |
| 306 |
QHBoxLayout *viewLayout = new QHBoxLayout(this); |
| 307 |
viewLayout->addWidget(m_renderer); |
| 308 |
|
| 309 |
if (!smallScreen) |
| 310 |
viewLayout->addWidget(m_controls); |
| 311 |
|
| 312 |
m_renderer->loadSourceFile(":res/pathstroke/pathstroke.cpp"); |
| 313 |
m_renderer->loadDescription(":res/pathstroke/pathstroke.html"); |
| 314 |
|
| 315 |
connect(m_renderer, &PathStrokeRenderer::clicked, this, &PathStrokeWidget::showControls); |
| 316 |
connect(m_controls, &PathStrokeControls::okPressed, this, &PathStrokeWidget::hideControls); |
| 317 |
connect(m_controls, &PathStrokeControls::quitPressed, QApplication::instance(), &QApplication::quit); |
| 318 |
} |
| 319 |
|
| 320 |
void PathStrokeWidget::showControls() |
| 321 |
{ |
| 322 |
m_controls->showFullScreen(); |
| 323 |
} |
| 324 |
|
| 325 |
void PathStrokeWidget::hideControls() |
| 326 |
{ |
| 327 |
m_controls->hide(); |
| 328 |
} |
| 329 |
|
| 330 |
void PathStrokeWidget::setStyle(QStyle *style) |
| 331 |
{ |
| 332 |
QWidget::setStyle(style); |
| 333 |
if (m_controls != nullptr) |
| 334 |
{ |
| 335 |
m_controls->setStyle(style); |
| 336 |
|
| 337 |
const QList<QWidget *> widgets = m_controls->findChildren<QWidget *>(); |
| 338 |
for (QWidget *w : widgets) |
| 339 |
w->setStyle(style); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
PathStrokeRenderer::PathStrokeRenderer(QWidget *parent, bool smallScreen) |
| 344 |
: ArthurFrame(parent) |
| 345 |
{ |
| 346 |
m_smallScreen = smallScreen; |
| 347 |
m_pointSize = 10; |
| 348 |
m_activePoint = -1; |
| 349 |
m_capStyle = Qt::FlatCap; |
| 350 |
m_joinStyle = Qt::BevelJoin; |
| 351 |
m_pathMode = CurveMode; |
| 352 |
m_penWidth = 1; |
| 353 |
m_penStyle = Qt::SolidLine; |
| 354 |
m_wasAnimated = true; |
| 355 |
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 356 |
setAttribute(Qt::WA_AcceptTouchEvents); |
| 357 |
} |
| 358 |
|
| 359 |
void PathStrokeRenderer::paint(QPainter *painter) |
| 360 |
{ |
| 361 |
if (m_points.isEmpty()) |
| 362 |
initializePoints(); |
| 363 |
|
| 364 |
painter->setRenderHint(QPainter::Antialiasing); |
| 365 |
|
| 366 |
QPalette pal = palette(); |
| 367 |
painter->setPen(Qt::NoPen); |
| 368 |
|
| 369 |
// Construct the path |
| 370 |
QPainterPath path; |
| 371 |
path.moveTo(m_points.at(0)); |
| 372 |
|
| 373 |
if (m_pathMode == LineMode) { |
| 374 |
for (int i=1; i<m_points.size(); ++i) |
| 375 |
path.lineTo(m_points.at(i)); |
| 376 |
} else { |
| 377 |
int i=1; |
| 378 |
while (i + 2 < m_points.size()) { |
| 379 |
path.cubicTo(m_points.at(i), m_points.at(i+1), m_points.at(i+2)); |
| 380 |
i += 3; |
| 381 |
} |
| 382 |
while (i < m_points.size()) { |
| 383 |
path.lineTo(m_points.at(i)); |
| 384 |
++i; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
// Draw the path |
| 389 |
{ |
| 390 |
QColor lg = Qt::red; |
| 391 |
|
| 392 |
// The "custom" pen |
| 393 |
if (m_penStyle == Qt::NoPen) { |
| 394 |
QPainterPathStroker stroker; |
| 395 |
stroker.setWidth(m_penWidth); |
| 396 |
stroker.setJoinStyle(m_joinStyle); |
| 397 |
stroker.setCapStyle(m_capStyle); |
| 398 |
|
| 399 |
QList<qreal> dashes; |
| 400 |
qreal space = 4; |
| 401 |
dashes << 1 << space |
| 402 |
<< 3 << space |
| 403 |
<< 9 << space |
| 404 |
<< 27 << space |
| 405 |
<< 9 << space |
| 406 |
<< 3 << space; |
| 407 |
stroker.setDashPattern(dashes); |
| 408 |
QPainterPath stroke = stroker.createStroke(path); |
| 409 |
painter->fillPath(stroke, lg); |
| 410 |
|
| 411 |
} else { |
| 412 |
QPen pen(lg, m_penWidth, m_penStyle, m_capStyle, m_joinStyle); |
| 413 |
painter->strokePath(path, pen); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
if (1) { |
| 418 |
// Draw the control points |
| 419 |
painter->setPen(QColor(50, 100, 120, 200)); |
| 420 |
painter->setBrush(QColor(200, 200, 210, 120)); |
| 421 |
for (int i=0; i<m_points.size(); ++i) { |
| 422 |
QPointF pos = m_points.at(i); |
| 423 |
painter->drawEllipse(QRectF(pos.x() - m_pointSize, |
| 424 |
pos.y() - m_pointSize, |
| 425 |
m_pointSize*2, m_pointSize*2)); |
| 426 |
} |
| 427 |
painter->setPen(QPen(Qt::lightGray, 0, Qt::SolidLine)); |
| 428 |
painter->setBrush(Qt::NoBrush); |
| 429 |
painter->drawPolyline(m_points); |
| 430 |
} |
| 431 |
|
| 432 |
} |
| 433 |
|
| 434 |
void PathStrokeRenderer::initializePoints() |
| 435 |
{ |
| 436 |
const int count = 7; |
| 437 |
m_points.clear(); |
| 438 |
m_vectors.clear(); |
| 439 |
|
| 440 |
QTransform m; |
| 441 |
qreal rot = 360.0 / count; |
| 442 |
QPointF center(width() / 2, height() / 2); |
| 443 |
QTransform vm; |
| 444 |
vm.shear(2, -1); |
| 445 |
vm.scale(3, 3); |
| 446 |
|
| 447 |
for (int i=0; i<count; ++i) { |
| 448 |
m_vectors << QPointF(.1f, .25f) * (m * vm); |
| 449 |
m_points << QPointF(0, 100) * m + center; |
| 450 |
m.rotate(rot); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
void PathStrokeRenderer::updatePoints() |
| 455 |
{ |
| 456 |
qreal pad = 10; |
| 457 |
qreal left = pad; |
| 458 |
qreal right = width() - pad; |
| 459 |
qreal top = pad; |
| 460 |
qreal bottom = height() - pad; |
| 461 |
|
| 462 |
Q_ASSERT(m_points.size() == m_vectors.size()); |
| 463 |
for (int i = 0; i < m_points.size(); ++i) { |
| 464 |
QPointF pos = m_points.at(i); |
| 465 |
QPointF vec = m_vectors.at(i); |
| 466 |
pos += vec; |
| 467 |
if (pos.x() < left || pos.x() > right) { |
| 468 |
vec.setX(-vec.x()); |
| 469 |
pos.setX(pos.x() < left ? left : right); |
| 470 |
} if (pos.y() < top || pos.y() > bottom) { |
| 471 |
vec.setY(-vec.y()); |
| 472 |
pos.setY(pos.y() < top ? top : bottom); |
| 473 |
} |
| 474 |
m_points[i] = pos; |
| 475 |
m_vectors[i] = vec; |
| 476 |
} |
| 477 |
update(); |
| 478 |
} |
| 479 |
|
| 480 |
void PathStrokeRenderer::mousePressEvent(QMouseEvent *e) |
| 481 |
{ |
| 482 |
if (!m_fingerPointMapping.isEmpty()) |
| 483 |
return; |
| 484 |
setDescriptionEnabled(false); |
| 485 |
m_activePoint = -1; |
| 486 |
qreal distance = -1; |
| 487 |
for (int i = 0; i < m_points.size(); ++i) { |
| 488 |
qreal d = QLineF(e->position().toPoint(), m_points.at(i)).length(); |
| 489 |
if ((distance < 0 && d < 8 * m_pointSize) || d < distance) { |
| 490 |
distance = d; |
| 491 |
m_activePoint = i; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
if (m_activePoint != -1) { |
| 496 |
m_wasAnimated = m_timer.isActive(); |
| 497 |
setAnimation(false); |
| 498 |
mouseMoveEvent(e); |
| 499 |
} |
| 500 |
|
| 501 |
// If we're not running in small screen mode, always assume we're dragging |
| 502 |
m_mouseDrag = !m_smallScreen; |
| 503 |
m_mousePress = e->position().toPoint(); |
| 504 |
} |
| 505 |
|
| 506 |
void PathStrokeRenderer::mouseMoveEvent(QMouseEvent *e) |
| 507 |
{ |
| 508 |
if (!m_fingerPointMapping.isEmpty()) |
| 509 |
return; |
| 510 |
// If we've moved more then 25 pixels, assume user is dragging |
| 511 |
if (!m_mouseDrag && QPoint(m_mousePress - e->position().toPoint()).manhattanLength() > 25) |
| 512 |
m_mouseDrag = true; |
| 513 |
|
| 514 |
if (m_mouseDrag && m_activePoint >= 0 && m_activePoint < m_points.size()) { |
| 515 |
m_points[m_activePoint] = e->position().toPoint(); |
| 516 |
update(); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
void PathStrokeRenderer::mouseReleaseEvent(QMouseEvent *) |
| 521 |
{ |
| 522 |
if (!m_fingerPointMapping.isEmpty()) |
| 523 |
return; |
| 524 |
m_activePoint = -1; |
| 525 |
setAnimation(m_wasAnimated); |
| 526 |
|
| 527 |
if (!m_mouseDrag && m_smallScreen) |
| 528 |
emit clicked(); |
| 529 |
} |
| 530 |
|
| 531 |
void PathStrokeRenderer::timerEvent(QTimerEvent *e) |
| 532 |
{ |
| 533 |
if (e->timerId() == m_timer.timerId()) { |
| 534 |
updatePoints(); |
| 535 |
} // else if (e->timerId() == m_fpsTimer.timerId()) { |
| 536 |
// emit frameRate(m_frameCount); |
| 537 |
// m_frameCount = 0; |
| 538 |
// } |
| 539 |
} |
| 540 |
|
| 541 |
bool PathStrokeRenderer::event(QEvent *e) |
| 542 |
{ |
| 543 |
bool touchBegin = false; |
| 544 |
switch (e->type()) { |
| 545 |
case QEvent::TouchBegin: |
| 546 |
touchBegin = true; |
| 547 |
Q_FALLTHROUGH(); |
| 548 |
case QEvent::TouchUpdate: |
| 549 |
{ |
| 550 |
const QTouchEvent *const event = static_cast<const QTouchEvent*>(e); |
| 551 |
const auto points = event->points(); |
| 552 |
for (const auto &point : points) { |
| 553 |
const int id = point.id(); |
| 554 |
switch (point.state()) { |
| 555 |
case QEventPoint::Pressed: |
| 556 |
{ |
| 557 |
// find the point, move it |
| 558 |
const auto mappedPoints = m_fingerPointMapping.values(); |
| 559 |
QSet<int> activePoints = QSet<int>(mappedPoints.begin(), mappedPoints.end()); |
| 560 |
int activePoint = -1; |
| 561 |
qreal distance = -1; |
| 562 |
const int pointsCount = m_points.size(); |
| 563 |
for (int i=0; i<pointsCount; ++i) { |
| 564 |
if (activePoints.contains(i)) |
| 565 |
continue; |
| 566 |
|
| 567 |
qreal d = QLineF(point.position(), m_points.at(i)).length(); |
| 568 |
if ((distance < 0 && d < 12 * m_pointSize) || d < distance) { |
| 569 |
distance = d; |
| 570 |
activePoint = i; |
| 571 |
} |
| 572 |
} |
| 573 |
if (activePoint != -1) { |
| 574 |
m_fingerPointMapping.insert(point.id(), activePoint); |
| 575 |
m_points[activePoint] = point.position(); |
| 576 |
} |
| 577 |
break; |
| 578 |
} |
| 579 |
case QEventPoint::Released: |
| 580 |
{ |
| 581 |
// move the point and release |
| 582 |
QHash<int,int>::iterator it = m_fingerPointMapping.find(id); |
| 583 |
m_points[it.value()] = point.position(); |
| 584 |
m_fingerPointMapping.erase(it); |
| 585 |
break; |
| 586 |
} |
| 587 |
case QEventPoint::Updated: |
| 588 |
{ |
| 589 |
// move the point |
| 590 |
const int pointIdx = m_fingerPointMapping.value(id, -1); |
| 591 |
if (pointIdx >= 0) |
| 592 |
m_points[pointIdx] = point.position(); |
| 593 |
break; |
| 594 |
} |
| 595 |
default: |
| 596 |
break; |
| 597 |
} |
| 598 |
} |
| 599 |
if (m_fingerPointMapping.isEmpty()) { |
| 600 |
e->ignore(); |
| 601 |
return false; |
| 602 |
} else { |
| 603 |
if (touchBegin) { |
| 604 |
m_wasAnimated = m_timer.isActive(); |
| 605 |
setAnimation(false); |
| 606 |
} |
| 607 |
update(); |
| 608 |
return true; |
| 609 |
} |
| 610 |
} |
| 611 |
break; |
| 612 |
case QEvent::TouchEnd: |
| 613 |
if (m_fingerPointMapping.isEmpty()) { |
| 614 |
e->ignore(); |
| 615 |
return false; |
| 616 |
} |
| 617 |
m_fingerPointMapping.clear(); |
| 618 |
setAnimation(m_wasAnimated); |
| 619 |
return true; |
| 620 |
default: |
| 621 |
break; |
| 622 |
} |
| 623 |
return QWidget::event(e); |
| 624 |
} |
| 625 |
|
| 626 |
void PathStrokeRenderer::setAnimation(bool animation) |
| 627 |
{ |
| 628 |
m_timer.stop(); |
| 629 |
// m_fpsTimer.stop(); |
| 630 |
|
| 631 |
if (animation) { |
| 632 |
m_timer.start(25, this); |
| 633 |
// m_fpsTimer.start(1000, this); |
| 634 |
// m_frameCount = 0; |
| 635 |
} |
| 636 |
} |