toolchainoptionspage.cpp 18 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "toolchainoptionspage.h"

#include "projectexplorerconstants.h"
#include "toolchainconfigwidget.h"
#include "toolchainmanager.h"

#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>

43 44
#include <utils/qtcassert.h>

45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <QtCore/QSignalMapper>
#include <QtCore/QTextStream>
#include <QtGui/QAction>
#include <QtGui/QItemSelectionModel>
#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>

namespace ProjectExplorer {
namespace Internal {

class ToolChainNode
{
public:
59 60
    explicit ToolChainNode(ToolChainNode *p, ToolChain *tc = 0, bool c = false) :
        parent(p), toolChain(tc), changed(c)
61
    {
62 63
        if (p)
            p->childNodes.append(this);
64 65 66 67 68 69 70 71 72
        widget = tc ? tc->configurationWidget() : 0;
        if (widget) {
            widget->setEnabled(tc ? !tc->isAutoDetected() : false);
            widget->setVisible(false);
        }
    }

    ~ToolChainNode()
    {
73
        qDeleteAll(childNodes);
74 75 76 77 78 79
        // Do not delete toolchain, we do not own it.
        delete widget;
    }

    ToolChainNode *parent;
    QString newName;
80
    QList<ToolChainNode *> childNodes;
81 82 83 84 85 86 87 88 89 90
    ToolChain *toolChain;
    ToolChainConfigWidget *widget;
    bool changed;
};

// --------------------------------------------------------------------------
// ToolChainModel
// --------------------------------------------------------------------------

ToolChainModel::ToolChainModel(QObject *parent) :
91
    QAbstractItemModel(parent)
92 93 94 95 96 97
{
    connect(ToolChainManager::instance(), SIGNAL(toolChainAdded(ProjectExplorer::ToolChain*)),
            this, SLOT(addToolChain(ProjectExplorer::ToolChain*)));
    connect(ToolChainManager::instance(), SIGNAL(toolChainRemoved(ProjectExplorer::ToolChain*)),
            this, SLOT(removeToolChain(ProjectExplorer::ToolChain*)));

98 99 100
    m_autoRoot = new ToolChainNode(0);
    m_manualRoot = new ToolChainNode(0);

101
    foreach (ToolChain *tc, ToolChainManager::instance()->toolChains()) {
102 103 104 105
        if (tc->isAutoDetected())
            new ToolChainNode(m_autoRoot, tc);
        else {
            ToolChainNode *node = new ToolChainNode(m_manualRoot, tc);
106 107 108 109 110 111 112 113 114
            if (node->widget)
                connect(node->widget, SIGNAL(dirty(ProjectExplorer::ToolChain*)),
                        this, SLOT(setDirty(ProjectExplorer::ToolChain*)));
        }
    }
}

ToolChainModel::~ToolChainModel()
{
115 116
    delete m_autoRoot;
    delete m_manualRoot;
117 118 119 120 121 122
}

QModelIndex ToolChainModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        if (row == 0)
123
            return createIndex(0, 0, static_cast<void *>(m_autoRoot));
124
        else
125
            return createIndex(1, 0, static_cast<void *>(m_manualRoot));
126 127 128
    }
    ToolChainNode *node = static_cast<ToolChainNode *>(parent.internalPointer());
    if (row < node->childNodes.count() && column < 2)
129
        return createIndex(row, column, static_cast<void *>(node->childNodes.at(row)));
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
    else
        return QModelIndex();
}

QModelIndex ToolChainModel::parent(const QModelIndex &idx) const
{
    ToolChainNode *node = static_cast<ToolChainNode *>(idx.internalPointer());
    if (node->parent == 0)
        return QModelIndex();
    return index(node->parent);
}

int ToolChainModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return 2;
    ToolChainNode *node = static_cast<ToolChainNode *>(parent.internalPointer());
    return node->childNodes.count();
}

int ToolChainModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 2;
}

QVariant ToolChainModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    ToolChainNode *node = static_cast<ToolChainNode *>(index.internalPointer());
    Q_ASSERT(node);
    if (node == m_autoRoot && index.column() == 0 && role == Qt::DisplayRole)
        return tr("Auto-detected");
    if (node == m_manualRoot && index.column() == 0 && role == Qt::DisplayRole)
        return tr("Manual");
    if (node->toolChain) {
        if (role == Qt::FontRole) {
            QFont f = QApplication::font();
            if (node->changed) {
                f.setBold(true);
            }
            return f;
        }
        if (role == Qt::DisplayRole || role == Qt::EditRole) {
            if (index.column() == 0) {
                return node->newName.isEmpty() ?
                    node->toolChain->displayName() : node->newName;
            }
            return node->toolChain->typeName();
        }
        if (role == Qt::ToolTipRole) {
            return tr("<nobr><b>ABI:</b> %1")
                    .arg(node->changed ? tr("not up-to-date") : node->toolChain->targetAbi().toString());
        }
    }
    return QVariant();
}

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

    ToolChainNode *node = static_cast<ToolChainNode *>(index.internalPointer());
    Q_ASSERT(node);
    if (index.column() != 0 || !node->toolChain || role != Qt::EditRole)
        return false;
    node->newName = value.toString();
    if (!node->newName.isEmpty() && node->newName != node->toolChain->displayName())
        node->changed = true;
    return true;
}

