miniprojecttargetselector.cpp 53.1 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
Tobias Hunger's avatar
Tobias Hunger committed
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
Tobias Hunger's avatar
Tobias Hunger committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Tobias Hunger's avatar
Tobias Hunger committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
Tobias Hunger's avatar
Tobias Hunger committed
28 29 30
**
**************************************************************************/

31
#include "miniprojecttargetselector.h"
32
#include "target.h"
33 34 35 36 37

#include <utils/qtcassert.h>
#include <utils/styledbar.h>
#include <utils/stylehelper.h>

38
#include <coreplugin/idocument.h>
39
#include <coreplugin/icore.h>
con's avatar
con committed
40
#include <coreplugin/coreconstants.h>
41
#include <coreplugin/modemanager.h>
42 43 44 45 46

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/project.h>
#include <projectexplorer/buildconfiguration.h>
Daniel Teske's avatar
Daniel Teske committed
47
#include <projectexplorer/deployconfiguration.h>
Tobias Hunger's avatar
Tobias Hunger committed
48
#include <projectexplorer/kitmanager.h>
49
#include <projectexplorer/projectmodels.h>
Daniel Teske's avatar
Daniel Teske committed
50
#include <projectexplorer/runconfiguration.h>
51

52 53 54 55 56 57 58 59 60 61
#include <QTimer>
#include <QLayout>
#include <QLabel>
#include <QListWidget>
#include <QStatusBar>
#include <QKeyEvent>
#include <QPainter>
#include <QAction>
#include <QItemDelegate>
#include <QApplication>
62

63 64 65 66 67 68 69
static QIcon createCenteredIcon(const QIcon &icon, const QIcon &overlay)
{
    QPixmap targetPixmap;
    targetPixmap = QPixmap(Core::Constants::TARGET_ICON_SIZE, Core::Constants::TARGET_ICON_SIZE);
    targetPixmap.fill(Qt::transparent);
    QPainter painter(&targetPixmap);

70 71 72
    QPixmap pixmap = icon.pixmap(Core::Constants::TARGET_ICON_SIZE);
    painter.drawPixmap((Core::Constants::TARGET_ICON_SIZE - pixmap.width())/2,
                       (Core::Constants::TARGET_ICON_SIZE - pixmap.height())/2, pixmap);
73
    if (!overlay.isNull()) {
74 75 76
        pixmap = overlay.pixmap(Core::Constants::TARGET_ICON_SIZE);
        painter.drawPixmap((Core::Constants::TARGET_ICON_SIZE - pixmap.width())/2,
                           (Core::Constants::TARGET_ICON_SIZE - pixmap.height())/2, pixmap);
77 78 79 80
    }
    return QIcon(targetPixmap);
}

81 82 83
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

84 85 86 87 88 89 90 91 92
static bool projectLesserThan(Project *p1, Project *p2)
{
    int result = caseFriendlyCompare(p1->displayName(), p2->displayName());
    if (result != 0)
        return result < 0;
    else
        return p1 < p2;
}

Daniel Teske's avatar
Daniel Teske committed
93 94 95
////////
// TargetSelectorDelegate
////////
96 97 98
class TargetSelectorDelegate : public QItemDelegate
{
public:
99
    TargetSelectorDelegate(ListWidget *parent) : QItemDelegate(parent), m_listWidget(parent) { }
100
private:
Daniel Teske's avatar
Daniel Teske committed
101
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
102 103 104 105
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const;
    mutable QImage selectionGradient;
106
    ListWidget *m_listWidget;
107 108
};

Daniel Teske's avatar
Daniel Teske committed
109 110 111 112
QSize TargetSelectorDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option)
    Q_UNUSED(index)
113
    return QSize(m_listWidget->size().width(), 30);
Daniel Teske's avatar
Daniel Teske committed
114 115
}

116 117
void TargetSelectorDelegate::paint(QPainter *painter,
                                   const QStyleOptionViewItem &option,
Daniel Teske's avatar
Daniel Teske committed
118
                                   const QModelIndex &index) const
119 120 121 122 123 124 125 126
{
    painter->save();
    painter->setClipping(false);

    if (selectionGradient.isNull())
        selectionGradient.load(QLatin1String(":/projectexplorer/images/targetpanel_gradient.png"));

    if (option.state & QStyle::State_Selected) {
127 128 129 130
        QColor color =(option.state & QStyle::State_HasFocus) ?
                      option.palette.highlight().color() :
                      option.palette.dark().color();
        painter->fillRect(option.rect, color.darker(140));
131 132 133 134 135 136 137 138
        Utils::StyleHelper::drawCornerImage(selectionGradient, painter, option.rect.adjusted(0, 0, 0, -1), 5, 5, 5, 5);
        painter->setPen(QColor(255, 255, 255, 60));
        painter->drawLine(option.rect.topLeft(), option.rect.topRight());
        painter->setPen(QColor(255, 255, 255, 30));
        painter->drawLine(option.rect.bottomLeft() - QPoint(0,1), option.rect.bottomRight() -  QPoint(0,1));
        painter->setPen(QColor(0, 0, 0, 80));
        painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
    }
Daniel Teske's avatar
Daniel Teske committed
139 140 141 142 143 144 145 146

    QFontMetrics fm(option.font);
    QString text = index.data(Qt::DisplayRole).toString();
    painter->setPen(QColor(255, 255, 255, 160));
    QString elidedText = fm.elidedText(text, Qt::ElideMiddle, option.rect.width() - 12);
    if (elidedText != text)
        const_cast<QAbstractItemModel *>(index.model())->setData(index, text, Qt::ToolTipRole);
    else
147
        const_cast<QAbstractItemModel *>(index.model())->setData(index, QString(), Qt::ToolTipRole);
Daniel Teske's avatar
Daniel Teske committed
148 149
    painter->drawText(option.rect.left() + 6, option.rect.top() + (option.rect.height() - fm.height()) / 2 + fm.ascent(), elidedText);

150 151 152
    painter->restore();
}

