void UberDelegate::paint_labor(const QRect &adjusted, QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &proxy_idx) const {
    QModelIndex idx = m_proxy->mapToSource(proxy_idx);
    short rating = idx.data(DwarfModel::DR_RATING).toInt();

    Dwarf *d = m_model->get_dwarf_by_id(idx.data(DwarfModel::DR_ID).toInt());
    if (!d) {
        return QStyledItemDelegate::paint(p, opt, idx);
    }

    int labor_id = idx.data(DwarfModel::DR_LABOR_ID).toInt();
    bool enabled = d->labor_enabled(labor_id);
    bool dirty = d->is_labor_state_dirty(labor_id);

    QColor bg = paint_bg(adjusted, enabled, p, opt, proxy_idx);
    paint_skill(adjusted, rating, bg, p, opt, proxy_idx);
    paint_grid(adjusted, dirty, p, opt, proxy_idx);
}
void UberDelegate::paint_aggregate(const QRect &adjusted, QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &proxy_idx) const {
    if (!proxy_idx.isValid()) {
        return;
    }
    QModelIndex model_idx = m_proxy->mapToSource(proxy_idx);
    QModelIndex first_col = m_proxy->index(proxy_idx.row(), 0, proxy_idx.parent());
    if (!first_col.isValid()) {
        return;
    }

    QString group_name = proxy_idx.data(DwarfModel::DR_GROUP_NAME).toString();
    int labor_id = proxy_idx.data(DwarfModel::DR_LABOR_ID).toInt();

    int dirty_count = 0;
    int enabled_count = 0;
    for (int i = 0; i < m_proxy->rowCount(first_col); ++i) {
        int dwarf_id = m_proxy->data(m_proxy->index(i, 0, first_col), DwarfModel::DR_ID).toInt();
        Dwarf *d = m_model->get_dwarf_by_id(dwarf_id);
        if (!d)
            continue;
        if (d->labor_enabled(labor_id))
            enabled_count++;
        if (d->is_labor_state_dirty(labor_id))
            dirty_count++;
    }

    QStyledItemDelegate::paint(p, opt, proxy_idx); // slap on the main bg

    p->save();
    if (enabled_count == m_proxy->rowCount(first_col)) {
        p->fillRect(adjusted, QBrush(color_active_group));
    } else if (enabled_count > 0) {
        p->fillRect(adjusted, QBrush(color_partial_group));
    } else {
        p->fillRect(adjusted, QBrush(color_inactive_group));
    }
    p->restore();

    paint_grid(adjusted, dirty_count > 0, p, opt, proxy_idx);
}
void UberDelegate::paint_cell(QPainter *p, const QStyleOptionViewItem &opt, const QModelIndex &idx, const bool drawing_aggregate) const {
    QModelIndex model_idx = idx;
    if (m_proxy)
        model_idx = m_proxy->mapToSource(idx);

    COLUMN_TYPE type = static_cast<COLUMN_TYPE>(model_idx.data(DwarfModel::DR_COL_TYPE).toInt());
    QRect adjusted = opt.rect.adjusted(cell_padding, cell_padding, -cell_padding, -cell_padding);

    float rating = model_idx.data(DwarfModel::DR_RATING).toFloat();
    QString text_rating = model_idx.data(DwarfModel::DR_DISPLAY_RATING).toString();
    float limit = 100.0;

    switch (type) {
    case CT_SKILL:
    {
        QColor bg = paint_bg(adjusted, p, opt, idx);
        limit = 15.0;
        if(rating >= 0)
            paint_values(adjusted, rating, text_rating, bg, p, opt, idx, 0, 0, limit, 0, 0);

        paint_mood_cell(adjusted,p,opt,idx,model_idx.data(DwarfModel::DR_SKILL_ID).toInt(),false);
    }
        break;
    case CT_LABOR:
    {
        if (!drawing_aggregate) {
            Dwarf *d = m_model->get_dwarf_by_id(idx.data(DwarfModel::DR_ID).toInt());
            if (!d) {
                return QStyledItemDelegate::paint(p, opt, idx);
            }
            int labor_id = idx.data(DwarfModel::DR_LABOR_ID).toInt();
            bool enabled = d->labor_enabled(labor_id);
            bool dirty = d->is_labor_state_dirty(labor_id);

            QColor bg = paint_bg_active(adjusted, enabled, p, opt, idx);
            limit = 15.0;
            if(rating >= 0)
                paint_values(adjusted, rating, text_rating, bg, p, opt, idx, 0, 0, limit, 0, 0);

            paint_mood_cell(adjusted,p,opt,idx,GameDataReader::ptr()->get_labor(labor_id)->skill_id, dirty);
        }else {
            paint_labor_aggregate(adjusted, p, opt, idx);
        }
    }
        break;
    case CT_HAPPINESS:
    {
        paint_bg(adjusted, p, opt, idx, true, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        if(draw_happiness_icons){
            paint_icon(adjusted,p,opt,idx);
        }else{
            p->save();
            paint_grid(adjusted, false, p, opt, idx);
            p->restore();
        }
    }
        break;
    case CT_EQUIPMENT:
    {
        int wear_level = idx.data(DwarfModel::DR_SPECIAL_FLAG).toInt();
        paint_bg(adjusted, p, opt, idx, true, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        paint_wear_cell(adjusted,p,opt,idx,wear_level);
    }
        break;
    case CT_ITEMTYPE:
    {
        QColor bg = paint_bg(adjusted, p, opt, idx, true, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        //if we're drawing numbers, we only want to draw counts for squads
        //this is a special case because we're drawing different information if it's text mode
        if(m_skill_drawing_method == SDM_NUMERIC && rating == 100)
            rating = -1;
        paint_values(adjusted, rating, text_rating, bg, p, opt, idx,90.0f,5.0f,95.0f,99.99f,102.0f,false);
        int wear_level = idx.data(DwarfModel::DR_SPECIAL_FLAG).toInt();
        paint_wear_cell(adjusted,p,opt,idx,wear_level);
    }
        break;
    case CT_ROLE:
    {
        bool active_labors = false;
        int dirty_alpha = 255;
        int active_alpha = 255;
        bool dirty = false;
        Dwarf *d = m_model->get_dwarf_by_id(idx.data(DwarfModel::DR_ID).toInt());
        if(d){
            if(idx.data(DwarfModel::DR_SPECIAL_FLAG).canConvert<QVariantList>()){
                QVariantList labors = idx.data(DwarfModel::DR_SPECIAL_FLAG).toList();
                int active_count = 0;
                int dirty_count = 0;
                foreach(QVariant id, labors){
                    if(d->labor_enabled(id.toInt())){
                        active_labors = true;
                        active_count++;
                    }
                    if(d->is_labor_state_dirty(id.toInt())){
                        dirty = true;
                        dirty_count++;
                    }
                }
                if(active_labors)
                    active_alpha = (255 * ((float)active_count / labors.count()));
                if(dirty)
                    dirty_alpha = (255 * ((float)dirty_count / labors.count()));                                
            }
        }
        QColor bg;
        QColor color_active_adjusted = color_active_labor;
        if(active_labors){
            color_active_adjusted.setAlpha(active_alpha);
            bg = paint_bg_active(adjusted, active_labors, p, opt, idx, color_active_adjusted);
        }else{
            bg = paint_bg(adjusted, p, opt, idx);
        }
        double limit_range = (DwarfStats::get_role_max() - DwarfStats::get_role_min()) * 0.05;
        paint_values(adjusted, rating, text_rating, bg, p, opt, idx,50.0f, DwarfStats::get_role_min() + limit_range,DwarfStats::get_role_max() - limit_range,45.0f,55.0f);
        if(dirty){
            QColor color_dirty_adjusted = color_dirty_border;
            color_dirty_adjusted.setAlpha(dirty_alpha);
            paint_border(adjusted,p,color_dirty_adjusted);
            paint_grid(adjusted,false,p,opt,idx,false);
        }else{
            paint_grid(adjusted, dirty, p, opt, idx);
        }
    }
        break;
    case CT_IDLE:
    {
        paint_bg(adjusted, p, opt, idx, true, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        paint_icon(adjusted,p,opt,idx);
    }
        break;
    case CT_PROFESSION:
    {
        paint_bg(adjusted, p, opt, idx, true, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        paint_icon(adjusted,p,opt,idx);
    }
        break;
    case CT_HIGHEST_MOOD:
    {
        paint_bg(adjusted, p, opt, idx, true, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        paint_icon(adjusted,p,opt,idx);

        bool had_mood = idx.data(DwarfModel::DR_SPECIAL_FLAG).toBool();
        if(had_mood){
            p->save();
            QRect moodr = adjusted;
            moodr.adjust(2,2,-2,-2);
            p->setPen(QPen(Qt::darkRed,2));
            p->drawLine(moodr.bottomLeft(), moodr.topRight());
            p->restore();
        }
    }
        break;
    case CT_TRAIT:
    {
        QColor bg = paint_bg(adjusted, p, opt, idx);
        paint_values(adjusted, rating, text_rating, bg, p, opt, idx, 50, 10, 90);
        paint_grid(adjusted, false, p, opt, idx);
    }
        break;
    case CT_ATTRIBUTE:
    {
        QColor bg = paint_bg(adjusted, p, opt, idx);
        paint_values(adjusted, rating, text_rating, bg, p, opt, idx, 50.0f, 2.0f, 98.0f);

        if(color_attribute_syns && idx.data(DwarfModel::DR_SPECIAL_FLAG).toInt() > 0){
            paint_border(adjusted,p,Attribute::color_affected_by_syns());
            paint_grid(adjusted, false, p, opt, idx, false);
        }else{
            paint_grid(adjusted, false, p, opt, idx);
        }
    }
        break;
    case CT_WEAPON:
    {
        QColor bg = paint_bg(adjusted, p, opt, idx);
        paint_values(adjusted, rating, text_rating, bg, p, opt, idx, 50.0f, 1, 99, 49, 51, true);
        paint_grid(adjusted, false, p, opt, idx);

    }
        break;
    case CT_FLAGS:
    {
        paint_flags(adjusted, p, opt, idx);
    }
        break;
    case CT_TRAINED:
    {
        QColor bg = paint_bg(adjusted, p, opt, idx, false, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        //arbitrary ignore range is used just to hide tame animals
        paint_values(adjusted, rating, text_rating, bg, p, opt, idx, 50.0f, 1.0f, 95.0f, 49.9f, 50.1f, true);
        paint_grid(adjusted, false, p, opt, idx);
    }
        break;
    case CT_HEALTH:
    {
        QColor bg = paint_bg(adjusted, p, opt, idx, false, model_idx.data(Qt::BackgroundColorRole).value<QColor>());

        //draw the symbol text in bold
        p->save();
        if (rating != 0) {
            if(color_health_cells){
                p->setPen(model_idx.data(Qt::TextColorRole).value<QColor>());
            }else{
                if(auto_contrast){
                    p->setPen(compliment(bg));
                }else{
                    p->setPen(Qt::black);
                }
            }

            QFont tmp = m_fnt;
            tmp.setBold(true);
            p->setFont(tmp);
            p->drawText(opt.rect, Qt::AlignCenter, text_rating);
        }
        p->restore();

        paint_grid(adjusted, false, p, opt, idx);
    }
        break;
    case CT_SPACER:
    case CT_DEFAULT:
    default:
    {
        if(adjusted.width() > 0)
            paint_bg(adjusted, p, opt, idx, true, model_idx.data(Qt::BackgroundColorRole).value<QColor>());
        break;
    }
    }