~ubuntu-branches/ubuntu/wily/mupen64plus/wily

« back to all changes in this revision

Viewing changes to main/gui_qt4/rommodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2011-07-24 14:23:26 UTC
  • mfrom: (10.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110724142326-x9z5qu8j9jecrmod
Tags: 1.99.4+2
* Upload to unstable
* Remove overrides for lintian warning about change to native package
* Update Vcs-* fields to new anonscm.debian.org URLs in debian/control
* Fix spelling of "Flexible" in debian/control (Closes: #633693)
* Mark all targets in debian/rules as phony
* Add some information about the mupen64plus 2.0 vision in debian/NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 
 *   Mupen64plus - rommodel.cpp                                            *
3
 
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4
 
 *   Copyright (C) 2008 Slougi                                             *
5
 
 *                                                                         *
6
 
 *   This program is free software; you can redistribute it and/or modify  *
7
 
 *   it under the terms of the GNU General Public License as published by  *
8
 
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 
 *   (at your option) any later version.                                   *
10
 
 *                                                                         *
11
 
 *   This program is distributed in the hope that it will be useful,       *
12
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 
 *   GNU General Public License for more details.                          *
15
 
 *                                                                         *
16
 
 *   You should have received a copy of the GNU General Public License     *
17
 
 *   along with this program; if not, write to the                         *
18
 
 *   Free Software Foundation, Inc.,                                       *
19
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20
 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
 
 
22
 
#include <QStringList>
23
 
#include <QDir>
24
 
#include <QFont>
25
 
#include <QUrl>
26
 
#include <QMetaEnum>
27
 
#include <QDateTime>
28
 
#include <QWidget>
29
 
 
30
 
#include <cstdio>
31
 
 
32
 
#include "rommodel.h"
33
 
#include "globals.h"
34
 
 
35
 
namespace core {
36
 
    extern "C" {
37
 
        #include "../rom.h"
38
 
        #include "../romcache.h"
39
 
        #include "../main.h"
40
 
        #include "../config.h"
41
 
        #include "../util.h"
42
 
    }
43
 
}
44
 
 
45
 
RomModel::RomModel(QObject* parent)
46
 
    : QAbstractItemModel(parent)
47
 
    , m_showFullPath(core::config_get_bool("RomBrowserShowFullPaths", FALSE))
48
 
    , m_romDirectories(romDirectories())
49
 
{
50
 
    QString file;
51
 
    char* iconpath = core::get_iconspath();
52
 
    QPixmap paustralia(file.sprintf("%s%s", iconpath, "australia.png"));
53
 
    QPixmap peurope(file.sprintf("%s%s", iconpath, "europe.png"));
54
 
    QPixmap pfrance(file.sprintf("%s%s", iconpath, "france.png"));
55
 
    QPixmap pgermany(file.sprintf("%s%s", iconpath, "germany.png"));
56
 
    QPixmap pitaly(file.sprintf("%s%s", iconpath, "italy.png"));
57
 
    QPixmap pjapan(file.sprintf("%s%s", iconpath, "japan.png"));
58
 
    QPixmap pspain(file.sprintf("%s%s", iconpath, "spain.png"));
59
 
    QPixmap pusa(file.sprintf("%s%s", iconpath, "usa.png"));
60
 
    QPixmap pjapanusa(file.sprintf("%s%s", iconpath, "japanusa.png"));
61
 
    QPixmap pn64cart(file.sprintf("%s%s", iconpath, "mupen64cart.png"));
62
 
 
63
 
    QPair<QString, QPixmap> demo(tr("Demo"), pn64cart);
64
 
    QPair<QString, QPixmap> beta(tr("Beta"), pn64cart);
65
 
    QPair<QString, QPixmap> japanusa(tr("Japan/USA"), pjapanusa);
66
 
    QPair<QString, QPixmap> usa(tr("USA"), pusa);
67
 
    QPair<QString, QPixmap> germany(tr("Germany"), pgermany);
68
 
    QPair<QString, QPixmap> france(tr("France"), pfrance);
69
 
    QPair<QString, QPixmap> italy(tr("Italy"), pitaly);
70
 
    QPair<QString, QPixmap> japan(tr("Japan"), pjapan);
71
 
    QPair<QString, QPixmap> spain(tr("Spain"), pspain);
72
 
    QPair<QString, QPixmap> australia(tr("Australia"), paustralia);
73
 
    QPair<QString, QPixmap> europe(tr("Europe"), peurope);
74
 
    QPair<QString, QPixmap> unknown(tr("Unknown"), pn64cart);
75
 
 
76
 
    m_countryInfo[char(0)] = demo;
77
 
    m_countryInfo['7'] = beta;
78
 
    m_countryInfo[char(0x41)] = japanusa;
79
 
    m_countryInfo[char(0x44)] = germany;
80
 
    m_countryInfo[char(0x45)] = usa;
81
 
    m_countryInfo[char(0x46)] = france;
82
 
    m_countryInfo['I'] = italy;
83
 
    m_countryInfo[char(0x4A)] = japan;
84
 
    m_countryInfo['S'] = spain;
85
 
    m_countryInfo[char(0x55)] = australia;
86
 
    m_countryInfo[char(0x59)] = australia;
87
 
    m_countryInfo[char(0x50)] = europe;
88
 
    m_countryInfo[char(0x58)] = europe;
89
 
    m_countryInfo[char(0x20)] = europe;
90
 
    m_countryInfo[char(0x21)] = europe;
91
 
    m_countryInfo[char(0x38)] = europe;
92
 
    m_countryInfo[char(0x70)] = europe;
93
 
    m_countryInfo['?'] = unknown;
94
 
}
95
 
 
96
 
RomModel* RomModel::self()
97
 
{
98
 
    static RomModel* instance = new RomModel;
99
 
    return instance;
100
 
}
101
 
 
102
 
void RomModel::update(unsigned int roms, unsigned short clear)
103
 
{
104
 
    //If clear flag is set, clear the GUI rombrowser.
105
 
    if (clear) {
106
 
        m_romList.clear();
107
 
        reset();
108
 
    }
109
 
 
110
 
    unsigned int arrayroms = static_cast<unsigned int>(m_romList.count());
111
 
 
112
 
    //If there are currently more ROMs in cache than GUI rombrowser, add them.
113
 
    if (roms > arrayroms) {
114
 
        core::cache_entry *entry;
115
 
        entry = core::g_romcache.top;
116
 
 
117
 
        //Advance cache pointer.
118
 
        for (unsigned i = 0; i < arrayroms; i++) {
119
 
            entry = entry->next;
120
 
            if (!entry) {
121
 
                return;
122
 
            }
123
 
        }
124
 
 
125
 
        for (unsigned i = 0; (i < roms) && (entry != NULL); i++) {
126
 
            //Actually add entries to RomModel
127
 
            m_romList << entry;
128
 
            //printf("Added: %s\n", entry->inientry->goodname);
129
 
            entry = entry->next;
130
 
        }
131
 
        reset();
132
 
    }
133
 
}
134
 
 
135
 
QModelIndex RomModel::index(int row, int column,
136
 
                             const QModelIndex& parent) const
137
 
{
138
 
    Q_UNUSED(parent);
139
 
    return createIndex(row, column, m_romList.at(row));
140
 
}
141
 
 
142
 
QModelIndex RomModel::parent(const QModelIndex& index) const
143
 
{
144
 
    Q_UNUSED(index);
145
 
    return QModelIndex();
146
 
}
147
 
 
148
 
int RomModel::rowCount(const QModelIndex& parent) const
149
 
{
150
 
    int retval = 0;
151
 
 
152
 
    if (!parent.isValid()) {
153
 
        retval = m_romList.count();
154
 
    }
155
 
 
156
 
    return retval;
157
 
}
158
 
 
159
 
int RomModel::columnCount(const QModelIndex& parent) const
160
 
{
161
 
    Q_UNUSED(parent);
162
 
    return 16;
163
 
}
164
 
 
165
 
QVariant RomModel::data(const QModelIndex& index, int role) const
166
 
{
167
 
    QVariant data;
168
 
 
169
 
    if (index.isValid() && (index.row() < m_romList.size())) {
170
 
        const core::cache_entry* entry = m_romList[index.row()];
171
 
 
172
 
        if (role == Qt::DisplayRole || role == Sort) {
173
 
            char* buffer;
174
 
            switch(index.column()) {
175
 
                case Country:
176
 
                    data = countryName(entry->countrycode);
177
 
                    break;
178
 
                case GoodName:
179
 
                    data = QString(entry->inientry->goodname);
180
 
                    break;
181
 
                case Status:
182
 
                    data = entry->inientry->status;
183
 
                    break;
184
 
                case UserComments:
185
 
                    data = QString(entry->usercomments);
186
 
                    break;
187
 
                case FileName:
188
 
                    if (core::config_get_bool("RomBrowserShowFullPaths", FALSE)) {
189
 
                        data = entry->filename;
190
 
                    } else {
191
 
                        data = QFileInfo(entry->filename).fileName();
192
 
                    }
193
 
                    break;
194
 
                case MD5Hash:
195
 
                    int counter;
196
 
                    buffer = (char*)calloc(33,sizeof(char));
197
 
                    for ( counter = 0; counter < 16; ++counter )
198
 
                        std::sprintf(buffer+counter*2, "%02X", entry->md5[counter]);
199
 
                    data = QString(buffer);
200
 
                    free(buffer);
201
 
                    break;
202
 
                case InternalName:
203
 
                    data = QString(entry->internalname);
204
 
                    break;
205
 
                case CRC1:
206
 
                    data = QString().sprintf("%08X", entry->crc1);
207
 
                    break;
208
 
                case CRC2:
209
 
                    data = QString().sprintf("%08X", entry->crc2);
210
 
                    break;
211
 
                case SaveType:
212
 
                    buffer = (char*)calloc(16,sizeof(char));
213
 
                    core::savestring(entry->inientry->savetype, buffer);
214
 
                    data = QString(buffer);
215
 
                    free(buffer);
216
 
                    break;
217
 
                case Players:
218
 
                    buffer = (char*)calloc(16,sizeof(char));
219
 
                    core::playersstring(entry->inientry->players, buffer);
220
 
                    data = QString(buffer);
221
 
                    free(buffer);
222
 
                    break;
223
 
                case Size:
224
 
                    data = tr("%0 Mbit").arg((entry->romsize*8) / 1024 / 1024);
225
 
                    break;
226
 
                case CompressionType:
227
 
                    buffer = (char*)calloc(16,sizeof(char));
228
 
                    core::compressionstring(entry->compressiontype, buffer);
229
 
                    data = QString(buffer);
230
 
                    free(buffer);
231
 
                    break;
232
 
                case ImageType:
233
 
                    buffer = (char*)calloc(32,sizeof(char));
234
 
                    core::imagestring(entry->imagetype, buffer);
235
 
                    data = QString(buffer);
236
 
                    free(buffer);
237
 
                    break;
238
 
                case CICChip:
239
 
                    buffer = (char*)calloc(16,sizeof(char));
240
 
                    core::cicstring(entry->cic, buffer);
241
 
                    data = QString(buffer);
242
 
                    free(buffer);
243
 
                    break;
244
 
                case Rumble:
245
 
                    buffer = (char*)calloc(16,sizeof(char));
246
 
                    core::rumblestring(entry->inientry->rumble, buffer);
247
 
                    data = QString(buffer);
248
 
                    free(buffer);
249
 
                    break;
250
 
                default:
251
 
                    data = tr("Internal error");
252
 
                    break;
253
 
            }
254
 
        } else if (role == Qt::FontRole) {
255
 
            switch(index.column()) {
256
 
                case Size:
257
 
                case InternalName:
258
 
                case MD5Hash:
259
 
                case CRC1:
260
 
                case CRC2:
261
 
                    data = QFont("monospace");
262
 
                    break;
263
 
            }
264
 
        } else if (role == Qt::TextAlignmentRole) {
265
 
            switch(index.column()) {
266
 
                case Size:
267
 
                case MD5Hash:
268
 
                case CRC1:
269
 
                case CRC2:
270
 
                    data = Qt::AlignRight;
271
 
                    break;
272
 
            }
273
 
        } else if (role == Qt::DecorationRole) {
274
 
            switch(index.column()) {
275
 
                case Country:
276
 
                    data = countryFlag(entry->countrycode);
277
 
                    break;
278
 
                }
279
 
        //Assign Role enums here to retrive information.
280
 
        } else if (role == FullPath) {
281
 
            data = entry->filename;
282
 
        } else if (role == ArchiveFile) {
283
 
            data = entry->archivefile;
284
 
        }
285
 
    }
286
 
 
287
 
    return data;
288
 
}
289
 
 
290
 
bool RomModel::setData(const QModelIndex& index, const QVariant& value,
291
 
                        int role)
292
 
{
293
 
    bool result = false;
294
 
    if (index.isValid() &&
295
 
        index.row() >= 0 &&
296
 
        index.row() < m_romList.count() &&
297
 
        role == Qt::EditRole) {
298
 
        switch (index.column()) {
299
 
            case UserComments:
300
 
                result = true;
301
 
                qstrncpy(m_romList.at(index.row())->usercomments,
302
 
                         qPrintable(value.toString()),
303
 
                         COMMENT_MAXLENGTH);
304
 
                core::g_romcache.rcstask = core::RCS_WRITE_CACHE;
305
 
                emit dataChanged(index, index);
306
 
                break;
307
 
        }
308
 
    }
309
 
    return result;
310
 
}
311
 
 
312
 
QVariant RomModel::headerData(int section, Qt::Orientation orientation,
313
 
                               int role) const
314
 
{
315
 
    QVariant data;
316
 
    if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
317
 
        return QVariant();
318
 
 
319
 
    switch (section) {
320
 
        case Country: return tr("Country");
321
 
        case GoodName: return tr("Good Name");
322
 
        case Status: return tr("Status");
323
 
        case UserComments: return tr("User Comments");
324
 
        case FileName: return tr("File Name");
325
 
        case MD5Hash: return tr("MD5 Hash");
326
 
        case InternalName: return tr("Internal Name");
327
 
        case CRC1: return tr("CRC1");
328
 
        case CRC2: return tr("CRC2");
329
 
        case SaveType: return tr("Save Type");
330
 
        case Players: return tr("Players");
331
 
        case Size: return tr("Size");
332
 
        case CompressionType: return tr("Compression Type");
333
 
        case ImageType: return tr("Image Type");
334
 
        case CICChip: return tr("CIC Chip");
335
 
        case Rumble: return tr("Rumble");
336
 
        default: return QVariant();
337
 
    }
338
 
}
339
 
 
340
 
void RomModel::settingsChanged()
341
 
{
342
 
    if (romDirectories() != m_romDirectories) {
343
 
        m_romDirectories = romDirectories();
344
 
        core::g_romcache.rcstask = core::RCS_RESCAN;
345
 
    }
346
 
    if (m_showFullPath != core::config_get_bool("RomBrowserShowFullPaths", FALSE)) {
347
 
        m_showFullPath = core::config_get_bool("RomBrowserShowFullPaths", FALSE);
348
 
        emit dataChanged(
349
 
            index(0, FileName),
350
 
            index(columnCount() - 1, FileName)
351
 
        );
352
 
    }
353
 
}
354
 
 
355
 
QPixmap RomModel::countryFlag(QChar c) const
356
 
{
357
 
    if (!m_countryInfo.contains(c)) {
358
 
        c = '?';
359
 
    }
360
 
    return m_countryInfo[c].second;
361
 
}
362
 
 
363
 
QString RomModel::countryName(QChar c) const
364
 
{
365
 
    if (!m_countryInfo.contains(c)) {
366
 
        c = '?';
367
 
    }
368
 
    return m_countryInfo[c].first;
369
 
}
370