File indexing completed on 2025-01-26 05:09:31

0001 /*
0002  * This file is part of the KDE wacomtablet project. For copyright
0003  * information and license terms see the AUTHORS and COPYING files
0004  * in the top-level directory of this distribution.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "calibrationdialog.h"
0021 
0022 #include "logging.h"
0023 #include "screensinfo.h"
0024 #include "x11wacom.h"
0025 
0026 // KDE includes
0027 #include <KLocalizedString>
0028 
0029 // Qt includes
0030 #include <QLabel>
0031 #include <QMouseEvent>
0032 #include <QPainter>
0033 #include <QVBoxLayout>
0034 
0035 using namespace Wacom;
0036 
0037 const int frameGap = 10;
0038 const int boxwidth = 100;
0039 
0040 CalibrationDialog::CalibrationDialog(const QString &toolname, const QString &targetScreen)
0041     : QDialog()
0042     , m_drawCross(0)
0043     , m_toolName(toolname)
0044 {
0045     auto screenList = ScreensInfo::getScreenGeometries();
0046     if (screenList.count() > 1) {
0047         if (screenList.contains(targetScreen)) {
0048             move(screenList.value(targetScreen).topLeft());
0049         } else {
0050             qCWarning(KCM) << "Calibration requested for unknown screen" << targetScreen;
0051         }
0052     }
0053 
0054     setWindowState(Qt::WindowFullScreen);
0055 
0056     m_shiftLeft = frameGap;
0057     m_shiftTop = frameGap;
0058 
0059     m_originaltabletArea = X11Wacom::getMaximumTabletArea(m_toolName);
0060 
0061     QLabel *showInfo = new QLabel();
0062     showInfo->setText(i18n("Please tap into all four corners to calibrate the tablet.\nPress escape to cancel the process."));
0063     showInfo->setAlignment(Qt::AlignCenter);
0064     QVBoxLayout *mainLayout = new QVBoxLayout;
0065     mainLayout->addWidget(showInfo);
0066 
0067     setLayout(mainLayout);
0068 }
0069 
0070 QRect CalibrationDialog::calibratedArea()
0071 {
0072     return m_newtabletArea.toRect();
0073 }
0074 
0075 void CalibrationDialog::paintEvent(QPaintEvent *event)
0076 {
0077     Q_UNUSED(event);
0078 
0079     QPainter painter(this);
0080     painter.setPen(palette().color(QPalette::WindowText));
0081 
0082     // vertical line
0083     painter.drawLine(m_shiftLeft + boxwidth / 2, m_shiftTop, m_shiftLeft + boxwidth / 2, m_shiftTop + boxwidth);
0084 
0085     // horizontal line
0086     painter.drawLine(m_shiftLeft, m_shiftTop + boxwidth / 2, m_shiftLeft + boxwidth, m_shiftTop + boxwidth / 2);
0087 
0088     // draw circle around center
0089     painter.drawEllipse(QPoint(m_shiftLeft + boxwidth / 2, m_shiftTop + boxwidth / 2), 10, 10);
0090 }
0091 
0092 void CalibrationDialog::mousePressEvent(QMouseEvent *event)
0093 {
0094     if (event->pos().x() > m_shiftLeft && event->pos().x() < m_shiftLeft + boxwidth && event->pos().y() > m_shiftTop
0095         && event->pos().y() < m_shiftTop + boxwidth) {
0096         m_drawCross++;
0097 
0098         switch (m_drawCross) {
0099         case 1:
0100             m_topLeft = event->windowPos();
0101             m_shiftLeft = frameGap;
0102             m_shiftTop = size().height() - frameGap - boxwidth;
0103             break;
0104         case 2:
0105             m_bottomLeft = event->windowPos();
0106             m_shiftLeft = size().width() - frameGap - boxwidth;
0107             m_shiftTop = size().height() - frameGap - boxwidth;
0108             break;
0109         case 3:
0110             m_bottomRight = event->windowPos();
0111             m_shiftLeft = size().width() - frameGap - boxwidth;
0112             m_shiftTop = frameGap;
0113             break;
0114         case 4:
0115             m_topRight = event->windowPos();
0116             calculateNewArea();
0117             accept();
0118             break;
0119         }
0120 
0121         update();
0122     }
0123 }
0124 
0125 void CalibrationDialog::calculateNewArea()
0126 {
0127     qreal frameoffset = frameGeometry().height() - size().height();
0128     qreal tabletScreenRatioWidth = m_originaltabletArea.width() / size().width();
0129     qreal tabletScreenRatioHeight = m_originaltabletArea.height() / size().height();
0130 
0131     const qreal clickedX = (m_topLeft.x() + m_bottomLeft.x()) / 2;
0132     const qreal clickedXadjusted = clickedX - frameGap - boxwidth / 2;
0133     const qreal newX = m_originaltabletArea.x() + clickedXadjusted * tabletScreenRatioWidth;
0134 
0135     const qreal clickedY = (m_topLeft.y() + m_topRight.y()) / 2;
0136     const qreal clickedYadjusted = clickedY - frameGap - boxwidth / 2;
0137     const qreal newY = m_originaltabletArea.y() + clickedYadjusted * tabletScreenRatioHeight;
0138 
0139     const qreal clickedXright = (m_topRight.x() + m_bottomRight.x()) / 2;
0140     const qreal newWidth = (clickedXright + frameGap + boxwidth / 2) * tabletScreenRatioWidth;
0141 
0142     const qreal clickedYbottom = (m_bottomRight.y() + m_bottomLeft.y()) / 2;
0143     const qreal newHeight = (clickedYbottom + frameGap + boxwidth / 2 + frameoffset) * tabletScreenRatioHeight;
0144 
0145     m_newtabletArea.setX(newX);
0146     m_newtabletArea.setY(newY);
0147     m_newtabletArea.setRight(newWidth);
0148     m_newtabletArea.setBottom(newHeight);
0149 
0150     qCDebug(KCM) << "Calibration debug:" << frameGeometry() << size() << m_originaltabletArea << m_topLeft << m_bottomLeft << m_topRight << m_bottomRight;
0151     qCDebug(KCM) << "Calibration debug:" << frameoffset << tabletScreenRatioWidth << tabletScreenRatioHeight << clickedX << clickedY << clickedXright
0152                  << clickedYbottom;
0153     qCDebug(KCM) << "Calibration debug:" << m_newtabletArea;
0154 }
0155 
0156 #include "moc_calibrationdialog.cpp"