miniprojecttargetselector.cpp 43.2 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
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
Tobias Hunger's avatar
Tobias Hunger committed
30 31 32
**
**************************************************************************/

33
#include "miniprojecttargetselector.h"
34
#include "target.h"
35 36 37 38 39

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

40
#include <coreplugin/ifile.h>
41
#include <coreplugin/icore.h>
con's avatar
con committed
42
#include <coreplugin/coreconstants.h>
43
#include <coreplugin/modemanager.h>
44 45 46 47 48

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/project.h>
#include <projectexplorer/buildconfiguration.h>
Daniel Teske's avatar
Daniel Teske committed
49 50
#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/runconfiguration.h>
51

Daniel Teske's avatar
Daniel Teske committed
52
#include <QtCore/QTimer>
53 54 55 56
#include <QtGui/QLayout>
#include <QtGui/QLabel>
#include <QtGui/QListWidget>
#include <QtGui/QStatusBar>
Tobias Hunger's avatar
Tobias Hunger committed
57
#include <QtGui/QKeyEvent>
con's avatar
con committed
58
#include <QtGui/QPainter>
59
#include <QtGui/QAction>
60
#include <QtGui/QItemDelegate>
61
#include <QtGui/QMainWindow>
Tobias Hunger's avatar
Tobias Hunger committed
62
#include <QtGui/QApplication>
63

64 65 66 67 68 69 70
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);

71 72 73
    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);
74
    if (!overlay.isNull()) {
75 76 77
        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);
78 79 80 81
    }
    return QIcon(targetPixmap);
}

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

Daniel Teske's avatar
Daniel Teske committed
85 86 87
////////
// TargetSelectorDelegate
////////
88 89 90 91 92
class TargetSelectorDelegate : public QItemDelegate
{
public:
    TargetSelectorDelegate(QObject *parent) : QItemDelegate(parent) { }
private:
Daniel Teske's avatar
Daniel Teske committed
93
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
94 95 96 97 98 99
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const;
    mutable QImage selectionGradient;
};

Daniel Teske's avatar
Daniel Teske committed
100 101 102 103 104 105 106
QSize TargetSelectorDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option)
    Q_UNUSED(index)
    return QSize(190, 30);
}

107 108
void TargetSelectorDelegate::paint(QPainter *painter,
                                   const QStyleOptionViewItem &option,
Daniel Teske's avatar
Daniel Teske committed
109
                                   const QModelIndex &index) const
110 111 112 113 114 115 116 117
{
    painter->save();
    painter->setClipping(false);

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

    if (option.state & QStyle::State_Selected) {
118 119 120 121
        QColor color =(option.state & QStyle::State_HasFocus) ?
                      option.palette.highlight().color() :
                      option.palette.dark().color();
        painter->fillRect(option.rect, color.darker(140));
122 123 124 125 126 127 128 129
        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
130 131 132 133 134 135 136 137

    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
138
        const_cast<QAbstractItemModel *>(index.model())->setData(index, QString(), Qt::ToolTipRole);
Daniel Teske's avatar
Daniel Teske committed
139 140
    painter->drawText(option.rect.left() + 6, option.rect.top() + (option.rect.height() - fm.height()) / 2 + fm.ascent(), elidedText);

141 142 143
    painter->restore();
}

Daniel Teske's avatar
Daniel Teske committed
144 145 146 147 148
////////
// ListWidget
////////
ListWidget::ListWidget(QWidget *parent)
    : QListWidget(parent), m_maxCount(0)
Tobias Hunger's avatar
Tobias Hunger committed
149 150 151 152 153
{
    setFocusPolicy(Qt::NoFocus);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setAlternatingRowColors(false);
    setFocusPolicy(Qt::WheelFocus);
154
    setItemDelegate(new TargetSelectorDelegate(this));
155
    setAttribute(Qt::WA_MacShowFocusRect, false);
Daniel Teske's avatar
Daniel Teske committed
156 157
    setStyleSheet(QString::fromLatin1("QListWidget { background: #464646; border-style: none; }"));
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
Tobias Hunger's avatar
Tobias Hunger committed
158 159 160
}


Daniel Teske's avatar
Daniel Teske committed
161
QSize ListWidget::sizeHint() const
Tobias Hunger's avatar
Tobias Hunger committed
162
{
Daniel Teske's avatar
Daniel Teske committed
163 164
    int height = m_maxCount * 30;
    int width = 190;
Tobias Hunger's avatar
Tobias Hunger committed
165

166
    // We try to keep the height of the popup equal to the actionbar
167
    QSize size(width, height);
hjk's avatar
hjk committed
168 169
    static QStatusBar *statusBar = Core::ICore::statusBar();
    static QWidget *actionBar = Core::ICore::mainWindow()->findChild<QWidget*>(QLatin1String("actionbar"));
170 171 172
    Q_ASSERT(actionBar);

    QMargins popupMargins = window()->contentsMargins();
Daniel Teske's avatar
Daniel Teske committed
173 174 175
    int alignedWithActionHeight
            = actionBar->height() - statusBar->height() - (popupMargins.top() + popupMargins.bottom());
    size.setHeight(qBound(alignedWithActionHeight, height, 2 * alignedWithActionHeight));
176
    return size;
Tobias Hunger's avatar
Tobias Hunger committed
177 178
}

Daniel Teske's avatar
Daniel Teske committed
179
void ListWidget::keyPressEvent(QKeyEvent *event)
Tobias Hunger's avatar
Tobias Hunger committed
180
{
Daniel Teske's avatar
Daniel Teske committed
181 182 183 184 185 186
    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
187 188
}

Daniel Teske's avatar
Daniel Teske committed
189
void ListWidget::keyReleaseEvent(QKeyEvent *event)
Tobias Hunger's avatar
Tobias Hunger committed
190
{
Daniel Teske's avatar
Daniel Teske committed
191 192 193
    if (event->key() != Qt::LeftArrow && event->key() != Qt::RightArrow)
        QListWidget::keyReleaseEvent(event);
}
Tobias Hunger's avatar
Tobias Hunger committed
194

Daniel Teske's avatar
Daniel Teske committed
195 196 197 198 199 200 201 202
void ListWidget::setMaxCount(int maxCount)
{
    // Note: the current assumption is that, this is not called while the listwidget is visible
    // Otherwise we would need to add code to MiniProjectTargetSelector reacting to the
    // updateGeometry (which then would jump ugly)
    m_maxCount = maxCount;
    updateGeometry();
}
Tobias Hunger's avatar
Tobias Hunger committed
203

Daniel Teske's avatar
Daniel Teske committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
////////
// 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*)));
    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
226
    }
