Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knobs with arcs #2275

Merged
merged 2 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/widget/wknobcomposed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "util/duration.h"
#include "widget/wknobcomposed.h"
#include "widget/wskincolor.h"

WKnobComposed::WKnobComposed(QWidget* pParent)
: WWidget(pParent),
Expand Down Expand Up @@ -45,9 +46,16 @@ void WKnobComposed::setup(const QDomNode& node, const SkinContext& context) {
context.hasNodeSelectDouble(node, "MaxAngle", &m_dMaxAngle);
context.hasNodeSelectDouble(node, "KnobCenterXOffset", &m_dKnobCenterXOffset);
context.hasNodeSelectDouble(node, "KnobCenterYOffset", &m_dKnobCenterYOffset);
m_dArcThickness = context.selectDouble(node, "ArcThickness");

if (m_dArcThickness > 0.0) {
m_arcColor = WSkinColor::getCorrectColor(context.selectColor(node, "ArcColor"));
m_arcUnipolar = context.selectBool(node, "ArcUnipolar", false);
}

m_dKnobCenterXOffset *= scaleFactor;
m_dKnobCenterYOffset *= scaleFactor;
m_dArcThickness *= scaleFactor;
}

void WKnobComposed::clear() {
Expand Down Expand Up @@ -101,16 +109,23 @@ void WKnobComposed::paintEvent(QPaintEvent* e) {
m_pPixmapBack->draw(rect(), &p, m_pPixmapBack->rect());
}

if ((!m_pKnob.isNull() && !m_pKnob->isNull()) || m_dArcThickness > 0.0) {
// We update m_dCurrentAngle since onConnectedControlChanged uses it for
// no-op detection.
m_dCurrentAngle = m_dMinAngle + (m_dMaxAngle - m_dMinAngle) * getControlParameterDisplay();
}

if (m_dArcThickness > 0.0) {
drawArc(rect(), &p);
}

QTransform transform;
if (!m_pKnob.isNull() && !m_pKnob->isNull()) {
qreal tx = m_dKnobCenterXOffset + width() / 2.0;
qreal ty = m_dKnobCenterYOffset + height() / 2.0;
transform.translate(-tx, -ty);
p.translate(tx, ty);

// We update m_dCurrentAngle since onConnectedControlChanged uses it for
// no-op detection.
m_dCurrentAngle = m_dMinAngle + (m_dMaxAngle - m_dMinAngle) * getControlParameterDisplay();
p.rotate(m_dCurrentAngle);

// Need to convert from QRect to a QRectF to avoid losing precision.
Expand All @@ -120,6 +135,21 @@ void WKnobComposed::paintEvent(QPaintEvent* e) {
}
}

void WKnobComposed::drawArc(const QRectF& targetRect, QPainter* pPainter) {
QMargins margins = QMargins();
margins += m_dArcThickness / 2.0;
QRectF rect = targetRect.marginsRemoved(margins);
QPen pen = QPen(m_arcColor);
pen.setWidth(m_dArcThickness);
pen.setCapStyle(Qt::FlatCap);
pPainter->setPen(pen);
if (m_arcUnipolar) {
pPainter->drawArc(rect, 90 * 16 - m_dMinAngle * 16, (m_dCurrentAngle - m_dMinAngle) * -16);
} else {
pPainter->drawArc(rect, 90 * 16, m_dCurrentAngle * -16);
}
}

void WKnobComposed::mouseMoveEvent(QMouseEvent* e) {
m_handler.mouseMoveEvent(this, e);
}
Expand Down
6 changes: 6 additions & 0 deletions src/widget/wknobcomposed.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class WKnobComposed : public WWidget {
PixmapSource source,
Paintable::DrawMode mode,
double scaleFactor);
void drawArc(
const QRectF& targetRect,
QPainter* pPainter);

double m_dCurrentAngle;
PaintablePointer m_pKnob;
Expand All @@ -54,6 +57,9 @@ class WKnobComposed : public WWidget {
double m_dMaxAngle;
double m_dKnobCenterXOffset;
double m_dKnobCenterYOffset;
double m_dArcThickness;
QColor m_arcColor;
bool m_arcUnipolar;
WidgetRenderTimer m_renderTimer;

friend class KnobEventHandler<WKnobComposed>;
Expand Down