File indexing completed on 2024-05-05 05:40:57
0001 #include "imagepathdelegateitem.h" 0002 0003 #include <QApplication> 0004 #include <QDebug> 0005 #include <QFileDialog> 0006 #include <QHBoxLayout> 0007 #include <QLineEdit> 0008 #include <QPainter> 0009 0010 #include "customs/imagepatheditor.h" 0011 #include "model/characterstatemodel.h" 0012 0013 namespace rwidgets 0014 { 0015 0016 ImagePathDelegateItem::ImagePathDelegateItem(const QString& root, QObject* parent) 0017 : QStyledItemDelegate(parent), m_root(root) 0018 { 0019 } 0020 0021 QWidget* ImagePathDelegateItem::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const 0022 { 0023 ImagePathEditor* editor= new ImagePathEditor(m_root, parent); 0024 0025 return editor; 0026 } 0027 0028 void ImagePathDelegateItem::setEditorData(QWidget* editor, const QModelIndex& index) const 0029 { 0030 if(index.isValid()) 0031 { 0032 ImagePathEditor* ImgEditor= qobject_cast<ImagePathEditor*>(editor); 0033 if(nullptr != ImgEditor) 0034 { 0035 ImgEditor->setPixmap(index.data(CharacterStateModel::PICTURE).value<QPixmap>()); 0036 } 0037 } 0038 QStyledItemDelegate::setEditorData(editor, index); 0039 } 0040 0041 void ImagePathDelegateItem::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, 0042 const QModelIndex& index) const 0043 { 0044 QStyledItemDelegate::updateEditorGeometry(editor, option, index); 0045 } 0046 0047 void ImagePathDelegateItem::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const 0048 { 0049 ImagePathEditor* imgEditor= qobject_cast<ImagePathEditor*>(editor); 0050 if(nullptr != imgEditor) 0051 { 0052 QVariant var; 0053 var.setValue(imgEditor->filename()); 0054 model->setData(index, var, Qt::EditRole); 0055 } 0056 else 0057 { 0058 QStyledItemDelegate::setModelData(editor, model, index); 0059 } 0060 } 0061 0062 void ImagePathDelegateItem::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 0063 { 0064 // paint selection 0065 QStyledItemDelegate::paint(painter, option, index); 0066 0067 QPixmap pix= index.data().value<QPixmap>(); 0068 0069 if(pix.isNull()) 0070 return; 0071 0072 QRect rectImg= pix.rect(); 0073 qreal ratioImg= (qreal)rectImg.width() / (qreal)rectImg.height(); 0074 0075 QRect target2= option.rect; 0076 qreal ratioZone= (qreal)target2.width() / (qreal)target2.height(); 0077 0078 QRect target(target2); 0079 0080 if(ratioZone >= 1) 0081 { 0082 target.setHeight(target2.height()); 0083 target.setWidth(target2.height() * ratioImg); 0084 } 0085 else 0086 { 0087 target.setWidth(target2.width()); 0088 target.setHeight(target2.width() / ratioImg); 0089 } 0090 0091 painter->drawImage(target, pix.toImage()); 0092 } 0093 0094 QSize ImagePathDelegateItem::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const 0095 { 0096 QSize a= QStyledItemDelegate::sizeHint(option, index); 0097 a.setHeight(30); 0098 return a; 0099 } 0100 } // namespace rwidgets