Chris@102: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@102: Chris@102: /* Chris@102: EasyMercurial Chris@102: Chris@102: Based on hgExplorer by Jari Korhonen Chris@102: Copyright (c) 2010 Jari Korhonen Chris@244: Copyright (c) 2011 Chris Cannam Chris@244: Copyright (c) 2011 Queen Mary, University of London Chris@102: Chris@102: This program is free software; you can redistribute it and/or Chris@102: modify it under the terms of the GNU General Public License as Chris@102: published by the Free Software Foundation; either version 2 of the Chris@102: License, or (at your option) any later version. See the file Chris@102: COPYING included with this distribution for more information. Chris@102: */ Chris@102: Chris@102: #include "confirmcommentdialog.h" Chris@109: #include "common.h" Chris@102: Chris@102: #include Chris@102: #include Chris@104: #include Chris@104: #include Chris@104: #include Chris@104: #include Chris@102: Chris@105: ConfirmCommentDialog::ConfirmCommentDialog(QWidget *parent, Chris@105: QString title, Chris@105: QString introText, Chris@193: QString initialComment, Chris@193: QString okButtonText) : Chris@105: QDialog(parent) Chris@105: { Chris@105: setWindowTitle(title); Chris@105: Chris@105: QGridLayout *layout = new QGridLayout; Chris@105: setLayout(layout); Chris@105: QLabel *label = new QLabel(introText); Chris@237: label->setWordWrap(true); Chris@105: layout->addWidget(label, 0, 0); Chris@105: Chris@105: m_textEdit = new QTextEdit; Chris@105: m_textEdit->setAcceptRichText(false); Chris@105: m_textEdit->document()->setPlainText(initialComment); Chris@105: m_textEdit->setMinimumWidth(360); Chris@105: connect(m_textEdit, SIGNAL(textChanged()), this, SLOT(commentChanged())); Chris@105: layout->addWidget(m_textEdit, 1, 0); Chris@105: Chris@105: QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok | Chris@105: QDialogButtonBox::Cancel); Chris@105: layout->addWidget(bbox, 2, 0); Chris@105: m_ok = bbox->button(QDialogButtonBox::Ok); Chris@275: m_ok->setDefault(true); Chris@105: m_ok->setEnabled(initialComment != ""); Chris@193: m_ok->setText(okButtonText); Chris@275: bbox->button(QDialogButtonBox::Cancel)->setAutoDefault(false); Chris@105: Chris@105: connect(bbox, SIGNAL(accepted()), this, SLOT(accept())); Chris@105: connect(bbox, SIGNAL(rejected()), this, SLOT(reject())); Chris@105: } Chris@105: Chris@105: void ConfirmCommentDialog::commentChanged() Chris@105: { Chris@105: m_ok->setEnabled(getComment() != ""); Chris@105: } Chris@105: Chris@105: QString ConfirmCommentDialog::getComment() const Chris@105: { Chris@105: return m_textEdit->document()->toPlainText(); Chris@105: } Chris@105: Chris@109: QString ConfirmCommentDialog::buildFilesText(QString intro, QStringList files) Chris@109: { Chris@109: QString text; Chris@298: Chris@298: if (intro == "") text = ""; Chris@298: else text = "" + intro + "

