void IconsetDelegate::drawFocusRect(QPainter *APainter, const QStyleOptionViewItemV4 &AIndexOption, const QRect &ARect) const
{
	if ((AIndexOption.state & QStyle::State_HasFocus) > 0)
	{
		QStyle *style = AIndexOption.widget ? AIndexOption.widget->style() : QApplication::style();

		QStyleOptionFocusRect focusOption;
		focusOption.QStyleOption::operator=(AIndexOption);
		focusOption.rect = ARect;
		focusOption.state |= QStyle::State_KeyboardFocusChange|QStyle::State_Item;

		QPalette::ColorGroup cg = (AIndexOption.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled;
		QPalette::ColorRole cr = (AIndexOption.state & QStyle::State_Selected) ? QPalette::Highlight : QPalette::Window;
		focusOption.backgroundColor = AIndexOption.palette.color(cg,cr);

		style->proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOption, APainter);
	}
}
示例#2
0
void ProcessItemDelegate::paint(QPainter * painter,
  const QStyleOptionViewItem &option, const QModelIndex & index) const
{
  QStyleOptionViewItemV4 opt = option;
  initStyleOption(&opt, index);

  painter->save();
  painter->setClipRect(opt.rect);

  // Draw the background.
  const QWidget * widget = opt.widget;
  QStyle * style = widget ? widget->style() : QApplication::style();
  style->proxy()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, widget);

  QRect iconRect = style->subElementRect(QStyle::SE_ItemViewItemDecoration, &opt,
    widget);
  QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, widget);

  // Draw the icon.
  QIcon::Mode mode = QIcon::Normal;
  if (!(opt.state & QStyle::State_Enabled))
  {
    mode = QIcon::Disabled;
  }
  else if (opt.state & QStyle::State_Selected)
  {
    mode = QIcon::Selected;
  }
  QIcon::State state = opt.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
  opt.icon.paint(painter, iconRect, opt.decorationAlignment, mode, state);

  // Draw the text.
  QTextDocument doc;
  doc.setHtml(opt.text);
  doc.setDocumentMargin(2);
  painter->translate(textRect.topLeft());
  doc.drawContents(painter);

  painter->restore();
}
示例#3
0
// Based on QCommonStyle::drawControl case CE_ItemViewItem
void FilesDelegate::paint( QPainter *p, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
// 	QStyledItemDelegate::paint(p, option, index);
// 	return;

	Q_ASSERT(index.isValid());

	QStyleOptionViewItemV4 opt = option;
	initStyleOption(&opt, index);
	const QWidget *widget = opt.widget;
	const QTreeView *view = qobject_cast<const QTreeView *>(widget);
        QStyle *style = widget ? widget->style() : qApp->style();
// 	style->drawControl(QStyle::CE_ItemViewItem, &opt, p, widget);
// 	return;

#if defined(Q_OS_WIN32)
	// Based on QWindowsVistaStyle::drawControl case CE_ItemViewItem
	if (FilesDelegate_win_useVista(style))
	{
		QPalette palette = opt.palette;
		palette.setColor(QPalette::All, QPalette::HighlightedText, palette.color(QPalette::Active, QPalette::Text));
		// Note that setting a saturated color here results in ugly XOR colors in the focus rect
		palette.setColor(QPalette::All, QPalette::Highlight, palette.base().color().darker(108));

		opt.palette = palette;
		// We hide the focusrect in singleselection as it is not required
		if ((view->selectionMode() == QAbstractItemView::SingleSelection) && !(opt.state & QStyle::State_KeyboardFocusChange))
			opt.state &= ~QStyle::State_HasFocus;
	}
#endif

	// Custom padding for first and last columns; Also fixing viewItemPosition for swapped columns
	if (!view->header()->logicalIndex(index.column()))
	{
		opt.rect.setLeft(opt.rect.left() + VIEWPORT_MARGIN_LEFT);
		opt.viewItemPosition = QStyleOptionViewItemV4::Beginning;
	}
	if (view->header()->logicalIndex(index.column()) == index.model()->columnCount() - 1)
	{
		opt.rect.setRight(opt.rect.right() - VIEWPORT_MARGIN_RIGHT);
		opt.viewItemPosition = QStyleOptionViewItemV4::End;
	}


	p->save();
	p->setClipRect(opt.rect);

	QRect checkRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &opt, widget);
	QRect iconRect = style->subElementRect(QStyle::SE_ItemViewItemDecoration, &opt, widget);
	QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, widget);

	// Initially selected elements have State_Selected state, but it's not what we want, so we remove it and set background color
	if (opt.state & QStyle::State_Selected)
	{
		opt.state &= ~(QStyle::State_Selected);
		opt.backgroundBrush = QBrush(QColor(255, 215, 188)); // Soft orange-pink color for selection
	}

	// State_Selected state is used for displaying cursor row only
	if (currentRow == index.data(FileListModel::IndexRowRole).toInt())
		opt.state |= QStyle::State_Selected;

	// draw the background
	style->proxy()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, p, widget);

	// draw the check mark
	if (opt.features & QStyleOptionViewItemV2::HasCheckIndicator)
	{
		QStyleOptionViewItemV4 option(opt);
		option.rect = checkRect;
		option.state = option.state & ~QStyle::State_HasFocus;

		switch (opt.checkState)
		{
		case Qt::Unchecked:
			option.state |= QStyle::State_Off;
			break;
		case Qt::PartiallyChecked:
			option.state |= QStyle::State_NoChange;
			break;
		case Qt::Checked:
			option.state |= QStyle::State_On;
			break;
		}
		style->proxy()->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &option, p, widget);
	}

	// draw the icon
	QIcon::Mode mode = QIcon::Normal;
	if (!(opt.state & QStyle::State_Enabled))
		mode = QIcon::Disabled;
	else if (opt.state & QStyle::State_Selected)
		mode = QIcon::Selected;
	QIcon::State state = opt.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
	opt.icon.paint(p, iconRect, opt.decorationAlignment, mode, state);

	// draw the text
	if (!opt.text.isEmpty())
	{
		QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
		if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
			cg = QPalette::Inactive;

		if (opt.state & QStyle::State_Selected)
			p->setPen(opt.palette.color(cg, QPalette::HighlightedText));
		else
			p->setPen(opt.palette.color(cg, QPalette::Text));

		if (opt.state & QStyle::State_Editing)
		{
			p->setPen(opt.palette.color(cg, QPalette::Text));
			p->drawRect(textRect.adjusted(0, 0, -1, -1));
		}

		QCommonStylePrivate_viewItemDrawText(style, p, &opt, textRect, !(index.data(FileListModel::AttributesRole).toUInt() & FileInfo::Directory));
	}

	// draw the focus rect
