File: checkoutdialog.cpp

package info (click to toggle)
dolphin-plugins 4%3A16.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 792 kB
  • ctags: 870
  • sloc: cpp: 8,133; sh: 7; makefile: 3
file content (260 lines) | stat: -rw-r--r-- 10,633 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/******************************************************************************
 *   Copyright (C) 2010 by Sebastian Doerner <sebastian@sebastian-doerner.de> *
 *                                                                            *
 *   This program is free software; you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published by     *
 *   the Free Software Foundation; either version 2 of the License, or        *
 *   (at your option) any later version.                                      *
 *                                                                            *
 *   This program is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *   GNU General Public License for more details.                             *
 *                                                                            *
 *   You should have received a copy of the GNU General Public License        *
 *   along with this program; if not, write to the                            *
 *   Free Software Foundation, Inc.,                                          *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA               *
 ******************************************************************************/

#include "checkoutdialog.h"
#include "gitwrapper.h"

#include <kcombobox.h>
#include <kdialog.h>
#include <klineedit.h>
#include <klocale.h>

#include <QCheckBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QRadioButton>
#include <QString>
#include <QVBoxLayout>

CheckoutDialog::CheckoutDialog(QWidget* parent):
    KDialog(parent, Qt::Dialog),
    m_userEditedNewBranchName(false)
{
    //branch/tag selection
    this->setCaption(xi18nc("@title:window", "<application>Git</application> Checkout"));
    this->setButtons(KDialog::Ok | KDialog::Cancel);
    this->setDefaultButton(KDialog::Ok);
    this->setButtonText(KDialog::Ok, i18nc("@action:button", "Checkout"));

    QWidget *boxWidget = new QWidget(this);
    QVBoxLayout *boxLayout = new QVBoxLayout(boxWidget);
    this->setMainWidget(boxWidget);

    m_branchSelectGroupBox = new QGroupBox(boxWidget);
    boxLayout->addWidget(m_branchSelectGroupBox);

    QGridLayout * gridLayout = new QGridLayout(m_branchSelectGroupBox);
    m_branchSelectGroupBox->setLayout(gridLayout);

    m_branchRadioButton = new QRadioButton(i18nc("@option:radio Git Checkout","Branch:"), m_branchSelectGroupBox);
    m_branchRadioButton->setChecked(true);
    gridLayout->addWidget(m_branchRadioButton,0,0);
    m_branchRadioButton->setFocus();
    m_branchComboBox = new KComboBox(false, m_branchSelectGroupBox);
    gridLayout->addWidget(m_branchComboBox,0,1);

    QRadioButton * tagRadioButton = new QRadioButton(i18nc("@option:radio Git Checkout", "Tag:"), m_branchSelectGroupBox);
    gridLayout->addWidget(tagRadioButton,1,0);
    m_tagComboBox = new KComboBox(false, m_branchSelectGroupBox);
    m_tagComboBox->setEnabled(false);
    gridLayout->addWidget(m_tagComboBox,1,1);

    //options
    QGroupBox * optionsGroupBox = new QGroupBox(boxWidget);
    boxLayout->addWidget(optionsGroupBox);
    optionsGroupBox->setTitle(i18nc("@title:group", "Options"));

    QGridLayout * optionsGridLayout = new QGridLayout(optionsGroupBox);
    optionsGroupBox->setLayout(optionsGridLayout);

    m_newBranchCheckBox = new QCheckBox(i18nc("@option:check", "Create New Branch: "), optionsGroupBox);
    m_newBranchCheckBox->setToolTip(i18nc("@info:tooltip", "Create a new branch based on a selected branch or tag."));
    optionsGridLayout->addWidget(m_newBranchCheckBox, 0,0);

    m_newBranchName = new KLineEdit(optionsGroupBox);
    m_newBranchName->setMinimumWidth(150);
    m_newBranchName->setClearButtonShown(true);
    optionsGridLayout->addWidget(m_newBranchName, 0, 1);

    //initialize alternate color scheme for errors
    m_errorColors = m_newBranchName->palette();
    m_errorColors.setColor(QPalette::Normal, QPalette::Base, Qt::red);
    m_errorColors.setColor(QPalette::Inactive, QPalette::Base, Qt::red);

    m_forceCheckBox = new QCheckBox(i18nc("@option:check", "Force"), optionsGroupBox);
    m_forceCheckBox->setToolTip(i18nc("@info:tooltip", "Discard local changes."));
    optionsGridLayout->addWidget(m_forceCheckBox, 1,0);

    //get branch names
    GitWrapper * gitWrapper = GitWrapper::instance();
    int currentBranchIndex;
    const QStringList branches = gitWrapper->branches(&currentBranchIndex);

    m_branchComboBox->addItems(branches);
    if (currentBranchIndex == -1) {
        m_branchComboBox->insertItem(0, "(no branch)");
        m_branchComboBox->setCurrentIndex(0);
    } else {
        m_branchComboBox->setCurrentIndex(currentBranchIndex);
    }
    setDefaultNewBranchName(m_branchComboBox->currentText());

    //keep local branches to prevent creating an equally named new branch
    foreach (const QString& b, branches) {
        if (!b.startsWith(QLatin1String("remotes/"))) {
            //you CAN create local branches called "remotes/...", but since no sane person
            //would do that, we save the effort of another call to "git branch"
            m_branchNames.insert(b);
        }
    }

    //get tag names
    const QStringList tags = gitWrapper->tags();
    m_tagComboBox->addItems(tags);
    m_tagComboBox->setCurrentIndex(m_tagComboBox->count()-1);
    if (m_tagComboBox->count() == 0){
        tagRadioButton->setEnabled(false);
        const QString tooltip = i18nc("@info:tooltip", "There are no tags in this repository.");
        tagRadioButton->setToolTip(tooltip);
        m_tagComboBox->setToolTip(tooltip);
    }

    //signals
    connect(m_branchRadioButton, SIGNAL(toggled(bool)),
            this, SLOT(branchRadioButtonToggled(bool)));
    connect(m_branchComboBox, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(setDefaultNewBranchName(QString)));
    connect(m_branchComboBox, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(setOkButtonState()));
    connect(m_tagComboBox, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(setDefaultNewBranchName(QString)));
    connect(m_newBranchCheckBox, SIGNAL(stateChanged(int)),
             this, SLOT(newBranchCheckBoxStateToggled(int)));
    connect(m_newBranchName, SIGNAL(textChanged(QString)),
            this, SLOT(setOkButtonState()));
    connect(m_newBranchName, SIGNAL(textEdited(QString)),
            this, SLOT(noteUserEditedNewBranchName()));
    //set initial widget states
    newBranchCheckBoxStateToggled(Qt::Unchecked);
}

