From 412cef6f84e31a76b3efd69e64fe83400c8ed8d0 Mon Sep 17 00:00:00 2001 From: pavelpa Date: Thu, 28 Jan 2010 12:43:19 +0000 Subject: [PATCH] some 'delegate' drawing optimizations - removed EVENT_CONFLICT table - used one SQL SELECT instead --- src/gui/eventdialog.cpp | 15 +++++- src/mvc/delegate.cpp | 3 -- src/mvc/event.cpp | 105 ++++++---------------------------------- src/mvc/event.h | 2 - src/mvc/treeview.cpp | 3 +- src/sql/sqlengine.cpp | 9 ---- 6 files changed, 30 insertions(+), 107 deletions(-) diff --git a/src/gui/eventdialog.cpp b/src/gui/eventdialog.cpp index be98e29..12838a5 100644 --- a/src/gui/eventdialog.cpp +++ b/src/gui/eventdialog.cpp @@ -45,6 +45,7 @@ void EventDialog::favouriteClicked() { Event event = Event::getById(mEventId,Conference::activeConference()); + QList conflicts = Event::conflictEvents(event.id(),Conference::activeConference()); if(event.isFavourite()) { event.setFavourite(false); @@ -56,12 +57,22 @@ void EventDialog::favouriteClicked() favouriteButton->setIcon(QIcon(":/icons/favourite-onBig.png")); } event.update("favourite"); + + if(event.isFavourite()) + { + // event has became 'favourite' and so 'conflicts' list may have changed + conflicts = Event::conflictEvents(event.id(),Conference::activeConference()); + } + qDebug() << " FAVOURITE [" << event.id() << "] -> " << event.isFavourite(); - // update EVENT_CONFLICT table - event.updateConflicts(); + // since the Favourite icon has changed, update TreeViews accordingly // all TreeViews have to listen on this signal emit(eventHasChanged(event.id())); + + // have to emit 'eventHasChanged' signal on all events in conflict + for(int i=0; i(index.internalPointer())->isFavourite()) if(static_cast(index.internalPointer())->hasTimeConflict()) - { bkgrColor = Qt::yellow; - } if(isLast(index)) { diff --git a/src/mvc/event.cpp b/src/mvc/event.cpp index ba7bbc4..07001f1 100644 --- a/src/mvc/event.cpp +++ b/src/mvc/event.cpp @@ -71,9 +71,19 @@ QList Event::nowEvents(int conferenceId, QString orderBy) QList Event::conflictEvents(int aEventId, int conferenceId) { QSqlQuery query; - query.prepare( selectQuery() + QString("WHERE id IN ( SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf ) ORDER BY %1").arg("start")); - query.bindValue(":id", aEventId); - query.bindValue(":conf", conferenceId); + Event event = Event::getById(aEventId,conferenceId); + query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \ + ( start <= :start1 AND ( start + duration ) >= :start2 ) \ + OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \ + OR ( start <= :end2 AND ( start + duration ) >= :end3 ) ) AND favourite = 1 AND NOT id = :id ORDER BY start"); + query.bindValue(":conf", event.conferenceId()); + query.bindValue(":start1", convertToDb(event.start(), QVariant::DateTime)); + query.bindValue(":start2", convertToDb(event.start(), QVariant::DateTime)); + query.bindValue(":start3", convertToDb(event.start(), QVariant::DateTime)); + query.bindValue(":end1", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime)); + query.bindValue(":end2", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime)); + query.bindValue(":end3", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime)); + query.bindValue(":id", event.id()); return load(query); } @@ -151,95 +161,12 @@ QMap Event::links() const return links; } -QList Event::conflicts() const -{ - QSqlQuery query; - // TODO: conference ID isn't used here - query.prepare("SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf"); - query.bindValue(":id", id()); - query.bindValue(":conf", conferenceId()); - query.exec(); - // TODO: handle qeury error - //qDebug() << query.lastError(); - - QList conflicts; - while(query.next()) - conflicts.append(query.record().value("conflict_event").toInt()); - - return conflicts; -} - bool Event::hasTimeConflict() const { - return conflicts().count() > 0 ? true : false; -} - -void Event::updateConflicts() -{ - qDebug() << "updating conflicts"; - QSqlQuery query; - query.prepare("SELECT id FROM event WHERE xid_conference = :conf AND ( \ - ( start <= :start1 AND ( start + duration ) >= :start2 ) \ - OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \ - OR ( start <= :end2 AND ( start + duration ) >= :end3 ) ) AND favourite = 1 ORDER BY start"); - query.bindValue(":conf", conferenceId()); - query.bindValue(":start1", convertToDb(start(), QVariant::DateTime)); - query.bindValue(":start2", convertToDb(start(), QVariant::DateTime)); - query.bindValue(":start3", convertToDb(start(), QVariant::DateTime)); - query.bindValue(":end1", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); - query.bindValue(":end2", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); - query.bindValue(":end3", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); - query.exec(); + if(!isFavourite()) // if it's not favourite, it can't have time-conflict + return false; - QList conflicts; - while(query.next()) - { - int idx = query.record().value("id").toInt(); - if(idx != id()) - conflicts.append(idx); - } - - if(isFavourite()) // event became favourite - { - for(int i=0; i " << conflicts[i]; - - QSqlQuery queryRemove; - queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id1 AND xid_conference = :conf AND conflict_event = :id2"); - queryRemove.bindValue(":id1",conflicts[i]); - queryRemove.bindValue(":conf",conferenceId()); - queryRemove.bindValue(":id2",id()); - queryRemove.exec(); - } - } + return conflictEvents(id(),conferenceId()).count() > 0 ? true : false; } void Event::setRoom(const QString &room) diff --git a/src/mvc/event.h b/src/mvc/event.h index 8d55cef..6ddc442 100644 --- a/src/mvc/event.h +++ b/src/mvc/event.h @@ -48,7 +48,6 @@ public: QString room() const; int roomId() const; QStringList persons() const; - QList conflicts() const; QMap links() const; void setId(int id) { setValue("id", id); } @@ -69,7 +68,6 @@ public: void setRoom(const QString& room); void setPersons(const QStringList &persons); void setLinks(const QMap &aLinks); - void updateConflicts(); friend class EventTest; }; diff --git a/src/mvc/treeview.cpp b/src/mvc/treeview.cpp index 0b3b632..36f1f69 100644 --- a/src/mvc/treeview.cpp +++ b/src/mvc/treeview.cpp @@ -57,8 +57,7 @@ bool TreeView::testForControlClicked(const QModelIndex &aIndex, const QPoint &aP event.update("favourite"); qDebug() << " FAVOURITE [" << qVariantValue(aIndex.data()) << "] -> " << event.isFavourite(); - // update EVENT_CONFLICT table - event.updateConflicts(); + if(event.isFavourite()) { // event has became 'favourite' and so 'conflicts' list may have changed diff --git a/src/sql/sqlengine.cpp b/src/sql/sqlengine.cpp index ea56d7a..fcaceb2 100644 --- a/src/sql/sqlengine.cpp +++ b/src/sql/sqlengine.cpp @@ -152,7 +152,6 @@ void SqlEngine::addEventToDB(QHash &aEvent) } } - void SqlEngine::addPersonToDB(QHash &aPerson) { QSqlDatabase db = QSqlDatabase::database(); @@ -289,14 +288,6 @@ bool SqlEngine::createTables(QSqlDatabase &aDatabase) "FOREIGN KEY(xid_event) REFERENCES EVENT(id), " "FOREIGN KEY(xid_room) REFERENCES ROOM(id));"); - query.exec("CREATE TABLE EVENT_CONFLICT ( " - "xid_conference INTEGER NOT NULL, " - "xid_event INTEGER NOT NULL, " - "conflict_event INTEGER NOT NULL, " - "UNIQUE ( xid_conference, xid_event, conflict_event ) ON CONFLICT IGNORE, " - "FOREIGN KEY(xid_conference) REFERENCES CONFERENCE(id), " - "FOREIGN KEY(xid_event) REFERENCES EVENT(id));"); - query.exec("CREATE TABLE LINK ( " "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " "xid_conference INTEGER NOT NULL, " -- 2.39.5