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