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