File indexing completed on 2024-04-21 05:40:59

0001 /*
0002     SPDX-FileCopyrightText: 2010 Sebastian Doerner <sebastian@sebastian-doerner.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "pushdialog.h"
0008 #include "gitwrapper.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KLocalizedString>
0012 
0013 #include <QCheckBox>
0014 #include <QComboBox>
0015 #include <QGroupBox>
0016 #include <QHBoxLayout>
0017 #include <QLabel>
0018 #include <QVBoxLayout>
0019 #include <QDialogButtonBox>
0020 #include <QPushButton>
0021 
0022 PushDialog::PushDialog (QWidget* parent ):
0023     QDialog (parent, Qt::Dialog)
0024 {
0025     this->setWindowTitle(xi18nc("@title:window", "<application>Git</application> Push"));
0026     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0027     QWidget *mainWidget = new QWidget(this);
0028     QVBoxLayout *mainLayout = new QVBoxLayout;
0029     this->setLayout(mainLayout);
0030     mainLayout->addWidget(mainWidget);
0031     QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
0032     okButton->setDefault(true);
0033     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0034     this->connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0035     this->connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0036     m_buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0037     okButton->setText(i18nc("@action:button", "Push"));
0038 
0039     QWidget * boxWidget = new QWidget(this);
0040     QVBoxLayout * boxLayout = new QVBoxLayout(boxWidget);
0041     mainLayout->addWidget(boxWidget);
0042 
0043     //Destination
0044     QGroupBox * destinationGroupBox = new QGroupBox(boxWidget);
0045     mainLayout->addWidget(destinationGroupBox);
0046     boxLayout->addWidget(destinationGroupBox);
0047     destinationGroupBox->setTitle(i18nc("@title:group The remote host", "Destination"));
0048     QHBoxLayout * destinationHBox = new QHBoxLayout(destinationGroupBox);
0049     destinationGroupBox->setLayout(destinationHBox);
0050 
0051     QLabel * remoteLabel = new QLabel(i18nc("@label:listbox a git remote", "Remote:"), destinationGroupBox);
0052     destinationHBox->addWidget(remoteLabel);
0053     m_remoteComboBox = new QComboBox(destinationGroupBox);
0054     destinationHBox->addWidget(m_remoteComboBox);
0055     destinationHBox->addStretch();
0056 
0057     //Branches
0058     QGroupBox* branchesGroupBox = new QGroupBox(boxWidget);
0059     mainLayout->addWidget(branchesGroupBox);
0060     boxLayout->addWidget(branchesGroupBox);
0061     branchesGroupBox->setTitle(i18nc("@title:group", "Branches"));
0062     QHBoxLayout * branchesHBox = new QHBoxLayout(branchesGroupBox);
0063     branchesGroupBox->setLayout(branchesHBox);
0064 
0065     QLabel * localBranchLabel = new QLabel(i18nc("@label:listbox", "Local Branch:"), branchesGroupBox);
0066     branchesHBox->addWidget(localBranchLabel);
0067     m_localBranchComboBox = new QComboBox(branchesGroupBox);
0068     branchesHBox->addWidget(m_localBranchComboBox);
0069 
0070     branchesHBox->addStretch();
0071 
0072     QLabel * remoteBranchLabel = new QLabel(i18nc("@label:listbox", "Remote Branch:"), branchesGroupBox);
0073     branchesHBox->addWidget(remoteBranchLabel);
0074     m_remoteBranchComboBox = new QComboBox(branchesGroupBox);
0075     branchesHBox->addWidget(m_remoteBranchComboBox);
0076 
0077     QGroupBox* optionsGroupBox = new QGroupBox(boxWidget);
0078     mainLayout->addWidget(optionsGroupBox);
0079     boxLayout->addWidget(optionsGroupBox);
0080     optionsGroupBox->setTitle(i18nc("@title:group", "Options"));
0081     QHBoxLayout * optionsHBox = new QHBoxLayout(optionsGroupBox);
0082     optionsGroupBox->setLayout(optionsHBox);
0083     m_forceCheckBox = new QCheckBox(i18nc("@option:check", "Force"), optionsGroupBox);
0084     m_forceCheckBox->setToolTip(i18nc("@info:tooltip", "Proceed even if the remote branch is not an ancestor of the local branch."));
0085     optionsHBox->addWidget(m_forceCheckBox);
0086 
0087     mainLayout->addWidget(m_buttonBox);
0088 
0089     //populate UI
0090     GitWrapper * gitWrapper = GitWrapper::instance();
0091 
0092     //get destinations
0093     QStringList remotes = gitWrapper->pushRemotes();
0094     m_remoteComboBox->addItems(remotes);
0095 
0096     //get branch names
0097     int currentBranchIndex;
0098     const QStringList branches = gitWrapper->branches(&currentBranchIndex);
0099 
0100     for (const QString& branch : branches) {
0101         if (branch.startsWith(QLatin1String("remotes/"))) {
0102             const QString remote = branch.section(QLatin1Char('/'), 1, 1);
0103             const QString name = branch.section(QLatin1Char('/'), 2);
0104             m_remoteBranches[remote] << name;
0105         } else {
0106             m_localBranchComboBox->addItem(branch);
0107         }
0108     }
0109     if (currentBranchIndex >= 0) {
0110         m_localBranchComboBox->setCurrentText(branches.at(currentBranchIndex));
0111     }
0112     remoteSelectionChanged(m_remoteComboBox->currentText());
0113 
0114     //Signals
0115     connect(m_remoteComboBox, SIGNAL(currentIndexChanged(QString)),
0116             this, SLOT(remoteSelectionChanged(QString)));
0117     connect(m_localBranchComboBox, SIGNAL(currentIndexChanged(QString)),
0118             this, SLOT(localBranchSelectionChanged(QString)));
0119 }
0120 
0121 QString PushDialog::destination() const
0122 {
0123     return m_remoteComboBox->currentText();
0124 }
0125 
0126 QString PushDialog::localBranch() const
0127 {
0128     return m_localBranchComboBox->currentText();
0129 }
0130 
0131 QString PushDialog::remoteBranch() const
0132 {
0133     return m_remoteBranchComboBox->currentText();
0134 }
0135 
0136 bool PushDialog::force() const
0137 {
0138     return m_forceCheckBox->isChecked();
0139 }
0140 
0141 void PushDialog::remoteSelectionChanged(const QString& newRemote)
0142 {
0143     m_remoteBranchComboBox->clear();
0144     m_remoteBranchComboBox->addItems(m_remoteBranches.value(newRemote));
0145     localBranchSelectionChanged(m_localBranchComboBox->currentText());
0146 }
0147 
0148 void PushDialog::localBranchSelectionChanged(const QString& newLocalBranch)
0149 {
0150     //select matching remote branch if possible
0151     const int index = m_remoteBranchComboBox->findText(newLocalBranch);
0152     if (index != -1) {
0153         m_remoteBranchComboBox->setCurrentIndex(index);
0154     }
0155     QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
0156     okButton->setEnabled(m_remoteBranchComboBox->count() > 0);
0157 }
0158 
0159 
0160 #include "moc_pushdialog.cpp"