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;
65 QVariant FileListModel::headerData(int/* section*/, Qt::Orientation/* orientation*/, int role/*= Qt::DisplayRole*/) const
68 // Not sure that this is necessary for our purposes...
69 // Especially since this model would never make use of this info...
70 if (role != Qt::DisplayRole)
73 if (orientation == Qt::Horizontal)
74 return QString("Column %1").arg(section);
76 return QString("Row %1").arg(section);
78 // This seems more like what we want...
79 if (role == Qt::SizeHintRole)
87 void FileListModel::AddData(QIcon pix)
89 pixList.push_back(pix);
93 void FileListModel::AddData(unsigned long index)
95 dbIndex.push_back(index);
100 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size)
102 // Assuming that both QString and QImage have copy constructors, this should work.
105 data.dbIndex = index;
106 data.fileSize = size;
110 list.push_back(data);
114 //FileListData FileListModel::GetData(const QModelIndex & index) const
116 // return list.at(index.row());
121 class StringListModel : public QAbstractListModel
126 StringListModel(const QStringList &strings, QObject *parent = 0)
127 : QAbstractListModel(parent), stringList(strings) {}
129 int rowCount(const QModelIndex &parent = QModelIndex()) const;
130 QVariant data(const QModelIndex &index, int role) const;
131 QVariant headerData(int section, Qt::Orientation orientation,
132 int role = Qt::DisplayRole) const;
135 QStringList stringList;
138 int StringListModel::rowCount(const QModelIndex &parent) const
140 return stringList.count();
143 QVariant StringListModel::data(const QModelIndex &index, int role) const
145 if (!index.isValid())
148 if (index.row() >= stringList.size())
151 if (role == Qt::DisplayRole)
152 return stringList.at(index.row());
158 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
160 if (role != Qt::DisplayRole)
163 if (orientation == Qt::Horizontal)
164 return QString("Column %1").arg(section);
166 return QString("Row %1").arg(section);
171 void ImageModel::setImage(const QImage &image)
177 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.