From 3733e88255cd2acfde21cb74c08826b421321d5d Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Mon, 28 Jan 2008 16:04:42 +0000 Subject: [PATCH] *** empty log message *** --- src/fgt/edit_quality/eqhandle.cpp | 166 ++++++++++++++++++++++++++++++ src/fgt/edit_quality/eqhandle.h | 61 +++++++++++ 2 files changed, 227 insertions(+) create mode 100644 src/fgt/edit_quality/eqhandle.cpp create mode 100644 src/fgt/edit_quality/eqhandle.h diff --git a/src/fgt/edit_quality/eqhandle.cpp b/src/fgt/edit_quality/eqhandle.cpp new file mode 100644 index 000000000..564747531 --- /dev/null +++ b/src/fgt/edit_quality/eqhandle.cpp @@ -0,0 +1,166 @@ +#include "EqHandle.h" + +EqHandle::EqHandle() +{ + _barHeight = 100; + _size = 4; + + QPointF a(-_size/2.0f,-_size); + QPointF b(_size/2.0f, -_size); + QPointF c(0, -1.87f*_size); + + _triangle.append(QLineF(a,b)); + _triangle.append(QLineF(b,c)); + _triangle.append(QLineF(c,a)); +} + +/* +EqHandle::~EqHandle(void) +{ +}*/ + +void EqHandle::setBarHeight(qreal height) +{ + _barHeight = height; +} + +void EqHandle::setHistogramInfo (CHART_INFO *info) +{ + _histogramInfo = info; +} + +void EqHandle::setType (EQUALIZER_HANDLE_TYPE type) +{ + _type = type; +} + +void EqHandle::setHandlesPointer(EqHandle* pointer) +{ + _handlesPointer = pointer; +} + +void EqHandle::setMidHandlePercentilePosition(qreal* pointer) +{ + _midHandlePercentilePosition = pointer; +} + +void EqHandle::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0*/ ) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + QPen pen(Qt::black); + pen.setWidth(2); + painter->setPen(_color); + painter->setBrush(_color); + painter->drawLine(0, -_size, 0, -_barHeight); + + painter->drawLines(_triangle); + painter->drawRect(-_size/2.0f, -_size, _size, _size); +} + +QRectF EqHandle::boundingRect () const +{ + return QRectF(-_size/2.0f, -_barHeight, _size, _barHeight); +} + +void EqHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + setCursor(Qt::OpenHandCursor); + + + QPointF newPos = event->scenePos(); + if ( (newPos.x() < _histogramInfo->leftBorder) || (newPos.x() > _histogramInfo->rightBorder) ) + return; + + QPointF oldPos = pos(); + qreal handleOffset = newPos.x()-oldPos.x(); + if (handleOffset<0) + handleOffset = -handleOffset; + + qreal leftx = _handlesPointer[LEFT_HANDLE].pos().x(); + qreal midx = _handlesPointer[MID_HANDLE].pos().x(); + qreal rightx= _handlesPointer[RIGHT_HANDLE].pos().x(); + + if (handleOffset >= std::numeric_limits::epsilon()) + { + switch (_type) + { + case MID_HANDLE: + if ( (newPos.x() > _handlesPointer[LEFT_HANDLE].pos().x()) && (newPos.x() < _handlesPointer[RIGHT_HANDLE].pos().x()) ) + { + *_midHandlePercentilePosition = calculateMidHandlePercentilePosition(newPos.x()); + moveMidHandle(); + } + break; + case LEFT_HANDLE: + if (newPos.x() < _handlesPointer[RIGHT_HANDLE].pos().x()) + { + setPos(newPos.x(), oldPos.y()); + qreal percentagePos = (pos().x()-_histogramInfo->leftBorder) / _histogramInfo->chartWidth; + qreal newSpinboxValue = percentagePos * (_histogramInfo->maxX - _histogramInfo->minX) + _histogramInfo->minX; + emit positionChangedToSpinBox((double)newSpinboxValue); + emit positionChangedToMidHandle(); + } + break; + case RIGHT_HANDLE: + if (newPos.x() > _handlesPointer[LEFT_HANDLE].pos().x()) + { + setPos(newPos.x(), oldPos.y()); + qreal percentagePos = (pos().x()-_histogramInfo->leftBorder) / _histogramInfo->chartWidth; + qreal newSpinboxValue = percentagePos * (_histogramInfo->maxX - _histogramInfo->minX) + _histogramInfo->minX; + emit positionChangedToSpinBox((double)newSpinboxValue); + emit positionChangedToMidHandle(); + } + break; + } + } +} + +void EqHandle::moveMidHandle() +{ + assert(_type==MID_HANDLE); + qreal newPosX = _handlesPointer[LEFT_HANDLE].pos().x() + *_midHandlePercentilePosition * (_handlesPointer[RIGHT_HANDLE].pos().x() - _handlesPointer[LEFT_HANDLE].pos().x()); + + setPos(newPosX, pos().y()); + + qreal percentagePos = (newPosX - _histogramInfo->leftBorder) / _histogramInfo->chartWidth; + qreal newSpinboxValue = percentagePos * (_histogramInfo->maxX - _histogramInfo->minX) + _histogramInfo->minX; + // Rischio di ricorsione? + emit positionChangedToSpinBox((double)newSpinboxValue); +} + +void EqHandle::setXBySpinBoxValueChanged(double spinBoxValue) +{ + qreal percentageValue = (spinBoxValue - _histogramInfo->minX) / (_histogramInfo->maxX - _histogramInfo->minX); + qreal newHandleX = percentageValue * _histogramInfo->chartWidth + _histogramInfo->leftBorder; + + qreal handleOffset = newHandleX-pos().x(); + if (handleOffset<0) + handleOffset = -handleOffset; + // this control avoid counter invoking (?) + if (handleOffset < std::numeric_limits::epsilon()) + return; + + switch (_type) + { + case MID_HANDLE: + if ( (newHandleX > _handlesPointer[LEFT_HANDLE].pos().x()) && (newHandleX < _handlesPointer[RIGHT_HANDLE].pos().x()) ) + { + *_midHandlePercentilePosition = calculateMidHandlePercentilePosition(newHandleX); + moveMidHandle(); + } + break; + case LEFT_HANDLE: + if (newHandleX < _handlesPointer[RIGHT_HANDLE].pos().x()) + { + setPos(newHandleX, pos().y()); + } + break; + case RIGHT_HANDLE: + if (newHandleX > _handlesPointer[LEFT_HANDLE].pos().x()) + { + setPos(newHandleX, pos().y()); + } + break; + } +} diff --git a/src/fgt/edit_quality/eqhandle.h b/src/fgt/edit_quality/eqhandle.h new file mode 100644 index 000000000..3a92c247a --- /dev/null +++ b/src/fgt/edit_quality/eqhandle.h @@ -0,0 +1,61 @@ +#ifndef _EQHANDLE_H_ +#define _EQHANDLE_H_ + +//#include +//#include +#include "handle.h" +#include "util.h" + +enum EQUALIZER_HANDLE_TYPE +{ + LEFT_HANDLE = 0, + MID_HANDLE, + RIGHT_HANDLE, + NUMBER_OF_POSITIONS +}; + +/* Specific handle for equalizerHistogramScene +It can only be dragged horizontally */ +class EqHandle : public Handle +{ + Q_OBJECT + +public: + EqHandle(); + //~EqHandle(void); + QRectF boundingRect () const; + void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); + void setBarHeight (qreal); + void setHistogramInfo (CHART_INFO*); + void setType(EQUALIZER_HANDLE_TYPE); + void setMidHandlePercentilePosition(qreal*); + void setHandlesPointer(EqHandle*); + + +protected: + void mouseMoveEvent (QGraphicsSceneMouseEvent *event); + +private: + qreal _barHeight; + QVector _triangle; + CHART_INFO* _histogramInfo; + EQUALIZER_HANDLE_TYPE _type; + qreal* _midHandlePercentilePosition; + EqHandle* _handlesPointer; + + qreal calculateMidHandlePercentilePosition(qreal newHandleX) + { + return (newHandleX - _handlesPointer[LEFT_HANDLE].pos().x()) / (_handlesPointer[RIGHT_HANDLE].pos().x() - _handlesPointer[LEFT_HANDLE].pos().x()); + }; + +signals: + void positionChangedToSpinBox(double); + void positionChangedToMidHandle(); + +private slots: + // changing equalizer spinboxes moves the connected handle + void setXBySpinBoxValueChanged (double spinBoxValue); + void moveMidHandle(); +}; + +#endif // EQHANDLE_H \ No newline at end of file