OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / buildconfigurationmodel.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
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.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "buildconfigurationmodel.h"
35 #include "target.h"
36 #include "buildconfiguration.h"
37
38 using namespace ProjectExplorer;
39
40 ///
41 /// RunConfigurationsModel
42 ///
43
44 class BuildConfigurationComparer
45 {
46 public:
47     bool operator()(BuildConfiguration *a, BuildConfiguration *b)
48     {
49         return a->displayName() < b->displayName();
50     }
51 };
52
53 BuildConfigurationModel::BuildConfigurationModel(Target *target, QObject *parent)
54     : QAbstractListModel(parent),
55       m_target(target)
56 {
57     m_buildConfigurations = m_target->buildConfigurations();
58     qSort(m_buildConfigurations.begin(), m_buildConfigurations.end(), BuildConfigurationComparer());
59
60     connect(target, SIGNAL(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
61             this, SLOT(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
62     connect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
63             this, SLOT(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
64
65     foreach (BuildConfiguration *bc, m_buildConfigurations)
66         connect(bc, SIGNAL(displayNameChanged()),
67                 this, SLOT(displayNameChanged()));
68 }
69
70 int BuildConfigurationModel::rowCount(const QModelIndex &parent) const
71 {
72     return parent.isValid() ? 0 : m_buildConfigurations.size();
73 }
74
75 int BuildConfigurationModel::columnCount(const QModelIndex &parent) const
76 {
77     return parent.isValid() ? 0 : 1;
78 }
79
80 void BuildConfigurationModel::displayNameChanged()
81 {
82     BuildConfiguration *rc = qobject_cast<BuildConfiguration *>(sender());
83     if (!rc)
84         return;
85
86     BuildConfigurationComparer compare;
87     // Find the old position
88     int oldPos = m_buildConfigurations.indexOf(rc);
89
90     if (oldPos >= 1 && compare(m_buildConfigurations.at(oldPos), m_buildConfigurations.at(oldPos - 1))) {
91         // We need to move up
92         int newPos = oldPos - 1;
93         while (newPos >= 0 && compare(m_buildConfigurations.at(oldPos), m_buildConfigurations.at(newPos))) {
94             --newPos;
95         }
96         ++newPos;
97
98         beginMoveRows(QModelIndex(), oldPos, oldPos, QModelIndex(), newPos);
99         m_buildConfigurations.insert(newPos, rc);
100         m_buildConfigurations.removeAt(oldPos + 1);
101         endMoveRows();
102         // Not only did we move, we also changed...
103         emit dataChanged(index(newPos, 0), index(newPos,0));
104     } else if  (oldPos < m_buildConfigurations.size() - 1
105                 && compare(m_buildConfigurations.at(oldPos + 1), m_buildConfigurations.at(oldPos))) {
106         // We need to move down
107         int newPos = oldPos + 1;
108         while (newPos < m_buildConfigurations.size()
109             && compare(m_buildConfigurations.at(newPos), m_buildConfigurations.at(oldPos))) {
110             ++newPos;
111         }
112         beginMoveRows(QModelIndex(), oldPos, oldPos, QModelIndex(), newPos);
113         m_buildConfigurations.insert(newPos, rc);
114         m_buildConfigurations.removeAt(oldPos);
115         endMoveRows();
116
117         // We need to subtract one since removing at the old place moves the newIndex down
118         emit dataChanged(index(newPos - 1, 0), index(newPos - 1, 0));
119     } else {
120         emit dataChanged(index(oldPos, 0), index(oldPos, 0));
121     }
122 }
123
124 QVariant BuildConfigurationModel::data(const QModelIndex &index, int role) const
125 {
126     if (role == Qt::DisplayRole) {
127         const int row = index.row();
128         if (row < m_buildConfigurations.size()) {
129             return m_buildConfigurations.at(row)->displayName();
130         }
131     }
132
133     return QVariant();
134 }
135
136 BuildConfiguration *BuildConfigurationModel::buildConfigurationAt(int i)
137 {
138     if (i > m_buildConfigurations.size() || i < 0)
139         return 0;
140     return m_buildConfigurations.at(i);
141 }
142
143 BuildConfiguration *BuildConfigurationModel::buildConfigurationFor(const QModelIndex &idx)
144 {
145     if (idx.row() > m_buildConfigurations.size() || idx.row() < 0)
146         return 0;
147     return m_buildConfigurations.at(idx.row());
148 }
149
150 QModelIndex BuildConfigurationModel::indexFor(BuildConfiguration *rc)
151 {
152     int idx = m_buildConfigurations.indexOf(rc);
153     if (idx == -1)
154         return QModelIndex();
155     return index(idx, 0);
156 }
157
158 void BuildConfigurationModel::addedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
159 {
160     // Find the right place to insert
161     BuildConfigurationComparer compare;
162     int i = 0;
163     for (; i < m_buildConfigurations.size(); ++i) {
164         if (compare(bc, m_buildConfigurations.at(i))) {
165             break;
166         }
167     }
168
169     beginInsertRows(QModelIndex(), i, i);
170     m_buildConfigurations.insert(i, bc);
171     endInsertRows();
172
173
174     connect(bc, SIGNAL(displayNameChanged()),
175             this, SLOT(displayNameChanged()));
176 }
177
178 void BuildConfigurationModel::removedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
179 {
180     int i = m_buildConfigurations.indexOf(bc);
181     beginRemoveRows(QModelIndex(), i, i);
182     m_buildConfigurations.removeAt(i);
183     endRemoveRows();
184 }