Update GPL blurb in source files.
[toast/confclerk.git] / src / mvc / delegate.cpp
index 4edde0e..54ab046 100644 (file)
@@ -1,3 +1,21 @@
+/*
+ * Copyright (C) 2010 Ixonos Plc.
+ *
+ * This file is part of ConfClerk.
+ *
+ * ConfClerk is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * ConfClerk is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
+ */
 #include "delegate.h"
 #include "eventmodel.h"
 #include <track.h>
@@ -5,6 +23,8 @@
 #include <QDebug>
 #include <QPainter>
 
+#include "room.h"
+
 const int RADIUS = 10;
 const int SPACER = 10;
 
@@ -35,13 +55,18 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
 
     painter->save();
     QColor bkgrColor = Qt::cyan;
+    //QColor bkgrColor = QColor(0xAA,0xAA,0xAA);
+    QColor conflictColor = Qt::yellow;
 
     QPen borderPen(bkgrColor.darker());
-    //QColor bkgrColor = QColor(0,0,113);
-    //QPen borderPen(Qt::cyan);
     if(hasParent(index))
     {
-        int aux = option.rect.height() - mControls[FavouriteControlOn]->drawPoint().y() - mControls[FavouriteControlOn]->image()->height();
+        // entry horisontal layout:
+        // * spacer (aka y position of image)
+        // * image
+        // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
+        int aux = option.rect.height() - SPACER - mControls[FavouriteControlOn]->image()->height();
+        Event *event = static_cast<Event*>(index.internalPointer());
         // font SMALL
         QFont fontSmall = option.font;
         fontSmall.setBold(false);
@@ -65,19 +90,18 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
 
         //Time conflicts are colored differently
-        if ((static_cast<Event*>(index.internalPointer())->isFavourite())
-            && (hasTimeConflict(index, index.parent())))
-        {
-            bkgrColor = Qt::yellow;
-        }
+        if(event->hasTimeConflict())
+            bkgrColor = conflictColor;
+
+        QLinearGradient itemGradient(option.rect.topLeft(), option.rect.bottomLeft());
+        itemGradient.setColorAt(0.0, Qt::white);
+        itemGradient.setColorAt(0.25, bkgrColor);
+        itemGradient.setColorAt(0.5, bkgrColor);
+        itemGradient.setColorAt(0.75, bkgrColor);
+        itemGradient.setColorAt(1.0, Qt::white);
 
         if(isLast(index))
         {
-            QLinearGradient lastGradient(option.rect.topLeft(), option.rect.bottomLeft());
-            lastGradient.setColorAt(0.0, Qt::white);
-            lastGradient.setColorAt(0.5, bkgrColor);
-            lastGradient.setColorAt(1.0, Qt::white);
-
             QPainterPath endPath;
             endPath.moveTo(option.rect.topLeft());
             endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS));
@@ -86,8 +110,8 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
             endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90);
             endPath.lineTo(option.rect.topRight());
 
-            painter->setBrush( bkgrColor );
-            //painter->setBrush(lastGradient);
+            //painter->setBrush( bkgrColor );
+            painter->setBrush(itemGradient);
             painter->setPen(borderPen);
             painter->drawPath(endPath);
 
@@ -95,15 +119,8 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         }
         else // middle elements
         {
-            QLinearGradient middleGradient(option.rect.topLeft(), option.rect.bottomLeft());
-            middleGradient.setColorAt(0.0, Qt::white);
-            middleGradient.setColorAt(0.25, bkgrColor);
-            middleGradient.setColorAt(0.5, Qt::white);
-            middleGradient.setColorAt(0.75, bkgrColor);
-            middleGradient.setColorAt(1.0, Qt::white);
-
-            painter->setBrush( bkgrColor );
-            //painter->setBrush(middleGradient);
+            //painter->setBrush( bkgrColor );
+            painter->setBrush(itemGradient);
             painter->setPen(Qt::NoPen);
             painter->drawRect(option.rect);
 
@@ -118,41 +135,33 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         }
 
         // draw Controls
-        // favourite
-        if(static_cast<Event*>(index.internalPointer())->isFavourite())
-            painter->drawImage(mControls[FavouriteControlOn]->drawPoint(option.rect),*mControls[FavouriteControlOn]->image());
+        foreach(Control* c, mControls.values()) {
+            c->setEnabled(false);
+        }
+        if(event->isFavourite())
+            mControls[FavouriteControlOn]->paint(painter, option.rect);
         else
-            painter->drawImage(mControls[FavouriteControlOff]->drawPoint(option.rect),*mControls[FavouriteControlOff]->image());
+            mControls[FavouriteControlOff]->paint(painter, option.rect);
 #ifdef MAEMO
