void ForecastReport::Delegate::initStyleOption(
QStyleOptionViewItem * option,
const QModelIndex & index) const
{
Super::initStyleOption(option, index);
if (index.column() != owner.table->currentIndex().column()
&& (option->state & QStyle::State_Selected)) {
fixupStyleOption(option, cache);
}
}
// предок ForecastReport::Delegate
void MyTableDelegate::fixupStyleOption(
QStyleOptionViewItem * option,
QBlendCache& cache)
{
QBrush& brush = option->backgroundBrush;
QColor cl = brush.color();
if (brush.style() == Qt::NoBrush) {
cl = AdvancedTableModel::BgColor::back(); // это QApplication::palette().base().color();
brush.setStyle(Qt::SolidPattern);
}
brush.setColor(cache(cl, Qt::black, 180));
option->state &= ~QStyle::State_Selected;
}
QColor QBlendCache::operator()(const QColor& aFg, const QColor& aBg, int aAlpha)
{
if (fg != aFg || bg != aBg || alpha != aAlpha) {
fg = aFg;
bg = aBg;
alpha = aAlpha;
result = qBlend(aFg, aBg, aAlpha);
}
return result;
}
namespace {
float fGamma = 2.2f;
float fInvGamma = 1.0f / 2.2f;
}
QColor qBlend(const QColor& aFg, const QColor& aBg, int aValue)
{
qreal rf, gf, bf, rb, gb, bb, a, ca;
aFg.getRgbF(&rf, &gf, &bf);
aBg.getRgbF(&rb, &gb, &bb);
a = static_cast<qreal>(aValue) / static_cast<qreal>(255);
ca = static_cast<qreal>(1) - a;
rf = pow(pow(rf, fGamma) * a + pow(rb, fGamma) * ca, fInvGamma);
gf = pow(pow(gf, fGamma) * a + pow(gb, fGamma) * ca, fInvGamma);
bf = pow(pow(bf, fGamma) * a + pow(bb, fGamma) * ca, fInvGamma);
return QColor::fromRgbF(rf, gf, bf);
}