tests/auto/xmlpatternssdk/TreeModel.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtDebug>
       
    43 
       
    44 #include "TestContainer.h"
       
    45 
       
    46 #include "TreeModel.h"
       
    47 
       
    48 using namespace QPatternistSDK;
       
    49 
       
    50 TreeModel::TreeModel(const QStringList columnData,
       
    51                      QObject *p) : QAbstractItemModel(p),
       
    52                                    m_root(0),
       
    53                                    m_columnData(columnData)
       
    54 {
       
    55 }
       
    56 
       
    57 TreeModel::~TreeModel()
       
    58 {
       
    59 }
       
    60 
       
    61 QVariant TreeModel::data(const QModelIndex &idx, int role) const
       
    62 {
       
    63     if(!idx.isValid())
       
    64         return QVariant();
       
    65 
       
    66     TreeItem *item = static_cast<TreeItem *>(idx.internalPointer());
       
    67     Q_ASSERT(item);
       
    68 
       
    69     return item->data(static_cast<Qt::ItemDataRole>(role), idx.column());
       
    70 }
       
    71 
       
    72 QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
       
    73 {
       
    74     if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
       
    75         return m_columnData.value(section);
       
    76 
       
    77     return QVariant();
       
    78 }
       
    79 
       
    80 void TreeModel::childChanged(TreeItem *item)
       
    81 {
       
    82     if (item) {
       
    83         const QModelIndex index = createIndex(item->row(), 0, item);
       
    84         dataChanged(index, index);
       
    85     } else {
       
    86         layoutChanged();
       
    87     }
       
    88 }
       
    89 
       
    90 QModelIndex TreeModel::index(int row, int column, const QModelIndex &p) const
       
    91 {
       
    92     const int c = columnCount(p);
       
    93 
       
    94     if(row < 0 || column < 0 || column >= c)
       
    95         return QModelIndex();
       
    96 
       
    97     TreeItem *parentItem;
       
    98 
       
    99     if(p.isValid())
       
   100         parentItem = static_cast<TreeItem *>(p.internalPointer());
       
   101     else
       
   102         parentItem = m_root;
       
   103 
       
   104     if(!parentItem)
       
   105         return QModelIndex();
       
   106 
       
   107     TreeItem *childItem = parentItem->child(row);
       
   108 
       
   109     if(childItem)
       
   110         return createIndex(row, column, childItem);
       
   111     else
       
   112         return QModelIndex();
       
   113 }
       
   114 
       
   115 QModelIndex TreeModel::parent(const QModelIndex &idx) const
       
   116 {
       
   117     if(!idx.isValid())
       
   118         return QModelIndex();
       
   119 
       
   120     TreeItem *childItem = static_cast<TreeItem *>(idx.internalPointer());
       
   121     Q_ASSERT(childItem);
       
   122     TreeItem *parentItem = childItem->parent();
       
   123 
       
   124     if(!parentItem || parentItem == m_root)
       
   125         return QModelIndex();
       
   126 
       
   127     Q_ASSERT(parentItem);
       
   128     return createIndex(parentItem->row(), 0, parentItem);
       
   129 }
       
   130 
       
   131 Qt::ItemFlags TreeModel::flags(const QModelIndex &idx) const
       
   132 {
       
   133     /* Not sure about this code. */
       
   134     if(!idx.isValid())
       
   135         return Qt::ItemFlags();
       
   136 
       
   137     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
       
   138 }
       
   139 
       
   140 int TreeModel::rowCount(const QModelIndex &p) const
       
   141 {
       
   142     if(p.column() > 0)
       
   143         return 0;
       
   144 
       
   145     const TreeItem *parentItem;
       
   146 
       
   147     if(p.isValid())
       
   148         parentItem = static_cast<TreeItem *>(p.internalPointer());
       
   149     else
       
   150     {
       
   151         if(m_root)
       
   152             parentItem = m_root;
       
   153         else
       
   154             return 0;
       
   155     }
       
   156 
       
   157     return parentItem->childCount();
       
   158 }
       
   159 
       
   160 int TreeModel::columnCount(const QModelIndex &p) const
       
   161 {
       
   162     if(p.isValid())
       
   163         return static_cast<TreeItem *>(p.internalPointer())->columnCount();
       
   164     else
       
   165         return m_columnData.count();
       
   166 }
       
   167 
       
   168 TreeItem *TreeModel::root() const
       
   169 {
       
   170     return m_root;
       
   171 }
       
   172 
       
   173 void TreeModel::setRoot(TreeItem *r)
       
   174 {
       
   175     TreeItem *const oldRoot = m_root;
       
   176     m_root = r;
       
   177 
       
   178     if(m_root)
       
   179         connect(r, SIGNAL(changed(TreeItem *)), SLOT(childChanged(TreeItem *)));
       
   180     reset(); /* Notify views that we have radically changed. */
       
   181     delete oldRoot;
       
   182 }
       
   183 
       
   184 // vim: et:ts=4:sw=4:sts=4