/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. */ #include #include #include #include #include #include #include #include #include #include #include #include "archiveviewwidget.h" #include "archivefileview.h" #include "moviemodel.h" #include "listmodel.h" #include "archiveproxy.h" #include "editarchiveitemdialog.h" #include "sizedelegate.h" #include "archiveddelegate.h" #include "coverarchiveeditor.h" #include "textenterdialog.h" #include "helper.h" ArchiveViewWidget::ArchiveViewWidget(MovieModel *model, ListModel *genre, ListModel *actors, QWidget *parent) : QWidget(parent), mMovieModel(model), mGenreModel(genre), mActorsModel(actors){ //filter bar QHBoxLayout *filterLayout = new QHBoxLayout; QLabel *l1 = new QLabel(tr("Filter by &genre")); mGenre = new QComboBox; l1->setBuddy(mGenre); mGenre->setModel(mGenreModel); connect(mGenre, SIGNAL(activated(const QString &)), this, SLOT(setGenreFilter(const QString &))); filterLayout->addWidget(l1); filterLayout->addWidget(mGenre); QLabel *l2 = new QLabel(tr("Filter by &actor")); mActors = new QComboBox; mActors->setModel(mActorsModel); connect(mActors, SIGNAL(activated(const QString &)), this, SLOT(setActorFilter(const QString &))); l2->setBuddy(mActors); filterLayout->addWidget(l2); filterLayout->addWidget(mActors); QLabel *l3 = new QLabel(tr("Filter by &title")); mName = new QLineEdit; l3->setBuddy(mName); filterLayout->addWidget(l3); filterLayout->addWidget(mName); mFilter = new QPushButton(tr("Filter")); connect(mFilter, SIGNAL(clicked()), this, SLOT(setFilter())); filterLayout->addWidget(mFilter); mClearFilter = new QPushButton(tr("Clear filter")); filterLayout->addWidget(mClearFilter); //treeview mFileView = new ArchiveFileView; mFileView->setItemDelegateForColumn(MovieItem::Size, new SizeDelegate(this)); mFileView->setItemDelegateForColumn(MovieItem::Dvd, new ArchivedDelegate(this)); mProxy = new ArchiveProxy(this); mProxy->setSourceModel(mMovieModel); mFileView->setModel(mProxy); mFileView->setSortingEnabled(true); mFileView->setItemsExpandable(false); mFileView->setRootIsDecorated(false); mFileView->setColumnHidden(MovieItem::Md5Sum, true); connect(mClearFilter, SIGNAL(clicked()), mProxy, SLOT(clearFilter())); connect(mMovieModel, SIGNAL(moviesChanged()), mProxy, SLOT(invalidate())); //main layout QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(filterLayout); mainLayout->addWidget(mFileView); connect(mFileView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex)), this, SLOT(rowChanged(const QModelIndex &, const QModelIndex &))); connect(mFileView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(showMovie(const QModelIndex &))); setLayout(mainLayout); } void ArchiveViewWidget::editFile(){ QModelIndexList selected = mFileView->selectionModel()->selectedRows(); if(!selected.isEmpty()){ QModelIndex idx = selected.at(0); QModelIndex real = mProxy->mapToSource(idx); mEditDialog->setMovie(real); mEditDialog->show(); mEditDialog->raise(); mEditDialog->activateWindow(); } } void ArchiveViewWidget::editCovers(){ QModelIndexList selected = mFileView->selectionModel()->selectedRows(); if(!selected.isEmpty()){ QModelIndex idx = selected.at(0); QModelIndex real = mProxy->mapToSource(idx); mCoverEditDialog->setMovie(real); mCoverEditDialog->show(); mCoverEditDialog->raise(); mCoverEditDialog->activateWindow(); } } void ArchiveViewWidget::addMovie(){ TextEnterDialog dlg(tr("Enter movie title"), this); dlg.exec(); if(dlg.result() == QDialog::Accepted){ QString title = dlg.text().toLower().trimmed(); QModelIndex idx = mMovieModel->index(title); if(idx != QModelIndex()){ QString msg = QString(tr("Already have an entry with title %1")).arg(title); QMessageBox::critical(this, tr("Error"), msg); return; } QList movieData; movieData << title; for(int i = 1; i < MovieItem::NumRows; ++i){ movieData << QVariant(); } QString md5(32, '0'); movieData[MovieItem::Md5Sum] = md5; movieData[MovieItem::Genre] = mGenreModel->defaultId(); movieData[MovieItem::Dvd] = -2; movieData[MovieItem::Filename] = tr("(DVD)"); movieData[MovieItem::Size] = Q_INT64_C(4707319808); movieData[MovieItem::Quality] = 0; mMovieModel->addMovie(movieData, QList(), QList()); } } void ArchiveViewWidget::showMovie(const QModelIndex &movie){ QModelIndex real = mProxy->mapToSource(movie); QModelIndex dvd = mMovieModel->index(real.row(), MovieItem::Dvd, QModelIndex()); int dvdno = dvd.data().toInt(); if(dvdno != -1){ emit statusbarMessage(tr("Movie is not present on filesystem")); return; } QModelIndex fnIdx = mMovieModel->index(real.row(), MovieItem::Filename, QModelIndex()); QModelIndex md5Idx = mMovieModel->index(real.row(), MovieItem::Md5Sum, QModelIndex()); QString path = Helper::createArchivePath(fnIdx.data().toString(), md5Idx.data().toString()); QSettings s; QString playerPath = s.value("paths/movieviewer").toString(); QStringList args = s.value("paths/movieviewerargs").toStringList(); args << path; QProcess::startDetached(playerPath, args); } void ArchiveViewWidget::setFilter(){ QString filter = mName->text().toLower(); if(filter.isEmpty()){ return; } mProxy->setFilter(filter, ArchiveProxy::TitleFilter); } void ArchiveViewWidget::setGenreFilter(const QString &filter){ mProxy->setFilter(filter, ArchiveProxy::GenreFilter); } void ArchiveViewWidget::setActorFilter(const QString &filter){ mProxy->setFilter(filter, ArchiveProxy::ActorFilter); } void ArchiveViewWidget::rowChanged(const QModelIndex ¤t, const QModelIndex & /*prev*/){ if(current.isValid()){ QModelIndex idx = current; if(current.column() != 0){ idx = mProxy->index(current.row(), 0, current.parent()); } QString title = QString(tr("%1 - %2")).arg(qApp->applicationName()).arg(idx.data().toString()); emit windowTitle(title); } }