Multiple rides per day in the calendar

This commit is contained in:
Berend De Schouwer
2009-12-24 12:33:58 +02:00
committed by Sean Rhea
parent abbdd4667e
commit 4f4973ab18
3 changed files with 125 additions and 94 deletions

View File

@@ -462,27 +462,34 @@ MainWindow::addRide(QString name, bool bSelect /*=true*/)
void
MainWindow::removeCurrentRide()
{
int x = 0;
QTreeWidgetItem *_item = treeWidget->currentItem();
if (_item->type() != RIDE_TYPE)
return;
RideItem *item = reinterpret_cast<RideItem*>(_item);
QTreeWidgetItem *itemToSelect = NULL;
for (int x=0; x<allRides->childCount(); ++x)
for (x=0; x<allRides->childCount(); ++x)
{
if (item==allRides->child(x))
{
if ((x+1)<allRides->childCount())
itemToSelect = allRides->child(x+1);
else if (x>0)
itemToSelect = allRides->child(x-1);
break;
}
}
calendar->removeRide(item);
if (x>0) {
itemToSelect = allRides->child(x-1);
calendar->addRide(reinterpret_cast<RideItem*>(itemToSelect));
}
if ((x+1)<allRides->childCount()) {
itemToSelect = allRides->child(x+1);
calendar->addRide(reinterpret_cast<RideItem*>(itemToSelect));
}
QString strOldFileName = item->fileName;
allRides->removeChild(item);
calendar->removeRide(item);
delete item;
QFile file(home.absolutePath() + "/" + strOldFileName);

View File

@@ -18,45 +18,111 @@ RideCalendar::RideCalendar(QWidget *parent)
void RideCalendar::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
if (_text.contains(date)) {
painter->save();
/*
* Draw a rectangle in the color specified. If this is the
* currently selected date, draw a black outline.
*/
QPen pen(Qt::SolidLine);
pen.setCapStyle(Qt::SquareCap);
painter->setBrush(_color[date]);
if (date == selectedDate()) {
pen.setColor(Qt::black);
pen.setWidth(1);
} else {
pen.setColor(_color[date]);
}
painter->setPen(pen);
/*
* We have to draw to height-1 and width-1 because Qt draws outlines
* outside the box by default.
*/
painter->drawRect(rect.x(), rect.y(), rect.width() - 1, rect.height() - 1);
/*
* Display the text.
*/
pen.setColor(Qt::black);
painter->setPen(pen);
QString text = QString::number(date.day());
text = text + "\n" + _text[date];
QFont font = painter->font();
font.setPointSize(font.pointSize() - 2);
painter->setFont(font);
painter->drawText(rect, Qt::AlignHCenter | Qt::TextWordWrap, text);
painter->restore();
} else {
if (!_rides.contains(date)) {
QCalendarWidget::paintCell(painter, rect, date);
return;
}
painter->save();
/*
* Fill in the text and color to some default
*/
QRect textRect;
int number = _rides.count(date);
float count = 1;
int startY, stopY;
QPen pen(Qt::SolidLine);
painter->setPen(pen);
pen.setCapStyle(Qt::SquareCap);
QMap<QDateTime, RideItem*> ridesToday;
RideItem * ride;
QFont font = painter->font();
font.setPointSize(font.pointSize() - 2);
painter->setFont(font);
/*
* Loop over all the matching rides, and record the time.
* That way, the entries are sorted earliest -> latest.
*/
QMultiMap<QDate, RideItem *>::const_iterator j = _rides.find(date);
while (j != _rides.end() && j.key() == date) {
ride = j.value();
ridesToday.insert(ride->dateTime, ride);
++j;
}
/*
* Loop over all the rides for today, and record colour and text.
*/
QMap<QDateTime, RideItem *>::const_iterator i = ridesToday.begin();
while (i != ridesToday.end()) {
RideItem *ride = i.value();
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];
}
}
}
}
startY = rect.y() + ((count - 1) * rect.height()) / number;
stopY = rect.height() / number;
/* fillRect() doesn't need width-1, height-1. drawRect() does */
painter->fillRect(rect.x(), startY,
rect.width(), stopY,
color);
textRect = QRect(rect.x(), startY, rect.width(), stopY);
pen.setColor(Qt::black);
pen.setStyle(Qt::SolidLine);
painter->setPen(pen);
painter->drawText(textRect, Qt::AlignHCenter | Qt::TextWordWrap, line);
pen.setColor(QColor(0, 0, 0, 63));
pen.setStyle(Qt::SolidLine);
painter->setPen(pen);
painter->drawRoundRect(textRect, 10, 10);
++i;
++count;
}
/*
* Draw a rectangle in the color specified. If this is the
* currently selected date, draw a black outline.
*/
if (date == selectedDate()) {
pen.setColor(Qt::black);
pen.setStyle(Qt::SolidLine);
pen.setWidth(2);
painter->setPen(pen);
/* drawRect() draws an outline, so needs width-1, height-1 */
/* We set +1, +1, -2, -2 because the width is 2. */
painter->drawRect(rect.x() + 1, rect.y() + 1,
rect.width() - 2, rect.height() - 2);
}
/*
* Display the date.
*/
pen.setColor(QColor(0, 0, 0, 63));
pen.setStyle(Qt::SolidLine);
pen.setWidth(1);
painter->setPen(pen);
QString textDate = QString::number(date.day());
painter->drawText(rect,
Qt::AlignHCenter | Qt::TextWordWrap | Qt::AlignVCenter,
textDate);
/* We're done, restore */
painter->restore();
}
void RideCalendar::setHome(const QDir &homeDir)
@@ -64,67 +130,28 @@ void RideCalendar::setHome(const QDir &homeDir)
home = homeDir;
}
void RideCalendar::addRide(RideItem* 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.
* Add a ride, to a specific date.
*/
void RideCalendar::addEvent(QDate date, QString string, QColor color)
void RideCalendar::addRide(RideItem* ride)
{
_text[date] = string;
_color[date] = color;
_rides.insert(ride->dateTime.date(), ride);
update();
}
/*
* Private:
* Remove the info for a current date.
* Remove the info for a current ride.
*/
void RideCalendar::removeEvent(QDate date)
void RideCalendar::removeRide(RideItem* ride)
{
if (_text.contains(date)) {
_text.remove(date);
_color.remove(date);
}
QDate date = ride->dateTime.date();
_rides.erase(_rides.find(date, ride));
update();
}
/*

View File

@@ -21,10 +21,7 @@ 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;
QMultiMap<QDate, RideItem*> _rides;
QMap<QString, QColor> workoutCodes;
QDir home;
};