Warning, file /plasma/khotkeys/kcm_hotkeys/helper_widgets/gesture_drawer.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only
0003     SPDX-FileCopyrightText: 2003 Mike Pilone <mpilone@slac.com>
0004     SPDX-FileCopyrightText: 2003 Lubos Lunak <l.lunak@kde.org>
0005     SPDX-FileCopyrightText: 2009 Michael Jansen <kde@michael-jansen.biz>
0006  */
0007 
0008 #include <QColor>
0009 #include <QGradient>
0010 #include <QPaintEvent>
0011 #include <QPainter>
0012 
0013 #include "gesture_drawer.h"
0014 
0015 GestureDrawer::GestureDrawer(QWidget *parent, const char *name)
0016     : QFrame(parent)
0017     , _data(KHotKeys::StrokePoints())
0018 {
0019     setObjectName(name);
0020     QPalette p;
0021     p.setColor(backgroundRole(), palette().color(QPalette::Base));
0022     setPalette(p);
0023     setFrameStyle(QFrame::Panel | QFrame::Sunken);
0024     setMinimumSize(30, 30);
0025 }
0026 
0027 GestureDrawer::~GestureDrawer()
0028 {
0029 }
0030 
0031 KHotKeys::StrokePoints GestureDrawer::pointData() const
0032 {
0033     return _data;
0034 }
0035 
0036 void GestureDrawer::setPointData(const KHotKeys::StrokePoints &data)
0037 {
0038     _data = data;
0039 
0040     repaint();
0041 }
0042 
0043 void GestureDrawer::paintEvent(QPaintEvent *ev)
0044 {
0045     const int n = _data.size();
0046 
0047     if (n < 2) {
0048         QFrame::paintEvent(ev);
0049         return;
0050     }
0051 
0052     const int border = 6;
0053 
0054     const int l = width() < height() ? width() : height();
0055     const int x_offset = border + (width() - l) / 2;
0056     const int y_offset = border + (height() - l) / 2;
0057 
0058     QPainter p(this);
0059     p.setRenderHint(QPainter::Antialiasing);
0060     QPen pen;
0061     pen.setWidth(4);
0062     pen.setCapStyle(Qt::RoundCap);
0063 
0064     // starting point
0065     double x = x_offset + _data[0].x * (l - 2 * border);
0066     double y = y_offset + _data[0].y * (l - 2 * border);
0067 
0068     for (int i = 0; i < n - 1; i++) {
0069         double nextx = x_offset + _data[i + 1].x * (l - 2 * border);
0070         double nexty = y_offset + _data[i + 1].y * (l - 2 * border);
0071 
0072         QLinearGradient grad(x, y, nextx, nexty);
0073         const int startRed = 0;
0074         const int startGreen = (1 - _data[i].s) * 255;
0075         const int startBlue = _data[i].s * 255;
0076         const int endRed = 0;
0077         const int endGreen = (1 - _data[i + 1].s) * 255;
0078         const int endBlue = _data[i + 1].s * 255;
0079         QColor startColor(startRed, startGreen, startBlue);
0080         QColor endColor(endRed, endGreen, endBlue);
0081         grad.setColorAt(0, startColor);
0082         grad.setColorAt(1, endColor);
0083 
0084         pen.setBrush(grad);
0085         p.setPen(pen);
0086         p.drawLine(x, y, nextx, nexty);
0087 
0088         x = nextx;
0089         y = nexty;
0090     }
0091 
0092     QFrame::paintEvent(ev);
0093 }
0094 
0095 #include "moc_gesture_drawer.cpp"