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