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 if (role == Qt::DecorationRole)
40 return list.at(index.row()).label;
41 else if (role == Qt::DisplayRole)
42 return (uint)list.at(index.row()).dbIndex;
43 else if (role == Qt::EditRole)
44 return list.at(index.row()).filename;
49 QVariant FileListModel::headerData(int/* section*/, Qt::Orientation/* orientation*/, int role/*= Qt::DisplayRole*/) const
52 // Not sure that this is necessary for our purposes...
53 // Especially since this model would never make use of this info...
54 if (role != Qt::DisplayRole)
57 if (orientation == Qt::Horizontal)
58 return QString("Column %1").arg(section);
60 return QString("Row %1").arg(section);
62 // This seems more like what we want...
63 if (role == Qt::SizeHintRole)
70 void FileListModel::AddData(QIcon pix)
72 pixList.push_back(pix);
76 void FileListModel::AddData(unsigned long index)
78 dbIndex.push_back(index);
82 void FileListModel::AddData(unsigned long index, QString str, QImage img)
84 // Assuming that both QString and QImage have copy constructors, this should work.
95 //FileListData FileListModel::GetData(const QModelIndex & index) const
97 // return list.at(index.row());
102 class StringListModel : public QAbstractListModel
107 StringListModel(const QStringList &strings, QObject *parent = 0)
108 : QAbstractListModel(parent), stringList(strings) {}
110 int rowCount(const QModelIndex &parent = QModelIndex()) const;
111 QVariant data(const QModelIndex &index, int role) const;
112 QVariant headerData(int section, Qt::Orientation orientation,
113 int role = Qt::DisplayRole) const;
116 QStringList stringList;
119 int StringListModel::rowCount(const QModelIndex &parent) const
121 return stringList.count();
124 QVariant StringListModel::data(const QModelIndex &index, int role) const
126 if (!index.isValid())
129 if (index.row() >= stringList.size())
132 if (role == Qt::DisplayRole)
133 return stringList.at(index.row());
139 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
141 if (role != Qt::DisplayRole)
144 if (orientation == Qt::Horizontal)
145 return QString("Column %1").arg(section);
147 return QString("Row %1").arg(section);
152 void ImageModel::setImage(const QImage &image)
158 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.