projectmodels.cpp 12.2 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

con's avatar
con committed
26
#include "projectmodels.h"
hjk's avatar
hjk committed
27 28

#include "project.h"
29
#include "projectnodes.h"
dt's avatar
dt committed
30
#include "projectexplorer.h"
31
#include "projecttree.h"
32
#include "session.h"
con's avatar
con committed
33 34

#include <coreplugin/fileiconprovider.h>
35
#include <utils/algorithm.h>
36
#include <utils/dropsupport.h>
hjk's avatar
hjk committed
37

38 39 40
#include <QDebug>
#include <QFileInfo>
#include <QFont>
41
#include <QMimeData>
42
#include <QLoggingCategory>
con's avatar
con committed
43

44 45
using namespace Utils;

46 47 48
namespace ProjectExplorer {

using namespace Internal;
con's avatar
con committed
49

50
static bool sortNodes(const Node *n1, const Node *n2)
hjk's avatar
hjk committed
51
{
52 53 54
    if (n1->priority() > n2->priority())
        return true;
    if (n1->priority() < n2->priority())
hjk's avatar
hjk committed
55
        return false;
con's avatar
con committed
56

57 58 59
    const int displayNameResult = caseFriendlyCompare(n1->displayName(), n2->displayName());
    if (displayNameResult != 0)
        return displayNameResult < 0;
con's avatar
con committed
60

61 62 63 64 65
    const int filePathResult = caseFriendlyCompare(n1->filePath().toString(),
                                 n2->filePath().toString());
    if (filePathResult != 0)
        return filePathResult < 0;
    return n1 < n2; // sort by pointer value
con's avatar
con committed
66 67
}

68 69 70 71
static bool sortWrapperNodes(const WrapperNode *w1, const WrapperNode *w2)
{
    return sortNodes(w1->m_node, w2->m_node);
}
hjk's avatar
hjk committed
72

73 74
FlatModel::FlatModel(QObject *parent)
    : TreeModel<WrapperNode, WrapperNode>(new WrapperNode(SessionManager::sessionNode()), parent)
con's avatar
con committed
75
{
76 77 78
    m_timer.setInterval(200);
    connect(&m_timer, &QTimer::timeout, this, &FlatModel::doUpdate);

79
    ProjectTree *tree = ProjectTree::instance();
80 81
    connect(tree, &ProjectTree::dataChanged, this, &FlatModel::update);
    connect(tree, &ProjectTree::nodeUpdated, this, &FlatModel::nodeUpdated);
con's avatar
con committed
82

83 84 85 86 87
    SessionManager *sm = SessionManager::instance();
    connect(sm, &SessionManager::projectRemoved, this, &FlatModel::update);
    connect(sm, &SessionManager::sessionLoaded, this, &FlatModel::loadExpandData);
    connect(sm, &SessionManager::aboutToSaveSession, this, &FlatModel::saveExpandData);
    connect(sm, &SessionManager::projectAdded, this, &FlatModel::handleProjectAdded);
88
    connect(sm, &SessionManager::startupProjectChanged, this, [this] { layoutChanged(); });
89
    update();
con's avatar
con committed
90 91 92 93 94 95 96
}

QVariant FlatModel::data(const QModelIndex &index, int role) const
{
    QVariant result;

    if (Node *node = nodeForIndex(index)) {
97
        FolderNode *folderNode = node->asFolderNode();
con's avatar
con committed
98
        switch (role) {
99 100
        case Qt::DisplayRole: {
            QString name = node->displayName();
101
            if (node->nodeType() == NodeType::Project
102
                    && node->parentFolderNode()
103
                    && node->parentFolderNode()->nodeType() == NodeType::Session) {
104
                const QString vcsTopic = static_cast<ProjectNode *>(node)->vcsTopic();
105 106

                if (!vcsTopic.isEmpty())
107
                    name += QLatin1String(" [") + vcsTopic + QLatin1Char(']');
108 109 110 111 112
            }

            result = name;
            break;
        }
con's avatar
con committed
113
        case Qt::EditRole: {
114
            result = node->filePath().fileName();
con's avatar
con committed
115 116 117
            break;
        }
        case Qt::ToolTipRole: {
118
            result = node->tooltip();
con's avatar
con committed
119 120 121 122 123 124
            break;
        }
        case Qt::DecorationRole: {
            if (folderNode)
                result = folderNode->icon();
            else
125
                result = Core::FileIconProvider::icon(node->filePath().toString());
con's avatar
con committed
126 127 128 129
            break;
        }
        case Qt::FontRole: {
            QFont font;
130 131 132 133
            if (Project *project = SessionManager::startupProject()) {
                if (node == project->rootProjectNode())
                    font.setBold(true);
            }
con's avatar
con committed
134 135 136
            result = font;
            break;
        }
137
        case Project::FilePathRole: {
138
            result = node->filePath().toString();
con's avatar
con committed
139 140
            break;
        }
141
        case Project::EnabledRole: {
142 143 144
            result = node->isEnabled();
            break;
        }
con's avatar
con committed
145 146 147 148 149 150
        }
    }

    return result;
}

dt's avatar
dt committed
151 152 153 154 155 156 157
Qt::ItemFlags FlatModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;
    // We claim that everything is editable
    // That's slightly wrong
    // We control the only view, and that one does the checks
158
    Qt::ItemFlags f = Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled;
159
    if (Node *node = nodeForIndex(index)) {
160
        if (!node->asProjectNode()) {
161
            // either folder or file node
162
            if (node->supportedActions(node).contains(Rename))
163 164 165 166
                f = f | Qt::ItemIsEditable;
        }
    }
    return f;
dt's avatar
dt committed
167 168 169 170 171 172 173 174 175
}

