1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include "TestContainer.h"
46 #include "TreeModel.h"
48 using namespace QPatternistSDK;
50 TreeModel::TreeModel(const QStringList columnData,
51 QObject *p) : QAbstractItemModel(p),
53 m_columnData(columnData)
57 TreeModel::~TreeModel()
61 QVariant TreeModel::data(const QModelIndex &idx, int role) const
66 TreeItem *item = static_cast<TreeItem *>(idx.internalPointer());
69 return item->data(static_cast<Qt::ItemDataRole>(role), idx.column());
72 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
74 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
75 return m_columnData.value(section);
80 void TreeModel::childChanged(TreeItem *item)
83 const QModelIndex index = createIndex(item->row(), 0, item);
84 dataChanged(index, index);
90 QModelIndex TreeModel::index(int row, int column, const QModelIndex &p) const
92 const int c = columnCount(p);
94 if(row < 0 || column < 0 || column >= c)
100 parentItem = static_cast<TreeItem *>(p.internalPointer());
105 return QModelIndex();
107 TreeItem *childItem = parentItem->child(row);
110 return createIndex(row, column, childItem);
112 return QModelIndex();
115 QModelIndex TreeModel::parent(const QModelIndex &idx) const
118 return QModelIndex();
120 TreeItem *childItem = static_cast<TreeItem *>(idx.internalPointer());
122 TreeItem *parentItem = childItem->parent();
124 if(!parentItem || parentItem == m_root)
125 return QModelIndex();
127 Q_ASSERT(parentItem);
128 return createIndex(parentItem->row(), 0, parentItem);
131 Qt::ItemFlags TreeModel::flags(const QModelIndex &idx) const
133 /* Not sure about this code. */
135 return Qt::ItemFlags();
137 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
140 int TreeModel::rowCount(const QModelIndex &p) const
145 const TreeItem *parentItem;
148 parentItem = static_cast<TreeItem *>(p.internalPointer());
157 return parentItem->childCount();
160 int TreeModel::columnCount(const QModelIndex &p) const
163 return static_cast<TreeItem *>(p.internalPointer())->columnCount();
165 return m_columnData.count();
168 TreeItem *TreeModel::root() const
173 void TreeModel::setRoot(TreeItem *r)
175 TreeItem *const oldRoot = m_root;
177 /* Notify views that we are radically changing. */
182 connect(r, SIGNAL(changed(TreeItem *)), SLOT(childChanged(TreeItem *)));
189 // vim: et:ts=4:sw=4:sts=4