QString CheckoutDialog::checkoutIdentifier() const
{
    QString identifier = m_branchComboBox->isEnabled() ? m_branchComboBox->currentText() : m_tagComboBox->currentText();
    if (identifier.length()==0 || identifier.at(0)=='(') {
        identifier = "";
    }
    return identifier;
}

bool CheckoutDialog::force() const
{
    return m_forceCheckBox->isChecked();
}

QString CheckoutDialog::newBranchName() const
{
    if (m_newBranchCheckBox->isChecked()) {
        return m_newBranchName->text().trimmed();
    } else {
        return QString();
    }
}

void CheckoutDialog::branchRadioButtonToggled(bool checked)
{
    m_branchComboBox->setEnabled(checked);
    m_tagComboBox->setEnabled(!checked);
    setDefaultNewBranchName(checked ? m_branchComboBox->currentText() : m_tagComboBox->currentText());
    setOkButtonState();
}

void CheckoutDialog::newBranchCheckBoxStateToggled(int state)
{
    m_newBranchName->setEnabled(state == Qt::Checked);
    m_branchSelectGroupBox->setTitle(
        state == Qt::Checked ?
        i18nc("@title:group", "Branch Base") :
        i18nc("@title:group", "Checkout"));
    if (state == Qt::Checked) {
        m_newBranchName->setFocus(Qt::TabFocusReason);
    }
    setOkButtonState();
}

void CheckoutDialog::setOkButtonState()
{
    //default to enabled
    bool enableButton = true;
    bool newNameError = false;

    //------------disable on these conditions---------------//
    if (m_newBranchCheckBox->isChecked()) {
        const QString newBranchName = m_newBranchName->text().trimmed();
        if (newBranchName.isEmpty()){
            enableButton = false;
            newNameError = true;
            const QString tt = i18nc("@info:tooltip", "You must enter a valid name for the new branch first.");
            m_newBranchName->setToolTip(tt);
            this->setButtonToolTip(KDialog::Ok, tt);
        }
        if (m_branchNames.contains(newBranchName)) {
            enableButton = false;
            newNameError = true;
            const QString tt = i18nc("@info:tooltip", "A branch with the name '%1' already exists.", newBranchName);
            m_newBranchName->setToolTip(tt);
            this->setButtonToolTip(KDialog::Ok, tt);
        }
        if (newBranchName.contains(QRegExp("\\s"))) {
            enableButton = false;
            newNameError = true;
            const QString tt = i18nc("@info:tooltip", "Branch names may not contain any whitespace.");
            m_newBranchName->setToolTip(tt);
            this->setButtonToolTip(KDialog::Ok, tt);
        }
    } //if we create a new branch and no valid branch is selected we create one based on the currently checked out version
    else if (m_branchRadioButton->isChecked() && m_branchComboBox->currentText().at(0) == '('){
        enableButton = false;
        this->setButtonToolTip(KDialog::Ok, i18nc("@info:tooltip", "You must select a valid branch first."));
    }
    //------------------------------------------------------//

    setLineEditErrorModeActive(newNameError);
    this->enableButtonOk(enableButton);
    if (!newNameError) {
        m_newBranchName->setToolTip(QString());
    }
    if (enableButton) {
        this->setButtonToolTip(KDialog::Ok, QString());
    }
}

void CheckoutDialog::setDefaultNewBranchName(const QString& baseBranchName)
{
    if (!m_userEditedNewBranchName) {
        if (baseBranchName.startsWith('(')) {
            m_newBranchName->setText("");
        } else {
            m_newBranchName->setText(i18nc("@item:intext Prepended to the current branch name "
            "to get the default name for a newly created branch", "branch") + '_' + baseBranchName);
        }
    }
}

void CheckoutDialog::noteUserEditedNewBranchName()
{
    m_userEditedNewBranchName = true;
}

void CheckoutDialog::setLineEditErrorModeActive(bool active)
{
    m_newBranchName->setPalette(active ? m_errorColors : QPalette());
}

#include "checkoutdialog.moc"