File indexing completed on 2024-05-12 17:07:10
0001 /* 0002 This file is part of the KDE Control Center Module for Joysticks 0003 0004 SPDX-FileCopyrightText: 2003 Martin Koller <kollix@aon.at> 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "caldialog.h" 0009 #include "joydevice.h" 0010 0011 #include <QApplication> 0012 #include <QDialogButtonBox> 0013 #include <QPushButton> 0014 #include <QTimer> 0015 #include <QVBoxLayout> 0016 0017 #include <KLocalizedString> 0018 #include <KMessageBox> 0019 0020 CalDialog::CalDialog(QWidget *parent, JoyDevice *joy) 0021 : QDialog(parent) 0022 , joydev(joy) 0023 { 0024 setObjectName(QStringLiteral("calibrateDialog")); 0025 setModal(true); 0026 setWindowTitle(i18n("Calibration")); 0027 0028 QVBoxLayout *main = new QVBoxLayout(this); 0029 main->setSpacing(0); 0030 0031 text = new QLabel(this); 0032 text->setMinimumHeight(200); 0033 valueLbl = new QLabel(this); 0034 0035 main->addWidget(text); 0036 main->addWidget(valueLbl); 0037 0038 QDialogButtonBox *buttonBox = new QDialogButtonBox(this); 0039 buttonBox->addButton(QDialogButtonBox::Cancel); 0040 QPushButton *Next = buttonBox->addButton(i18n("Next"), QDialogButtonBox::ApplyRole); 0041 Next->setDefault(true); 0042 main->addWidget(buttonBox); 0043 setLayout(main); 0044 0045 connect(Next, &QPushButton::clicked, this, &CalDialog::slotNext); 0046 } 0047 0048 void CalDialog::calibrate() 0049 { 0050 text->setText(i18n("Please wait a moment to calculate the precision")); 0051 setResult(-1); 0052 show(); 0053 0054 // calibrate precision (which min,max delivers the joystick in its center position) 0055 // get values through the normal idle procedure 0056 QTimer ti; 0057 ti.setSingleShot(true); // single shot 0058 ti.start(2000); // in 2 seconds 0059 0060 // normally I'd like to hide the 'Next' button in this step, 0061 // but it does not work - which means: in the steps after the first, 0062 // the 'Next' button does not have the focus (to be the default button) 0063 0064 do { 0065 qApp->processEvents(QEventLoop::AllEvents, 2000); 0066 } while (ti.isActive() && (result() != QDialog::Rejected)); 0067 0068 if (result() == QDialog::Rejected) 0069 return; // user cancelled the dialog 0070 0071 joydev->calcPrecision(); 0072 0073 int i, lastVal; 0074 int min[2], center[2], max[2]; 0075 QString hint; 0076 0077 for (i = 0; i < joydev->numAxes(); i++) { 0078 if (i == 0) 0079 hint = i18n("(usually X)"); 0080 else if (i == 1) 0081 hint = i18n("(usually Y)"); 0082 else 0083 hint = QString(); 0084 0085 // minimum position 0086 text->setText( 0087 i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />" 0088 "Please move <b>axis %1 %2</b> on your device to the <b>minimum</b> position.<br /><br />" 0089 "Press any button on the device or click on the 'Next' button " 0090 "to continue with the next step.</qt>", 0091 i + 1, 0092 hint)); 0093 waitButton(i, true, lastVal); 0094 0095 if (result() == QDialog::Rejected) 0096 return; // user cancelled the dialog 0097 0098 joydev->resetMinMax(i, lastVal); 0099 if (result() != -2) 0100 waitButton(i, false, lastVal); 0101 0102 if (result() == QDialog::Rejected) 0103 return; // user cancelled the dialog 0104 0105 min[0] = joydev->axisMin(i); 0106 min[1] = joydev->axisMax(i); 0107 0108 // center position 0109 text->setText( 0110 i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />" 0111 "Please move <b>axis %1 %2</b> on your device to the <b>center</b> position.<br /><br />" 0112 "Press any button on the device or click on the 'Next' button " 0113 "to continue with the next step.</qt>", 0114 i + 1, 0115 hint)); 0116 waitButton(i, true, lastVal); 0117 0118 if (result() == QDialog::Rejected) 0119 return; // user cancelled the dialog 0120 0121 joydev->resetMinMax(i, lastVal); 0122 if (result() != -2) 0123 waitButton(i, false, lastVal); 0124 0125 if (result() == QDialog::Rejected) 0126 return; // user canceled the dialog 0127 0128 center[0] = joydev->axisMin(i); 0129 center[1] = joydev->axisMax(i); 0130 0131 // maximum position 0132 text->setText( 0133 i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />" 0134 "Please move <b>axis %1 %2</b> on your device to the <b>maximum</b> position.<br /><br />" 0135 "Press any button on the device or click on the 'Next' button " 0136 "to continue with the next step.</qt>", 0137 i + 1, 0138 hint)); 0139 waitButton(i, true, lastVal); 0140 0141 if (result() == QDialog::Rejected) 0142 return; // user cancelled the dialog 0143 0144 joydev->resetMinMax(i, lastVal); 0145 if (result() != -2) 0146 waitButton(i, false, lastVal); 0147 0148 if (result() == QDialog::Rejected) 0149 return; // user canceled the dialog 0150 0151 max[0] = joydev->axisMin(i); 0152 max[1] = joydev->axisMax(i); 0153 0154 joydev->calcCorrection(i, min, center, max); 0155 } 0156 0157 JoyDevice::ErrorCode ret = joydev->applyCalibration(); 0158 0159 if (ret != JoyDevice::SUCCESS) { 0160 KMessageBox::error(this, joydev->errText(ret), i18n("Communication Error")); 0161 reject(); 0162 } 0163 0164 KMessageBox::information(this, i18n("You have successfully calibrated your device"), i18n("Calibration Success")); 0165 accept(); 0166 } 0167 0168 void CalDialog::waitButton(int axis, bool press, int &lastVal) 0169 { 0170 JoyDevice::EventType type; 0171 int number, value; 0172 bool button = false; 0173 lastVal = 0; 0174 0175 setResult(-1); 0176 // loop until the user presses a button on the device or on the dialog 0177 do { 0178 qApp->processEvents(QEventLoop::AllEvents, 100); 0179 0180 if (joydev->getEvent(type, number, value, true)) { 0181 button = ((type == JoyDevice::BUTTON) && (press ? (value == 1) : (value == 0))); 0182 0183 if ((type == JoyDevice::AXIS) && (number == axis)) 0184 valueLbl->setText(i18n("Value Axis %1: %2", axis + 1, lastVal = value)); 0185 } 0186 } while (!button && (result() == -1)); 0187 } 0188 0189 // Next button 0190 0191 void CalDialog::slotNext() 0192 { 0193 setResult(-2); 0194 }