stateseditormodel.cpp 5.68 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30
31
32

#include "stateseditormodel.h"
#include "stateseditorview.h"

33
#include <QDebug>
34
#include <QMessageBox>
35

36
37
38
39
#include <nodelistproperty.h>
#include <modelnode.h>
#include <variantproperty.h>

40
41
42
43
44
45
46
enum {
    debug = false
};


namespace QmlDesigner {

47
48
49
50
StatesEditorModel::StatesEditorModel(StatesEditorView *view)
    : QAbstractListModel(view),
      m_statesEditorView(view),
      m_updateCounter(0)
51
52
53
{
    QHash<int, QByteArray> roleNames;
    roleNames.insert(StateNameRole, "stateName");
54
    roleNames.insert(StateImageSourceRole, "stateImageSource");
55
    roleNames.insert(NodeId, "nodeId");
56
57
58
59
60
61
    setRoleNames(roleNames);
}


int StatesEditorModel::count() const
{
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    return rowCount();
}

QModelIndex StatesEditorModel::index(int row, int column, const QModelIndex &parent) const
{
    if (m_statesEditorView.isNull())
        return QModelIndex();


    int internalId = 0;
    if (row > 0)
        internalId = m_statesEditorView->rootModelNode().nodeListProperty("states").at(row - 1).internalId();

    return hasIndex(row, column, parent) ? createIndex(row, column,  internalId) : QModelIndex();
76
77
78
79
}

int StatesEditorModel::rowCount(const QModelIndex &parent) const
{
80
    if (parent.isValid() || m_statesEditorView.isNull() || !m_statesEditorView->model())
81
        return 0;
82
83
84
85
86
87
88
89
90

    if (!m_statesEditorView->rootModelNode().hasNodeListProperty("states"))
        return 1;

    return m_statesEditorView->rootModelNode().nodeListProperty("states").count() + 1;
}

void StatesEditorModel::reset()
{
91
92
    QAbstractListModel::beginResetModel();
    QAbstractListModel::endResetModel();
93
94
95
96
}

QVariant StatesEditorModel::data(const QModelIndex &index, int role) const
{
97
    if (index.parent().isValid() || index.column() != 0 || m_statesEditorView.isNull() || !m_statesEditorView->hasModelNodeForInternalId(index.internalId()))
98
99
        return QVariant();

100
101
102
103
104
    ModelNode stateNode;

    if (index.internalId() > 0)
        stateNode = m_statesEditorView->modelNodeForInternalId(index.internalId());

105
106
    switch (role) {
    case StateNameRole: {
107
108
109
            if (index.row() == 0) {
                return QString(tr("base state", "Implicit default state"));
            } else {
110
                if (stateNode.hasVariantProperty("name"))
111
                    return stateNode.variantProperty("name").value();
112
                else
113
114
115
                    return QVariant();
            }

116
        }
117
118
119
    case StateImageSourceRole: {
        static int randomNumber = 0;
        randomNumber++;
120
        if (index.row() == 0)
121
            return QString("image://qmldesigner_stateseditor/baseState-%1").arg(randomNumber);
122
        else
123
124
            return QString("image://qmldesigner_stateseditor/%1-%2").arg(index.internalId()).arg(randomNumber);
    }
125
    case NodeId : return index.internalId();
126
127
    }

128
129

    return QVariant();
130
131
}

132
void StatesEditorModel::insertState(int stateIndex)
133
{
134
    if (stateIndex >= 0) {
135

136
137
        const int updateIndex = stateIndex + 1;
        beginInsertRows(QModelIndex(), updateIndex, updateIndex);
138

139
        endInsertRows();
140

141
        emit dataChanged(index(updateIndex, 0), index(updateIndex, 0));
142
143
        emit countChanged();
    }
144
145
}

146
void StatesEditorModel::updateState(int beginIndex, int endIndex)
147
{
148
149
    if (beginIndex >= 0 && endIndex >= 0)
        emit dataChanged(index(beginIndex, 0), index(endIndex, 0));
150
151
}

152
void StatesEditorModel::removeState(int stateIndex)
153
{
154
    if (stateIndex >= 0) {
155
156
        const int updateIndex = stateIndex + 1;
        beginRemoveRows(QModelIndex(), updateIndex, updateIndex);
157
158
159
160


        endRemoveRows();

161
        emit dataChanged(createIndex(updateIndex, 0), createIndex(updateIndex, 0));
162
        emit countChanged();
163
164
165
    }
}

166
void StatesEditorModel::renameState(int nodeId, const QString &newName)
167
168
169
170
{
    if (newName == m_statesEditorView->currentStateName())
        return;

171
172
173
174
175
176
177
178
179
    if (newName.isEmpty() ||! m_statesEditorView->validStateName(newName)) {
        QMessageBox::warning(0, tr("Invalid state name"),
                             newName.isEmpty() ?
                                 tr("The empty string as a name is reserved for the base state.") :
                                 tr("Name already used in another state"));
    } else {
        m_statesEditorView->renameState(nodeId, newName);
    }

180
181
182
183
184
185
186
187
}

void StatesEditorModel::emitChangedToState(int n)
{
    emit changedToState(n);
}

} // namespace QmlDesigner