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);
shun_iwasawa 23727d
  m_isTyping = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void DoubleValueLineEdit::mousePressEvent(QMouseEvent *e) {
Jeremy Bullock 771290
  if (e->buttons() == Qt::MiddleButton) {
Jeremy Bullock 771290
    m_xMouse           = e->x();
Jeremy Bullock 771290
    m_mouseDragEditing = true;
shun_iwasawa 23727d
  } else {
Jeremy Bullock 771290
    QLineEdit::mousePressEvent(e);
shun_iwasawa 23727d
    if (!m_isTyping) {  // only the first click will select all
shun_iwasawa 23727d
      selectAll();
shun_iwasawa 23727d
      m_isTyping = true;
shun_iwasawa 23727d
    }
shun_iwasawa 23727d
  }
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void DoubleValueLineEdit::mouseMoveEvent(QMouseEvent *e) {
Jeremy Bullock 771290
  if (e->buttons() == Qt::MiddleButton) {
Jeremy Bullock 771290
    setValue(getValue() + ((e->x() - m_xMouse) / 2));
Jeremy Bullock 771290
    m_xMouse = e->x();
Jeremy Bullock 771290
    emit valueChanged();
Jeremy Bullock 771290
  } else
Jeremy Bullock 771290
    QLineEdit::mouseMoveEvent(e);
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void DoubleValueLineEdit::mouseReleaseEvent(QMouseEvent *e) {
Jeremy Bullock 771290
  if ((e->buttons() == Qt::NoButton && m_mouseDragEditing)) {
Jeremy Bullock 771290
    m_mouseDragEditing = false;
Jeremy Bullock 771290
    clearFocus();
shun_iwasawa 23727d
  } else
Jeremy Bullock 771290
    QLineEdit::mouseReleaseEvent(e);
Jeremy Bullock 771290
}
Jeremy Bullock 771290
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)
shun-iwasawa f53bf9
    : QWidget(parent)
shun-iwasawa f53bf9
    , m_lineEdit(lineEdit)
shun-iwasawa f53bf9
    , m_slider(0)
shun-iwasawa f53bf9
    , m_roller(0)
shun-iwasawa f53bf9
    , m_isLinearSlider(true) {
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()));
shun-iwasawa 76d093
  ret      = ret && connect(m_roller, SIGNAL(valueChanged(bool)),
Shinya Kitaoka 120a6e
                       SLOT(onRollerValueChanged(bool)));
shun-iwasawa 76d093
  ret      = ret && connect(m_slider, SIGNAL(valueChanged(int)),
Shinya Kitaoka 120a6e
                       SLOT(onSliderChanged(int)));
shun-iwasawa 76d093
  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
shun-iwasawa 76d093
double DoubleValueField::pos2value(int x) const {
shun-iwasawa 76d093
  int dicimal = m_lineEdit->getDecimals();
shun-iwasawa 76d093
  if (m_isLinearSlider) return (double)x * pow(0.1, dicimal);
shun-iwasawa 76d093
shun-iwasawa 76d093
  // nonlinear slider case
shun-iwasawa 76d093
  double rangeSize = (double)(m_slider->maximum() - m_slider->minimum());
shun-iwasawa 76d093
  double posRatio  = (double)(x - m_slider->minimum()) / rangeSize;
shun-iwasawa 76d093
  double t;
shun-iwasawa 76d093
  if (posRatio <= 0.5)
shun-iwasawa 76d093
    t = 0.04 * posRatio;
shun-iwasawa 76d093
  else if (posRatio <= 0.75)
shun-iwasawa 76d093
    t = -0.02 + 0.08 * posRatio;
shun-iwasawa 76d093
  else if (posRatio <= 0.9)
shun-iwasawa 76d093
    t = -0.26 + 0.4 * posRatio;
shun-iwasawa 76d093
  else
shun-iwasawa 76d093
    t = -8.0 + 9.0 * posRatio;
shun-iwasawa 76d093
  double sliderValue = round((double)m_slider->minimum() + rangeSize * t);
shun-iwasawa 76d093
  return sliderValue * pow(0.1, dicimal);
shun-iwasawa 76d093
}
shun-iwasawa 76d093
shun-iwasawa 76d093
//-----------------------------------------------------------------------------
shun-iwasawa 76d093
shun-iwasawa 76d093
int DoubleValueField::value2pos(double v) const {
shun-iwasawa 76d093
  int dicimal        = m_lineEdit->getDecimals();
shun-iwasawa 76d093
  double sliderValue = round(v * pow(10., dicimal));
shun-iwasawa 76d093
  if (m_isLinearSlider) return (int)sliderValue;
shun-iwasawa 76d093
shun-iwasawa 76d093
  // nonlinear slider case
shun-iwasawa 76d093
  double rangeSize  = (double)(m_slider->maximum() - m_slider->minimum());
shun-iwasawa 76d093
  double valueRatio = (sliderValue - (double)m_slider->minimum()) / rangeSize;
shun-iwasawa 76d093
  double t;
shun-iwasawa 76d093
  if (valueRatio <= 0.02)
shun-iwasawa 76d093
    t = valueRatio / 0.04;
shun-iwasawa 76d093
  else if (valueRatio <= 0.04)
shun-iwasawa 76d093
    t = (valueRatio + 0.02) / 0.08;
shun-iwasawa 76d093
  else if (valueRatio <= 0.1)
shun-iwasawa 76d093
    t = (valueRatio + 0.26) / 0.4;
shun-iwasawa 76d093
  else
shun-iwasawa 76d093
    t = (valueRatio + 8.0) / 9.0;
shun-iwasawa 76d093
  return m_slider->minimum() + (int)(t * rangeSize);
shun-iwasawa 76d093
}
shun-iwasawa 76d093
shun-iwasawa 76d093
//-----------------------------------------------------------------------------
shun-iwasawa 76d093
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();
shun-iwasawa 192c8e
  int sliderMax = (int)round(maxValue * pow(10., dicimal));
