PageRenderTime 202ms CodeModel.GetById 30ms app.highlight 141ms RepoModel.GetById 20ms app.codeStats 1ms

/ui/ActionDialog.cpp

https://github.com/atraber/RaspExt
C++ | 1262 lines | 909 code | 301 blank | 52 comment | 65 complexity | 8ca723c4f493408f8f96ff9d8956443d MD5 | raw file
   1
   2#include "ui/ActionDialog.h"
   3#include "ui_ActionDialog.h"
   4
   5#include "script/ActionOutput.h"
   6#include "script/ActionOutputLED.h"
   7#include "script/ActionOutputDCMotor.h"
   8#include "script/ActionOutputRelay.h"
   9#include "script/ActionOutputGPO.h"
  10#include "script/ActionOutputStepper.h"
  11#include "script/ActionOutputStepperSoftStop.h"
  12#include "script/ActionOutputStepperRunVelocity.h"
  13#include "script/ActionOutputStepperPositioning.h"
  14#include "script/ActionOutputStepperSetParam.h"
  15#include "script/ActionVariable.h"
  16#include "script/ActionSleep.h"
  17#include "script/ActionCallRule.h"
  18#include "script/ActionMusic.h"
  19#include "hw/HWInput.h"
  20#include "util/Debug.h"
  21
  22#include <QFileDialog>
  23
  24ActionDialog::ActionDialog(QWidget *parent, Script *script) :
  25    QDialog(parent),
  26    ui(new Ui::ActionDialog)
  27{
  28    pi_assert(script != NULL);
  29
  30    ui->setupUi(this);
  31
  32    m_script = script;
  33    m_action = NULL;
  34    m_baseWidget = NULL;
  35
  36    // connect all signals - slots
  37    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(okPressed()));
  38    connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(close()));
  39    connect(ui->comboAction, SIGNAL(currentIndexChanged(int)), this, SLOT(actionChanged(int)));
  40
  41    // default values
  42    this->actionChanged( ui->comboAction->currentIndex() );
  43}
  44
  45void ActionDialog::actionChanged(int index)
  46{
  47    if(m_baseWidget != NULL)
  48    {
  49        ui->frameGrid->removeWidget(m_baseWidget);
  50        delete m_baseWidget;
  51        m_baseWidget = NULL;
  52    }
  53
  54    Action::Type type = (Action::Type)index;
  55
  56    switch(type)
  57    {
  58    case Action::Output:
  59        m_baseWidget = new ActionOutputWidget(this, m_script);
  60        break;
  61
  62    case Action::Var:
  63        m_baseWidget = new ActionVariableWidget(this, m_script);
  64        break;
  65
  66    case Action::Sleep:
  67        m_baseWidget = new ActionSleepWidget(this, m_script);
  68        break;
  69
  70    case Action::CallRule:
  71        m_baseWidget = new ActionCallRuleWidget(this, m_script);
  72        break;
  73    case Action::Music:
  74        m_baseWidget = new ActionMusicWidget(this, m_script);
  75        break;
  76    }
  77
  78    if(m_baseWidget != NULL)
  79    {
  80        ui->frameGrid->addWidget(m_baseWidget, 1, 0, 1, 2);
  81    }
  82}
  83
  84void ActionDialog::okPressed()
  85{
  86    pi_assert(m_action == NULL);
  87    pi_assert(m_baseWidget != NULL);
  88
  89    m_action = ((IActionWidget*)m_baseWidget)->assemble();
  90
  91    if(m_action == NULL)
  92        LOG_WARN(Logger::UI, "Assembling Action failed");
  93
  94    this->done(Accepted);
  95}
  96
  97void ActionDialog::edit(Action *action)
  98{
  99    ui->comboAction->setCurrentIndex(action->getType());
 100    if(m_baseWidget != NULL)
 101    {
 102        m_baseWidget->edit(action);
 103    }
 104}
 105
 106ActionDialog::~ActionDialog()
 107{
 108    delete ui;
 109}
 110
 111
 112
 113ActionOutputWidget::ActionOutputWidget(QWidget* parent, Script *script) : IActionWidget(parent)
 114{
 115    m_baseWidget = NULL;
 116    m_script = script;
 117
 118    m_comboOutput = new QComboBox(this);
 119    m_label = new QLabel("Select input", this);
 120    m_labelType = new QLabel("");
 121
 122    // fill combo box with all outputs
 123    std::list<HWOutput*> listOutputs = script->getOutputList();
 124    for(std::list<HWOutput*>::iterator it = listOutputs.begin(); it != listOutputs.end(); it++)
 125    {
 126        m_comboOutput->addItem((*it)->getName().c_str(), qVariantFromValue((void*)*it));
 127    }
 128
 129    m_layout = new QGridLayout(this);
 130
 131    // remove spacing around widget, it looks kind of odd otherwise
 132    m_layout->setContentsMargins(0, 0, 0, 0);
 133
 134    // add our widgets to the layout
 135    m_layout->addWidget(m_labelType, 0, 0);
 136    m_layout->addWidget(m_comboOutput, 0, 1);
 137    m_layout->addWidget(m_labelType, 1, 0, 1, 2);
 138
 139    this->setLayout(m_layout);
 140
 141    // connect all signals - slots
 142    connect(m_comboOutput, SIGNAL(currentIndexChanged(int)), this, SLOT(outputChanged(int)));
 143
 144    // default values
 145    this->outputChanged( m_comboOutput->currentIndex() );
 146}
 147
 148void ActionOutputWidget::outputChanged(int index)
 149{
 150    // if the combobox is empty, then there is nothing to do here
 151    if(m_comboOutput->count() == 0)
 152        return;
 153
 154    QVariant variant = m_comboOutput->itemData( index );
 155    HWOutput* hw = (HWOutput*)variant.value<void*>();
 156
 157    HWOutput::HWOutputType type = hw->getType();
 158
 159    QString outputtype = "Selected output type is ";
 160    outputtype.append( HWOutput::HWOutputTypeToString( type ).c_str() );
 161
 162    m_labelType->setText( outputtype );
 163
 164    // delete old baseWidget if it exists
 165    if(m_baseWidget != NULL)
 166    {
 167        m_layout->removeWidget(m_baseWidget);
 168        delete m_baseWidget;
 169        m_baseWidget = NULL;
 170    }
 171
 172    switch(type)
 173    {
 174    case HWOutput::Relay:
 175        m_baseWidget = new ActionOutputRelayWidget(this, m_script);
 176        break;
 177    case HWOutput::GPO:
 178        m_baseWidget = new ActionOutputGPOWidget(this, m_script);
 179        break;
 180    case HWOutput::DCMotor:
 181        m_baseWidget = new ActionOutputDCMotorWidget(this, m_script);
 182        break;
 183    case HWOutput::LED:
 184        m_baseWidget = new ActionOutputLEDWidget(this, m_script);
 185        break;
 186    case HWOutput::Stepper:
 187        m_baseWidget = new ActionOutputStepperWidget(this, m_script);
 188        break;
 189
 190    default:
 191        LOG_WARN(Logger::UI, "Unknown output type");
 192        break;
 193    }
 194
 195    // add new baseWidget, this should never be NULL. If it is, somewhere something went wrong
 196    if(m_baseWidget != NULL)
 197    {
 198        m_layout->addWidget(m_baseWidget, 2, 0, 1, 2);
 199    }
 200}
 201
 202void ActionOutputWidget::edit(Action* act)
 203{
 204    ActionOutput* action = (ActionOutput*)act;
 205
 206    QString str = QString::fromStdString( action->getHWName() );
 207
 208    for(int i = 0; i < m_comboOutput->count(); i++)
 209    {
 210        if( m_comboOutput->itemText(i).compare(str, Qt::CaseInsensitive) == 0 )
 211        {
 212            m_comboOutput->setCurrentIndex(i);
 213            break;
 214        }
 215    }
 216
 217    if( m_baseWidget != NULL)
 218    {
 219        m_baseWidget->edit(action);
 220    }
 221}
 222
 223Action* ActionOutputWidget::assemble()
 224{
 225    pi_assert(m_baseWidget != NULL);
 226
 227    ActionOutput* action = (ActionOutput*) ((IActionWidget*)m_baseWidget)->assemble();
 228
 229    action->setHWName( m_comboOutput->currentText().toStdString() );
 230
 231    return action;
 232}
 233
 234
 235ActionOutputRelayWidget::ActionOutputRelayWidget(QWidget* parent, Script *script) : IActionWidget(parent)
 236{
 237    // create comboBox and add items
 238    m_combo = new QComboBox(this);
 239    m_combo->addItem("Off");
 240    m_combo->addItem("On");
 241    m_combo->addItem("Toggle");
 242
 243
 244    m_label = new QLabel("Set relay to", this);
 245
 246    QGridLayout* layout = new QGridLayout(this);
 247
 248    // remove spacing around widget, it looks kind of odd otherwise
 249    layout->setContentsMargins(0, 0, 0, 0);
 250
 251    // add our widgets to the layout
 252    layout->addWidget(m_label, 0, 0);
 253    layout->addWidget(m_combo, 0, 1);
 254
 255    this->setLayout(layout);
 256}
 257
 258Action* ActionOutputRelayWidget::assemble()
 259{
 260    ActionOutputRelay* action = new ActionOutputRelay();
 261
 262    action->setState( (ActionOutputRelay::State)m_combo->currentIndex() );
 263
 264    return action;
 265}
 266
 267void ActionOutputRelayWidget::edit(Action* act)
 268{
 269    ActionOutputRelay* action = (ActionOutputRelay*)act;
 270    m_combo->setCurrentIndex( action->getState());
 271}
 272
 273
 274
 275ActionOutputGPOWidget::ActionOutputGPOWidget(QWidget* parent, Script *script) : IActionWidget(parent)
 276{
 277    // create comboBox and add items
 278    m_combo = new QComboBox(this);
 279    m_combo->addItem("Low");
 280    m_combo->addItem("High");
 281    m_combo->addItem("Toggle");
 282
 283    m_label = new QLabel("Set GPO to", this);
 284
 285    QGridLayout* layout = new QGridLayout(this);
 286
 287    // remove spacing around widget, it looks kind of odd otherwise
 288    layout->setContentsMargins(0, 0, 0, 0);
 289
 290    // add our widgets to the layout
 291    layout->addWidget(m_label, 0, 0);
 292    layout->addWidget(m_combo, 0, 1);
 293
 294    this->setLayout(layout);
 295}
 296
 297Action* ActionOutputGPOWidget::assemble()
 298{
 299    ActionOutputGPO* action = new ActionOutputGPO();
 300
 301    action->setState( (ActionOutputGPO::State)m_combo->currentIndex() );
 302
 303    return action;
 304}
 305
 306void ActionOutputGPOWidget::edit(Action* act)
 307{
 308    ActionOutputGPO* action = (ActionOutputGPO*)act;
 309    m_combo->setCurrentIndex( action->getState());
 310}
 311
 312
 313ActionOutputLEDWidget::ActionOutputLEDWidget(QWidget* parent, Script *script) : IActionWidget(parent)
 314{
 315    m_label = new QLabel("Set brightness of LED to ", this);
 316    m_spinBox = new QSpinBox(this);
 317    m_spinBox->setMinimum(0);
 318    m_spinBox->setMaximum(100);
 319
 320    QGridLayout* layout = new QGridLayout(this);
 321
 322    // remove spacing around widget, it looks kind of odd otherwise
 323    layout->setContentsMargins(0, 0, 0, 0);
 324
 325    // add our widgets to the layout
 326    layout->addWidget(m_label, 0, 0);
 327    layout->addWidget(m_spinBox, 0, 1);
 328
 329    this->setLayout(layout);
 330}
 331
 332Action* ActionOutputLEDWidget::assemble()
 333{
 334    ActionOutputLED* action = new ActionOutputLED();
 335
 336    action->setValue( m_spinBox->value() );
 337
 338    return action;
 339}
 340
 341void ActionOutputLEDWidget::edit(Action* act)
 342{
 343    ActionOutputLED* action = (ActionOutputLED*)act;
 344
 345    m_spinBox->setValue( action->getValue() );
 346}
 347
 348ActionOutputDCMotorWidget::ActionOutputDCMotorWidget(QWidget* parent, Script* script) : IActionWidget(parent)
 349{
 350    m_labelState = new QLabel("Set motor to", this);
 351    // create comboBox and add items
 352    m_comboState = new QComboBox(this);
 353    m_comboState->addItem("Standby");
 354    m_comboState->addItem("Reverse");
 355    m_comboState->addItem("Forward");
 356    m_comboState->addItem("Brake");
 357
 358    m_labelSpeed = new QLabel("Set speed to", this);
 359
 360    // create comboBox to select a fader
 361    m_comboSpeed = new QComboBox(this);
 362    m_comboSpeed->addItem("value");
 363    std::list<HWInput*> listInputs = script->getInputList();
 364    for(std::list<HWInput*>::iterator it = listInputs.begin(); it != listInputs.end(); it++)
 365    {
 366        if( (*it)->getType() == HWInput::Fader )
 367            m_comboSpeed->addItem((*it)->getName().c_str());
 368    }
 369
 370    m_spinSpeed = new QSpinBox(this);
 371    m_spinSpeed->setMaximum(100);
 372    m_spinSpeed->setMinimum(0);
 373
 374    QGridLayout* layout = new QGridLayout(this);
 375
 376    // remove spacing around widget, it looks kind of odd otherwise
 377    layout->setContentsMargins(0, 0, 0, 0);
 378
 379    // add our widgets to the layout
 380    layout->addWidget(m_labelState, 0, 0);
 381    layout->addWidget(m_comboState, 0, 1, 1, 2);
 382    layout->addWidget(m_labelSpeed, 1, 0);
 383    layout->addWidget(m_comboSpeed, 1, 1);
 384    layout->addWidget(m_spinSpeed, 1, 2);
 385
 386    this->setLayout(layout);
 387
 388    // connect all signals-slots
 389    connect(m_comboSpeed, SIGNAL(currentIndexChanged(int)), this, SLOT(speedComboChanged(int)));
 390}
 391
 392void ActionOutputDCMotorWidget::speedComboChanged(int index)
 393{
 394    if( index != 0 )
 395        m_spinSpeed->hide();
 396    else
 397        m_spinSpeed->show();
 398}
 399
 400Action* ActionOutputDCMotorWidget::assemble()
 401{
 402    ActionOutputDCMotor* action = new ActionOutputDCMotor();
 403
 404    action->setState( (HWOutputDCMotor::MotorState)m_comboState->currentIndex() );
 405    action->setSpeed( m_spinSpeed->value() );
 406
 407    if( m_comboSpeed->currentIndex() != 0 )
 408    {
 409        action->setInputName( m_comboSpeed->currentText().toStdString() );
 410    }
 411
 412    return action;
 413}
 414
 415void ActionOutputDCMotorWidget::edit(Action* act)
 416{
 417    ActionOutputDCMotor* action = (ActionOutputDCMotor*)act;
 418
 419    m_comboState->setCurrentIndex( action->getState() );
 420    m_spinSpeed->setValue( action->getSpeed() );
 421
 422
 423    // only set this if it is used
 424    if( !action->getInputName().empty() )
 425    {
 426        QString str = QString::fromStdString( action->getInputName() );
 427
 428        for(int i = 0; i < m_comboSpeed->count(); i++)
 429        {
 430            if( m_comboSpeed->itemText(i).compare(str, Qt::CaseInsensitive) == 0 )
 431            {
 432                m_comboSpeed->setCurrentIndex(i);
 433                break;
 434            }
 435        }
 436    }
 437}
 438
 439ActionOutputStepperWidget::ActionOutputStepperWidget(QWidget* parent, Script* script) : IActionWidget(parent)
 440{
 441    m_baseWidget = NULL;
 442
 443    m_label = new QLabel("Select type", this);
 444
 445    // create comboBox for all possible types
 446    m_combo = new QComboBox(this);
 447    m_combo->addItem("Soft stop");
 448    m_combo->addItem("RunVelocity");
 449    m_combo->addItem("Positioning");
 450    m_combo->addItem("Set parameters");
 451
 452    m_layout = new QGridLayout(this);
 453
 454    // remove spacing around widget, it looks kind of odd otherwise
 455    m_layout->setContentsMargins(0, 0, 0, 0);
 456
 457    // add our widgets to the layout
 458    m_layout->addWidget(m_label, 0, 0);
 459    m_layout->addWidget(m_combo, 0, 1);
 460
 461    this->setLayout(m_layout);
 462
 463    // connect all signals-slots
 464    connect(m_combo, SIGNAL(currentIndexChanged(int)), this, SLOT(typeComboChanged(int)));
 465
 466    // default values
 467    this->typeComboChanged( m_combo->currentIndex() );
 468}
 469
 470void ActionOutputStepperWidget::typeComboChanged(int index)
 471{
 472    if(m_baseWidget != NULL)
 473    {
 474        m_layout->removeWidget(m_baseWidget);
 475        delete m_baseWidget;
 476        m_baseWidget = NULL;
 477    }
 478
 479    switch(index)
 480    {
 481    case ActionOutputStepper::SoftStop:
 482        m_baseWidget = new ActionOutputStepperSoftStopWidget(this, NULL); // we do not need Script in this class, so we just take NULL
 483        break;
 484    case ActionOutputStepper::RunVelocity:
 485        m_baseWidget = new ActionOutputStepperRunVelocityWidget(this, NULL); // we do not need Script in this class, so we just take NULL
 486        break;
 487    case ActionOutputStepper::Positioning:
 488        m_baseWidget = new ActionOutputStepperPositioningWidget(this, NULL); // we do not need Script in this class, so we just take NULL
 489        break;
 490    case ActionOutputStepper::SetParam:
 491        m_baseWidget = new ActionOutputStepperSetParamWidget(this, NULL); // we do not need Script in this class, so we just take NULL
 492        break;
 493    }
 494
 495    // add new baseWidget, this should never be NULL. If it is, somewhere something went wrong
 496    if(m_baseWidget != NULL)
 497    {
 498        m_layout->addWidget(m_baseWidget, 2, 0, 1, 2);
 499    }
 500}
 501
 502Action* ActionOutputStepperWidget::assemble()
 503{
 504    if( m_baseWidget != NULL)
 505        return m_baseWidget->assemble();
 506
 507    return NULL;
 508}
 509
 510void ActionOutputStepperWidget::edit(Action* act)
 511{
 512    ActionOutputStepper* action = (ActionOutputStepper*)act;
 513
 514    m_combo->setCurrentIndex( action->getStepperType() );
 515
 516    if( m_baseWidget != NULL)
 517    {
 518        m_baseWidget->edit(action);
 519    }
 520}
 521
 522ActionOutputStepperSoftStopWidget::ActionOutputStepperSoftStopWidget(QWidget* parent, Script* script) : IActionWidget(parent)
 523{
 524    m_label = new QLabel("Do a soft stop", this);
 525
 526    QGridLayout* layout = new QGridLayout(this);
 527
 528    // remove spacing around widget, it looks kind of odd otherwise
 529    layout->setContentsMargins(0, 0, 0, 0);
 530
 531    // add our widgets to the layout
 532    layout->addWidget(m_label, 0, 0);
 533
 534    this->setLayout(layout);
 535}
 536
 537Action* ActionOutputStepperSoftStopWidget::assemble()
 538{
 539    return new ActionOutputStepperSoftStop();
 540}
 541
 542void ActionOutputStepperSoftStopWidget::edit(Action* act)
 543{
 544    // nothing to do
 545}
 546
 547ActionOutputStepperRunVelocityWidget::ActionOutputStepperRunVelocityWidget(QWidget* parent, Script* script) : IActionWidget(parent)
 548{
 549    m_label = new QLabel("Send command RunVelocity to the stepper", this);
 550
 551    QGridLayout* layout = new QGridLayout(this);
 552
 553    // remove spacing around widget, it looks kind of odd otherwise
 554    layout->setContentsMargins(0, 0, 0, 0);
 555
 556    // add our widgets to the layout
 557    layout->addWidget(m_label, 0, 0);
 558
 559    this->setLayout(layout);
 560}
 561
 562Action* ActionOutputStepperRunVelocityWidget::assemble()
 563{
 564    return new ActionOutputStepperRunVelocity();
 565}
 566
 567void ActionOutputStepperRunVelocityWidget::edit(Action* act)
 568{
 569    // nothing to do
 570}
 571
 572ActionOutputStepperPositioningWidget::ActionOutputStepperPositioningWidget(QWidget* parent, Script* script) : IActionWidget(parent)
 573{
 574    QLabel* labelType = new QLabel("Select positioning type", this);
 575    m_comboType = new QComboBox(this);
 576    m_comboType->addItem("Set Position");
 577    m_comboType->addItem("Set Dual Position");
 578    m_comboType->addItem("Reset Position");
 579
 580    m_label = new QLabel("Set target position", this);
 581    m_spinBox = new QSpinBox(this);
 582    m_spinBox->setMinimum( SHRT_MIN );
 583    m_spinBox->setMaximum( SHRT_MAX );
 584
 585    m_label2 = new QLabel("Set target position 2", this);
 586    m_spinBox2 = new QSpinBox(this);
 587    m_spinBox2->setMinimum( SHRT_MIN );
 588    m_spinBox2->setMaximum( SHRT_MAX );
 589
 590    m_labelVmin = new QLabel("Set Vmin", this);
 591    m_spinVmin = new QSpinBox(this);
 592    m_spinVmin->setMinimum( 0 );
 593    m_spinVmin->setMaximum( 15 );
 594
 595    m_labelVmax = new QLabel("Set Vmax", this);
 596    m_spinVmax = new QSpinBox(this);
 597    m_spinVmax->setMinimum( 0 );
 598    m_spinVmax->setMaximum( 15 );
 599
 600    QGridLayout* layout = new QGridLayout(this);
 601
 602    // remove spacing around widget, it looks kind of odd otherwise
 603    layout->setContentsMargins(0, 0, 0, 0);
 604
 605    // add our widgets to the layout
 606    layout->addWidget(labelType, 0, 0);
 607    layout->addWidget(m_comboType, 0, 1);
 608    layout->addWidget(m_label, 1, 0);
 609    layout->addWidget(m_spinBox, 1, 1);
 610    layout->addWidget(m_label2, 2, 0);
 611    layout->addWidget(m_spinBox2, 2, 1);
 612
 613    layout->addWidget(m_labelVmin, 3, 0);
 614    layout->addWidget(m_spinVmin, 3, 1);
 615    layout->addWidget(m_labelVmax, 4, 0);
 616    layout->addWidget(m_spinVmax, 4, 1);
 617
 618    this->setLayout(layout);
 619
 620    // connect all signals - slots
 621    connect(m_comboType, SIGNAL(currentIndexChanged(int)), this, SLOT(comboTypeChanged(int)));
 622
 623    // set default values
 624    this->comboTypeChanged( m_comboType->currentIndex() );
 625}
 626
 627void ActionOutputStepperPositioningWidget::comboTypeChanged(int index)
 628{
 629    if(index == ActionOutputStepperPositioning::SetPosition)
 630    {
 631        m_label->setText("Set target position");
 632
 633        m_label->show();
 634        m_spinBox->show();
 635        m_label2->hide();
 636        m_spinBox2->hide();
 637        m_labelVmin->hide();
 638        m_spinVmin->hide();
 639        m_labelVmax->hide();
 640        m_spinVmax->hide();
 641    }
 642    else if(index == ActionOutputStepperPositioning::SetDualPosition)
 643    {
 644        m_label->setText("Set target position 1");
 645
 646        m_label->show();
 647        m_spinBox->show();
 648        m_label2->show();
 649        m_spinBox2->show();
 650        m_labelVmin->show();
 651        m_spinVmin->show();
 652        m_labelVmax->show();
 653        m_spinVmax->show();
 654    }
 655    else // index == ActionOutputStepperPositioning::ResetPosition
 656    {
 657        m_label->hide();
 658        m_spinBox->hide();
 659        m_label2->hide();
 660        m_spinBox2->hide();
 661        m_labelVmin->hide();
 662        m_spinVmin->hide();
 663        m_labelVmax->hide();
 664        m_spinVmax->hide();
 665    }
 666}
 667
 668Action* ActionOutputStepperPositioningWidget::assemble()
 669{
 670    ActionOutputStepperPositioning* action =  new ActionOutputStepperPositioning();
 671
 672    action->setPositioningType( (ActionOutputStepperPositioning::PositioningType) m_comboType->currentIndex() );
 673    action->setDualPosition( m_spinBox->value(), m_spinBox2->value(), m_spinVmin->value(), m_spinVmax->value());
 674    action->setPosition( m_spinBox->value() );
 675
 676    return action;
 677}
 678
 679void ActionOutputStepperPositioningWidget::edit(Action* act)
 680{
 681    ActionOutputStepperPositioning* action = (ActionOutputStepperPositioning*)act;
 682
 683    m_comboType->setCurrentIndex( action->getPositioningType() );
 684    m_spinBox->setValue( action->getPosition() );
 685    m_spinBox2->setValue( action->getDualPosition2() );
 686    m_spinVmin->setValue( action->getDualVmin() );
 687    m_spinVmax->setValue( action->getDualVmax() );
 688}
 689
 690ActionOutputStepperSetParamWidget::ActionOutputStepperSetParamWidget(QWidget* parent, Script* script) : IActionWidget(parent)
 691{
 692    QGridLayout* layout = new QGridLayout(this);
 693
 694    // remove spacing around widget, it looks kind of odd otherwise
 695    layout->setContentsMargins(0, 0, 0, 0);
 696
 697    unsigned int i = 0;
 698
 699    QLabel* m_labelIrun = new QLabel("Irun", this);
 700    m_comboIrun = new QComboBox(this);
 701    m_comboIrun->addItem("keep");
 702    m_comboIrun->addItem("set");
 703    m_spinIrun = new QSpinBox(this);
 704    m_spinIrun->setMinimum(0);
 705    m_spinIrun->setMaximum(0xF);
 706
 707    layout->addWidget(m_labelIrun, i, 0);
 708    layout->addWidget(m_comboIrun, i, 1);
 709    layout->addWidget(m_spinIrun, i, 2);
 710    i++;
 711
 712
 713    QLabel* m_labelIhold = new QLabel("Ihold", this);
 714    m_comboIhold = new QComboBox(this);
 715    m_comboIhold->addItem("keep");
 716    m_comboIhold->addItem("set");
 717    m_spinIhold = new QSpinBox(this);
 718    m_spinIhold->setMinimum(0);
 719    m_spinIhold->setMaximum(0xF);
 720
 721    layout->addWidget(m_labelIhold, i, 0);
 722    layout->addWidget(m_comboIhold, i, 1);
 723    layout->addWidget(m_spinIhold, i, 2);
 724    i++;
 725
 726
 727    QLabel* m_labelVmax = new QLabel("Vmax", this);
 728    m_comboVmax = new QComboBox(this);
 729    m_comboVmax->addItem("keep");
 730    m_comboVmax->addItem("set");
 731    m_spinVmax = new QSpinBox(this);
 732    m_spinVmax->setMinimum(0);
 733    m_spinVmax->setMaximum(0xF);
 734
 735    layout->addWidget(m_labelVmax, i, 0);
 736    layout->addWidget(m_comboVmax, i, 1);
 737    layout->addWidget(m_spinVmax, i, 2);
 738    i++;
 739
 740
 741    QLabel* m_labelVmin = new QLabel("Vmin", this);
 742    m_comboVmin = new QComboBox(this);
 743    m_comboVmin->addItem("keep");
 744    m_comboVmin->addItem("set");
 745    m_spinVmin = new QSpinBox(this);
 746    m_spinVmin->setMinimum(0);
 747    m_spinVmin->setMaximum(0xF);
 748
 749    layout->addWidget(m_labelVmin, i, 0);
 750    layout->addWidget(m_comboVmin, i, 1);
 751    layout->addWidget(m_spinVmin, i, 2);
 752    i++;
 753
 754
 755    QLabel* m_labelMinSamples = new QLabel("Min Samples", this);
 756    m_comboMinSamples = new QComboBox(this);
 757    m_comboMinSamples->addItem("keep");
 758    m_comboMinSamples->addItem("set");
 759    m_spinMinSamples = new QSpinBox(this);
 760    m_spinMinSamples->setMinimum(0);
 761    m_spinMinSamples->setMaximum(0x7);
 762
 763    layout->addWidget(m_labelMinSamples, i, 0);
 764    layout->addWidget(m_comboMinSamples, i, 1);
 765    layout->addWidget(m_spinMinSamples, i, 2);
 766    i++;
 767
 768
 769    QLabel* m_labelShaft = new QLabel("Shaft", this);
 770    m_comboShaft = new QComboBox(this);
 771    m_comboShaft->addItem("keep");
 772    m_comboShaft->addItem("set");
 773    m_spinShaft = new QSpinBox(this);
 774    m_spinShaft->setMinimum(0);
 775    m_spinShaft->setMaximum(1);
 776
 777    layout->addWidget(m_labelShaft, i, 0);
 778    layout->addWidget(m_comboShaft, i, 1);
 779    layout->addWidget(m_spinShaft, i, 2);
 780    i++;
 781
 782
 783    QLabel* m_labelAcc = new QLabel("Acceleration", this);
 784    m_comboAcc = new QComboBox(this);
 785    m_comboAcc->addItem("keep");
 786    m_comboAcc->addItem("set");
 787    m_spinAcc = new QSpinBox(this);
 788    m_spinAcc->setMinimum(0);
 789    m_spinAcc->setMaximum(0xF);
 790
 791    layout->addWidget(m_labelAcc, i, 0);
 792    layout->addWidget(m_comboAcc, i, 1);
 793    layout->addWidget(m_spinAcc, i, 2);
 794    i++;
 795
 796
 797    QLabel* m_labelAbsThr = new QLabel("Absolut Threshold", this);
 798    m_comboAbsThr = new QComboBox(this);
 799    m_comboAbsThr->addItem("keep");
 800    m_comboAbsThr->addItem("set");
 801    m_spinAbsThr = new QSpinBox(this);
 802    m_spinAbsThr->setMinimum(0);
 803    m_spinAbsThr->setMaximum(0xF);
 804
 805    layout->addWidget(m_labelAbsThr, i, 0);
 806    layout->addWidget(m_comboAbsThr, i, 1);
 807    layout->addWidget(m_spinAbsThr, i, 2);
 808    i++;
 809
 810
 811    QLabel* m_labelDelThr = new QLabel("Delta Threshold", this);
 812    m_comboDelThr = new QComboBox(this);
 813    m_comboDelThr->addItem("keep");
 814    m_comboDelThr->addItem("set");
 815    m_spinDelThr = new QSpinBox(this);
 816    m_spinDelThr->setMinimum(0);
 817    m_spinDelThr->setMaximum(0xF);
 818
 819    layout->addWidget(m_labelDelThr, i, 0);
 820    layout->addWidget(m_comboDelThr, i, 1);
 821    layout->addWidget(m_spinDelThr, i, 2);
 822    i++;
 823
 824    QLabel* m_labelFS2StallEn = new QLabel("FS2 Stall Enabled", this);
 825    m_comboFS2StallEn = new QComboBox(this);
 826    m_comboFS2StallEn->addItem("keep");
 827    m_comboFS2StallEn->addItem("set");
 828    m_spinFS2StallEn = new QSpinBox(this);
 829    m_spinFS2StallEn->setMinimum(0);
 830    m_spinFS2StallEn->setMaximum(0x7);
 831
 832    layout->addWidget(m_labelFS2StallEn, i, 0);
 833    layout->addWidget(m_comboFS2StallEn, i, 1);
 834    layout->addWidget(m_spinFS2StallEn, i, 2);
 835    i++;
 836
 837    QLabel* m_labelAccShape = new QLabel("Acceleration Shape", this);
 838    m_comboAccShape = new QComboBox(this);
 839    m_comboAccShape->addItem("keep");
 840    m_comboAccShape->addItem("set");
 841    m_spinAccShape = new QSpinBox(this);
 842    m_spinAccShape->setMinimum(0);
 843    m_spinAccShape->setMaximum(1);
 844
 845    layout->addWidget(m_labelAccShape, i, 0);
 846    layout->addWidget(m_comboAccShape, i, 1);
 847    layout->addWidget(m_spinAccShape, i, 2);
 848    i++;
 849
 850    QLabel* m_labelStepMode= new QLabel("Step Mode", this);
 851    m_comboStepMode = new QComboBox(this);
 852    m_comboStepMode->addItem("keep");
 853    m_comboStepMode->addItem("set");
 854    m_spinStepMode = new QSpinBox(this);
 855    m_spinStepMode->setMinimum(0);
 856    m_spinStepMode->setMaximum(3);
 857
 858    layout->addWidget(m_labelStepMode, i, 0);
 859    layout->addWidget(m_comboStepMode, i, 1);
 860    layout->addWidget(m_spinStepMode, i, 2);
 861    i++;
 862
 863    QLabel* m_labelDC100StEn= new QLabel("DC100StEn", this);
 864    m_comboDC100StEn = new QComboBox(this);
 865    m_comboDC100StEn->addItem("keep");
 866    m_comboDC100StEn->addItem("set");
 867    m_spinDC100StEn = new QSpinBox(this);
 868    m_spinDC100StEn->setMinimum(0);
 869    m_spinDC100StEn->setMaximum(1);
 870
 871    layout->addWidget(m_labelDC100StEn, i, 0);
 872    layout->addWidget(m_comboDC100StEn, i, 1);
 873    layout->addWidget(m_spinDC100StEn, i, 2);
 874    i++;
 875
 876
 877    QLabel* m_labelPWMJEn= new QLabel("PWM Jitter Enable", this);
 878    m_comboPWMJEn = new QComboBox(this);
 879    m_comboPWMJEn->addItem("keep");
 880    m_comboPWMJEn->addItem("set");
 881    m_spinPWMJEn = new QSpinBox(this);
 882    m_spinPWMJEn->setMinimum(0);
 883    m_spinPWMJEn->setMaximum(1);
 884
 885    layout->addWidget(m_labelPWMJEn, i, 0);
 886    layout->addWidget(m_comboPWMJEn, i, 1);
 887    layout->addWidget(m_spinPWMJEn, i, 2);
 888    i++;
 889
 890
 891    QLabel* m_labelPWMfreq= new QLabel("PWM Frequency", this);
 892    QLabel* m_labelPWMfreq2 = new QLabel("set");
 893    m_spinPWMfreq = new QSpinBox(this);
 894    m_spinPWMfreq->setMinimum(0);
 895    m_spinPWMfreq->setMaximum(1);
 896
 897    layout->addWidget(m_labelPWMfreq, i, 0);
 898    layout->addWidget(m_labelPWMfreq2, i, 1);
 899    layout->addWidget(m_spinPWMfreq, i, 2);
 900    i++;
 901
 902
 903    QLabel* m_labelSecPos = new QLabel("Secure Position", this);
 904    m_comboSecPos = new QComboBox(this);
 905    m_comboSecPos->addItem("keep");
 906    m_comboSecPos->addItem("set");
 907    m_spinSecPos = new QSpinBox(this);
 908    m_spinSecPos->setMinimum(0);
 909    m_spinSecPos->setMaximum(0x03FF);
 910
 911    layout->addWidget(m_labelSecPos, i, 0);
 912    layout->addWidget(m_comboSecPos, i, 1);
 913    layout->addWidget(m_spinSecPos, i, 2);
 914    i++;
 915
 916    this->setLayout(layout);
 917}
 918
 919Action* ActionOutputStepperSetParamWidget::assemble()
 920{
 921    ActionOutputStepperSetParam* action = new ActionOutputStepperSetParam();
 922
 923    HWOutputStepper::Param param;
 924
 925    param.irunSet = m_comboIrun->currentIndex();
 926    param.irun = m_spinIrun->value();
 927
 928    param.iholdSet = m_comboIhold->currentIndex();
 929    param.ihold = m_spinIhold->value();
 930
 931    param.vmaxSet = m_comboVmax->currentIndex();
 932    param.vmax = m_spinVmax->value();
 933
 934    param.vminSet = m_comboVmin->currentIndex();
 935    param.vmin = m_spinVmin->value();
 936
 937    param.accShapeSet = m_comboAccShape->currentIndex();
 938    param.accShape = m_spinAccShape->value();
 939
 940    param.stepModeSet = m_comboStepMode->currentIndex();
 941    param.stepMode = m_spinStepMode->value();
 942
 943    param.shaftSet = m_comboShaft->currentIndex();
 944    param.shaft = m_spinShaft->value();
 945
 946    param.accSet = m_comboAcc->currentIndex();
 947    param.acc = m_spinAcc->value();
 948
 949    param.absoluteThresholdSet = m_comboAbsThr->currentIndex();
 950    param.absoluteThreshold = m_spinAbsThr->value();
 951
 952    param.deltaThresholdSet = m_comboDelThr->currentIndex();
 953    param.deltaThreshold = m_spinDelThr->value();
 954
 955    param.securePositionSet = m_comboSecPos->currentIndex();
 956    param.securePosition = m_spinSecPos->value();
 957
 958    param.fs2StallEnabledSet = m_comboFS2StallEn->currentIndex();
 959    param.fs2StallEnabled = m_spinFS2StallEn->value();
 960
 961    param.minSamplesSet = m_comboMinSamples->currentIndex();
 962    param.minSamples = m_spinMinSamples->value();
 963
 964    param.dc100StallEnableSet = m_comboDC100StEn->currentIndex();
 965    param.dc100StallEnable = m_spinDC100StEn->value();
 966
 967    param.PWMJitterEnableSet = m_comboPWMJEn->currentIndex();
 968    param.PWMJitterEnable = m_spinPWMJEn->value();
 969
 970    param.PWMfreqSet = true; // TODO: Make this adjustable
 971    param.PWMfreq = m_spinPWMfreq->value();
 972
 973    action->setParam(param);
 974
 975    return action;
 976}
 977
 978void ActionOutputStepperSetParamWidget::edit(Action* act)
 979{
 980    ActionOutputStepperSetParam* action = (ActionOutputStepperSetParam*)act;
 981    HWOutputStepper::Param param = action->getParam();
 982
 983    m_comboIrun->setCurrentIndex( param.irunSet );
 984    m_spinIrun->setValue( param.irun );
 985
 986    m_comboIhold->setCurrentIndex( param.iholdSet );
 987    m_spinIhold->setValue( param.ihold );
 988
 989    m_comboVmax->setCurrentIndex( param.vmaxSet );
 990    m_spinVmax->setValue( param.vmax );
 991
 992    m_comboVmin->setCurrentIndex( param.vminSet );
 993    m_spinVmin->setValue( param.vmin );
 994
 995    m_comboAccShape->setCurrentIndex( param.accShapeSet );
 996    m_spinAccShape->setValue( param.accShape );
 997
 998    m_comboStepMode->setCurrentIndex( param.stepModeSet );
 999    m_spinStepMode->setValue( param.stepMode );
1000
1001    m_comboShaft->setCurrentIndex( param.shaftSet );
1002    m_spinShaft->setValue( param.shaft );
1003
1004    m_comboAcc->setCurrentIndex( param.accSet );
1005    m_spinAcc->setValue( param.acc );
1006
1007    m_comboAbsThr->setCurrentIndex( param.absoluteThresholdSet );
1008    m_spinAbsThr->setValue( param.absoluteThreshold );
1009
1010    m_comboDelThr->setCurrentIndex( param.deltaThresholdSet );
1011    m_spinDelThr->setValue( param.deltaThreshold );
1012
1013    m_comboSecPos->setCurrentIndex( param.securePositionSet );
1014    m_spinSecPos->setValue( param.securePosition );
1015
1016    m_comboFS2StallEn->setCurrentIndex( param.fs2StallEnabledSet );
1017    m_spinFS2StallEn->setValue( param.fs2StallEnabled );
1018
1019    m_comboMinSamples->setCurrentIndex( param.minSamplesSet );
1020    m_spinMinSamples->setValue( param.minSamples );
1021
1022    m_comboDC100StEn->setCurrentIndex( param.dc100StallEnableSet );
1023    m_spinDC100StEn->setValue( param.dc100StallEnable );
1024
1025    m_comboPWMJEn->setCurrentIndex( param.PWMJitterEnableSet );
1026    m_spinPWMJEn->setValue( param.PWMJitterEnable );
1027
1028    m_spinPWMfreq->setValue( param.PWMfreq );
1029}
1030
1031
1032ActionVariableWidget::ActionVariableWidget(QWidget* parent, Script *script) : IActionWidget(parent)
1033{
1034    m_combo = new QComboBox(this);
1035    m_label = new QLabel("Select variable", this);
1036
1037    // fill combo box with all variables
1038    std::list<Variable*> listVariable = script->getVariableList();
1039    for(std::list<Variable*>::iterator it = listVariable.begin(); it != listVariable.end(); it++)
1040    {
1041        m_combo->addItem((*it)->getName().c_str());
1042    }
1043
1044    m_comboOperator = new QComboBox(this);
1045    m_comboOperator->addItem("=");
1046    m_comboOperator->addItem("+=");
1047    m_comboOperator->addItem("-=");
1048
1049    m_labelOperator = new QLabel("Set variable", this);
1050
1051    m_spinOperand = new QSpinBox(this);
1052    m_spinOperand->setMinimum(-100);
1053    m_spinOperand->setMaximum(100);
1054
1055
1056    m_layout = new QGridLayout(this);
1057
1058    // remove spacing around widget, it looks kind of odd otherwise
1059    m_layout->setContentsMargins(0, 0, 0, 0);
1060
1061    // add our widgets to the layout
1062    m_layout->addWidget(m_label, 0, 0);
1063    m_layout->addWidget(m_combo, 0, 1, 1, 2);
1064    m_layout->addWidget(m_labelOperator, 1, 0);
1065    m_layout->addWidget(m_comboOperator, 1, 1);
1066    m_layout->addWidget(m_spinOperand, 1, 2);
1067
1068    this->setLayout(m_layout);
1069}
1070
1071void ActionVariableWidget::edit(Action* act)
1072{
1073    ActionVariable* action = (ActionVariable*)act;
1074
1075    QString str = QString::fromStdString( action->getVarName() );
1076
1077    for(int i = 0; i < m_combo->count(); i++)
1078    {
1079        if( m_combo->itemText(i).compare(str, Qt::CaseInsensitive) == 0 )
1080        {
1081            m_combo->setCurrentIndex(i);
1082            break;
1083        }
1084    }
1085
1086    m_comboOperator->setCurrentIndex( action->getOperator() );
1087    m_spinOperand->setValue( action->getOperand() );
1088}
1089
1090Action* ActionVariableWidget::assemble()
1091{
1092    ActionVariable* action = new ActionVariable();
1093
1094    action->setVarName( m_combo->currentText().toStdString() );
1095    action->setOperator( (ActionVariable::Operator)m_comboOperator->currentIndex() );
1096    action->setOperand( m_spinOperand->value() );
1097
1098    return action;
1099}
1100
1101ActionSleepWidget::ActionSleepWidget(QWidget* parent, Script *script) : IActionWidget(parent)
1102{
1103    m_label = new QLabel("Sleep for", this);
1104
1105    m_spinWaitMs = new QSpinBox(this);
1106    m_spinWaitMs->setMinimum(0);
1107    m_spinWaitMs->setMaximum(30 * 1000);
1108
1109    m_labelMs = new QLabel("ms", this);
1110
1111
1112    m_layout = new QGridLayout(this);
1113
1114    // remove spacing around widget, it looks kind of odd otherwise
1115    m_layout->setContentsMargins(0, 0, 0, 0);
1116
1117    // add our widgets to the layout
1118    m_layout->addWidget(m_label, 0, 0);
1119    m_layout->addWidget(m_spinWaitMs, 0, 1);
1120    m_layout->addWidget(m_labelMs, 0, 2);
1121
1122    this->setLayout(m_layout);
1123}
1124
1125void ActionSleepWidget::edit(Action* act)
1126{
1127    ActionSleep* action = (ActionSleep*)act;
1128
1129    m_spinWaitMs->setValue( action->getWaitMs() );
1130}
1131
1132Action* ActionSleepWidget::assemble()
1133{
1134    ActionSleep* action = new ActionSleep();
1135
1136    action->setWaitMs( m_spinWaitMs->value() );
1137
1138    return action;
1139}
1140
1141ActionCallRuleWidget::ActionCallRuleWidget(QWidget* parent, Script *script) : IActionWidget(parent)
1142{
1143    m_label = new QLabel("Select rule", this);
1144
1145    m_comboRule = new QComboBox(this);
1146
1147    // fill combo box with all rule names
1148    std::vector<Rule*> list = script->getRuleList();
1149    for(std::vector<Rule*>::iterator it = list.begin(); it != list.end(); it++)
1150    {
1151        m_comboRule->addItem((*it)->getName().c_str());
1152    }
1153
1154
1155    m_layout = new QGridLayout(this);
1156
1157    // remove spacing around widget, it looks kind of odd otherwise
1158    m_layout->setContentsMargins(0, 0, 0, 0);
1159
1160    // add our widgets to the layout
1161    m_layout->addWidget(m_label, 0, 0);
1162    m_layout->addWidget(m_comboRule, 0, 1);
1163
1164    this->setLayout(m_layout);
1165}
1166
1167void ActionCallRuleWidget::edit(Action* act)
1168{
1169    ActionCallRule* action = (ActionCallRule*)act;
1170
1171    QString str = QString::fromStdString( action->getRuleName() );
1172
1173    for(int i = 0; i < m_comboRule->count(); i++)
1174    {
1175        if( m_comboRule->itemText(i).compare(str, Qt::CaseInsensitive) == 0 )
1176        {
1177            m_comboRule->setCurrentIndex(i);
1178            break;
1179        }
1180    }
1181}
1182
1183Action* ActionCallRuleWidget::assemble()
1184{
1185    ActionCallRule* action = new ActionCallRule();
1186
1187    action->setRuleName( m_comboRule->currentText().toStdString() );
1188
1189    return action;
1190}
1191
1192
1193ActionMusicWidget::ActionMusicWidget(QWidget* parent, Script *script) : IActionWidget(parent)
1194{
1195    QLabel* label = new QLabel("Select type", this);
1196
1197    m_combo = new QComboBox(this);
1198    m_combo->addItem( "Play music" );
1199    m_combo->addItem( "Stop music" );
1200
1201    m_labelMusic = new QLabel(this);
1202    m_buttonSelect = new QPushButton("Select", this);
1203
1204    QGridLayout* layout = new QGridLayout(this);
1205
1206    // remove spacing around widget, it looks kind of odd otherwise
1207    layout->setContentsMargins(0, 0, 0, 0);
1208
1209    // add our widgets to the layout
1210    layout->addWidget(label, 0, 0);
1211    layout->addWidget(m_combo, 0, 1);
1212    layout->addWidget(m_labelMusic, 1, 0, 1, 2);
1213    layout->addWidget(m_buttonSelect, 2, 1);
1214
1215    this->setLayout(layout);
1216
1217    // connect all signals-slots
1218    connect(m_buttonSelect, SIGNAL(clicked()), this, SLOT(selectMusic()));
1219    connect(m_combo, SIGNAL(currentIndexChanged(int)), this, SLOT(comboChanged(int)));
1220}
1221
1222void ActionMusicWidget::selectMusic()
1223{
1224    QFileDialog dialog;
1225
1226    dialog.setNameFilter("*.mp3 *.wav *.ogg *.flac");
1227
1228    if( dialog.exec() == QDialog::Accepted )
1229        m_labelMusic->setText( dialog.selectedFiles().front() );
1230}
1231
1232void ActionMusicWidget::comboChanged(int index)
1233{
1234    if(index == ActionMusic::Play)
1235    {
1236        m_labelMusic->show();
1237        m_buttonSelect->show();
1238    }
1239    else
1240    {
1241        m_labelMusic->hide();
1242        m_buttonSelect->hide();
1243    }
1244}
1245
1246void ActionMusicWidget::edit(Action* act)
1247{
1248    ActionMusic* action = (ActionMusic*)act;
1249
1250    m_combo->setCurrentIndex( action->getMusicAction() );
1251    m_labelMusic->setText( QString::fromStdString( action->getFilename() ) );
1252}
1253
1254Action* ActionMusicWidget::assemble()
1255{
1256    ActionMusic* action = new ActionMusic();
1257
1258    action->setMusicAction( (ActionMusic::MusicAction)m_combo->currentIndex() );
1259    action->setFilename( m_labelMusic->text().toStdString() );
1260
1261    return action;
1262}