HistoryDock.cpp 9.09 KB
Newer Older
1 2 3 4 5 6 7
#include "HistoryDock.h"

#include <QtGui/QVBoxLayout>
#include <kicon.h>
#include <klineedit.h>
#include <QtGui/QTreeWidget>
#include <QtGui/QComboBox>
8
#include <QtGui/QPushButton>
9
#include <QtGui/QLabel>
10
#include <kdatewidget.h>
11
#include <QHeaderView>
12
#include <QtGui/QCheckBox>
13 14 15
#include <QDate>
#include "SFLPhone.h"
#include "widgets/HistoryTreeItem.h"
16
#include "conf/ConfigurationSkeleton.h"
17
#include "AkonadiBackend.h"
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
class QNumericTreeWidgetItem : public QTreeWidgetItem {
   public:
      QNumericTreeWidgetItem(QTreeWidget* parent):QTreeWidgetItem(parent),widget(0),weight(-1){}
      HistoryTreeItem* widget;
      int weight;
   private:
      bool operator<(const QTreeWidgetItem & other) const {
         int column = treeWidget()->sortColumn();
         if (dynamic_cast<QNumericTreeWidgetItem*>((QTreeWidgetItem*)&other)) {
            if (widget !=0 && dynamic_cast<QNumericTreeWidgetItem*>((QTreeWidgetItem*)&other)->widget != 0)
               return widget->getTimeStamp() < dynamic_cast<QNumericTreeWidgetItem*>((QTreeWidgetItem*)&other)->widget->getTimeStamp();
            else if (weight > 0 && dynamic_cast<QNumericTreeWidgetItem*>((QTreeWidgetItem*)&other)->weight > 0)
               return weight > dynamic_cast<QNumericTreeWidgetItem*>((QTreeWidgetItem*)&other)->weight;
         }
         return text(column) < other.text(column);
      }
};

37 38
HistoryDock::HistoryDock(QWidget* parent) : QDockWidget(parent)
{
39 40
   setMinimumSize(250,0);
   setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
41 42 43
   m_pFilterLE   = new KLineEdit();
   m_pItemView   = new QTreeWidget(this);
   m_pSortByCBB  = new QComboBox();
44 45 46 47 48 49
   m_pSortByL    = new QLabel("Sort by:");
   m_pFromL      = new QLabel("From:");
   m_pToL        = new QLabel("To:");
   m_pFromDW     = new KDateWidget();
   m_pToDW       = new KDateWidget();
   m_pAllTimeCB  = new QCheckBox("Display all");
50
   m_pLinkPB     = new QPushButton(this);
51 52
   
   m_pAllTimeCB->setChecked(ConfigurationSkeleton::displayDataRange());
53 54

   m_pSortByL->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Preferred);
55
   m_pSortByCBB->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
56 57 58 59
   m_pLinkPB->setMaximumSize(20,9999999);
   m_pLinkPB->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
   m_pLinkPB->setCheckable(true);
   
60 61 62 63
   m_pItemView->headerItem()->setText(0,"Calls"          );
   m_pItemView->header    ()->setClickable(true          );
   m_pItemView->header    ()->setSortIndicatorShown(true );
   m_pItemView->setAlternatingRowColors(true             );
64 65 66

   m_pFilterLE->setPlaceholderText("Filter");
   m_pFilterLE->setClearButtonShown(true);
67 68
   
   QStringList sortBy;
69
   sortBy << "Date" << "Name" << "Popularity" << "Duration";
70
   m_pSortByCBB->addItems(sortBy);
71 72 73 74 75 76 77

   QWidget* mainWidget = new QWidget(this);
   setWidget(mainWidget);

   QGridLayout* mainLayout = new QGridLayout(mainWidget);

   mainLayout->addWidget(m_pSortByL   ,0,0     );
78 79 80
   mainLayout->addWidget(m_pSortByCBB ,0,1,1,2 );
   mainLayout->addWidget(m_pAllTimeCB ,1,0,1,3 );
   mainLayout->addWidget(m_pLinkPB    ,3,2,3,1 );
81 82 83 84
   mainLayout->addWidget(m_pFromL     ,2,0,1,2 );
   mainLayout->addWidget(m_pFromDW    ,3,0,1,2 );
   mainLayout->addWidget(m_pToL       ,4,0,1,2 );
   mainLayout->addWidget(m_pToDW      ,5,0,1,2 );
85 86
   mainLayout->addWidget(m_pItemView  ,6,0,1,3 );
   mainLayout->addWidget(m_pFilterLE  ,7,0,1,3 );
87 88
   
   setWindowTitle("History");
89 90 91

   QDate date(2000,1,1);
   m_pFromDW->setDate(date);
92
   
93 94 95 96
   reload();
   m_pCurrentFromDate = m_pFromDW->date();
   m_pCurrentToDate   = m_pToDW->date();

97 98 99 100 101 102
   connect(m_pAllTimeCB,                  SIGNAL(toggled(bool)),            this, SLOT(enableDateRange(bool)       ));
   connect(m_pFilterLE,                   SIGNAL(textChanged(QString)),     this, SLOT(filter(QString)             ));
   connect(m_pFromDW  ,                   SIGNAL(changed(QDate)),           this, SLOT(updateLinkedFromDate(QDate) ));
   connect(m_pToDW    ,                   SIGNAL(changed(QDate)),           this, SLOT(updateLinkedToDate(QDate)   ));
   connect(m_pSortByCBB,                  SIGNAL(currentIndexChanged(int)), this, SLOT(reload()                    ));
   connect(AkonadiBackend::getInstance(), SIGNAL(collectionChanged()),      this, SLOT(updateContactInfo()         ));