-        // alarm
-        if(static_cast<Event*>(index.internalPointer())->hasAlarm())
-            painter->drawImage(mControls[AlarmControlOn]->drawPoint(option.rect),*mControls[AlarmControlOn]->image());
+        if(event->hasAlarm())
+            mControls[AlarmControlOn]->paint(painter, option.rect);
         else
-            painter->drawImage(mControls[AlarmControlOff]->drawPoint(option.rect),*mControls[AlarmControlOff]->image());
+            mControls[AlarmControlOff]->paint(painter, option.rect);
 #endif
-        // map
-        painter->drawImage(mControls[MapControl]->drawPoint(option.rect),*mControls[MapControl]->image());
-        // Time conflict
-        //if(static_cast<Event*>(index.internalPointer())->hasTimeConflict())
-        if(bkgrColor == Qt::yellow)
-        {
-            painter->drawImage(mControls[WarningControlOn]->drawPoint(option.rect),*mControls[WarningControlOn]->image());
-            mControls[WarningControlOn]->hasConflict=true;
-        }
-        else
-        {
-            painter->drawImage(mControls[WarningControlOff]->drawPoint(option.rect),*mControls[WarningControlOff]->image());
-            mControls[WarningControlOn]->hasConflict=false;
-        }
+        if (event->room()->hasMap())
+            mControls[MapControl]->paint(painter, option.rect);
+        if(event->hasTimeConflict())
+            mControls[WarningControl]->paint(painter, option.rect);
 
         // draw texts
-        Event *event = static_cast<Event*>(index.internalPointer());
-        QPointF titlePointF(mControls[FavouriteControlOn]->drawPoint(option.rect));
-        titlePointF.setX(option.rect.x()+SPACER);
-        titlePointF.setY(titlePointF.y()+mControls[FavouriteControlOn]->image()->height());
+        // it starts just below the image
+        // ("position of text" is lower-left angle of the first letter,
+        //  so the first line is actually at the same height as the image)
+        QPointF titlePointF(option.rect.x() + SPACER,
+                            option.rect.y() + SPACER + mControls[FavouriteControlOn]->image()->height());
         QTime start = event->start().time();
         painter->setFont(fontBig);
-        painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->room());
+        painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
         // title
         titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
         painter->setFont(fontBigB);
@@ -178,7 +187,7 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         painter->drawText(titlePointF,"Presenter(s): " + event->persons().join(" and "));
         // track
         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
-        painter->drawText(titlePointF,"Track: " + Track::getTrackName(event->trackId()));
+        painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
     }
     else // doesn't have parent - time-groups' elements (top items)
     {
@@ -230,24 +239,25 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
 
         // draw icons 
         painter->setFont(fontSmall);
+        QImage *image = mControls[FavouriteControlOn]->image();
         QPoint drawPoint =
             option.rect.topRight()
             - QPoint(
-                    spacer + mControls[FavouriteControlOn]->image()->width(),
-                    - option.rect.height()/2 + mControls[FavouriteControlOn]->image()->height()/2);
-        painter->drawImage(drawPoint,*mControls[FavouriteControlOn]->image());
-        painter->drawText(drawPoint+QPoint(mControls[FavouriteControlOn]->image()->width()+2, option.rect.height()/2),
+                    spacer + image->width(),
+                    - option.rect.height()/2 + image->height()/2);
+        painter->drawImage(drawPoint,*image);
+        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
                 QString::number(numberOfFavourities(index)));
 #ifdef MAEMO
-        drawPoint.setX(drawPoint.x() - spacer - mControls[FavouriteControlOn]->image()->width());
+        drawPoint.setX(drawPoint.x() - spacer - image->width());
         painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
-        painter->drawText(drawPoint+QPoint(mControls[FavouriteControlOn]->image()->width()+2, option.rect.height()/2),
+        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
                 QString::number(numberOfAlarms(index)));
 #endif
         // draw texts
         QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
         drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