Daniel Teske's avatar
Daniel Teske committed
227 228 229 230 231 232 233
    return 0;
}

QString ProjectListWidget::fullName(ProjectExplorer::Project *project)
{
    return tr("%1 (%2)").arg(project->displayName(), project->file()->fileName());
}
234

Daniel Teske's avatar
Daniel Teske committed
235 236 237
void ProjectListWidget::addProject(Project *project)
{
    m_ignoreIndexChange = true;
238

Daniel Teske's avatar
Daniel Teske committed
239 240 241 242 243 244 245 246
    QString sortName = fullName(project);
    int pos = 0;
    for (int i=0; i < count(); ++i) {
        Project *p = item(i)->data(Qt::UserRole).value<Project*>();
        QString itemSortName = fullName(p);
        if (itemSortName > sortName)
            pos = i;
    }
247

Daniel Teske's avatar
Daniel Teske committed
248 249 250 251 252 253 254 255
    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));
        }
    }
256

Daniel Teske's avatar
Daniel Teske committed
257 258 259 260 261
    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
262

Daniel Teske's avatar
Daniel Teske committed
263 264
    if (project == ProjectExplorerPlugin::instance()->startupProject()) {
        setCurrentItem(item);
Tobias Hunger's avatar
Tobias Hunger committed
265
    }
266

Daniel Teske's avatar
Daniel Teske committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    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
291
    }
292

Daniel Teske's avatar
Daniel Teske committed
293 294
    m_ignoreIndexChange = false;

295 296
}

Daniel Teske's avatar
Daniel Teske committed
297
void ProjectListWidget::setProject(int index)
con's avatar
con committed
298
{
Daniel Teske's avatar
Daniel Teske committed
299 300 301 302 303 304
    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
305 306
}

Daniel Teske's avatar
Daniel Teske committed
307
void ProjectListWidget::changeStartupProject(Project *project)
Tobias Hunger's avatar
Tobias Hunger committed
308
{
Daniel Teske's avatar
Daniel Teske committed
309
    setCurrentItem(itemForProject(project));
Tobias Hunger's avatar
Tobias Hunger committed
310 311
}

Daniel Teske's avatar
Daniel Teske committed
312 313 314 315 316 317
/////////
// GenericListWidget
/////////

GenericListWidget::GenericListWidget(QWidget *parent)
    : ListWidget(parent), m_ignoreIndexChange(false)
318
{
Daniel Teske's avatar
Daniel Teske committed
319 320
    connect(this, SIGNAL(currentRowChanged(int)),
            this, SLOT(rowChanged(int)));
321 322
}

Daniel Teske's avatar
Daniel Teske committed
323
void GenericListWidget::setProjectConfigurations(const QList<ProjectConfiguration *> &list, ProjectConfiguration *active)
324
{
Daniel Teske's avatar
Daniel Teske committed
325 326 327 328 329 330 331 332 333 334 335
    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()));
    }
    foreach (ProjectConfiguration *pc, list)
        addProjectConfiguration(pc);
    setActiveProjectConfiguration(active);
    m_ignoreIndexChange = false;
336
}
dt's avatar
dt committed
337

Daniel Teske's avatar
Daniel Teske committed
338
void GenericListWidget::setActiveProjectConfiguration(ProjectConfiguration *active)
339
{
Daniel Teske's avatar
Daniel Teske committed
340 341
    QListWidgetItem *item = itemForProjectConfiguration(active);
    setCurrentItem(item);
342 343
}

Daniel Teske's avatar
Daniel Teske committed
344
void GenericListWidget::addProjectConfiguration(ProjectExplorer::ProjectConfiguration *pc)
345
{
Daniel Teske's avatar
Daniel Teske committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    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()));
    m_ignoreIndexChange = false;
