Make it possible to disable PercentBarDelegate.

Add a check to PercentBarDelegate to see if the caller set text for this
item or did not set a valid double value. If either case is true, just
draw the item normally and return.

Change-Id: I028ee15d54f06f2cb16c6e5f1ef73c47b2886ccd
Reviewed-on: https://code.wireshark.org/review/10600
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Gerald Combs <gerald@wireshark.org>
This commit is contained in:
Gerald Combs 2015-09-21 10:32:40 -07:00
parent 460ae03ec3
commit 9a02bd0c39
2 changed files with 29 additions and 1 deletions

View File

@ -34,10 +34,17 @@ void PercentBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
{
QStyleOptionViewItemV4 optv4 = option;
QStyledItemDelegate::initStyleOption(&optv4, index);
double value = index.data(Qt::UserRole).toDouble();
QStyledItemDelegate::paint(painter, option, index);
bool ok = false;
double value = index.data(Qt::UserRole).toDouble(&ok);
if (!ok || !index.data(Qt::DisplayRole).toString().isEmpty()) {
// We don't have a valid value or the item has visible text.
return;
}
painter->save();
if (QApplication::style()->objectName().contains("vista")) {

View File

@ -22,6 +22,27 @@
#ifndef PERCENTBARDELEGATE_H
#define PERCENTBARDELEGATE_H
/*
* @file Percent bar delegate.
*
* QStyledItemDelegate subclass that will draw a percentage value and a
* single-item bar chart for the specified value.
*
* This is intended to be used in QTreeWidgets to show percentage values.
* To use it, first call setItemDelegate:
*
* myTreeWidget()->setItemDelegateForColumn(col_pct_, new PercentBarDelegate());
*
* Then, for each QTreeWidgetItem, set a double value using setData:
*
* setData(col_pct_, Qt::UserRole, QVariant::fromValue<double>(packets_ * 100.0 / num_packets));
*
* If the item data cannot be converted to a valid double value or if its
* text string is non-empty then it will be rendered normally (i.e. the
* percent text and bar will not be drawn). This lets you mix normal and
* percent bar rendering between rows.
*/
#include <QStyledItemDelegate>
class PercentBarDelegate : public QStyledItemDelegate