]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filelistmodel.cpp
8308c5c946123c3511c1eb07f7b776e8519c1123
[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         reset();
135 }
136
137 void FileListModel::ClearData(void)
138 {
139         if (list.size() == 0)
140                 return;
141
142         beginResetModel();
143         list.clear();
144         endResetModel();
145 }
146
147 //FileListData FileListModel::GetData(const QModelIndex & index) const
148 //{
149 //      return list.at(index.row());
150 //}
151
152 #if 0
153
154 class StringListModel : public QAbstractListModel
155 {
156         Q_OBJECT
157
158         public:
159                 StringListModel(const QStringList &strings, QObject *parent = 0)
160                         : QAbstractListModel(parent), stringList(strings) {}
161
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;
166
167         private:
168                 QStringList stringList;
169 };
170
171 int StringListModel::rowCount(const QModelIndex &parent) const
172 {
173         return stringList.count();
174 }
175
176 QVariant StringListModel::data(const QModelIndex &index, int role) const
177 {
178         if (!index.isValid())
179                 return QVariant();
180
181         if (index.row() >= stringList.size())
182                 return QVariant();
183
184         if (role == Qt::DisplayRole)
185                 return stringList.at(index.row());
186         else
187                 return QVariant();
188 }
189
190
191 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
192 {
193         if (role != Qt::DisplayRole)
194                 return QVariant();
195
196         if (orientation == Qt::Horizontal)
197                 return QString("Column %1").arg(section);
198         else
199                 return QString("Row %1").arg(section);
200 }
201
202
203
204  void ImageModel::setImage(const QImage &image)
205  {
206      modelImage = image;
207      reset();
208  }
209
210 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.
211
212 #endif