Improve conversion of bytes to human readable string
Fixes issues like showing 0 MiB when the value is just slightly less then 1 MiB.
This commit is contained in:
parent
db2594a87c
commit
5bdd3fa95d
7 changed files with 125 additions and 44 deletions
|
|
@ -5,6 +5,7 @@
|
|||
#include "ResultTableModelUtil.h"
|
||||
#include "CustomDataRole.h"
|
||||
#include "AbstractEditorFactory.h"
|
||||
#include "utils/HumanReadableBytes.h"
|
||||
|
||||
PgLabItemDelegate::PgLabItemDelegate(QObject *parent)
|
||||
: QStyledItemDelegate(parent)
|
||||
|
|
@ -111,37 +112,11 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
|
|||
else {
|
||||
forground_color = GetDefaultColorForType(oid);
|
||||
if (meaning == DataMeaning::Bytes) {
|
||||
QString suffix;
|
||||
auto s = value.toLongLong();
|
||||
double val;
|
||||
if (s > 1024 * 1024 * 1000) {
|
||||
val = s / (1024 * 1024 * 1024);
|
||||
suffix = "GiB";
|
||||
forground_color = QColorConstants::Svg::darkorange;
|
||||
option->font.setBold(true);
|
||||
}
|
||||
else if (s > 1024 * 1000) {
|
||||
val = s / (1024 * 1024);
|
||||
suffix = "MiB";
|
||||
forground_color = QColorConstants::Svg::darkgoldenrod;
|
||||
}
|
||||
else if (s > 1000) {
|
||||
val = s / 1024;
|
||||
suffix = "KiB";
|
||||
forground_color = QColorConstants::Svg::darkgreen;
|
||||
}
|
||||
else {
|
||||
val = s;
|
||||
suffix = "B";
|
||||
forground_color = QColorConstants::Svg::darkblue;
|
||||
}
|
||||
option->text = QString{ "%1 %2" }.arg(val, 0, 'g', 3).arg(suffix) ;
|
||||
option->text = HandleBytes(value.toLongLong(), forground_color);
|
||||
}
|
||||
else {
|
||||
auto str = value.toString();
|
||||
auto s = str.left(100);
|
||||
// auto f = s.indexOf('\n');
|
||||
// option->text = ((f > 0) ? s.left(f) : s).toString();
|
||||
option->text = s;
|
||||
}
|
||||
}
|
||||
|
|
@ -161,30 +136,48 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
|
|||
option->styleObject = nullptr;
|
||||
}
|
||||
|
||||
void PgLabItemDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
QString PgLabItemDelegate::HandleBytes(qlonglong s, QColor &forground_color) const
|
||||
{
|
||||
Q_ASSERT(index.isValid());
|
||||
auto str = HumanReadableBytes(s);
|
||||
if (s > 1024 * 1024 * 1024) {
|
||||
forground_color = QColorConstants::Svg::darkorange;
|
||||
}
|
||||
else if (s > 1024 * 1024) {
|
||||
forground_color = QColorConstants::Svg::darkgoldenrod;
|
||||
}
|
||||
else if (s > 1024) {
|
||||
forground_color = QColorConstants::Svg::darkgreen;
|
||||
}
|
||||
else {
|
||||
forground_color = QColorConstants::Svg::darkblue;
|
||||
}
|
||||
return QString::fromStdString(str);
|
||||
}
|
||||
|
||||
QStyleOptionViewItem opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
void PgLabItemDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
Q_ASSERT(index.isValid());
|
||||
|
||||
const QWidget *widget = option.widget; // QStyledItemDelegatePrivate::widget(option);
|
||||
QStyle *style = widget ? widget->style() : QApplication::style();
|
||||
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
|
||||
QStyleOptionViewItem opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
|
||||
const QWidget *widget = option.widget; // QStyledItemDelegatePrivate::widget(option);
|
||||
QStyle *style = widget ? widget->style() : QApplication::style();
|
||||
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
|
||||
}
|
||||
|
||||
QWidget *PgLabItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
if (m_editorFactory)
|
||||
return m_editorFactory->createEditor(parent, option, index);
|
||||
return QStyledItemDelegate::createEditor(parent, option, index);
|
||||
if (m_editorFactory)
|
||||
return m_editorFactory->createEditor(parent, option, index);
|
||||
return QStyledItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
|
||||
void PgLabItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||
{
|
||||
if (m_editorFactory)
|
||||
m_editorFactory->setEditorData(editor, index);
|
||||
if (m_editorFactory)
|
||||
m_editorFactory->setEditorData(editor, index);
|
||||
else
|
||||
QStyledItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue