aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/android/androidextralibrarylistmodel.cpp
blob: 75b27109190b432aba994b3db3dfa506cf5246ce (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
// Copyright (C) 2016 BogDan Vatra <bog_dan_ro@yahoo.com>
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "androidextralibrarylistmodel.h"

#include <android/androidconstants.h>
#include <android/androidmanager.h>

#include <projectexplorer/buildsystem.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/target.h>

#include <utils/qtcassert.h>

using namespace ProjectExplorer;

namespace Android {

AndroidExtraLibraryListModel::AndroidExtraLibraryListModel(BuildSystem *buildSystem,
                                                           QObject *parent)
    : QAbstractItemModel(parent),
      m_buildSystem(buildSystem)
{
    updateModel();

    connect(buildSystem, &BuildSystem::parsingStarted,
            this, &AndroidExtraLibraryListModel::updateModel);
    connect(buildSystem, &BuildSystem::parsingFinished,
            this, &AndroidExtraLibraryListModel::updateModel);
    // Causes target()->activeBuildKey() result and consequently the node data
    // extracted below to change.
    connect(buildSystem->target(), &Target::activeRunConfigurationChanged,
            this, &AndroidExtraLibraryListModel::updateModel);
}

QModelIndex AndroidExtraLibraryListModel::index(int row, int column, const QModelIndex &) const
{
    return createIndex(row, column);
}

QModelIndex AndroidExtraLibraryListModel::parent(const QModelIndex &) const
{
    return QModelIndex();
}

int AndroidExtraLibraryListModel::rowCount(const QModelIndex &) const
{
    return m_entries.size();
}

int AndroidExtraLibraryListModel::columnCount(const QModelIndex &) const
{
    return 1;
}

QVariant AndroidExtraLibraryListModel::data(const QModelIndex &index, int role) const
{
    Q_ASSERT(index.row() >= 0 && index.row() < m_entries.size());
    if (role == Qt::DisplayRole)
        return QDir::cleanPath(m_entries.at(index.row()));
    return {};
}

void AndroidExtraLibraryListModel::updateModel()
{
    const QString buildKey = m_buildSystem->target()->activeBuildKey();
    const ProjectNode *node = m_buildSystem->target()->project()->findNodeForBuildKey(buildKey);
    if (!node)
        return;

    if (node->parseInProgress()) {
        emit enabledChanged(false);
        return;
    }

    bool enabled;
    beginResetModel();
    if (node->validParse()) {
        m_entries = node->data(Constants::AndroidExtraLibs).toStringList();
        enabled = true;
    } else {
        // parsing error
        m_entries.clear();
        enabled = false;
    }
    endResetModel();

    emit enabledChanged(enabled);
}

void AndroidExtraLibraryListModel::addEntries(const QStringList &list)
{
    const QString buildKey = m_buildSystem->target()->activeBuildKey();
    const ProjectNode *node = m_buildSystem->target()->project()->findNodeForBuildKey(buildKey);
    QTC_ASSERT(node, return);

    beginInsertRows(QModelIndex(), m_entries.size(), m_entries.size() + list.size());

    const QDir dir = node->filePath().toFileInfo().absoluteDir();
    for (const QString &path : list)
        m_entries += "$$PWD/" + dir.relativeFilePath(path);

    m_buildSystem->setExtraData(buildKey, Constants::AndroidExtraLibs, m_entries);
    endInsertRows();
}

bool greaterModelIndexByRow(const QModelIndex &a, const QModelIndex &b)
{
    return a.row() > b.row();
}

void AndroidExtraLibraryListModel::removeEntries(QModelIndexList list)
{
    if (list.isEmpty())
        return;

    std::sort(list.begin(), list.end(), greaterModelIndexByRow);

    int i = 0;
    while (i < list.size()) {
        int lastRow = list.at(i++).row();
        int firstRow = lastRow;
        while (i < list.size() && firstRow - list.at(i).row()  <= 1)
            firstRow = list.at(i++).row();

        beginRemoveRows(QModelIndex(), firstRow, lastRow);
        int count = lastRow - firstRow + 1;
        while (count-- > 0)
            m_entries.removeAt(firstRow);
        endRemoveRows();
    }

    const QString buildKey = m_buildSystem->target()->activeBuildKey();
    m_buildSystem->setExtraData(buildKey, Constants::AndroidExtraLibs, m_entries);
}

} // Android