soundeditor/qatsh/ColorScale.cpp

165 lines
5.1 KiB
C++

// ColorScale class implementation
//
// A QGraphicsView displaying a color scale from a give color map
//
// QATSH Copyright 2009 Jean-Philippe MEURET <jpmeuret@free.fr>
#include <QtGui/QResizeEvent>
#include <QtGui/QGraphicsSimpleTextItem>
#include <iostream>
#include "ColorScale.h"
#include "ColorMap.h"
ColorScale::ColorScale(QWidget *parent)
: QGraphicsView(parent), _pColorMap(0), _pqgiMinLabel(0), _pqgiMaxLabel(0)
{
setAlignment(Qt::AlignTop|Qt::AlignLeft);
setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
setBackgroundBrush(QBrush(Qt::black));
setScene(new QGraphicsScene(parent));
//std::cout << "ColorScale::ColorScale : viewRect = "
// << width() << "x" << height() << std::endl;
}
ColorScale::~ColorScale()
{
clear();
}
void ColorScale::onColorMapChanged(const ColorMap* pColorMap)
{
_pColorMap = pColorMap;
redraw();
}
void ColorScale::clear()
{
QGraphicsScene* pqgvScene = scene();
foreach(QGraphicsItem* pqgiItem, pqgvScene->items())
{
pqgvScene->removeItem(pqgiItem);
delete pqgiItem;
}
_pqgiMinLabel = 0;
_pqgiMaxLabel = 0;
}
void ColorScale::redraw()
{
//std::cout << "ColorScale::redraw" << std::endl;
//setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
double dLabelScaleX = 1.0;
double dLabelScaleY = 1.0;
if (_pqgiMinLabel)
{
const QTransform qtLabelTrans = _pqgiMinLabel->transform();
dLabelScaleX = qtLabelTrans.m11();
dLabelScaleY = qtLabelTrans.m22();
}
//std::cout << "ColorScale::redraw : Label scale (" << _pqgiMinLabel << ") x="
// << dLabelScaleX << ", y=" << dLabelScaleY << std::endl;
// Clear the scene.
clear();
// Reset scene rectangle.
QGraphicsScene* pqgvScene = scene();
const int nColors = _pColorMap->nbColors();
pqgvScene->setSceneRect(0.0, 0.0, 24.0, nColors);
// Draw the scale : 1 colored rectangle for each color.
for (int nColorInd = 0; nColorInd < nColors; nColorInd++)
{
const QColor& qColor = _pColorMap->color(nColorInd);
QGraphicsRectItem* pgiRect =
new QGraphicsRectItem(0.0, 0.0, 24.0, 1.0);
pgiRect->setPos(0.0, nColors - 1 - nColorInd);
pgiRect->setPen(QPen(qColor));
pgiRect->setBrush(QBrush(qColor));
pgiRect->setZValue(-1.0); // Ensure min/max labels not drawn behind this one.
pqgvScene->addItem(pgiRect);
}
// Draw min and max value.
//QFont qFont("Serif", 8); // Windows OK.
//QFont qFont("Courier", 8); // Windows OK.
//QFont qFont("Times", 8); // Windows OK.
//QFont qFont("Fixed", 8); // Windows OK.
QFont qFont("Tahoma", 8); // Windows OK.
qFont.setStretch(80);
//std::cout << "ColorScale::redraw : Font " << qFont.rawName().toStdString()
// << ", size pt " << qFont.pointSize()
// << ", pix " << qFont.pixelSize() << std::endl;
_pqgiMaxLabel =
new QGraphicsSimpleTextItem(QString("%1").arg(_pColorMap->maxValue(), 0, 'g', 2));
_pqgiMaxLabel->setPos(1.0, 0.0);
//QPen qPen; // = _pqgiMaxLabel->pen();
//qPen.setColor(Qt::white);
//qPen.setCosmetic(true);
_pqgiMaxLabel->setPen(QPen(Qt::white));
//_pqgiMaxLabel->setBrush(Qt::white);
_pqgiMaxLabel->setFont(qFont);
_pqgiMaxLabel->scale(dLabelScaleX, dLabelScaleY);
pqgvScene->addItem(_pqgiMaxLabel);
_pqgiMinLabel =
new QGraphicsSimpleTextItem(QString("%1").arg(_pColorMap->minValue(), 0, 'g', 2));
_pqgiMinLabel->setPos(1.0, nColors - _pqgiMinLabel->sceneBoundingRect().height());
_pqgiMinLabel->setPen(QPen(Qt::white));
//_pqgiMinLabel->setPen(QColor(0, 0, 0));
//_pqgiMinLabel->setBrush(QBrush(Qt::white));
_pqgiMinLabel->setFont(qFont);
_pqgiMinLabel->scale(dLabelScaleX, dLabelScaleY);
pqgvScene->addItem(_pqgiMinLabel);
QResizeEvent qre(size(), size());
resizeEvent(&qre);
//setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
}
void ColorScale::resizeEvent(QResizeEvent *pqEvent)
{
//std::cout << "ColorScale::resizeEvent("
// << pqEvent->size().width() << "x" << pqEvent->size().height()<< ")"<< std::endl;
// Save previous view scale.
const QTransform qmOldTrans = transform();
// Apply new scale because of resizing.
QTransform qmNewTrans;
const QRectF sceneRect = scene()->sceneRect();
qmNewTrans.scale(pqEvent->size().width() / sceneRect.width(),
pqEvent->size().height() / sceneRect.height());
setTransform(qmNewTrans);
// Apply inverted scale to the labels (we don't want they change their size).
// TODO: Remove this awful trick and use QGraphicsItem::ItemIgnoresTransformations
// when creating the labels.
_pqgiMinLabel->scale(qmOldTrans.m11() / qmNewTrans.m11(),
qmOldTrans.m22() / qmNewTrans.m22());
_pqgiMinLabel->setPos(1.0, _pColorMap->nbColors()
- _pqgiMinLabel->sceneBoundingRect().height());
_pqgiMaxLabel->scale(qmOldTrans.m11() / qmNewTrans.m11(),
qmOldTrans.m22() / qmNewTrans.m22());
//QTransform qtLabelTrans = _pqgiMinLabel->transform();
//std::cout << "ColorScale::resizeEvent : Label trans m11="
// << qtLabelTrans.m11() << ", m22=" << qtLabelTrans.m22() << std::endl;
QGraphicsView::resizeEvent(pqEvent);
}