/
waterstone
/
TaskList_Demo
Обзор
Документация
Войти
/
waterstone
/
TaskList_Demo
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/tasklist_progress_cell_delegate.cpp
81 строка
2 KB
Peter Sakhno
TaskListProgressCellDelegate small code review
22 янв 2026, 11:58
22 янв 2026, 11:58
71e2563
Код
Авторство
О чём код?
#include "tasklist_progress_cell_delegate.h" #include <QPainter> #include <QGuiApplication> #include "tasklist_ranges.h" namespace { QRectF squared(const QRectF& rect) { if (rect.width() > rect.height()) { qreal diff = rect.width() - rect.height(); return rect.adjusted(diff / 2, 0, -diff / 2, 0); } else { qreal diff = rect.height() - rect.width(); return rect.adjusted(0, diff / 2, 0, -diff / 2); } } } QSize TaskListProgressCellDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { const int min_radius{ 3 * kPenWidth }; auto hint = QStyledItemDelegate::sizeHint(option, index); if (hint.width() < kMinSize.width()) hint.setWidth(kMinSize.width()); if (hint.height() < kMinSize.height()) hint.setHeight(kMinSize.height()); return hint; } void TaskListProgressCellDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyledItemDelegate::paint(painter, option, index); const auto var = index.data(Qt::UserRole); if (var.userType() != QMetaType::Int) return; int progress = var.toInt(); if (!ProgressRange::valid(progress) and !AnimationRange::valid(progress)) return; const auto adj = kPenWidth * 1.5; auto bounds = squared(option.rect.adjusted(adj, adj, -adj, -adj)); QPalette::ColorGroup group = QPalette::Inactive; QPalette::ColorRole role = QPalette::Text; int back_alpha = 20; static const int fore_alpha = 255; if (option.state & QStyle::State_Active and option.state & QStyle::State_Selected) { group = QPalette::Active; role = QPalette::HighlightedText; back_alpha = 50; } QColor color = QGuiApplication::palette().color(group, role); painter->setRenderHint(QPainter::Antialiasing); color.setAlpha(back_alpha); painter->setPen(QPen(color, kPenWidth, Qt::SolidLine, Qt::FlatCap)); painter->drawArc(bounds, 0, 360 * 16); color.setAlpha(fore_alpha); painter->setPen(QPen(color, kPenWidth, Qt::SolidLine, Qt::FlatCap)); if (ProgressRange::valid(progress)) { painter->drawArc(bounds, 90 * 16, -((progress * 360 / ProgressRange::max()) * 16 + 90)); } else if (AnimationRange::valid(progress)) { painter->drawArc(bounds, -(progress - AnimationRange::min() - 90) * 16, -45 * 16); } }