365 366
}

Daniel Teske's avatar
Daniel Teske committed
367
void GenericListWidget::removeProjectConfiguration(ProjectExplorer::ProjectConfiguration *pc)
368
{
Daniel Teske's avatar
Daniel Teske committed
369 370 371 372 373
    m_ignoreIndexChange = true;
    disconnect(pc, SIGNAL(displayNameChanged()),
               this, SLOT(displayNameChanged()));
    delete itemForProjectConfiguration(pc);
    m_ignoreIndexChange = false;
374 375
}

Daniel Teske's avatar
Daniel Teske committed
376
void GenericListWidget::rowChanged(int index)
377
{
Daniel Teske's avatar
Daniel Teske committed
378 379 380 381 382
    if (m_ignoreIndexChange)
        return;
    if (index < 0)
        return;
    emit changeActiveProjectConfiguration(item(index)->data(Qt::UserRole).value<ProjectConfiguration *>());
383 384
}

Daniel Teske's avatar
Daniel Teske committed
385
void GenericListWidget::displayNameChanged()
386
{
Daniel Teske's avatar
Daniel Teske committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    m_ignoreIndexChange = true;
    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);
    m_ignoreIndexChange = false;
412 413
}

Daniel Teske's avatar
Daniel Teske committed
414
QListWidgetItem *GenericListWidget::itemForProjectConfiguration(ProjectConfiguration *pc)
415
{
Daniel Teske's avatar
Daniel Teske committed
416 417 418 419 420 421 422
    for (int i = 0; i < count(); ++i) {
        QListWidgetItem *lwi = item(i);
        if (lwi->data(Qt::UserRole).value<ProjectConfiguration *>() == pc) {
            return lwi;
        }
    }
    return 0;
423 424
}

Daniel Teske's avatar
Daniel Teske committed
425
QWidget *createTitleLabel(const QString &text)
Tobias Hunger's avatar
Tobias Hunger committed
426
{
Daniel Teske's avatar
Daniel Teske committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    Utils::StyledBar *bar = new Utils::StyledBar;
    bar->setSingleRow(true);
    QVBoxLayout *toolLayout = new QVBoxLayout(bar);
    toolLayout->setMargin(0);
    toolLayout->setSpacing(0);

    QLabel *l = new QLabel(text);
    l->setIndent(6);
    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
444 445
}

Daniel Teske's avatar
Daniel Teske committed
446
class OnePixelGreyLine : public QWidget
447
{
Daniel Teske's avatar
Daniel Teske committed
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
public:
    OnePixelGreyLine(QWidget *parent)
        : QWidget(parent)
    {
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
        setMinimumWidth(1);
        setMaximumWidth(1);
    }
    void paintEvent(QPaintEvent *e)
    {
        Q_UNUSED(e);
        QPainter p(this);
        p.fillRect(contentsRect(), QColor(160, 160, 160, 255));
    }
};

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);
476 477
    setProperty("panelwidget", true);
    setContentsMargins(QMargins(0, 1, 1, 8));
478 479 480 481 482
    setWindowFlags(Qt::Popup);

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

Daniel Teske's avatar
Daniel Teske committed
483 484 485 486 487 488 489 490
    QGridLayout *grid = new QGridLayout(this);
    grid->setMargin(0);
    grid->setSpacing(0);

    m_summaryLabel = new QLabel(this);
    m_summaryLabel->setMargin(3);
    m_summaryLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    m_summaryLabel->setStyleSheet(QString::fromLatin1("background: #464646;"));
491 492
    m_summaryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    m_summaryLabel->setTextInteractionFlags(m_summaryLabel->textInteractionFlags() | Qt::LinksAccessibleByMouse);
Daniel Teske's avatar
Daniel Teske committed
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

    grid->addWidget(m_summaryLabel, 0, 0, 1, 2 * LAST - 1);

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

    m_titleWidgets[PROJECT] = createTitleLabel(tr("Project"));
    grid->addWidget(m_titleWidgets[PROJECT], 1, 0, 1, 2);
    m_projectListWidget = new ProjectListWidget(m_sessionManager, this);
    grid->addWidget(m_projectListWidget, 2, 0);
    m_separators[PROJECT] = new OnePixelGreyLine(this);
    grid->addWidget(m_separators[PROJECT], 2, 1);

    QStringList titles;
    titles << tr("Target") << tr("Build")
           << tr("Deploy") << tr("Run");

    for (int i = TARGET; i < LAST; ++i) {
        m_titleWidgets[i] = createTitleLabel(titles.at(i -1));
        grid->addWidget(m_titleWidgets[i], 1, 2 * i, 1, 2);
        m_listWidgets[i] = new GenericListWidget(this);
        grid->addWidget(m_listWidgets[i], 2, 2 *i);
        m_separators[i] = new OnePixelGreyLine(this);
        grid->addWidget(m_separators[i], 2, 1 + 2 * i);
    }

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

525 526 527
    connect(m_summaryLabel, SIGNAL(linkActivated(QString)),
            this, SLOT(switchToProjectsMode()));

