File indexing completed on 2024-05-05 04:51:43

0001 /*
0002     SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
0003     SPDX-FileCopyrightText: 1998-2010 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bbootimagemodel.h"
0009 #include "k3bdatadoc.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 namespace K3b {
0014 
0015 BootImageModel::BootImageModel( DataDoc* doc, QObject* parent )
0016     : QAbstractTableModel( parent ),
0017       m_doc( doc )
0018 {
0019 }
0020 
0021 
0022 BootImageModel::~BootImageModel()
0023 {
0024 }
0025 
0026 
0027 BootItem* BootImageModel::bootItemForIndex( const QModelIndex& index ) const
0028 {
0029     if( index.isValid() && index.row() >= 0 && index.row() < m_doc->bootImages().size() )
0030         return m_doc->bootImages().at( index.row() );
0031     else
0032         return 0;
0033 }
0034 
0035 
0036 QModelIndex BootImageModel::indexForBootItem( BootItem* bootItem, int column ) const
0037 {
0038     int i = m_doc->bootImages().indexOf( bootItem );
0039     if( i >= 0 && i < m_doc->bootImages().size() )
0040         return index( i, column );
0041     else
0042         return QModelIndex();
0043 }
0044 
0045 
0046 void BootImageModel::createBootItem( const QString& file, BootItem::ImageType imageType )
0047 {
0048     if( !file.isEmpty() ) {
0049         int row = m_doc->bootImages().size();
0050         beginInsertRows( QModelIndex(), row, row );
0051         m_doc->createBootItem( file )->setImageType( imageType );
0052         endInsertRows();
0053     }
0054 }
0055 
0056 
0057 void BootImageModel::setImageType( const QModelIndex& index, BootItem::ImageType imageType )
0058 {
0059     if( BootItem* bootItem = bootItemForIndex( index ) ) {
0060         bootItem->setImageType( imageType );
0061         QModelIndex changedIndex = sibling( index.row(), EmulationTypeColumn, index );
0062         Q_EMIT dataChanged( changedIndex, changedIndex );
0063     }
0064 }
0065 
0066 
0067 QVariant BootImageModel::data( const QModelIndex& index, int role ) const
0068 {
0069     if( BootItem* bootItem = bootItemForIndex( index ) ) {
0070         if( Qt::DisplayRole == role ) {
0071             switch( index.column() ) {
0072                 case EmulationTypeColumn:
0073                     switch( bootItem->imageType() ) {
0074                         case BootItem::FLOPPY:   return i18n("Floppy");
0075                         case BootItem::HARDDISK: return i18n("Harddisk");
0076                         default:                 return i18n("None");
0077                     }
0078                     
0079                 case SizeColumn:
0080                     return QString( "%1 KB" ).arg( bootItem->size()/1024 );
0081                     
0082                 case LocalPathColumn:
0083                     return bootItem->localPath();
0084                     
0085                 default:
0086                     break;
0087             }
0088         }
0089     }
0090     return QVariant();
0091 }
0092 
0093 
0094 int BootImageModel::columnCount( const QModelIndex& /*parent*/ ) const
0095 {
0096     return NumColumns;
0097 }
0098 
0099 
0100 int BootImageModel::rowCount( const QModelIndex& parent ) const
0101 {
0102     if( parent.isValid() )
0103         return 0;
0104     else
0105         return m_doc->bootImages().count();
0106 }
0107 
0108 
0109 QVariant BootImageModel::headerData( int section, Qt::Orientation orientation, int role ) const
0110 {
0111     if( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
0112         switch( section ) {
0113             case EmulationTypeColumn: return i18n( "Emulation Type" );
0114             case SizeColumn: return i18n( "Size" );
0115             case LocalPathColumn: return i18n( "Local Path" );
0116             default: return QVariant();
0117         }
0118     }
0119     return QVariant();
0120 }
0121 
0122 
0123 bool BootImageModel::removeRows( int row, int count, const QModelIndex& parent )
0124 {
0125     if( !parent.isValid() ) {
0126         beginRemoveRows( parent, row, row+count-1 );
0127         for( int i = 0; i < count; ++i ) {
0128             if( row >= 0 && row < m_doc->bootImages().size() ) {
0129                 BootItem* item = m_doc->bootImages().takeAt( i );
0130                 delete item;
0131             }
0132         }
0133         endRemoveRows();
0134         return true;
0135     }
0136     else {
0137         return false;
0138     }
0139 }
0140 
0141 } // namespace K3b
0142 
0143 #include "moc_k3bbootimagemodel.cpp"