OSDN Git Service

19e258b39684ac12adf0b227929d999ead5518aa
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / deployconfigurationmodel.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 "deployconfigurationmodel.h"
35 #include "target.h"
36 #include "deployconfiguration.h"
37
38 using namespace ProjectExplorer;
39
40 ///
41 /// DeployConfigurationsModel
42 ///
43
44 class DeployConfigurationComparer
45 {
46 public:
47     bool operator()(DeployConfiguration *a, DeployConfiguration *b)
48     {
49         return a->displayName() < b->displayName();
50     }
51 };
52
53 DeployConfigurationModel::DeployConfigurationModel(Target *target, QObject *parent)
54     : QAbstractListModel(parent),
55       m_target(target)
56 {
57     m_deployConfigurations = m_target->deployConfigurations();
58     qSort(m_deployConfigurations.begin(), m_deployConfigurations.end(), DeployConfigurationComparer());
59
60     connect(target, SIGNAL(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
61             this, SLOT(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
62     connect(target, SIGNAL(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
63             this, SLOT(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
64
65     foreach (DeployConfiguration *dc, m_deployConfigurations)
66         connect(dc, SIGNAL(displayNameChanged()),
67                 this, SLOT(displayNameChanged()));
68 }
69
70 int DeployConfigurationModel::rowCount(const QModelIndex &parent) const
71 {
72     return parent.isValid() ? 0 : m_deployConfigurations.size();
73 }
74
75 int DeployConfigurationModel::columnCount(const QModelIndex &parent) const
76 {
77     return parent.isValid() ? 0 : 1;
78 }
79
80 void DeployConfigurationModel::displayNameChanged()
81 {
82     DeployConfiguration *dc = qobject_cast<DeployConfiguration *>(sender());
83     if (!dc)
84         return;
85
86     DeployConfigurationComparer compare;
87     // Find the old position
88     int oldPos = m_deployConfigurations.indexOf(dc);
89
90     if (oldPos >= 1 && compare(m_deployConfigurations.at(oldPos), m_deployConfigurations.at(oldPos - 1))) {
91         // We need to move up
92         int newPos = oldPos - 1;
93         while (newPos >= 0 && compare(m_deployConfigurations.at(oldPos), m_deployConfigurations.at(newPos))) {
94             --newPos;
95         }
96         ++newPos;
97
98         beginMoveRows(QModelIndex(), oldPos, oldPos, QModelIndex(), newPos);
99         m_deployConfigurations.insert(newPos, dc);
100         m_deployConfigurations.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_deployConfigurations.size() - 1
105                && compare(m_deployConfigurations.at(oldPos + 1), m_deployConfigurations.at(oldPos))) {
106         // We need to move down
107         int newPos = oldPos + 1;
108         while (newPos < m_deployConfigurations.size()
109             && compare(m_deployConfigurations.at(newPos), m_deployConfigurations.at(oldPos))) {
110             ++newPos;
111         }
112         beginMoveRows(QModelIndex(), oldPos, oldPos, QModelIndex(), newPos);
113         m_deployConfigurations.insert(newPos, dc);
114         m_deployConfigurations.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 DeployConfigurationModel::data(const QModelIndex &index, int role) const
125 {
126     if (role == Qt::DisplayRole) {
127         const int row = index.row();
128         if (row < m_deployConfigurations.size()) {
129             return m_deployConfigurations.at(row)->displayName();
130         }
131     }
132
133     return QVariant();
134 }
135
136 DeployConfiguration *DeployConfigurationModel::deployConfigurationAt(int i)
137 {
138     if (i > m_deployConfigurations.size() || i < 0)
139         return 0;
140     return m_deployConfigurations.at(i);
141 }
142
143 DeployConfiguration *DeployConfigurationModel::deployConfigurationFor(const QModelIndex &idx)
144 {
145     if (idx.row() > m_deployConfigurations.size() || idx.row() < 0)
146         return 0;
147     return m_deployConfigurations.at(idx.row());
148 }
149
150 QModelIndex DeployConfigurationModel::indexFor(DeployConfiguration *rc)
151 {
152     int idx = m_deployConfigurations.indexOf(rc);
153     if (idx == -1)
154         return QModelIndex();
155     return index(idx, 0);
156 }
157
158 void DeployConfigurationModel::addedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
159 {
160     // Find the right place to insert
161     DeployConfigurationComparer compare;
162     int i = 0;
163     for (; i < m_deployConfigurations.size(); ++i) {
164         if (compare(dc, m_deployConfigurations.at(i))) {
165             break;
166         }
167     }
168
169     beginInsertRows(QModelIndex(), i, i);
170     m_deployConfigurations.insert(i, dc);
171     endInsertRows();
172
173     connect(dc, SIGNAL(displayNameChanged()),
174             this, SLOT(displayNameChanged()));
175 }
176
177 void DeployConfigurationModel::removedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
178 {
179     int i = m_deployConfigurations.indexOf(dc);
180     if (i < 0)
181         return;
182     beginRemoveRows(QModelIndex(), i, i);
183     m_deployConfigurations.removeAt(i);
184     endRemoveRows();
185 }