CallTreeModel.cpp 10.4 KB
Newer Older
1 2 3 4
/***************************************************************************
 *   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).     *
 *   All rights reserved.                                                  *
 *   Contact: Nokia Corporation (qt-info@nokia.com)                        *
5 6
 *   Author : Mathieu Leduc-Hamel mathieu.leduc-hamel@savoirfairelinux.com *
 *            Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>*
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *                                                                         *
 *   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.             *
 ***************************************************************************/

#include <QtGui>
25
#include <klocale.h>
26 27 28

#include "CallTreeModel.h"
#include "CallTreeItem.h"
29
#include "calllist_interface_singleton.h"
30 31

CallTreeModel::CallTreeModel(QObject *parent)
32 33 34
   : QAbstractItemModel(parent),
     rootItem(0)
     
35
{
36 37 38
   QStringList data = QString("Calls").split("\n");
   QVector<QVariant> rootData;
   rootData << i18n("Calls");
39

40 41
   rootItem = new CallTreeItem(rootData, 0);
   setupModelData(data, rootItem);
42 43 44 45
}

CallTreeModel::~CallTreeModel()
{
46 47 48
   if(rootItem) {
      delete rootItem;
   }
49 50 51 52
}

int CallTreeModel::columnCount(const QModelIndex & /* parent */) const
{
53
   return rootItem->columnCount();
54 55 56
}

QVariant CallTreeModel::data(const QModelIndex &index, int role) const
57
{
58 59 60
   if (!index.isValid()) {
      return QVariant();
   }
61

62 63 64
   if (role != Qt::DisplayRole && role != Qt::EditRole) {
      return QVariant();
   }
65

66
   CallTreeItem *item = getItem(index);
67

68
   return item->data(index.column());
69 70 71
}

Call* CallTreeModel::call(const QModelIndex &index, int role) const
72
{
73 74 75
   if (!index.isValid()) {
      return 0;
   }
76

77 78 79
   if (role != Qt::DisplayRole && role != Qt::EditRole) {
      return 0;
   }
80

81
   CallTreeItem *item = getItem(index);
82

83
   return item->call();
84 85 86 87
}

Qt::ItemFlags CallTreeModel::flags(const QModelIndex &index) const
{
88 89 90 91
   if (!index.isValid()) {
      return 0;
   }
   Qt::ItemFlags val = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
92

93
   return val;
94 95 96 97
}

CallTreeItem *CallTreeModel::getItem(const QModelIndex &index) const
{
98 99 100 101 102 103 104
   if (index.isValid()) {
      CallTreeItem *item = static_cast<CallTreeItem*>(index.internalPointer());
      if (item) {
         return item;
      }
   }
   return rootItem;
105 106 107 108
}

QVariant CallTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
109 110 111 112
   if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
      return rootItem->data(section);
   }
   return QVariant();
113 114 115 116
}

QModelIndex CallTreeModel::index(int row, int column, const QModelIndex &parent) const
{
117 118 119 120 121 122 123 124 125 126 127 128 129
   if (parent.isValid() && parent.column() != 0) {
      return QModelIndex();
   }

   CallTreeItem *parentItem = getItem(parent);
   CallTreeItem *childItem = parentItem->child(row);

   if (childItem) {
      return createIndex(row, column, childItem);
   }
   else {
      return QModelIndex();
   }
130 131 132 133
}

bool CallTreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
{
134
   bool success;
135

136 137 138
   beginInsertColumns(parent, position, position + columns - 1);
   success = rootItem->insertColumns(position, columns);
   endInsertColumns();
139

140
   return success;
141 142 143 144
}

bool CallTreeModel::insertRows(int position, int rows, const QModelIndex &parent)
{
145 146
   CallTreeItem *parentItem = getItem(parent);
   bool success;
147

148 149 150
   beginInsertRows(parent, position, position + rows - 1);
   success = parentItem->insertChildren(position, rows, rootItem->columnCount());
   endInsertRows();
151

152
   return success;
153 154 155 156
}

QModelIndex CallTreeModel::parent(const QModelIndex &index) const
{
157 158 159
   if (!index.isValid()) {
      return QModelIndex();
   }
160

161 162
   CallTreeItem *childItem = getItem(index);
   CallTreeItem *parentItem = childItem->parent();
163

164 165 166
   if (parentItem == rootItem) {
      return QModelIndex();
   }
167

168
   return createIndex(parentItem->childNumber(), 0, parentItem);
169 170 171 172
}

bool CallTreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
{
173
   bool success;
174

175 176 177
   beginRemoveColumns(parent, position, position + columns - 1);
   success = rootItem->removeColumns(position, columns);
   endRemoveColumns();
178

179 180 181
   if (rootItem->columnCount() == 0) {
      removeRows(0, rowCount());
   }
182

183
   return success;
184 185 186 187
}

