]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filelistmodel.cpp
09389b86289043e5afd7b2d93af51bc2a4794aa9
[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
61         return QVariant();
62 #endif
63 }
64
65 QVariant FileListModel::headerData(int/* section*/, Qt::Orientation/* orientation*/, int role/*= Qt::DisplayRole*/) const
66 {
67 #if 0
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)
71                 return QVariant();
72
73         if (orientation == Qt::Horizontal)
74                 return QString("Column %1").arg(section);
75         else
76                 return QString("Row %1").arg(section);
77 #else
78         // This seems more like what we want...
79         if (role == Qt::SizeHintRole)
80                 return QSize(1, 1);
81
82         return QVariant();
83 #endif
84 }
85
86 /*
87 void FileListModel::AddData(QIcon pix)
88 {
89         pixList.push_back(pix);
90         reset();
91 }
92
93 void FileListModel::AddData(unsigned long index)
94 {
95         dbIndex.push_back(index);
96         reset();
97 }
98 */
99
100 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size)
101 {
102         // Assuming that both QString and QImage have copy constructors, this should work.
103         FileListData data;
104
105         data.dbIndex = index;
106         data.fileSize = size;
107         data.filename = str;
108         data.label = img;
109
110         list.push_back(data);
111         reset();
112 }
113
114 //FileListData FileListModel::GetData(const QModelIndex & index) const
115 //{
116 //      return list.at(index.row());
117 //}
118
119 #if 0
120
121 class StringListModel : public QAbstractListModel
122 {
123         Q_OBJECT
124
125         public:
126                 StringListModel(const QStringList &strings, QObject *parent = 0)
127                         : QAbstractListModel(parent), stringList(strings) {}
128
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;
133
134         private:
135                 QStringList stringList;
136 };
137
138 int StringListModel::rowCount(const QModelIndex &parent) const
139 {
140         return stringList.count();
141 }
142
143 QVariant StringListModel::data(const QModelIndex &index, int role) const
144 {
145         if (!index.isValid())
146                 return QVariant();
147
148         if (index.row() >= stringList.size())
149                 return QVariant();
150
151         if (role == Qt::DisplayRole)
152                 return stringList.at(index.row());
153         else
154                 return QVariant();
155 }
156
157
158 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
159 {
160         if (role != Qt::DisplayRole)
161                 return QVariant();
162
163         if (orientation == Qt::Horizontal)
164                 return QString("Column %1").arg(section);
165         else
166                 return QString("Row %1").arg(section);
167 }
168
169
170
171  void ImageModel::setImage(const QImage &image)
172  {
173      modelImage = image;
174      reset();
175  }
176
177 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.
178
179 #endif