~ubuntu-branches/ubuntu/wily/scribus/wily-proposed

« back to all changes in this revision

Viewing changes to scribus/tt/simpletreemodel/treemodel.cpp

  • Committer: Package Import Robot
  • Author(s): Oleksandr Moskalenko
  • Date: 2012-02-09 21:50:56 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120209215056-2wrx1ara0jbm7fi5
Tags: 1.4.0.dfsg+r17287-1
* New upstream stable release upload into Debian (Closes: #654703).
* Applied the Ubuntu armel patch.
* Removed non-free color swatches from resources.
* debian/control:
  - Moved icc-profiles from Recommends to Suggests (Closes: #655885).
  - Updated Standards-Version to 3.9.2.
  - Updated extended description per lintian warning.
* debian/rules:
  - Update mailcap (Closes: #630751). A request for mime.types update has
    been sent to the mime-support maintainer.
  - Added build-arch and build-indep targets per lintian warning.
* debian/patches:
  - top_cmakelists.patch - don't copy extra docs and changelogs.
  - scribus_cmakelists.patch - don't copy extra docs and changelogs.
  - scribus_cmakelists.patch - don't install the non-free "doc" dir.
  - profiles_cmakelists.patch - don't install non-free sRGB profile.
* debian/copyright: 
  - Converted to the DEP5 machine readable foramt.
  - Added licenses for free color swatches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
**
3
 
** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
4
 
**
5
 
** This file is part of the example classes of the Qt Toolkit.
6
 
**
7
 
** This file may be used under the terms of the GNU General Public
8
 
** License version 2.0 as published by the Free Software Foundation
9
 
** and appearing in the file LICENSE.GPL included in the packaging of
10
 
** this file.  Please review the following information to ensure GNU
11
 
** General Public Licensing requirements will be met:
12
 
** http://www.trolltech.com/products/qt/opensource.html
13
 
**
14
 
** If you are unsure which license is appropriate for your use, please
15
 
** review the following information:
16
 
** http://www.trolltech.com/products/qt/licensing.html or contact the
17
 
** sales department at sales@trolltech.com.
18
 
**
19
 
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20
 
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21
 
**
22
 
****************************************************************************/
23
 
 
24
 
/*
25
 
    treemodel.cpp
26
 
 
27
 
    Provides a simple tree model to show how to create and use hierarchical
28
 
    models.
29
 
*/
30
 
 
31
 
#include <QtGui>
32
 
 
33
 
#include "treeitem.h"
34
 
#include "treemodel.h"
35
 
 
36
 
TreeModel::TreeModel(const QString &data, QObject *parent)
37
 
    : QAbstractItemModel(parent)
38
 
{
39
 
    QList<QVariant> rootData;
40
 
    rootData << "Title" << "Summary";
41
 
    rootItem = new TreeItem(rootData);
42
 
    setupModelData(data.split(QString("\n")), rootItem);
43
 
}
44
 
 
45
 
TreeModel::~TreeModel()
46
 
{
47
 
    delete rootItem;
48
 
}
49
 
 
50
 
int TreeModel::columnCount(const QModelIndex &parent) const
51
 
{
52
 
    if (parent.isValid())
53
 
        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
54
 
    else
55
 
        return rootItem->columnCount();
56
 
}
57
 
 
58
 
QVariant TreeModel::data(const QModelIndex &index, int role) const
59
 
{
60
 
    if (!index.isValid())
61
 
        return QVariant();
62
 
 
63
 
    if (role != Qt::DisplayRole)
64
 
        return QVariant();
65
 
 
66
 
    TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
67
 
 
68
 
    return item->data(index.column());
69
 
}
70
 
 
71
 
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
72
 
{
73
 
    if (!index.isValid())
74
 
        return Qt::ItemIsEnabled;
75
 
 
76
 
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
77
 
}
78
 
 
79
 
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
80
 
                               int role) const
81
 
{
82
 
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
83
 
        return rootItem->data(section);
84
 
 
85
 
    return QVariant();
86
 
}
87
 
 
88
 
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
89
 
            const
90
 
{
91
 
    TreeItem *parentItem;
92
 
 
93
 
    if (!parent.isValid())
94
 
        parentItem = rootItem;
95
 
    else
96
 
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
97
 
 
98
 
    TreeItem *childItem = parentItem->child(row);
99
 
    if (childItem)
100
 
        return createIndex(row, column, childItem);
101
 
    else
102
 
        return QModelIndex();
103
 
}
104
 
 
105
 
QModelIndex TreeModel::parent(const QModelIndex &index) const
106
 
{
107
 
    if (!index.isValid())
108
 
        return QModelIndex();
109
 
 
110
 
    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
111
 
    TreeItem *parentItem = childItem->parent();
112
 
 
113
 
    if (parentItem == rootItem)
114
 
        return QModelIndex();
115
 
 
116
 
    return createIndex(parentItem->row(), 0, parentItem);
117
 
}
118
 
 
119
 
int TreeModel::rowCount(const QModelIndex &parent) const
120
 
{
121
 
    TreeItem *parentItem;
122
 
 
123
 
    if (!parent.isValid())
124
 
        parentItem = rootItem;
125
 
    else
126
 
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
127
 
 
128
 
    return parentItem->childCount();
129
 
}
130
 
 
131
 
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
132
 
{
133
 
    QList<TreeItem*> parents;
134
 
    QList<int> indentations;
135
 
    parents << parent;
136
 
    indentations << 0;
137
 
 
138
 
    int number = 0;
139
 
 
140
 
    while (number < lines.count()) {
141
 
        int position = 0;
142
 
        while (position < lines[number].length()) {
143
 
            if (lines[number].mid(position, 1) != " ")
144
 
                break;
145
 
            position++;
146
 
        }
147
 
 
148
 
        QString lineData = lines[number].mid(position).trimmed();
149
 
 
150
 
        if (!lineData.isEmpty()) {
151
 
            // Read the column data from the rest of the line.
152
 
            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
153
 
            QList<QVariant> columnData;
154
 
            for (int column = 0; column < columnStrings.count(); ++column)
155
 
                columnData << columnStrings[column];
156
 
 
157
 
            if (position > indentations.last()) {
158
 
                // The last child of the current parent is now the new parent
159
 
                // unless the current parent has no children.
160
 
 
161
 
                if (parents.last()->childCount() > 0) {
162
 
                    parents << parents.last()->child(parents.last()->childCount()-1);
163
 
                    indentations << position;
164
 
                }
165
 
            } else {
166
 
                while (position < indentations.last() && parents.count() > 0) {
167
 
                    parents.pop_back();
168
 
                    indentations.pop_back();
169
 
                }
170
 
            }
171
 
 
172
 
            // Append a new item to the current parent's list of children.
173
 
            parents.last()->appendChild(new TreeItem(columnData, parents.last()));
174
 
        }
175
 
 
176
 
        number++;
177
 
    }
178
 
}
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2011 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 examples of the Qt Toolkit.
 
8
**
 
9
** $QT_BEGIN_LICENSE:BSD$
 
10
** You may use this file under the terms of the BSD license as follows:
 
11
**
 
12
** "Redistribution and use in source and binary forms, with or without
 
13
** modification, are permitted provided that the following conditions are
 
14
** met:
 
15
**   * Redistributions of source code must retain the above copyright
 
16
**     notice, this list of conditions and the following disclaimer.
 
17
**   * Redistributions in binary form must reproduce the above copyright
 
18
**     notice, this list of conditions and the following disclaimer in
 
19
**     the documentation and/or other materials provided with the
 
20
**     distribution.
 
21
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 
22
**     the names of its contributors may be used to endorse or promote
 
23
**     products derived from this software without specific prior written
 
24
**     permission.
 
25
**
 
26
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
27
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
28
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
29
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
30
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
31
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
32
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
33
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
34
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
35
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
36
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
/*
 
42
    treemodel.cpp
 
43
 
 
44
    Provides a simple tree model to show how to create and use hierarchical
 
45
    models.
 
46
*/
 
47
 
 
48
#include <QtGui>
 
49
 
 
50
#include "treeitem.h"
 
51
#include "treemodel.h"
 
52
 
 
53
//! [0]
 
54
TreeModel::TreeModel(const QString &data, QObject *parent)
 
55
    : QAbstractItemModel(parent)
 
56
{
 
57
    QList<QVariant> rootData;
 
58
    rootData << "Title" << "Summary";
 
59
    rootItem = new TreeItem(rootData);
 
60
    setupModelData(data.split(QString("\n")), rootItem);
 
61
}
 
62
//! [0]
 
63
 
 
64
//! [1]
 
65
TreeModel::~TreeModel()
 
66
{
 
67
    delete rootItem;
 
68
}
 
69
//! [1]
 
70
 
 
71
//! [2]
 
72
int TreeModel::columnCount(const QModelIndex &parent) const
 
73
{
 
74
    if (parent.isValid())
 
75
        return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
 
76
    else
 
77
        return rootItem->columnCount();
 
78
}
 
79
//! [2]
 
80
 
 
81
//! [3]
 
82
QVariant TreeModel::data(const QModelIndex &index, int role) const
 
83
{
 
84
    if (!index.isValid())
 
85
        return QVariant();
 
86
 
 
87
    if (role != Qt::DisplayRole)
 
88
        return QVariant();
 
89
 
 
90
    TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
 
91
 
 
92
    return item->data(index.column());
 
93
}
 
94
//! [3]
 
95
 
 
96
//! [4]
 
97
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
 
98
{
 
99
    if (!index.isValid())
 
100
        return 0;
 
101
 
 
102
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
103
}
 
104
//! [4]
 
105
 
 
106
//! [5]
 
107
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
 
108
                               int role) const
 
109
{
 
110
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
 
111
        return rootItem->data(section);
 
112
 
 
113
    return QVariant();
 
114
}
 
115
//! [5]
 
116
 
 
117
//! [6]
 
118
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
 
119
            const
 
120
{
 
121
    if (!hasIndex(row, column, parent))
 
122
        return QModelIndex();
 
123
 
 
124
    TreeItem *parentItem;
 
125
 
 
126
    if (!parent.isValid())
 
127
        parentItem = rootItem;
 
128
    else
 
129
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
 
130
 
 
131
    TreeItem *childItem = parentItem->child(row);
 
132
    if (childItem)
 
133
        return createIndex(row, column, childItem);
 
134
    else
 
135
        return QModelIndex();
 
136
}
 
137
//! [6]
 
138
 
 
139
//! [7]
 
140
QModelIndex TreeModel::parent(const QModelIndex &index) const
 
141
{
 
142
    if (!index.isValid())
 
143
        return QModelIndex();
 
144
 
 
145
    TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
 
146
    TreeItem *parentItem = childItem->parent();
 
147
 
 
148
    if (parentItem == rootItem)
 
149
        return QModelIndex();
 
150
 
 
151
    return createIndex(parentItem->row(), 0, parentItem);
 
152
}
 
153
//! [7]
 
154
 
 
155
//! [8]
 
156
int TreeModel::rowCount(const QModelIndex &parent) const
 
157
{
 
158
    TreeItem *parentItem;
 
159
    if (parent.column() > 0)
 
160
        return 0;
 
161
 
 
162
    if (!parent.isValid())
 
163
        parentItem = rootItem;
 
164
    else
 
165
        parentItem = static_cast<TreeItem*>(parent.internalPointer());
 
166
 
 
167
    return parentItem->childCount();
 
168
}
 
169
//! [8]
 
170
 
 
171
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
 
172
{
 
173
    QList<TreeItem*> parents;
 
174
    QList<int> indentations;
 
175
    parents << parent;
 
176
    indentations << 0;
 
177
 
 
178
    int number = 0;
 
179
 
 
180
    while (number < lines.count()) {
 
181
        int position = 0;
 
182
        while (position < lines[number].length()) {
 
183
            if (lines[number].mid(position, 1) != " ")
 
184
                break;
 
185
            position++;
 
186
        }
 
187
 
 
188
        QString lineData = lines[number].mid(position).trimmed();
 
189
 
 
190
        if (!lineData.isEmpty()) {
 
191
            // Read the column data from the rest of the line.
 
192
            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
 
193
            QList<QVariant> columnData;
 
194
            for (int column = 0; column < columnStrings.count(); ++column)
 
195
                columnData << columnStrings[column];
 
196
 
 
197
            if (position > indentations.last()) {
 
198
                // The last child of the current parent is now the new parent
 
199
                // unless the current parent has no children.
 
200
 
 
201
                if (parents.last()->childCount() > 0) {
 
202
                    parents << parents.last()->child(parents.last()->childCount()-1);
 
203
                    indentations << position;
 
204
                }
 
205
            } else {
 
206
                while (position < indentations.last() && parents.count() > 0) {
 
207
                    parents.pop_back();
 
208
                    indentations.pop_back();
 
209
                }
 
210
            }
 
211
 
 
212
            // Append a new item to the current parent's list of children.
 
213
            parents.last()->appendChild(new TreeItem(columnData, parents.last()));
 
214
        }
 
215
 
 
216
        number++;
 
217
    }
 
218
}