Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/intfield.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qintvalidator></qintvalidator>
Toshihiro Shimizu 890ddd
#include <qslider></qslider>
Toshihiro Shimizu 890ddd
#include <qhboxlayout></qhboxlayout>
Toshihiro Shimizu 890ddd
#include <qaction></qaction>
Toshihiro Shimizu 890ddd
#include <qfocusevent></qfocusevent>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
shun-iwasawa 76d093
namespace {
shun-iwasawa 76d093
const int NonLinearSliderPrecision = 2;
shun-iwasawa 76d093
}
shun-iwasawa 76d093
Toshihiro Shimizu 890ddd
using namespace DVGui;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// RollerField
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
RollerField::RollerField(QWidget *parent)
Shinya Kitaoka 120a6e
    : QWidget(parent)
Shinya Kitaoka 120a6e
    , m_value(0)
Shinya Kitaoka 120a6e
    , m_minValue(-100000.0)
Shinya Kitaoka 120a6e
    , m_maxValue(100000.0)
Shinya Kitaoka 120a6e
    , m_xPos(0)
Shinya Kitaoka 120a6e
    , m_step(1.0) {
Shinya Kitaoka 120a6e
  setMinimumSize(43, 7);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RollerField::setValue(double value) {
Shinya Kitaoka 120a6e
  if (m_value == value) return;
Shinya Kitaoka 120a6e
  if (value < m_minValue) m_value = m_minValue;
Shinya Kitaoka 120a6e
  if (value > m_maxValue) m_value = m_maxValue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_value = value;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double RollerField::getValue() const { return m_value; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RollerField::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 RollerField::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 RollerField::paintEvent(QPaintEvent *e) {
Shinya Kitaoka 120a6e
  QPainter p(this);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int w = width();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  drawArrow(p, QPointF(3, 3), QPointF(5, 5), QPointF(5, 1), true, Qt::black,
Shinya Kitaoka 120a6e
            Qt::black);
Shinya Kitaoka 120a6e
  drawArrow(p, QPointF(w - 4, 3), QPointF(w - 6, 5), QPointF(w - 6, 1), true,
Shinya Kitaoka 120a6e
            Qt::black, Qt::black);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  p.drawLine(QPoint(3, 3), QPoint(w - 4, 3));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RollerField::mousePressEvent(QMouseEvent *e) {
Shinya Kitaoka 120a6e
  if (e->buttons() == Qt::LeftButton) {
Shinya Kitaoka 120a6e
    m_xPos = e->pos().x();
Shinya Kitaoka 120a6e
    e->accept();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RollerField::mouseMoveEvent(QMouseEvent *e) {
Shinya Kitaoka 120a6e
  if (e->buttons() == Qt::LeftButton) {
Shinya Kitaoka 120a6e
    if (m_xPos < e->pos().x())
Shinya Kitaoka 120a6e
      addValue(true);
Shinya Kitaoka 120a6e
    else if (m_xPos > e->pos().x())
Shinya Kitaoka 120a6e
      removeValue(true);
Shinya Kitaoka 120a6e
    m_xPos = e->pos().x();
Shinya Kitaoka 120a6e
    e->accept();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RollerField::mouseReleaseEvent(QMouseEvent *e) {
Shinya Kitaoka 120a6e
  e->accept();
Shinya Kitaoka 120a6e
  emit valueChanged(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RollerField::addValue(bool isDragging) {
Shinya Kitaoka 120a6e
  double newValue = tcrop(m_value + m_step, m_minValue, m_maxValue);
Shinya Kitaoka 120a6e
  if (newValue == m_value) return;
Shinya Kitaoka 120a6e
  m_value = newValue;
Shinya Kitaoka 120a6e
  emit valueChanged(isDragging);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RollerField::removeValue(bool isDragging) {
Shinya Kitaoka 120a6e
  double newValue = tcrop(m_value - m_step, m_minValue, m_maxValue);
Shinya Kitaoka 120a6e
  if (newValue == m_value) return;
Shinya Kitaoka 120a6e
  m_value = newValue;
Shinya Kitaoka 120a6e
  emit valueChanged(isDragging);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// IntLineEdit
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
IntLineEdit::IntLineEdit(QWidget *parent, int value, int minValue, int maxValue,
Shinya Kitaoka 120a6e
                         int showedDigits)
Shinya Kitaoka 120a6e
    : LineEdit(parent), m_showedDigits(showedDigits) {
Shinya Kitaoka 120a6e
  setFixedWidth(54);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_validator = new QIntValidator(this);
Shinya Kitaoka 120a6e
  setValue(value);
Shinya Kitaoka 120a6e
  setRange(minValue, maxValue);
Shinya Kitaoka 120a6e
  setValidator(m_validator);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntLineEdit::setValue(int value) {
Shinya Kitaoka 120a6e
  int 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
  if (m_showedDigits > 0) {
Shinya Kitaoka 120a6e
    while (str.length() < m_showedDigits) str.push_front("0");
Shinya Kitaoka 120a6e
    while (str.length() > m_showedDigits) str.remove(0, 1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  setText(str);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Faccio in modo che il cursore sia sulla prima cifra, cosi' se la stringa da
Shinya Kitaoka 120a6e
  // visualizzare
Shinya Kitaoka 120a6e
  // e' piu' lunga del campo le cifre che vengono troncate sono le ultime e non
Shinya Kitaoka 120a6e
  // le prime.
Shinya Kitaoka 120a6e
  setCursorPosition(0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int IntLineEdit::getValue() { return text().toInt(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntLineEdit::setRange(int minValue, int maxValue) {
Shinya Kitaoka 120a6e
  m_validator->setRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntLineEdit::getRange(int &minValue, int &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 IntLineEdit::setBottomRange(int minValue) {
Shinya Kitaoka 120a6e
  m_validator->setBottom(minValue);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntLineEdit::setTopRange(int maxValue) { m_validator->setTop(maxValue); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntLineEdit::focusOutEvent(QFocusEvent *e) {
Shinya Kitaoka 120a6e
  int value = getValue();
Shinya Kitaoka 120a6e
  int minValue, maxValue;
Shinya Kitaoka 120a6e
  getRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (e->lostFocus()) setValue(value);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QLineEdit::focusOutEvent(e);
shun_iwasawa 23727d
  m_isTyping = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// for fps edit in flip console
Shinya Kitaoka 120a6e
void IntLineEdit::setLineEditBackgroundColor(QColor color) {
Kite 521c84
  // Set text color based on luminescence of bg color
Kite 521c84
  int value           = 0;
Kite 521c84
  double luminescence = ((0.299 * color.red()) + (0.587 * color.green()) +
Kite 521c84
                         (0.114 * color.blue())) /
Kite 521c84
                        255;
Kite 521c84
  if (luminescence > 0.5)
Kite 521c84
    value = 0;  // black
Kite 521c84
  else
Kite 521c84
    value = 255;  // white
Kite 521c84
Kite 521c84
  QString sheet =
Kite 521c84
      QString("background-color: rgb(") + QString::number(color.red()) +
Kite 521c84
      QString(",") + QString::number(color.green()) + QString(",") +
Kite 521c84
      QString::number(color.blue()) + QString(",") +
Kite 521c84
      QString::number(color.alpha()) +
Kite 521c84
      QString(");" +
Kite 521c84
              QString("color: rgb(" + QString::number(value) + QString(",") +
Kite 521c84
                      QString::number(value) + QString(",") +
Kite 521c84
                      QString::number(value) + QString(");")));
Shinya Kitaoka 120a6e
  setStyleSheet(sheet);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void IntLineEdit::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 IntLineEdit::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
  } else
Jeremy Bullock 771290
    QLineEdit::mouseMoveEvent(e);
Jeremy Bullock 771290
}
Jeremy Bullock 771290
Jeremy Bullock 771290
//-----------------------------------------------------------------------------
Jeremy Bullock 771290
Jeremy Bullock 771290
void IntLineEdit::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
// IntField
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
IntField::IntField(QWidget *parent, bool isMaxRangeLimited, bool isRollerHide)
Shinya Kitaoka 120a6e
    : QWidget(parent)
Shinya Kitaoka 120a6e
    , m_lineEdit(0)
Shinya Kitaoka 120a6e
    , m_slider(0)
Shinya Kitaoka 120a6e
    , m_roller(0)
shun-iwasawa 76d093
    , m_isMaxRangeLimited(isMaxRangeLimited)
shun-iwasawa 76d093
    , m_isLinearSlider(true) {
Shinya Kitaoka 120a6e
  setObjectName("IntField");
Shinya Kitaoka 120a6e
  QHBoxLayout *layout = new QHBoxLayout(this);
Shinya Kitaoka 120a6e
  layout->setMargin(0);
Shinya Kitaoka 120a6e
  layout->setSpacing(5);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QWidget *field = new QWidget(this);
Shinya Kitaoka 120a6e
  field->setMaximumWidth(43);
Shinya Kitaoka 120a6e
  QVBoxLayout *vLayout = new QVBoxLayout(field);
Shinya Kitaoka 120a6e
  vLayout->setMargin(0);
Shinya Kitaoka 120a6e
  vLayout->setSpacing(0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_lineEdit = new DVGui::IntLineEdit(field);
Shinya Kitaoka 120a6e
  bool ret   = connect(m_lineEdit, SIGNAL(editingFinished()), this,
Shinya Kitaoka 120a6e
                     SLOT(onEditingFinished()));
Shinya Kitaoka 120a6e
  vLayout->addWidget(m_lineEdit);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_roller = new RollerField(field);
Shinya Kitaoka 120a6e
  ret      = ret && connect(m_roller, SIGNAL(valueChanged(bool)), this,
Shinya Kitaoka 120a6e
                       SLOT(onRollerValueChanged(bool)));
Shinya Kitaoka 120a6e
  vLayout->addWidget(m_roller);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (isRollerHide) enableRoller(false);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  layout->addWidget(field);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_slider = new QSlider(Qt::Horizontal, this);
Shinya Kitaoka 120a6e
  ret      = ret && connect(m_slider, SIGNAL(valueChanged(int)), this,
Shinya Kitaoka 120a6e
                       SLOT(onSliderChanged(int)));
Kite 521c84
  ret = ret && connect(m_slider, SIGNAL(sliderReleased()), this,
Shinya Kitaoka 120a6e
                       SLOT(onSliderReleased()));
Toshihiro Shimizu 890ddd
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
  layout->addWidget(m_slider);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  setValues(0, 0, 100);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  setLayout(layout);
Shinya Kitaoka 120a6e
  assert(ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::getRange(int &minValue, int &maxValue) {
Shinya Kitaoka 120a6e
  double min, max;
Shinya Kitaoka 120a6e
  m_roller->getRange(min, max);
Shinya Kitaoka 120a6e
  minValue = tround(min);
Shinya Kitaoka 120a6e
  maxValue = tround(max);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::setRange(int minValue, int maxValue) {
Shinya Kitaoka 120a6e
  m_lineEdit->setRange(minValue, m_isMaxRangeLimited
Shinya Kitaoka 120a6e
                                     ? maxValue
Shinya Kitaoka 120a6e
                                     : (std::numeric_limits<int>::max)());</int>
shun-iwasawa 76d093
  if (m_isLinearSlider)
shun-iwasawa 76d093
    m_slider->setRange(minValue, maxValue);
shun-iwasawa 76d093
  else
shun-iwasawa 76d093
    m_slider->setRange(minValue * pow(10., NonLinearSliderPrecision),
shun-iwasawa 76d093
                       maxValue * pow(10., NonLinearSliderPrecision));
Shinya Kitaoka 120a6e
  m_roller->setRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::setValue(int value) {
Shinya Kitaoka 120a6e
  if (m_lineEdit->getValue() == value) return;
Shinya Kitaoka 120a6e
  m_lineEdit->setValue(value);
shun-iwasawa 76d093
  m_slider->setSliderPosition(value2pos(value));
Shinya Kitaoka 120a6e
  m_roller->setValue((double)value);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int IntField::getValue() { return (m_lineEdit->getValue()); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::setValues(int value, int minValue, int 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 IntField::enableSlider(bool enable) {
Shinya Kitaoka 120a6e
  m_slider->setEnabled(enable);
Shinya Kitaoka 120a6e
  if (enable)
Shinya Kitaoka 120a6e
    m_slider->show();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    m_slider->hide();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool IntField::sliderIsEnabled() { return m_slider->isEnabled(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::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 IntField::rollerIsEnabled() { return m_roller->isEnabled(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::setLineEditBackgroundColor(QColor color) {
Shinya Kitaoka 120a6e
  m_lineEdit->setLineEditBackgroundColor(color);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 76d093
int IntField::pos2value(int x) const {
shun-iwasawa 76d093
  if (m_isLinearSlider) return x;
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
Kite 521c84
    t              = -8.0 + 9.0 * posRatio;
shun-iwasawa 76d093
  double sliderVal = (double)m_slider->minimum() + rangeSize * t;
shun-iwasawa 76d093
  return (int)round(sliderVal * pow(0.1, NonLinearSliderPrecision));
shun-iwasawa 76d093
}
shun-iwasawa 76d093
shun-iwasawa 76d093
//-----------------------------------------------------------------------------
shun-iwasawa 76d093
shun-iwasawa 76d093
int IntField::value2pos(int v) const {
shun-iwasawa 76d093
  if (m_isLinearSlider) return v;
shun-iwasawa 76d093
shun-iwasawa 76d093
  // nonlinear slider case
shun-iwasawa 76d093
  double sliderVal  = (double)v * pow(10., NonLinearSliderPrecision);
shun-iwasawa 76d093
  double rangeSize  = (double)(m_slider->maximum() - m_slider->minimum());
shun-iwasawa 76d093
  double valueRatio = (double)(sliderVal - 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
shun-iwasawa 76d093
void IntField::onSliderChanged(int sliderPos) {
shun-iwasawa 76d093
  int value = pos2value(sliderPos);
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() == value ||
Shinya Kitaoka 120a6e
      ((int)m_roller->getValue() == value && m_roller->isVisible()))
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  m_lineEdit->setValue(value);
Shinya Kitaoka 120a6e
  m_roller->setValue((double)value);
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);
Shinya Kitaoka 120a6e
  emit valueChanged(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::onEditingFinished() {
Shinya Kitaoka 120a6e
  double value = m_lineEdit->getValue();
Shinya Kitaoka 120a6e
  // Controllo necessario per evitare che il segnale di cambiamento venga emesso
Shinya Kitaoka 120a6e
  // piu' volte.
shun-iwasawa 76d093
  if ((pos2value(m_slider->value()) == value && m_slider->isVisible()) ||
Rozhuk Ivan 823a31
      ((int)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((double)value);
Shinya Kitaoka 120a6e
  emit valueChanged(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntField::onRollerValueChanged(bool isDragging) {
Shinya Kitaoka 120a6e
  int value = m_roller->getValue();
Shinya Kitaoka 120a6e
  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
}