Skip to content
Snippets Groups Projects
branchdialog.cpp 6.96 KiB
Newer Older
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
hjk's avatar
hjk committed
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
** 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.
con's avatar
con committed
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
** 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
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
** Nokia at info@qt.nokia.com.
**
**************************************************************************/

#include "branchdialog.h"
Tobias Hunger's avatar
Tobias Hunger committed
#include "branchadddialog.h"
#include "branchmodel.h"
#include "gitclient.h"
#include "ui_branchdialog.h"
#include "stashdialog.h" // Label helpers

Tobias Hunger's avatar
Tobias Hunger committed
#include <utils/checkablemessagebox.h>
#include <vcsbase/vcsbaseoutputwindow.h>

#include <QtGui/QItemSelectionModel>
#include <QtGui/QPushButton>
#include <QtGui/QMessageBox>

namespace Git {
Tobias Hunger's avatar
Tobias Hunger committed
namespace Internal {
BranchDialog::BranchDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::BranchDialog),
Tobias Hunger's avatar
Tobias Hunger committed
    m_model(new BranchModel(GitPlugin::instance()->gitClient(), this))
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    setAttribute(Qt::WA_DeleteOnClose, true); // Do not update unnecessarily
    m_ui->setupUi(this);
Tobias Hunger's avatar
Tobias Hunger committed
    connect(m_ui->refreshButton, SIGNAL(clicked()), this, SLOT(refresh()));
    connect(m_ui->addButton, SIGNAL(clicked()), this, SLOT(add()));
    connect(m_ui->checkoutButton, SIGNAL(clicked()), this, SLOT(checkout()));
    connect(m_ui->removeButton, SIGNAL(clicked()), this, SLOT(remove()));
    connect(m_ui->diffButton, SIGNAL(clicked()), this, SLOT(diff()));
    connect(m_ui->logButton, SIGNAL(clicked()), this, SLOT(log()));
Tobias Hunger's avatar
Tobias Hunger committed
    m_ui->branchView->setModel(m_model);
Tobias Hunger's avatar
Tobias Hunger committed
    connect(m_ui->branchView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(enableButtons()));
Tobias Hunger's avatar
Tobias Hunger committed
    enableButtons();
}

BranchDialog::~BranchDialog()
{
    delete m_ui;
Tobias Hunger's avatar
Tobias Hunger committed
    delete m_model;
    m_model = 0;
}

