]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filelistmodel.cpp
Hooked up CD BIOS loading/running.
[virtualjaguar] / src / gui / filelistmodel.cpp
1 //
2 // filepicker.cpp - A ROM chooser
3 //
4 // by James L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  02/01/2010  Created this file
12 //
13
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.
19
20 #include "filelistmodel.h"
21
22
23 FileListModel::FileListModel(QObject * parent/*= 0*/): QAbstractListModel(parent)
24 {
25 }
26
27 int FileListModel::rowCount(const QModelIndex & parent/*= QModelIndex()*/) const
28 {
29 //      return pixList.size();
30 //      return dbIndex.size();
31         return list.size();
32 }
33
34 QVariant FileListModel::data(const QModelIndex & index, int role) const
35 {
36 //      return QVariant();
37 //      return pixList.at(index.row());
38 //      return (uint)dbIndex.at(index.row());
39 //It could be that this is fucking things up...
40 #if 0
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;
49         else
50                 return QVariant();
51 #else
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;
66
67         return QVariant();
68 #endif
69 }
70
71 QVariant FileListModel::headerData(int/* section*/, Qt::Orientation/* orientation*/, int role/*= Qt::DisplayRole*/) const
72 {
73 #if 0
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)
77                 return QVariant();
78
79         if (orientation == Qt::Horizontal)
80                 return QString("Column %1").arg(section);
81         else
82                 return QString("Row %1").arg(section);
83 #else
84         // This seems more like what we want...
85         if (role == Qt::SizeHintRole)
86                 return QSize(1, 1);
87
88         return QVariant();
89 #endif
90 }
91
92 /*
93 void FileListModel::AddData(QIcon pix)
94 {
95         pixList.push_back(pix);
96         reset();
97 }
98
99 void FileListModel::AddData(unsigned long index)
100 {
101         dbIndex.push_back(index);
102         reset();
103 }
104 */
105
106 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size)
107 {
108         // Assuming that both QString and QImage have copy constructors, this should work.
109         FileListData data;
110
111         data.dbIndex = index;
112         data.fileSize = size;
113         data.filename = str;
114         data.label = img;
115
116         list.push_back(data);
117         reset();
118 }
119
120 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size, bool univHdr, uint32_t type, uint32_t fileCrc)
121 {
122         // Assuming that both QString and QImage have copy constructors, this should work.
123         FileListData data;
124
125         data.dbIndex = index;
126         data.fileSize = size;
127         data.filename = str;
128         data.label = img;
129         data.hasUniversalHeader = univHdr;
130         data.fileType = type;
131         data.crc = fileCrc;
132
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 !!!"
137         reset();
138 }
139
140 void FileListModel::ClearData(void)
141 {
142         if (list.size() == 0)
143                 return;
144
145         beginResetModel();
146         list.clear();
147         endResetModel();
148 }
149
150 //FileListData FileListModel::GetData(const QModelIndex & index) const
151 //{
152 //      return list.at(index.row());
153 //}
154
155 #if 0
156
157 class StringListModel : public QAbstractListModel
158 {
159         Q_OBJECT
160
161         public:
162                 StringListModel(const QStringList &strings, QObject *parent = 0)
163                         : QAbstractListModel(parent), stringList(strings) {}
164
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;
169
170         private:
171                 QStringList stringList;
172 };
173
174 int StringListModel::rowCount(const QModelIndex &parent) const
175 {
176         return stringList.count();
177 }
178
179 QVariant StringListModel::data(const QModelIndex &index, int role) const
180 {
181         if (!index.isValid())
182                 return QVariant();
183
184         if (index.row() >= stringList.size())
185                 return QVariant();
186
187         if (role == Qt::DisplayRole)
188                 return stringList.at(index.row());
189         else
190                 return QVariant();
191 }
192
193
194 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
195 {
196         if (role != Qt::DisplayRole)
197                 return QVariant();
198
199         if (orientation == Qt::Horizontal)
200                 return QString("Column %1").arg(section);
201         else
202                 return QString("Row %1").arg(section);
203 }
204
205
206
207  void ImageModel::setImage(const QImage &image)
208  {
209      modelImage = image;
210      reset();
211  }
212
213 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.
214
215 #endif