Qt::ItemFlags ToolChainModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    ToolChainNode *node = static_cast<ToolChainNode *>(index.internalPointer());
    Q_ASSERT(node);
    if (!node->toolChain)
        return Qt::ItemIsEnabled;

    if (node->toolChain->isAutoDetected())
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    else if (index.column() == 0)
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    else
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant ToolChainModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        return section == 0 ? tr("Name") : tr("Type");
    return QVariant();
}

ToolChain *ToolChainModel::toolChain(const QModelIndex &index)
{
    if (!index.isValid())
        return 0;
    ToolChainNode *node = static_cast<ToolChainNode *>(index.internalPointer());
    Q_ASSERT(node);
    return node->toolChain;
}

int ToolChainModel::manualToolChains() const
{
    return m_manualRoot->childNodes.count();
}

ToolChainConfigWidget *ToolChainModel::widget(const QModelIndex &index)
{
    if (!index.isValid())
        return 0;
    ToolChainNode *node = static_cast<ToolChainNode *>(index.internalPointer());
    Q_ASSERT(node);
    return node->widget;
}

bool ToolChainModel::isDirty() const
{
255
    foreach (ToolChainNode *n, m_manualRoot->childNodes) {
256 257 258 259 260 261 262 263
        if (n->changed)
            return true;
    }
    return false;
}

bool ToolChainModel::isDirty(ToolChain *tc) const
{
264
    foreach (ToolChainNode *n, m_manualRoot->childNodes) {
265 266 267 268 269 270 271 272
        if (n->toolChain == tc && n->changed)
            return true;
    }
    return false;
}

void ToolChainModel::setDirty(ToolChain *tc)
{
273
    foreach (ToolChainNode *n, m_manualRoot->childNodes) {
274 275
        if (n->toolChain == tc) {
            n->changed = true;
276
            emit dataChanged(index(n, 0), index(n, columnCount(QModelIndex())));
277 278 279 280 281 282 283
        }
    }
}

void ToolChainModel::apply()
{
    // Remove unused ToolChains:
284 285
    QList<ToolChainNode *> nodes = m_toRemoveList;
    foreach (ToolChainNode *n, nodes) {
286 287
        ToolChainManager::instance()->deregisterToolChain(n->toolChain);
    }
288
    Q_ASSERT(m_toRemoveList.isEmpty());
289 290

    // Update toolchains:
291 292
    foreach (ToolChainNode *n, m_manualRoot->childNodes) {
        Q_ASSERT(n);
293 294 295 296 297 298 299 300
        if (n->changed) {
            Q_ASSERT(n->toolChain);
            if (!n->newName.isEmpty())
                n->toolChain->setDisplayName(n->newName);
            if (n->widget)
                n->widget->apply();
            n->changed = false;

301
            emit dataChanged(index(n, 0), index(n, columnCount(QModelIndex())));
302 303 304 305 306
        }
    }

    // Add new (and already updated) toolchains
    nodes = m_toAddList;
307
    foreach (ToolChainNode *n, nodes) {
308 309
        ToolChainManager::instance()->registerToolChain(n->toolChain);
    }
310 311 312 313 314 315
    //
    foreach (ToolChainNode *n, m_toAddList) {
        delete n->toolChain;
        n->toolChain = 0;
    }
    qDeleteAll(m_toAddList);
316 317 318 319 320
}

void ToolChainModel::discard()
{
    // Remove newly "added" toolchains:
321 322 323
    foreach (ToolChainNode *n, m_toAddList) {
        int pos = m_manualRoot->childNodes.indexOf(n);
        Q_ASSERT(pos >= 0);
324
        m_manualRoot->childNodes.removeAt(pos);
325

326 327 328 329
        // Clean up Node: We still own the toolchain!
        delete n->toolChain;
        n->toolChain = 0;
    }
330
    qDeleteAll(m_toAddList);
331 332 333
    m_toAddList.clear();

    // Add "removed" toolchains again:
334 335
    foreach (ToolChainNode *n, m_toRemoveList) {
        m_manualRoot->childNodes.append(n);
336 337 338 339
    }
    m_toRemoveList.clear();

    // Reset toolchains:
340 341
    foreach (ToolChainNode *n, m_manualRoot->childNodes) {
        Q_ASSERT(n);
342 343 344 345 346
        n->newName.clear();
        if (n->widget)
            n->widget->discard();
        n->changed = false;
    }
347
    reset();
348 349 350 351
}

