]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filelistmodel.cpp
More ROM picker refinements...
[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 }
31
32 QVariant FileListModel::data(const QModelIndex & index, int role) const
33 {
34 //      return QVariant();
35         return pixList.at(index.row());
36 }
37
38 QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role/*= Qt::DisplayRole*/) const
39 {
40 #if 0
41         // Not sure that this is necessary for our purposes...
42         // Especially since this model would never make use of this info...
43         if (role != Qt::DisplayRole)
44                 return QVariant();
45
46         if (orientation == Qt::Horizontal)
47                 return QString("Column %1").arg(section);
48         else
49                 return QString("Row %1").arg(section);
50 #else
51         // This seems more like what we want...
52         if (role == Qt::SizeHintRole)
53                 return QSize(1, 1);
54
55         return QVariant();
56 #endif
57 }
58
59 void FileListModel::AddData(QIcon pix)
60 {
61         pixList.push_back(pix);
62         reset();
63 }
64
65
66 #if 0
67
68 class StringListModel : public QAbstractListModel
69 {
70         Q_OBJECT
71
72         public:
73                 StringListModel(const QStringList &strings, QObject *parent = 0)
74                         : QAbstractListModel(parent), stringList(strings) {}
75
76                 int rowCount(const QModelIndex &parent = QModelIndex()) const;
77                 QVariant data(const QModelIndex &index, int role) const;
78                 QVariant headerData(int section, Qt::Orientation orientation,
79                                                         int role = Qt::DisplayRole) const;
80
81         private:
82                 QStringList stringList;
83 };
84
85 int StringListModel::rowCount(const QModelIndex &parent) const
86 {
87         return stringList.count();
88 }
89
90 QVariant StringListModel::data(const QModelIndex &index, int role) const
91 {
92         if (!index.isValid())
93                 return QVariant();
94
95         if (index.row() >= stringList.size())
96                 return QVariant();
97
98         if (role == Qt::DisplayRole)
99                 return stringList.at(index.row());
100         else
101                 return QVariant();
102 }
103
104
105 QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
106 {
107         if (role != Qt::DisplayRole)
108                 return QVariant();
109
110         if (orientation == Qt::Horizontal)
111                 return QString("Column %1").arg(section);
112         else
113                 return QString("Row %1").arg(section);
114 }
115
116
117
118  void ImageModel::setImage(const QImage &image)
119  {
120      modelImage = image;
121      reset();
122  }
123
124 The QAbstractItemModel::reset() call tells the view(s) that the model has changed.
125
126 #endif