liversedge
6th June 2010, 08:49
Hi,
I'm writing an app that needs to show a wavy-line under cell values that have been identified as anomalies. I got there in the end, and whilst it might not be the most elegant code, it works. I suffered lots of issues getting it to work and thought it might be useful to share here.
Basically, the item delegate's paint function colors the cell or puts a wavy line under the value or puts a little green triangle in the corner of the cell.
Hope someone finds it useful or can tell me how to improve it :-)
// anomalies are underlined in red, otherwise straight paintjob
void CellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
// use background shading to show values we have modified
// and can therefore revert to original
if (rideEditor->table->selectionModel()->isSelected(index) == false &&
rideEditor->isEdited(index.row(), index.column())) {
painter->fillRect(option.rect, QBrush(QColor(230,230,230)));
}
// found items in yellow
if (rideEditor->isFound(index.row(), index.column()) == true) {
painter->fillRect(option.rect, QBrush(QColor(255,255,0)));
}
if (rideEditor->isAnomaly(index.row(), index.column())) {
// wavy line is a pain!
QTextDocument *meh = new QTextDocument(QString(value));
QTextCharFormat wavy;
wavy.setUnderlineStyle(QTextCharFormat::WaveUnderl ine);
wavy.setUnderlineColor(Qt::red);
QTextCursor cur = meh->find(value);
cur.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
cur.selectionStart();
cur.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
cur.selectionEnd();
cur.setCharFormat(wavy);
// only red background if not selected
if (rideEditor->table->selectionModel()->isSelected(index) == false)
painter->fillRect(option.rect, QBrush(QColor(255,230,230)));
painter->save();
painter->translate(option.rect.x(), option.rect.y());
meh->drawContents(painter);
painter->restore();
delete meh;
} else {
// normal render
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, value);
drawFocus(painter, myOption, myOption.rect);
}
// warning triangle - for high precision numbers
if (rideEditor->isTooPrecise(index.row(), index.column())) {
QPolygon triangle(3);
triangle.putPoints(0, 3, option.rect.x(), option.rect.y(),
option.rect.x()+4, option.rect.y(),
option.rect.x(), option.rect.y()+4);
painter->setBrush(QBrush(QColor(Qt::darkGreen)));
painter->setPen(QPen(QColor(Qt::darkGreen)));
painter->drawPolygon(triangle);
}
}
I'm writing an app that needs to show a wavy-line under cell values that have been identified as anomalies. I got there in the end, and whilst it might not be the most elegant code, it works. I suffered lots of issues getting it to work and thought it might be useful to share here.
Basically, the item delegate's paint function colors the cell or puts a wavy line under the value or puts a little green triangle in the corner of the cell.
Hope someone finds it useful or can tell me how to improve it :-)
// anomalies are underlined in red, otherwise straight paintjob
void CellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
// use background shading to show values we have modified
// and can therefore revert to original
if (rideEditor->table->selectionModel()->isSelected(index) == false &&
rideEditor->isEdited(index.row(), index.column())) {
painter->fillRect(option.rect, QBrush(QColor(230,230,230)));
}
// found items in yellow
if (rideEditor->isFound(index.row(), index.column()) == true) {
painter->fillRect(option.rect, QBrush(QColor(255,255,0)));
}
if (rideEditor->isAnomaly(index.row(), index.column())) {
// wavy line is a pain!
QTextDocument *meh = new QTextDocument(QString(value));
QTextCharFormat wavy;
wavy.setUnderlineStyle(QTextCharFormat::WaveUnderl ine);
wavy.setUnderlineColor(Qt::red);
QTextCursor cur = meh->find(value);
cur.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
cur.selectionStart();
cur.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
cur.selectionEnd();
cur.setCharFormat(wavy);
// only red background if not selected
if (rideEditor->table->selectionModel()->isSelected(index) == false)
painter->fillRect(option.rect, QBrush(QColor(255,230,230)));
painter->save();
painter->translate(option.rect.x(), option.rect.y());
meh->drawContents(painter);
painter->restore();
delete meh;
} else {
// normal render
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, value);
drawFocus(painter, myOption, myOption.rect);
}
// warning triangle - for high precision numbers
if (rideEditor->isTooPrecise(index.row(), index.column())) {
QPolygon triangle(3);
triangle.putPoints(0, 3, option.rect.x(), option.rect.y(),
option.rect.x()+4, option.rect.y(),
option.rect.x(), option.rect.y()+4);
painter->setBrush(QBrush(QColor(Qt::darkGreen)));
painter->setPen(QPen(QColor(Qt::darkGreen)));
painter->drawPolygon(triangle);
}
}