Daniel Teske's avatar
Daniel Teske committed
153 154 155 156
////////
// ListWidget
////////
ListWidget::ListWidget(QWidget *parent)
157
    : QListWidget(parent), m_maxCount(0), m_optimalWidth(0)
Tobias Hunger's avatar
Tobias Hunger committed
158 159 160 161 162
{
    setFocusPolicy(Qt::NoFocus);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setAlternatingRowColors(false);
    setFocusPolicy(Qt::WheelFocus);
163
    setItemDelegate(new TargetSelectorDelegate(this));
164
    setAttribute(Qt::WA_MacShowFocusRect, false);
Daniel Teske's avatar
Daniel Teske committed
165 166
    setStyleSheet(QString::fromLatin1("QListWidget { background: #464646; border-style: none; }"));
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
Tobias Hunger's avatar
Tobias Hunger committed
167 168
}

Daniel Teske's avatar
Daniel Teske committed
169
void ListWidget::keyPressEvent(QKeyEvent *event)
Tobias Hunger's avatar
Tobias Hunger committed
170
{
Daniel Teske's avatar
Daniel Teske committed
171 172 173 174 175 176
    if (event->key() == Qt::Key_Left)
        focusPreviousChild();
    else if (event->key() == Qt::Key_Right)
        focusNextChild();
    else
        QListWidget::keyPressEvent(event);
Tobias Hunger's avatar
Tobias Hunger committed
177 178
}

Daniel Teske's avatar
Daniel Teske committed
179
void ListWidget::keyReleaseEvent(QKeyEvent *event)
Tobias Hunger's avatar
Tobias Hunger committed
180
{
181
    if (event->key() != Qt::Key_Left && event->key() != Qt::Key_Right)
Daniel Teske's avatar
Daniel Teske committed
182 183
        QListWidget::keyReleaseEvent(event);
}
Tobias Hunger's avatar
Tobias Hunger committed
184

Daniel Teske's avatar
Daniel Teske committed
185 186 187 188 189
void ListWidget::setMaxCount(int maxCount)
{
    m_maxCount = maxCount;
    updateGeometry();
}
Tobias Hunger's avatar
Tobias Hunger committed
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
int ListWidget::maxCount()
{
    return m_maxCount;
}

int ListWidget::optimalWidth() const
{
    return m_optimalWidth;
}

void ListWidget::setOptimalWidth(int width)
{
    m_optimalWidth = width;
    updateGeometry();
}

int ListWidget::padding()
{
    // there needs to be enough extra pixels to show a scrollbar
    return 30;
}

Daniel Teske's avatar
Daniel Teske committed
213 214 215 216 217 218 219 220 221 222 223 224
////////
// ProjectListWidget
////////
ProjectListWidget::ProjectListWidget(SessionManager *sessionManager, QWidget *parent)
    : ListWidget(parent), m_sessionManager(sessionManager), m_ignoreIndexChange(false)
{
    connect(m_sessionManager, SIGNAL(projectAdded(ProjectExplorer::Project*)),
            this, SLOT(addProject(ProjectExplorer::Project*)));
    connect(m_sessionManager, SIGNAL(aboutToRemoveProject(ProjectExplorer::Project*)),
            this, SLOT(removeProject(ProjectExplorer::Project*)));
    connect(m_sessionManager, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
            this, SLOT(changeStartupProject(ProjectExplorer::Project*)));
225 226
    connect(m_sessionManager, SIGNAL(projectDisplayNameChanged(ProjectExplorer::Project*)),
            this, SLOT(projectDisplayNameChanged(ProjectExplorer::Project*)));
Daniel Teske's avatar
Daniel Teske committed
227 228 229 230 231 232 233 234 235 236
    connect(this, SIGNAL(currentRowChanged(int)),
            this, SLOT(setProject(int)));
}

QListWidgetItem *ProjectListWidget::itemForProject(Project *project)
{
    for (int i = 0; i < count(); ++i) {
        QListWidgetItem *currentItem = item(i);
        if (currentItem->data(Qt::UserRole).value<Project*>() == project)
            return currentItem;
Tobias Hunger's avatar
Tobias Hunger committed
237
    }
Daniel Teske's avatar
Daniel Teske committed
238 239 240 241 242
    return 0;
}

QString ProjectListWidget::fullName(ProjectExplorer::Project *project)
{
243
    return tr("%1 (%2)").arg(project->displayName(), project->document()->fileName());
Daniel Teske's avatar
Daniel Teske committed
244
}
245

Daniel Teske's avatar
Daniel Teske committed
246 247 248
void ProjectListWidget::addProject(Project *project)
{
    m_ignoreIndexChange = true;
249

250
    int pos = count();
Daniel Teske's avatar
Daniel Teske committed
251 252
    for (int i=0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project*>();
253
        if (projectLesserThan(project, p)) {
Daniel Teske's avatar
Daniel Teske committed
254
            pos = i;
255 256
            break;
        }
Daniel Teske's avatar
Daniel Teske committed
257
    }
258

Daniel Teske's avatar
Daniel Teske committed
259 260 261 262 263 264 265 266
    bool useFullName = false;
    for (int i = 0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project*>();
        if (p->displayName() == project->displayName()) {
            useFullName = true;
            item(i)->setText(fullName(p));
        }
    }
267

Daniel Teske's avatar
Daniel Teske committed
268 269 270 271 272
    QString displayName = useFullName ? fullName(project) : project->displayName();
    QListWidgetItem *item = new QListWidgetItem();
    item->setData(Qt::UserRole, QVariant::fromValue(project));
    item->setText(displayName);
    insertItem(pos, item);
Tobias Hunger's avatar
Tobias Hunger committed
273

Daniel Teske's avatar
Daniel Teske committed
274 275
    if (project == ProjectExplorerPlugin::instance()->startupProject()) {
        setCurrentItem(item);
Tobias Hunger's avatar
Tobias Hunger committed
276
    }
277

278 279 280 281 282
    QFontMetrics fn(font());
    int width = fn.width(project->displayName()) + padding();
    if (width > optimalWidth())
        setOptimalWidth(width);

Daniel Teske's avatar
Daniel Teske committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    m_ignoreIndexChange = false;
}