103 104 105 106 107
}

HistoryDock::~HistoryDock()
{
}
108

109 110 111 112 113 114 115 116
QString HistoryDock::getIdentity(HistoryTreeItem* item)
{
   if (item->getName().trimmed().isEmpty())
      return item->getPhoneNumber();
   else
      return item->getName();
}

117 118 119 120 121 122 123
void HistoryDock::updateContactInfo()
{
   foreach(HistoryTreeItem* hitem, m_pHistory) {
      hitem->updated();
   }
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
void HistoryDock::reload()
{
   m_pItemView->clear();
   foreach(HistoryTreeItem* hitem, m_pHistory) {
      delete hitem;
   }
   m_pHistory.clear();
   foreach (Call* call, SFLPhone::app()->model()->getHistory()) {
      if (!m_pAllTimeCB->isChecked() || (QDateTime(m_pFromDW->date()).toTime_t() < call->getStartTimeStamp().toUInt() && QDateTime(m_pToDW->date().addDays(1)).toTime_t() > call->getStartTimeStamp().toUInt() )) {
         HistoryTreeItem* callItem = new HistoryTreeItem(m_pItemView);
         callItem->setCall(call);
         m_pHistory << callItem;
      }
   }
   switch (m_pSortByCBB->currentIndex()) {
      case Date:
         foreach(HistoryTreeItem* hitem, m_pHistory) {
            QNumericTreeWidgetItem* item = new QNumericTreeWidgetItem(m_pItemView);
            item->widget = hitem;
            hitem->setItem(item);
            m_pItemView->addTopLevelItem(item);
            m_pItemView->setItemWidget(item,0,hitem);
         }
         break;
      case Name: {
         QHash<QString,QTreeWidgetItem*> group;
         foreach(HistoryTreeItem* item, m_pHistory) {
            if (!group[getIdentity(item)]) {
               group[getIdentity(item)] = new QTreeWidgetItem(m_pItemView);
               group[getIdentity(item)]->setText(0,getIdentity(item));
               m_pItemView->addTopLevelItem(group[getIdentity(item)]);
            }
            QTreeWidgetItem* twItem = new QTreeWidgetItem(group[getIdentity(item)]);
            item->setItem(twItem);
            m_pItemView->setItemWidget(twItem,0,item);
         }
         break;
      }
      case Popularity: {
         QHash<QString,QNumericTreeWidgetItem*> group;
         foreach(HistoryTreeItem* item, m_pHistory) {
            if (!group[getIdentity(item)]) {
               group[getIdentity(item)] = new QNumericTreeWidgetItem(m_pItemView);
               group[getIdentity(item)]->weight = 0;
               m_pItemView->addTopLevelItem(group[getIdentity(item)]);
            }
            group[getIdentity(item)]->weight++;
            group[getIdentity(item)]->setText(0,getIdentity(item)+" ("+QString::number(group[getIdentity(item)]->weight)+")");
            QTreeWidgetItem* twItem = new QTreeWidgetItem(group[getIdentity(item)]);
            item->setItem(twItem);
            m_pItemView->setItemWidget(twItem,0,item);
         }
         break;
      }
      case Duration:
         foreach(HistoryTreeItem* hitem, m_pHistory) {
            QNumericTreeWidgetItem* item = new QNumericTreeWidgetItem(m_pItemView);
            item->weight = hitem->getDuration();
            hitem->setItem(item);
            m_pItemView->addTopLevelItem(item);
            m_pItemView->setItemWidget(item,0,hitem);
         }
         break;
   }
   m_pItemView->sortItems(0,Qt::AscendingOrder);
}

191 192 193 194 195 196
void HistoryDock::enableDateRange(bool enable)
{
   m_pFromL->setVisible(enable);
   m_pToL->setVisible(enable);
   m_pFromDW->setVisible(enable);
   m_pToDW->setVisible(enable);
197
   m_pLinkPB->setVisible(enable);
198 199
   
   ConfigurationSkeleton::setDisplayDataRange(enable);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
}

void HistoryDock::filter(QString text)
{
   foreach(HistoryTreeItem* item, m_pHistory) {
      bool visible = (item->getName().toLower().indexOf(text) != -1) || (item->getPhoneNumber().toLower().indexOf(text) != -1);
      item->getItem()-> setHidden(!visible);
   }
   m_pItemView->expandAll();
}

void HistoryDock::updateLinkedDate(KDateWidget* item, QDate& prevDate, QDate& newDate)
{
   if (m_pLinkPB->isChecked()) {
      if (prevDate.day() != newDate.day()) {
         QDate tmp = item->date();
         tmp = tmp.addDays(newDate.day() - prevDate.day());
         item->setDate(tmp);
      }
      if (prevDate.month() != newDate.month()) {
         QDate tmp = item->date();
         tmp = tmp.addMonths(newDate.month() - prevDate.month());
         item->setDate(tmp);
      }
      if (prevDate.year() != newDate.year()) {
         QDate tmp = item->date();
         tmp = tmp.addYears(newDate.year() - prevDate.year());
         item->setDate(tmp);
      }
   }
   prevDate = newDate;
}

void HistoryDock::updateLinkedFromDate(QDate date)
{
   disconnect(m_pToDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedToDate(QDate)));
   updateLinkedDate(m_pToDW,m_pCurrentFromDate,date);
   connect(m_pToDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedToDate(QDate)));
}

void HistoryDock::updateLinkedToDate(QDate date)
{
   disconnect(m_pFromDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedFromDate(QDate)));
   updateLinkedDate(m_pFromDW,m_pCurrentToDate,date);
   connect(m_pFromDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedFromDate(QDate)));
245
}