defuze.me  Client
editabletreemodel.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2012
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "editabletreemodel.hpp"
00012 #include "editabletreeitem.hpp"
00013 #include <QtGui>
00014 
00015 EditableTreeModel::EditableTreeModel(QObject *parent)
00016     : QAbstractItemModel(parent)
00017 {
00018 //  QVector<QVariant> rootData;
00019 //  rootData << "One";
00020 
00021 //  rootItem = new EditableTreeItem(rootData);
00022     //setupModelData(data.split(QString("\n")), rootItem);
00023 }
00024 
00025 EditableTreeModel::~EditableTreeModel()
00026 {
00027     delete rootItem;
00028 }
00029 
00030 int EditableTreeModel::columnCount(const QModelIndex & /* parent */) const
00031 {
00032     return rootItem->columnCount();
00033 }
00034 
00035 QVariant EditableTreeModel::data(const QModelIndex &index, int role) const
00036 {
00037     if (!index.isValid())
00038         return QVariant();
00039 
00040     if (role != Qt::DisplayRole && role != Qt::EditRole)
00041         return QVariant();
00042 
00043     EditableTreeItem *item = getItem(index);
00044 
00045     return item->data(index.column());
00046 }
00047 
00048 Qt::ItemFlags EditableTreeModel::flags(const QModelIndex &index) const
00049 {
00050     if (!index.isValid())
00051         return 0;
00052 
00053     return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00054 }
00055 
00056 EditableTreeItem *EditableTreeModel::getItem(const QModelIndex &index) const
00057 {
00058     if (index.isValid()) {
00059         EditableTreeItem *item = static_cast<EditableTreeItem*>(index.internalPointer());
00060         if (item) return item;
00061     }
00062     return rootItem;
00063 }
00064 
00065 QVariant EditableTreeModel::headerData(int section, Qt::Orientation orientation,
00066                                int role) const
00067 {
00068     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
00069         return rootItem->data(section);
00070 
00071     return QVariant();
00072 }
00073 
00074 QModelIndex EditableTreeModel::index(int row, int column, const QModelIndex &parent) const
00075 {
00076     if (parent.isValid() && parent.column() != 0)
00077         return QModelIndex();
00078 
00079     EditableTreeItem *parentItem = getItem(parent);
00080 
00081     EditableTreeItem *childItem = parentItem->child(row);
00082     if (childItem)
00083     {
00084         QModelIndex idx = createIndex(row, column, childItem);
00085         childItem->index = idx;
00086         return idx;
00087     }
00088     else
00089         return QModelIndex();
00090 }
00091 
00092 bool EditableTreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
00093 {
00094     bool success;
00095 
00096     beginInsertColumns(parent, position, position + columns - 1);
00097     success = rootItem->insertColumns(position, columns);
00098     endInsertColumns();
00099 
00100     return success;
00101 }
00102 
00103 bool EditableTreeModel::insertRows(int position, int rows, const QModelIndex &parent)
00104 {
00105     EditableTreeItem *parentItem = getItem(parent);
00106     bool success;
00107 
00108     beginInsertRows(parent, position, position + rows - 1);
00109     success = parentItem->insertChildren(position, rows, rootItem->columnCount());
00110     endInsertRows();
00111 
00112     return success;
00113 }
00114 
00115 QModelIndex EditableTreeModel::parent(const QModelIndex &index) const
00116 {
00117     if (!index.isValid())
00118         return QModelIndex();
00119 
00120     EditableTreeItem *childItem = getItem(index);
00121     EditableTreeItem *parentItem = childItem->parent();
00122 
00123     if (parentItem == rootItem)
00124         return QModelIndex();
00125 
00126     QModelIndex idx = createIndex(parentItem->childNumber(), 0, parentItem);
00127     parentItem->index = idx;
00128     return idx;
00129 }
00130 
00131 bool EditableTreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
00132 {
00133     bool success;
00134 
00135     beginRemoveColumns(parent, position, position + columns - 1);
00136     success = rootItem->removeColumns(position, columns);
00137     endRemoveColumns();
00138 
00139     if (rootItem->columnCount() == 0)
00140         removeRows(0, rowCount());
00141 
00142     return success;
00143 }
00144 
00145 bool EditableTreeModel::removeRows(int position, int rows, const QModelIndex &parent)
00146 {
00147     EditableTreeItem *parentItem = getItem(parent);
00148     bool success = true;
00149 
00150     beginRemoveRows(parent, position, position + rows - 1);
00151     success = parentItem->removeChildren(position, rows);
00152     endRemoveRows();
00153 
00154     return success;
00155 }
00156 
00157 int EditableTreeModel::rowCount(const QModelIndex &parent) const
00158 {
00159     EditableTreeItem *parentItem = getItem(parent);
00160 
00161     return parentItem->childCount();
00162 }
00163 
00164 bool EditableTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
00165 {
00166     if (role != Qt::EditRole)
00167         return false;
00168 
00169     EditableTreeItem *item = getItem(index);
00170     bool result = item->setData(index.column(), value);
00171 
00172     if (result)
00173         emit dataChanged(index, index);
00174 
00175     return result;
00176 }
00177 
00178 bool EditableTreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
00179 {
00180     if (role != Qt::EditRole || orientation != Qt::Horizontal)
00181         return false;
00182 
00183     bool result = rootItem->setData(section, value);
00184 
00185     if (result)
00186         emit headerDataChanged(orientation, section, section);
00187 
00188     return result;
00189 }
00190 
00191 void EditableTreeModel::setupModelData(const QStringList &lines, EditableTreeItem *parent)
00192 {
00193     QList<EditableTreeItem*> parents;
00194     QList<int> indentations;
00195     parents << parent;
00196     indentations << 0;
00197 
00198     int number = 0;
00199 
00200     while (number < lines.count()) {
00201         int position = 0;
00202         while (position < lines[number].length()) {
00203             if (lines[number].mid(position, 1) != " ")
00204                 break;
00205             position++;
00206         }
00207 
00208         QString lineData = lines[number].mid(position).trimmed();
00209 
00210         if (!lineData.isEmpty()) {
00211             // Read the column data from the rest of the line.
00212             QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
00213             QVector<QVariant> columnData;
00214             for (int column = 0; column < columnStrings.count(); ++column)
00215                 columnData << columnStrings[column];
00216 
00217             if (position > indentations.last()) {
00218                 // The last child of the current parent is now the new parent
00219                 // unless the current parent has no children.
00220 
00221                 if (parents.last()->childCount() > 0) {
00222                     parents << parents.last()->child(parents.last()->childCount()-1);
00223                     indentations << position;
00224                 }
00225             } else {
00226                 while (position < indentations.last() && parents.count() > 0) {
00227                     parents.pop_back();
00228                     indentations.pop_back();
00229                 }
00230             }
00231 
00232             // Append a new item to the current parent's list of children.
00233             EditableTreeItem *parent = parents.last();
00234             parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
00235             for (int column = 0; column < columnData.size(); ++column)
00236                 parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
00237         }
00238 
00239         number++;
00240     }
00241 }