Check Date/Time unique

Issue a warning if the user changes the ride date/time
to the same as an existing ride.

They can still go ahead, but when saving it will overwrite
the existing file.

Fixing the save routines to check would require significant
refactoring and can be fixed at a later date.

Fixes #466.

[code refactored from commit dfaf151 release3 and folded in manually]
This commit is contained in:
Mark Liversedge
2011-10-06 20:09:02 +01:00
parent 63fe2fb443
commit b04e308d9f
2 changed files with 45 additions and 7 deletions

View File

@@ -70,13 +70,13 @@ RideMetadata::configUpdate()
Form *form;
#ifdef ENABLE_METRICS_TRANSLATION
if ((form = tabList.value(specialTabs.displayName(field.tab), NULL)) == NULL) {
form = new Form(main);
form = new Form(main, this);
tabs->addTab(form, specialTabs.displayName(field.tab));
tabList.insert(specialTabs.displayName(field.tab), form);
}
#else
if ((form = tabList.value(field.tab, NULL)) == NULL) {
form = new Form(main);
form = new Form(main, this);
tabs->addTab(form, field.tab);
tabList.insert(field.tab, form);
}
@@ -95,10 +95,37 @@ RideMetadata::configUpdate()
main->notifyRideSelected(); // refresh
}
void
RideMetadata::warnDateTime(QDateTime datetime)
{
if (main->rideItem() == NULL) return;
// see if there is a ride with this date/time already?
// Check if an existing ride has the same starttime
QChar zero = QLatin1Char('0');
QString targetnosuffix = QString ("%1_%2_%3_%4_%5_%6")
.arg(datetime.date().year(), 4, 10, zero)
.arg(datetime.date().month(), 2, 10, zero)
.arg(datetime.date().day(), 2, 10, zero)
.arg(datetime.time().hour(), 2, 10, zero)
.arg(datetime.time().minute(), 2, 10, zero)
.arg(datetime.time().second(), 2, 10, zero);
// now make a regexp for all know ride types
foreach(QString suffix, RideFileFactory::instance().suffixes()) {
QString conflict = main->home.absolutePath() + "/" + targetnosuffix + "." + suffix;
if (QFile(conflict).exists() && QFileInfo(conflict).fileName() != main->rideItem()->fileName) {
QMessageBox::warning(this, "Date/Time Entry", "A ride already exists with that date/time, if you do not change it then you will overwrite and lose existing data");
return; // only warn on the first conflict!
}
}
}
/*----------------------------------------------------------------------
* Forms (one per tab)
*--------------------------------------------------------------------*/
Form::Form(MainWindow *main) : main(main)
Form::Form(MainWindow *main, RideMetadata *meta) : main(main), meta(meta)
{
contents = new QWidget;
QVBoxLayout *mainLayout = new QVBoxLayout(contents);
@@ -197,7 +224,7 @@ Form::arrange()
/*----------------------------------------------------------------------
* Form fields
*--------------------------------------------------------------------*/
FormField::FormField(FieldDefinition field, MainWindow *main) : definition(field), main(main), active(false)
FormField::FormField(FieldDefinition field, MainWindow *main, RideMetadata *meta) : definition(field), main(main), active(false), meta(meta)
{
QString units;
@@ -355,6 +382,10 @@ FormField::editFinished()
QDateTime update = QDateTime(date, current.time());
main->rideItem()->setStartTime(update);
main->notifyRideSelected();
// warn if the ride already exists with that date/time
meta->warnDateTime(update);
} else if (definition.name == "Start Time") {
QDateTime current = main->rideItem()->ride()->startTime();
QTime time(/* hours*/ text.mid(0,2).toInt(),
@@ -364,6 +395,10 @@ FormField::editFinished()
QDateTime update = QDateTime(current.date(), time);
main->rideItem()->setStartTime(update);
main->notifyRideSelected();
// warn if the ride already exists with that date/time
meta->warnDateTime(update);
} else {
if (sp.isMetric(definition.name) && enabled->isChecked()) {

View File

@@ -54,7 +54,7 @@ class FormField : public QWidget
Q_OBJECT
public:
FormField(FieldDefinition, MainWindow *);
FormField(FieldDefinition, MainWindow *, RideMetadata *meta);
~FormField();
FieldDefinition definition; // define the field
QLabel *label; // label
@@ -72,6 +72,7 @@ class FormField : public QWidget
bool edited; // value has been changed
bool active; // when data being changed for rideSelected
SpecialFields sp;
RideMetadata *meta;
};
class KeywordCompleter : public QCompleter
@@ -110,9 +111,9 @@ class Form : public QScrollArea
Q_OBJECT
public:
Form(MainWindow *);
Form(MainWindow *, RideMetadata *meta);
~Form();
void addField(FieldDefinition x) { fields.append(new FormField(x, main)); }
void addField(FieldDefinition x) { fields.append(new FormField(x, main, meta)); }
void arrange(); // the meat of the action, arranging fields on the screen
private:
@@ -124,6 +125,7 @@ class Form : public QScrollArea
QGridLayout *grid1, *grid2;
QList<FormField*> fields; // keep track so we can destroy
QList<QHBoxLayout *> overrides; // keep track so we can destroy
RideMetadata *meta;
};
@@ -140,6 +142,7 @@ class RideMetadata : public QWidget
public slots:
void configUpdate();
void warnDateTime(QDateTime); // warn if file already exists after date/time changed
private:
MainWindow *main;