1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/**
* @author Mark Larkin, Conway Institute, UCD. mark.larkin@ucd.ie
*
* Changes:
*
* 16-01-07,Nigel Brown(EMBL): added drawContents() called by paintEvent().
*
* 31-01-07,Nigel Brown(EMBL): changed && to || in updateBoxSize(). Changed
* drawContents() to paint histogram 'bars' before writing ruler text as this
* was effectively clipped by the histogram bar on the next pass.
*
* 06-02-07,Nigel Brown(EMBL): darker histogram bars for Mac.
*
* 20-02-07,Nigel Brown(EMBL): constructor and updateBoxSize() parameters
* reordered.
*
* 12-4-07, Mark Larkin : Changed destructor. We dont need to delete QObjects.
*
* 16-04-07,Nigel Brown(EMBL): removed destructor; arrayOfHeights not
* allocated here.
****************************************************************************/
#include "HistogramWidget.h"
#include <QtGui>
#include <QImage>
//#include <QPixmap>
#include <QPoint>
#include <QSize>
#include <QString>
#include <QWidget>
#include <cmath>
// next two only needed for cerr/debugging
//#include <iostream>
//using namespace std;
/**
* This is the constructor for a HistogramWidget.
* @param histogramValues[] The array of histogram values to be displayed.
* @param length The length of the alignment.
* @param boxSize The width of the histogram bars.
* @param *parent The parent of this widget. This parameter is optional.
*/
HistogramWidget::HistogramWidget(int _boxWidth, int _boxSize, QWidget *parent)
: QWidget(parent)
{
/*
* Set up the initial values.
*/
alignLength = 0;
arrayOfHeights = 0;
isSet = false;
boxSize = _boxSize;
boxWidth = _boxWidth;
heightOfBox = 1;
//histogramImage = 0;
displayStartPos = 0;
extraWhiteSpace = 5;
lengthSmallBar = 1;
lengthLargeBar = 3;
setBackgroundRole(QPalette::Base);
/*
* Create a color to display the ruler in.
*/
rulerColor = new QColor;
rulerColor->setRgb(192,215,233);
offSetFromTop = 10;
heightAllowed = 50;
updateSizeHint();
}
/**
* This function overrides the parents sizeHint function. It gives an idea of
* the size this widget needs.
*/
QSize HistogramWidget::sizeHint() const
{
//(void) printf("HistogramWidget::sizeHint(w=%d,h=%d) <- [ha=%d, hob=%d, ews=%d]\n",
// alignLength * boxWidth, (heightAllowed * heightOfBox) + extraWhiteSpace,
// heightAllowed, heightOfBox, extraWhiteSpace);
return QSize(alignLength * boxWidth, (heightAllowed * heightOfBox) + extraWhiteSpace);
}
/**
* This function will be called every time the alignment may have changed size.
*/
void HistogramWidget::updateSizeHint()
{
resize(alignLength * boxWidth, (heightAllowed * heightOfBox) + extraWhiteSpace);
update();
}
void HistogramWidget::addHistogramInfo(std::vector<int>* histogramValues)
{
arrayOfHeights = histogramValues;
alignLength = arrayOfHeights->size();
/* bug 143: we re-use these scores for the column quality, i.e.
don't reduce to 99 HERE
99 seems to be needed, otherwise histogram display is messed up
// Check each element is <= 100
for(int i = 0; i < alignLength; i++)
{
if((*arrayOfHeights)[i] >= 100)
{
(*arrayOfHeights)[i] = 99; // Make sure 99 is the maximum value.
}
}
*/
isSet = true;
updateSizeHint();
calculateHistogramBars();
}
void HistogramWidget::clearHistogram()
{
isSet = false;
arrayOfHeights = 0;
alignLength = 0;
updateSizeHint();
}
/**
* This function is an event handler. It is called automatically when the widget
* needs to be repainted.
* It draws the histogram and the ruler from the appropriate start and end positions.
* @param *event A paint event object.
*/
void HistogramWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect rect = event->rect();
int cx = rect.x();
int cy = rect.y();
int cw = rect.width();
int ch = rect.height();
//(void) printf("HistogramWidget::paintEvent(%d,%d, %d,%d)\n", cx,cy, cw,ch);
drawContents(&painter, cx, cy, cw, ch);
}
void HistogramWidget::drawContents(QPainter *painter, int cx, int cy, int cw, int ch)
{
painter->fillRect(cx, cy, cw, ch, QBrush(Qt::blue));
painter->setBackgroundMode(Qt::OpaqueMode);
//painter->setBackground(QBrush(Qt::white));
// Andreas Wilm (UCD):ff this is too small especially on high
// resolutions. Calculate dynamically
rulerFont.setPixelSize(8);
painter->setFont(rulerFont);
QFontMetrics fontMetrics(rulerFont);
int offSetForRuler = fontMetrics.height() - 1;
// Set up the begin and end columns
int beginColumn = cx / boxWidth;
int endColumn = (cx+cw-1) / boxWidth;
int index = 0;
int rulerNum;
int xPos;
int yPos = 0;
//(void) printf("HistogramWidget::drawContents(x=%d,y=%d, w=%d,h=%d)(bw=%d,bh=%d) -> [bc=%d,ec=%d]\n",
// cx,cy, cw,ch, boxWidth,boxSize, beginColumn,endColumn);
if(isSet && arrayOfHeights)
{
//31-01-07,nige: the histogram vertical stripes must be painted first
//otherwise the current stripe may paint over the last ruler text.
for (int column = beginColumn; column <= endColumn; ++column)
{
if(column >= 0 && column < arrayOfHeights->size())
{
index = (*arrayOfHeights)[column];
// bug 143: dynamically set to 99 here (see above)
// otherwise histogram is messed up
if (index>=100)
index=99;
if(index >= 0 && index < histogramBars.size() &&
!histogramBars[index].isNull())
{
painter->drawPixmap((column) * boxWidth, 0, histogramBars[index]);
}
}
}
for (int column = beginColumn; column <= endColumn; ++column)
{
if(column >= 0 && column < arrayOfHeights->size())
{
rulerNum = column + 1;
if((rulerNum % 10) == 0)
{
painter->drawLine(column * boxWidth + (boxWidth / 2), offSetForRuler,
column * boxWidth + (boxWidth / 2),
offSetForRuler + lengthLargeBar);
xPos = column * boxWidth + (boxWidth / 2) -
(fontMetrics.width(QString::number(rulerNum))) / 2;
if(rulerNum >= 10000)
{
painter->drawText(xPos, yPos, 7 * boxWidth, boxSize,
Qt::TextSingleLine, QString::number(rulerNum));
}
else if(rulerNum >= 1000 )
{
painter->drawText(xPos, yPos, 6 * boxWidth, boxSize,
Qt::TextSingleLine, QString::number(rulerNum));
}
else if(rulerNum >= 100 )
{
painter->drawText(xPos, yPos, 5 * boxWidth, boxSize,
Qt::TextSingleLine, QString::number(rulerNum));
}
else if(rulerNum >= 10 )
{
painter->drawText(xPos, yPos, 3 * boxWidth, boxSize,
Qt::TextSingleLine, QString::number(rulerNum));
}
else
{
painter->drawText(xPos, yPos, boxWidth, boxSize,
Qt::TextSingleLine, QString::number(rulerNum));
}
}
else
{
if(rulerNum == 1)
{
painter->drawText((boxWidth / 2) -
(fontMetrics.width(QString::number(0))) / 2, yPos, boxWidth,
boxSize, Qt::TextSingleLine, QString::number(rulerNum));
}
painter->drawLine(column * boxWidth + (boxWidth / 2), offSetForRuler,
column * boxWidth + (boxWidth / 2),
offSetForRuler + lengthSmallBar);
}
}
}
}
}
void HistogramWidget::updateBoxSize(int _boxWidth, int _boxSize)
{
if (boxWidth != _boxWidth || boxSize != _boxSize)
{
boxWidth = _boxWidth;
boxSize = _boxSize;
resize(alignLength * boxWidth, (heightAllowed * heightOfBox) + extraWhiteSpace);
calculateHistogramBars();
update();
}
}
void HistogramWidget::calculateHistogramBars()
{
histogramBars.clear();
histogramBars.resize(100);
int heightOfBar;
for(int i = 0; i < histogramBars.size(); i++)
{
QPoint origin(0, height());
histogramBars[i] = QPixmap(boxWidth, height());
heightOfBar = (i * heightOfBox) / 3;
QPainter painter(&histogramBars[i]);
painter.fillRect(0, 0, boxWidth, height(), QBrush(Qt::white));
painter.translate(origin);
#if OS_MAC
painter.fillRect(0, 0, boxWidth, -heightOfBar, QBrush(Qt::gray));
#else
painter.fillRect(0, 0, boxWidth, -heightOfBar, QBrush(Qt::lightGray));
#endif
painter.fillRect(0, -height(), boxWidth, 11, QBrush(rulerColor->rgb()));
}
}
/* TEST
void HistogramWidget::mousePressEvent(QMouseEvent *event) //nige
{
if (event->button() == Qt::LeftButton)
{
QSize size = sizeHint();
printf("SIZEHINT: w=%d, h=%d\n", size.width(), size.height());
}
else
{
QWidget::mousePressEvent(event);
}
}
TEST end */
|