bool CallTreeModel::removeRows(int position, int rows, const QModelIndex &parent)
{
188 189
   CallTreeItem *parentItem = getItem(parent);
   bool success = true;
190

191 192 193
   beginRemoveRows(parent, position, position + rows - 1);
   success = parentItem->removeChildren(position, rows);
   endRemoveRows();
194

195
   return success;
196 197 198 199
}

int CallTreeModel::rowCount(const QModelIndex &parent) const
{
200
   CallTreeItem *parentItem = getItem(parent);
201

202
   return parentItem->childCount();
203 204 205 206
}

bool CallTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
207 208 209
   if (role != Qt::EditRole) {
      return false;
   }
210

211 212
   CallTreeItem *item = getItem(index);
   bool result = item->setData(index.column(), value);
213 214 215
        
        //item->setData(1, QString("test"));
        //item->setData(2, QString("test2"));
216

217 218 219
   if (result) {
      emit dataChanged(index, index);
   }
220

221
   return result;
222 223
}

224
bool CallTreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
225
{
226 227 228
   if (role != Qt::EditRole || orientation != Qt::Horizontal) {
      return false;
   }
229

230
   bool result = rootItem->setData(section, value);
231

232 233 234
   if (result) {
      emit headerDataChanged(orientation, section, section);
   }
235

236
   return result;
237 238 239 240
}

void CallTreeModel::setupModelData(const QStringList &lines, CallTreeItem *parent)
{
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
   QList<CallTreeItem*> parents;
   QList<int> indentations;
//   parents << parent;
   indentations << 0;

   int number = 0;

   while (number < lines.count()) {
      int position = 0;

      while (position < lines[number].length()) {
         if (lines[number].mid(position, 1) != " ") {
            break;
         }
         position++;
      }

      QString lineData = lines[number].mid(position).trimmed();

      if (!lineData.isEmpty()) {
         // Read the column data from the rest of the line.
         QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
         QVector<QVariant> columnData;
         for (int column = 0; column < columnStrings.count(); ++column) {
            columnData << columnStrings[column];
         }

         if (position > indentations.last()) {
            // The last child of the current parent is now the new parent
            // unless the current parent has no children.

            if (parents.last()->childCount() > 0) {
               parents << parents.last()->child(parents.last()->childCount()-1);
               indentations << position;
            }
         } 
         else {
            while (position < indentations.last() && parents.count() > 0) {
               parents.pop_back();
               indentations.pop_back();
            }
         }

         // Append a new item to the current parent's list of children.
         /*CallTreeItem *parent = parents.last();
         parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
         
         for (int column = 0; column < columnData.size(); ++column)
         {
            parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
            }*/
      }      
      number++;
   }
295
}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

Qt::DropActions CallTreeModel::supportedDropActions() 
{
  return Qt::MoveAction;// | Qt::CopyAction ;
}

bool CallTreeModel::dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ) {  
  
  if (action == Qt::IgnoreAction)
      return true;

  if (!data->hasFormat("application/vnd.text.list"))
      return false;

  if (column > 0)
      return false;
  
  int beginRow;

  if (row != -1)
      beginRow = row;
  else if (parent.isValid())
      beginRow = parent.row();
  else
      beginRow = rowCount(QModelIndex());
  
  CallTreeItem *item = getItem(parent);
  QByteArray encodedData = data->data("application/vnd.text.list");
  QDataStream stream(&encodedData, QIODevice::ReadOnly);
  QStringList newItems;
  int rows = 0;

  while (!stream.atEnd()) {
      QString text;
      stream >> text;
      newItems << text;
      ++rows;
  }
  
  if (rows) {
    Call* secondCall = CallListInterfaceSingleton::getInstance().findCallByCallId(newItems[0]);
    Call* conVersation = CallListInterfaceSingleton::getInstance().createConversationFromCall(item->call(), secondCall);
    emit joinCall(newItems[0], item->call()->getCallId());
    
     insertRows(beginRow, rows, parent);
     foreach (QString text, newItems) {
         QModelIndex idx = index(beginRow, 0, parent);
         setData(idx, "test");
         beginRow++;
     }
  }
  else
    qDebug() << "Unknow drop";

  return true;
}

//This can be modified later to implement an universal drag and drop
QStringList CallTreeModel::mimeTypes() const
{
    QStringList types;
    types << "application/vnd.text.list";
    return types;
}

QMimeData* CallTreeModel::mimeData(const QModelIndexList &indexes) const
{
     QMimeData *mimeData = new QMimeData();
     QByteArray encodedData;
     QDataStream stream(&encodedData, QIODevice::WriteOnly);

     foreach (QModelIndex index, indexes) {
         if (index.isValid()) {
             CallTreeItem *item = getItem(index);
             stream << item->call()->getCallId();
         }
     }

     mimeData->setData("application/vnd.text.list", encodedData);
     return mimeData;
}