Add Athlete > Delete menu

Allows to delete a closed athlete without the need to exit the program,
it re-uses the logic in ChooseCyclistDialog and removes the AthleteCard
from AthleteView.
This commit is contained in:
Ale Martinez
2021-03-03 22:27:17 -03:00
parent 453b05a188
commit 06914d9446
6 changed files with 98 additions and 11 deletions

View File

@@ -45,6 +45,8 @@ AthleteView::AthleteView(Context *context) : ChartSpace(context, OverviewScope::
connect(this, SIGNAL(itemConfigRequested(ChartSpaceItem*)), this, SLOT(configItem(ChartSpaceItem*)));
// new athlete
connect(context->mainWindow, SIGNAL(newAthlete(QString)), this, SLOT(newAthlete(QString)));
// delete athlete
connect(context->mainWindow, SIGNAL(deletedAthlete(QString)), this, SLOT(deleteAthlete(QString)));
}
void
@@ -67,6 +69,16 @@ AthleteView::newAthlete(QString name)
updateGeometry();
}
void
AthleteView::deleteAthlete(QString name)
{
foreach(ChartSpaceItem* item, allItems()) {
if (item->name == name) {
removeItem(item);
}
}
}
void
AthleteView::configChanged(qint32)
{

View File

@@ -12,6 +12,7 @@ public slots:
void configChanged(qint32);
void configItem(ChartSpaceItem*);
void newAthlete(QString);
void deleteAthlete(QString);
private:
int row, col;

View File

@@ -148,17 +148,12 @@ ChooseCyclistDialog::cancelClicked()
reject();
}
void
ChooseCyclistDialog::deleteClicked()
bool
ChooseCyclistDialog::deleteAthlete(QDir &homeDir, QString name, QWidget *parent)
{
// nothing selected
if (listWidget->selectedItems().count() <= 0) return;
QListWidgetItem *item = listWidget->selectedItems().first();
QMessageBox msgBox;
QMessageBox msgBox(parent);
msgBox.setWindowTitle(tr("Delete athlete"));
msgBox.setText(tr("You are about to delete %1").arg(item->text()));
msgBox.setText(tr("You are about to delete %1").arg(name));
msgBox.setInformativeText(tr("This cannot be undone and all data will be permanently deleted.\n\nAre you sure?"));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
@@ -168,11 +163,26 @@ ChooseCyclistDialog::deleteClicked()
if(msgBox.clickedButton() == msgBox.button(QMessageBox::Ok)) {
// ok .. lets wipe the athlete !
QDir athleteDir = QDir(home.absolutePath() + "/" + item->text());
QDir athleteDir = QDir(homeDir.absolutePath() + "/" + name);
// zap!
recursiveDelete(athleteDir);
home.rmdir(item->text());
homeDir.rmdir(name);
return true;
}
return false;
}
void
ChooseCyclistDialog::deleteClicked()
{
// nothing selected
if (listWidget->selectedItems().count() <= 0) return;
QListWidgetItem *item = listWidget->selectedItems().first();
if(deleteAthlete(home, item->text(), this)) {
// list again, with athlete now gone
getList();

View File

@@ -44,6 +44,7 @@ class ChooseCyclistDialog : public QDialog
QString choice();
void getList();
static QString newCyclistDialog(QDir &homeDir, QWidget *parent);
static bool deleteAthlete(QDir &homeDir, QString name, QWidget *parent);
private slots:
void newClicked();

View File

@@ -466,6 +466,12 @@ MainWindow::MainWindow(const QDir &home)
backupMapper = new QSignalMapper(this); // maps each option
connect(backupMapper, SIGNAL(mapped(const QString &)), this, SLOT(backupAthlete(const QString &)));
fileMenu->addSeparator();
deleteAthleteMenu = fileMenu->addMenu(tr("Delete..."));
connect(deleteAthleteMenu, SIGNAL(aboutToShow()), this, SLOT(setDeleteAthleteMenu()));
deleteMapper = new QSignalMapper(this); // maps each option
connect(deleteMapper, SIGNAL(mapped(const QString &)), this, SLOT(deleteAthlete(const QString &)));
fileMenu->addSeparator();
fileMenu->addAction(tr("Save all modified activities"), this, SLOT(saveAllUnsavedRides()));
fileMenu->addSeparator();
@@ -1947,6 +1953,54 @@ MainWindow::backupAthlete(QString name)
delete backup;
}
void
MainWindow::setDeleteAthleteMenu()
{
// wipe existing
deleteAthleteMenu->clear();
// get a list of all cyclists
QStringListIterator i(QDir(gcroot).entryList(QDir::Dirs | QDir::NoDotAndDotDot));
while (i.hasNext()) {
QString name = i.next();
SKIP_QTWE_CACHE // skip Folder Names created by QTWebEngine on Windows
// new action
QAction *action = new QAction(QString("%1").arg(name), this);
// get the config directory
AthleteDirectoryStructure subDirs(name);
// icon / mugshot ?
QString icon = QString("%1/%2/%3/avatar.png").arg(gcroot).arg(name).arg(subDirs.config().dirName());
if (QFile(icon).exists()) action->setIcon(QIcon(icon));
// only allow selection of cyclists which are not already open
foreach (MainWindow *x, mainwindows) {
QMapIterator<QString, Tab*> t(x->tabs);
while (t.hasNext()) {
t.next();
if (t.key() == name)
action->setEnabled(false);
}
}
// add to menu
deleteAthleteMenu->addAction(action);
connect(action, SIGNAL(triggered()), deleteMapper, SLOT(map()));
deleteMapper->setMapping(action, name);
}
}
void
MainWindow::deleteAthlete(QString name)
{
QDir home(gcroot);
if(ChooseCyclistDialog::deleteAthlete(home, name, this)) {
// notify deletion
emit deletedAthlete(name);
}
}
void
MainWindow::saveGCState(Context *context)
{

View File

@@ -126,6 +126,7 @@ class MainWindow : public QMainWindow
void forwardClicked();
void openingAthlete(QString, Context *);
void newAthlete(QString);
void deletedAthlete(QString);
public slots:
@@ -165,6 +166,10 @@ class MainWindow : public QMainWindow
void setBackupAthleteMenu();
void backupAthlete(QString name);
// Athlete Delete
void setDeleteAthleteMenu();
void deleteAthlete(QString name);
// Search / Filter
void setFilter(QStringList);
void clearFilter();
@@ -307,6 +312,10 @@ class MainWindow : public QMainWindow
QMenu *backupAthleteMenu;
QSignalMapper *backupMapper;
// delete
QMenu *deleteAthleteMenu;
QSignalMapper *deleteMapper;
// chart menus
QMenu *chartMenu;
QMenu *subChartMenu;