diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 98943030c..7483bc75c 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -138,6 +138,11 @@ MainWindow::MainWindow(const QDir &home) : calendar = new RideCalendar; calendar->setFirstDayOfWeek(Qt::Monday); + calendar->setHome(home); + calendar->addWorkoutCode(QString("race"), QColor(Qt::red)); + calendar->addWorkoutCode(QString("sick"), QColor(Qt::yellow)); + calendar->addWorkoutCode(QString("swim"), QColor(Qt::blue)); + calendar->addWorkoutCode(QString("gym"), QColor(Qt::gray)); treeWidget = new QTreeWidget; treeWidget->setColumnCount(3); @@ -177,39 +182,7 @@ MainWindow::MainWindow(const QDir &home) : last = new RideItem(RIDE_TYPE, home.path(), name, dt, &zones, notesFileName); allRides->addChild(last); - - /* - * We want to display these things inside the Calendar. - * Pick a colour (this should really be configurable) - * - red for races - * - yellow for sick days - * - green for rides - */ - QString notesPath = home.absolutePath() + "/" + notesFileName; - QFile notesFile(notesPath); - QColor color(Qt::green); - QString line("Ride"); - if (notesFile.exists()) { - if (notesFile.open(QFile::ReadOnly | QFile::Text)) { - QTextStream in(¬esFile); - line = in.readLine(); - notesFile.close(); - if (line.contains("race", Qt::CaseInsensitive)) { - color = QColor(Qt::red); - line = "RACE"; - } - if (line.contains("sick", Qt::CaseInsensitive)) { - color = QColor(Qt::red); - } - if (line.contains("swim", Qt::CaseInsensitive)) { - color = QColor(Qt::blue); - } - if (line.contains("gym", Qt::CaseInsensitive)) { - color = QColor(Qt::gray); - } - } - } - calendar->addEvent(dt.date(), line, color); + calendar->addRide(reinterpret_cast(last)); } } @@ -703,6 +676,7 @@ MainWindow::addRide(QString name, bool bSelect /*=true*/) ++index; } allRides->insertChild(index, last); + calendar->addRide(last); cpintPlot->needToScanRides = true; if (bSelect) { @@ -734,6 +708,7 @@ MainWindow::removeCurrentRide() QString strOldFileName = item->fileName; allRides->removeChild(item); + calendar->removeRide(item); delete item; QFile file(home.absolutePath() + "/" + strOldFileName); diff --git a/src/RideCalendar.cpp b/src/RideCalendar.cpp index fb13e0b74..75bb8c40f 100644 --- a/src/RideCalendar.cpp +++ b/src/RideCalendar.cpp @@ -8,6 +8,7 @@ #include #include +#include "RideItem.h" #include "RideCalendar.h" RideCalendar::RideCalendar(QWidget *parent) @@ -15,13 +16,6 @@ RideCalendar::RideCalendar(QWidget *parent) { }; -void RideCalendar::addEvent(QDate date, QString string, QColor color) -{ - _text[date] = string; - _color[date] = color; - update(); -} - void RideCalendar::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const { if (_text.contains(date)) { @@ -65,6 +59,75 @@ void RideCalendar::paintCell(QPainter *painter, const QRect &rect, const QDate & } } +void RideCalendar::setHome(const QDir &homeDir) +{ + home = homeDir; +} + +void RideCalendar::addRide(RideItem* ride) +{ + qWarning("RideCalendar::addRide() Adding a ride."); + /* + * We want to display these things inside the Calendar. + * Pick a colour (this should really be configurable) + * - red for races + * - yellow for sick days + * - green for rides + */ + QDateTime dt = ride->dateTime; + QString notesPath = home.absolutePath() + "/" + ride->notesFileName; + QFile notesFile(notesPath); + QColor color(Qt::green); + QString line("Ride"); + QString code; + if (notesFile.exists()) { + if (notesFile.open(QFile::ReadOnly | QFile::Text)) { + QTextStream in(¬esFile); + line = in.readLine(); + notesFile.close(); + foreach(code, workoutCodes.keys()) { + if (line.contains(code, Qt::CaseInsensitive)) { + color = workoutCodes[code]; + } + } + } + } + addEvent(dt.date(), line, color); +} + +void RideCalendar::removeRide(RideItem* ride) +{ + removeEvent(ride->dateTime.date()); +} + +void RideCalendar::addWorkoutCode(QString string, QColor color) +{ + workoutCodes[string] = color; +} + +/* + * Private: + * Add a string, and a color, to a specific date. + */ +void RideCalendar::addEvent(QDate date, QString string, QColor color) +{ + _text[date] = string; + _color[date] = color; + update(); +} + +/* + * Private: + * Remove the info for a current date. + */ +void RideCalendar::removeEvent(QDate date) +{ + if (_text.contains(date)) { + _text.remove(date); + _color.remove(date); + } +} + /* * We extend QT's QCalendarWidget's sizeHint() so we claim a little bit of * extra space. diff --git a/src/RideCalendar.h b/src/RideCalendar.h index 8eb8f94a9..4d6d23c1a 100644 --- a/src/RideCalendar.h +++ b/src/RideCalendar.h @@ -3,6 +3,7 @@ #include #include +#include "RideItem.h" class RideCalendar : public QCalendarWidget { @@ -10,16 +11,22 @@ class RideCalendar : public QCalendarWidget public: RideCalendar(QWidget *parent = 0); - - void addEvent(QDate, QString, QColor); + void removeRide(RideItem*); + void addRide(RideItem*); QSize sizeHint() const; + void setHome(const QDir&); + void addWorkoutCode(QString, QColor); protected: void paintCell(QPainter *, const QRect &, const QDate &) const; private: + void addEvent(QDate, QString, QColor); + void removeEvent(QDate); QMap _text; QMap _color; + QMap workoutCodes; + QDir home; }; #endif