void ToolChainModel::markForRemoval(ToolChain *tc)
{
352 353 354 355 356 357 358 359 360 361
    ToolChainNode *node = 0;
    foreach (ToolChainNode *n, m_manualRoot->childNodes) {
        if (n->toolChain == tc) {
            node = n;
            break;
        }
    }
    if (node) {
        emit beginRemoveRows(index(m_manualRoot), m_manualRoot->childNodes.indexOf(node), m_manualRoot->childNodes.indexOf(node));
        m_manualRoot->childNodes.removeOne(node);
362 363 364 365 366 367 368
        if (m_toAddList.contains(node)) {
            delete node->toolChain;
            node->toolChain = 0;
            m_toAddList.removeOne(node);
        } else {
            m_toRemoveList.append(node);
        }
369 370 371 372 373 374
        emit endRemoveRows();
    }
}

void ToolChainModel::markForAddition(ToolChain *tc)
{
375 376
    int pos = m_manualRoot->childNodes.size();
    emit beginInsertRows(index(m_manualRoot), pos, pos);
377

378
    ToolChainNode *node = new ToolChainNode(m_manualRoot, tc);
379 380 381 382 383 384 385 386 387
    node->changed = true;
    m_toAddList.append(node);

    emit endInsertRows();
}

QModelIndex ToolChainModel::index(ToolChainNode *node, int column) const
{
    if (!node->parent)
388 389 390
        return index(node == m_autoRoot ? 0 : 1, column, QModelIndex());
    else
        return index(node->parent->childNodes.indexOf(node), column, index(node->parent));
391 392 393 394
}

void ToolChainModel::addToolChain(ToolChain *tc)
{
395 396 397 398 399 400 401
    QList<ToolChainNode *> nodes = m_toAddList;
    foreach (ToolChainNode *n, nodes) {
        if (n->toolChain == tc) {
            m_toAddList.removeOne(n);
            // do not delete n: Still used elsewhere!
            return;
        }
402 403
    }

404 405 406 407 408 409 410
    ToolChainNode *parent = m_manualRoot;
    if (tc->isAutoDetected())
        parent = m_autoRoot;
    int row = parent->childNodes.count();

    beginInsertRows(index(parent), row, row);
    new ToolChainNode(parent, tc, true);
411 412 413 414 415 416 417
    endInsertRows();

    emit toolChainStateChanged();
}

void ToolChainModel::removeToolChain(ToolChain *tc)
{
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    QList<ToolChainNode *> nodes = m_toRemoveList;
    foreach (ToolChainNode *n, nodes) {
        if (n->toolChain == tc) {
            m_toRemoveList.removeOne(n);
            delete n;
            return;
        }
    }

    ToolChainNode *parent = m_manualRoot;
    if (tc->isAutoDetected())
        parent = m_autoRoot;
    int row = 0;
    ToolChainNode *node = 0;
    foreach (ToolChainNode *current, parent->childNodes) {
        if (current->toolChain == tc) {
            node = current;
            break;
        }
        ++row;
438 439
    }

440 441 442 443 444
    beginRemoveRows(index(parent), row, row);
    parent->childNodes.removeAt(row);
    delete node;
    endRemoveRows();

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    emit toolChainStateChanged();
}

// --------------------------------------------------------------------------
// ToolChainOptionsPage
// --------------------------------------------------------------------------

ToolChainOptionsPage::ToolChainOptionsPage() :
    m_ui(0), m_cloneAction(0), m_model(0), m_selectionModel(0), m_currentTcWidget(0)
{ }

QString ToolChainOptionsPage::id() const
{
    return QLatin1String(Constants::TOOLCHAIN_SETTINGS_PAGE_ID);
}

QString ToolChainOptionsPage::displayName() const
{
    return tr("Toolchains");
}

QString ToolChainOptionsPage::category() const
{
    return QLatin1String(Constants::TOOLCHAIN_SETTINGS_CATEGORY);
}

QString ToolChainOptionsPage::displayCategory() const
{
    return tr("Toolchains");
}

QIcon ToolChainOptionsPage::categoryIcon() const
{
    return QIcon(QLatin1String(Constants::ICON_TOOLCHAIN_SETTINGS_CATEGORY));
}

