aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/dependenciespanel.cpp
blob: 97609e57d9320a0dd9d2cfab438ce9978269e384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "dependenciespanel.h"

#include "project.h"
#include "projectexplorertr.h"
#include "projectmanager.h"
#include "projectpanelfactory.h"
#include "projectsettingswidget.h"

#include <coreplugin/icore.h>
#include <coreplugin/session.h>

#include <utils/algorithm.h>
#include <utils/detailswidget.h>
#include <utils/fsengine/fileiconprovider.h>

#include <QAbstractListModel>
#include <QCheckBox>
#include <QGridLayout>
#include <QMessageBox>
#include <QSize>
#include <QSpacerItem>
#include <QTreeView>

namespace ProjectExplorer::Internal {

class DependenciesModel : public QAbstractListModel
{
public:
    explicit DependenciesModel(Project *project)
        : m_project(project)
    {
        resetModel();

        connect(ProjectManager::instance(), &ProjectManager::projectRemoved,
                this, &DependenciesModel::resetModel);
        connect(ProjectManager::instance(), &ProjectManager::projectAdded,
                this, &DependenciesModel::resetModel);
        connect(Core::SessionManager::instance(), &Core::SessionManager::sessionLoaded,
                this, &DependenciesModel::resetModel);
    }

    int rowCount(const QModelIndex &index) const override;
    int columnCount(const QModelIndex &index) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;

private:
    void resetModel();

    Project *m_project;
    QList<Project *> m_projects;
};

void DependenciesModel::resetModel()
{
    beginResetModel();
    m_projects = ProjectManager::projects();
    m_projects.removeAll(m_project);
    Utils::sort(m_projects, [](Project *a, Project *b) {
        return a->displayName() < b->displayName();
    });
    endResetModel();
}

int DependenciesModel::rowCount(const QModelIndex &index) const
{
    return index.isValid() ? 0 : m_projects.isEmpty() ? 1 : m_projects.size();
}

int DependenciesModel::columnCount(const QModelIndex &index) const
{
    return index.isValid() ? 0 : 1;
}

QVariant DependenciesModel::data(const QModelIndex &index, int role) const
{
    if (m_projects.isEmpty())
        return role == Qt::DisplayRole
            ? Tr::tr("<No other projects in this session>")
            : QVariant();

    const Project *p = m_projects.at(index.row());

    switch (role) {
    case Qt::DisplayRole:
        return p->displayName();
    case Qt::ToolTipRole:
        return p->projectFilePath().toUserOutput();
    case Qt::CheckStateRole:
        return ProjectManager::hasDependency(m_project, p) ? Qt::Checked : Qt::Unchecked;
    case Qt::DecorationRole:
        return Utils::FileIconProvider::icon(p->projectFilePath());
    default:
        return {};
    }
}

bool DependenciesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (role == Qt::CheckStateRole) {
        Project *p = m_projects.at(index.row());
        const auto c = static_cast<Qt::CheckState>(value.toInt());

        if (c == Qt::Checked) {
            if (ProjectManager::addDependency(m_project, p)) {
                emit dataChanged(index, index);
                return true;
            } else {
                QMessageBox::warning(Core::ICore::dialogParent(), Tr::tr("Unable to Add Dependency"),
                                     Tr::tr("This would create a circular dependency."));
            }
        } else if (c == Qt::Unchecked) {
            if (ProjectManager::hasDependency(m_project, p)) {
                ProjectManager::removeDependency(m_project, p);
                emit dataChanged(index, index);
                return true;
            }
        }
    }
    return false;
}

Qt::ItemFlags DependenciesModel::flags(const QModelIndex &index) const
{
    if (m_projects.isEmpty())
        return Qt::NoItemFlags;

    Qt::ItemFlags rc = QAbstractListModel::flags(index);
    if (index.column() == 0)
        rc |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
    return rc;
}

//
// DependenciesView
//

class DependenciesView : public QTreeView
{
public:
    explicit DependenciesView(QWidget *parent);

    QSize sizeHint() const override;
    void setModel(QAbstractItemModel *model) override;

private:
    void updateSizeHint();

    QSize m_sizeHint;
};

DependenciesView::DependenciesView(QWidget *parent)
    : QTreeView(parent)
{
    m_sizeHint = QSize(250, 250);
    setUniformRowHeights(true);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
    setRootIsDecorated(false);
}