Daniel Teske's avatar
Daniel Teske committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    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*)));

    connect(m_listWidgets[TARGET], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
            this, SLOT(setActiveTarget(ProjectExplorer::ProjectConfiguration *)));
    connect(m_listWidgets[BUILD], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
            this, SLOT(setActiveBuildConfiguration(ProjectExplorer::ProjectConfiguration *)));
    connect(m_listWidgets[DEPLOY], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
            this, SLOT(setActiveDeployConfiguration(ProjectExplorer::ProjectConfiguration *)));
    connect(m_listWidgets[RUN], SIGNAL(changeActiveProjectConfiguration(ProjectExplorer::ProjectConfiguration*)),
            this, SLOT(setActiveRunConfiguration(ProjectExplorer::ProjectConfiguration *)));
}
545

Daniel Teske's avatar
Daniel Teske committed
546 547 548 549
void MiniProjectTargetSelector::setActiveTarget(ProjectExplorer::ProjectConfiguration *pc)
{
    m_project->setActiveTarget(static_cast<Target *>(pc));
}
550

Daniel Teske's avatar
Daniel Teske committed
551 552 553 554
void MiniProjectTargetSelector::setActiveBuildConfiguration(ProjectExplorer::ProjectConfiguration *pc)
{
    m_target->setActiveBuildConfiguration(static_cast<BuildConfiguration *>(pc));
}
555

Daniel Teske's avatar
Daniel Teske committed
556 557 558 559 560 561 562 563 564
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));
}
565

Daniel Teske's avatar
Daniel Teske committed
566 567 568 569
void MiniProjectTargetSelector::projectAdded(ProjectExplorer::Project *project)
{
    connect(project, SIGNAL(addedTarget(ProjectExplorer::Target*)),
            this, SLOT(addedTarget(ProjectExplorer::Target*)));
570

Daniel Teske's avatar
Daniel Teske committed
571 572
    connect(project, SIGNAL(removedTarget(ProjectExplorer::Target*)),
            this, SLOT(removedTarget(ProjectExplorer::Target*)));
573

Daniel Teske's avatar
Daniel Teske committed
574 575
    foreach (Target *t, project->targets())
        addedTarget(t);
576

Daniel Teske's avatar
Daniel Teske committed
577 578 579 580 581
    updateProjectListVisible();
    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
582 583
}

Daniel Teske's avatar
Daniel Teske committed
584
void MiniProjectTargetSelector::projectRemoved(ProjectExplorer::Project *project)
585
{
Daniel Teske's avatar
Daniel Teske committed
586 587
    disconnect(project, SIGNAL(addedTarget(ProjectExplorer::Target*)),
               this, SLOT(addedTarget(ProjectExplorer::Target*)));
588

Daniel Teske's avatar
Daniel Teske committed
589 590
    disconnect(project, SIGNAL(removedTarget(ProjectExplorer::Target*)),
               this, SLOT(removedTarget(ProjectExplorer::Target*)));
591

Daniel Teske's avatar
Daniel Teske committed
592 593
    foreach (Target *t, project->targets())
        removedTarget(t);
594

Daniel Teske's avatar
Daniel Teske committed
595 596 597 598 599
    updateProjectListVisible();
    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
600 601
}

