]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filelistmodel.cpp
097514c851ca18958bbaa2d40b3b3cad335147f9
[virtualjaguar] / src / gui / filelistmodel.cpp
1 //
2 // filelistmodel.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 // JLH  07/05/2011  Fixed model to not reset itself with each new row insertion
13 //
14
15 // Note that we have to put in convenience functions to the model for adding data
16 // and calling reset() to tell the view(s) that the model has changed. So that much
17 // should be simple. According to the docs, we have to reimplement flags() in the
18 // QAbstractListModel subclass, but in the example given below they don't. Not sure
19 // if it's necessary or not.
20
21 #include "filelistmodel.h"
22
23
24 FileListModel::FileListModel(QObject * parent/*= 0*/): QAbstractListModel(parent)
25 {
26 }
27
28 int FileListModel::rowCount(const QModelIndex & parent/*= QModelIndex()*/) const
29 {
30         return list.size();
31 }
32
33 QVariant FileListModel::data(const QModelIndex & index, int role) const
34 {
35         if (role == FLM_LABEL)
36                 return list.at(index.row()).label;
37         else if (role == FLM_INDEX)
38                 return (uint)list.at(index.row()).dbIndex;
39         else if (role == FLM_FILENAME)
40                 return list.at(index.row()).filename;
41         else if (role == FLM_FILESIZE)
42                 return (uint)list.at(index.row()).fileSize;
43         else if (role == FLM_UNIVERSALHDR)
44                 return (uint)list.at(index.row()).hasUniversalHeader;
45         else if (role == FLM_FILETYPE)
46                 return (uint)list.at(index.row()).fileType;
47         else if (role == FLM_CRC)
48                 return (uint)list.at(index.row()).crc;
49
50         return QVariant();
51 }
52
53 QVariant FileListModel::headerData(int/* section*/, Qt::Orientation/* orientation*/, int role/*= Qt::DisplayRole*/) const
54 {
55         // This seems more like what we want...
56         if (role == Qt::SizeHintRole)
57                 return QSize(1, 1);
58
59         return QVariant();
60 }
61
62 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size)
63 {
64         // Assuming that both QString and QImage have copy constructors, this should work.
65         FileListData data;
66
67         data.dbIndex = index;
68         data.fileSize = size;
69         data.filename = str;
70         data.label = img;
71
72         // Let's try this:
73         beginInsertRows(QModelIndex(), list.size(), list.size());
74         list.push_back(data);
75         endInsertRows();
76 }
77
78 void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size, bool univHdr, uint32_t type, uint32_t fileCrc)
79 {
80         // Assuming that both QString and QImage have copy constructors, this should work.
81         FileListData data;
82
83         data.dbIndex = index;
84         data.fileSize = size;
85         data.filename = str;
86         data.label = img;
87         data.hasUniversalHeader = univHdr;
88         data.fileType = type;
89         data.crc = fileCrc;
90
91         // Let's try this:
92         beginInsertRows(QModelIndex(), list.size(), list.size());
93         list.push_back(data);
94         endInsertRows();
95 }
96
97 void FileListModel::ClearData(void)
98 {
99         if (list.size() == 0)
100                 return;
101
102         beginResetModel();
103         list.clear();
104         endResetModel();
105 }
106
107 //FileListData FileListModel::GetData(const QModelIndex & index) const
108 //{
109 //      return list.at(index.row());
110 //}