// rdimagepickermodel.cpp // // One-dimensional model for picking images // // (C) Copyright 2020 Fred Gleason // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public // License along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // #include #include "rddb.h" #include "rdimagepickermodel.h" RDImagePickerModel::RDImagePickerModel(const QString &tbl_name, const QString &cat_id_col, const QString &img_id_col, QObject *parent) : QAbstractListModel(parent) { c_table_name=tbl_name; c_category_column=cat_id_col; c_image_column=img_id_col; c_category_id=-1;; c_image_size=QSize(100,100); } RDImagePickerModel::~RDImagePickerModel() { for(int i=0;ifirst()) { c_descriptions[row]=q->value(0).toString()+"\n"+ +"["+q->value(1).toString().toUpper()+", "+ QString().sprintf("%dx%d]",q->value(2).toInt(),q->value(3).toInt()); emit dataChanged(createIndex(row,0),createIndex(row,0)); } delete q; } void RDImagePickerModel::refresh() { LoadRows(c_category_id,c_image_size); } int RDImagePickerModel::rowCount(const QModelIndex &parent) const { return c_images.size(); } QVariant RDImagePickerModel::data(const QModelIndex &index,int role) const { if(index.column()==0) { if(role==Qt::DisplayRole) { return QVariant(c_descriptions.at(index.row())); } if(role==Qt::DecorationRole) { if(c_images.at(index.row())!=NULL) { return QVariant(*(c_images.at(index.row()))); } } if(role==Qt::SizeHintRole) { return QVariant(QSize(200,50)); } } return QVariant(); } QVariant RDImagePickerModel::headerData(int section,Qt::Orientation orient, int role) { if((orient==Qt::Horizontal)&&(section==0)) { if(role==Qt::DisplayRole) { return QVariant(tr("Image")); } } return QVariant(); } void RDImagePickerModel::setCategoryId(int id) { if(id!=c_category_id) { LoadRows(id,c_image_size); c_category_id=id; } } void RDImagePickerModel::LoadRows(int cat_id,const QSize &img_size) { QString sql; RDSqlQuery *q=NULL; QImage img; // // Clear stale data // if(c_images.size()>0) { beginRemoveRows(QModelIndex(),0,c_images.size()-1); for(int i=0;isize()>0) { beginInsertRows(QModelIndex(),0,q->size()-1); while(q->next()) { c_image_ids.push_back(q->value(0).toUInt()); c_descriptions. push_back(q->value(1).toString()+"\n"+ "["+q->value(2).toString().toUpper()+", "+ QString().sprintf("%dx%d]", q->value(3).toInt(),q->value(4).toInt())); img.loadFromData(q->value(5).toByteArray()); c_images.push_back(new QPixmap(QPixmap::fromImage(img.scaled(img_size, Qt::KeepAspectRatio,Qt::SmoothTransformation)))); } endInsertRows(); } delete q; }