void ProjectListWidget::removeProject(Project *project)
{
    m_ignoreIndexChange = true;

    QListWidgetItem *listItem = itemForProject(project);
    delete listItem;

    // Update display names
    QString name = project->displayName();
    int countDisplayName = 0;
    int otherIndex = -1;
    for (int i = 0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project *>();
        if (p->displayName() == name) {
            ++countDisplayName;
            otherIndex = i;
        }
    }
    if (countDisplayName == 1) {
        Project *p = item(otherIndex)->data(Qt::UserRole).value<Project *>();
        item(otherIndex)->setText(p->displayName());
Tobias Hunger's avatar
Tobias Hunger committed
307
    }
308

309 310 311 312 313 314 315 316 317
    QFontMetrics fn(font());

    // recheck optimal width
    int width = 0;
    for (int i = 0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project *>();
        width = qMax(fn.width(p->displayName()) + padding(), width);
    }
    setOptimalWidth(width);
Daniel Teske's avatar
Daniel Teske committed
318

319
    m_ignoreIndexChange = false;
320 321
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
void ProjectListWidget::projectDisplayNameChanged(Project *project)
{
    m_ignoreIndexChange = true;

    int oldPos = 0;
    bool useFullName = false;
    for (int i = 0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project*>();
        if (p == project) {
            oldPos = i;
        } else if (p->displayName() == project->displayName()) {
            useFullName = true;
            item(i)->setText(fullName(p));
        }
    }

    bool isCurrentItem = (oldPos == currentRow());
    QListWidgetItem *projectItem = takeItem(oldPos);

    int pos = count();
    for (int i = 0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project*>();
344
        if (projectLesserThan(project, p)) {
345 346 347 348 349 350 351 352 353 354 355
            pos = i;
            break;
        }
    }

    QString displayName = useFullName ? fullName(project) : project->displayName();
    projectItem->setText(displayName);
    insertItem(pos, projectItem);
    if (isCurrentItem)
        setCurrentRow(pos);

356 357 358 359 360 361 362 363 364
    // recheck optimal width
    QFontMetrics fn(font());
    int width = 0;
    for (int i = 0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project *>();
        width = qMax(fn.width(p->displayName()) + padding(), width);
    }
    setOptimalWidth(width);

365 366 367
    m_ignoreIndexChange = false;
}

Daniel Teske's avatar
Daniel Teske committed
368
void ProjectListWidget::setProject(int index)
con's avatar
con committed
369
{
Daniel Teske's avatar
Daniel Teske committed
370 371 372 373 374 375
    if (m_ignoreIndexChange)
        return;
    if (index < 0)
        return;
    Project *p = item(index)->data(Qt::UserRole).value<Project *>();
    m_sessionManager->setStartupProject(p);
con's avatar
con committed
376 377
}

Daniel Teske's avatar
Daniel Teske committed
378
void ProjectListWidget::changeStartupProject(Project *project)
Tobias Hunger's avatar
Tobias Hunger committed
379
{
Daniel Teske's avatar
Daniel Teske committed
380
    setCurrentItem(itemForProject(project));
Tobias Hunger's avatar
Tobias Hunger committed
381 382
}

Daniel Teske's avatar
Daniel Teske committed
383 384 385 386 387 388
/////////
// GenericListWidget
/////////

GenericListWidget::GenericListWidget(QWidget *parent)
    : ListWidget(parent), m_ignoreIndexChange(false)
389
{
Daniel Teske's avatar
Daniel Teske committed
390 391
    connect(this, SIGNAL(currentRowChanged(int)),
            this, SLOT(rowChanged(int)));
392 393
}

Daniel Teske's avatar
Daniel Teske committed
394
void GenericListWidget::setProjectConfigurations(const QList<ProjectConfiguration *> &list, ProjectConfiguration *active)
395
{
Daniel Teske's avatar
Daniel Teske committed
396 397 398 399 400 401 402
    m_ignoreIndexChange = true;
    clear();
    for (int i = 0; i < count(); ++i) {
        ProjectConfiguration *p = item(i)->data(Qt::UserRole).value<ProjectConfiguration *>();
        disconnect(p, SIGNAL(displayNameChanged()),
                   this, SLOT(displayNameChanged()));
    }
403 404 405 406

    QFontMetrics fn(font());
    int width = 0;
    foreach (ProjectConfiguration *pc, list) {
Daniel Teske's avatar
Daniel Teske committed
407
        addProjectConfiguration(pc);
408 409 410
        width = qMax(width, fn.width(pc->displayName()) + padding());
    }
    setOptimalWidth(width);
Daniel Teske's avatar
Daniel Teske committed
411
    setActiveProjectConfiguration(active);
412

Daniel Teske's avatar
Daniel Teske committed
413
    m_ignoreIndexChange = false;
414
}
dt's avatar
dt committed
415

Daniel Teske's avatar
Daniel Teske committed
416
void GenericListWidget::setActiveProjectConfiguration(ProjectConfiguration *active)
417
{
Daniel Teske's avatar
Daniel Teske committed
418 419
    QListWidgetItem *item = itemForProjectConfiguration(active);
    setCurrentItem(item);
420 421
}

Daniel Teske's avatar
Daniel Teske committed
422
void GenericListWidget::addProjectConfiguration(ProjectExplorer::ProjectConfiguration *pc)
423
{
Daniel Teske's avatar
Daniel Teske committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    m_ignoreIndexChange = true;
    QListWidgetItem *lwi = new QListWidgetItem();
    lwi->setText(pc->displayName());
    lwi->setData(Qt::UserRole, QVariant::fromValue(pc));

    // Figure out pos
    int pos = count();
    for (int i = 0; i < count(); ++i) {
        ProjectConfiguration *p = item(i)->data(Qt::UserRole).value<ProjectConfiguration *>();
        if (pc->displayName() < p->displayName()) {
            pos = i;
            break;
        }
    }
    insertItem(pos, lwi);

    connect(pc, SIGNAL(displayNameChanged()),
            this, SLOT(displayNameChanged()));
442 443 444 445 446

    QFontMetrics fn(font());
    int width = fn.width(pc->displayName()) + padding();
    if (width > optimalWidth())
        setOptimalWidth(width);
Daniel Teske's avatar
Daniel Teske committed
447
    m_ignoreIndexChange = false;
448 449
}

Daniel Teske's avatar
Daniel Teske committed
450
void GenericListWidget::removeProjectConfiguration(ProjectExplorer::ProjectConfiguration *pc)
451
{
Daniel Teske's avatar
Daniel Teske committed
452 453 454 455
    m_ignoreIndexChange = true;
    disconnect(pc, SIGNAL(displayNameChanged()),
               this, SLOT(displayNameChanged()));
    delete itemForProjectConfiguration(pc);
456 457 458 459 460 461 462 463 464

    QFontMetrics fn(font());
    int width = 0;
    for (int i = 0; i < count(); ++i) {
        ProjectConfiguration *p = item(i)->data(Qt::UserRole).value<ProjectConfiguration *>();
        width = qMax(width, fn.width(p->displayName()) + padding());
    }
    setOptimalWidth(width);

Daniel Teske's avatar
Daniel Teske committed
465
    m_ignoreIndexChange = false;
466 467
}

Daniel Teske's avatar
Daniel Teske committed
468
void GenericListWidget::rowChanged(int index)
469
{
Daniel Teske's avatar
Daniel Teske committed
470 471 472 473 474
    if (m_ignoreIndexChange)
        return;
    if (index < 0)
        return;
    emit changeActiveProjectConfiguration(item(index)->data(Qt::UserRole).value<ProjectConfiguration *>());
475 476
}

Daniel Teske's avatar
Daniel Teske committed
477
void GenericListWidget::displayNameChanged()
478
{
Daniel Teske's avatar
Daniel Teske committed
479
    m_ignoreIndexChange = true;
480 481 482 483
    ProjectConfiguration *activeProjectConfiguration = 0;
    if (currentItem())
        activeProjectConfiguration = currentItem()->data(Qt::UserRole).value<ProjectConfiguration *>();

Daniel Teske's avatar
Daniel Teske committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    ProjectConfiguration *pc = qobject_cast<ProjectConfiguration *>(sender());
    int index = -1;
    int i = 0;
    for (; i < count(); ++i) {
        QListWidgetItem *lwi = item(i);
        if (lwi->data(Qt::UserRole).value<ProjectConfiguration *>() == pc) {
            index = i;
            break;
        }
    }
    if (index == -1)
        return;
    QListWidgetItem *lwi = takeItem(i);
    lwi->setText(pc->displayName());
    int pos = count();
    for (int i = 0; i < count(); ++i) {
        ProjectConfiguration *p = item(i)->data(Qt::UserRole).value<ProjectConfiguration *>();
        if (pc->displayName() < p->displayName()) {
            pos = i;
            break;
        }
    }
    insertItem(pos, lwi);
507 508
    if (activeProjectConfiguration)
        setCurrentItem(itemForProjectConfiguration(activeProjectConfiguration));
509 510 511 512 513 514 515 516 517

    QFontMetrics fn(font());
    int width = 0;
    for (int i = 0; i < count(); ++i) {
        ProjectConfiguration *p = item(i)->data(Qt::UserRole).value<ProjectConfiguration *>();
        width = qMax(width, fn.width(p->displayName()) + padding());
    }
    setOptimalWidth(width);

Daniel Teske's avatar
Daniel Teske committed
518
    m_ignoreIndexChange = false;
519 520
}

Daniel Teske's avatar
Daniel Teske committed
521
QListWidgetItem *GenericListWidget::itemForProjectConfiguration(ProjectConfiguration *pc)
522
{
Daniel Teske's avatar
Daniel Teske committed
523 524 525 526 527 528 529
    for (int i = 0; i < count(); ++i) {
        QListWidgetItem *lwi = item(i);
        if (lwi->data(Qt::UserRole).value<ProjectConfiguration *>() == pc) {
            return lwi;
        }
    }
    return 0;
530 531
}

532
QWidget *MiniProjectTargetSelector::createTitleLabel(const QString &text)
Tobias Hunger's avatar
Tobias Hunger committed
533
{
534
    Utils::StyledBar *bar = new Utils::StyledBar(this);
Daniel Teske's avatar
Daniel Teske committed
535 536
    bar->setSingleRow(true);
    QVBoxLayout *toolLayout = new QVBoxLayout(bar);
537
    toolLayout->setContentsMargins(6, 0, 6, 0);
Daniel Teske's avatar
Daniel Teske committed
538 539 540 541 542 543 544 545 546 547 548 549
    toolLayout->setSpacing(0);

    QLabel *l = new QLabel(text);
    QFont f = l->font();
    f.setBold(true);
    l->setFont(f);
    toolLayout->addWidget(l);

    int panelHeight = l->fontMetrics().height() + 12;
    bar->ensurePolished(); // Required since manhattanstyle overrides height
    bar->setFixedHeight(panelHeight);
    return bar;
Tobias Hunger's avatar
Tobias Hunger committed
550 551
}

Daniel Teske's avatar
Daniel Teske committed
552 553 554 555 556 557 558 559 560 561 562 563
MiniProjectTargetSelector::MiniProjectTargetSelector(QAction *targetSelectorAction, SessionManager *sessionManager, QWidget *parent) :
    QWidget(parent), m_projectAction(targetSelectorAction), m_sessionManager(sessionManager),
    m_project(0),
    m_target(0),
    m_buildConfiguration(0),
    m_deployConfiguration(0),
    m_runConfiguration(0),
    m_hideOnRelease(false)
{
    QPalette p = palette();
    p.setColor(QPalette::Text, QColor(255, 255, 255, 160));
    setPalette(p);
564 565
    setProperty("panelwidget", true);
    setContentsMargins(QMargins(0, 1, 1, 8));
566 567 568 569 570
    setWindowFlags(Qt::Popup);

    targetSelectorAction->setIcon(style()->standardIcon(QStyle::SP_ComputerIcon));
    targetSelectorAction->setProperty("titledAction", true);

Daniel Teske's avatar
Daniel Teske committed
571 572 573 574
    m_summaryLabel = new QLabel(this);
    m_summaryLabel->setMargin(3);
    m_summaryLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    m_summaryLabel->setStyleSheet(QString::fromLatin1("background: #464646;"));
575 576
    m_summaryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    m_summaryLabel->setTextInteractionFlags(m_summaryLabel->textInteractionFlags() | Qt::LinksAccessibleByMouse);
Daniel Teske's avatar
Daniel Teske committed
577 578 579 580 581 582 583 584 585

    m_listWidgets.resize(LAST);
    m_titleWidgets.resize(LAST);
    m_listWidgets[PROJECT] = 0; //project is not a generic list widget

    m_titleWidgets[PROJECT] = createTitleLabel(tr("Project"));
    m_projectListWidget = new ProjectListWidget(m_sessionManager, this);

    QStringList titles;
Tobias Hunger's avatar
Tobias Hunger committed
586
    titles << tr("Kit") << tr("Build")
Daniel Teske's avatar
Daniel Teske committed
587 588 589 590 591 592 593 594 595 596 597
           << tr("Deploy") << tr("Run");

    for (int i = TARGET; i < LAST; ++i) {
        m_titleWidgets[i] = createTitleLabel(titles.at(i -1));
        m_listWidgets[i] = new GenericListWidget(this);
    }

    changeStartupProject(m_sessionManager->startupProject());
    if (m_sessionManager->startupProject())
        activeTargetChanged(m_sessionManager->startupProject()->activeTarget());

598 599 600
    connect(m_summaryLabel, SIGNAL(linkActivated(QString)),
            this, SLOT(switchToProjectsMode()));

Daniel Teske's avatar
Daniel Teske committed
601 602 603 604 605 606 607
    connect(m_sessionManager, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
            this, SLOT(changeStartupProject(ProjectExplorer::Project*)));

    connect(m_sessionManager, SIGNAL(projectAdded(ProjectExplorer::Project*)),
            this, SLOT(projectAdded(ProjectExplorer::Project*)));
    connect(m_sessionManager, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
            this, SLOT(projectRemoved(ProjectExplorer::Project*)));
608 609
    connect(m_sessionManager, SIGNAL(projectDisplayNameChanged(ProjectExplorer::Project*)),
            this, SLOT(updateActionAndSummary()));
Daniel Teske's avatar
Daniel Teske committed
610

Tobias Hunger's avatar
Tobias Hunger committed
611
    // for icon changes:
Tobias Hunger's avatar
Tobias Hunger committed
612 613
    connect(ProjectExplorer::KitManager::instance(), SIGNAL(kitUpdated(ProjectExplorer::Kit*)),
            this, SLOT(kitChanged(ProjectExplorer::Kit*)));
Tobias Hunger's avatar
Tobias Hunger committed
614

Daniel Teske's avatar
Daniel Teske committed
615
    connect(m_listWidgets[TARGET], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
Robert Loehning's avatar
Robert Loehning committed
616
            this, SLOT(setActiveTarget(ProjectExplorer::ProjectConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
617
    connect(m_listWidgets[BUILD], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
Robert Loehning's avatar
Robert Loehning committed
618
            this, SLOT(setActiveBuildConfiguration(ProjectExplorer::ProjectConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
619
    connect(m_listWidgets[DEPLOY], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
Robert Loehning's avatar
Robert Loehning committed
620
            this, SLOT(setActiveDeployConfiguration(ProjectExplorer::ProjectConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
621
    connect(m_listWidgets[RUN], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
Robert Loehning's avatar
Robert Loehning committed
622
            this, SLOT(setActiveRunConfiguration(ProjectExplorer::ProjectConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
623
}
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
bool MiniProjectTargetSelector::event(QEvent *event)
{
    if (event->type() != QEvent::LayoutRequest)
        return QWidget::event(event);
    doLayout(true);
    return true;
}

class IndexSorter
{
public:
    enum SortOrder { Less = 0, Greater = 1};

    IndexSorter(QVector<int> result, SortOrder order)
        : m_result(result), m_order(order)
    { }

    bool operator()(int i, int j)
    { return (m_result[i] < m_result[j]) ^ bool(m_order); }

private:
    QVector<int> m_result;
    SortOrder m_order;
};

// does some fancy calculations to ensure proper widths for the list widgets
QVector<int> MiniProjectTargetSelector::listWidgetWidths(int minSize, int maxSize)
{
    QVector<int> result;
    result.resize(LAST);
    if (m_projectListWidget->isVisibleTo(this))
        result[PROJECT] = m_projectListWidget->optimalWidth();
    else
        result[PROJECT] = -1;

    for (int i = TARGET; i < LAST; ++i) {
        if (m_listWidgets[i]->isVisibleTo(this))
            result[i] = m_listWidgets[i]->optimalWidth();
        else
            result[i] = -1;
    }

    int totalWidth = 0;
    // Adjust to minimum width of title
    for (int i = PROJECT; i < LAST; ++i) {
        if (result[i] != -1) {
            // We want at least 100 pixels per column
            int width = qMax(m_titleWidgets[i]->sizeHint().width(), 100);
            if (result[i] < width)
                result[i] = width;
            totalWidth += result[i];
        }
    }

    if (totalWidth == 0) // All hidden
        return result;

    bool tooSmall;
    if (totalWidth < minSize)
        tooSmall = true;
    else if (totalWidth > maxSize)
        tooSmall = false;
    else
        return result;

    int widthToDistribute = tooSmall ? (minSize - totalWidth)
                                     : (totalWidth - maxSize);
    QVector<int> indexes;
    indexes.reserve(LAST);
    for (int i = PROJECT; i < LAST; ++i)
        if (result[i] != -1)
            indexes.append(i);

    IndexSorter indexSorter(result, tooSmall ? IndexSorter::Less : IndexSorter::Greater);
    qSort(indexes.begin(), indexes.end(), indexSorter);

    int i = 0;
    int first = result[indexes.first()]; // biggest or smallest

    // we resize the biggest columns until they are the same size as the second biggest
    // since it looks prettiest if all the columns are the same width
    while (true) {
        for (; i < indexes.size(); ++i) {
            if (result[indexes[i]] != first)
                break;
        }
        int next = tooSmall ? INT_MAX : 0;
        if (i < indexes.size())
            next = result[indexes[i]];

        int delta;
        if (tooSmall)
            delta = qMin(next - first, widthToDistribute / i);
        else
            delta = qMin(first - next, widthToDistribute / i);

        if (delta == 0)
            return result;

        if (tooSmall) {
            for (int j = 0; j < i; ++j)
                result[indexes[j]] += delta;
        } else {
            for (int j = 0; j < i; ++j)
                result[indexes[j]] -= delta;
        }

        widthToDistribute -= delta * i;
        if (widthToDistribute == 0)
            return result;

        first = result[indexes.first()];
        i = 0; // TODO can we do better?
    }
}

void MiniProjectTargetSelector::doLayout(bool keepSize)
{
    // An unconfigured project shows empty build/deploy/run sections
    // if there's a configured project in the seesion
    // that could be improved
    static QStatusBar *statusBar = Core::ICore::statusBar();
    static QWidget *actionBar = Core::ICore::mainWindow()->findChild<QWidget*>(QLatin1String("actionbar"));
    Q_ASSERT(actionBar);

    // 1. Calculate the summary label height
    int summaryLabelY = 1;
    int summaryLabelHeight = 0;
    int oldSummaryLabelHeight = m_summaryLabel->height();
    bool onlySummary = false;
    // Count the number of lines
    int visibleLineCount = m_projectListWidget->isVisibleTo(this) ? 0 : 1;
    for (int i = TARGET; i < LAST; ++i)
        visibleLineCount += m_listWidgets[i]->isVisibleTo(this) ? 0 : 1;

    if (visibleLineCount == LAST) {
        summaryLabelHeight = visibleLineCount * QFontMetrics(m_summaryLabel->font()).height()
                + m_summaryLabel->margin() *2;
        onlySummary = true;
    } else {
        if (visibleLineCount < 3) {
            foreach (Project *p, m_sessionManager->projects()) {
                if (p->needsConfiguration()) {
                    visibleLineCount = 3;
                    break;
                }
            }
        }
        if (visibleLineCount)
            summaryLabelHeight = visibleLineCount * QFontMetrics(m_summaryLabel->font()).height()
                    + m_summaryLabel->margin() *2;
    }

    if (keepSize && oldSummaryLabelHeight > summaryLabelHeight)
        summaryLabelHeight = oldSummaryLabelHeight;

    m_summaryLabel->move(0, summaryLabelY);

    // Height to be aligned with side bar button
    int alignedWithActionHeight = actionBar->height() - statusBar->height();
    int bottomMargin = 9;
    int totalHeight = 0;

    if (!onlySummary) {
        // list widget heigth
        int maxItemCount = m_projectListWidget->maxCount();
        for (int i = TARGET; i < LAST; ++i)
            maxItemCount = qMax(maxItemCount, m_listWidgets[i]->maxCount());

        int titleWidgetsHeight = m_titleWidgets.first()->height();
        if (keepSize) {
            totalHeight = height();
        } else {
            // Clamp the size of the listwidgets to be
            // at least as high as the the sidebar button
            // and at most twice as high
            totalHeight = summaryLabelHeight + qBound(alignedWithActionHeight,
                                                      maxItemCount * 30 + bottomMargin + titleWidgetsHeight,
                                                      alignedWithActionHeight * 2);
        }

        int titleY = summaryLabelY + summaryLabelHeight;
        int listY = titleY + titleWidgetsHeight;
        int listHeight = totalHeight - bottomMargin - listY + 1;

        // list widget widths
        int minWidth = qMax(m_summaryLabel->sizeHint().width(), 250);
        if (keepSize) {
            // Do not make the widget smaller then it was before
            int oldTotalListWidgetWidth = m_projectListWidget->isVisibleTo(this) ?
                    m_projectListWidget->width() : 0;
            for (int i = TARGET; i < LAST; ++i)
                oldTotalListWidgetWidth += m_listWidgets[i]->width();
            minWidth = qMax(minWidth, oldTotalListWidgetWidth);
        }

        QVector<int> widths = listWidgetWidths(minWidth, 1000);
        int x = 0;
        for (int i = PROJECT; i < LAST; ++i) {
            int optimalWidth = widths[i];
            if (i == PROJECT) {
                m_projectListWidget->resize(optimalWidth, listHeight);
                m_projectListWidget->move(x, listY);
            } else {
                m_listWidgets[i]->resize(optimalWidth, listHeight);
                m_listWidgets[i]->move(x, listY);
            }
            m_titleWidgets[i]->resize(optimalWidth, titleWidgetsHeight);
            m_titleWidgets[i]->move(x, titleY);
            x += optimalWidth + 1; //1 extra pixel for the separators or the right border
        }

        m_summaryLabel->resize(x - 1, summaryLabelHeight);
        setFixedSize(x, totalHeight);
    } else {
        if (keepSize)
            totalHeight = height();
        else
            totalHeight = qMax(summaryLabelHeight + bottomMargin, alignedWithActionHeight);
        m_summaryLabel->resize(m_summaryLabel->sizeHint().width(), totalHeight - bottomMargin);
        setFixedSize(m_summaryLabel->width() + 1, totalHeight); //1 extra pixel for the border
    }

    if (isVisibleTo(parentWidget())) {
        QPoint moveTo = statusBar->mapToGlobal(QPoint(0,0));
        moveTo -= QPoint(0, totalHeight);
        move(moveTo);
    }
}

Daniel Teske's avatar
Daniel Teske committed
855 856 857 858
void MiniProjectTargetSelector::setActiveTarget(ProjectExplorer::ProjectConfiguration *pc)
{
    m_project->setActiveTarget(static_cast<Target *>(pc));
}
859

Daniel Teske's avatar
Daniel Teske committed
860 861 862 863
void MiniProjectTargetSelector::setActiveBuildConfiguration(ProjectExplorer::ProjectConfiguration *pc)
{
    m_target->setActiveBuildConfiguration(static_cast<BuildConfiguration *>(pc));
}
864

Daniel Teske's avatar
Daniel Teske committed
865 866 867 868 869 870 871 872 873
void MiniProjectTargetSelector::setActiveDeployConfiguration(ProjectExplorer::ProjectConfiguration *pc)
{
    m_target->setActiveDeployConfiguration(static_cast<DeployConfiguration *>(pc));
}

void MiniProjectTargetSelector::setActiveRunConfiguration(ProjectExplorer::ProjectConfiguration *pc)
{
    m_target->setActiveRunConfiguration(static_cast<RunConfiguration *>(pc));
}
874

Daniel Teske's avatar
Daniel Teske committed
875 876 877
void MiniProjectTargetSelector::projectAdded(ProjectExplorer::Project *project)
{
    connect(project, SIGNAL(addedTarget(ProjectExplorer::Target*)),
878
            this, SLOT(slotAddedTarget(ProjectExplorer::Target*)));
879

Daniel Teske's avatar
Daniel Teske committed
880
    connect(project, SIGNAL(removedTarget(ProjectExplorer::Target*)),
881
            this, SLOT(slotRemovedTarget(ProjectExplorer::Target*)));
882

Daniel Teske's avatar
Daniel Teske committed
883 884
    foreach (Target *t, project->targets())
        addedTarget(t);
885

Daniel Teske's avatar
Daniel Teske committed
886 887 888 889 890
    updateProjectListVisible();
    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
891 892
}

Daniel Teske's avatar
Daniel Teske committed
893
void MiniProjectTargetSelector::projectRemoved(ProjectExplorer::Project *project)
894
{
Daniel Teske's avatar
Daniel Teske committed
895
    disconnect(project, SIGNAL(addedTarget(ProjectExplorer::Target*)),
896
               this, SLOT(slotAddedTarget(ProjectExplorer::Target*)));
897

Daniel Teske's avatar
Daniel Teske committed
898
    disconnect(project, SIGNAL(removedTarget(ProjectExplorer::Target*)),
899
               this, SLOT(slotRemovedTarget(ProjectExplorer::Target*)));
900

Daniel Teske's avatar
Daniel Teske committed
901 902
    foreach (Target *t, project->targets())
        removedTarget(t);
903

Daniel Teske's avatar
Daniel Teske committed
904 905 906 907 908
    updateProjectListVisible();
    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
909 910
}

Daniel Teske's avatar
Daniel Teske committed
911
void MiniProjectTargetSelector::addedTarget(ProjectExplorer::Target *target)
912
{
Daniel Teske's avatar
Daniel Teske committed
913
    connect(target, SIGNAL(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
914
            this, SLOT(slotAddedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
915
    connect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
916
            this, SLOT(slotRemovedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
917 918

    connect(target, SIGNAL(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
919
            this, SLOT(slotAddedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
920
    connect(target, SIGNAL(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
921
            this, SLOT(slotRemovedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
922 923

    connect(target, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
924
            this, SLOT(slotAddedRunConfiguration(ProjectExplorer::RunConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
925
    connect(target, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
926
            this, SLOT(slotRemovedRunConfiguration(ProjectExplorer::RunConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
927 928 929 930 931 932 933 934 935 936

    if (target->project() == m_project)
        m_listWidgets[TARGET]->addProjectConfiguration(target);

    foreach (BuildConfiguration *bc, target->buildConfigurations())
        addedBuildConfiguration(bc);
    foreach (DeployConfiguration *dc, target->deployConfigurations())
        addedDeployConfiguration(dc);
    foreach (RunConfiguration *rc, target->runConfigurations())
        addedRunConfiguration(rc);
937
}
Daniel Teske's avatar
Daniel Teske committed
938

939 940 941
void MiniProjectTargetSelector::slotAddedTarget(ProjectExplorer::Target *target)
{
    addedTarget(target);
Daniel Teske's avatar
Daniel Teske committed
942 943 944 945
    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
946 947
}

Daniel Teske's avatar
Daniel Teske committed
948
void MiniProjectTargetSelector::removedTarget(ProjectExplorer::Target *target)
949
{
Daniel Teske's avatar
Daniel Teske committed
950
    disconnect(target, SIGNAL(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
951
               this, SLOT(slotAddedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
952
    disconnect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
953
               this, SLOT(slotRemovedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
954 955

    disconnect(target, SIGNAL(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
956
               this, SLOT(slotAddedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
957
    disconnect(target, SIGNAL(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
958
               this, SLOT(slotRemovedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
959 960

    disconnect(target, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
961
               this, SLOT(slotAddedRunConfiguration(ProjectExplorer::RunConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
962
    disconnect(target, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
963
               this, SLOT(slotRemovedRunConfiguration(ProjectExplorer::RunConfiguration*)));
Daniel Teske's avatar
Daniel Teske committed
964 965 966 967 968 969 970 971 972 973

    if (target->project() == m_project)
        m_listWidgets[TARGET]->removeProjectConfiguration(target);

    foreach (BuildConfiguration *bc, target->buildConfigurations())
        removedBuildConfiguration(bc);
    foreach (DeployConfiguration *dc, target->deployConfigurations())
        removedDeployConfiguration(dc);
    foreach (RunConfiguration *rc, target->runConfigurations())
        removedRunConfiguration(rc);
974 975 976 977 978
}

void MiniProjectTargetSelector::slotRemovedTarget(ProjectExplorer::Target *target)
{
    removedTarget(target);
Daniel Teske's avatar
Daniel Teske committed
979 980 981 982 983

    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
984 985
}

986

Daniel Teske's avatar
Daniel Teske committed
987
void MiniProjectTargetSelector::addedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
988 989 990 991 992 993
{
    if (bc->target() == m_target)
        m_listWidgets[BUILD]->addProjectConfiguration(bc);
}

void MiniProjectTargetSelector::slotAddedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
994
{
Daniel Teske's avatar
Daniel Teske committed
995 996 997 998
    if (bc->target() == m_target)
        m_listWidgets[BUILD]->addProjectConfiguration(bc);
    updateBuildListVisible();
}
999

Daniel Teske's avatar
Daniel Teske committed
1000
void MiniProjectTargetSelector::removedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
1001 1002 1003 1004 1005 1006
{
    if (bc->target() == m_target)
        m_listWidgets[BUILD]->removeProjectConfiguration(bc);
}

void MiniProjectTargetSelector::slotRemovedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
Daniel Teske's avatar
Daniel Teske committed
1007 1008 1009 1010 1011
{
    if (bc->target() == m_target)
        m_listWidgets[BUILD]->removeProjectConfiguration(bc);
    updateBuildListVisible();
}
dt's avatar
dt committed
1012

Daniel Teske's avatar
Daniel Teske committed
1013
void MiniProjectTargetSelector::addedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
1014 1015 1016 1017 1018 1019
{
    if (dc->target() == m_target)
        m_listWidgets[DEPLOY]->addProjectConfiguration(dc);
}

void MiniProjectTargetSelector::slotAddedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
Daniel Teske's avatar
Daniel Teske committed
1020 1021 1022 1023 1024
{
    if (dc->target() == m_target)
        m_listWidgets[DEPLOY]->addProjectConfiguration(dc);
    updateDeployListVisible();
}
dt's avatar
dt committed
1025

Daniel Teske's avatar
Daniel Teske committed
1026
void MiniProjectTargetSelector::removedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
1027 1028 1029 1030 1031 1032
{
    if (dc->target() == m_target)
        m_listWidgets[DEPLOY]->removeProjectConfiguration(dc);
}

void MiniProjectTargetSelector::slotRemovedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
Daniel Teske's avatar
Daniel Teske committed
1033 1034 1035 1036 1037
{
    if (dc->target() == m_target)
        m_listWidgets[DEPLOY]->removeProjectConfiguration(dc);
    updateDeployListVisible();
}
dt's avatar
dt committed
1038

Daniel Teske's avatar
Daniel Teske committed
1039
void MiniProjectTargetSelector::addedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
1040 1041 1042 1043 1044 1045
{
    if (rc->target() == m_target)
        m_listWidgets[RUN]->addProjectConfiguration(rc);
}

void MiniProjectTargetSelector::slotAddedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
Daniel Teske's avatar
Daniel Teske committed
1046 1047 1048 1049 1050
{
    if (rc->target() == m_target)
        m_listWidgets[RUN]->addProjectConfiguration(rc);
    updateRunListVisible();
}
1051

Daniel Teske's avatar
Daniel Teske committed
1052
void MiniProjectTargetSelector::removedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
1053 1054 1055 1056 1057 1058
{
    if (rc->target() == m_target)
        m_listWidgets[RUN]->removeProjectConfiguration(rc);
}

void MiniProjectTargetSelector::slotRemovedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
Daniel Teske's avatar
Daniel Teske committed
1059 1060 1061 1062 1063
{
    if (rc->target() == m_target)
        m_listWidgets[RUN]->removeProjectConfiguration(rc);
    updateRunListVisible();
}
1064

Daniel Teske's avatar
Daniel Teske committed
1065 1066 1067
void MiniProjectTargetSelector::updateProjectListVisible()
{
    bool visible = m_sessionManager->projects().size() > 1;
1068

Daniel Teske's avatar
Daniel Teske committed
1069 1070 1071
    m_projectListWidget->setVisible(visible);
    m_projectListWidget->setMaxCount(m_sessionManager->projects().size());
    m_titleWidgets[PROJECT]->setVisible(visible);
1072

Daniel Teske's avatar
Daniel Teske committed
1073 1074
    updateSummary();
}
Tobias Hunger's avatar
Tobias Hunger committed
1075

Daniel Teske's avatar
Daniel Teske committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
void MiniProjectTargetSelector::updateTargetListVisible()
{
    int maxCount = 0;
    foreach (Project *p, m_sessionManager->projects())
        maxCount = qMax(p->targets().size(), maxCount);

    bool visible = maxCount > 1;
    m_listWidgets[TARGET]->setVisible(visible);
    m_listWidgets[TARGET]->setMaxCount(maxCount);
    m_titleWidgets[TARGET]->setVisible(visible);
    updateSummary();
}
dt's avatar
dt committed
1088

Daniel Teske's avatar
Daniel Teske committed
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
void MiniProjectTargetSelector::updateBuildListVisible()
{
    int maxCount = 0;
    foreach (Project *p, m_sessionManager->projects())
        foreach (Target *t, p->targets())
            maxCount = qMax(t->buildConfigurations().size(), maxCount);

    bool visible = maxCount > 1;
    m_listWidgets[BUILD]->setVisible(visible);
    m_listWidgets[BUILD]->setMaxCount(maxCount);
    m_titleWidgets[BUILD]->setVisible(visible);
    updateSummary();
}
1102

Daniel Teske's avatar
Daniel Teske committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
void MiniProjectTargetSelector::updateDeployListVisible()
{
    int maxCount = 0;
    foreach (Project *p, m_sessionManager->projects())
        foreach (Target *t, p->targets())
            maxCount = qMax(t->deployConfigurations().size(), maxCount);

    bool visible = maxCount > 1;
    m_listWidgets[DEPLOY]->setVisible(visible);
    m_listWidgets[DEPLOY]->setMaxCount(maxCount);
    m_titleWidgets[DEPLOY]->setVisible(visible);
    updateSummary();