Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include "toonzqt/doublefield.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
#include "tunit.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qdoublevalidator></qdoublevalidator>
Toshihiro Shimizu 890ddd
#include <qhboxlayout></qhboxlayout>
Toshihiro Shimizu 890ddd
#include <qfocusevent></qfocusevent>
Toshihiro Shimizu 890ddd
#include <qslider></qslider>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace DVGui;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// DoubleValueLineEdit
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueLineEdit::focusOutEvent(QFocusEvent *e) {
Shinya Kitaoka 120a6e
  double value = getValue();
Shinya Kitaoka 120a6e
  double minValue, maxValue;
Shinya Kitaoka 120a6e
  getRange(minValue, maxValue);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool isOutOfRange;
Shinya Kitaoka 120a6e
  /*---誤差を吸収する---*/
Shinya Kitaoka 120a6e
  MeasuredDoubleLineEdit *lineEdit =
Shinya Kitaoka 120a6e
      qobject_cast<measureddoublelineedit *="">(this);</measureddoublelineedit>
Shinya Kitaoka 120a6e
  if (lineEdit) {
Shinya Kitaoka 120a6e
    int decimal  = lineEdit->getDecimals();
Shinya Kitaoka 120a6e
    isOutOfRange = (value < minValue - pow(0.1, decimal + 1) ||
Shinya Kitaoka 120a6e
                    value > maxValue + pow(0.1, decimal + 1));
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    isOutOfRange = (value < minValue || value > maxValue);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (isOutOfRange) {
Shinya Kitaoka 120a6e
    setValue(tcrop(value, minValue, maxValue));
Shinya Kitaoka 120a6e
    emit editingFinished();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  QLineEdit::focusOutEvent(e);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// DoubleValueField
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
DoubleValueField::DoubleValueField(QWidget *parent,
Shinya Kitaoka 120a6e
                                   DoubleValueLineEdit *lineEdit)
Shinya Kitaoka 120a6e
    : QWidget(parent), m_lineEdit(lineEdit), m_slider(0), m_roller(0) {
Shinya Kitaoka 120a6e
  assert(m_lineEdit);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QWidget *field = new QWidget(this);
Shinya Kitaoka 120a6e
  m_roller       = new RollerField(field);
Shinya Kitaoka 120a6e
  m_slider       = new QSlider(Qt::Horizontal, this);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  field->setMaximumWidth(100);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //---layout
Shinya Kitaoka 120a6e
  QHBoxLayout *layout = new QHBoxLayout(this);
Shinya Kitaoka 120a6e
  layout->setMargin(0);
Shinya Kitaoka 120a6e
  layout->setSpacing(5);
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    QVBoxLayout *vLayout = new QVBoxLayout(field);
Shinya Kitaoka 120a6e
    vLayout->setMargin(0);
Shinya Kitaoka 120a6e
    vLayout->setSpacing(0);
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      vLayout->addWidget(m_lineEdit);
Shinya Kitaoka 120a6e
      vLayout->addWidget(m_roller);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    layout->addWidget(field);
Shinya Kitaoka 120a6e
    layout->addWidget(m_slider);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  setLayout(layout);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //----signal/slot connections
Shinya Kitaoka 120a6e
  bool ret = true;
Shinya Kitaoka 120a6e
  ret      = ret && connect(m_lineEdit, SIGNAL(valueChanged()),
Shinya Kitaoka 120a6e
                       SLOT(onLineEditValueChanged()));
Shinya Kitaoka 120a6e
  ret = ret && connect(m_roller, SIGNAL(valueChanged(bool)),
Shinya Kitaoka 120a6e
                       SLOT(onRollerValueChanged(bool)));
Shinya Kitaoka 120a6e
  ret = ret && connect(m_slider, SIGNAL(valueChanged(int)),
Shinya Kitaoka 120a6e
                       SLOT(onSliderChanged(int)));
Shinya Kitaoka 120a6e
  ret = ret &&
Shinya Kitaoka 120a6e
        connect(m_slider, SIGNAL(sliderReleased()), SLOT(onSliderReleased()));
Shinya Kitaoka 120a6e
  ret = ret && connect(m_lineEdit, SIGNAL(editingFinished()), this,
Shinya Kitaoka 120a6e
                       SIGNAL(valueEditedByHand()));
Shinya Kitaoka 120a6e
  ret = ret && connect(m_slider, SIGNAL(sliderReleased()), this,
Shinya Kitaoka 120a6e
                       SIGNAL(valueEditedByHand()));
Shinya Kitaoka 120a6e
  assert(ret);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_spaceWidget = new QWidget();
Shinya Kitaoka 120a6e
  m_spaceWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
Shinya Kitaoka 120a6e
  layout->addWidget(m_spaceWidget, 1, Qt::AlignLeft);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setRange(-100.0, 100.0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::getRange(double &minValue, double &maxValue) {
Shinya Kitaoka 120a6e
  m_lineEdit->getRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::setRange(double minValue, double maxValue) {
Shinya Kitaoka 120a6e
  m_lineEdit->setRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_roller->setRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int dicimal   = m_lineEdit->getDecimals();
Shinya Kitaoka 120a6e
  int sliderMax = maxValue * pow(10., dicimal);
Shinya Kitaoka 120a6e
  int sliderMin = minValue * pow(10., dicimal);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_slider->setRange(sliderMin, sliderMax);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  enableSlider(maxValue - minValue < 10000);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::setValue(double value) {
Shinya Kitaoka 120a6e
  if (m_lineEdit->getValue() == value) return;
Shinya Kitaoka 120a6e
  m_lineEdit->setValue(value);
Shinya Kitaoka 120a6e
  m_roller->setValue(value);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int dicimal     = m_lineEdit->getDecimals();
Shinya Kitaoka 120a6e
  int sliderValue = value * pow(10., dicimal);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_slider->setValue(sliderValue);
Shinya Kitaoka 120a6e
  // forzo il repaint... non sempre si aggiorna e l'update non sembra risolvere
Shinya Kitaoka 120a6e
  // il ptroblema!!!
Shinya Kitaoka 120a6e
  m_slider->repaint();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double DoubleValueField::getValue() { return (m_lineEdit->getValue()); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::setValues(double value, double minValue,
Shinya Kitaoka 120a6e
                                 double maxValue) {
Shinya Kitaoka 120a6e
  setRange(minValue, maxValue);
Shinya Kitaoka 120a6e
  setValue(value);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::enableSlider(bool enable) {
Shinya Kitaoka 120a6e
  m_slider->setEnabled(enable);
Shinya Kitaoka 120a6e
  m_spaceWidget->setEnabled(!enable);
Shinya Kitaoka 120a6e
  if (enable) {
Shinya Kitaoka 120a6e
    m_slider->show();
Shinya Kitaoka 120a6e
    m_spaceWidget->hide();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_slider->hide();
Shinya Kitaoka 120a6e
    m_spaceWidget->show();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool DoubleValueField::isSliderEnabled() { return m_slider->isEnabled(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::enableRoller(bool enable) {
Shinya Kitaoka 120a6e
  m_roller->setEnabled(enable);
Shinya Kitaoka 120a6e
  if (enable)
Shinya Kitaoka 120a6e
    m_roller->show();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    m_roller->hide();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool DoubleValueField::isRollerEnabled() { return m_roller->isEnabled(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::onSliderChanged(int value) {
Shinya Kitaoka 120a6e
  int dicimal = m_lineEdit->getDecimals();
Shinya Kitaoka 120a6e
  double val  = double(value) * pow(0.1, dicimal);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Controllo necessario per evitare che il segnale di cambiamento venga emesso
Shinya Kitaoka 120a6e
  // piu' volte.
Shinya Kitaoka 120a6e
  if (m_lineEdit->getValue() == val ||
Shinya Kitaoka 120a6e
      (m_roller->getValue() == val && m_roller->isVisible()))
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  m_lineEdit->setValue(val);
Shinya Kitaoka 120a6e
  m_roller->setValue(val);
Shinya Kitaoka 120a6e
  // Faccio in modo che il cursore sia sulla prima cifra, cosi' se la stringa
Shinya Kitaoka 120a6e
  // da visualizzare e' piu' lunga del campo le cifre che vengono troncate sono
Shinya Kitaoka 120a6e
  // le ultime e non le prime (dovrebbero essere quelle dopo la virgola).
Shinya Kitaoka 120a6e
  m_lineEdit->setCursorPosition(0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  emit valueChanged(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::onLineEditValueChanged() {
Shinya Kitaoka 120a6e
  double value       = m_lineEdit->getValue();
Shinya Kitaoka 120a6e
  int dicimal        = m_lineEdit->getDecimals();
Shinya Kitaoka 120a6e
  double sliderValue = value * pow(10., dicimal);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Controllo necessario per evitare che il segnale di cambiamento venga emesso
Shinya Kitaoka 120a6e
  // piu' volte.
Shinya Kitaoka 120a6e
  if ((m_slider->value() == sliderValue && m_slider->isVisible()) ||
Shinya Kitaoka 120a6e
      (m_roller->getValue() == value && m_roller->isVisible()))
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  m_slider->setValue(sliderValue);
Shinya Kitaoka 120a6e
  m_roller->setValue(value);
Shinya Kitaoka 120a6e
  emit valueChanged(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleValueField::onRollerValueChanged(bool isDragging) {
Shinya Kitaoka 120a6e
  double value = m_roller->getValue();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int dicimal        = m_lineEdit->getDecimals();
Shinya Kitaoka 120a6e
  double sliderValue = value * pow(10., dicimal);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (sliderValue == m_lineEdit->getValue()) {
Shinya Kitaoka 120a6e
    assert(m_slider->value() == value || !m_slider->isVisible());
Shinya Kitaoka 120a6e
    // Se isDragging e' falso e' giusto che venga emessa la notifica di
Shinya Kitaoka 120a6e
    // cambiamento.
Shinya Kitaoka 120a6e
    if (!isDragging) emit valueChanged(isDragging);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_slider->setValue(sliderValue);
Shinya Kitaoka 120a6e
  m_lineEdit->setValue(value);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Faccio in modo che il cursore sia sulla prima cifra, cosi' se la stringa
Shinya Kitaoka 120a6e
  // da visualizzare e' piu' lunga del campo le cifre che vengono troncate sono
Shinya Kitaoka 120a6e
  // le ultime e non le prime (dovrebbero essere quelle dopo la virgola).
Shinya Kitaoka 120a6e
  m_lineEdit->setCursorPosition(0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  emit valueChanged(isDragging);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// DoubleLineEdit
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DoubleLineEdit::DoubleLineEdit(QWidget *parent, double value)
Shinya Kitaoka 120a6e
    : DoubleValueLineEdit(parent) {
Shinya Kitaoka 120a6e
  m_validator =
Shinya Kitaoka 120a6e
      new QDoubleValidator(-(std::numeric_limits<double>::max)(),</double>
Shinya Kitaoka 120a6e
                           (std::numeric_limits<double>::max)(), 8, this);</double>
Shinya Kitaoka 120a6e
  setValidator(m_validator);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  setValue(value);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool ret = connect(this, SIGNAL(editingFinished()), SIGNAL(valueChanged()));
Shinya Kitaoka 120a6e
  assert(ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleLineEdit::setValue(double value) {
Shinya Kitaoka 120a6e
  double minValue, maxValue;
Shinya Kitaoka 120a6e
  getRange(minValue, maxValue);
Shinya Kitaoka 120a6e
  if (value < minValue) value = minValue;
Shinya Kitaoka 120a6e
  if (value > maxValue) value = maxValue;
Shinya Kitaoka 120a6e
  QString str;
Shinya Kitaoka 120a6e
  str.setNum(value);
Shinya Kitaoka 120a6e
  setText(str);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Faccio in modo che il cursore sia sulla prima cifra, cosi' se la stringa
Shinya Kitaoka 120a6e
  // da visualizzare e' piu' lunga del campo le cifre che vengono troncate sono
Shinya Kitaoka 120a6e
  // le ultime e non le prime (dovrebbero essere quelle dopo la virgola).
Shinya Kitaoka 120a6e
  setCursorPosition(0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double DoubleLineEdit::getValue() { return text().toDouble(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleLineEdit::setRange(double minValue, double maxValue) {
Shinya Kitaoka 120a6e
  m_validator->setRange(minValue, maxValue, m_validator->decimals());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleLineEdit::getRange(double &minValue, double &maxValue) {
Shinya Kitaoka 120a6e
  minValue = m_validator->bottom();
Shinya Kitaoka 120a6e
  maxValue = m_validator->top();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DoubleLineEdit::setDecimals(int decimals) {
Shinya Kitaoka 120a6e
  m_validator->setDecimals(decimals);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int DoubleLineEdit::getDecimals() { return m_validator->decimals(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// DoubleField
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DoubleField::DoubleField(QWidget *parent, bool isRollerHide, int decimals)
Shinya Kitaoka 120a6e
    : DoubleValueField(parent, new DoubleLineEdit(0)) {
Shinya Kitaoka 120a6e
  if (isRollerHide) enableRoller(false);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  DoubleLineEdit *lineEdit = dynamic_cast<doublelineedit *="">(m_lineEdit);</doublelineedit>
Shinya Kitaoka 120a6e
  lineEdit->setDecimals(decimals);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*--rollerにもStepを設定--*/
Shinya Kitaoka 120a6e
  if (!isRollerHide) m_roller->setStep(pow(0.1, decimals));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// MeasuredDoubleLineEdit
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
MeasuredDoubleLineEdit::MeasuredDoubleLineEdit(QWidget *parent)
Shinya Kitaoka 120a6e
    : DoubleValueLineEdit(parent)
Shinya Kitaoka 120a6e
    , m_minValue(-(std::numeric_limits<double>::max)())</double>
Shinya Kitaoka 120a6e
    , m_maxValue((std::numeric_limits<double>::max)())</double>
Shinya Kitaoka 120a6e
    , m_modified(false)
Shinya Kitaoka 120a6e
    , m_errorHighlighting(0)
Shinya Kitaoka 120a6e
    , m_errorHighlightingTimerId(0)
Shinya Kitaoka 120a6e
    , m_decimals(7) {
Shinya Kitaoka 120a6e
  setObjectName("ValueLineEdit");
Shinya Kitaoka 120a6e
  m_value = new TMeasuredValue("length");
Shinya Kitaoka 120a6e
  valueToText();
Shinya Kitaoka 120a6e
  bool ret =
Shinya Kitaoka 120a6e
      connect(this, SIGNAL(editingFinished()), this, SLOT(onEditingFinished()));
Shinya Kitaoka 120a6e
  ret = ret && connect(this, SIGNAL(textChanged(const QString &)), this,
Shinya Kitaoka 120a6e
                       SLOT(onTextChanged(const QString &)));
Shinya Kitaoka 120a6e
  assert(ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
MeasuredDoubleLineEdit::~MeasuredDoubleLineEdit() { delete m_value; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::valueToText() {
Shinya Kitaoka 120a6e
  bool oldModified = m_modified;
Shinya Kitaoka 120a6e
  setText(QString::fromStdWString(m_value->toWideString(m_decimals)));
Shinya Kitaoka 120a6e
  setCursorPosition(0);
Shinya Kitaoka 120a6e
  m_modified = oldModified;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::setValue(double value) {
Shinya Kitaoka 120a6e
  m_value->setValue(TMeasuredValue::MainUnit, value);
Shinya Kitaoka 120a6e
  valueToText();
Shinya Kitaoka 120a6e
  m_modified = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double MeasuredDoubleLineEdit::getValue() {
Shinya Kitaoka 120a6e
  return m_value->getValue(TMeasuredValue::MainUnit);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::setRange(double minValue, double maxValue) {
Shinya Kitaoka 120a6e
  m_minValue = minValue;
Shinya Kitaoka 120a6e
  m_maxValue = maxValue;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::getRange(double &minValue, double &maxValue) {
Shinya Kitaoka 120a6e
  minValue = m_minValue;
Shinya Kitaoka 120a6e
  maxValue = m_maxValue;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::setMeasure(std::string name) {
Shinya Kitaoka 120a6e
  delete m_value;
Shinya Kitaoka 120a6e
  m_value = new TMeasuredValue(name != "" ? name : "dummy");
Shinya Kitaoka 120a6e
  valueToText();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::setDecimals(int decimals) {
Shinya Kitaoka 120a6e
  m_decimals = decimals;
Shinya Kitaoka 120a6e
  valueToText();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int MeasuredDoubleLineEdit::getDecimals() { return m_decimals; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::onTextChanged(const QString &) {
Shinya Kitaoka 120a6e
  m_modified = true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::onEditingFinished() {
Shinya Kitaoka 120a6e
  if (!m_modified) return;
Shinya Kitaoka 120a6e
  m_modified = false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double oldValue       = getValue();
Shinya Kitaoka 120a6e
  QString oldStyleSheet = styleSheet();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int err = -10;
Shinya Kitaoka 120a6e
  m_value->setValue(text().toStdWString(), &err);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool outOfRange = false;
Shinya Kitaoka 120a6e
  if (!err) {
Shinya Kitaoka 120a6e
    double v   = getValue();
Shinya Kitaoka 120a6e
    outOfRange = m_minValue > v || m_maxValue < v;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (err) {
Shinya Kitaoka 120a6e
    m_errorHighlighting = 1;
Shinya Kitaoka 120a6e
    if (m_errorHighlightingTimerId == 0)
Shinya Kitaoka 120a6e
      m_errorHighlightingTimerId = startTimer(40);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (m_errorHighlightingTimerId != 0) killTimer(m_errorHighlightingTimerId);
Shinya Kitaoka 120a6e
    m_errorHighlightingTimerId = 0;
Shinya Kitaoka 120a6e
    m_errorHighlighting        = 0;
Shinya Kitaoka 120a6e
    setStyleSheet("");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double newValue = getValue();
Shinya Kitaoka 120a6e
  if (m_minValue > newValue || m_maxValue < newValue) {
Shinya Kitaoka 120a6e
    m_value->setValue(TMeasuredValue::MainUnit, oldValue);
Shinya Kitaoka 120a6e
    valueToText();
Shinya Kitaoka 120a6e
    emit valueChanged();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  valueToText();
Shinya Kitaoka 120a6e
  emit valueChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleLineEdit::timerEvent(QTimerEvent *) {
Shinya Kitaoka 120a6e
  if (m_errorHighlighting < 0.01) {
Shinya Kitaoka 120a6e
    if (m_errorHighlightingTimerId != 0) killTimer(m_errorHighlightingTimerId);
Shinya Kitaoka 120a6e
    m_errorHighlightingTimerId = 0;
Shinya Kitaoka 120a6e
    m_errorHighlighting        = 0;
Shinya Kitaoka 120a6e
    setStyleSheet("");
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    int v               = 255 - (int)(m_errorHighlighting * 255);
Shinya Kitaoka 120a6e
    m_errorHighlighting = m_errorHighlighting * 0.8;
Shinya Kitaoka 120a6e
    int c               = 255 << 16 | v << 8 | v;
Shinya Kitaoka 120a6e
    setStyleSheet(QString("#ValueLineEdit {background-color:#%1}")
Shinya Kitaoka 120a6e
                      .arg(c, 6, 16, QLatin1Char('0')));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// MeasuredDoubleField
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
MeasuredDoubleField::MeasuredDoubleField(QWidget *parent, bool isRollerHide)
Shinya Kitaoka 120a6e
    : DoubleValueField(parent, new MeasuredDoubleLineEdit(0)) {
Shinya Kitaoka 120a6e
  m_lineEdit->setMaximumWidth(100);
Shinya Kitaoka 120a6e
  if (isRollerHide) enableRoller(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
///----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleField::setMeasure(std::string measureName) {
Shinya Kitaoka 120a6e
  MeasuredDoubleLineEdit *lineEdit =
Shinya Kitaoka 120a6e
      dynamic_cast<measureddoublelineedit *="">(m_lineEdit);</measureddoublelineedit>
Shinya Kitaoka 120a6e
  assert(lineEdit);
Shinya Kitaoka 120a6e
  lineEdit->setMeasure(measureName);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void MeasuredDoubleField::setDecimals(int decimals) {
Shinya Kitaoka 120a6e
  MeasuredDoubleLineEdit *lineEdit =
Shinya Kitaoka 120a6e
      qobject_cast<measureddoublelineedit *="">(m_lineEdit);</measureddoublelineedit>
Shinya Kitaoka 120a6e
  if (lineEdit) lineEdit->setDecimals(decimals);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*--- rollerにもStepを設定 ---*/
Shinya Kitaoka 120a6e
  if (isRollerEnabled()) m_roller->setStep(pow(0.1, std::max(decimals - 1, 1)));
Toshihiro Shimizu 890ddd
}