| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "pentool.h" |
| 5 |
#include <QtWidgets/QPushButton> |
| 6 |
#include <QtWidgets/QDoubleSpinBox> |
| 7 |
#include <QtWidgets/QComboBox> |
| 8 |
#include <QtWidgets/QFormLayout> |
| 9 |
#include <QtWidgets/QColorDialog> |
| 10 |
|
| 11 |
PenTool::PenTool(QString title, QWidget *parent) |
| 12 |
: QWidget(parent) |
| 13 |
{ |
| 14 |
setWindowTitle(title); |
| 15 |
setWindowFlags(Qt::Tool); |
| 16 |
|
| 17 |
m_colorButton = new QPushButton(); |
| 18 |
|
| 19 |
m_widthSpinBox = new QDoubleSpinBox(); |
| 20 |
|
| 21 |
m_styleCombo = new QComboBox(); |
| 22 |
m_styleCombo->addItem("NoPen"); |
| 23 |
m_styleCombo->addItem("SolidLine"); |
| 24 |
m_styleCombo->addItem("DashLine"); |
| 25 |
m_styleCombo->addItem("DotLine"); |
| 26 |
m_styleCombo->addItem("DashDotLine"); |
| 27 |
m_styleCombo->addItem("DashDotDotLine"); |
| 28 |
|
| 29 |
m_capStyleCombo = new QComboBox(); |
| 30 |
m_capStyleCombo->addItem("FlatCap", Qt::FlatCap); |
| 31 |
m_capStyleCombo->addItem("SquareCap", Qt::SquareCap); |
| 32 |
m_capStyleCombo->addItem("RoundCap", Qt::RoundCap); |
| 33 |
|
| 34 |
m_joinStyleCombo = new QComboBox(); |
| 35 |
m_joinStyleCombo->addItem("MiterJoin", Qt::MiterJoin); |
| 36 |
m_joinStyleCombo->addItem("BevelJoin", Qt::BevelJoin); |
| 37 |
m_joinStyleCombo->addItem("RoundJoin", Qt::RoundJoin); |
| 38 |
m_joinStyleCombo->addItem("SvgMiterJoin", Qt::SvgMiterJoin); |
| 39 |
|
| 40 |
QFormLayout *layout = new QFormLayout(); |
| 41 |
layout->addRow("Color", m_colorButton); |
| 42 |
layout->addRow("Width", m_widthSpinBox); |
| 43 |
layout->addRow("Style", m_styleCombo); |
| 44 |
layout->addRow("Cap style", m_capStyleCombo); |
| 45 |
layout->addRow("Join style", m_joinStyleCombo); |
| 46 |
setLayout(layout); |
| 47 |
|
| 48 |
connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); |
| 49 |
connect(m_widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateWidth(double))); |
| 50 |
connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle(int))); |
| 51 |
connect(m_capStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateCapStyle(int))); |
| 52 |
connect(m_joinStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateJoinStyle(int))); |
| 53 |
} |
| 54 |
|
| 55 |
void PenTool::setPen(const QPen &pen) |
| 56 |
{ |
| 57 |
m_pen = pen; |
| 58 |
m_colorButton->setText(m_pen.color().name()); |
| 59 |
m_widthSpinBox->setValue(m_pen.widthF()); |
| 60 |
m_styleCombo->setCurrentIndex(m_pen.style()); // index matches the enum |
| 61 |
m_capStyleCombo->setCurrentIndex(m_capStyleCombo->findData(m_pen.capStyle())); |
| 62 |
m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(m_pen.joinStyle())); |
| 63 |
} |
| 64 |
|
| 65 |
QPen PenTool::pen() const |
| 66 |
{ |
| 67 |
return m_pen; |
| 68 |
} |
| 69 |
|
| 70 |
QString PenTool::name() |
| 71 |
{ |
| 72 |
return name(m_pen); |
| 73 |
} |
| 74 |
|
| 75 |
QString PenTool::name(const QPen &pen) |
| 76 |
{ |
| 77 |
return pen.color().name() + ":" + QString::number(pen.widthF()); |
| 78 |
} |
| 79 |
|
| 80 |
void PenTool::showColorDialog() |
| 81 |
{ |
| 82 |
QColorDialog dialog(m_pen.color()); |
| 83 |
dialog.show(); |
| 84 |
dialog.exec(); |
| 85 |
m_pen.setColor(dialog.selectedColor()); |
| 86 |
m_colorButton->setText(m_pen.color().name()); |
| 87 |
emit changed(); |
| 88 |
} |
| 89 |
|
| 90 |
void PenTool::updateWidth(double width) |
| 91 |
{ |
| 92 |
if (!qFuzzyCompare((qreal) width, m_pen.widthF())) { |
| 93 |
m_pen.setWidthF(width); |
| 94 |
emit changed(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void PenTool::updateStyle(int style) |
| 99 |
{ |
| 100 |
if (m_pen.style() != style) { |
| 101 |
m_pen.setStyle((Qt::PenStyle) style); |
| 102 |
emit changed(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void PenTool::updateCapStyle(int index) |
| 107 |
{ |
| 108 |
Qt::PenCapStyle capStyle = (Qt::PenCapStyle) m_capStyleCombo->itemData(index).toInt(); |
| 109 |
if (m_pen.capStyle() != capStyle) { |
| 110 |
m_pen.setCapStyle(capStyle); |
| 111 |
emit changed(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void PenTool::updateJoinStyle(int index) |
| 116 |
{ |
| 117 |
Qt::PenJoinStyle joinStyle = (Qt::PenJoinStyle) m_joinStyleCombo->itemData(index).toInt(); |
| 118 |
if (m_pen.joinStyle() != joinStyle) { |
| 119 |
m_pen.setJoinStyle(joinStyle); |
| 120 |
emit changed(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
#include "moc_pentool.cpp" |