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);
137 void FileListModel::ClearData(void)
139 if (list.size() == 0)
147 //FileListData FileListModel::GetData(const QModelIndex & index) const
149 // return list.at(index.row());
154 class StringListModel : public QAbstractListModel
159 StringListModel(const QStringList &strings, QObject *parent = 0)
160 : QAbstractListModel(parent), stringList(strings) {}
162 int rowCount(const QModelIndex &parent = QModelIndex()) const;
163 QVariant data(const QModelIndex &index, int role) const;
164 QVariant headerData(int section, Qt::Orientation orientation,
165 int role = Qt::DisplayRole) const;
168 QStringList stringList;
171 int StringListModel::rowCount(const QModelIndex &parent) const
173 return stringList.count();
176 QVariant StringListModel::data(const QModelIndex &index, int role) const
178 if (!index.isValid())
181 if (index.row() >= stringList.size())
184 if (role == Qt::DisplayRole)
185 return stringList.at(index.row());
191 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
193 if (role != Qt::DisplayRole)
196 if (orientation == Qt::Horizontal)
197 return QString("Column %1").arg(section);
199 return QString("Row %1").arg(section);
204 void ImageModel::setImage(const QImage &image)
210 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.