Subversion Repositories OpenInventory

Compare Revisions

Ignore whitespace Rev 179 → Rev 180

/trunk/DBViewer/delegate.cpp
403,3 → 403,173
// done
painter->restore();
}
 
 
 
 
 
 
IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent)
{
}
 
 
 
 
 
QWidget *IconDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex & /* index */) const
{
 
IconForm *editor = new IconForm(parent);
 
return editor;
 
}
 
 
 
 
void IconDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
 
 
const QAbstractItemModel * model = index.model();
IconForm *icnFrm = static_cast<IconForm*>(editor);
QVariant currentImage = model->data(index,0);
QByteArray bytes = currentImage.toByteArray();
if (currentImage.isValid()) {
m_pxPicture.loadFromData(bytes);
}
else {
//QItemDelegate::paint(painter, option, index);
 
 
return;
}
icnFrm->setPixmap(m_pxPicture);
// tblView->setRowHeight(index.row(), icnFrm->geometry().height());
}
 
 
 
 
void IconDelegate::setModelData(
QWidget *editor,
QAbstractItemModel *model,
const QModelIndex& index) const {
IconForm *icnFrm = static_cast<IconForm*>(editor);
if (!(icnFrm->dataIsChanged())) return;
 
m_pxPicture = icnFrm->pixmap();
QImage currentImage = m_pxPicture.toImage();
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
currentImage.save(&buffer, "PNG");
 
 
 
 
model->setData(index, QVariant (bytes), Qt::EditRole);
 
// int widht = m_pxPicture.width();
// int heigh = m_pxPicture.height();
// tblView->setRowHeight(index.row(), heigh + 10);
// model->submitAll();
 
 
 
 
// model->setData(index, value);
}
 
 
 
void IconDelegate::updateEditorGeometry(
QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex& /* index */) const {
 
 
// QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
// editor->setGeometry(r);
 
editor->setGeometry(option.rect);
}
 
 
 
 
void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
 
 
m_pxPicture.fill( QColor(Qt::white) );
 
const QAbstractItemModel * model = index.model();
QString sFileName = model->data( index, Qt::DisplayRole ).toString();
 
QVariant currentImage = model->data(index,0);
 
 
QByteArray bytes = currentImage.toByteArray();
// QImage image;
// image.loadFromData(bytes);
 
 
if (currentImage.isValid()) {
m_pxPicture.loadFromData(bytes);
 
}
else {
//QItemDelegate::paint(painter, option, index);
return;
}
 
 
 
 
 
/*
 
if ( !sFileName.isEmpty() )
m_pxPicture.load( sFileName );
else {
//QItemDelegate::paint(painter, option, index);
return;
}
*/
 
 
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
QPalette::Disabled;
 
if (option.state & QStyle::State_Selected)
 
 
painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
 
int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
painter->drawPixmap( nX, nY, m_pxPicture );
 
 
// drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
 
/*
QPen pen = painter->pen();
painter->setPen(option.palette.color(QPalette::Mid));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
painter->setPen(pen);
*/
}
 
 
void IconDelegate::setTableView(QTableView* table){
tblView = table;
}