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

0001 /*
0002     SPDX-FileCopyrightText: 2011 Vishesh Yadav <vishesh3y@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "branchdialog.h"
0008 #include "hgwrapper.h"
0009 
0010 #include <QLabel>
0011 #include <QHBoxLayout>
0012 #include <QVBoxLayout>
0013 #include <QLineEdit>
0014 #include <KComboBox>
0015 #include <KLocalizedString>
0016 #include <QDebug>
0017 #include <KMessageBox>
0018 
0019 HgBranchDialog::HgBranchDialog(QWidget *parent):
0020     DialogBase(QDialogButtonBox::NoButton, parent)
0021 {
0022     // dialog properties
0023     this->setWindowTitle(i18nc("@title:window",
0024                 "<application>Hg</application> Branch"));
0025 
0026     // UI 
0027     QVBoxLayout *vbox = new QVBoxLayout;
0028 
0029     m_currentBranchLabel = new QLabel;
0030     vbox->addWidget(m_currentBranchLabel);
0031 
0032     m_branchComboBox = new KComboBox;
0033     m_branchComboBox->setEditable(true);
0034     vbox->addWidget(m_branchComboBox);
0035 
0036     QHBoxLayout *buttonLayout = new QHBoxLayout;
0037     m_createBranch = new QPushButton(i18n("Create New Branch"));
0038     m_updateBranch = new QPushButton(i18n("Switch Branch"));
0039     buttonLayout->addWidget(m_createBranch);
0040     buttonLayout->addWidget(m_updateBranch);
0041     vbox->addLayout(buttonLayout);
0042 
0043     m_createBranch->setEnabled(false);
0044     m_updateBranch->setEnabled(false);
0045 
0046     updateInitialDialog();
0047     slotUpdateDialog(QString());
0048     layout()->insertLayout(0, vbox);
0049 
0050     slotUpdateDialog(m_branchComboBox->currentText());
0051     
0052     // connections
0053     connect(m_createBranch, &QAbstractButton::clicked,
0054             this, &HgBranchDialog::slotCreateBranch);
0055     connect(m_updateBranch, &QAbstractButton::clicked,
0056             this, &HgBranchDialog::slotSwitch);
0057     connect(m_branchComboBox, &QComboBox::editTextChanged,
0058             this, &HgBranchDialog::slotUpdateDialog);
0059     connect(m_branchComboBox->lineEdit(), &QLineEdit::textChanged,
0060             this, &HgBranchDialog::slotUpdateDialog);
0061 }
0062 
0063 void HgBranchDialog::updateInitialDialog()
0064 {
0065     HgWrapper *hgWrapper = HgWrapper::instance();
0066 
0067     // update label - current branch
0068     QString out;
0069     hgWrapper->executeCommand(QLatin1String("branch"), QStringList(), out);
0070     out = i18n("<b>Current Branch: </b>") + out;
0071     m_currentBranchLabel->setText(out);
0072 
0073     // update combo box
0074     m_branchList = hgWrapper->getBranches();
0075     m_branchComboBox->addItems(m_branchList);
0076 
0077 }
0078 
0079 void HgBranchDialog::slotUpdateDialog(const QString &text)
0080 {
0081     // update pushbuttons
0082     if (text.length() == 0) {
0083         m_createBranch->setEnabled(false);
0084         m_updateBranch->setEnabled(false);
0085     }
0086     else if (m_branchList.contains(text)) {
0087         m_createBranch->setEnabled(false);
0088         m_updateBranch->setEnabled(true);
0089     }
0090     else {
0091         m_createBranch->setEnabled(true);
0092         m_updateBranch->setEnabled(false);
0093     }
0094 }
0095 
0096 void HgBranchDialog::slotSwitch()
0097 {
0098     HgWrapper *hgWrapper = HgWrapper::instance();
0099     QString out;
0100     QStringList args;
0101     args << QLatin1String("-c");
0102     args << m_branchComboBox->currentText();
0103     if (hgWrapper->executeCommand(QLatin1String("update"), args, out)) {
0104         //KMessageBox::information(this, i18n("Updated working directory!"));
0105         done(QDialog::Accepted);
0106     }
0107     else {
0108         KMessageBox::error(this, i18n("Some error occurred"));
0109     }
0110 }
0111 
0112 void HgBranchDialog::slotCreateBranch()
0113 {
0114     HgWrapper *hgWrapper = HgWrapper::instance();
0115     QString out;
0116     QStringList args;
0117     args << m_branchComboBox->currentText();
0118     if (hgWrapper->executeCommand(QLatin1String("branch"), args, out)) {
0119         //KMessageBox::information(this, i18n("Created branch successfully!"));
0120         done(QDialog::Accepted);
0121     }
0122     else {
0123         KMessageBox::error(this, i18n("Some error occurred"));
0124     }
0125 }
0126 
0127 
0128 
0129 #include "moc_branchdialog.cpp"