fix deletion of last conference
authorkirilma <kirilma@localhost>
Thu, 15 Apr 2010 12:50:23 +0000 (12:50 +0000)
committerkirilma <kirilma@localhost>
Thu, 15 Apr 2010 12:50:23 +0000 (12:50 +0000)
implement for cleaning all views in the tabs
clean the models when no active conference found
fix cleaning model and signalling views

src/gui/mainwindow.cpp
src/gui/mainwindow.h
src/gui/tabcontainer.cpp
src/gui/tabcontainer.h
src/mvc/eventmodel.cpp
src/mvc/eventmodel.h

index b2d33d7..80321c3 100644 (file)
@@ -50,6 +50,8 @@ MainWindow::MainWindow(int aEventId, QWidget *aParent)
 {
     setupUi(this);
 
+    saved_title = windowTitle();
+
 #ifdef N810
     tabWidget->setTabText(1,"Favs");
     //tabWidget->setTabText(2,"Day");
@@ -98,22 +100,17 @@ MainWindow::MainWindow(int aEventId, QWidget *aParent)
         fillAndShowConferenceHeader();
         setWindowTitle(Conference::getById(confId).title());
 
-        if(confCount==1) // don't have to show 'selectConference' widget, if there is only one conference in the DB
-            selectConferenceWidget->hide();
-        else
+        QList<Conference> confs = Conference::getAll();
+        QListIterator<Conference> i(confs);
+        while(i.hasNext())
         {
-            // have to fill comboBox with available conferences
-            QList<Conference> confs = Conference::getAll();
-            QListIterator<Conference> i(confs);
-            while(i.hasNext())
-            {
-                Conference conf = i.next();
-                selectConference->addItem(conf.title(),conf.id());
-            }
-            int idx = selectConference->findText(Conference::getById(Conference::activeConference()).title());
-            selectConference->setCurrentIndex(idx);
+            Conference conf = i.next();
+            selectConference->addItem(conf.title(),conf.id());
         }
+        int idx = selectConference->findText(Conference::getById(Conference::activeConference()).title());
+        selectConference->setCurrentIndex(idx);
         connect(selectConference, SIGNAL(currentIndexChanged(int)), SLOT(conferenceChanged(int)));
+        conferenceChanged(idx);
     }
     else
     {
@@ -154,8 +151,7 @@ void MainWindow::scheduleImported(int aConfId)
         int idx = selectConference->findText(conf.title());
         selectConference->setCurrentIndex(idx);
 
-        if(confCount>1)
-            selectConferenceWidget->show();
+        selectConferenceWidget->show();
 
         conferenceChanged(idx);
     }
@@ -168,6 +164,14 @@ void MainWindow::scheduleDeleted(const QString& title)
     if (idx == -1) {
         // should not happen
         qWarning() << __PRETTY_FUNCTION__ << "removed non-existent item:" << title;
+        // this happens when you remove the only conference (the list is not ptoperly inited in this case)
+        // now make sure that conferencet
+        if (selectConference->count() > 0) {
+            selectConference->setCurrentIndex(0);
+            conferenceChanged(0);
+        } else {
+            conferenceChanged(-1);
+        }
     } else {
         // will it signal "changed"?
         selectConference->removeItem(idx);
@@ -237,10 +241,36 @@ void MainWindow::initTabs()
     nowTabContainer->updateTreeView(QDate::currentDate());
 }
 
