add more spacing
[personal-kdebase.git] / workspace / kcontrol / hardware / joystick / caldialog.cpp
blobd979a151ac9a670a9d5673277b0014449735f2e8
1 /***************************************************************************
2 * Copyright (C) 2003 by Martin Koller
3 * m.koller@surfeu.at
4 * This file is part of the KDE Control Center Module for Joysticks
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 ***************************************************************************/
22 #include "caldialog.h"
23 #include "joydevice.h"
25 #include <QLabel>
26 #include <QTimer>
27 #include <QApplication>
29 #include <klocale.h>
30 #include <kmessagebox.h>
31 #include <kdebug.h>
32 #include <kvbox.h>
33 //--------------------------------------------------------------
35 CalDialog::CalDialog(QWidget *parent, JoyDevice *joy)
36 : KDialog( parent ),
37 joydev(joy)
39 setObjectName( "calibrateDialog" );
40 setModal( true );
41 setCaption( i18n("Calibration") );
42 setButtons( Cancel | User1 );
43 setDefaultButton( User1 );
44 setButtonGuiItem( User1, KGuiItem( i18n("Next") ) );
45 showButtonSeparator( true );
47 KVBox *main = new KVBox( this );
48 setMainWidget( main );
50 text = new QLabel(main);
51 text->setMinimumHeight(200);
52 valueLbl = new QLabel(main);
53 connect(this,SIGNAL(user1Clicked()),this,SLOT(slotUser1()));
56 //--------------------------------------------------------------
58 void CalDialog::calibrate()
60 text->setText(i18n("Please wait a moment to calculate the precision"));
61 setResult(-1);
62 show();
64 // calibrate precision (which min,max delivers the joystick in its center position)
65 // get values through the normal idle procedure
66 QTimer ti;
67 ti.setSingleShot(true); // single shot
68 ti.start(2000); // in 2 seconds
70 // normally I'd like to hide the 'Next' button in this step,
71 // but it does not work - which means: in the steps after the first,
72 // the 'Next' button does not have the focus (to be the default button)
76 qApp->processEvents(QEventLoop::AllEvents, 2000);
78 while ( ti.isActive() && (result() != QDialog::Rejected) );
80 if ( result() == QDialog::Rejected ) return; // user cancelled the dialog
82 joydev->calcPrecision();
84 int i, lastVal;
85 int min[2], center[2], max[2];
86 QString hint;
88 for (i = 0; i < joydev->numAxes(); i++)
90 if ( i == 0 )
91 hint = i18n("(usually X)");
92 else if ( i == 1 )
93 hint = i18n("(usually Y)");
94 else
95 hint = "";
97 // minimum position
98 text->setText(i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />"
99 "Please move <b>axis %1 %2</b> on your device to the <b>minimum</b> position.<br /><br />"
100 "Press any button on the device or click on the 'Next' button "
101 "to continue with the next step.</qt>", i+1, hint));
102 waitButton(i, true, lastVal);
104 if ( result() == QDialog::Rejected ) return; // user cancelled the dialog
106 joydev->resetMinMax(i, lastVal);
107 if ( result() != -2 ) waitButton(i, false, lastVal);
109 if ( result() == QDialog::Rejected ) return; // user cancelled the dialog
111 min[0] = joydev->axisMin(i);
112 min[1] = joydev->axisMax(i);
114 // center position
115 text->setText(i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />"
116 "Please move <b>axis %1 %2</b> on your device to the <b>center</b> position.<br /><br />"
117 "Press any button on the device or click on the 'Next' button "
118 "to continue with the next step.</qt>", i+1, hint));
119 waitButton(i, true, lastVal);
121 if ( result() == QDialog::Rejected ) return; // user cancelled the dialog
123 joydev->resetMinMax(i, lastVal);
124 if ( result() != -2 ) waitButton(i, false, lastVal);
126 if ( result() == QDialog::Rejected ) return; // user canceled the dialog
128 center[0] = joydev->axisMin(i);
129 center[1] = joydev->axisMax(i);
131 // maximum position
132 text->setText(i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />"
133 "Please move <b>axis %1 %2</b> on your device to the <b>maximum</b> position.<br /><br />"
134 "Press any button on the device or click on the 'Next' button "
135 "to continue with the next step.</qt>", i+1, hint));
136 waitButton(i, true, lastVal);
138 if ( result() == QDialog::Rejected ) return; // user cancelled the dialog
140 joydev->resetMinMax(i, lastVal);
141 if ( result() != -2 ) waitButton(i, false, lastVal);
143 if ( result() == QDialog::Rejected ) return; // user canceled the dialog
145 max[0] = joydev->axisMin(i);
146 max[1] = joydev->axisMax(i);
148 joydev->calcCorrection(i, min, center, max);
151 JoyDevice::ErrorCode ret = joydev->applyCalibration();
153 if ( ret != JoyDevice::SUCCESS )
155 KMessageBox::error(this, joydev->errText(ret), i18n("Communication Error"));
156 reject();
159 KMessageBox::information(this, i18n("You have successfully calibrated your device"), i18n("Calibration Success"));
160 accept();
163 //--------------------------------------------------------------
165 void CalDialog::waitButton(int axis, bool press, int &lastVal)
167 JoyDevice::EventType type;
168 int number, value;
169 bool button = false;
170 lastVal = 0;
172 setResult(-1);
173 // loop until the user presses a button on the device or on the dialog
176 qApp->processEvents(QEventLoop::AllEvents, 100);
178 if ( joydev->getEvent(type, number, value) )
180 button = ( (type == JoyDevice::BUTTON) && (press ? (value == 1) : (value == 0)) );
182 if ( (type == JoyDevice::AXIS) && (number == axis) )
183 valueLbl->setText(i18n("Value Axis %1: %2", axis+1, lastVal = value));
186 while ( !button && (result() == -1) );
189 //--------------------------------------------------------------
190 // Next button
192 void CalDialog::slotUser1()
194 setResult(-2);
197 //--------------------------------------------------------------
199 #include "caldialog.moc"
201 //--------------------------------------------------------------