Skip to content

Commit

Permalink
TrainSidebar: Delete multiple workouts
Browse files Browse the repository at this point in the history
  • Loading branch information
grauser authored and liversedge committed Nov 24, 2015
1 parent 92071e3 commit 7cf63b4
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions src/TrainSidebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ TrainSidebar::TrainSidebar(Context *context) : GcWindow(context), context(contex
workoutTree->setFrameStyle(QFrame::NoFrame);
workoutTree->setAlternatingRowColors(false);
workoutTree->setEditTriggers(QAbstractItemView::NoEditTriggers); // read-only
workoutTree->setSelectionMode(QAbstractItemView::ExtendedSelection);
workoutTree->expandAll();
workoutTree->header()->setCascadingSectionResizes(true); // easier to resize this way
workoutTree->setContextMenuPolicy(Qt::CustomContextMenu);
Expand Down Expand Up @@ -529,12 +530,27 @@ TrainSidebar::workoutPopup()
menu.addAction(wizard);
menu.addAction(scan);


// we can delete too
QModelIndex current = workoutTree->currentIndex();
QModelIndex target = sortModel->mapToSource(current);
QString filename = workoutModel->data(workoutModel->index(target.row(), 0), Qt::DisplayRole).toString();
if (QFileInfo(filename).exists()) {
int delNumber = 0;

QModelIndexList list = workoutTree->selectionModel()->selectedRows();
foreach (QModelIndex index, list)
{
QModelIndex target = sortModel->mapToSource(index);

QString filename = workoutModel->data(workoutModel->index(target.row(), 0), Qt::DisplayRole).toString();
if (QFileInfo(filename).exists()) {
delNumber++;
}
}

if (delNumber > 0) {
QAction *del = new QAction(tr("Delete selected Workout"), workoutTree);

if (delNumber > 1) {
del->setText(QString(tr("Delete %1 selected Workouts")).arg(delNumber));
}
menu.addAction(del);
connect(del, SIGNAL(triggered(void)), this, SLOT(deleteWorkouts(void)));
}
Expand Down Expand Up @@ -902,16 +918,33 @@ TrainSidebar::removeInvalidVideoSync()
void
TrainSidebar::deleteWorkouts()
{
QModelIndex current = workoutTree->currentIndex();
QModelIndex target = sortModel->mapToSource(current);
QString filename = workoutModel->data(workoutModel->index(target.row(), 0), Qt::DisplayRole).toString();

if (QFileInfo(filename).exists()) {
QStringList nameList;
QModelIndexList list = workoutTree->selectionModel()->selectedRows();
foreach (QModelIndex index, list) {
QModelIndex target = sortModel->mapToSource(index);
QString filename = workoutModel->data(workoutModel->index(target.row(), 0), Qt::DisplayRole).toString();
if (QFileInfo(filename).exists()) {
nameList.append(filename);
}
}

if (nameList.count()>0) {
// are you sure?
QMessageBox msgBox;
msgBox.setText(tr("Are you sure you want to delete this Workout?"));
msgBox.setInformativeText(filename);
if (nameList.count()==1) {
msgBox.setText(tr("Are you sure you want to delete this Workout?"));
}
else {
msgBox.setText(QString(tr("Are you sure you want to delete this %1 workouts?")).arg(nameList.count()));
}
QString info;
for (int i=0;i<nameList.count() && i<21;i++) {
info += nameList.at(i)+"\n";
if (i == 20) {
info += "...\n";
}
}
msgBox.setInformativeText(info);
QPushButton *deleteButton = msgBox.addButton(tr("Delete"),QMessageBox::YesRole);
msgBox.setStandardButtons(QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
Expand All @@ -920,12 +953,16 @@ TrainSidebar::deleteWorkouts()

if(msgBox.clickedButton() != deleteButton) return;

// delete from disk
QFile(filename).remove();
// delete from DB
trainDB->startLUW();
trainDB->deleteWorkout(filename);
trainDB->endLUW();
foreach (QString filename, nameList) {
if (QFileInfo(filename).exists()) {
// delete from disk
QFile(filename).remove();
// delete from DB
trainDB->startLUW();
trainDB->deleteWorkout(filename);
trainDB->endLUW();
}
}
}
}

Expand Down

0 comments on commit 7cf63b4

Please sign in to comment.