// 	if (opt.state & QStyle::State_HasFocus)
// 	{
// 		QStyleOptionFocusRect o;
// 		o.QStyleOption::operator=(opt);
// 		o.rect = style->subElementRect(QStyle::SE_ItemViewItemFocusRect, &opt, widget);
// 		o.state |= QStyle::State_KeyboardFocusChange;
// 		o.state |= QStyle::State_Item;
// 		QPalette::ColorGroup cg = (opt.state & QStyle::State_Enabled)	? QPalette::Normal : QPalette::Disabled;
// 		o.backgroundColor = opt.palette.color(cg, (opt.state & QStyle::State_Selected) ? QPalette::Highlight : QPalette::Window);
// 		style->proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, p, widget);
// 	}

	p->restore();
}
void IconsetDelegate::drawBackground(QPainter *APainter, const QStyleOptionViewItemV4 &AIndexOption) const
{
	QStyle *style = AIndexOption.widget ? AIndexOption.widget->style() : QApplication::style();
	style->proxy()->drawPrimitive(QStyle::PE_PanelItemViewItem,&AIndexOption,APainter,AIndexOption.widget);
}
示例#5
0
void LedgerDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);

    // never change the background of the cell the mouse is hovering over
    opt.state &= ~QStyle::State_MouseOver;

    // show the focus only on the detail column
    opt.state &= ~QStyle::State_HasFocus;
    if(index.column() == LedgerModel::DetailColumn) {
        QAbstractItemView* view = qobject_cast< QAbstractItemView* >(parent());
        if(view) {
            if(view->currentIndex().row() == index.row()) {
                opt.state |= QStyle::State_HasFocus;
            }
        }
    }

    painter->save();

    // Background
    QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);

    // Do not paint text if the edit widget is shown
    const LedgerView *view = qobject_cast<const LedgerView *>(opt.widget);
    if (view && view->indexWidget(index)) {
        painter->restore();
        return;
    }

    const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
    const QRect textArea = QRect(opt.rect.x() + margin, opt.rect.y() + margin, opt.rect.width() - 2 * margin, opt.rect.height() - 2 * margin);

    QStringList lines;
    if(index.column() == LedgerModel::DetailColumn) {
        lines << index.model()->data(index, LedgerModel::PayeeNameRole).toString();
        lines << index.model()->data(index, LedgerModel::CounterAccountRole).toString();
        lines << index.model()->data(index, LedgerModel::SingleLineMemoRole).toString();
        lines.removeAll(QString());
    }

    const bool erroneous = index.model()->data(index, LedgerModel::ErroneousRole).toBool();
    const bool selected = opt.state & QStyle::State_Selected;

    // draw the text items
    if(!opt.text.isEmpty() || !lines.isEmpty()) {

        // check if it is a scheduled transaction and display it as inactive
        if(!index.model()->data(index, LedgerModel::ScheduleIdRole).toString().isEmpty()) {
            opt.state &= ~QStyle::State_Enabled;
        }

        QPalette::ColorGroup cg = (opt.state & QStyle::State_Enabled)
                                  ? QPalette::Normal : QPalette::Disabled;

        if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) {
            cg = QPalette::Inactive;
        }
        if (opt.state & QStyle::State_Selected) {
            painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
        } else {
            painter->setPen(opt.palette.color(cg, QPalette::Text));
        }
        if (opt.state & QStyle::State_Editing) {
            painter->setPen(opt.palette.color(cg, QPalette::Text));
            painter->drawRect(textArea.adjusted(0, 0, -1, -1));
        }

        // Don't play with the color if it's selected
        // otherwise switch the color if the transaction has errors
        if(erroneous && !selected) {
            painter->setPen(m_erroneousColor);
        }

        // collect data for the various colums
        if(index.column() == LedgerModel::DetailColumn) {
            for(int i = 0; i < lines.count(); ++i) {
                painter->drawText(textArea.adjusted(0, (opt.fontMetrics.lineSpacing() + 5) * i, 0, 0), opt.displayAlignment, lines[i]);
            }

        } else {
            painter->drawText(textArea, opt.displayAlignment, opt.text);
        }
    }

    // draw the focus rect
    if(opt.state & QStyle::State_HasFocus) {
        QStyleOptionFocusRect o;
        o.QStyleOption::operator=(opt);
        o.rect = style->proxy()->subElementRect(QStyle::SE_ItemViewItemFocusRect, &opt, opt.widget);
        o.state |= QStyle::State_KeyboardFocusChange;
        o.state |= QStyle::State_Item;

        QPalette::ColorGroup cg = (opt.state & QStyle::State_Enabled)
                                  ? QPalette::Normal : QPalette::Disabled;
        o.backgroundColor = opt.palette.color(cg, (opt.state & QStyle::State_Selected)
                                              ? QPalette::Highlight : QPalette::Window);
        style->proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, opt.widget);
    }

    if((index.column() == LedgerModel::DetailColumn)
            && erroneous) {
        QPixmap attention;
        attention.loadFromData(attentionSign, sizeof(attentionSign), 0, 0);
        style->proxy()->drawItemPixmap(painter, option.rect, Qt::AlignRight | Qt::AlignTop, attention);
    }

    painter->restore();