QSize DependenciesView::sizeHint() const
{
    return m_sizeHint;
}

void DependenciesView::setModel(QAbstractItemModel *newModel)
{
    if (QAbstractItemModel *oldModel = model()) {
        disconnect(oldModel, &QAbstractItemModel::rowsInserted,
                   this, &DependenciesView::updateSizeHint);
        disconnect(oldModel, &QAbstractItemModel::rowsRemoved,
                   this, &DependenciesView::updateSizeHint);
        disconnect(oldModel, &QAbstractItemModel::modelReset,
                   this, &DependenciesView::updateSizeHint);
        disconnect(oldModel, &QAbstractItemModel::layoutChanged,
                   this, &DependenciesView::updateSizeHint);
    }

    QTreeView::setModel(newModel);

    if (newModel) {
        connect(newModel, &QAbstractItemModel::rowsInserted,
                this, &DependenciesView::updateSizeHint);
        connect(newModel, &QAbstractItemModel::rowsRemoved,
                this, &DependenciesView::updateSizeHint);
        connect(newModel, &QAbstractItemModel::modelReset,
                this, &DependenciesView::updateSizeHint);
        connect(newModel, &QAbstractItemModel::layoutChanged,
                this, &DependenciesView::updateSizeHint);
    }
    updateSizeHint();
}

void DependenciesView::updateSizeHint()
{
    if (!model()) {
        m_sizeHint = QSize(250, 250);
        return;
    }

    int heightOffset = size().height() - viewport()->height();

    int heightPerRow = sizeHintForRow(0);
    if (heightPerRow == -1)
        heightPerRow = 30;
    int rows = qMin(qMax(model()->rowCount(), 2), 10);
    int height = rows * heightPerRow + heightOffset;
    if (m_sizeHint.height() != height) {
        m_sizeHint.setHeight(height);
        updateGeometry();
    }
}

//
// DependenciesWidget
//

class DependenciesWidget : public ProjectSettingsWidget
{
public:
    explicit DependenciesWidget(Project *project)
        : m_model(project)
    {
        setUseGlobalSettingsCheckBoxVisible(false);
        setUseGlobalSettingsLabelVisible(false);
        auto vbox = new QVBoxLayout(this);
        vbox->setContentsMargins(0, 0, 0, 0);
        m_detailsContainer = new Utils::DetailsWidget(this);
        m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
        vbox->addWidget(m_detailsContainer);

        auto detailsWidget = new QWidget(m_detailsContainer);
        m_detailsContainer->setWidget(detailsWidget);
        auto layout = new QGridLayout(detailsWidget);
        layout->setContentsMargins(0, -1, 0, -1);
        auto treeView = new DependenciesView(this);
        treeView->setModel(&m_model);
        treeView->setHeaderHidden(true);
        layout->addWidget(treeView, 0 ,0);
        layout->addItem(new QSpacerItem(0, 0 , QSizePolicy::Expanding, QSizePolicy::Fixed), 0, 1);

        m_cascadeSetActiveCheckBox = new QCheckBox;
        m_cascadeSetActiveCheckBox->setText(Tr::tr("Synchronize configuration"));
        m_cascadeSetActiveCheckBox->setToolTip(Tr::tr("Synchronize active kit, build, and deploy configuration between projects."));
        m_cascadeSetActiveCheckBox->setChecked(ProjectManager::isProjectConfigurationCascading());
        connect(m_cascadeSetActiveCheckBox, &QCheckBox::toggled,
                ProjectManager::instance(), &ProjectManager::setProjectConfigurationCascading);
        layout->addWidget(m_cascadeSetActiveCheckBox, 1, 0, 2, 1);
    }

private:
    DependenciesModel m_model;
    Utils::DetailsWidget *m_detailsContainer;
    QCheckBox *m_cascadeSetActiveCheckBox;
};

class DependenciesProjectPanelFactory final : public ProjectPanelFactory
{
public:
    DependenciesProjectPanelFactory()
    {
        setPriority(50);
        setDisplayName(Tr::tr("Dependencies"));
        setCreateWidgetFunction([](Project *project) { return new DependenciesWidget(project); });
    }
};

void setupDependenciesProjectPanel()
{
    static DependenciesProjectPanelFactory theDependenciesProjectPanelFactory;
}

} // ProjectExplorer::Internal