Commit | Line | Data |
---|---|---|
cf76e892 JPM |
1 | // |
2 | // filelistmodel.cpp - A ROM chooser | |
3 | // | |
4 | // by James Hammons | |
5 | // (C) 2010 Underground Software | |
6 | // | |
7 | // JLH = James 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 | // JLH 07/07/2011 Added code to help the built-in keyboard based auto-search | |
14 | // of the QListView class | |
15 | // | |
16 | ||
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. | |
22 | ||
23 | #include "filelistmodel.h" | |
24 | ||
25 | #include "filedb.h" | |
26 | ||
27 | ||
28 | FileListModel::FileListModel(QObject * parent/*= 0*/): QAbstractListModel(parent) | |
29 | { | |
30 | } | |
31 | ||
32 | int FileListModel::rowCount(const QModelIndex & /*parent = QModelIndex()*/) const | |
33 | { | |
34 | return list.size(); | |
35 | } | |
36 | ||
37 | QVariant FileListModel::data(const QModelIndex & index, int role) const | |
38 | { | |
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) | |
54 | { | |
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; | |
59 | QString nameToMatch; | |
60 | ||
61 | // Pull name from file DB, otherwise, use the filename... | |
62 | if (dbIndex != 0xFFFFFFFF) | |
63 | nameToMatch = romList[dbIndex].name; | |
64 | else | |
65 | { | |
66 | int lastSlashPos = filename.lastIndexOf('/'); | |
67 | nameToMatch = filename.mid(lastSlashPos + 1); | |
68 | } | |
69 | ||
70 | return nameToMatch; | |
71 | } | |
72 | ||
73 | return QVariant(); | |
74 | } | |
75 | ||
76 | QVariant FileListModel::headerData(int/* section*/, Qt::Orientation/* orientation*/, int role/*= Qt::DisplayRole*/) const | |
77 | { | |
78 | // This seems more like what we want... | |
79 | if (role == Qt::SizeHintRole) | |
80 | return QSize(1, 1); | |
81 | ||
82 | return QVariant(); | |
83 | } | |
84 | ||
85 | void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size) | |
86 | { | |
87 | // Assuming that both QString and QImage have copy constructors, this should work. | |
88 | FileListData data; | |
89 | ||
90 | data.dbIndex = index; | |
91 | data.fileSize = size; | |
92 | data.filename = str; | |
93 | data.label = img; | |
94 | ||
95 | // Let's try this: | |
96 | beginInsertRows(QModelIndex(), list.size(), list.size()); | |
97 | list.push_back(data); | |
98 | endInsertRows(); | |
99 | } | |
100 | ||
101 | void FileListModel::AddData(unsigned long index, QString str, QImage img, unsigned long size, bool univHdr, uint32_t type, uint32_t fileCrc) | |
102 | { | |
103 | // Assuming that both QString and QImage have copy constructors, this should work. | |
104 | FileListData data; | |
105 | ||
106 | data.dbIndex = index; | |
107 | data.fileSize = size; | |
108 | data.filename = str; | |
109 | data.label = img; | |
110 | data.hasUniversalHeader = univHdr; | |
111 | data.fileType = type; | |
112 | data.crc = fileCrc; | |
113 | ||
114 | // Let's try this: | |
115 | beginInsertRows(QModelIndex(), list.size(), list.size()); | |
116 | list.push_back(data); | |
117 | endInsertRows(); | |
118 | } | |
119 | ||
120 | void FileListModel::ClearData(void) | |
121 | { | |
122 | if (list.size() == 0) | |
123 | return; | |
124 | ||
125 | beginResetModel(); | |
126 | list.clear(); | |
127 | endResetModel(); | |
128 | } | |
129 | ||
130 | //FileListData FileListModel::GetData(const QModelIndex & index) const | |
131 | //{ | |
132 | // return list.at(index.row()); | |
133 | //} |