Warning, file /sdk/cervisia/commitdialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (C) 1999-2002 Bernd Gehrmann
0003  *                          bernd@mail.berlios.de
0004  *  Copyright (c) 2002-2008 Christian Loose <christian.loose@kdemail.net>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0019  */
0020 
0021 #include "commitdialog.h"
0022 
0023 #include <QCheckBox>
0024 #include <QComboBox>
0025 #include <QDialogButtonBox>
0026 #include <QDir>
0027 #include <QFileInfo>
0028 #include <QLabel>
0029 #include <QListWidget>
0030 #include <QPushButton>
0031 #include <QTextStream>
0032 #include <QVBoxLayout>
0033 
0034 #include <KConfig>
0035 #include <KConfigGroup>
0036 #include <KGuiItem>
0037 #include <KHelpClient>
0038 #include <KLocalizedString>
0039 
0040 #include "cvsserviceinterface.h"
0041 #include "diffdialog.h"
0042 #include "logmessageedit.h"
0043 
0044 class CommitListItem : public QListWidgetItem
0045 {
0046 public:
0047     CommitListItem(const QString &text, const QString &fileName, QListWidget *parent = nullptr)
0048         : QListWidgetItem(text, parent)
0049         , m_fileName(fileName)
0050     {
0051     }
0052 
0053     QString fileName() const
0054     {
0055         return m_fileName;
0056     }
0057 
0058 private:
0059     QString m_fileName;
0060 };
0061 
0062 CommitDialog::CommitDialog(KConfig &cfg, OrgKdeCervisia5CvsserviceCvsserviceInterface *service, QWidget *parent)
0063     : QDialog(parent)
0064     , partConfig(cfg)
0065     , cvsService(service)
0066 {
0067     setWindowTitle(i18n("CVS Commit"));
0068     setModal(true);
0069 
0070     auto mainLayout = new QVBoxLayout;
0071     setLayout(mainLayout);
0072 
0073     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help);
0074     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0075     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0076 
0077     user1Button = new QPushButton;
0078     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0079 
0080     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0081     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0082     connect(buttonBox, &QDialogButtonBox::helpRequested, this, &CommitDialog::slotHelp);
0083 
0084     KGuiItem::assign(user1Button, KGuiItem(i18n("&Diff")));
0085 
0086     auto textlabel = new QLabel(i18n("Commit the following &files:"));
0087     mainLayout->addWidget(textlabel);
0088 
0089     m_fileList = new QListWidget;
0090     m_fileList->setEditTriggers(QAbstractItemView::NoEditTriggers);
0091     textlabel->setBuddy(m_fileList);
0092     mainLayout->addWidget(m_fileList);
0093     connect(m_fileList, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(fileSelected(QListWidgetItem *)));
0094     connect(m_fileList, SIGNAL(itemSelectionChanged()), this, SLOT(fileHighlighted()));
0095 
0096     auto archivelabel = new QLabel(i18n("Older &messages:"));
0097     mainLayout->addWidget(archivelabel);
0098 
0099     combo = new QComboBox;
0100     mainLayout->addWidget(combo);
0101     archivelabel->setBuddy(combo);
0102     connect(combo, SIGNAL(activated(int)), this, SLOT(comboActivated(int)));
0103     // make sure that combobox is smaller than the screen
0104     combo->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed));
0105 
0106     auto messagelabel = new QLabel(i18n("&Log message:"));
0107     mainLayout->addWidget(messagelabel);
0108 
0109     edit = new Cervisia::LogMessageEdit(this);
0110     messagelabel->setBuddy(edit);
0111     edit->setFocus();
0112     edit->setMinimumSize(400, 100);
0113     mainLayout->addWidget(edit, 10);
0114 
0115     m_useTemplateChk = new QCheckBox(i18n("Use log message &template"));
0116     mainLayout->addWidget(m_useTemplateChk);
0117     connect(m_useTemplateChk, SIGNAL(clicked()), this, SLOT(useTemplateClicked()));
0118 
0119     mainLayout->addWidget(buttonBox);
0120     okButton->setDefault(true);
0121 
0122     checkForTemplateFile();
0123 
0124     user1Button->setEnabled(false);
0125     connect(user1Button, SIGNAL(clicked()), this, SLOT(diffClicked()));
0126 
0127     KConfigGroup cg(&partConfig, "CommitDialog");
0128     restoreGeometry(cg.readEntry<QByteArray>("geometry", QByteArray()));
0129 }
0130 
0131 CommitDialog::~CommitDialog()
0132 {
0133     KConfigGroup cg(&partConfig, "CommitDialog");
0134     cg.writeEntry("geometry", saveGeometry());
0135     cg.writeEntry("UseTemplate", m_useTemplateChk->isChecked());
0136 }
0137 
0138 void CommitDialog::slotHelp()
0139 {
0140     KHelpClient::invokeHelp(QLatin1String("committingfiles"));
0141 }
0142 
0143 void CommitDialog::setFileList(const QStringList &list)
0144 {
0145     QString currentDirName = QFileInfo(QLatin1String(".")).absoluteFilePath();
0146 
0147     QStringList::ConstIterator it = list.begin();
0148     for (; it != list.end(); ++it) {
0149         // the dot for the root directory is hard to see, so
0150         // we convert it to the absolut path
0151         QString text = (*it != QLatin1String(".") ? *it : currentDirName);
0152 
0153         edit->compObj()->addItem(text);
0154 
0155         auto item = new CommitListItem(text, *it, m_fileList);
0156         item->setCheckState(Qt::Checked);
0157     }
0158 }
0159 
0160 QStringList CommitDialog::fileList() const
0161 {
0162     QStringList files;
0163 
0164     for (int i = 0; i < m_fileList->count(); ++i) {
0165         auto item = static_cast<CommitListItem *>(m_fileList->item(i));
0166         if (item->checkState() & Qt::Checked)
0167             files.append(item->fileName());
0168     }
0169 
0170     return files;
0171 }
0172 
0173 void CommitDialog::setLogMessage(const QString &msg)
0174 {
0175     edit->setText(msg);
0176 
0177     if (m_useTemplateChk->isChecked())
0178         addTemplateText();
0179 }
0180 
0181 QString CommitDialog::logMessage() const
0182 {
0183     return edit->toPlainText();
0184 }
0185 
0186 void CommitDialog::setLogHistory(const QStringList &list)
0187 {
0188     commits = list;
0189 
0190     combo->addItem(i18n("Current"));
0191 
0192     for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
0193         if ((*it).isEmpty())
0194             continue;
0195 
0196         QString txt = *it;
0197         int index = txt.indexOf('\n', 0);
0198         if (index != -1) // Fetch first line
0199         {
0200             txt = txt.mid(0, index);
0201             txt += "...";
0202         }
0203 
0204         combo->addItem(txt);
0205     }
0206 }
0207 
0208 void CommitDialog::comboActivated(int index)
0209 {
0210     if (index == current_index)
0211         return;
0212 
0213     if (index == 0) // Handle current text
0214         edit->setText(current_text);
0215     else {
0216         if (current_index == 0) // Store current text
0217             current_text = edit->toPlainText();
0218 
0219         // Show archived text
0220         edit->setText(commits[index - 1]);
0221     }
0222     current_index = index;
0223 }
0224 
0225 void CommitDialog::fileSelected(QListWidgetItem *item)
0226 {
0227     showDiffDialog(item->text());
0228 }
0229 
0230 void CommitDialog::fileHighlighted()
0231 {
0232     bool isItemSelected = !m_fileList->selectedItems().isEmpty();
0233     user1Button->setEnabled(isItemSelected);
0234 }
0235 
0236 void CommitDialog::diffClicked()
0237 {
0238     QListWidgetItem *item = m_fileList->selectedItems().first();
0239     if (!item)
0240         return;
0241 
0242     showDiffDialog(item->text());
0243 }
0244 
0245 void CommitDialog::showDiffDialog(const QString &fileName)
0246 {
0247     auto l = new DiffDialog(partConfig, this, "diffdialog");
0248 
0249     // disable diff button so user doesn't open the same diff several times (#83018)
0250     user1Button->setEnabled(false);
0251 
0252     if (l->parseCvsDiff(cvsService, fileName, "", ""))
0253         l->show();
0254     else
0255         delete l;
0256 
0257     // re-enable diff button
0258     user1Button->setEnabled(true);
0259 }
0260 
0261 void CommitDialog::useTemplateClicked()
0262 {
0263     if (m_useTemplateChk->isChecked()) {
0264         addTemplateText();
0265     } else {
0266         removeTemplateText();
0267     }
0268 }
0269 
0270 void CommitDialog::checkForTemplateFile()
0271 {
0272     QString filename = QDir::current().absolutePath() + "/CVS/Template";
0273     if (QFile::exists(filename)) {
0274         QFile f(filename);
0275         if (f.open(QIODevice::ReadOnly)) {
0276             QTextStream stream(&f);
0277             m_templateText = stream.readAll();
0278             f.close();
0279 
0280             m_useTemplateChk->setEnabled(true);
0281             KConfigGroup cs(&partConfig, "CommitDialog");
0282             bool check = cs.readEntry("UseTemplate", true);
0283             m_useTemplateChk->setChecked(check);
0284 
0285             addTemplateText();
0286         } else {
0287             m_useTemplateChk->setEnabled(false);
0288         }
0289     } else {
0290         m_useTemplateChk->setEnabled(false);
0291     }
0292 }
0293 
0294 void CommitDialog::addTemplateText()
0295 {
0296     edit->append(m_templateText);
0297     edit->textCursor().movePosition(QTextCursor::Start);
0298     edit->ensureCursorVisible();
0299 }
0300 
0301 void CommitDialog::removeTemplateText()
0302 {
0303     edit->setText(edit->toPlainText().remove(m_templateText));
0304 }
0305 
0306 // Local Variables:
0307 // c-basic-offset: 4
0308 // End: