]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filepicker.cpp
Changed executable from vj to virtualjaguar to avoid possible future naming
[virtualjaguar] / src / gui / filepicker.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  01/22/2010  Created this file
12 // JLH  02/06/2010  Modified to use Qt model/view framework
13 // JLH  03/08/2010  Added large cart view and info text
14 //
15
16 #include "filepicker.h"
17
18 #include "filedb.h"
19 #include "filelistmodel.h"
20 #include "filethread.h"
21 #include "imagedelegate.h"
22 //#include "settings.h"
23 //#include "types.h"
24
25 /*
26 Our strategy here is like so:
27 Look at the files in the directory pointed to by ROMPath.
28 For each file in the directory, take the CRC32 of it and compare it to the CRC
29 in the romList[]. If there's a match, put it in a list and note it's index value
30 in romList for future reference.
31
32 When constructing the list, use the index to pull up an image of the cart and
33 put that in the list. User picks from a graphical image of the cart.
34
35 Ideally, the label will go into the archive along with the ROM image, but that's
36 for the future...
37 Maybe box art, screenshots will go as well...
38
39 I'm thinking compatibility info should be displayed as well... Just to stop the
40 inevitable stupid questions from people too lazy to do basic research for themselves.
41
42
43 Data strategy:
44
45 - Should keep a QImage of the blank cart with blank label
46 - Should keep a QImage of the blank cart? (For overpainting the ROMs label)
47 - Should we have a special Alpine image? Floppy image (for COF/ABS)?
48
49 - Need some way of keeping track of cart size and compatibility info
50   [compat info needs to be BAD DUMP or % of what works]
51 - Need to properly scale the thumbnails images in the list
52 */
53
54 //could use Window as well...
55 //FilePickerWindow::FilePickerWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Dialog)
56 FilePickerWindow::FilePickerWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Window)
57 {
58         setWindowTitle(tr("Insert Cartridge..."));
59
60 //is there any reason why this must be cast as a QAbstractListModel? No
61 //Also, need to think about data structure for the model...
62         model = new FileListModel;
63         fileList = new QListView;
64         fileList->setModel(model);
65 //      fileList->setItemDelegate(new ImageDelegate(this));
66         fileList->setItemDelegate(new ImageDelegate());
67
68 //      QVBoxLayout * layout = new QVBoxLayout;
69         QHBoxLayout * layout = new QHBoxLayout;
70         setLayout(layout);
71         layout->addWidget(fileList);
72
73         // Weird note: This layout has to be added *before* putting anything into it...
74         QVBoxLayout * vLayout = new QVBoxLayout;
75         layout->addLayout(vLayout);
76
77         cartImage = new QLabel;
78         QImage cartImg(":/res/cart-blank.png");
79         QPainter painter(&cartImg);
80         painter.drawPixmap(23, 87, QPixmap(":/res/label-blank.png"));
81         painter.end();
82         cartImage->setPixmap(QPixmap::fromImage(cartImg));
83         cartImage->setMargin(4);
84         vLayout->addWidget(cartImage);
85
86         title = new QLabel(QString(tr("<h2>...</h2>")));
87         title->setMargin(6);
88         title->setAlignment(Qt::AlignCenter);
89         vLayout->addWidget(title);
90
91 #if 1
92         QHBoxLayout * dataLayout = new QHBoxLayout;
93         vLayout->addLayout(dataLayout);
94
95         QLabel * labels = new QLabel(QString(tr(
96                 "<b>Type: </b><br>"
97                 "<b>CRC32: </b><br>"
98                 "<b>Compatibility: </b><br>"
99                 "<b>Notes:</b>"
100         )));
101         labels->setAlignment(Qt::AlignRight);
102         labels->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
103         dataLayout->addWidget(labels);
104         data = new QLabel(QString(tr(
105                 "4MB Cartridge<br>"
106                 "FEDCBA98<br>"
107                 "DOES NOT WORK<br>"
108                 "Universal Header detected; Requires DSP"
109         )));
110         data->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
111         dataLayout->addWidget(data);
112 #else
113         QLabel * text2 = new QLabel(QString(tr(
114                 "<table>"
115                 "<tr><td align='right'><b>Type: </b></td><td>4MB Cartridge</td></tr>"
116                 "<tr><td align='right'><b>CRC32: </b></td><td>FEDCBA98</td></tr>"
117                 "<tr><td align='right'><b>Compatibility: </b></td><td>DOES NOT WORK</td></tr>"
118                 "<tr><td align='right'><b>Notes: </b></td><td>Universal Header detected; Requires DSP</td></tr>"
119                 "</table>"
120         )));
121         vLayout->addWidget(text2);
122 #endif
123
124         fileThread = new FileThread(this);
125 //      connect(fileThread, SIGNAL(FoundAFile(unsigned long)), this, SLOT(AddFileToList(unsigned long)));
126         connect(fileThread, SIGNAL(FoundAFile2(unsigned long, QString, QImage *)), this, SLOT(AddFileToList2(unsigned long, QString, QImage *)));
127         fileThread->Go();
128 /*
129 New sizes: 373x172 (label), 420x340 (cart)
130 */
131
132 //      QItemSelectionModel * ism = fileList->selectionModel();
133 //      connect(ism, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(UpdateSelection(const QModelIndex &, const QModelIndex &)));
134         connect(fileList->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(UpdateSelection(const QModelIndex &, const QModelIndex &)));
135 }
136
137 //
138 // This slot gets called by the FileThread's run() function when it finds a
139 // match in the filesystem to a ROM on our CRC list.
140 //
141 void FilePickerWindow::AddFileToList(unsigned long index)
142 {
143 printf("FilePickerWindow: Found match [%s]...\n", romList[index].name);
144         // NOTE: The model *ignores* what you send it, so this is crap. !!! FIX !!! [DONE, somewhat]
145 //      model->AddData(QIcon(":/res/generic.png"));
146         model->AddData(index);
147 }
148
149 void FilePickerWindow::AddFileToList2(unsigned long index, QString str, QImage * img)
150 {
151 printf("FilePickerWindow(2): Found match [%s]...\n", romList[index].name);
152         if (img)
153                 model->AddData(index, str, *img);
154         else
155                 model->AddData(index, str, QImage());
156 }
157
158 //
159 // This slot gets called when the QListView gets clicked on. Updates
160 // the cart graphic and accompanying text.
161 //
162 void FilePickerWindow::UpdateSelection(const QModelIndex & current, const QModelIndex &/*previous*/)
163 {
164         QString s = current.model()->data(current, Qt::EditRole).toString();
165         unsigned long i = current.model()->data(current, Qt::DisplayRole).toUInt();
166         QImage label = current.model()->data(current, Qt::DecorationRole).value<QImage>();
167 //      printf("FPW: %s\n", s.toAscii().data());
168
169         if (!label.isNull())
170         {
171                 QImage cart(":/res/cart-blank.png");
172                 QPainter painter(&cart);
173                 painter.drawPixmap(23, 87, QPixmap::fromImage(label));
174                 painter.end();
175                 cartImage->setPixmap(QPixmap::fromImage(cart));
176         }
177
178         title->setText(QString("<h2>%1</h2>").arg(romList[i].name));
179         QString crcString = QString("%1").arg(romList[i].crc32, 8, 16, QChar('0')).toUpper();
180         data->setText(QString("%1<br>%2<br>%3<br>%4").arg("Cart").arg(crcString).arg("100%").arg("Requires BIOS"));
181 }
182
183 /*
184     Super Duper Awesome Guy (World)
185
186          Type: 4MB Cartridge
187         CRC32: FEDCBA98
188 Compatibility: DOES NOT WORK
189         Notes: Universal Header detected; Requires DSP
190
191
192     Stupid Homebrew Game That Sux
193
194          Type: ABS/COF Executable (43853 bytes)
195         CRC32: 76543210
196 Compatibility: Unknown
197         Notes: $4000 Load, $4000 Run
198
199
200     Action Hopscotch Plus (Prototype)
201
202          Type: 2MB Alpine ROM
203         CRC32: 44889921
204 Compatibility: 80% (or ****)
205         Notes: EEPROM available
206
207
208 */
209
210