OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / dependenciespanel.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
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 #include "dependenciespanel.h"
34 #include "project.h"
35 #include "session.h"
36
37 #include <coreplugin/fileiconprovider.h>
38 #include <coreplugin/ifile.h>
39 #include <utils/detailswidget.h>
40
41 #include <QtCore/QVector>
42 #include <QtCore/QDebug>
43 #include <QtCore/QSize>
44 #include <QtCore/QCoreApplication>
45
46 #include <QtGui/QLabel>
47 #include <QtGui/QApplication>
48 #include <QtGui/QHBoxLayout>
49 #include <QtGui/QTreeView>
50 #include <QtGui/QSpacerItem>
51 #include <QtGui/QHeaderView>
52 #include <QtGui/QMessageBox>
53 #include <QtGui/QPushButton>
54 #include <QtGui/QToolButton>
55 #include <QtGui/QWidget>
56 #include <QtGui/QLabel>
57
58 namespace ProjectExplorer {
59 namespace Internal {
60
61 DependenciesModel::DependenciesModel(SessionManager *session,
62                                      Project *project,
63                                      QObject *parent)
64     : QAbstractListModel(parent)
65     , m_session(session)
66     , m_project(project)
67     , m_projects(session->projects())
68 {
69     // We can't select ourselves as a dependency
70     m_projects.removeAll(m_project);
71     connect(session, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
72             this, SLOT(resetModel()));
73     connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
74             this, SLOT(resetModel()));
75     connect(session, SIGNAL(sessionLoaded()),
76             this, SLOT(resetModel()));
77 //    qDebug()<<"Dependencies Model"<<this<<"for project"<<project<<"("<<project->file()->fileName()<<")";
78 }
79
80 DependenciesModel::~DependenciesModel()
81 {
82 //    qDebug()<<"~DependenciesModel"<<this;
83 }
84
85 void DependenciesModel::resetModel()
86 {
87     m_projects = m_session->projects();
88     m_projects.removeAll(m_project);
89     reset();
90 }
91
92 int DependenciesModel::rowCount(const QModelIndex &index) const
93 {
94     return index.isValid() ? 0 : m_projects.isEmpty() ? 1 : m_projects.size();
95 }
96
97 int DependenciesModel::columnCount(const QModelIndex &index) const
98 {
99     return index.isValid() ? 0 : 1;
100 }
101
102 QVariant DependenciesModel::data(const QModelIndex &index, int role) const
103 {
104     if (m_projects.isEmpty())
105         return role == Qt::DisplayRole
106             ? tr("<No other projects in this session>")
107             : QVariant();
108
109     const Project *p = m_projects.at(index.row());
110
111     switch (role) {
112     case Qt::DisplayRole:
113         return p->displayName();
114     case Qt::CheckStateRole:
115         return m_session->hasDependency(m_project, p) ? Qt::Checked : Qt::Unchecked;
116     case Qt::DecorationRole:
117         return Core::FileIconProvider::instance()->icon(QFileInfo(p->file()->fileName()));
118     default:
119         return QVariant();
120     }
121 }
122
123 bool DependenciesModel::setData(const QModelIndex &index, const QVariant &value, int role)
124 {
125     if (role == Qt::CheckStateRole) {
126         Project *p = m_projects.at(index.row());
127         const Qt::CheckState c = static_cast<Qt::CheckState>(value.toInt());
128
129         if (c == Qt::Checked) {
130             if (m_session->addDependency(m_project, p)) {
131                 emit dataChanged(index, index);
132                 return true;
133             } else {
134                 QMessageBox::warning(0, QCoreApplication::translate("DependenciesModel", "Unable to Add Dependency"),
135                                      QCoreApplication::translate("DependenciesModel", "This would create a circular dependency."));
136             }
137         } else if (c == Qt::Unchecked) {
138             if (m_session->hasDependency(m_project, p)) {
139                 m_session->removeDependency(m_project, p);
140                 emit dataChanged(index, index);
141                 return true;
142             }
143         }
144     }
145     return false;
146 }
147
148 Qt::ItemFlags DependenciesModel::flags(const QModelIndex &index) const
149 {
150     if (m_projects.isEmpty())
151         return Qt::NoItemFlags;
152
153     Qt::ItemFlags rc = QAbstractListModel::flags(index);
154     if (index.column() == 0)
155         rc |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
156     return rc;
157 }
158
159 //
160 // DependenciesView
161 //
162 DependenciesView::DependenciesView(QWidget *parent)
163     : QTreeView(parent)
164 {
165     m_sizeHint = QSize(250, 250);
166     setUniformRowHeights(true);
167     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
168     setRootIsDecorated(false);
169 }
170
171 DependenciesView::~DependenciesView()
172 {
173
174 }
175
176 QSize DependenciesView::sizeHint() const
177 {
178     return m_sizeHint;
179 }
180
181 void DependenciesView::setModel(QAbstractItemModel *newModel)
182 {
183     if (QAbstractItemModel *oldModel = model()) {
184         disconnect(oldModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
185                 this, SLOT(updateSizeHint()));
186         disconnect(oldModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
187                 this, SLOT(updateSizeHint()));
188         disconnect(oldModel, SIGNAL(modelReset()),
189                 this, SLOT(updateSizeHint()));
190         disconnect(oldModel, SIGNAL(layoutChanged()),
191                 this, SLOT(updateSizeHint()));
192     }
193
194     QTreeView::setModel(newModel);
195
196     if (newModel) {
197         connect(newModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
198                 this, SLOT(updateSizeHint()));
199         connect(newModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
200                 this, SLOT(updateSizeHint()));
201         connect(newModel, SIGNAL(modelReset()),
202                 this, SLOT(updateSizeHint()));
203         connect(newModel, SIGNAL(layoutChanged()),
204                 this, SLOT(updateSizeHint()));
205     }
206     updateSizeHint();
207 }
208
209 void DependenciesView::updateSizeHint()
210 {
211     if (!model()) {
212         m_sizeHint = QSize(250, 250);
213         return;
214     }
215
216     int heightOffset = size().height() - viewport()->height();
217
218     int heightPerRow = sizeHintForRow(0);
219     if (heightPerRow == -1) {
220         heightPerRow = 30;
221     }
222     int rows = qMin(qMax(model()->rowCount(), 2), 10);
223     int height = rows * heightPerRow + heightOffset;
224     if (m_sizeHint.height() != height) {
225         m_sizeHint.setHeight(height);
226         updateGeometry();
227     }
228 }
229
230 //
231 // DependenciesWidget
232 //
233
234 DependenciesWidget::DependenciesWidget(SessionManager *session,
235                                        Project *project,
236                                        QWidget *parent)
237     : QWidget(parent)
238     , m_session(session)
239     , m_project(project)
240     , m_model(new DependenciesModel(session, project, this))
241 {
242     QVBoxLayout *vbox = new QVBoxLayout(this);
243     vbox->setContentsMargins(0, 0, 0, 0);
244     m_detailsContainer = new Utils::DetailsWidget(this);
245     m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
246     vbox->addWidget(m_detailsContainer);
247
248     QWidget *detailsWidget = new QWidget(m_detailsContainer);
249     m_detailsContainer->setWidget(detailsWidget);
250     QHBoxLayout *layout = new QHBoxLayout(detailsWidget);
251     layout->setContentsMargins(0, -1, 0, -1);
252     DependenciesView *treeView = new DependenciesView(this);
253     treeView->setModel(m_model);
254     treeView->setHeaderHidden(true);
255     layout->addWidget(treeView);
256     layout->addSpacerItem(new QSpacerItem(0, 0 , QSizePolicy::Expanding, QSizePolicy::Fixed));
257 }
258
259 //
260 // DependenciesPanel
261 //
262
263 DependenciesPanel::DependenciesPanel(SessionManager *session, Project *project) :
264     m_widget(new DependenciesWidget(session, project)),
265     m_icon(":/projectexplorer/images/ProjectDependencies.png")
266 {
267 }
268
269 DependenciesPanel::~DependenciesPanel()
270 {
271     delete m_widget;
272 }
273
274 QString DependenciesPanel::displayName() const
275 {
276     return QCoreApplication::translate("DependenciesPanel", "Dependencies");
277 }
278
279 QWidget *DependenciesPanel::widget() const
280 {
281     return m_widget;
282 }
283
284 QIcon DependenciesPanel::icon() const
285 {
286     return m_icon;
287 }
288
289 //
290 // DependenciesPanelFactory
291 //
292
293 DependenciesPanelFactory::DependenciesPanelFactory(SessionManager *session)
294     : m_session(session)
295 {
296 }
297
298 QString DependenciesPanelFactory::id() const
299 {
300     return QLatin1String(DEPENDENCIES_PANEL_ID);
301 }
302
303 QString DependenciesPanelFactory::displayName() const
304 {
305     return QCoreApplication::translate("DependenciesPanelFactory", "Dependencies");
306 }
307
308 bool DependenciesPanelFactory::supports(Project *project)
309 {
310     Q_UNUSED(project);
311     return true;
312 }
313
314 IPropertiesPanel *DependenciesPanelFactory::createPanel(Project *project)
315 {
316     return new DependenciesPanel(m_session, project);
317 }
318
319 } // namespace Internal
320 } // namespace ProjectExplorer