bool FlatModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;
    if (role != Qt::EditRole)
        return false;

176
    Node *node = nodeForIndex(index);
177
    QTC_ASSERT(node, return false);
178

179
    Utils::FileName orgFilePath = node->filePath();
180
    Utils::FileName newFilePath = orgFilePath.parentDir().appendPath(value.toString());
181

182
    ProjectExplorerPlugin::renameFile(node, newFilePath.toString());
183
    emit renamed(orgFilePath, newFilePath);
dt's avatar
dt committed
184 185 186
    return true;
}

187
void FlatModel::update()
con's avatar
con committed
188
{
189
    m_timer.start(300);
con's avatar
con committed
190 191
}

192
void FlatModel::doUpdate()
con's avatar
con committed
193
{
194 195
    m_timer.stop();
    rebuildModel();
con's avatar
con committed
196 197
}

198
void FlatModel::rebuildModel()
con's avatar
con committed
199
{
200
    QSet<Node *> seen;
con's avatar
con committed
201

202
    rootItem()->removeChildren();
203 204 205 206 207
    for (Node *node : SessionManager::sessionNode()->nodes()) {
        if (ProjectNode *projectNode = node->asProjectNode()) {
            if (!seen.contains(projectNode))
                addProjectNode(rootItem(), projectNode, &seen);
        }
con's avatar
con committed
208
    }
209
    rootItem()->sortChildren(&sortWrapperNodes);
con's avatar
con committed
210

211 212 213 214 215
    forAllItems([this](WrapperNode *node) {
        const QString path = node->m_node->filePath().toString();
        const QString displayName = node->m_node->displayName();
        ExpandData ed(path, displayName);
        if (m_toExpand.contains(ed))
216
            emit requestExpansion(node->index());
217
    });
con's avatar
con committed
218 219
}

220
void FlatModel::onCollapsed(const QModelIndex &idx)
con's avatar
con committed
221
{
222
    m_toExpand.remove(expandDataForNode(nodeForIndex(idx)));
con's avatar
con committed
223 224
}

225
void FlatModel::onExpanded(const QModelIndex &idx)
con's avatar
con committed
226
{
227
    m_toExpand.insert(expandDataForNode(nodeForIndex(idx)));
con's avatar
con committed
228 229
}

230
ExpandData FlatModel::expandDataForNode(const Node *node) const
con's avatar
con committed
231
{
232 233 234 235
    QTC_ASSERT(node, return ExpandData());
    const QString path = node->filePath().toString();
    const QString displayName = node->displayName();
    return ExpandData(path, displayName);
con's avatar
con committed
236 237
}

238
void FlatModel::handleProjectAdded(Project *project)
con's avatar
con committed
239
{
240 241 242 243 244 245
    Node *node = project->rootProjectNode();
    m_toExpand.insert(expandDataForNode(node));
    if (WrapperNode *wrapper = wrapperForNode(node)) {
        wrapper->forFirstLevelChildren([this](WrapperNode *child) {
            m_toExpand.insert(expandDataForNode(child->m_node));
        });
con's avatar
con committed
246
    }
247
    doUpdate();
con's avatar
con committed
248 249
}

250
void FlatModel::loadExpandData()
con's avatar
con committed
251
{
252 253 254
    const QList<QVariant> data = SessionManager::value("ProjectTree.ExpandData").value<QList<QVariant>>();
    m_toExpand = Utils::transform<QSet>(data, [](const QVariant &v) { return ExpandData::fromSettings(v); });
    m_toExpand.remove(ExpandData());
con's avatar
con committed
255 256
}

257
void FlatModel::saveExpandData()
con's avatar
con committed
258
{
259 260 261
    // TODO if there are multiple ProjectTreeWidgets, the last one saves the data
    QList<QVariant> data = Utils::transform<QList>(m_toExpand, &ExpandData::toSettings);
    SessionManager::setValue(QLatin1String("ProjectTree.ExpandData"), data);
con's avatar
con committed
262 263
}

264
void FlatModel::addProjectNode(WrapperNode *parent, ProjectNode *projectNode, QSet<Node *> *seen)
con's avatar
con committed
265
{
266 267 268 269
    seen->insert(projectNode);
    auto node = new WrapperNode(projectNode);
    parent->appendChild(node);
    addFolderNode(node, projectNode, seen);
270 271 272 273 274
    for (Node *subNode : projectNode->nodes()) {
        if (ProjectNode *subProjectNode = subNode->asProjectNode()) {
            if (!seen->contains(subProjectNode))
                addProjectNode(node, subProjectNode, seen);
        }
con's avatar
con committed
275
    }
276
    node->sortChildren(&sortWrapperNodes);
con's avatar
con committed
277 278
}

