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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 2009 Arthur Mello <arthur@mandriva.com>
0004     SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
0005     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "k3bvcdprojectmodel.h"
0011 
0012 #include "k3bvcddoc.h"
0013 #include "k3bvcdtrack.h"
0014 
0015 #include <KUrlMimeData>
0016 #include <KLocalizedString>
0017 
0018 #include <QDataStream>
0019 #include <QMimeData>
0020 #include <QUrl>
0021 
0022 namespace K3b {
0023 
0024 class VcdProjectModel::Private
0025 {
0026     public:
0027         Private( VcdProjectModel* parent )
0028             : doc( 0 ),
0029             q( parent ) { }
0030 
0031         VcdDoc* doc;
0032 
0033         void _k_aboutToAddRows(int pos, int count)
0034         {
0035             q->beginInsertRows(QModelIndex(), pos, pos + count - 1);
0036         }
0037 
0038         void _k_addedRows()
0039         {
0040             q->endInsertRows();
0041         }
0042 
0043         void _k_aboutToRemoveRows(int pos, int count)
0044         {
0045             q->beginRemoveRows(QModelIndex(), pos, pos + count - 1);
0046         }
0047 
0048         void _k_removedRows()
0049         {
0050             q->endRemoveRows();
0051         }
0052 
0053     private:
0054         VcdProjectModel* q;
0055 };
0056 
0057 
0058 VcdProjectModel::VcdProjectModel( VcdDoc* doc, QObject* parent )
0059     : QAbstractTableModel( parent ),
0060     d( new Private(this) )
0061 {
0062     d->doc = doc;
0063 
0064     connect( doc, SIGNAL(aboutToAddVCDTracks(int,int)),
0065              this, SLOT(_k_aboutToAddRows(int,int)), Qt::DirectConnection );
0066     connect( doc, SIGNAL(addedVCDTracks()),
0067              this, SLOT(_k_addedRows()), Qt::DirectConnection );
0068     connect( doc, SIGNAL(aboutToRemoveVCDTracks(int,int)),
0069              this, SLOT(_k_aboutToRemoveRows(int,int)), Qt::DirectConnection );
0070     connect( doc, SIGNAL(removedVCDTracks()),
0071              this, SLOT(_k_removedRows()), Qt::DirectConnection );
0072 }
0073 
0074 
0075 VcdProjectModel::~VcdProjectModel()
0076 {
0077     delete d;
0078 }
0079 
0080 
0081 VcdDoc* VcdProjectModel::doc() const
0082 {
0083     return d->doc;
0084 }
0085 
0086 
0087 VcdTrack* VcdProjectModel::trackForIndex( const QModelIndex& index ) const
0088 {
0089     if( index.isValid() && index.row() >= 0 && index.row() < d->doc->numOfTracks() )
0090         return d->doc->at( index.row() );
0091     else
0092         return 0;
0093 }
0094 
0095 
0096 QModelIndex VcdProjectModel::indexForTrack( VcdTrack* track, int column ) const
0097 {
0098     if( track != 0 && column >= 0 && column < NumColumns )
0099         return createIndex( track->index(), column, track );
0100     else
0101         return QModelIndex();
0102 }
0103 
0104 
0105 int VcdProjectModel::rowCount( const QModelIndex& parent) const
0106 {
0107     if( parent.isValid() )
0108         return 0;
0109     else
0110         return d->doc->numOfTracks();
0111 }
0112 
0113 
0114 int VcdProjectModel::columnCount( const QModelIndex& /*parent*/) const
0115 {
0116     return NumColumns;
0117 }
0118 
0119 
0120 bool VcdProjectModel::setData( const QModelIndex& index, const QVariant& value, int role )
0121 {
0122     if ( index.isValid() )
0123     {
0124         VcdTrack* track = trackForIndex( index );
0125         if ( role == Qt::EditRole )
0126         {
0127             if ( index.column() == TitleColumn )
0128             {
0129                 track->setTitle( value.toString() );
0130                 return true;
0131             }
0132         }
0133     }
0134 
0135     return false;
0136 }
0137 
0138 
0139 QVariant VcdProjectModel::data( const QModelIndex& index, int role ) const
0140 {
0141     if ( index.isValid() ) {
0142         VcdTrack* track = trackForIndex( index );
0143 
0144         switch( index.column() ) {
0145             case NoColumn:
0146                 if( role == Qt::DisplayRole ||
0147                     role == Qt::EditRole )
0148                 {
0149                     return track->index() + 1;
0150                 }
0151                 break;
0152             case TitleColumn:
0153                 if( role == Qt::DisplayRole ||
0154                     role == Qt::EditRole )
0155                 {
0156                     return track->title();
0157                 }
0158                 break;
0159             case TypeColumn:
0160                 if( role == Qt::DisplayRole ||
0161                     role == Qt::EditRole )
0162                 {
0163                     return track->mpegTypeS();
0164                 }
0165                 break;
0166             case ResolutionColumn:
0167                 if( role == Qt::DisplayRole ||
0168                     role == Qt::EditRole )
0169                 {
0170                     return track->resolution();
0171                 }
0172                 break;
0173             case HighResolutionColumn:
0174                 if( role == Qt::DisplayRole ||
0175                     role == Qt::EditRole )
0176                 {
0177                     return track->highresolution();
0178                 }
0179                 break;
0180             case FrameRateColumn:
0181                 if( role == Qt::DisplayRole ||
0182                     role == Qt::EditRole )
0183                 {
0184                     return track->video_frate();
0185                 }
0186                 break;
0187             case MuxRateColumn:
0188                 if( role == Qt::DisplayRole ||
0189                     role == Qt::EditRole )
0190                 {
0191                     return QString::number( track->muxrate() );
0192                 }
0193                 break;
0194             case DurationColumn:
0195                 if( role == Qt::DisplayRole ||
0196                     role == Qt::EditRole )
0197                 {
0198                     return track->duration();
0199                 }
0200                 break;
0201             case SizeColumn:
0202                 if( role == Qt::DisplayRole ||
0203                     role == Qt::EditRole )
0204                 {
0205                     return KIO::convertSize( track->size() );
0206                 }
0207                 break;
0208             case FilenameColumn:
0209                 if( role == Qt::DisplayRole ||
0210                     role == Qt::EditRole )
0211                 {
0212                     return track->fileName();
0213                 }
0214                 break;
0215         }
0216     }
0217 
0218     return QVariant();
0219 }
0220 
0221 
0222 QVariant VcdProjectModel::headerData( int section, Qt::Orientation orientation, int role ) const
0223 {
0224     if( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
0225         switch( section ) {
0226             case NoColumn:
0227                 return i18nc( "Video CD Track Number", "No." );
0228             case TitleColumn:
0229                 return i18nc( "Video CD Track Title", "Title" );
0230             case TypeColumn:
0231                 return i18nc( "Video CD Track Type (ie. MPEG1)", "Type" );
0232             case ResolutionColumn:
0233                 return i18nc( "Video CD Track Resolution", "Resolution" );
0234             case HighResolutionColumn:
0235                 return i18nc(   "Video CD Track High Resolution",
0236                                 "High Resolution" );
0237             case FrameRateColumn:
0238                 return i18nc( "Video CD Track Framerate", "Framerate" );
0239             case MuxRateColumn:
0240                 return i18nc( "Video CD Track Muxrate", "Muxrate" );
0241             case DurationColumn:
0242                 return i18nc( "Video CD Track Duration", "Duration" );
0243             case SizeColumn:
0244                 return i18nc( "Video CD Track File Size", "File Size" );
0245             case FilenameColumn:
0246                 return i18nc( "Video CD Track Filename", "Filename" );
0247             default:
0248                 return QVariant();
0249         }
0250     }
0251     else {
0252         return QVariant();
0253     }
0254 }
0255 
0256 
0257 Qt::ItemFlags VcdProjectModel::flags( const QModelIndex& index ) const
0258 {
0259     if( index.isValid() )
0260     {
0261         Qt::ItemFlags f = Qt::ItemIsSelectable |
0262                           Qt::ItemIsEnabled |
0263                           Qt::ItemIsDropEnabled |
0264                           Qt::ItemIsDragEnabled;
0265 
0266         if( index.column() == TitleColumn )
0267         {
0268             f |= Qt::ItemIsEditable;
0269         }
0270 
0271         return f;
0272     }
0273     else
0274     {
0275         return QAbstractItemModel::flags( index )|Qt::ItemIsDropEnabled;
0276     }
0277 }
0278 
0279 
0280 Qt::DropActions VcdProjectModel::supportedDragActions() const
0281 {
0282     return Qt::MoveAction;
0283 }
0284 
0285 
0286 Qt::DropActions VcdProjectModel::supportedDropActions() const
0287 {
0288     return Qt::CopyAction | Qt::MoveAction;
0289 }
0290 
0291 
0292 QMimeData* VcdProjectModel::mimeData( const QModelIndexList& indexes ) const
0293 {
0294     QMimeData* mime = new QMimeData();
0295 
0296     QList<VcdTrack*> tracks;
0297     QList<QUrl> urls;
0298 
0299     foreach( const QModelIndex& index, indexes ) {
0300         VcdTrack* track = trackForIndex( index );
0301         tracks << track;
0302 
0303         if( !urls.contains( QUrl::fromLocalFile( track->absolutePath() ) ) )
0304         {
0305             urls << QUrl::fromLocalFile( track->absolutePath() );
0306         }
0307     }
0308     mime->setUrls(urls);
0309 
0310     // the easy road: encode the pointers
0311     QByteArray trackData;
0312     QDataStream trackDataStream( &trackData, QIODevice::WriteOnly );
0313 
0314     foreach( VcdTrack* track, tracks ) {
0315         trackDataStream << ( qint64 )track;
0316     }
0317 
0318     mime->setData( "application/x-k3bvcdtrack", trackData );
0319 
0320     return mime;
0321 }
0322 
0323 
0324 QStringList VcdProjectModel::mimeTypes() const
0325 {
0326     QStringList s = KUrlMimeData::mimeDataTypes();
0327 
0328     s += QString::fromLatin1( "application/x-k3bvcdtrack" );
0329 
0330     return s;
0331 }
0332 
0333 
0334 bool VcdProjectModel::dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int /*column*/, const QModelIndex& parent )
0335 {
0336     if( action == Qt::IgnoreAction ) {
0337         return true;
0338     }
0339     else if( data->hasFormat( "application/x-k3bvcdtrack" ) ) {
0340         VcdTrack* before;
0341         if( parent.isValid() )
0342             row = parent.row();
0343 
0344         if( row >= 0 && row < d->doc->numOfTracks() )
0345             before = d->doc->at( row );
0346         else
0347             before = 0;
0348 
0349         QByteArray trackData = data->data( "application/x-k3bvcdtrack" );
0350         QDataStream trackDataStream( trackData );
0351         while ( !trackDataStream.atEnd() )
0352         {
0353             qint64 p;
0354             trackDataStream >> p;
0355             VcdTrack* track = reinterpret_cast< VcdTrack* >( p );
0356             d->doc->moveTrack( track, before );
0357         }
0358 
0359         return true;
0360     }
0361     else if( data->hasUrls() ) {
0362         int pos;
0363         if( parent.isValid() )
0364             row = parent.row();
0365 
0366         if( row >= 0 )
0367             pos = row;
0368         else
0369             pos = d->doc->numOfTracks();
0370 
0371         QList<QUrl> urls = KUrlMimeData::urlsFromMimeData( data );
0372         d->doc->addTracks( urls, pos );
0373 
0374         return true;
0375     }
0376     else {
0377         return false;
0378     }
0379 }
0380 
0381 
0382 bool VcdProjectModel::removeRows( int row, int count, const QModelIndex& parent )
0383 {
0384     // remove the indexes from the project
0385     while (count > 0)
0386     {
0387         QModelIndex i = index( row, 0, parent );
0388         d->doc->removeTrack( trackForIndex(i) );
0389 
0390         row++;
0391         count--;
0392     }
0393 
0394     return true;
0395 }
0396 
0397 } // namespace K3b
0398 
0399 #include "moc_k3bvcdprojectmodel.cpp"