QModelIndex ChapterModel::createNewChapter(const QModelIndex& index) { ChapterFile *file = dynamic_cast<ChapterFile*>(priv::treeItem(index)); if( file == 0 ) { return QModelIndex(); } ChapterNode *sources = dynamic_cast<ChapterNode*>(file->parentItem()); if( sources == 0 || !sources->isSource() ) { return QModelIndex(); } const int count = index.row()+1; const QStringList files = sources->files(count); const QModelIndex parent = ChapterModel::parent(index); beginRemoveRows(parent, 0, count-1); sources->remove(count); endRemoveRows(); ChapterRoot *root = dynamic_cast<ChapterRoot*>(_root); beginInsertRows(QModelIndex(), sources->row(), sources->row()); ChapterNode *newNode = new ChapterNode(root); root->insert(newNode); newNode->setTitle(tr("New Chapter")); newNode->setFiles(files); endInsertRows(); return ChapterModel::index(newNode->row(), 0, QModelIndex()); }
QVariant ChapterModel::data(const QModelIndex& index, int role) const { if( !index.isValid() ) { return QVariant(); } csAbstractTreeItem *item = priv::treeItem(index); if( role == Qt::DisplayRole ) { ChapterNode *node = dynamic_cast<ChapterNode*>(priv::treeItem(index)); if( node != 0 && !node->isSource() ) { return chapterTitle(node); } else { return item->data(index.column()); } } else if( role == Qt::EditRole ) { if( isNode(item) ) { ChapterNode *node = dynamic_cast<ChapterNode*>(item); return node->title(); } } else if( role == Qt::DecorationRole ) { if( isFile(item) ) { ChapterFile *file = dynamic_cast<ChapterFile*>(item); if( file->fileName() == _playingFileName ) { return QColor(Qt::green); } } } return QVariant(); }
Jobs ChapterModel::buildJobs() const { Jobs jobs; // NOTE: Don't Build Job for <Files>!!! -> rowCount()-1 for(int cntNode = 0; cntNode < rowCount(QModelIndex())-1; cntNode++) { QModelIndex nodeIndex = index(cntNode, 0, QModelIndex()); Job job; for(int cntFile = 0; cntFile < rowCount(nodeIndex); cntFile++) { QModelIndex fileIndex = index(cntFile, 0, nodeIndex); ChapterFile *file = dynamic_cast<ChapterFile*>(priv::treeItem(fileIndex)); job.inputFiles.push_back(file->fileName()); } // File ChapterNode *node = dynamic_cast<ChapterNode*>(priv::treeItem(nodeIndex)); const QString title = chapterTitle(node); const QString outputDir = QFileInfo(job.inputFiles.front()).absolutePath(); job.outputFile = QFileInfo(QDir(outputDir), title+_L1(".mp3")).absoluteFilePath(); jobs.push_back(job); } // Node return jobs; }
QStringList ChapterNode::files(const int count) const { QStringList list; for(int i = 0; i < qMin(count, rowCount()); i++) { ChapterFile *file = dynamic_cast<ChapterFile*>(childItem(i)); list.push_back(file->fileName()); } return list; }
int ChapterNode::insertFiles(const QStringList& fileNames) { if( !_isSource ) { return 0; } QStringList names = fileNames; qSort(names); int cntInserted(0); for(int i = 0; i < names.size(); i++) { ChapterFile *file = new ChapterFile(this); insertChild(i, file); file->setFileName(names[i]); cntInserted++; } return cntInserted; }