Daniel Teske's avatar
Daniel Teske committed
602
void MiniProjectTargetSelector::addedTarget(ProjectExplorer::Target *target)
603
{
Daniel Teske's avatar
Daniel Teske committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    connect(target, SIGNAL(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
            this, SLOT(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
    connect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
            this, SLOT(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));

    connect(target, SIGNAL(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
            this, SLOT(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
    connect(target, SIGNAL(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
            this, SLOT(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));

    connect(target, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
            this, SLOT(addedRunConfiguration(ProjectExplorer::RunConfiguration*)));
    connect(target, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
            this, SLOT(removedRunConfiguration(ProjectExplorer::RunConfiguration*)));

    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);

    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
633 634
}

Daniel Teske's avatar
Daniel Teske committed
635
void MiniProjectTargetSelector::removedTarget(ProjectExplorer::Target *target)
636
{
Daniel Teske's avatar
Daniel Teske committed
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
    disconnect(target, SIGNAL(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
               this, SLOT(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
    disconnect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
               this, SLOT(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));

    disconnect(target, SIGNAL(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
               this, SLOT(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
    disconnect(target, SIGNAL(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
               this, SLOT(removedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));

    disconnect(target, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
               this, SLOT(addedRunConfiguration(ProjectExplorer::RunConfiguration*)));
    disconnect(target, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
               this, SLOT(removedRunConfiguration(ProjectExplorer::RunConfiguration*)));

    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);

    updateTargetListVisible();
    updateBuildListVisible();
    updateDeployListVisible();
    updateRunListVisible();
666 667
}

Daniel Teske's avatar
Daniel Teske committed
668
void MiniProjectTargetSelector::addedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
669
{
Daniel Teske's avatar
Daniel Teske committed
670 671 672 673
    if (bc->target() == m_target)
        m_listWidgets[BUILD]->addProjectConfiguration(bc);
    updateBuildListVisible();
}
674

Daniel Teske's avatar
Daniel Teske committed
675 676 677 678 679 680
void MiniProjectTargetSelector::removedBuildConfiguration(ProjectExplorer::BuildConfiguration *bc)
{
    if (bc->target() == m_target)
        m_listWidgets[BUILD]->removeProjectConfiguration(bc);
    updateBuildListVisible();
}
dt's avatar
dt committed
681

Daniel Teske's avatar
Daniel Teske committed
682 683 684 685 686 687
void MiniProjectTargetSelector::addedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
{
    if (dc->target() == m_target)
        m_listWidgets[DEPLOY]->addProjectConfiguration(dc);
    updateDeployListVisible();
}
dt's avatar
dt committed
688

Daniel Teske's avatar
Daniel Teske committed
689 690 691 692 693 694
void MiniProjectTargetSelector::removedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
{
    if (dc->target() == m_target)
        m_listWidgets[DEPLOY]->removeProjectConfiguration(dc);
    updateDeployListVisible();
}
dt's avatar
dt committed
695

Daniel Teske's avatar
Daniel Teske committed
696 697 698 699 700 701
void MiniProjectTargetSelector::addedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
{
    if (rc->target() == m_target)
        m_listWidgets[RUN]->addProjectConfiguration(rc);
    updateRunListVisible();
}
702

Daniel Teske's avatar
Daniel Teske committed
703 704 705 706 707 708
void MiniProjectTargetSelector::removedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
{
    if (rc->target() == m_target)
        m_listWidgets[RUN]->removeProjectConfiguration(rc);
    updateRunListVisible();
}
709

Daniel Teske's avatar
Daniel Teske committed
710 711 712
void MiniProjectTargetSelector::updateProjectListVisible()
{
    bool visible = m_sessionManager->projects().size() > 1;
713

Daniel Teske's avatar
Daniel Teske committed
714 715 716
    m_projectListWidget->setVisible(visible);
    m_projectListWidget->setMaxCount(m_sessionManager->projects().size());
    m_titleWidgets[PROJECT]->setVisible(visible);
717

Daniel Teske's avatar
Daniel Teske committed
718 719 720
    updateSummary();
    updateSeparatorVisible();
}
Tobias Hunger's avatar
Tobias Hunger committed
721

Daniel Teske's avatar
Daniel Teske committed
722 723 724 725 726 727 728 729 730 731 732 733 734
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();
    updateSeparatorVisible();
}
dt's avatar
dt committed
735

Daniel Teske's avatar
Daniel Teske committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749
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();
    updateSeparatorVisible();
}
750

Daniel Teske's avatar
Daniel Teske committed
751 752 753 754 755 756 757 758 759 760 761 762 763
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();
    updateSeparatorVisible();
764 765
}

Daniel Teske's avatar
Daniel Teske committed
766
void MiniProjectTargetSelector::updateRunListVisible()
767
{
Daniel Teske's avatar
Daniel Teske committed
768 769 770 771 772 773 774 775 776 777 778 779
    int maxCount = 0;
    foreach (Project *p, m_sessionManager->projects())
        foreach (Target *t, p->targets())
            maxCount = qMax(t->runConfigurations().size(), maxCount);

    bool visible = maxCount > 1;
    m_listWidgets[RUN]->setVisible(visible);
    m_listWidgets[RUN]->setMaxCount(maxCount);
    m_titleWidgets[RUN]->setVisible(visible);
    updateSummary();
    updateSeparatorVisible();
}
780

Daniel Teske's avatar
Daniel Teske committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794
void MiniProjectTargetSelector::changeStartupProject(ProjectExplorer::Project *project)
{
    if (m_project) {
        disconnect(m_project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
                   this, SLOT(activeTargetChanged(ProjectExplorer::Target*)));
    }
    m_project = project;
    if (m_project) {
        connect(m_project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
                this, SLOT(activeTargetChanged(ProjectExplorer::Target*)));
        activeTargetChanged(m_project->activeTarget());
    } else {
        activeTargetChanged(0);
    }
795

Daniel Teske's avatar
Daniel Teske committed
796 797 798 799 800 801 802
    if (project) {
        QList<ProjectConfiguration *> list;
        foreach (Target *t, project->targets())
            list.append(t);
        m_listWidgets[TARGET]->setProjectConfigurations(list, project->activeTarget());
    } else {
        m_listWidgets[TARGET]->setProjectConfigurations(QList<ProjectConfiguration *>(), 0);
803
    }
Daniel Teske's avatar
Daniel Teske committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

    updateActionAndSummary();
}

void MiniProjectTargetSelector::activeTargetChanged(ProjectExplorer::Target *target)
{
    if (m_target) {
        disconnect(m_target, SIGNAL(displayNameChanged()),
                   this, SLOT(updateActionAndSummary()));
        disconnect(m_target, SIGNAL(toolTipChanged()),
                   this, SLOT(updateActionAndSummary()));
        disconnect(m_target, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
                   this, SLOT(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)));
        disconnect(m_target, SIGNAL(activeDeployConfigurationChanged(ProjectExplorer::DeployConfiguration*)),
                   this, SLOT(activeDeployConfigurationChanged(ProjectExplorer::DeployConfiguration*)));
        disconnect(m_target, SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
                   this, SLOT(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)));
821 822
    }

Daniel Teske's avatar
Daniel Teske committed
823 824
    m_target = target;

825 826
    m_listWidgets[TARGET]->setActiveProjectConfiguration(m_target);

Daniel Teske's avatar
Daniel Teske committed
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 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
    if (m_buildConfiguration)
        disconnect(m_buildConfiguration, SIGNAL(displayNameChanged()),
                   this, SLOT(updateActionAndSummary()));
    if (m_deployConfiguration)
        disconnect(m_deployConfiguration, SIGNAL(displayNameChanged()),
                   this, SLOT(updateActionAndSummary()));

    if (m_runConfiguration)
        disconnect(m_runConfiguration, SIGNAL(displayNameChanged()),
                   this, SLOT(updateActionAndSummary()));

    if (m_target) {
        QList<ProjectConfiguration *> bl;
        foreach (BuildConfiguration *bc, target->buildConfigurations())
            bl.append(bc);
        m_listWidgets[BUILD]->setProjectConfigurations(bl, target->activeBuildConfiguration());

        QList<ProjectConfiguration *> dl;
        foreach (DeployConfiguration *dc, target->deployConfigurations())
            dl.append(dc);
        m_listWidgets[DEPLOY]->setProjectConfigurations(dl, target->activeDeployConfiguration());

        QList<ProjectConfiguration *> rl;
        foreach (RunConfiguration *rc, target->runConfigurations())
            rl.append(rc);
        m_listWidgets[RUN]->setProjectConfigurations(rl, target->activeRunConfiguration());

        m_buildConfiguration = m_target->activeBuildConfiguration();
        if (m_buildConfiguration)
            connect(m_buildConfiguration, SIGNAL(displayNameChanged()),
                    this, SLOT(updateActionAndSummary()));
        m_deployConfiguration = m_target->activeDeployConfiguration();
        if (m_deployConfiguration)
            connect(m_deployConfiguration, SIGNAL(displayNameChanged()),
                    this, SLOT(updateActionAndSummary()));
        m_runConfiguration = m_target->activeRunConfiguration();
        if (m_runConfiguration)
            connect(m_runConfiguration, SIGNAL(displayNameChanged()),
                    this, SLOT(updateActionAndSummary()));

        connect(m_target, SIGNAL(displayNameChanged()),
                this, SLOT(updateActionAndSummary()));
        connect(m_target, SIGNAL(toolTipChanged()),
                this, SLOT(updateActionAndSummary()));
        connect(m_target, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
                this, SLOT(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)));
        connect(m_target, SIGNAL(activeDeployConfigurationChanged(ProjectExplorer::DeployConfiguration*)),
                this, SLOT(activeDeployConfigurationChanged(ProjectExplorer::DeployConfiguration*)));
        connect(m_target, SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
                this, SLOT(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)));
    } else {
        m_listWidgets[BUILD]->setProjectConfigurations(QList<ProjectConfiguration *>(), 0);
        m_listWidgets[DEPLOY]->setProjectConfigurations(QList<ProjectConfiguration *>(), 0);
        m_listWidgets[RUN]->setProjectConfigurations(QList<ProjectConfiguration *>(), 0);
        m_buildConfiguration = 0;
        m_deployConfiguration = 0;
        m_runConfiguration = 0;
    }
    updateActionAndSummary();
886 887
}

Daniel Teske's avatar
Daniel Teske committed
888
void MiniProjectTargetSelector::activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration *bc)
Tobias Hunger's avatar
Tobias Hunger committed
889
{
Daniel Teske's avatar
Daniel Teske committed
890 891 892 893 894 895 896 897 898 899
    if (m_buildConfiguration)
        disconnect(m_buildConfiguration, SIGNAL(displayNameChanged()),
                   this, SLOT(updateActionAndSummary()));
    m_buildConfiguration = bc;
    if (m_buildConfiguration)
        connect(m_buildConfiguration, SIGNAL(displayNameChanged()),
                this, SLOT(updateActionAndSummary()));
    m_listWidgets[BUILD]->setActiveProjectConfiguration(bc);
    updateActionAndSummary();
}
Tobias Hunger's avatar
Tobias Hunger committed
900

Daniel Teske's avatar
Daniel Teske committed
901 902 903 904 905 906 907 908 909 910
void MiniProjectTargetSelector::activeDeployConfigurationChanged(ProjectExplorer::DeployConfiguration *dc)
{
    if (m_deployConfiguration)
        disconnect(m_deployConfiguration, SIGNAL(displayNameChanged()),
                   this, SLOT(updateActionAndSummary()));
    m_deployConfiguration = dc;
    if (m_deployConfiguration)
        connect(m_deployConfiguration, SIGNAL(displayNameChanged()),
                this, SLOT(updateActionAndSummary()));
    m_listWidgets[DEPLOY]->setActiveProjectConfiguration(dc);
911
    updateActionAndSummary();
Daniel Teske's avatar
Daniel Teske committed
912
}
Tobias Hunger's avatar
Tobias Hunger committed
913

Daniel Teske's avatar
Daniel Teske committed
914 915 916 917 918 919 920 921 922 923 924 925
void MiniProjectTargetSelector::activeRunConfigurationChanged(ProjectExplorer::RunConfiguration *rc)
{
    if (m_runConfiguration)
        disconnect(m_runConfiguration, SIGNAL(displayNameChanged()),
                   this, SLOT(updateActionAndSummary()));
    m_runConfiguration = rc;
    if (m_runConfiguration)
        connect(m_runConfiguration, SIGNAL(displayNameChanged()),
                this, SLOT(updateActionAndSummary()));
    m_listWidgets[RUN]->setActiveProjectConfiguration(rc);
    updateActionAndSummary();
}
926

Daniel Teske's avatar
Daniel Teske committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
void MiniProjectTargetSelector::updateSeparatorVisible()
{
    QVector<bool> visibility;
    visibility.resize(LAST);
    visibility[PROJECT] = m_projectListWidget->isVisibleTo(this);
    for (int i = TARGET; i < LAST; ++i)
        visibility[i] = m_listWidgets[i]->isVisibleTo(this);
    int lastVisible = visibility.lastIndexOf(true);
    if (lastVisible != -1)
        visibility[lastVisible] = false;

    for (int i = PROJECT; i < LAST; ++i)
        m_separators[i]->setVisible(visibility[i]);
}

void MiniProjectTargetSelector::setVisible(bool visible)
{
    if (visible) {
hjk's avatar
hjk committed
945
        QStatusBar *statusBar = Core::ICore::statusBar();
Daniel Teske's avatar
Daniel Teske committed
946 947 948 949 950 951 952 953 954 955 956 957
        QPoint moveTo = statusBar->mapToGlobal(QPoint(0,0));
        moveTo -= QPoint(0, sizeHint().height());
        move(moveTo);
        if (!focusWidget() || !focusWidget()->isVisibleTo(this)) { // Does the second part actually work?
            if (m_projectListWidget->isVisibleTo(this))
                m_projectListWidget->setFocus();
            for (int i = TARGET; i < LAST; ++i) {
                if (m_listWidgets[i]->isVisibleTo(this)) {
                    m_listWidgets[i]->setFocus();
                    break;
                }
            }
958 959 960
        }
    }

Daniel Teske's avatar
Daniel Teske committed
961 962
    QWidget::setVisible(visible);
    m_projectAction->setChecked(visible);
Tobias Hunger's avatar
Tobias Hunger committed
963 964
}

Daniel Teske's avatar
Daniel Teske committed
965
void MiniProjectTargetSelector::toggleVisible()
Tobias Hunger's avatar
Tobias Hunger committed
966
{
Daniel Teske's avatar
Daniel Teske committed
967 968
    setVisible(!isVisible());
}
Tobias Hunger's avatar
Tobias Hunger committed
969

Daniel Teske's avatar
Daniel Teske committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
void MiniProjectTargetSelector::nextOrShow()
{
    if (!isVisible()) {
        show();
    } else {
        m_hideOnRelease = true;
        m_earliestHidetime = QDateTime::currentDateTime().addMSecs(800);
        if (ListWidget *lw = qobject_cast<ListWidget *>(focusWidget())) {
            if (lw->currentRow() < lw->count() -1)
                lw->setCurrentRow(lw->currentRow() + 1);
            else
                lw->setCurrentRow(0);
        }
    }
}
Tobias Hunger's avatar
Tobias Hunger committed
985

Daniel Teske's avatar
Daniel Teske committed
986 987 988 989 990 991 992 993 994 995
void MiniProjectTargetSelector::keyReleaseEvent(QKeyEvent *ke)
{
    if (m_hideOnRelease) {
        if (ke->modifiers() == 0
                /*HACK this is to overcome some event inconsistencies between platforms*/
                || (ke->modifiers() == Qt::AltModifier
                    && (ke->key() == Qt::Key_Alt || ke->key() == -1))) {
            delayedHide();
            m_hideOnRelease = false;
        }
Tobias Hunger's avatar
Tobias Hunger committed
996
    }
Daniel Teske's avatar
Daniel Teske committed
997 998
    QWidget::keyReleaseEvent(ke);
}
999

Daniel Teske's avatar
Daniel Teske committed
1000 1001
QSize MiniProjectTargetSelector::sizeHint() const
{
hjk's avatar
hjk committed
1002 1003
    static QStatusBar *statusBar = Core::ICore::statusBar();
    static QWidget *actionBar = Core::ICore::mainWindow()->findChild<QWidget*>(QLatin1String("actionbar"));
Daniel Teske's avatar
Daniel Teske committed
1004
    Q_ASSERT(actionBar);
1005

1006
    // At least the size of the actionbar
Daniel Teske's avatar
Daniel Teske committed
1007 1008 1009 1010 1011 1012
    int alignedWithActionHeight
            = actionBar->height() - statusBar->height();
    QSize s = QWidget::sizeHint();
    if (s.height() < alignedWithActionHeight)
        s.setHeight(alignedWithActionHeight);
    return s;
Tobias Hunger's avatar
Tobias Hunger committed
1013
}
1014

Daniel Teske's avatar
Daniel Teske committed
1015
void MiniProjectTargetSelector::delayedHide()
1016
{
Daniel Teske's avatar
Daniel Teske committed
1017 1018 1019 1020 1021 1022
    QDateTime current = QDateTime::currentDateTime();
    if (m_earliestHidetime > current) {
        // schedule for later
        QTimer::singleShot(current.msecsTo(m_earliestHidetime) + 50, this, SLOT(delayedHide()));
    } else {
        hide();
1023 1024 1025
    }
}

Daniel Teske's avatar
Daniel Teske committed
1026 1027 1028 1029 1030 1031 1032 1033
// This is a workaround for the problem that Windows
// will let the mouse events through when you click
// outside a popup to close it. This causes the popup
// to open on mouse release if you hit the button, which
//
//
// A similar case can be found in QComboBox
void MiniProjectTargetSelector::mousePressEvent(QMouseEvent *e)
1034
{
Daniel Teske's avatar
Daniel Teske committed
1035 1036 1037
    setAttribute(Qt::WA_NoMouseReplay);
    QWidget::mousePressEvent(e);
}
1038

Daniel Teske's avatar
Daniel Teske committed
1039 1040 1041
void MiniProjectTargetSelector::updateActionAndSummary()
{
    QString projectName;
1042
    QString targetName;
con's avatar
con committed
1043
    QString targetToolTipText;
1044
    QString buildConfig;
Daniel Teske's avatar
Daniel Teske committed
1045
    QString deployConfig;
1046
    QString runConfig;
1047
    QIcon targetIcon = style()->standardIcon(QStyle::SP_ComputerIcon);
1048

Daniel Teske's avatar
Daniel Teske committed
1049
    Project *project = ProjectExplorerPlugin::instance()->startupProject();
1050 1051
    if (project) {
        projectName = project->displayName();
Tobias Hunger's avatar
Tobias Hunger committed
1052

1053
        if (Target *target = project->activeTarget()) {
Daniel Teske's avatar
Daniel Teske committed
1054 1055 1056
            targetName = project->activeTarget()->displayName();

            if (BuildConfiguration *bc = target->activeBuildConfiguration())
Tobias Hunger's avatar
Tobias Hunger committed
1057
                buildConfig = bc->displayName();
1058

Daniel Teske's avatar
Daniel Teske committed
1059 1060 1061 1062
            if (DeployConfiguration *dc = target->activeDeployConfiguration())
                deployConfig = dc->displayName();

            if (RunConfiguration *rc = target->activeRunConfiguration())
1063
                runConfig = rc->displayName();
Daniel Teske's avatar
Daniel Teske committed
1064

con's avatar
con committed
1065
            targetToolTipText = target->toolTip();
1066
            targetIcon = createCenteredIcon(target->icon(), target->overlayIcon());
Tobias Hunger's avatar
Tobias Hunger committed
1067
        }
1068 1069
    }
    m_projectAction->setProperty("heading", projectName);
1070 1071 1072 1073 1074
    if (project && project->needsConfiguration()) {
        m_projectAction->setProperty("subtitle", tr("Unconfigured"));
    } else {
        m_projectAction->setProperty("subtitle", buildConfig);
    }
con's avatar
con committed
1075
    m_projectAction->setIcon(targetIcon);
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    QStringList lines;
    lines << tr("<b>Project:</b> %1").arg(projectName);
    if (!targetName.isEmpty())
        lines << tr("<b>Target:</b> %1").arg(targetName);
    if (!buildConfig.isEmpty())
        lines << tr("<b>Build:</b> %1").arg(buildConfig);
    if (!deployConfig.isEmpty())
        lines << tr("<b>Deploy:</b> %1").arg(deployConfig);
    if (!runConfig.isEmpty())
        lines << tr("<b>Run:</b> %1").arg(runConfig);
    if (!targetToolTipText.isEmpty())
        lines << tr("%1").arg(targetToolTipText);
    QString toolTip = tr("<html><nobr>%1</html>")
            .arg(lines.join(QLatin1String("<br/>")));
    m_projectAction->setToolTip(toolTip);
Daniel Teske's avatar
Daniel Teske committed
1091
    updateSummary();
Tobias Hunger's avatar
Tobias Hunger committed
1092 1093
}

Daniel Teske's avatar
Daniel Teske committed
1094
void MiniProjectTargetSelector::updateSummary()
Tobias Hunger's avatar
Tobias Hunger committed
1095
{
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    // 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) {
        m_summaryLabel->setMinimumHeight(0);
        m_summaryLabel->setMaximumHeight(800);
    } else {
        if (visibleLineCount < 3) {
            foreach (Project *p, m_sessionManager->projects()) {
                if (p->needsConfiguration()) {
                    visibleLineCount = 3;
                    break;
                }
            }
        }

        int height = visibleLineCount * QFontMetrics(m_summaryLabel->font()).height() + m_summaryLabel->margin() *2;
        m_summaryLabel->setMinimumHeight(height);
        m_summaryLabel->setMaximumHeight(height);
    }