00001 #include "clock.h"
00002 
00003 #include "taskmanagerdata.h"
00004 
00005 using namespace domain;
00006 
00007 Clock::Clock() : m_time(QDateTime::currentDateTime())
00008 {
00009     m_timer.setInterval(1000);
00010     connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
00011 }
00012 
00013 const QDateTime& Clock::time() const
00014 {
00015     return m_time;
00016 }
00017 
00018 void Clock::setTime(const QDateTime& time)
00019 {
00020     m_time = time;
00021 
00022     QList<StorableData*> notified;
00023 
00024     Q_FOREACH (QDateTime eventTime, m_events.keys())
00025     {
00026         if (eventTime > time)
00027             break;
00028 
00029         Q_FOREACH (StorableData* data, m_events.values(eventTime))
00030         {
00031             if (!notified.contains(data))
00032             {
00033                 data->notify();
00034                 notified << data;
00035             }
00036         }
00037 
00038         m_events.remove(eventTime);
00039     }
00040 
00041     emit timeChanged(time);
00042 }
00043 
00044 void Clock::addEvent(const QDateTime& time, StorableData* data)
00045 {
00046     if (time > m_time)
00047         m_events.insert(time, data);
00048     else if (time == m_time)
00049         data->notify();
00050 }
00051 
00052 void Clock::removeEvents(StorableData* data)
00053 {
00054     Q_FOREACH (QDateTime time, m_events.keys())
00055         m_events.remove(time, data);
00056 }
00057 
00058 void Clock::deleteEvents(StorableData* data)
00059 {
00060     TaskManagerData::instance()->clock()->removeEvents(data);
00061 }
00062 
00063 QDateTime Clock::currentTime()
00064 {
00065     return TaskManagerData::instance()->clock()->time();
00066 }
00067 
00068 void Clock::newEvent(const QDateTime& time, StorableData* data)
00069 {
00070     TaskManagerData::instance()->clock()->addEvent(time, data);
00071 }
00072 
00073 void Clock::setTicking(bool enable)
00074 {
00075     if (enable)
00076     {
00077         if (!m_timer.isActive())
00078             m_timer.start();
00079     }
00080     else
00081         m_timer.stop();
00082 }
00083 
00084 bool Clock::isTicking() const
00085 {
00086     return m_timer.isActive();
00087 }
00088 
00089 void Clock::tick()
00090 {
00091     setTime(m_time.addSecs(1));
00092 }