void BranchDialog::refresh(const QString &repository, bool force)
{
    if (m_repository == repository && !force)
Tobias Hunger's avatar
Tobias Hunger committed
        return;

    m_repository = repository;
    m_ui->repositoryLabel->setText(StashDialog::msgRepositoryLabel(m_repository));
Tobias Hunger's avatar
Tobias Hunger committed
    QString errorMessage;
    if (!m_model->refresh(m_repository, &errorMessage))
        VCSBase::VCSBaseOutputWindow::instance()->appendError(errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
    m_ui->branchView->expandAll();
Tobias Hunger's avatar
Tobias Hunger committed
void BranchDialog::enableButtons()
Tobias Hunger's avatar
Tobias Hunger committed
    QModelIndex idx = selectedIndex();
    const bool hasSelection = idx.isValid();
    const bool currentSelected = hasSelection && idx == m_model->currentBranch();
    const bool isLocal = m_model->isLocal(idx);
    const bool isLeaf = m_model->isLeaf(idx);
Tobias Hunger's avatar
Tobias Hunger committed
    m_ui->removeButton->setEnabled(hasSelection && !currentSelected && isLocal && isLeaf);
    m_ui->logButton->setEnabled(hasSelection && isLeaf);
    m_ui->diffButton->setEnabled(hasSelection && isLeaf);
    m_ui->checkoutButton->setEnabled(hasSelection && !currentSelected && isLocal && isLeaf);
Tobias Hunger's avatar
Tobias Hunger committed
void BranchDialog::refresh()
Tobias Hunger's avatar
Tobias Hunger committed
void BranchDialog::add()
Tobias Hunger's avatar
Tobias Hunger committed
    QString trackedBranch = m_model->branchName(selectedIndex());
    bool isLocal = m_model->isLocal(selectedIndex());
    if (trackedBranch.isEmpty()) {
        trackedBranch = m_model->branchName(m_model->currentBranch());
        isLocal = true;
    }
Tobias Hunger's avatar
Tobias Hunger committed
    QStringList localNames = m_model->localBranchNames();
Tobias Hunger's avatar
Tobias Hunger committed
    QString suggestedNameBase = trackedBranch.mid(trackedBranch.lastIndexOf(QLatin1Char('/')) + 1);
    QString suggestedName = suggestedNameBase;
    int i = 2;
    while (localNames.contains(suggestedName)) {
        suggestedName = suggestedNameBase + QString::number(i);
        ++i;
    }
Tobias Hunger's avatar
Tobias Hunger committed
    BranchAddDialog branchAddDialog;
    branchAddDialog.setBranchName(suggestedName);
    branchAddDialog.setTrackedBranchName(trackedBranch, !isLocal);

    if (branchAddDialog.exec() == QDialog::Accepted && m_model) {
        QModelIndex idx = m_model->addBranch(branchAddDialog.branchName(), branchAddDialog.track(), trackedBranch);
        m_ui->branchView->selectionModel()->select(idx, QItemSelectionModel::Clear
                                                        | QItemSelectionModel::Select
                                                        | QItemSelectionModel::Current);
        m_ui->branchView->scrollTo(idx);
Tobias Hunger's avatar
Tobias Hunger committed
void BranchDialog::checkout()
Tobias Hunger's avatar
Tobias Hunger committed
    QModelIndex idx = selectedIndex();
    Q_ASSERT(m_model->isLocal(idx));
Tobias Hunger's avatar
Tobias Hunger committed
    m_model->checkoutBranch(idx);
Tobias Hunger's avatar
Tobias Hunger committed
/* Prompt to delete a local branch and do so. */
void BranchDialog::remove()
Tobias Hunger's avatar
Tobias Hunger committed
    QModelIndex selected = selectedIndex();
    Q_ASSERT(selected != m_model->currentBranch()); // otherwise the button would not be enabled!

    QString branchName = m_model->branchName(selected);
    if (branchName.isEmpty())
Tobias Hunger's avatar
Tobias Hunger committed

    QString message = tr("Would you like to delete the branch '%1'?").arg(branchName);
    bool wasMerged = m_model->branchIsMerged(selected);
    if (!wasMerged)
        message = tr("Would you like to delete the <b>unmerged</b> branch '%1'?").arg(branchName);

    if (QMessageBox::question(this, tr("Delete Branch"), message, QMessageBox::Yes|QMessageBox::No,
                              wasMerged ? QMessageBox::Yes : QMessageBox::No) == QMessageBox::Yes)
        m_model->removeBranch(selected);
Tobias Hunger's avatar
Tobias Hunger committed
void BranchDialog::diff()
Tobias Hunger's avatar
Tobias Hunger committed
    QString branchName = m_model->branchName(selectedIndex());
    if (branchName.isEmpty())
Tobias Hunger's avatar
Tobias Hunger committed
    GitPlugin::instance()->gitClient()->diffBranch(m_repository, QStringList(), branchName);
Tobias Hunger's avatar
Tobias Hunger committed
void BranchDialog::log()
Tobias Hunger's avatar
Tobias Hunger committed
    QString branchName = m_model->branchName(selectedIndex());
    if (branchName.isEmpty())
Tobias Hunger's avatar
Tobias Hunger committed
    GitPlugin::instance()->gitClient()->graphLog(m_repository, branchName);
void BranchDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

Tobias Hunger's avatar
Tobias Hunger committed
QModelIndex BranchDialog::selectedIndex()
{
    QModelIndexList selected = m_ui->branchView->selectionModel()->selectedIndexes();
    if (selected.isEmpty())
        return QModelIndex();
    return selected.at(0);
}

} // namespace Internal
} // namespace Git