1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "dependenciespanel.h"
37 #include <coreplugin/fileiconprovider.h>
38 #include <coreplugin/ifile.h>
39 #include <utils/detailswidget.h>
41 #include <QtCore/QVector>
42 #include <QtCore/QDebug>
43 #include <QtCore/QSize>
44 #include <QtCore/QCoreApplication>
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>
58 namespace ProjectExplorer {
61 DependenciesModel::DependenciesModel(SessionManager *session,
64 : QAbstractListModel(parent)
67 , m_projects(session->projects())
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()<<")";
80 DependenciesModel::~DependenciesModel()
82 // qDebug()<<"~DependenciesModel"<<this;
85 void DependenciesModel::resetModel()
87 m_projects = m_session->projects();
88 m_projects.removeAll(m_project);
92 int DependenciesModel::rowCount(const QModelIndex &index) const
94 return index.isValid() ? 0 : m_projects.isEmpty() ? 1 : m_projects.size();
97 int DependenciesModel::columnCount(const QModelIndex &index) const
99 return index.isValid() ? 0 : 1;
102 QVariant DependenciesModel::data(const QModelIndex &index, int role) const
104 if (m_projects.isEmpty())
105 return role == Qt::DisplayRole
106 ? tr("<No other projects in this session>")
109 const Project *p = m_projects.at(index.row());
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()));
123 bool DependenciesModel::setData(const QModelIndex &index, const QVariant &value, int role)
125 if (role == Qt::CheckStateRole) {
126 Project *p = m_projects.at(index.row());
127 const Qt::CheckState c = static_cast<Qt::CheckState>(value.toInt());
129 if (c == Qt::Checked) {
130 if (m_session->addDependency(m_project, p)) {
131 emit dataChanged(index, index);
134 QMessageBox::warning(0, QCoreApplication::translate("DependenciesModel", "Unable to Add Dependency"),
135 QCoreApplication::translate("DependenciesModel", "This would create a circular dependency."));
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);
148 Qt::ItemFlags DependenciesModel::flags(const QModelIndex &index) const
150 if (m_projects.isEmpty())
151 return Qt::NoItemFlags;
153 Qt::ItemFlags rc = QAbstractListModel::flags(index);
154 if (index.column() == 0)
155 rc |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
162 DependenciesView::DependenciesView(QWidget *parent)
165 m_sizeHint = QSize(250, 250);
166 setUniformRowHeights(true);
167 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
168 setRootIsDecorated(false);
171 DependenciesView::~DependenciesView()
176 QSize DependenciesView::sizeHint() const
181 void DependenciesView::setModel(QAbstractItemModel *newModel)
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()));
194 QTreeView::setModel(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()));
209 void DependenciesView::updateSizeHint()
212 m_sizeHint = QSize(250, 250);
216 int heightOffset = size().height() - viewport()->height();
218 int heightPerRow = sizeHintForRow(0);
219 if (heightPerRow == -1) {
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);
231 // DependenciesWidget
234 DependenciesWidget::DependenciesWidget(SessionManager *session,
240 , m_model(new DependenciesModel(session, project, this))
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);
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));
263 DependenciesPanel::DependenciesPanel(SessionManager *session, Project *project) :
264 m_widget(new DependenciesWidget(session, project)),
265 m_icon(":/projectexplorer/images/ProjectDependencies.png")
269 DependenciesPanel::~DependenciesPanel()
274 QString DependenciesPanel::displayName() const
276 return QCoreApplication::translate("DependenciesPanel", "Dependencies");
279 QWidget *DependenciesPanel::widget() const
284 QIcon DependenciesPanel::icon() const
290 // DependenciesPanelFactory
293 DependenciesPanelFactory::DependenciesPanelFactory(SessionManager *session)
298 QString DependenciesPanelFactory::id() const
300 return QLatin1String(DEPENDENCIES_PANEL_ID);
303 QString DependenciesPanelFactory::displayName() const
305 return QCoreApplication::translate("DependenciesPanelFactory", "Dependencies");
308 bool DependenciesPanelFactory::supports(Project *project)
314 IPropertiesPanel *DependenciesPanelFactory::createPanel(Project *project)
316 return new DependenciesPanel(m_session, project);
319 } // namespace Internal
320 } // namespace ProjectExplorer