2 // filelistmodel.cpp - A ROM chooser
5 // (C) 2010 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
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 // JLH 07/07/2011 Added code to help the built-in keyboard based auto-search
14 // of the QListView class
17 // Note that we have to put in convenience functions to the model for adding data
18 // and calling reset() to tell the view(s) that the model has changed. So that much
19 // should be simple. According to the docs, we have to reimplement flags() in the
20 // QAbstractListModel subclass, but in the example given below they don't. Not sure
21 // if it's necessary or not.
23 #include "filelistmodel.h"
28 FileListModel::FileListModel(QObject
* parent
/*= 0*/): QAbstractListModel(parent
)
32 int FileListModel::rowCount(const QModelIndex
& /*parent = QModelIndex()*/) const
37 QVariant
FileListModel::data(const QModelIndex
& index
, int role
) const
39 if (role
== FLM_LABEL
)
40 return list
.at(index
.row()).label
;
41 else if (role
== FLM_INDEX
)
42 return (uint
)list
.at(index
.row()).dbIndex
;
43 else if (role
== FLM_FILENAME
)
44 return list
.at(index
.row()).filename
;
45 else if (role
== FLM_FILESIZE
)
46 return (uint
)list
.at(index
.row()).fileSize
;
47 else if (role
== FLM_UNIVERSALHDR
)
48 return (uint
)list
.at(index
.row()).hasUniversalHeader
;
49 else if (role
== FLM_FILETYPE
)
50 return (uint
)list
.at(index
.row()).fileType
;
51 else if (role
== FLM_CRC
)
52 return (uint
)list
.at(index
.row()).crc
;
53 else if (role
== Qt::DisplayRole
)
55 // The QListView uses this role to do keyboard based searching,
56 // so we help it along by giving it what it needs to do the job. :-)
57 unsigned long dbIndex
= list
.at(index
.row()).dbIndex
;
58 QString filename
= list
.at(index
.row()).filename
;
61 // Pull name from file DB, otherwise, use the filename...
62 if (dbIndex
!= 0xFFFFFFFF)
63 nameToMatch
= romList
[dbIndex
].name
;
66 int lastSlashPos
= filename
.lastIndexOf('/');
67 nameToMatch
= filename
.mid(lastSlashPos
+ 1);
76 QVariant
FileListModel::headerData(int/* section*/, Qt::Orientation
/* orientation*/, int role
/*= Qt::DisplayRole*/) const
78 // This seems more like what we want...
79 if (role
== Qt::SizeHintRole
)
85 void FileListModel::AddData(unsigned long index
, QString str
, QImage img
, unsigned long size
)
87 // Assuming that both QString and QImage have copy constructors, this should work.
96 beginInsertRows(QModelIndex(), list
.size(), list
.size());
101 void FileListModel::AddData(unsigned long index
, QString str
, QImage img
, unsigned long size
, bool univHdr
, uint32_t type
, uint32_t fileCrc
)
103 // Assuming that both QString and QImage have copy constructors, this should work.
106 data
.dbIndex
= index
;
107 data
.fileSize
= size
;
110 data
.hasUniversalHeader
= univHdr
;
111 data
.fileType
= type
;
115 beginInsertRows(QModelIndex(), list
.size(), list
.size());
116 list
.push_back(data
);
120 void FileListModel::ClearData(void)
122 if (list
.size() == 0)
130 //FileListData FileListModel::GetData(const QModelIndex & index) const
132 // return list.at(index.row());