refactor: cache whole Room object in Event
authorkirilma <kirilma@localhost>
Wed, 5 May 2010 13:57:37 +0000 (13:57 +0000)
committerkirilma <kirilma@localhost>
Wed, 5 May 2010 13:57:37 +0000 (13:57 +0000)
src/gui/tabcontainer.cpp
src/mvc/delegate.cpp
src/mvc/event.cpp
src/mvc/event.h

index 94ce625..3f5d4ce 100644 (file)
@@ -85,8 +85,7 @@ void TabContainer::displayMap(const QModelIndex &aIndex)
 {
     Event *event = static_cast<Event*>(aIndex.internalPointer());
 
-    Room room = Room::retrieve(event->roomId());
-    QVariant mapPathV = room.map();
+    QVariant mapPathV = event->room()->map();
     QString mapPath;
     if (!mapPathV.isValid()) {
         error_message("No map for this room");
@@ -100,7 +99,7 @@ void TabContainer::displayMap(const QModelIndex &aIndex)
     }
 
     QPixmap map(mapPath);
-    MapWindow window(map, room.name(),this);
+    MapWindow window(map, event->room()->name(),this);
     window.exec();
 }
 
index ab0774f..403795e 100644 (file)
@@ -153,7 +153,7 @@ void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
         titlePointF.setY(titlePointF.y()+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);
index 40d9f32..1cd600d 100644 (file)
@@ -38,7 +38,7 @@ QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>()
     << QSqlField("description", QVariant::String));
 
 Event::Event() :
-    mRoomId( 0 )
+    room_(NULL)
 {
 }
 
@@ -119,28 +119,9 @@ QList<Event> Event::getFavByDate(const QDate& date, int conferenceId)
     return load(query);
 }
 
-QString Event::room()
+Room* Event::room()
 {
-    if ( mRoomName.isEmpty() )
-    {
-        QSqlQuery query;
-        // TODO: conference ID isn't used here
-        query.prepare("SELECT name FROM room WHERE id = :roomId");
-        query.bindValue(":roomId", roomId());
-        query.exec();
-        // TODO: handle qeury error
-        //qDebug() << query.lastError();
-        if(query.next())
-            mRoomName = query.record().value("name").toString();
-        else
-            mRoomName = QString("not-available");
-    }
-    return mRoomName;
-}
-
-int Event::roomId()
-{
-    if ( mRoomId == 0 )
+    if (room_ == NULL)
     {
         QSqlQuery query;
         query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
@@ -154,9 +135,20 @@ int Event::roomId()
             qDebug() << "No room found for event id: " << id();
             throw OrmNoObjectException();
         }
-        mRoomId = query.record().value("xid_room").toInt();
+        int id = query.record().value("xid_room").toInt();
+        room_ = new Room(Room::retrieve(id));
     }
-    return mRoomId;
+    return room_;
+}
+
+QString Event::roomName()
+{
+    return room()->name();
+}
+
+int Event::roomId()
+{
+    return room()->id();
 }
 
 QStringList Event::persons()
index a4a73f0..7bd8bb1 100644 (file)
@@ -25,6 +25,8 @@
 
 #include <ormrecord.h>
 
+class Room;
+
 /**
   NoSuchEventException is thrown when required event does not exist.
 */
@@ -64,7 +66,8 @@ public:
     QString abstract() const { return value("abstract").toString(); }
     QString description() const { return value("description").toString(); }
     // records from other tables associated with 'id'
-    QString room();
+    Room* room();
+    QString roomName();
     int roomId();
     QStringList persons();
     QMap<QString,QString> links();
@@ -95,6 +98,7 @@ private:
     QMap<QString,QString> mLinksList;
     int mRoomId;
     QString mRoomName;
+    Room* room_;
 };
 
 #endif // EVENT_H