279
void FlatModel::addFolderNode(WrapperNode *parent, FolderNode *folderNode, QSet<Node *> *seen)
con's avatar
con committed
280
{
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    const QList<FolderNode *> subFolderNodes = folderNode->folderNodes();
    for (FolderNode *subFolderNode : subFolderNodes) {
        if (!filter(subFolderNode) && !seen->contains(subFolderNode)) {
            seen->insert(subFolderNode);
            auto node = new WrapperNode(subFolderNode);
            parent->appendChild(node);
            addFolderNode(node, subFolderNode, seen);
            node->sortChildren(&sortWrapperNodes);
        } else {
            addFolderNode(parent, subFolderNode, seen);
        }
    }
    const QList<FileNode *> fileNodes = folderNode->fileNodes();
    for (FileNode *fileNode : fileNodes) {
        if (!filter(fileNode) && !seen->contains(fileNode)) {
            seen->insert(fileNode);
            parent->appendChild(new WrapperNode(fileNode));
        }
    }
con's avatar
con committed
300 301
}

302 303 304 305 306
Qt::DropActions FlatModel::supportedDragActions() const
{
    return Qt::MoveAction;
}

307 308
QStringList FlatModel::mimeTypes() const
{
309
    return Utils::DropSupport::mimeTypesForFilePaths();
310 311 312 313
}

QMimeData *FlatModel::mimeData(const QModelIndexList &indexes) const
{
314
    auto data = new Utils::DropMimeData;
315
    foreach (const QModelIndex &index, indexes) {
316 317 318 319 320
        if (Node *node = nodeForIndex(index)) {
            if (node->asFileNode())
                data->addFile(node->filePath().toString());
            data->addValue(QVariant::fromValue(node));
        }
321
    }
322
    return data;
323 324
}

325
WrapperNode *FlatModel::wrapperForNode(const Node *node) const
con's avatar
con committed
326
{
327 328 329 330
    return findNonRooItem([this, node](WrapperNode *item) {
        return item->m_node == node;
    });
}
con's avatar
con committed
331

332 333 334 335
QModelIndex FlatModel::indexForNode(const Node *node) const
{
    WrapperNode *wrapper = wrapperForNode(node);
    return wrapper ? indexForItem(wrapper) : QModelIndex();
con's avatar
con committed
336 337 338 339
}

void FlatModel::setProjectFilterEnabled(bool filter)
{
340 341
    if (filter == m_filterProjects)
        return;
con's avatar
con committed
342
    m_filterProjects = filter;
343
    rebuildModel();
con's avatar
con committed
344 345 346 347 348
}

void FlatModel::setGeneratedFilesFilterEnabled(bool filter)
{
    m_filterGeneratedFiles = filter;
349
    rebuildModel();
con's avatar
con committed
350 351
}

352 353 354 355 356 357 358 359 360 361
bool FlatModel::projectFilterEnabled()
{
    return m_filterProjects;
}

bool FlatModel::generatedFilesFilterEnabled()
{
    return m_filterGeneratedFiles;
}

con's avatar
con committed
362 363
Node *FlatModel::nodeForIndex(const QModelIndex &index) const
{
364 365
    WrapperNode *flatNode = itemForIndex(index);
    return flatNode ? flatNode->m_node : nullptr;
con's avatar
con committed
366 367 368 369 370
}

bool FlatModel::filter(Node *node) const
{
    bool isHidden = false;
371
    if (FolderNode *folderNode = node->asFolderNode()) {
372
        if (m_filterProjects)
373
            isHidden = !folderNode->showInSimpleTree();
374
    } else if (FileNode *fileNode = node->asFileNode()) {
con's avatar
con committed
375 376 377 378 379 380
        if (m_filterGeneratedFiles)
            isHidden = fileNode->isGenerated();
    }
    return isHidden;
}

381 382 383 384 385 386
const QLoggingCategory &FlatModel::logger()
{
    static QLoggingCategory logger("qtc.projectexplorer.flatmodel");
    return logger;
}

387 388 389 390 391 392 393 394 395 396
bool isSorted(const QList<Node *> &nodes)
{
    int size = nodes.size();
    for (int i = 0; i < size -1; ++i) {
        if (!sortNodes(nodes.at(i), nodes.at(i+1)))
            return false;
    }
    return true;
}

397
void FlatModel::nodeUpdated(Node *)
398
{
399
    update();
400 401
}

402 403 404 405 406 407 408 409 410 411 412
namespace Internal {

int caseFriendlyCompare(const QString &a, const QString &b)
{
    int result = a.compare(b, Qt::CaseInsensitive);
    if (result != 0)
        return result;
    return a.compare(b, Qt::CaseSensitive);
}

}
413 414

} // namespace ProjectExplorer
415