libpsxsave/qt-gui/psxmemcard.cpp
2010-08-10 09:40:01 -04:00

108 lines
3.3 KiB
C++

#include "psxmemcard.h"
#include "psxicon.h"
#include <QTextCodec>
#include <iostream>
using namespace std;
PSXMemoryCardModel::PSXMemoryCardModel( MemoryCard *memCard, QObject *parent ) : QAbstractTableModel( parent ) {
this->memCard = memCard;
for( int i = 1; i < 16; i++ ) {
DirectoryFrame *directory = memCard->directory()->Directory( i - 1 );
int type = directory->blockType();
if( type == Block::PSX_BLOCK_INITIAL ) {
this->blocks.append( memCard->getBlock( i ) );
this->blockId.append( i );
}
}
}
int PSXMemoryCardModel::rowCount( const QModelIndex &parent ) const {
if( parent.isValid() ) { return 0; }
return this->blocks.count();
}
int PSXMemoryCardModel::columnCount( const QModelIndex &parent ) const {
if( parent.isValid() ) { return 0; }
return 5;
}
QVariant PSXMemoryCardModel::data( const QModelIndex & index, int role ) const {
if( !index.isValid() ) { return QVariant(); }
InitialBlock *block = (InitialBlock*)blocks[index.row()];
switch( index.column() ) {
case 0:
if( role != Qt::DisplayRole ) { return QVariant(); }
return blockId[ index.row() ];
case 1:
if( role == Qt::DisplayRole ) {
QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
if( codec == 0 ) { return QVariant(); }
return codec->toUnicode( block->Header()->SJISSaveTitle() );
} else if ( role == Qt::DecorationRole ) {
Icon *icon = block->getIcon( 0 );
QImage image( 16, 16, QImage::Format_Indexed8 );
image.setNumColors( 16 );
for( int i = 0; i < 16; i++ ) {
RGB col = icon->paletteColor( i );
QRgb rgb = QColor::fromRgb( col.r, col.g, col.b ).rgb();
image.setColor( i, rgb );
}
for( int x = 0; x < 16; x++ ) {
for( int y = 0; y < 16; y++ ) {
image.setPixel( x, y, icon->pixel( x, y ) );
}
}
return QPixmap::fromImage( image );
} else {
return QVariant();
}
break;
case 2:
if( role == Qt::DisplayRole ) {
return QString( this->memCard->directory()->Directory( blockId[ index.row() ] - 1 )->licenseCode() );
} else if ( role == Qt::DecorationRole ) {
QString image = QString( this->memCard->directory()->Directory( blockId[ index.row() ] - 1 )->territoryCode() );
image.append( ".png" );
return QPixmap::fromImage( QImage( image ) );
} else {
return QVariant();
}
case 3:
if( role != Qt::DisplayRole ) { return QVariant(); }
return QString( this->memCard->directory()->Directory( blockId[ index.row() ] - 1 )->saveCode() );
case 4:
if( role != Qt::DisplayRole ) { return QVariant(); }
return ( (InitialBlock*)blocks[ index.row() ] )->Header()->blocksUsed();
}
return QVariant();
}
QVariant PSXMemoryCardModel::headerData( int section, Qt::Orientation orientation, int role ) const {
if( role != Qt::DisplayRole ) { return QVariant(); }
switch( section ) {
case 0:
return tr( "Id" );
case 1:
return tr( "Save Title" );
case 2:
return tr( "License Code" );
case 3:
return tr( "Save Code" );
case 4:
return tr( "Blocks" );
default:
return QVariant();
}
}
QString unicodeToAscii( QString text ) {
for( int i = 0xff01; i < 0xff5f; i++ ) {
text.replace( QChar( i ), QChar( i - 0xff00 + 0x0020 ) );
}
for( int i = 0; i < text.length(); i++ ) {
if( text[i].unicode() > 0x7f ) { text[i] = 0x5f; }
}
return text.toAscii();
}