Warning, file /education/kwordquiz/src/kwqtabledelegate.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2006-2010 Peter Hedlund <peter.hedlund@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kwqtabledelegate.h"
0007 
0008 #include <QLineEdit>
0009 #include <QPainter>
0010 
0011 #include "kwqtablemodel.h"
0012 
0013 
0014 KWQTableDelegate::KWQTableDelegate(QObject * parent) : QItemDelegate(parent)
0015 {
0016 }
0017 
0018 QWidget * KWQTableDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
0019 {
0020   Q_UNUSED(option);
0021   QLineEdit *editor = new QLineEdit(parent);
0022   editor->setFrame(false);
0023   editor->setFont(index.data(Qt::FontRole).value<QFont>());
0024 
0025   //connect(editor, SIGNAL(returnPressed()), this, SLOT(commitAndCloseEditor()));
0026   return editor;
0027 }
0028 
0029 void KWQTableDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
0030 {
0031   QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
0032   lineEdit->setText(index.data(Qt::DisplayRole).toString());
0033 }
0034 
0035 void KWQTableDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
0036 {
0037   QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
0038   model->setData(index, lineEdit->text());
0039 }
0040 
0041 void KWQTableDelegate::updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const
0042 {
0043   Q_UNUSED(index);
0044   editor->setGeometry(option.rect);
0045 }
0046 
0047 void KWQTableDelegate::commitAndCloseEditor()
0048 {
0049   QWidget *editor = qobject_cast<QWidget *>(sender());
0050 
0051   Q_EMIT commitData(editor);
0052   Q_EMIT closeEditor(editor, QAbstractItemDelegate::NoHint);
0053 }
0054 
0055 void KWQTableDelegate::drawDecoration(QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect, const QPixmap& pixmap) const
0056 {
0057   if (pixmap.isNull() || !rect.isValid())
0058     return;
0059   QPoint p = QStyle::alignedRect(option.direction, option.decorationAlignment, pixmap.size(), rect).topLeft();
0060   QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
0061   if (option.state & QStyle::State_Selected) {
0062     painter->fillRect(rect, option.palette.brush(cg, option.state & QStyle::State_HasFocus ?
0063           QPalette::Base : QPalette::Highlight));
0064     const QPixmap pm = selectedPixmap(pixmap, option.palette, option.state & QStyle::State_Enabled);
0065     painter->drawPixmap(p, pm);
0066   } else {
0067     painter->drawPixmap(p, pixmap);
0068   }
0069 }
0070 
0071 
0072 void KWQTableDelegate::drawDisplay(QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text) const
0073 {
0074     QPen pen = painter->pen();
0075     QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
0076     if (option.state & QStyle::State_Selected) {
0077         painter->fillRect(rect, option.palette.brush(cg, option.state & QStyle::State_HasFocus ?
0078           QPalette::Base : QPalette::Highlight));
0079         painter->setPen(option.palette.color(cg, option.state & QStyle::State_HasFocus ?
0080           QPalette::Text : QPalette::HighlightedText));
0081     } else {
0082         painter->setPen(option.palette.color(cg, QPalette::Text));
0083     }
0084 
0085     if (option.state & QStyle::State_Editing) {
0086         painter->save();
0087         painter->setPen(option.palette.color(cg, QPalette::Text));
0088         painter->drawRect(rect.adjusted(0, 0, -1, -1));
0089         painter->restore();
0090     }
0091 
0092     QFont font = painter->font();
0093     painter->setFont(option.font);
0094     QRect textRect = rect.adjusted(3, 0, -3, 0); // remove width padding
0095     painter->drawText(textRect, option.displayAlignment | Qt::TextWordWrap, text);
0096     painter->setFont(font);
0097     painter->setPen(pen);
0098 }
0099 
0100 
0101 void KWQTableDelegate::drawFocus(QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect) const
0102 {
0103   if (option.state & QStyle::State_HasFocus) {
0104     painter->save();
0105     QPen pen = painter->pen();
0106     pen.setColor(Qt::black);
0107     pen.setWidth(0);
0108     painter->setBrush(Qt::NoBrush);
0109     painter->drawRect(rect.adjusted(0, 0, -1, -1));
0110     painter->drawRect(rect.adjusted(1, 1, -2, -2));
0111     painter->restore();
0112   }
0113 }
0114 
0115 
0116 void KWQTableDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
0117 {
0118   painter->save();
0119   drawBackground(painter, option, index);
0120 
0121   QStyleOptionViewItem opt = option;
0122   opt.decorationSize = QSize(0, 0);
0123   QPixmap decorationPixmap;
0124   QVariant decorationVariant = index.data(Qt::DecorationRole);
0125   if (!decorationVariant.isNull()) {
0126     decorationPixmap = decoration(option, decorationVariant);
0127     opt.decorationPosition = QStyleOptionViewItem::Right;
0128     opt.decorationAlignment = Qt::AlignRight | Qt::AlignVCenter;
0129     opt.decorationSize = QSize(decorationPixmap.width(), decorationPixmap.height());
0130     drawDecoration(painter, opt, opt.rect, decorationPixmap);
0131   }
0132 
0133   opt.rect.adjust(0, 0, -opt.decorationSize.width(), 0);
0134   const KWQTableModel *model = static_cast<const KWQTableModel *>(index.model());
0135 
0136   if (!model->checkBlanksSyntax(index.data(Qt::DisplayRole).toString())) {
0137     QPalette::ColorGroup cg = QPalette::Normal;
0138     opt.palette.setColor(cg, QPalette::Text, Qt::red);
0139   }
0140   opt.font = index.data(Qt::FontRole).value<QFont>();
0141   drawDisplay(painter, opt, opt.rect, index.data(Qt::DisplayRole).toString());
0142   if (!index.data(KWQTableModel::SoundRole).isNull())
0143     painter->fillRect(option.rect.right() - 3, option.rect.top(), 4, 4, Qt::red);
0144   drawFocus(painter, opt, option.rect);
0145 
0146   painter->restore();
0147 }