#if 0
    const QHeaderView* horizontalHeader = view->horizontalHeader();
    const QHeaderView* verticalHeader = view->verticalHeader();
    const QWidget* viewport = view->viewport();
    const bool showGrid = view->showGrid() && !view->indexWidget(index);
    const int gridSize = showGrid ? 1 : 0;
    const int gridHint = style->styleHint(QStyle::SH_Table_GridLineColor, &option, view);
    const QColor gridColor = static_cast<QRgb>(gridHint);
    const QPen gridPen = QPen(gridColor, 0, view->gridStyle());
    const bool rightToLeft = view->isRightToLeft();
    const int viewportOffset = horizontalHeader->offset();


    // QStyledItemDelegate::paint(painter, opt, index);

    if(!horizontalHeader->isSectionHidden(LedgerModel::DateColumn)) {
        QDate postDate = index.data(LedgerModel::PostDateRole).toDate();
        if(postDate.isValid()) {
            int ofs = horizontalHeader->sectionViewportPosition(LedgerModel::DateColumn) + viewportOffset;
            QRect oRect = opt.rect;
            opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
            opt.rect.setLeft(opt.rect.left()+ofs);
            opt.rect.setTop(opt.rect.top()+margin);
            opt.rect.setWidth(horizontalHeader->sectionSize(LedgerModel::DateColumn));
            opt.text = KGlobal::locale()->formatDate(postDate, QLocale::ShortFormat);
            style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
            opt.rect = oRect;
        }
    }

    if(!horizontalHeader->isSectionHidden(LedgerModel::DetailColumn)) {
        QString payee = index.data(LedgerModel::PayeeRole).toString();
        QString counterAccount = index.data(LedgerModel::CounterAccountRole).toString();
        QString txt = payee;
        if(payee.length() > 0)
            txt += '\n';
        txt += counterAccount;
        int ofs = horizontalHeader->sectionViewportPosition(LedgerModel::DetailColumn) + viewportOffset;
        QRect oRect = opt.rect;
        opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
        opt.rect.setLeft(opt.rect.left()+ofs);
        opt.rect.setTop(opt.rect.top()+margin);
        opt.rect.setWidth(horizontalHeader->sectionSize(LedgerModel::DetailColumn));
        opt.text = txt;
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
        opt.rect = oRect;

    }
