File indexing completed on 2024-05-12 04:51:51
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 "k3bvideodvdaudiomodel.h" 0009 #include "k3bvideodvd.h" 0010 #include "k3bvideodvdtitle.h" 0011 0012 #include <KLocalizedString> 0013 0014 #include <QHash> 0015 #include <QLocale> 0016 #include <QSize> 0017 0018 namespace K3b { 0019 0020 class VideoDVDAudioModel::Private 0021 { 0022 public: 0023 Private( const VideoDVD::VideoDVD& d, const QList<int>& t ) 0024 : dvd( d ), titles( t ) {} 0025 0026 VideoDVD::VideoDVD dvd; 0027 QList<int> titles; 0028 QHash< const VideoDVD::AudioStream*, const VideoDVD::Title* > parents; 0029 QHash< const VideoDVD::Title*, QSize > videoSizes; 0030 QHash< const VideoDVD::Title*, KIO::filesize_t > fileSizes; 0031 QHash< const VideoDVD::Title*, QString > fileNames; 0032 QHash< const VideoDVD::Title*, int > chosenAudio; 0033 }; 0034 0035 0036 VideoDVDAudioModel::VideoDVDAudioModel( const VideoDVD::VideoDVD& dvd, const QList<int>& titles, QObject* parent ) 0037 : QAbstractItemModel( parent ), 0038 d( new Private( dvd, titles ) ) 0039 { 0040 Q_FOREACH( int titleNum, titles ) { 0041 if( titleNum > 0 && titleNum <= static_cast<int>( d->dvd.numTitles() ) ) { 0042 const VideoDVD::Title& title = d->dvd[ titleNum-1 ]; 0043 0044 d->videoSizes.insert( &title, QSize( title.videoStream().realPictureWidth(), 0045 title.videoStream().realPictureHeight() ) ); 0046 d->fileSizes.insert( &title, 0 ); 0047 d->fileNames.insert( &title, QString() ); 0048 d->chosenAudio.insert( &title, 0 ); 0049 0050 for( int i = 0; i < static_cast<int>( title.numAudioStreams() ); ++i ) { 0051 const VideoDVD::AudioStream& audio = title.audioStream( i ); 0052 d->parents.insert( &audio, &title ); 0053 if( QLocale( audio.langCode() ).language() == QLocale().language() && 0054 audio.format() != K3b::VideoDVD::AUDIO_FORMAT_DTS ) { 0055 d->chosenAudio[ &title ] = i; 0056 } 0057 } 0058 } 0059 } 0060 } 0061 0062 0063 VideoDVDAudioModel::~VideoDVDAudioModel() 0064 { 0065 delete d; 0066 } 0067 0068 0069 const VideoDVD::Title* VideoDVDAudioModel::titleForIndex( const QModelIndex& index ) const 0070 { 0071 if( index.isValid() && !index.internalPointer() && index.row() >= 0 && index.row() < d->titles.size() ) 0072 { 0073 const int title = d->titles.at( index.row() ) - 1; 0074 if( title >= 0 && title < static_cast<int>( d->dvd.numTitles() ) ) 0075 return &d->dvd[ title ]; 0076 } 0077 return 0; 0078 } 0079 0080 0081 QModelIndex VideoDVDAudioModel::indexForTitle( const VideoDVD::Title& title, int column ) const 0082 { 0083 int row = d->titles.indexOf( title.titleNumber() ); 0084 if( row >= 0 ) 0085 return createIndex( row, column, nullptr ); 0086 else 0087 return QModelIndex(); 0088 } 0089 0090 0091 const VideoDVD::AudioStream* VideoDVDAudioModel::audioForIndex( const QModelIndex& index ) const 0092 { 0093 if( index.isValid() && index.internalPointer() ) 0094 return static_cast<VideoDVD::AudioStream*>( index.internalPointer() ); 0095 else 0096 return 0; 0097 } 0098 0099 0100 QModelIndex VideoDVDAudioModel::indexForAudio( const VideoDVD::AudioStream& audio, int column ) const 0101 { 0102 if( const VideoDVD::Title* title = d->parents[ &audio ] ) { 0103 for( int i = 0; i < static_cast<int>( title->numAudioStreams() ); ++i ) { 0104 if( &title->audioStream( i ) == &audio ) { 0105 return createIndex( i, column, const_cast<void*>( reinterpret_cast<const void*>( &audio ) ) ); 0106 } 0107 } 0108 } 0109 return QModelIndex(); 0110 } 0111 0112 0113 void VideoDVDAudioModel::setVideoSize( const VideoDVD::Title& title, const QSize& videoSize ) 0114 { 0115 d->videoSizes[ &title ] = videoSize; 0116 QModelIndex index = indexForTitle( title, VideoSizeColumn ); 0117 Q_EMIT dataChanged( index, index ); 0118 } 0119 0120 0121 void VideoDVDAudioModel::setFileSize( const VideoDVD::Title& title, KIO::filesize_t fileSize ) 0122 { 0123 d->fileSizes[ &title ] = fileSize; 0124 QModelIndex index = indexForTitle( title, FileSizeColumn ); 0125 Q_EMIT dataChanged( index, index ); 0126 } 0127 0128 0129 void VideoDVDAudioModel::setFileName( const VideoDVD::Title& title, const QString& fileName ) 0130 { 0131 d->fileNames[ &title ] = fileName; 0132 QModelIndex index = indexForTitle( title, FileNameColumn ); 0133 Q_EMIT dataChanged( index, index ); 0134 } 0135 0136 0137 int VideoDVDAudioModel::chosenAudio( const VideoDVD::Title& title ) const 0138 { 0139 return d->chosenAudio[ &title ]; 0140 } 0141 0142 0143 QVariant VideoDVDAudioModel::data( const QModelIndex& index, int role ) const 0144 { 0145 if( const VideoDVD::Title* title = titleForIndex( index ) ) { 0146 if( Qt::DisplayRole == role ) { 0147 switch( index.column() ) { 0148 case TitleColumn: 0149 return i18n("Title %1 (%2)", 0150 title->titleNumber(), 0151 title->playbackTime().toString() ); 0152 case VideoSizeColumn: 0153 return QString("%1x%2") 0154 .arg( d->videoSizes[ title ].width() ) 0155 .arg( d->videoSizes[ title ].height() ); 0156 case FileSizeColumn: 0157 return KIO::convertSize( d->fileSizes[ title ] ); 0158 case FileNameColumn: 0159 return d->fileNames[ title ]; 0160 default: 0161 break; 0162 } 0163 } 0164 else if( Qt::ToolTipRole == role && index.column() == FileNameColumn ) { 0165 return d->fileNames[ title ]; 0166 } 0167 } 0168 else if( const VideoDVD::AudioStream* audio = audioForIndex( index ) ) { 0169 if( Qt::DisplayRole == role && index.column() == TitleColumn ) { 0170 QString text = i18n("%1 %2Ch (%3%4)", 0171 K3b::VideoDVD::audioFormatString( audio->format() ), 0172 audio->channels(), 0173 ( audio->langCode().isEmpty() 0174 ? i18n("unknown language") 0175 : QLocale( audio->langCode() ).nativeLanguageName() ), 0176 ( audio->codeExtension() != K3b::VideoDVD::AUDIO_CODE_EXT_UNSPECIFIED 0177 ? QString(" ") + K3b::VideoDVD::audioCodeExtensionString( audio->codeExtension() ) 0178 : QString() ) ); 0179 0180 if( audio->format() == K3b::VideoDVD::AUDIO_FORMAT_DTS ) 0181 return i18n( "%1 (not supported)", text ); 0182 else 0183 return text; 0184 } 0185 else if( Qt::CheckStateRole == role && index.column() == TitleColumn ) { 0186 if( d->chosenAudio[ d->parents[ audio ] ] == index.row() ) 0187 return Qt::Checked; 0188 else 0189 return Qt::Unchecked; 0190 } 0191 } 0192 return QVariant(); 0193 } 0194 0195 0196 int VideoDVDAudioModel::columnCount( const QModelIndex& /*parent*/ ) const 0197 { 0198 return NumColumns; 0199 } 0200 0201 0202 int VideoDVDAudioModel::rowCount( const QModelIndex& parent ) const 0203 { 0204 if( !parent.isValid() ) 0205 return d->titles.count(); 0206 else if( const VideoDVD::Title* title = titleForIndex( parent ) ) 0207 return title->numAudioStreams(); 0208 else 0209 return 0; 0210 } 0211 0212 0213 QModelIndex VideoDVDAudioModel::parent( const QModelIndex& child ) const 0214 { 0215 if( const VideoDVD::AudioStream* audio = audioForIndex( child ) ) { 0216 if( const VideoDVD::Title* title = d->parents[ audio ] ) { 0217 return indexForTitle( *title ); 0218 } 0219 } 0220 return QModelIndex(); 0221 } 0222 0223 0224 QModelIndex VideoDVDAudioModel::index( int row, int column, const QModelIndex& parent ) const 0225 { 0226 if( !hasIndex( row, column, parent ) ) { 0227 return QModelIndex(); 0228 } 0229 else if( !parent.isValid() ) { 0230 if( row >= 0 && row < d->titles.size() ) 0231 return createIndex( row, column, nullptr ); 0232 else 0233 return QModelIndex(); 0234 } 0235 else if( const VideoDVD::Title* title = titleForIndex( parent ) ) { 0236 if( row >= 0 && row < static_cast<int>( title->numAudioStreams() ) ) 0237 return createIndex( row, column, const_cast<void*>( reinterpret_cast<const void*>( &title->audioStream( row ) ) ) ); 0238 else 0239 return QModelIndex(); 0240 } 0241 else { 0242 return QModelIndex(); 0243 } 0244 } 0245 0246 0247 QVariant VideoDVDAudioModel::headerData( int section, Qt::Orientation orientation, int role ) const 0248 { 0249 if( orientation == Qt::Horizontal && Qt::DisplayRole == role ) { 0250 switch( section ) { 0251 case TitleColumn: return i18n("Title"); 0252 case VideoSizeColumn: return i18n("Video Size"); 0253 case FileSizeColumn: return i18n("File Size"); 0254 case FileNameColumn: return i18n("Filename"); 0255 default: break; 0256 } 0257 } 0258 return QVariant(); 0259 } 0260 0261 0262 bool VideoDVDAudioModel::setData( const QModelIndex& index, const QVariant& value, int role ) 0263 { 0264 if( role == Qt::CheckStateRole && value.toBool() ) { 0265 if( const VideoDVD::AudioStream* audio = audioForIndex( index ) ) { 0266 if( const VideoDVD::Title* title = d->parents[ audio ] ) { 0267 d->chosenAudio[ title ] = index.row(); 0268 QModelIndex titleIndex = indexForTitle( *title ); 0269 Q_EMIT dataChanged( VideoDVDAudioModel::index( 0, TitleColumn, titleIndex ), 0270 VideoDVDAudioModel::index( rowCount( titleIndex )-1, TitleColumn, titleIndex ) ); 0271 } 0272 } 0273 } 0274 return false; 0275 } 0276 0277 0278 Qt::ItemFlags VideoDVDAudioModel::flags( const QModelIndex& index ) const 0279 { 0280 if( audioForIndex( index ) != 0 ) 0281 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; 0282 else if( titleForIndex( index ) != 0 ) 0283 return Qt::ItemIsEnabled; 0284 else 0285 return Qt::NoItemFlags; 0286 } 0287 0288 } // namespace K3b 0289 0290 #include "moc_k3bvideodvdaudiomodel.cpp"