-        drawPoint.setY(drawPoint.y() + option.rect.height()/2);
+        drawPoint.setY(drawPoint.y()+image->height() - 2);
         painter->drawText(drawPoint,numEvents);
 
         QPointF titlePointF = QPoint(
@@ -315,87 +325,68 @@ Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, con
     while (i.hasNext())
     {
         ControlId id = i.next();
-        if((mControls[id]->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
-            && (id != WarningControlOn) && (id != WarningControlOff))
+        Control *control = mControls[id];
+        if (control->enabled()
+            and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
         {
             return id;
         }
-        else
-        {
-            if ((mControls[id]->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
-            && (mControls[id]->hasConflict))
-            {
-            return id;
-            }
-        }
     }
 
     return ControlNone;
 }
 
+Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
+    : mId(aControlId)
+    , mImage(new QImage(aImageName))
+    , mDrawPoint(QPoint(0,0))
+    , mEnabled(false)
+{
+    QPoint p;
+    if (prev_control == NULL) {
+        p = QPoint(0, SPACER);
+    } else {
+        p = prev_control->drawPoint();
+    }
+    p.setX(p.x()-image()->width()-SPACER);
+    setDrawPoint(p);
+}
+
+void Delegate::Control::paint(QPainter* painter, const QRect rect)
+{
+    painter->drawImage(drawPoint(rect),*image());
+    setEnabled(true);
+}
+
 void Delegate::defineControls()
 {
-    Control *control;
-    QPoint p(0,0);
     // FAVOURITE ICONs
     // on
-    control = new Control(FavouriteControlOn,QString(":icons/favourite-onBig.png"));
-    p = QPoint(0,SPACER);
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(FavouriteControlOn,control);
+    mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/favourite-onBig.png"), NULL));
     // off
-    control = new Control(FavouriteControlOff,QString(":icons/favourite-offBig.png"));
-    p = QPoint(0,SPACER);
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(FavouriteControlOff,control);
+    mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/favourite-offBig.png"), NULL));
 
 #ifdef MAEMO
     // ALARM ICONs
     // on
-    control = new Control(AlarmControlOn,QString(":icons/alarm-onBig.png"));
-    p = mControls[FavouriteControlOn]->drawPoint();
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(AlarmControlOn,control);
+    mControls.insert(AlarmControlOn,
+                    new Control(AlarmControlOn, QString(":icons/alarm-onBig.png"), mControls[FavouriteControlOn]));
     // off
-    control = new Control(AlarmControlOff,QString(":icons/alarm-offBig.png"));
-    p = mControls[FavouriteControlOff]->drawPoint();
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(AlarmControlOff,control);
+    mControls.insert(AlarmControlOff,
+                    new Control(AlarmControlOff, QString(":icons/alarm-offBig.png"), mControls[FavouriteControlOff]));
 
     // MAP ICON
-    control = new Control(MapControl,QString(":icons/compassBig.png"));
-    p = mControls[AlarmControlOn]->drawPoint();
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(MapControl,control);
+    mControls.insert(MapControl,
+                    new Control(MapControl, QString(":icons/compassBig.png"), mControls[AlarmControlOn]));
 #else
     // MAP ICON
-    control = new Control(MapControl,QString(":icons/compassBig.png"));
-    p = mControls[FavouriteControlOn]->drawPoint();
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(MapControl,control);
+    mControls.insert(MapControl,
+                    new Control(MapControl, QString(":icons/compassBig.png"), mControls[FavouriteControlOn]));
 #endif
 
-    // WARNING ICONs
-    // on
-    control = new Control(WarningControlOn,QString(":icons/exclamation-iconOn.png"));
-    p = mControls[MapControl]->drawPoint();
-    control->hasConflict = false;
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(WarningControlOn,control);
-    // off
-    control = new Control(WarningControlOff,QString(":icons/exclamation-iconOff.png"));
-    p = mControls[MapControl]->drawPoint();
-    control->hasConflict = false;
-    p.setX(p.x()-control->image()->width()-SPACER);
-    control->setDrawPoint(p);
-    mControls.insert(WarningControlOff,control);
+    // WARNING ICON
+    mControls.insert(WarningControl,
+                    new Control(WarningControl, QString(":icons/exclamation.png"), mControls[MapControl]));
 }
 
 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
@@ -432,28 +423,3 @@ int Delegate::numberOfAlarms(const QModelIndex &index) const
     return nrofAlarms;
 }
 
-bool Delegate::hasTimeConflict(const QModelIndex &index, const QModelIndex &parent) const
-{
-    Event *event = static_cast<Event*>(index.internalPointer());
-    QTime start = event->start().time();
-    QTime end = start.addSecs(event->duration());
-    for(int i=0; i<parent.model()->rowCount(parent); i++)
-    {
-        if((event->id()!=static_cast<Event*>(parent.child(i,0).internalPointer())->id())
-        &&
-        (static_cast<Event*>(parent.child(i,0).internalPointer())->isFavourite()))
-        {
-            if (((start >= static_cast<Event*>(parent.child(i,0).internalPointer())->start().time())
-            &&
-            (start < static_cast<Event*>(parent.child(i,0).internalPointer())->start().time().addSecs(static_cast<Event*>(parent.child(i,0).internalPointer())->duration())))
-            ||
-            ((end > static_cast<Event*>(parent.child(i,0).internalPointer())->start().time())
-            &&
-            (end <= static_cast<Event*>(parent.child(i,0).internalPointer())->start().time().addSecs(static_cast<Event*>(parent.child(i,0).internalPointer())->duration()))))
-            {
-                return true;
-            }
-        }
-    }
-    return false;
-}