#if 0
    opt.features |= QStyleOptionViewItemV2::HasDisplay;
    QString txt = QString("%1").arg(index.isValid() ? "true" : "false");
    if(index.isValid())
        txt += QString(" %1 - %2").arg(index.row()).arg(view->verticalHeader()->sectionViewportPosition(index.row()));
    opt.text = displayText(txt, opt.locale);

    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
#endif

    // paint grid
    if(showGrid) {
        painter->save();
        QPen old = painter->pen();
        painter->setPen(gridPen);

        // qDebug() << "Paint grid for" << index.row() << "in" << opt.rect;
        for(int i=0; i < horizontalHeader->count(); ++i) {
            if(!horizontalHeader->isSectionHidden(i)) {
                int ofs = horizontalHeader->sectionViewportPosition(i) + viewportOffset;
                if(!rightToLeft) {
                    ofs += horizontalHeader->sectionSize(i) - gridSize;
                }
                if(ofs-viewportOffset < viewport->width()) {
                    // I have no idea, why I need to paint the grid for the selected row and the one below
                    // but it was the only way to get this working correctly. Otherwise the grid was missing
                    // while moving the mouse over the view from bottom to top.
                    painter->drawLine(opt.rect.x()+ofs, opt.rect.y(), opt.rect.x()+ofs, opt.rect.height());
                    painter->drawLine(opt.rect.x()+ofs, opt.rect.y()+verticalHeader->sectionSize(index.row()), opt.rect.x()+ofs, opt.rect.height());
                }
            }
        }
        painter->setPen(old);
        painter->restore();
    }
#endif
}