QModelIndex DirModel::index(int row, int col,
                        const QModelIndex& parent) const
{
  if(col < 0 || row < 0)
    return QModelIndex();
  if(col >= columnCount(parent) || row >= rowCount(parent))
    return QModelIndex();

  if(!parent.isValid() )
  {
    assert(row == 0 && col == 0);
    return createIndex(row, col, _rootFolder);
  }

  MTP::GenericObject* temp = (MTP::GenericObject*)parent.internalPointer();

  if (temp->Type() != MtpFolder)
    assert(false);

  MTP::Folder* folder = (MTP::Folder*)temp;
  int total = (int) folder->FolderCount() + folder->FileCount();
  if (row >= (int) total)
  {
    qDebug() << "Requested row is out of bounds- not enough children!";
    return QModelIndex();
  }

  if (row < (int) folder->FolderCount() )
  {
    MTP::Folder* ret = folder->ChildFolder(row);
    assert(ret);
    return createIndex(row, col, ret);
  }
  int idx = row - folder->FolderCount();
  assert(idx < (int)folder->FileCount() && idx >= 0);
  MTP::File* ret = folder->ChildFile(idx);
  assert(ret);
  return createIndex(row, col, ret);
}
QVariant DirModel::data(const QModelIndex& index, int role ) const
{
  if (!index.isValid())
    return QVariant();
  if (index.internalPointer() == NULL)
    return QVariant();
  if (role == Qt::DisplayRole)
  {
    MTP::GenericObject* temp = (MTP::GenericObject*) index.internalPointer();
    if (temp->Type() == MtpFolder && index.column() == 0)
    {
        MTP::Folder* tempFolder = (MTP::Folder*)temp;
        return QString::fromUtf8(tempFolder->Name());
    }
    else if (temp->Type() == MtpFile && index.column() == 0)
    {
        MTP::File* tempFile = (MTP::File*)temp;
        QString temp = QString::fromUtf8(tempFile->Name());
        return temp;
    }
    return QVariant();
  }

  if (role == Qt::DecorationRole)
  {
    MTP::GenericObject* temp = (MTP::GenericObject*) index.internalPointer();
    if (temp->Type() == MtpFolder && index.column() == 0)
    {
      QPixmap ret(":/pixmaps/folder.png");
      return ret.scaledToWidth(24, Qt::SmoothTransformation);
    }
    else if (temp->Type() == MtpFile && index.column() == 0)
    {
      QPixmap ret;
      /* TODO SampleData for MTP::Files
      MTP::File* tempFile = (MTP::File*)temp;
      LIBMTP_filesampledata_t sample = tempFile->SampleData();
      if (sample.filetype == LIBMTP_FILETYPE_UNKNOWN && (sample.size > 0 && sample.data) )
      {
          ret.loadFromData( (const uchar*)sample.data, sample.size);
          return ret.scaledToWidth(24, Qt::SmoothTransformation);
      }
      */
      ret.load(":/pixmaps/file.png");
      return ret.scaledToWidth(24, Qt::SmoothTransformation);
    }
    return QVariant();
  }

  if (role == Qt::FontRole)
  {
    MTP::GenericObject* temp = (MTP::GenericObject*) index.internalPointer();
    //Its an album
    if (temp->Type() == MtpFolder && index.column() == 0)
    {
      QFont temp;
      temp.setBold(true);
      temp.setPointSize(8);
      return temp;
    }
  }
  return QVariant();
}
Exemple #3
0
/**
 * Returns the data to display at the given index and the role
 * @param index the index of the item to display
 * @param role the role this data will be used for
 */
QVariant AlbumModel::data(const QModelIndex& index, int role ) const
{
  if (!index.isValid())
    return QVariant();
  if (role == Qt::DisplayRole)
  {
    MTP::GenericObject* temp = (MTP::GenericObject*) index.internalPointer();
    if (temp->Type() == MtpAlbum && index.column() == 0)
    {
        MTP::Album* tempAlbum = (MTP::Album*)temp;
        QString first = QString::fromUtf8(tempAlbum->Name());
        return QVariant(first);
    }
    else if (temp->Type() == MtpTrack && index.column() == 0)
    {
        MTP::Track* tempTrack = (MTP::Track*)temp;
        QString temp = QString::fromUtf8(tempTrack->Name());
        return QVariant(temp);
    }
    return QVariant();
  }
  if (role == Qt::DecorationRole)
  {
    MTP::GenericObject* temp = (MTP::GenericObject*) index.internalPointer();
    if (temp->Type() == MtpAlbum && index.column() == 0)
    {
      MTP::Album* tempAlbum = (MTP::Album*)temp;
      LIBMTP_filesampledata_t sample = tempAlbum->SampleData();
      if (sample.size > 0 && sample.data )
        /*&&  interesting bug on some devices
          (sample.filetype == LIBMTP_FILETYPE_JPEG ||
          sample.filetype == LIBMTP_FILETYPE_JPX ||
          sample.filetype == LIBMTP_FILETYPE_JP2))
          */
      {
        QPixmap ret;
        ret.loadFromData( (const uchar*)sample.data, sample.size);
        //qDebug() << "album decoration found:" << sample.filetype  << " with size: " << sample.size;

        //TODO uncomment when CommandLineOpts is moved to singleton
        //qDebug()  << "Actual sample found in simulate mode!";
        return ret.scaledToWidth(24, Qt::SmoothTransformation);
      }
      else
      {
        QPixmap ret("pixmaps/AlbumIcon.png");
        return ret.scaledToWidth(24, Qt::SmoothTransformation);
      }
    }

    else if (temp->Type() == MtpTrack && index.column() == 0)
    {
        return QIcon(QPixmap (":/pixmaps/Track.png"));
    }
    else
      return QVariant();
  }
  if (role == Qt::SizeHintRole)
  {
    MTP::GenericObject* temp = (MTP::GenericObject*) index.internalPointer();
    if (temp->Type() == MtpAlbum && index.column() == 0)
      return QSize(28, 28);
  }
  if (role == Qt::FontRole)
  {
    MTP::GenericObject* temp = (MTP::GenericObject*) index.internalPointer();
    //Its an album
    if (temp->Type() == MtpAlbum && index.column() == 0)
    {
      QFont temp;
      temp.setBold(true);
      temp.setPointSize(8);
      return temp;
    }
  }
  return QVariant();
}