changeselectiondialog.cpp 6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "changeselectiondialog.h"
31 32
#include "gitplugin.h"
#include "gitclient.h"
con's avatar
con committed
33

34
#include <QProcess>
35 36 37 38 39 40 41 42
#include <QFormLayout>
#include <QSpacerItem>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
con's avatar
con committed
43

44 45
namespace Git {
namespace Internal {
con's avatar
con committed
46

47
ChangeSelectionDialog::ChangeSelectionDialog(const QString &workingDirectory, QWidget *parent)
con's avatar
con committed
48
    : QDialog(parent)
49
    , m_process(0)
50 51 52 53 54 55
    , m_workingDirectoryLabel(new QLabel(workingDirectory, this))
    , m_changeNumberEdit(new QLineEdit(this))
    , m_detailsText(new QPlainTextEdit(this))
    , m_showButton(new QPushButton(tr("Show"), this))
    , m_cherryPickButton(new QPushButton(tr("Cherry Pick"), this))
    , m_revertButton(new QPushButton(tr("Revert"), this))
Petar Perisin's avatar
Petar Perisin committed
56
    , m_checkoutButton(new QPushButton(tr("Checkout"), this))
57 58
    , m_cancelButton(new QPushButton(tr("Cancel"), this))
    , m_command(NoCommand)
con's avatar
con committed
59
{
60 61 62 63 64
    bool ok;
    m_gitBinaryPath = GitPlugin::instance()->gitClient()->gitBinaryPath(&ok);
    if (!ok)
        return;

65 66 67 68 69 70 71 72 73 74
    QGridLayout* layout = new QGridLayout(this);
    layout->addWidget(new QLabel(tr("Working Directory:"), this), 0, 0 , 1, 1);
    layout->addWidget(m_workingDirectoryLabel, 0, 1, 1, 1);
    layout->addWidget(new QLabel(tr("Change:"), this),1, 0, 1, 1);
    layout->addWidget(m_changeNumberEdit, 1, 1, 1, 1);
    layout->addWidget(m_detailsText, 2, 0, 1, 2);

    QHBoxLayout* buttonsLine = new QHBoxLayout();
    buttonsLine->addWidget(m_cancelButton);
    buttonsLine->addStretch();
Petar Perisin's avatar
Petar Perisin committed
75
    buttonsLine->addWidget(m_checkoutButton);
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    buttonsLine->addWidget(m_revertButton);
    buttonsLine->addWidget(m_cherryPickButton);
    buttonsLine->addWidget(m_showButton);

    layout->addLayout(buttonsLine, 3, 0 ,1 ,2);

    m_changeNumberEdit->setFocus(Qt::ActiveWindowFocusReason);
    m_changeNumberEdit->setText(QLatin1String("HEAD"));
    m_changeNumberEdit->selectAll();

    setWindowTitle(tr("Select a Git Commit"));
    setGeometry(0, 0, 550, 350);
    adjustPosition(parent);

90
    m_gitEnvironment = GitPlugin::instance()->gitClient()->processEnvironment();
91
    connect(m_changeNumberEdit, SIGNAL(textChanged(QString)),
92
            this, SLOT(recalculateDetails(QString)));
93 94 95
    connect(m_showButton, SIGNAL(clicked()), this, SLOT(acceptShow()));
    connect(m_cherryPickButton, SIGNAL(clicked()), this, SLOT(acceptCherryPick()));
    connect(m_revertButton, SIGNAL(clicked()), this, SLOT(acceptRevert()));
Petar Perisin's avatar
Petar Perisin committed
96
    connect(m_checkoutButton, SIGNAL(clicked()), this, SLOT(acceptCheckout()));
97 98 99
    connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

    recalculateDetails(m_changeNumberEdit->text());
100 101 102 103 104
}

ChangeSelectionDialog::~ChangeSelectionDialog()
{
    delete m_process;
con's avatar
con committed
105 106
}

107 108
QString ChangeSelectionDialog::change() const
{
109
    return m_changeNumberEdit->text();
110 111
}

112
QString ChangeSelectionDialog::workingDirectory() const
113
{
114
    return m_workingDirectoryLabel->text();
115 116
}

117
ChangeCommand ChangeSelectionDialog::command() const
118
{
119
    return m_command;
120 121
}

Petar Perisin's avatar
Petar Perisin committed
122 123 124 125 126 127
void ChangeSelectionDialog::acceptCheckout()
{
    m_command = Checkout;
    accept();
}

128
void ChangeSelectionDialog::acceptCherryPick()
con's avatar
con committed
129
{
130 131 132
    m_command = CherryPick;
    accept();
}
con's avatar
con committed
133

134 135 136 137 138 139 140 141 142 143
void ChangeSelectionDialog::acceptRevert()
{
    m_command = Revert;
    accept();
}

void ChangeSelectionDialog::acceptShow()
{
    m_command = Show;
    accept();
con's avatar
con committed
144
}
145

146 147
//! Set commit message in details
void ChangeSelectionDialog::setDetails(int exitCode)
148
{
149 150 151 152 153 154 155 156 157 158 159 160 161
    if (exitCode == 0) {
        m_detailsText->setPlainText(QString::fromUtf8(m_process->readAllStandardOutput()));
        enableButtons(true);
    } else {
        m_detailsText->setPlainText(tr("Error: Unknown reference"));
    }
}

void ChangeSelectionDialog::enableButtons(bool b)
{
    m_showButton->setEnabled(b);
    m_cherryPickButton->setEnabled(b);
    m_revertButton->setEnabled(b);
162
}
163

164
void ChangeSelectionDialog::recalculateDetails(const QString &ref)
165 166 167 168 169 170
{
    if (m_process) {
        m_process->kill();
        m_process->waitForFinished();
        delete m_process;
    }
171
    enableButtons(false);
172 173 174 175 176 177 178 179

    QStringList args;
    args << QLatin1String("log") << QLatin1String("-n1") << ref;

    m_process = new QProcess(this);
    m_process->setWorkingDirectory(workingDirectory());
    m_process->setProcessEnvironment(m_gitEnvironment);

180
    connect(m_process, SIGNAL(finished(int)), this, SLOT(setDetails(int)));
181 182 183 184

    m_process->start(m_gitBinaryPath, args);
    m_process->closeWriteChannel();
    if (!m_process->waitForStarted())
185
        m_detailsText->setPlainText(tr("Error: Could not start Git."));
186
    else
187
        m_detailsText->setPlainText(tr("Fetching commit data..."));
188 189
}

190 191
} // Internal
} // Git