File indexing completed on 2024-05-12 05:46:38

0001 /*
0002    Drawpile - a collaborative drawing program.
0003 
0004    Copyright (C) 2017 Calle Laakkonen
0005 
0006    Drawpile is free software: you can redistribute it and/or modify
0007    it under the terms of the GNU General Public License as published by
0008    the Free Software Foundation, either version 3 of the License, or
0009    (at your option) any later version.
0010 
0011    Drawpile is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014    GNU General Public License for more details.
0015 
0016    You should have received a copy of the GNU General Public License
0017    along with Drawpile.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "tablettest.h"
0021 
0022 #include <QPaintEvent>
0023 #include <QPainter>
0024 
0025 TabletTester::TabletTester(QWidget *parent)
0026     : QWidget(parent), m_mouseDown(false), m_tabletDown(false)
0027 {
0028 }
0029 
0030 QSize TabletTester::sizeHint() const
0031 {
0032     return QSize(500, 200);
0033 }
0034 
0035 void TabletTester::clear()
0036 {
0037     m_mousePath.clear();
0038     m_tabletPath.clear();
0039     update();
0040 }
0041 
0042 void TabletTester::paintEvent(QPaintEvent *e)
0043 {
0044     Q_UNUSED(e);
0045     const int w = width();
0046     const int h = height();
0047     QPainter p(this);
0048     p.fillRect(0, 0, w, h, QColor(200, 200, 200));
0049     p.setPen(QColor(128, 128, 128));
0050 
0051     // Draw grid
0052     for(int i=w/8;i<w;i+=w/8)
0053         p.drawLine(i, 0, i, h);
0054     for(int i=h/8;i<h;i+=h/8)
0055         p.drawLine(0, i, w, i);
0056 
0057     // Draw paths
0058     if(!m_mousePath.isEmpty()) {
0059         p.setPen(QPen(Qt::red, 3));
0060         p.drawPolyline(m_mousePath);
0061     }
0062     if(!m_tabletPath.isEmpty()) {
0063         p.setPen(QPen(Qt::blue, 2));
0064         p.drawPolyline(m_tabletPath);
0065     }
0066 }
0067 
0068 void TabletTester::mousePressEvent(QMouseEvent *e)
0069 {
0070     Q_EMIT eventReport(QString("Mouse press X=%1 Y=%2 B=%3").arg(e->x()).arg(e->y()).arg(e->button()));
0071     m_mouseDown = true;
0072     m_mousePath.clear();
0073     update();
0074 }
0075 
0076 void TabletTester::mouseMoveEvent(QMouseEvent *e)
0077 {
0078     Q_EMIT eventReport(QString("Mouse move X=%1 Y=%2 B=%3").arg(e->x()).arg(e->y()).arg(e->buttons()));
0079     m_mousePath << e->pos();
0080     update();
0081 }
0082 
0083 void TabletTester::mouseReleaseEvent(QMouseEvent *e)
0084 {
0085     Q_UNUSED(e);
0086     Q_EMIT eventReport(QString("Mouse release"));
0087     m_mouseDown = false;
0088 }
0089 
0090 void TabletTester::tabletEvent(QTabletEvent *e)
0091 {
0092     e->accept();
0093 
0094     QString msg;
0095     switch(e->device()) {
0096         case QTabletEvent::Stylus: msg = "Stylus"; break;
0097         default: msg = QString("Device(%1)").arg(e->device()); break;
0098     }
0099 
0100     switch(e->type()) {
0101         case QEvent::TabletMove:
0102             msg += " move";
0103             break;
0104         case QEvent::TabletPress:
0105             msg += " press";
0106             m_tabletPath.clear();
0107             m_tabletDown = true;
0108             break;
0109         case QEvent::TabletRelease:
0110             msg += " release";
0111             m_tabletDown = false;
0112             break;
0113         default:
0114             msg += QString(" event-%1").arg(e->type());
0115             break;
0116     }
0117 
0118     msg += QString(" X=%1 Y=%2 B=%3 P=%4%")
0119         .arg(e->posF().x(), 0, 'f', 2)
0120         .arg(e->posF().y(), 0, 'f', 2)
0121         .arg(e->buttons())
0122         .arg(e->pressure()*100, 0, 'f', 1)
0123         ;
0124 
0125     if(e->type() == QEvent::TabletMove) {
0126         if(m_tabletDown) {
0127             msg += " (DRAW)";
0128             m_tabletPath << e->pos();
0129             update();
0130         } else {
0131             msg += " (HOVER)";
0132         }
0133     }
0134 
0135     Q_EMIT eventReport(msg);
0136 }