caldialog.cpp 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/***************************************************************************
 *   Copyright (C) 2003 by Martin Koller
 *   kollix@aon.at
 *   This file is part of the KDE Control Center Module for Joysticks
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ***************************************************************************/

#include "caldialog.h"
#include "joydevice.h"

#include <QTimer>
#include <QApplication>
27 28 29
#include <QVBoxLayout>
#include <QPushButton>
#include <QDialogButtonBox>
30 31

#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
32
#include <KMessageBox>
33

34 35 36
//--------------------------------------------------------------

CalDialog::CalDialog(QWidget *parent, JoyDevice *joy)
37
  : QDialog( parent ),
38 39
    joydev(joy)
{
40
  setObjectName( QStringLiteral("calibrateDialog") );
41
  setModal( true );
42
  setWindowTitle( i18n("Calibration") );
43

44 45
  QVBoxLayout *main = new QVBoxLayout( this );
  main->setSpacing(0);
46

47
  text = new QLabel( this );
48
  text->setMinimumHeight(200);
49 50 51 52 53 54 55 56 57 58 59 60 61
  valueLbl = new QLabel( this );

  main->addWidget(text);
  main->addWidget(valueLbl);

  QDialogButtonBox *buttonBox = new QDialogButtonBox( this );
  buttonBox->addButton(QDialogButtonBox::Cancel);
  QPushButton *Next = buttonBox->addButton(i18n("Next"), QDialogButtonBox::ApplyRole);
  Next->setDefault( true );
  main->addWidget(buttonBox);
  setLayout(main);

  connect(Next,&QPushButton::clicked,this,&CalDialog::slotNext);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

//--------------------------------------------------------------

void CalDialog::calibrate()
{
  text->setText(i18n("Please wait a moment to calculate the precision"));
  setResult(-1);
  show();

  // calibrate precision (which min,max delivers the joystick in its center position)
  // get values through the normal idle procedure
  QTimer ti;
  ti.setSingleShot(true); // single shot
  ti.start(2000);         // in 2 seconds

  // normally I'd like to hide the 'Next' button in this step,
  // but it does not work - which means: in the steps after the first,
  // the 'Next' button does not have the focus (to be the default button)

  do
  {
    qApp->processEvents(QEventLoop::AllEvents, 2000);
  }
  while ( ti.isActive() && (result() != QDialog::Rejected) );

  if ( result() == QDialog::Rejected ) return;  // user cancelled the dialog

  joydev->calcPrecision();

  int i, lastVal;
  int min[2], center[2], max[2];
  QString hint;

  for (i = 0; i < joydev->numAxes(); i++)
  {
    if ( i == 0 )
      hint = i18n("(usually X)");
    else if ( i == 1 )
      hint = i18n("(usually Y)");
    else
103
      hint = QLatin1String("");
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

    // minimum position
    text->setText(i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />"
                       "Please move <b>axis %1 %2</b> on your device to the <b>minimum</b> position.<br /><br />"
                       "Press any button on the device or click on the 'Next' button "
                       "to continue with the next step.</qt>", i+1, hint));
    waitButton(i, true, lastVal);

    if ( result() == QDialog::Rejected ) return;  // user cancelled the dialog

    joydev->resetMinMax(i, lastVal);
    if ( result() != -2 ) waitButton(i, false, lastVal);

    if ( result() == QDialog::Rejected ) return;  // user cancelled the dialog

    min[0] = joydev->axisMin(i);
    min[1] = joydev->axisMax(i);

    // center position
    text->setText(i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />"
                       "Please move <b>axis %1 %2</b> on your device to the <b>center</b> position.<br /><br />"
                       "Press any button on the device or click on the 'Next' button "
                       "to continue with the next step.</qt>", i+1, hint));
    waitButton(i, true, lastVal);

    if ( result() == QDialog::Rejected ) return;  // user cancelled the dialog

    joydev->resetMinMax(i, lastVal);
    if ( result() != -2 ) waitButton(i, false, lastVal);

    if ( result() == QDialog::Rejected ) return;  // user canceled the dialog

    center[0] = joydev->axisMin(i);
    center[1] = joydev->axisMax(i);

    // maximum position
    text->setText(i18n("<qt>Calibration is about to check the value range your device delivers.<br /><br />"
                       "Please move <b>axis %1 %2</b> on your device to the <b>maximum</b> position.<br /><br />"
                       "Press any button on the device or click on the 'Next' button "
                       "to continue with the next step.</qt>", i+1, hint));
    waitButton(i, true, lastVal);

    if ( result() == QDialog::Rejected ) return;  // user cancelled the dialog

    joydev->resetMinMax(i, lastVal);
    if ( result() != -2 ) waitButton(i, false, lastVal);

    if ( result() == QDialog::Rejected ) return;  // user canceled the dialog

    max[0] = joydev->axisMin(i);
    max[1] = joydev->axisMax(i);

    joydev->calcCorrection(i, min, center, max);
  }

  JoyDevice::ErrorCode ret = joydev->applyCalibration();

  if ( ret != JoyDevice::SUCCESS )
  {
    KMessageBox::error(this, joydev->errText(ret), i18n("Communication Error"));
    reject();
  }

  KMessageBox::information(this, i18n("You have successfully calibrated your device"), i18n("Calibration Success"));
  accept();
}

//--------------------------------------------------------------

void CalDialog::waitButton(int axis, bool press, int &lastVal)
{
  JoyDevice::EventType type;
  int number, value;
  bool button = false;
  lastVal = 0;

  setResult(-1);
  // loop until the user presses a button on the device or on the dialog
  do
  {
    qApp->processEvents(QEventLoop::AllEvents, 100);

    if ( joydev->getEvent(type, number, value) )
    {
      button = ( (type == JoyDevice::BUTTON) && (press ? (value == 1) : (value == 0)) );

      if ( (type == JoyDevice::AXIS) && (number == axis) )
        valueLbl->setText(i18n("Value Axis %1: %2", axis+1, lastVal = value));
    }
  }
  while ( !button && (result() == -1) );
}

//--------------------------------------------------------------
// Next button

200
void CalDialog::slotNext()
201 202 203 204 205
{
  setResult(-2);
}