2 // filepicker.cpp - A ROM chooser
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 02/01/2010 Created this file
14 // Note that we have to put in convenience functions to the model for adding data
15 // and calling reset() to tell the view(s) that the model has changed. So that much
16 // should be simple. According to the docs, we have to reimplement flags() in the
17 // QAbstractListModel subclass, but in the example given below they don't. Not sure
18 // if it's necessary or not.
20 #include "filelistmodel.h"
23 FileListModel::FileListModel(QObject * parent/*= 0*/): QAbstractListModel(parent)
27 int FileListModel::rowCount(const QModelIndex & parent/*= QModelIndex()*/) const
29 // return pixList.size();
30 // return dbIndex.size();
34 QVariant FileListModel::data(const QModelIndex & index, int role) const
37 // return pixList.at(index.row());
38 // return (uint)dbIndex.at(index.row());
39 //It could be that this is fucking things up...
41 if (role == Qt::DecorationRole)
42 return list.at(index.row()).label;
43 else if (role == Qt::DisplayRole)
44 return (uint)list.at(index.row()).dbIndex;
45 else if (role == Qt::EditRole)
46 return list.at(index.row()).filename;
47 else if (role == Qt::WhatsThisRole)
48 return (uint)list.at(index.row()).fileSize;
52 if (role == FLM_LABEL)
53 return list.at(index.row()).label;
54 else if (role == FLM_INDEX)
55 return (uint)list.at(index.row()).dbIndex;
56 else if (role == FLM_FILENAME)
57 return list.at(index.row()).filename;
58 else if (role == FLM_FILESIZE)
59 return (uint)list.at(index.row()).fileSize;
60 else if (role == FLM_UNIVERSALHDR)
61 return (uint)list.at(index.row()).hasUniversalHeader;
62 else if (role == FLM_FILETYPE)
63 return (uint)list.at(index.row()).fileType;
64 else if (role == FLM_CRC)
65 return (uint)list.at(index.row()).crc;
71 QVariant FileListModel::headerData(int/* section*/, Qt::Orientation/* orientation*/, int role/*= Qt::DisplayRole*/) const
74 // Not sure that this is necessary for our purposes...
75 // Especially since this model would never make use of this info...
76 if (role != Qt::DisplayRole)
79 if (orientation == Qt::Horizontal)
80 return QString("Column %1").arg(section);
82 return QString("Row %1").arg(section);
84 // This seems more like what we want...
85 if (role == Qt::SizeHintRole)
93 void FileListModel::AddData(QIcon pix)
95 pixList.push_back(pix);
99 void FileListModel::AddData(unsigned long index)
101 dbIndex.push_back(index);
106 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size)
108 // Assuming that both QString and QImage have copy constructors, this should work.
111 data.dbIndex = index;
112 data.fileSize = size;
116 list.push_back(data);
120 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size, bool univHdr, uint32_t type, uint32_t fileCrc)
122 // Assuming that both QString and QImage have copy constructors, this should work.
125 data.dbIndex = index;
126 data.fileSize = size;
129 data.hasUniversalHeader = univHdr;
130 data.fileType = type;
133 list.push_back(data);
134 //This is probably not the best way to do this, it prevents the user from using the
135 //list while it populates.
136 #warning "!!! AddData calls reset() for every addition, this is bad !!!"
140 void FileListModel::ClearData(void)
142 if (list.size() == 0)
150 //FileListData FileListModel::GetData(const QModelIndex & index) const
152 // return list.at(index.row());
157 class StringListModel : public QAbstractListModel
162 StringListModel(const QStringList &strings, QObject *parent = 0)
163 : QAbstractListModel(parent), stringList(strings) {}
165 int rowCount(const QModelIndex &parent = QModelIndex()) const;
166 QVariant data(const QModelIndex &index, int role) const;
167 QVariant headerData(int section, Qt::Orientation orientation,
168 int role = Qt::DisplayRole) const;
171 QStringList stringList;
174 int StringListModel::rowCount(const QModelIndex &parent) const
176 return stringList.count();
179 QVariant StringListModel::data(const QModelIndex &index, int role) const
181 if (!index.isValid())
184 if (index.row() >= stringList.size())
187 if (role == Qt::DisplayRole)
188 return stringList.at(index.row());
194 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
196 if (role != Qt::DisplayRole)
199 if (orientation == Qt::Horizontal)
200 return QString("Column %1").arg(section);
202 return QString("Row %1").arg(section);
207 void ImageModel::setImage(const QImage &image)
213 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.