File indexing completed on 2024-05-12 16:41:07

0001 /********************************************************************************************
0002     begin                : Sunday Jun 27 2008
0003     copyright            : (C) 2008 by Mathias Soeken (msoeken@informatik.uni-bremen.de)
0004     copyright            : (C) 2005-2006 by Holger Danielsson (holger.danielsson@t-online.de)
0005  ********************************************************************************************/
0006 
0007 /***************************************************************************
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or modify  *
0010  *   it under the terms of the GNU General Public License as published by  *
0011  *   the Free Software Foundation; either version 2 of the License, or     *
0012  *   (at your option) any later version.                                   *
0013  *                                                                         *
0014  ***************************************************************************/
0015 
0016 #include "tabularcelldelegate.h"
0017 
0018 #include <QApplication>
0019 #include <QLineEdit>
0020 #include <QPainter>
0021 #include <QStyleOptionViewItem>
0022 
0023 #include "tabularcell.h"
0024 
0025 namespace KileDialog {
0026 
0027 TabularCellDelegate::TabularCellDelegate(QTableWidget *parent)
0028     : QStyledItemDelegate(parent),
0029       m_Table(parent)
0030 {
0031 }
0032 
0033 void TabularCellDelegate::paint(QPainter *painter,
0034                                 const QStyleOptionViewItem &option,
0035                                 const QModelIndex &index) const
0036 {
0037     if(option.state & QStyle::State_Selected || option.state & QStyle::State_MouseOver) {
0038         QStyledItemDelegate::paint(painter, option, index);
0039     } else {
0040         painter->fillRect(option.rect, qvariant_cast<QBrush>(index.model()->data(index, Qt::BackgroundRole)));
0041         QFont oldFont = painter->font();
0042         painter->setFont(qvariant_cast<QFont>(index.model()->data(index, Qt::FontRole)));
0043         QRect textRect(option.rect.x() + 3, option.rect.y(), option.rect.width() - 6, option.rect.height());
0044         QApplication::style()->drawItemText(painter, textRect,
0045                                             index.model()->data(index, Qt::TextAlignmentRole).toInt(),
0046                                             QPalette(qvariant_cast<QBrush>(index.model()->data(index, Qt::ForegroundRole)).color()),
0047                                             true, index.model()->data(index, Qt::DisplayRole).toString(), QPalette::Window );
0048         painter->setFont(oldFont);
0049     }
0050 
0051     int rowCount = m_Table->rowCount();
0052     int columnCount = m_Table->columnCount();
0053 
0054     int row = index.row();
0055     int column = index.column();
0056 
0057     TabularCell *cell = static_cast<TabularCell*>(m_Table->item(row, column));
0058 
0059     if(column == 0) {
0060         painter->setPen(cell->border() & TabularCell::Left ? Qt::black : Qt::lightGray);
0061         painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
0062     }
0063 
0064     if(row == 0) {
0065         painter->setPen(cell->border() & TabularCell::Top ? Qt::black : Qt::lightGray);
0066         painter->drawLine(option.rect.topLeft(), option.rect.topRight());
0067     }
0068 
0069     bool right = (cell->border() & TabularCell::Right)
0070                  || (column < (columnCount - 1) && static_cast<TabularCell*>(m_Table->item(row, column + 1))->border() & TabularCell::Left);
0071     painter->setPen(right ? Qt::black : Qt::lightGray);
0072     painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
0073 
0074     bool bottom = (cell->border() & TabularCell::Bottom)
0075                   || (row < (rowCount - 1) && static_cast<TabularCell*>(m_Table->item(row + 1, column))->border() & TabularCell::Top);
0076     painter->setPen(bottom ? Qt::black : Qt::lightGray);
0077     painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
0078 }
0079 
0080 QWidget* TabularCellDelegate::createEditor(QWidget *parent,
0081         const QStyleOptionViewItem &option,
0082         const QModelIndex &index) const
0083 {
0084     Q_UNUSED(option);
0085     Q_UNUSED(index);
0086 
0087     QLineEdit *editor = new QLineEdit(parent);
0088     editor->setFrame(false);
0089     return editor;
0090 }
0091 
0092 void TabularCellDelegate::setEditorData(QWidget *editor,
0093                                         const QModelIndex &index) const
0094 {
0095     QString value = index.model()->data(index, Qt::EditRole).toString();
0096     QBrush bgBrush = qvariant_cast<QBrush>(index.model()->data(index, Qt::BackgroundRole));
0097     QBrush fgBrush = qvariant_cast<QBrush>(index.model()->data(index, Qt::ForegroundRole));
0098     QFont font = qvariant_cast<QFont>(index.model()->data(index, Qt::FontRole));
0099     int alignment = index.model()->data(index, Qt::TextAlignmentRole).toInt();
0100     QLineEdit *edit = static_cast<QLineEdit*>(editor);
0101     QString styleSheet;
0102     if(bgBrush.style() != Qt::NoBrush) {
0103         styleSheet += "background-color:" + bgBrush.color().name() + ';';
0104     }
0105     if(fgBrush.style() != Qt::NoBrush) {
0106         styleSheet += "color:" + fgBrush.color().name() + ';';
0107     }
0108     edit->setStyleSheet(styleSheet);
0109     edit->setAlignment((Qt::Alignment)alignment);
0110     edit->setFont(font);
0111     edit->setText(value);
0112 }
0113 
0114 void TabularCellDelegate::setModelData(QWidget *editor,
0115                                        QAbstractItemModel *model,
0116                                        const QModelIndex &index) const
0117 {
0118     QLineEdit *edit = static_cast<QLineEdit*>(editor);
0119     QString value = edit->text();
0120     model->setData(index, value, Qt::EditRole);
0121 }
0122 
0123 void TabularCellDelegate::updateEditorGeometry(QWidget *editor,
0124         const QStyleOptionViewItem &option,
0125         const QModelIndex &index) const
0126 {
0127     Q_UNUSED(index);
0128 
0129     editor->setGeometry(option.rect);
0130 }
0131 
0132 }