|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "toonzqt/spectrumfield.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "toonzqt/styleeditor.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tconvert.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "toonzqt/dvdialog.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "toonzqt/gutil.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tcolorstyles.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include <qhboxlayout></qhboxlayout>
|
|
Toshihiro Shimizu |
890ddd |
#include <qlabel></qlabel>
|
|
Toshihiro Shimizu |
890ddd |
#include <qslider></qslider>
|
|
Toshihiro Shimizu |
890ddd |
#include <qpainter></qpainter>
|
|
Toshihiro Shimizu |
890ddd |
#include <qlineargradient></qlineargradient>
|
|
Toshihiro Shimizu |
890ddd |
#include <qmouseevent></qmouseevent>
|
|
Toshihiro Shimizu |
890ddd |
#include <qimage></qimage>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
using namespace DVGui;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=============================================================================
|
|
Toshihiro Shimizu |
890ddd |
/*! \class DVGui::SpectrumBar
|
|
Shinya Kitaoka |
120a6e |
\brief The SpectrumBar class provides a bar to display a
|
|
Shinya Kitaoka |
120a6e |
spectrum
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
Inherits \b QWidget.
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
The object allows to manage a spectrum, permit you to insert,
|
|
Shinya Kitaoka |
120a6e |
move and
|
|
Shinya Kitaoka |
120a6e |
remove key.
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
You can set spectrum default color in constructor, or set color
|
|
Shinya Kitaoka |
120a6e |
of current
|
|
Shinya Kitaoka |
120a6e |
key, \b getCurrentColor(), using \b setCurrentColor(). It's
|
|
Shinya Kitaoka |
120a6e |
possible to change
|
|
Shinya Kitaoka |
120a6e |
current key, \b getCurrentKeyIndex(), through its index, \b
|
|
Shinya Kitaoka |
120a6e |
setCurrentKeyIndex().
|
|
Shinya Kitaoka |
120a6e |
You can also set current key position, \b getCurrentPos(), using
|
|
Shinya Kitaoka |
120a6e |
\b setCurrentPos()
|
|
Shinya Kitaoka |
120a6e |
or add a new key in a specific position, addKeyAt().
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
/*! \fn void DVGui::SpectrumBar::currentPosChanged()
|
|
Shinya Kitaoka |
120a6e |
The signal is emitted when current key position change.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
/*! \fn void DVGui::SpectrumBar::currentKeyChanged()
|
|
Shinya Kitaoka |
120a6e |
The signal is emitted when current key index change.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
SpectrumBar::SpectrumBar(QWidget *parent, TPixel32 color)
|
|
Shinya Kitaoka |
120a6e |
: QWidget(parent)
|
|
Shinya Kitaoka |
120a6e |
, m_x0(10)
|
|
Shinya Kitaoka |
120a6e |
, m_currentKeyIndex(0)
|
|
Shinya Kitaoka |
120a6e |
, m_spectrum(color, color)
|
|
Shinya Kitaoka |
120a6e |
, m_chessBg(":Resources/backg.png") {
|
|
Shinya Kitaoka |
120a6e |
setMinimumWidth(200);
|
|
Shinya Kitaoka |
120a6e |
setFixedHeight(WidgetHeight);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
SpectrumBar::~SpectrumBar() {}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Return current key position.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
int SpectrumBar::getCurrentPos() {
|
|
Shinya Kitaoka |
120a6e |
if (m_currentKeyIndex == -1) return -1;
|
|
Shinya Kitaoka |
120a6e |
return spectrumValueToPos(m_spectrum.getKey(m_currentKeyIndex).first);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Return current key color.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
TPixel32 SpectrumBar::getCurrentColor() {
|
|
Shinya Kitaoka |
120a6e |
if (m_currentKeyIndex == -1) // C'e' sicuramente una key perche' lo spectrum
|
|
Shinya Kitaoka |
120a6e |
// e' strutturato in modo da non avere mai size
|
|
Shinya Kitaoka |
120a6e |
// nulla.
|
|
Shinya Kitaoka |
120a6e |
return m_spectrum.getKey(getMaxPosKeyIndex()).second;
|
|
Shinya Kitaoka |
120a6e |
return m_spectrum.getKey(m_currentKeyIndex).second;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Set current key index to \b index. Update spectrum bar and emit a signal to
|
|
Shinya Kitaoka |
120a6e |
notify current key changed \b currentKeyChanged().
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::setCurrentKeyIndex(int index) {
|
|
Shinya Kitaoka |
120a6e |
if (m_currentKeyIndex == index) return;
|
|
Shinya Kitaoka |
120a6e |
m_currentKeyIndex = index;
|
|
Shinya Kitaoka |
120a6e |
update();
|
|
Shinya Kitaoka |
120a6e |
emit currentKeyChanged();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Add a new key to spectrum. Key position is computed \b x using
|
|
Shinya Kitaoka |
120a6e |
\b posToSpectrumValue() and color come from position.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::addKeyAt(int x) {
|
|
Shinya Kitaoka |
120a6e |
TPixel32 color = m_spectrum.getValue(posToSpectrumValue(x));
|
|
Shinya Kitaoka |
120a6e |
TSpectrum::ColorKey key(posToSpectrumValue(x), color);
|
|
Shinya Kitaoka |
120a6e |
m_spectrum.addKey(key);
|
|
Shinya Kitaoka |
120a6e |
int newIndex = m_spectrum.getKeyCount() - 1;
|
|
Shinya Kitaoka |
120a6e |
setCurrentKeyIndex(newIndex);
|
|
Shinya Kitaoka |
120a6e |
emit currentKeyAdded(newIndex);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Set current key position to \b pos. Update spectrum bar and emit a signal to
|
|
Shinya Kitaoka |
120a6e |
notify current key changed \b currentKeyChanged().
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::setCurrentPos(int pos, bool isDragging) {
|
|
Shinya Kitaoka |
120a6e |
TSpectrum::ColorKey key = m_spectrum.getKey(m_currentKeyIndex);
|
|
Shinya Kitaoka |
120a6e |
key.first = posToSpectrumValue(pos);
|
|
Shinya Kitaoka |
120a6e |
m_spectrum.setKey(m_currentKeyIndex, key);
|
|
Shinya Kitaoka |
120a6e |
update();
|
|
Shinya Kitaoka |
120a6e |
emit currentPosChanged(isDragging);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Set current key color to \b color. Update spectrum bar.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::setCurrentColor(const TPixel32 &color) {
|
|
Shinya Kitaoka |
120a6e |
if (m_currentKeyIndex == -1) return;
|
|
Shinya Kitaoka |
120a6e |
TSpectrum::ColorKey key = m_spectrum.getKey(m_currentKeyIndex);
|
|
Shinya Kitaoka |
120a6e |
TPixel32 oldColor = key.second;
|
|
Shinya Kitaoka |
120a6e |
if (oldColor == color) return;
|
|
Shinya Kitaoka |
120a6e |
key.second = color;
|
|
Shinya Kitaoka |
120a6e |
m_spectrum.setKey(m_currentKeyIndex, key);
|
|
Shinya Kitaoka |
120a6e |
update();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Return spectrum value from widget position \b pos.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
double SpectrumBar::posToSpectrumValue(int pos) {
|
|
Shinya Kitaoka |
120a6e |
return (double)(pos - m_x0) / (double)(width() - 2 * m_x0 - 1);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Return widget position from spectrum value \b spectrumValue.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
int SpectrumBar::spectrumValueToPos(double spectrumValue) {
|
|
Shinya Kitaoka |
120a6e |
return m_x0 + int(0.5 + spectrumValue * (width() - 2 * m_x0));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Paint widget object:
|
|
Shinya Kitaoka |
120a6e |
\li a linear gradient computed with spectrum value;
|
|
Shinya Kitaoka |
120a6e |
\li arrows for each spectrum key.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::paintEvent(QPaintEvent *e) {
|
|
Shinya Kitaoka |
120a6e |
QPainter p(this);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
int h = height() - 1;
|
|
Shinya Kitaoka |
120a6e |
int y1 = height() / 2;
|
|
Shinya Kitaoka |
120a6e |
int x1 = width() - m_x0;
|
|
Shinya Kitaoka |
120a6e |
QRectF rectBar = QRectF(m_x0, 0, x1 - m_x0 + 1, y1);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Build the linear gradient
|
|
Shinya Kitaoka |
120a6e |
QLinearGradient linearGrad(QPointF(m_x0, h), QPointF(x1, h));
|
|
Shinya Kitaoka |
120a6e |
int spectrumSize = m_spectrum.getKeyCount();
|
|
Shinya Kitaoka |
120a6e |
int i;
|
|
Shinya Kitaoka |
120a6e |
for (i = 0; i < spectrumSize; i++) {
|
|
Shinya Kitaoka |
120a6e |
TSpectrum::ColorKey key = m_spectrum.getKey(i);
|
|
Shinya Kitaoka |
120a6e |
// Linear Gradient
|
|
Shinya Kitaoka |
120a6e |
TPixel32 pix = key.second;
|
|
Shinya Kitaoka |
120a6e |
double spectrumValue = key.first;
|
|
Shinya Kitaoka |
120a6e |
linearGrad.setColorAt(spectrumValue, QColor(pix.r, pix.g, pix.b, pix.m));
|
|
Shinya Kitaoka |
120a6e |
// Frecce delle key
|
|
Shinya Kitaoka |
120a6e |
int pos = spectrumValueToPos(spectrumValue);
|
|
Shinya Kitaoka |
120a6e |
int f = 4;
|
|
Shinya Kitaoka |
120a6e |
drawArrow(p, QPointF(pos - f, y1 + f), QPointF(pos, y1),
|
|
Shinya Kitaoka |
120a6e |
QPointF(pos + f, y1 + f), true,
|
|
Shinya Kitaoka |
120a6e |
(m_currentKeyIndex == i) ? Qt::black : Qt::white);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
p.setPen(Qt::NoPen);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Draw chess bg
|
|
Shinya Kitaoka |
120a6e |
QBrush bg;
|
|
Shinya Kitaoka |
120a6e |
bg.setTexture(m_chessBg);
|
|
Shinya Kitaoka |
120a6e |
p.setBrush(bg);
|
|
Shinya Kitaoka |
120a6e |
p.drawRect(rectBar);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Draw the gradient
|
|
Shinya Kitaoka |
120a6e |
p.setBrush(linearGrad);
|
|
Shinya Kitaoka |
120a6e |
p.drawRect(rectBar);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
luz paz |
6454c4 |
/*! Manage mouse press event. Search existing key near click position, if
|
|
Shinya Kitaoka |
120a6e |
there is set it as current key; otherwise add a new key in click
|
|
Shinya Kitaoka |
120a6e |
position.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::mousePressEvent(QMouseEvent *e) {
|
|
Shinya Kitaoka |
120a6e |
QPoint pos = e->pos();
|
|
Shinya Kitaoka |
120a6e |
int x = pos.x();
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Verifico se esiste una key vicino alla posizione in cui ho cliccato
|
|
Shinya Kitaoka |
120a6e |
int index = -1;
|
|
Shinya Kitaoka |
120a6e |
int spectrumSize = m_spectrum.getKeyCount();
|
|
Shinya Kitaoka |
120a6e |
if (x < m_x0)
|
|
Shinya Kitaoka |
120a6e |
index = getMinPosKeyIndex();
|
|
Shinya Kitaoka |
120a6e |
else if (x > width() - m_x0)
|
|
Shinya Kitaoka |
120a6e |
index = getMaxPosKeyIndex();
|
|
Shinya Kitaoka |
120a6e |
else
|
|
Shinya Kitaoka |
120a6e |
index = getNearPosKeyIndex(x);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Se x e' vicino a una key esistente setto questa come corrente
|
|
Shinya Kitaoka |
120a6e |
if (index != -1)
|
|
Shinya Kitaoka |
120a6e |
setCurrentKeyIndex(index);
|
|
Shinya Kitaoka |
120a6e |
else // Altrimenti aggiungo una nuova key
|
|
Shinya Kitaoka |
120a6e |
addKeyAt(x);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Manage mouse move event. If y position value greater than widget height
|
|
Shinya Kitaoka |
120a6e |
erase current key; otherwise move current key position.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::mouseMoveEvent(QMouseEvent *e) {
|
|
Shinya Kitaoka |
120a6e |
QPoint pos = e->pos();
|
|
Shinya Kitaoka |
120a6e |
int x = pos.x();
|
|
Shinya Kitaoka |
120a6e |
int y = pos.y();
|
|
Shinya Kitaoka |
120a6e |
if (x < m_x0 || x > width() - m_x0 - 1) return;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Se il valore della y e' maggiore dell'altezza widget, cioe' va fuori dal
|
|
Shinya Kitaoka |
120a6e |
// widget
|
|
Shinya Kitaoka |
120a6e |
// elimino la key corrente
|
|
Shinya Kitaoka |
120a6e |
if (y > height()) {
|
|
Shinya Kitaoka |
120a6e |
if (m_currentKeyIndex == -1 || m_spectrum.getKeyCount() == 1) return;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
m_spectrum.removeKey(m_currentKeyIndex);
|
|
Shinya Kitaoka |
120a6e |
int keyIndex = m_currentKeyIndex;
|
|
Shinya Kitaoka |
120a6e |
setCurrentKeyIndex(-1);
|
|
Shinya Kitaoka |
120a6e |
emit currentKeyRemoved(keyIndex);
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Se il valore della y e' interno al widget
|
|
Shinya Kitaoka |
120a6e |
// Se l'indice della key corrente e' -1 avevo eliminato un key; ora devo
|
|
Shinya Kitaoka |
120a6e |
// riaggiungerla.
|
|
Shinya Kitaoka |
120a6e |
if (m_currentKeyIndex == -1) addKeyAt(x);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Setto la nuova posizione x alla chiave corrente
|
|
Shinya Kitaoka |
120a6e |
setCurrentPos(x, true);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void SpectrumBar::mouseReleaseEvent(QMouseEvent *e) {
|
|
Shinya Kitaoka |
120a6e |
if (m_currentKeyIndex == -1) return;
|
|
Shinya Kitaoka |
120a6e |
QPoint pos = e->pos();
|
|
Shinya Kitaoka |
120a6e |
int x = pos.x();
|
|
Shinya Kitaoka |
120a6e |
int y = pos.y();
|
|
Shinya Kitaoka |
120a6e |
if (x < m_x0 || x > width() - m_x0 - 1) return;
|
|
Shinya Kitaoka |
120a6e |
if (y > height()) return;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
setCurrentPos(x, false);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Return index of key with maximum position.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
int SpectrumBar::getMaxPosKeyIndex() {
|
|
Shinya Kitaoka |
120a6e |
int spectrumSize = m_spectrum.getKeyCount();
|
|
Shinya Kitaoka |
120a6e |
if (!spectrumSize) return -1;
|
|
Shinya Kitaoka |
120a6e |
int maxPosIndex = 0;
|
|
Shinya Kitaoka |
120a6e |
int maxPos = spectrumValueToPos(m_spectrum.getKey(0).first);
|
|
Shinya Kitaoka |
120a6e |
int i;
|
|
Shinya Kitaoka |
120a6e |
for (i = 0; i < spectrumSize; i++) {
|
|
Shinya Kitaoka |
120a6e |
int newPos = spectrumValueToPos(m_spectrum.getKey(i).first);
|
|
Shinya Kitaoka |
120a6e |
if (maxPos < newPos) {
|
|
Shinya Kitaoka |
120a6e |
maxPos = newPos;
|
|
Shinya Kitaoka |
120a6e |
maxPosIndex = i;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
return maxPosIndex;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Return index of key with minimum position.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
int SpectrumBar::getMinPosKeyIndex() {
|
|
Shinya Kitaoka |
120a6e |
int spectrumSize = m_spectrum.getKeyCount();
|
|
Shinya Kitaoka |
120a6e |
if (!spectrumSize) return -1;
|
|
Shinya Kitaoka |
120a6e |
int minPosIndex = 0;
|
|
Shinya Kitaoka |
120a6e |
int minPos = spectrumValueToPos(m_spectrum.getKey(0).first);
|
|
Shinya Kitaoka |
120a6e |
int i;
|
|
Shinya Kitaoka |
120a6e |
for (i = 0; i < spectrumSize; i++) {
|
|
Shinya Kitaoka |
120a6e |
int newPos = spectrumValueToPos(m_spectrum.getKey(i).first);
|
|
Shinya Kitaoka |
120a6e |
if (minPos > newPos) {
|
|
Shinya Kitaoka |
120a6e |
minPos = newPos;
|
|
Shinya Kitaoka |
120a6e |
minPosIndex = i;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
return minPosIndex;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Return index of key "near" \b pos. "near" mean with a dinstace less than
|
|
Shinya Kitaoka |
120a6e |
a constant size. If there aren't key near pos return \b -1.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
int SpectrumBar::getNearPosKeyIndex(int pos) {
|
|
Shinya Kitaoka |
120a6e |
int i;
|
|
Shinya Kitaoka |
120a6e |
int gap = 20;
|
|
Shinya Kitaoka |
120a6e |
for (i = 0; i < m_spectrum.getKeyCount(); i++)
|
|
Shinya Kitaoka |
120a6e |
if (areAlmostEqual(double(pos),
|
|
Shinya Kitaoka |
120a6e |
double(spectrumValueToPos(m_spectrum.getKey(i).first)),
|
|
Shinya Kitaoka |
120a6e |
gap))
|
|
Shinya Kitaoka |
120a6e |
return i;
|
|
Shinya Kitaoka |
120a6e |
return -1;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=============================================================================
|
|
Toshihiro Shimizu |
890ddd |
/*! \class DVGui::SpectrumField
|
|
Shinya Kitaoka |
120a6e |
\brief The SpectrumField class provides an object to display and
|
|
Shinya Kitaoka |
120a6e |
manage a spectrum.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
Inherits \b QWidget.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
The object is composed of a vertical layout \b QVBoxLayout which
|
|
Shinya Kitaoka |
120a6e |
contains a
|
|
Shinya Kitaoka |
120a6e |
spectrum bar \b SpectrumBar and a color field \b ColorField.
|
|
Shinya Kitaoka |
120a6e |
Using color field
|
|
Shinya Kitaoka |
120a6e |
it's possible to set spectrum current key color.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
\b Example: Spectrum Field.
|
|
Shinya Kitaoka |
120a6e |
\image html SpectrumField.jpg
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
SpectrumField::SpectrumField(QWidget *parent, TPixel32 color)
|
|
Shinya Kitaoka |
120a6e |
: QWidget(parent), m_margin(0), m_spacing(4) {
|
|
Shinya Kitaoka |
120a6e |
setFixedHeight(60);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
QVBoxLayout *layout = new QVBoxLayout();
|
|
Shinya Kitaoka |
120a6e |
layout->setMargin(m_margin);
|
|
Shinya Kitaoka |
120a6e |
layout->setSpacing(m_spacing);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
m_spectrumbar = new SpectrumBar(this, color);
|
|
Shinya Kitaoka |
120a6e |
connect(m_spectrumbar, SIGNAL(currentPosChanged(bool)),
|
|
Shinya Kitaoka |
120a6e |
SLOT(onCurrentPosChanged(bool)));
|
|
Shinya Kitaoka |
120a6e |
connect(m_spectrumbar, SIGNAL(currentKeyChanged()),
|
|
Shinya Kitaoka |
120a6e |
SLOT(onCurrentKeyChanged()));
|
|
Shinya Kitaoka |
120a6e |
connect(m_spectrumbar, SIGNAL(currentKeyAdded(int)), SIGNAL(keyAdded(int)));
|
|
Shinya Kitaoka |
120a6e |
connect(m_spectrumbar, SIGNAL(currentKeyRemoved(int)),
|
|
Shinya Kitaoka |
120a6e |
SIGNAL(keyRemoved(int)));
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
layout->addWidget(m_spectrumbar);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
m_colorField = new ColorField(this, true, color, 36);
|
|
Shinya Kitaoka |
120a6e |
connect(m_colorField, SIGNAL(colorChanged(const TPixel32 &, bool)), this,
|
|
Shinya Kitaoka |
120a6e |
SLOT(onColorChanged(const TPixel32 &, bool)));
|
|
Shinya Kitaoka |
120a6e |
layout->addWidget(m_colorField, 0, Qt::AlignLeft);
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
setLayout(layout);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Update all widget and emit keyPositionChanged() signal.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumField::onCurrentPosChanged(bool isDragging) {
|
|
Shinya Kitaoka |
120a6e |
if (m_spectrumbar->getCurrentKeyIndex() == -1) return;
|
|
Shinya Kitaoka |
120a6e |
update();
|
|
Shinya Kitaoka |
120a6e |
emit keyPositionChanged(isDragging);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Set color field to spectrum current key color and update.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumField::onCurrentKeyChanged() {
|
|
Shinya Kitaoka |
120a6e |
if (m_spectrumbar->getCurrentKeyIndex() != -1)
|
|
Shinya Kitaoka |
120a6e |
m_colorField->setColor(m_spectrumbar->getCurrentColor());
|
|
Shinya Kitaoka |
120a6e |
update();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Set spectrum current key color to \b color and emit keyColorChanged().
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumField::onColorChanged(const TPixel32 &color, bool isDragging) {
|
|
Shinya Kitaoka |
120a6e |
m_spectrumbar->setCurrentColor(color);
|
|
Shinya Kitaoka |
120a6e |
emit keyColorChanged(isDragging);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
/*! Paint an arrow to connect current spectrum key and square color field.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Shinya Kitaoka |
120a6e |
void SpectrumField::paintEvent(QPaintEvent *e) {
|
|
Shinya Kitaoka |
120a6e |
int curPos = m_spectrumbar->getCurrentPos();
|
|
Shinya Kitaoka |
120a6e |
if (curPos == -1) return;
|
|
Shinya Kitaoka |
120a6e |
QPainter p(this);
|
|
Shinya Kitaoka |
120a6e |
int x0 = 18 + m_margin;
|
|
Shinya Kitaoka |
120a6e |
int y0 = 2 * m_margin + WidgetHeight + m_spacing;
|
|
Shinya Kitaoka |
120a6e |
int y = m_margin + m_spacing +
|
|
Shinya Kitaoka |
120a6e |
(WidgetHeight * 0.5 -
|
|
Shinya Kitaoka |
120a6e |
4); // 4 e' l'altezza della freccia nella spectrum bar
|
|
Shinya Kitaoka |
120a6e |
int y1 = y0 - y * 0.5 + 1;
|
|
Shinya Kitaoka |
120a6e |
int y2 = y0 - y;
|
|
Shinya Kitaoka |
120a6e |
curPos += m_margin;
|
|
Shinya Kitaoka |
120a6e |
p.setPen(Qt::black);
|
|
Shinya Kitaoka |
120a6e |
p.drawLine(x0, y0, x0, y1);
|
|
Shinya Kitaoka |
120a6e |
p.drawLine(x0, y1, curPos, y1);
|
|
Shinya Kitaoka |
120a6e |
p.drawLine(curPos, y1, curPos, y2);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
SpectrumField::~SpectrumField() {}
|