QWidget *ToolChainOptionsPage::createPage(QWidget *parent)
{
    // Actual page setup:
    m_configWidget = new QWidget(parent);

    m_currentTcWidget = 0;

    m_ui = new Ui::ToolChainOptionsPage;
    m_ui->setupUi(m_configWidget);

    m_model = new ToolChainModel(m_ui->toolChainView);
    connect(m_model, SIGNAL(toolChainStateChanged()), this, SLOT(updateState()));

    m_ui->toolChainView->setModel(m_model);
    m_ui->toolChainView->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    m_ui->toolChainView->header()->setResizeMode(1, QHeaderView::ResizeToContents);
    m_ui->toolChainView->expandAll();

    m_selectionModel = m_ui->toolChainView->selectionModel();
    connect(m_selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(toolChainSelectionChanged(QModelIndex,QModelIndex)));

    // Get toolchainfactories:
    m_factories = ExtensionSystem::PluginManager::instance()->getObjects<ToolChainFactory>();

    // Set up add menu:
    QMenu *addMenu = new QMenu(m_ui->addButton);
    QSignalMapper *mapper = new QSignalMapper(addMenu);
    connect(mapper, SIGNAL(mapped(QObject*)), this, SLOT(createToolChain(QObject*)));

    foreach (ToolChainFactory *factory, m_factories) {
        if (factory->canCreate()) {
            QAction *action = new QAction(addMenu);
            action->setText(factory->displayName());
            connect(action, SIGNAL(triggered()), mapper, SLOT(map()));
            mapper->setMapping(action, static_cast<QObject *>(factory));

            addMenu->addAction(action);
        }
    }
    m_cloneAction = new QAction(addMenu);
    m_cloneAction->setText(tr("Clone ..."));
    connect(m_cloneAction, SIGNAL(triggered()), mapper, SLOT(map()));
    mapper->setMapping(m_cloneAction, static_cast<QObject *>(0));

    if (!addMenu->isEmpty())
        addMenu->addSeparator();
    addMenu->addAction(m_cloneAction);
    m_ui->addButton->setMenu(addMenu);

    connect(m_ui->delButton, SIGNAL(clicked()), this, SLOT(removeToolChain()));

    // setup keywords:
    if (m_searchKeywords.isEmpty()) {
        QLatin1Char sep(' ');
        QTextStream stream(&m_searchKeywords);
        stream << tr("Toolchains");
        foreach (ToolChainFactory *f, m_factories)
            stream << sep << f->displayName();

        m_searchKeywords.remove(QLatin1Char('&'));
    }

    return m_configWidget;
}

void ToolChainOptionsPage::apply()
{
dt's avatar
dt committed
549 550
    if (m_model)
        m_model->apply();
551 552 553 554
}

void ToolChainOptionsPage::finish()
{
555 556
    if (m_model)
        m_model->discard();
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
}

bool ToolChainOptionsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

void ToolChainOptionsPage::toolChainSelectionChanged(const QModelIndex &current,
                                                     const QModelIndex &previous)
{
    Q_UNUSED(previous);
    if (m_currentTcWidget) {
        m_configWidget->layout()->removeWidget(m_currentTcWidget);
        m_currentTcWidget->setVisible(false);
    }

    m_currentTcWidget = m_model->widget(current);

    if (m_currentTcWidget) {
        m_configWidget->layout()->addWidget(m_currentTcWidget);
        m_currentTcWidget->setVisible(true);
    }

    updateState();
}

void ToolChainOptionsPage::createToolChain(QObject *factoryObject)
{
    ToolChain *tc = 0;

    ToolChainFactory *factory = static_cast<ToolChainFactory *>(factoryObject);
    if (!factory) {
        // Copy current item!
        ToolChain *oldTc = m_model->toolChain(m_selectionModel->currentIndex());
        Q_ASSERT(oldTc);
        tc = oldTc->clone();
    } else {
        Q_ASSERT(factory->canCreate());
        tc = factory->create();
    } if (!tc)
        return;
    m_model->markForAddition(tc);
}

void ToolChainOptionsPage::removeToolChain()
{
    ToolChain *tc = m_model->toolChain(m_selectionModel->currentIndex());
    Q_ASSERT(tc && !tc->isAutoDetected());
    m_model->markForRemoval(tc);
}

void ToolChainOptionsPage::updateState()
{

    bool canCopy = false;
    bool canDelete = false;
    ToolChain *tc = m_model->toolChain(m_selectionModel->currentIndex());
    if (tc) {
        canCopy = tc->isValid() && tc->canClone();
        canDelete = !tc->isAutoDetected();
    }

    m_cloneAction->setEnabled(canCopy);
    m_ui->delButton->setEnabled(canDelete);
}

} // namespace Internal
} // namespace ProjectExplorer