+void MainWindow::unsetConference()
+{
+    dayTabContainer->clearModel();
+    tracksTabContainer->clearModel();
+    roomsTabContainer->clearModel();
+    favsTabContainer->clearModel();
+    searchTabContainer->clearModel();
+    searchTabContainer->searchAgainClicked();
+    nowTabContainer->clearModel();
+
+    conferenceHeader->hide();
+    setWindowTitle(saved_title);
+}
+
 void MainWindow::conferenceChanged(int aIndex)
 {
-    Conference::getById(Conference::activeConference()).update("active",0);
-    Conference::getById(selectConference->itemData(aIndex).toInt()).update("active",1);
+    if (aIndex < 0) {
+        // no conferences left? reset all views
+        unsetConference();
+        return;
+    }
+
+    try {
+        Conference::getById(Conference::activeConference()).update("active",0);
+        Conference::getById(selectConference->itemData(aIndex).toInt()).update("active",1);
+    } catch (OrmException& e) {
+        // cannon set an active conference
+        unsetConference();
+        return;
+    }
 
     initTabs();
     fillAndShowConferenceHeader();
index 7740b85..c069636 100644 (file)
@@ -42,6 +42,9 @@ private slots:
 private:
     void fillAndShowConferenceHeader();
     void initTabs();
+    void unsetConference();
+
+    QString saved_title;
 };
 
 #endif /* MAINWINDOW_H */
index 04c1bd9..01900d8 100644 (file)
@@ -55,9 +55,13 @@ TabContainer::TabContainer(QWidget *aParent)
 
 void TabContainer::updateTreeView(const QDate &aDate)
 {
+    int active_id = Conference::activeConference();
     dayNavigator->show();
-    loadEvents( aDate, Conference::activeConference() );
-    treeView->reset();
+    if (active_id > 0) {
+        loadEvents(aDate, active_id);
+    } else {
+        static_cast<EventModel*>(treeView->model())->clearModel();
+    }
 }
 
 void TabContainer::itemClicked(const QModelIndex &aIndex)
@@ -132,3 +136,8 @@ void TabContainer::setDates(const QDate &aStart, const QDate &aEnd)
     dayNavigator->setDates(aStart, aEnd);
 }
 
+void TabContainer::clearModel()
+{
+    static_cast<EventModel*>(treeView->model())->clearModel();
+}
+
index e292c06..c4a4b88 100644 (file)
@@ -35,6 +35,7 @@ public:
     TabContainer(QWidget *aParent = NULL);
     virtual ~TabContainer() {}
 
+    void clearModel();
 protected:
     virtual void loadEvents( const QDate &aDate, const int aConferenceId )
     {
index a9cca04..c57702a 100644 (file)
@@ -24,9 +24,7 @@
 const QString EventModel::COMMA_SEPARATOR = ", ";
 
 EventModel::EventModel()
-{
-    mEvents.clear();
-}
+{ }
 
 void EventModel::createTimeGroups()
 {
@@ -62,6 +60,8 @@ void EventModel::createTimeGroups()
     }
 
     mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
+
+    reset();
 }
 
 void EventModel::createTrackGroups() {
@@ -199,15 +199,12 @@ int EventModel::rowCount (const QModelIndex & parent) const
 
 void EventModel::clearModel()
 {
-    for(int i = 0;i < mGroups.count();i++){
-        QModelIndex idx = index(i, 0);
-        Group group = mGroups[i];
-        beginRemoveRows(idx, 0, group.mChildCount - 1);
-        /*bool ok =*/ removeRows(0, group.mChildCount, idx);
-        endRemoveRows();
-        //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
-    }
+    qDebug() << __PRETTY_FUNCTION__ << this << mEvents.count();
+    mGroups.clear();
     mEvents.clear();
+    mParents.clear();
+
+    reset();
 }
 
 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
index 9e90ec4..5a21f36 100644 (file)
@@ -41,6 +41,7 @@ public:
     void loadNowEvents(int aConferenceId); // loads Now events from the DB
     void loadEventsByRoom(const QDate &aDate, int aConferenceId);
     void loadConflictEvents(int aEventId, int aConferenceId); // loads events in conflict
+    void clearModel();
 
 private:
     struct Group
@@ -62,7 +63,6 @@ private:
     void createTimeGroups();
     void createTrackGroups();
     void createTrackGroupsNew();
-    void clearModel();
     void createRoomGroups();
 
 public slots: