Calendar add-remove rides

This commit is contained in:
Berend De Schouwer
2009-08-31 16:40:59 +02:00
committed by Justin Knotzke
parent a1bbf4d50f
commit 86cfac2d9e
3 changed files with 87 additions and 42 deletions

View File

@@ -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(&notesFile);
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<RideItem*>(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);

View File

@@ -8,6 +8,7 @@
#include <QTextCharFormat>
#include <QPen>
#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(&notesFile);
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.

View File

@@ -3,6 +3,7 @@
#include <QCalendarWidget>
#include <QMultiMap>
#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<QDate, QString> _text;
QMap<QDate, QColor> _color;
QMap<QString, QColor> workoutCodes;
QDir home;
};
#endif