shun-iwasawa 192c8e
  int sliderMin = (int)round(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);
shun-iwasawa 76d093
  m_slider->setValue(value2pos(value));
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
shun-iwasawa 76d093
void DoubleValueField::onSliderChanged(int sliderPos) {
shun-iwasawa 76d093
  double val = pos2value(sliderPos);
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() {
shun-iwasawa 76d093
  double value = m_lineEdit->getValue();
shun-iwasawa 76d093
  int dicimal  = m_lineEdit->getDecimals();
Toshihiro Shimizu 890ddd
shun-iwasawa 192c8e
  // Control necessary to prevent the change signal from being emitted more than
shun-iwasawa 192c8e
  // once.
shun-iwasawa 76d093
  if ((pos2value(m_slider->value()) == value && m_slider->isVisible()) ||
Shinya Kitaoka 120a6e
      (m_roller->getValue() == value && m_roller->isVisible()))
Shinya Kitaoka 120a6e
    return;
shun-iwasawa 76d093
  m_slider->setValue(value2pos(value));
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
shun-iwasawa 76d093
  if (value == m_lineEdit->getValue()) {
shun-iwasawa 76d093
    assert(pos2value(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
  }
shun-iwasawa 76d093
  m_slider->setValue(value2pos(value));
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
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void MeasuredDoubleLineEdit::mousePressEvent(QMouseEvent *e) {
Jeremy Bullock 771290
  if ((e->buttons() == Qt::MiddleButton) || m_labelClicked) {
Jeremy Bullock 771290
    m_xMouse           = e->x();
Jeremy Bullock 771290
    m_mouseDragEditing = true;
shun_iwasawa 23727d
  } else {
Jeremy Bullock 771290
    QLineEdit::mousePressEvent(e);
shun_iwasawa 23727d
    if (!m_isTyping) {  // only the first click will select all
shun_iwasawa 23727d
      selectAll();
shun_iwasawa 23727d
      m_isTyping = true;
shun_iwasawa 23727d
    }
shun_iwasawa 23727d
  }
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void MeasuredDoubleLineEdit::mouseMoveEvent(QMouseEvent *e) {
Jeremy Bullock 771290
  if ((e->buttons() == Qt::MiddleButton) || m_labelClicked) {
Jeremy Bullock 771290
    int precision = (m_maxValue > 100) ? 0 : ((m_maxValue > 10) ? 1 : 2);
Jeremy Bullock 771290
    m_value->modifyValue((e->x() - m_xMouse) / 2, precision);
Jeremy Bullock 771290
    m_xMouse = e->x();
Jeremy Bullock 771290
    valueToText();
Jeremy Bullock 771290
    m_modified = false;
Jeremy Bullock 771290
  } else
Jeremy Bullock 771290
    QLineEdit::mouseMoveEvent(e);
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void MeasuredDoubleLineEdit::mouseReleaseEvent(QMouseEvent *e) {
Jeremy Bullock 771290
  if ((e->buttons() == Qt::NoButton && m_mouseDragEditing) || m_labelClicked) {
Jeremy Bullock 771290
    m_xMouse   = -1;
Jeremy Bullock 771290
    m_modified = true;
Jeremy Bullock 771290
    onEditingFinished();
Jeremy Bullock 771290
    clearFocus();
Jeremy Bullock 771290
    m_mouseDragEditing = false;
Jeremy Bullock 771290
    m_labelClicked     = false;
Jeremy Bullock 771290
shun_iwasawa 23727d
  } else
Jeremy Bullock 771290
    QLineEdit::mouseReleaseEvent(e);
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
void MeasuredDoubleLineEdit::receiveMousePress(QMouseEvent *e) {
Jeremy Bullock 771290
  m_labelClicked = true;
Jeremy Bullock 771290
  mousePressEvent(e);
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
void MeasuredDoubleLineEdit::receiveMouseMove(QMouseEvent *e) {
Jeremy Bullock 771290
  mouseMoveEvent(e);
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
void MeasuredDoubleLineEdit::receiveMouseRelease(QMouseEvent *e) {
Jeremy Bullock 771290
  mouseReleaseEvent(e);
Jeremy Bullock 771290
  m_labelClicked = false;
Jeremy Bullock 771290
}
Jeremy Bullock 771290
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
}