CallTreeView.cpp 6.74 KB
Newer Older
1 2
/***************************************************************************
 *   Copyright (C) 2010 by Savoir-Faire Linux                              *
3 4
 *   Author : Mathieu Leduc-Hamel mathieu.leduc-hamel@savoirfairelinux.com *
 *            Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>*
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

22 23
#include <QtCore/QMimeData>

24 25 26 27
#include "CallTreeView.h"
#include "CallTreeModel.h"
#include "CallTreeItem.h"
#include "Call.h"
28
#include <QDebug>
29 30

CallTreeView::CallTreeView(QWidget * parent)
31 32 33 34
   : QTreeView(parent)
{    
   treeModel = new CallTreeModel(this);
   setModel(treeModel);
35 36
//    CallTreeItemDelegate *delegate = new CallTreeItemDelegate();
//    setItemDelegate(delegate); 
37 38 39 40 41 42 43
   setHeaderHidden(true);
   setRootIsDecorated(false);
   setSelectionMode(QAbstractItemView::SingleSelection);
   setDragEnabled(true);
   setAcceptDrops(true);
   setUniformRowHeights(true);
   setDropIndicatorShown(true);
44 45 46 47 48 49 50 51 52 53 54
   //setDragDropMode(QAbstractItemView::DragDrop);
   setSelectionMode(QAbstractItemView::ExtendedSelection);
   
   setDragEnabled(TRUE);
   setAcceptDrops(TRUE);
   setDropIndicatorShown(TRUE);
   
   connect(this , SIGNAL(clicked(QModelIndex)), this, SLOT(itemClicked(QModelIndex)));
   connect(treeModel, SIGNAL(joinCall(QString,QString)), this, SLOT(joinCall(QString, QString)));
   connect(treeModel, SIGNAL(joinCall(QString,QString)), this, SLOT(expandAll()));
   connect(treeModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex& ) ), this, SLOT(adaptColumns(const QModelIndex &, const QModelIndex&) ) );
55 56 57 58
}

CallTreeView::~CallTreeView()
{
59
   delete treeModel;
60 61 62 63
}

CallTreeItem* CallTreeView::insert(Call *call)
{
64 65 66 67 68 69 70
   QModelIndex index = selectionModel()->currentIndex();
   int position = index.row()+1;
   QModelIndex parent = index.parent();
   CallTreeItem *item;

   if (!treeModel->insertRow(position, parent))
      return 0;
71 72 73 74
        
        QModelIndex child = model()->index(index.row()+1, 0, index.parent());
        treeModel->setData(child, QVariant(""), Qt::EditRole);
        
75
        for (int column = 1; column < treeModel->columnCount(index); ++column) {
76 77 78 79
                QModelIndex child2 = treeModel->index(index.row()+1, column, index.parent());
                treeModel->setData(child2, QString("test"), Qt::EditRole);
        }
        
80 81
   item = treeModel->getItem(child);
    item->setCall(call);
82 83 84 85 86 87 88 89 90
//         qDebug() << "Will connect, id " << call << ", " << call->getPeerPhoneNumber();
//         connect(call, SIGNAL(changed()), item, SLOT(updated()));
//         item->setCall(call);
//         item->setData(1,call->getPeerPhoneNumber());
//         item->setData(2,call->getPeerName());
//         resizeColumnToContents(0);
//         resizeColumnToContents(1);
//         resizeColumnToContents(2);
//         //item->updated();
91
   setIndexWidget(child, item->widget());
92 93 94 95
}

CallTreeItem* CallTreeView::insert(CallTreeItem *parent, Call *call)
{
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
   QModelIndex index = selectionModel()->currentIndex();

   if (treeModel->columnCount(index) == 0) {
      if (!model()->insertColumn(0, index)) {
         return 0; 
      }
         
   }
      
   if (!treeModel->insertRow(0, index)) {
      return 0;
   }

   CallTreeItem *item = treeModel->getItem(index);
   
   for (int column = 0; column < treeModel->columnCount(index); ++column) {
      QModelIndex child = treeModel->index(0, column, index);
113
                qDebug() << "I just added data: 0, " << column << " \n\n\n\n";
114 115
      treeModel->setData(child, QVariant(""), Qt::EditRole);      
   }
116

117 118
   item->setCall(call);
   selectionModel()->setCurrentIndex(model()->index(0, 0, index), QItemSelectionModel::ClearAndSelect);
119

120
   QModelIndex newIndex = selectionModel()->currentIndex();
121

122
   return treeModel->getItem(newIndex);
123
}
124

125
void CallTreeView::remove(QModelIndex & index) const
126
{
127
   treeModel->removeRow(index.row(), index.parent());
128
}
129

130 131 132 133 134 135 136 137 138
void CallTreeView::remove(Call* call) const //BUG not used
{
  for(int i=0; i < 15/* model.rowCount()*/;i++) { //TODO anything better
    QModelIndex anIndex = this->indexAt(QPoint(0,i));
    if (anIndex.isValid()) {
      qDebug() << "This index is valid";
    }
  }
}
139

140
void CallTreeView::removeCurrent() const
141
{
142 143
   QModelIndex index = selectionModel()->currentIndex();
   treeModel->removeRow(index.row(), index.parent());
144 145
}

146 147
CallTreeItem* CallTreeView::currentItem()
{
148
   QModelIndex index = selectionModel()->currentIndex();      
149

150
   CallTreeItem *item = treeModel->getItem(index);
151

152 153 154 155
   if (!item->call()) {
      return 0;
   }
   return item;      
156 157 158 159
}

CallTreeItem* CallTreeView::getItem(const QModelIndex &index)
{
160
   return treeModel->getItem(index);
161 162 163 164
}

void CallTreeView::setCurrentRow(int row)
{
165
   CallTreeModel * treeModel = static_cast<CallTreeModel*>(model());
166

167 168 169
   QModelIndex currentIndex = selectionModel()->currentIndex();
   QModelIndex index = treeModel->index(row, 0, currentIndex);
   selectionModel()->setCurrentIndex(index,  QItemSelectionModel::Current);
170 171 172 173
}

int CallTreeView::count()
{
174
   return model()->rowCount();
175
}
176 177 178

QStringList CallTreeView::mimeTypes() const
{
179
  
180 181 182 183
}

Qt::DropActions CallTreeView::supportedDropActions () const
{
184 185
  return Qt::CopyAction | Qt::MoveAction;
}
186

187 188 189 190 191
void CallTreeView::itemClicked(const QModelIndex& anIndex) 
{
  if (currentModel != anIndex)
    emit itemChanged();
  currentModel = anIndex;
192
}
193 194 195 196 197 198 199 200 201 202 203

void CallTreeView::adaptColumns (const QModelIndex & topleft, const QModelIndex& bottomRight)
{
  int firstColumn= topleft.column();
  int lastColumn = bottomRight.column();
  do {
    //if (firstColumn) //TODO remove this and fix the resulting bug
      resizeColumnToContents(firstColumn);
    firstColumn++;
  } while (firstColumn < lastColumn);
}