Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/intpairfield.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qlabel></qlabel>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
#include <qmouseevent></qmouseevent>
Toshihiro Shimizu 890ddd
#include <qslider></qslider>
Toshihiro Shimizu 890ddd
#include <qhboxlayout></qhboxlayout>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace DVGui;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
#define MARGIN_OFFSET 7
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define MARGIN_OFFSET 2
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// IntPairField
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
IntPairField::IntPairField(QWidget *parent, bool isMaxRangeLimited)
Shinya Kitaoka 120a6e
    : QWidget(parent)
Shinya Kitaoka 120a6e
    , m_values(0, 0)
Shinya Kitaoka 120a6e
    , m_minValue(0)
Shinya Kitaoka 120a6e
    , m_maxValue(100)
Shinya Kitaoka 120a6e
    , m_grabOffset(0)
Shinya Kitaoka 120a6e
    , m_grabIndex(-1)
Shinya Kitaoka 120a6e
    , m_leftMargin(52)
Shinya Kitaoka 120a6e
    , m_rightMargin(52)
Shinya Kitaoka 120a6e
    , m_isMaxRangeLimited(isMaxRangeLimited) {
Shinya Kitaoka 120a6e
  setObjectName("IntPairField");
Shinya Kitaoka 120a6e
  setFixedHeight(WidgetHeight);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_leftLabel    = new QLabel("", this);
Shinya Kitaoka 120a6e
  m_leftLineEdit = new DVGui::IntLineEdit(this);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_rightLabel    = new QLabel("", this);
Shinya Kitaoka 120a6e
  m_rightLineEdit = new DVGui::IntLineEdit(this);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_leftLineEdit->setFixedWidth(40);
Shinya Kitaoka 120a6e
  m_rightLineEdit->setFixedWidth(40);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //----layout
Shinya Kitaoka 120a6e
  QHBoxLayout *m_mainLayout = new QHBoxLayout;
Shinya Kitaoka 120a6e
  m_mainLayout->setMargin(0);
Shinya Kitaoka 120a6e
  m_mainLayout->setSpacing(5);
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    m_mainLayout->addWidget(m_leftLabel, 1);
Shinya Kitaoka 120a6e
    m_mainLayout->addWidget(m_leftLineEdit, 1, Qt::AlignLeft);
Shinya Kitaoka 120a6e
    m_mainLayout->addSpacing(35);
Shinya Kitaoka 120a6e
    m_mainLayout->addStretch(100);
Shinya Kitaoka 120a6e
    m_mainLayout->addWidget(m_rightLabel, 1);
Shinya Kitaoka 120a6e
    m_mainLayout->addWidget(m_rightLineEdit, 1, Qt::AlignRight);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  setLayout(m_mainLayout);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //---signal-slot connections
Shinya Kitaoka 120a6e
  bool ret = connect(m_leftLineEdit, SIGNAL(editingFinished()),
Shinya Kitaoka 120a6e
                     SLOT(onLeftEditingFinished()));
Shinya Kitaoka 120a6e
  ret = ret && connect(m_rightLineEdit, SIGNAL(editingFinished()),
Shinya Kitaoka 120a6e
                       SLOT(onRightEditingFinished()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double IntPairField::pos2value(int x) const {
Shinya Kitaoka 120a6e
  int xMin = m_leftMargin, xMax = width() - m_rightMargin - 1;
Shinya Kitaoka 120a6e
  return m_minValue + (m_maxValue - m_minValue) * (x - xMin) / (xMax - xMin);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int IntPairField::value2pos(double v) const {
Shinya Kitaoka 120a6e
  int xMin = m_leftMargin, xMax = width() - m_rightMargin - 1;
Shinya Kitaoka 120a6e
  return xMin +
Shinya Kitaoka 120a6e
         (int)(((xMax - xMin) * (v - m_minValue)) / (m_maxValue - m_minValue));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::paintEvent(QPaintEvent *) {
Shinya Kitaoka 120a6e
  QPainter p(this);
Shinya Kitaoka 120a6e
  p.setBrush(Qt::NoBrush);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int x0 = value2pos(m_minValue);
Shinya Kitaoka 120a6e
  int x1 = value2pos(m_maxValue);
Shinya Kitaoka 120a6e
  int y  = height() / 2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  p.setPen(getLightLineColor());
Shinya Kitaoka 120a6e
  p.drawLine(x0 - 1, y, x1 + 1, y);
Shinya Kitaoka 120a6e
  p.drawPoint(x1 + 1, y - 1);
Shinya Kitaoka 120a6e
  p.drawPoint(x1 + 1, y - 2);
Shinya Kitaoka 120a6e
  p.drawPoint(x1 + 1, y - 3);
Shinya Kitaoka 120a6e
  p.setPen(QColor(0, 0, 0));
Shinya Kitaoka 120a6e
  p.drawLine(x0, y - 2, x1 - 1, y - 2);
Shinya Kitaoka 120a6e
  p.setPen(getDarkLineColor());
Shinya Kitaoka 120a6e
  p.drawLine(x0 - 1, y - 3, x1, y - 3);
Shinya Kitaoka 120a6e
  p.drawPoint(x0 - 1, y - 1);
Shinya Kitaoka 120a6e
  p.drawPoint(x0 - 1, y - 2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  p.setPen(Qt::black);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int y1 = height() - 1;
Shinya Kitaoka 120a6e
  int x;
Shinya Kitaoka 120a6e
  x                = value2pos(m_values.first);
Shinya Kitaoka 120a6e
  QRect sliderRect = QRect(x0, -5, x1 - x0 + 1, 10);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (sliderRect.contains(QPoint(x, 0)))
Shinya Kitaoka 120a6e
    p.drawPixmap(x - m_handleLeftPixmap.width() + 1, 2, m_handleLeftPixmap);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    p.drawPixmap(sliderRect.right() - m_handleLeftPixmap.width() + 1, 2,
Shinya Kitaoka 120a6e
                 m_handleLeftGrayPixmap);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  x = value2pos(m_values.second);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (sliderRect.contains(QPoint(x, 0)))
Shinya Kitaoka 120a6e
    p.drawPixmap(x, 2, m_handleRightPixmap);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    p.drawPixmap(sliderRect.right(), 2, m_handleRightGrayPixmap);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::setLeftText(const QString &text) {
Shinya Kitaoka 120a6e
  QPoint pos       = m_leftLabel->pos();
Shinya Kitaoka 120a6e
  QString oldText  = m_leftLabel->text();
Shinya Kitaoka 120a6e
  int oldLabelSize = fontMetrics().width(oldText);
Shinya Kitaoka 120a6e
  int newLabelSize = fontMetrics().width(text);
Shinya Kitaoka 120a6e
  int labelSize    = newLabelSize - oldLabelSize;
Shinya Kitaoka 120a6e
  m_leftMargin += labelSize + MARGIN_OFFSET;
Shinya Kitaoka 120a6e
  m_leftLabel->setText(text);
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::setRightText(const QString &text) {
Shinya Kitaoka 120a6e
  QString oldText  = m_rightLabel->text();
Shinya Kitaoka 120a6e
  int oldLabelSize = fontMetrics().width(oldText);
Shinya Kitaoka 120a6e
  int newLabelSize = fontMetrics().width(text);
Shinya Kitaoka 120a6e
  int labelSize    = newLabelSize - oldLabelSize;
Shinya Kitaoka 120a6e
  m_rightMargin += labelSize + MARGIN_OFFSET;
Shinya Kitaoka 120a6e
  m_rightLabel->setText(text);
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::setLabelsEnabled(bool enable) {
Shinya Kitaoka 120a6e
  if (!enable) {
Shinya Kitaoka 120a6e
    m_rightLabel->hide();
Shinya Kitaoka 120a6e
    m_leftLabel->hide();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_rightLabel->show();
Shinya Kitaoka 120a6e
    m_leftLabel->show();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::setValue(int value) {
Shinya Kitaoka 120a6e
  value = tcrop(value, m_minValue, m_maxValue);
Shinya Kitaoka 120a6e
  value = tround(value * 100) * 0.01;
Shinya Kitaoka 120a6e
  if (m_grabIndex == 0)  // Left grab
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    m_values.first = value;
Shinya Kitaoka 120a6e
    m_leftLineEdit->setValue(m_values.first);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (value > m_values.second) {
Shinya Kitaoka 120a6e
      m_values.second = value;
Shinya Kitaoka 120a6e
      m_rightLineEdit->setValue(m_values.second);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else  // Right grab
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    m_values.second = value;
Shinya Kitaoka 120a6e
    m_rightLineEdit->setValue(m_values.second);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (value < m_values.first) {
Shinya Kitaoka 120a6e
      m_values.first = value;
Shinya Kitaoka 120a6e
      m_leftLineEdit->setValue(m_values.first);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::setValues(const std::pair<int, int=""> &values) {</int,>
Shinya Kitaoka 120a6e
  assert(values.first <= values.second);
Shinya Kitaoka 120a6e
  m_values.first = tcrop(values.first, m_minValue, m_maxValue);
Shinya Kitaoka 120a6e
  m_leftLineEdit->setValue(m_values.first);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_values.second = values.second;
Shinya Kitaoka 120a6e
  if (m_isMaxRangeLimited)
Shinya Kitaoka 120a6e
    m_values.second = tcrop(values.second, m_values.first, m_maxValue);
Shinya Kitaoka 120a6e
  m_rightLineEdit->setValue(m_values.second);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::setRange(int minValue, int maxValue) {
Shinya Kitaoka 120a6e
  m_minValue = minValue;
Shinya Kitaoka 120a6e
  m_maxValue = maxValue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!m_isMaxRangeLimited) maxValue = (std::numeric_limits<int>::max)();</int>
Shinya Kitaoka 120a6e
  m_leftLineEdit->setRange(minValue, maxValue);
Shinya Kitaoka 120a6e
  m_rightLineEdit->setRange(minValue, maxValue);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::getRange(int &minValue, int &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 IntPairField::mousePressEvent(QMouseEvent *event) {
Shinya Kitaoka 120a6e
  if (event->button() == Qt::LeftButton) {
Shinya Kitaoka 120a6e
    int x = event->pos().x();
Shinya Kitaoka 120a6e
    int cur0, cur1;
Shinya Kitaoka 120a6e
    if (m_values.first > m_maxValue)
Shinya Kitaoka 120a6e
      cur0 = value2pos(m_maxValue) - 5;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      cur0 = value2pos(m_values.first);
Shinya Kitaoka 120a6e
    if (m_values.second > m_maxValue)
Shinya Kitaoka 120a6e
      cur1 = value2pos(m_maxValue);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      cur1 = value2pos(m_values.second);
Shinya Kitaoka 120a6e
    int d0 = abs(cur0 - x);
Shinya Kitaoka 120a6e
    int d1 = abs(cur1 - x);
Shinya Kitaoka 120a6e
    int d, cur;
Shinya Kitaoka 120a6e
    if (d0 < d1 || (d0 == d1 && x < cur0)) {
Shinya Kitaoka 120a6e
      d           = d0;
Shinya Kitaoka 120a6e
      cur         = cur0;
Shinya Kitaoka 120a6e
      m_grabIndex = 0;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      d           = d1;
Shinya Kitaoka 120a6e
      cur         = cur1;
Shinya Kitaoka 120a6e
      m_grabIndex = 1;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (d < 6)
Shinya Kitaoka 120a6e
      m_grabOffset = cur - x;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      m_grabOffset = 0;
Shinya Kitaoka 120a6e
      setValue(pos2value(x));
Shinya Kitaoka 120a6e
      emit valuesChanged(true);
Shinya Kitaoka 120a6e
      update();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::mouseMoveEvent(QMouseEvent *event) {
Shinya Kitaoka 120a6e
  if (event->buttons()) {
Shinya Kitaoka 120a6e
    std::pair<int, int=""> oldValues = m_values;</int,>
Shinya Kitaoka 120a6e
    int x = event->pos().x() + m_grabOffset;
Shinya Kitaoka 120a6e
    setValue(pos2value(x));
Shinya Kitaoka 120a6e
    if (oldValues == m_values) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    emit valuesChanged(true);
Shinya Kitaoka 120a6e
    update();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::mouseReleaseEvent(QMouseEvent *event) {
Shinya Kitaoka 120a6e
  m_grabOffset = 0;
Shinya Kitaoka 120a6e
  m_grabIndex  = 0;
Shinya Kitaoka 120a6e
  emit valuesChanged(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::onLeftEditingFinished() {
Shinya Kitaoka 120a6e
  int value = m_leftLineEdit->getValue();
Shinya Kitaoka 120a6e
  if (value == m_values.first) return;
Shinya Kitaoka 120a6e
  if (!m_isMaxRangeLimited && value < m_minValue)
Shinya Kitaoka 120a6e
    value = m_minValue;
Shinya Kitaoka 120a6e
  else if (m_isMaxRangeLimited)
Shinya Kitaoka 120a6e
    value        = tcrop(value, m_minValue, m_maxValue);
Shinya Kitaoka 120a6e
  m_values.first = value;
Shinya Kitaoka 120a6e
  if (m_values.first > m_values.second) {
Shinya Kitaoka 120a6e
    m_values.second = m_values.first;
Shinya Kitaoka 120a6e
    m_rightLineEdit->setValue(m_values.second);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  emit valuesChanged(false);
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void IntPairField::onRightEditingFinished() {
Shinya Kitaoka 120a6e
  int value = m_rightLineEdit->getValue();
Shinya Kitaoka 120a6e
  if (value == m_values.second) return;
Shinya Kitaoka 120a6e
  if (m_isMaxRangeLimited) value = tcrop(value, m_minValue, m_maxValue);
Shinya Kitaoka 120a6e
  m_values.second                = value;
Shinya Kitaoka 120a6e
  if (m_values.second < m_values.first) {
Shinya Kitaoka 120a6e
    m_values.first = m_values.second;
Shinya Kitaoka 120a6e
    m_leftLineEdit->setValue(m_values.first);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  emit valuesChanged(false);
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}