1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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