"; Chris@298: Chris@298: text += ""; Chris@109: foreach (QString file, files) { Chris@109: text += "   " + xmlEncode(file) + "
"; Chris@109: } Chris@109: text += "
"; Chris@298: Chris@109: return text; Chris@109: } Chris@109: Chris@193: bool ConfirmCommentDialog::confirm(QWidget *parent, Chris@193: QString title, Chris@298: QString head, Chris@193: QString text, Chris@193: QString okButtonText) Chris@193: { Chris@193: QMessageBox box(QMessageBox::Question, Chris@193: title, Chris@298: head, Chris@193: QMessageBox::Cancel, Chris@193: parent); Chris@193: Chris@298: box.setInformativeText(text); Chris@298: Chris@193: QPushButton *ok = box.addButton(QMessageBox::Ok); Chris@193: ok->setText(okButtonText); Chris@275: box.setDefaultButton(QMessageBox::Ok); Chris@194: if (box.exec() == -1) return false; Chris@194: return box.standardButton(box.clickedButton()) == QMessageBox::Ok; Chris@193: } Chris@193: Chris@193: bool ConfirmCommentDialog::confirmDangerous(QWidget *parent, Chris@193: QString title, Chris@298: QString head, Chris@193: QString text, Chris@193: QString okButtonText) Chris@193: { Chris@193: QMessageBox box(QMessageBox::Warning, Chris@193: title, Chris@298: head, Chris@193: QMessageBox::Cancel, Chris@193: parent); Chris@193: Chris@298: box.setInformativeText(text); Chris@298: Chris@193: QPushButton *ok = box.addButton(QMessageBox::Ok); Chris@193: ok->setText(okButtonText); Chris@275: box.setDefaultButton(QMessageBox::Cancel); Chris@194: if (box.exec() == -1) return false; Chris@194: return box.standardButton(box.clickedButton()) == QMessageBox::Ok; Chris@193: } Chris@193: Chris@102: bool ConfirmCommentDialog::confirmFilesAction(QWidget *parent, Chris@102: QString title, Chris@102: QString introText, Chris@102: QString introTextWithCount, Chris@193: QStringList files, Chris@193: QString okButtonText) Chris@102: { Chris@102: QString text; Chris@102: if (files.size() <= 10) { Chris@109: text = buildFilesText(introText, files); Chris@102: } else { Chris@155: text = "" + introTextWithCount + ""; Chris@102: } Chris@298: return confirm(parent, title, text, "", okButtonText); Chris@102: } Chris@102: Chris@109: bool ConfirmCommentDialog::confirmDangerousFilesAction(QWidget *parent, Chris@109: QString title, Chris@109: QString introText, Chris@109: QString introTextWithCount, Chris@193: QStringList files, Chris@193: QString okButtonText) Chris@109: { Chris@109: QString text; Chris@109: if (files.size() <= 10) { Chris@109: text = buildFilesText(introText, files); Chris@109: } else { Chris@155: text = "" + introTextWithCount + ""; Chris@109: } Chris@298: return confirmDangerous(parent, title, text, "", okButtonText); Chris@109: } Chris@109: Chris@109: bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent, Chris@109: QString title, Chris@109: QString introText, Chris@109: QString introTextWithCount, Chris@109: QStringList files, Chris@193: QString &comment, Chris@193: QString okButtonText) Chris@109: { Chris@109: return confirmAndComment(parent, title, introText, Chris@193: introTextWithCount, files, comment, false, Chris@193: okButtonText); Chris@109: } Chris@109: Chris@109: bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent, Chris@109: QString title, Chris@109: QString introText, Chris@109: QString introTextWithCount, Chris@109: QStringList files, Chris@193: QString &comment, Chris@193: QString okButtonText) Chris@109: { Chris@109: return confirmAndComment(parent, title, introText, Chris@193: introTextWithCount, files, comment, true, Chris@193: okButtonText); Chris@109: } Chris@109: Chris@102: bool ConfirmCommentDialog::confirmAndComment(QWidget *parent, Chris@102: QString title, Chris@102: QString introText, Chris@102: QString introTextWithCount, Chris@102: QStringList files, Chris@104: QString &comment, Chris@193: bool longComment, Chris@193: QString okButtonText) Chris@102: { Chris@102: QString text; Chris@102: if (files.size() <= 10) { Chris@109: text = buildFilesText(introText, files); Chris@102: } else { Chris@155: text = "" + introTextWithCount; Chris@102: } Chris@109: text += tr("

Please enter your comment:"); Chris@193: return confirmAndComment(parent, title, text, comment, longComment, Chris@193: okButtonText); Chris@102: } Chris@102: Chris@109: bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent, Chris@109: QString title, Chris@109: QString introText, Chris@193: QString &comment, Chris@193: QString okButtonText) Chris@109: { Chris@193: return confirmAndComment(parent, title, introText, comment, false, Chris@193: okButtonText); Chris@109: } Chris@109: Chris@109: bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent, Chris@109: QString title, Chris@109: QString introText, Chris@193: QString &comment, Chris@193: QString okButtonText) Chris@109: { Chris@193: return confirmAndComment(parent, title, introText, comment, true, Chris@193: okButtonText); Chris@109: } Chris@109: Chris@102: bool ConfirmCommentDialog::confirmAndComment(QWidget *parent, Chris@102: QString title, Chris@102: QString introText, Chris@104: QString &comment, Chris@193: bool longComment, Chris@193: QString okButtonText) Chris@102: { Chris@102: bool ok = false; Chris@104: if (!longComment) { Chris@193: QInputDialog d(parent); Chris@193: d.setWindowTitle(title); Chris@193: d.setLabelText(introText); Chris@193: d.setTextValue(comment); Chris@193: d.setOkButtonText(okButtonText); Chris@193: d.setTextEchoMode(QLineEdit::Normal); Chris@193: if (d.exec() == QDialog::Accepted) { Chris@193: comment = d.textValue(); Chris@193: ok = true; Chris@193: } Chris@104: } else { Chris@193: ConfirmCommentDialog d(parent, title, introText, comment, okButtonText); Chris@193: if (d.exec() == QDialog::Accepted) { Chris@193: comment = d.getComment(); Chris@104: ok = true; Chris@104: } Chris@104: } Chris@193: Chris@102: return ok; Chris@102: }