| Chris@102 | 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */ | 
| Chris@102 | 2 | 
| Chris@102 | 3 /* | 
| Chris@102 | 4     EasyMercurial | 
| Chris@102 | 5 | 
| Chris@102 | 6     Based on hgExplorer by Jari Korhonen | 
| Chris@102 | 7     Copyright (c) 2010 Jari Korhonen | 
| Chris@244 | 8     Copyright (c) 2011 Chris Cannam | 
| Chris@244 | 9     Copyright (c) 2011 Queen Mary, University of London | 
| Chris@102 | 10 | 
| Chris@102 | 11     This program is free software; you can redistribute it and/or | 
| Chris@102 | 12     modify it under the terms of the GNU General Public License as | 
| Chris@102 | 13     published by the Free Software Foundation; either version 2 of the | 
| Chris@102 | 14     License, or (at your option) any later version.  See the file | 
| Chris@102 | 15     COPYING included with this distribution for more information. | 
| Chris@102 | 16 */ | 
| Chris@102 | 17 | 
| Chris@102 | 18 #include "confirmcommentdialog.h" | 
| Chris@109 | 19 #include "common.h" | 
| Chris@102 | 20 | 
| Chris@102 | 21 #include <QMessageBox> | 
| Chris@102 | 22 #include <QInputDialog> | 
| Chris@104 | 23 #include <QGridLayout> | 
| Chris@104 | 24 #include <QLabel> | 
| Chris@104 | 25 #include <QTextEdit> | 
| Chris@104 | 26 #include <QDialogButtonBox> | 
| Chris@102 | 27 | 
| Chris@105 | 28 ConfirmCommentDialog::ConfirmCommentDialog(QWidget *parent, | 
| Chris@105 | 29                                            QString title, | 
| Chris@105 | 30                                            QString introText, | 
| Chris@193 | 31                                            QString initialComment, | 
| Chris@193 | 32                                            QString okButtonText) : | 
| Chris@105 | 33     QDialog(parent) | 
| Chris@105 | 34 { | 
| Chris@105 | 35     setWindowTitle(title); | 
| Chris@105 | 36 | 
| Chris@105 | 37     QGridLayout *layout = new QGridLayout; | 
| Chris@105 | 38     setLayout(layout); | 
| Chris@105 | 39     QLabel *label = new QLabel(introText); | 
| Chris@237 | 40     label->setWordWrap(true); | 
| Chris@105 | 41     layout->addWidget(label, 0, 0); | 
| Chris@105 | 42 | 
| Chris@105 | 43     m_textEdit = new QTextEdit; | 
| Chris@105 | 44     m_textEdit->setAcceptRichText(false); | 
| Chris@105 | 45     m_textEdit->document()->setPlainText(initialComment); | 
| Chris@105 | 46     m_textEdit->setMinimumWidth(360); | 
| Chris@105 | 47     connect(m_textEdit, SIGNAL(textChanged()), this, SLOT(commentChanged())); | 
| Chris@105 | 48     layout->addWidget(m_textEdit, 1, 0); | 
| Chris@105 | 49 | 
| Chris@105 | 50     QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok | | 
| Chris@105 | 51                                                   QDialogButtonBox::Cancel); | 
| Chris@105 | 52     layout->addWidget(bbox, 2, 0); | 
| Chris@105 | 53     m_ok = bbox->button(QDialogButtonBox::Ok); | 
| Chris@275 | 54     m_ok->setDefault(true); | 
| Chris@105 | 55     m_ok->setEnabled(initialComment != ""); | 
| Chris@193 | 56     m_ok->setText(okButtonText); | 
| Chris@275 | 57     bbox->button(QDialogButtonBox::Cancel)->setAutoDefault(false); | 
| Chris@105 | 58 | 
| Chris@105 | 59     connect(bbox, SIGNAL(accepted()), this, SLOT(accept())); | 
| Chris@105 | 60     connect(bbox, SIGNAL(rejected()), this, SLOT(reject())); | 
| Chris@105 | 61 } | 
| Chris@105 | 62 | 
| Chris@105 | 63 void ConfirmCommentDialog::commentChanged() | 
| Chris@105 | 64 { | 
| Chris@105 | 65     m_ok->setEnabled(getComment() != ""); | 
| Chris@105 | 66 } | 
| Chris@105 | 67 | 
| Chris@105 | 68 QString ConfirmCommentDialog::getComment() const | 
| Chris@105 | 69 { | 
| Chris@105 | 70     return m_textEdit->document()->toPlainText(); | 
| Chris@105 | 71 } | 
| Chris@105 | 72 | 
| Chris@109 | 73 QString ConfirmCommentDialog::buildFilesText(QString intro, QStringList files) | 
| Chris@109 | 74 { | 
| Chris@109 | 75     QString text; | 
| Chris@298 | 76 | 
| Chris@298 | 77     if (intro == "") text = "<qt>"; | 
| Chris@298 | 78     else text = "<qt>" + intro + "<p>"; | 
| Chris@298 | 79 | 
| Chris@298 | 80     text += "<code>"; | 
| Chris@109 | 81     foreach (QString file, files) { | 
| Chris@109 | 82         text += "   " + xmlEncode(file) + "<br>"; | 
| Chris@109 | 83     } | 
| Chris@109 | 84     text += "</code></qt>"; | 
| Chris@298 | 85 | 
| Chris@109 | 86     return text; | 
| Chris@109 | 87 } | 
| Chris@109 | 88 | 
| Chris@193 | 89 bool ConfirmCommentDialog::confirm(QWidget *parent, | 
| Chris@193 | 90                                    QString title, | 
| Chris@298 | 91                                    QString head, | 
| Chris@193 | 92                                    QString text, | 
| Chris@193 | 93                                    QString okButtonText) | 
| Chris@193 | 94 { | 
| Chris@193 | 95     QMessageBox box(QMessageBox::Question, | 
| Chris@193 | 96                     title, | 
| Chris@298 | 97                     head, | 
| Chris@193 | 98                     QMessageBox::Cancel, | 
| Chris@193 | 99                     parent); | 
| Chris@193 | 100 | 
| Chris@298 | 101     box.setInformativeText(text); | 
| Chris@298 | 102 | 
| Chris@193 | 103     QPushButton *ok = box.addButton(QMessageBox::Ok); | 
| Chris@193 | 104     ok->setText(okButtonText); | 
| Chris@275 | 105     box.setDefaultButton(QMessageBox::Ok); | 
| Chris@194 | 106     if (box.exec() == -1) return false; | 
| Chris@194 | 107     return box.standardButton(box.clickedButton()) == QMessageBox::Ok; | 
| Chris@193 | 108 } | 
| Chris@193 | 109 | 
| Chris@193 | 110 bool ConfirmCommentDialog::confirmDangerous(QWidget *parent, | 
| Chris@193 | 111                                             QString title, | 
| Chris@298 | 112                                             QString head, | 
| Chris@193 | 113                                             QString text, | 
| Chris@193 | 114                                             QString okButtonText) | 
| Chris@193 | 115 { | 
| Chris@193 | 116     QMessageBox box(QMessageBox::Warning, | 
| Chris@193 | 117                     title, | 
| Chris@298 | 118                     head, | 
| Chris@193 | 119                     QMessageBox::Cancel, | 
| Chris@193 | 120                     parent); | 
| Chris@193 | 121 | 
| Chris@298 | 122     box.setInformativeText(text); | 
| Chris@298 | 123 | 
| Chris@193 | 124     QPushButton *ok = box.addButton(QMessageBox::Ok); | 
| Chris@193 | 125     ok->setText(okButtonText); | 
| Chris@275 | 126     box.setDefaultButton(QMessageBox::Cancel); | 
| Chris@194 | 127     if (box.exec() == -1) return false; | 
| Chris@194 | 128     return box.standardButton(box.clickedButton()) == QMessageBox::Ok; | 
| Chris@193 | 129 } | 
| Chris@193 | 130 | 
| Chris@102 | 131 bool ConfirmCommentDialog::confirmFilesAction(QWidget *parent, | 
| Chris@102 | 132                                               QString title, | 
| Chris@102 | 133                                               QString introText, | 
| Chris@102 | 134                                               QString introTextWithCount, | 
| Chris@193 | 135                                               QStringList files, | 
| Chris@193 | 136                                               QString okButtonText) | 
| Chris@102 | 137 { | 
| Chris@102 | 138     QString text; | 
| Chris@102 | 139     if (files.size() <= 10) { | 
| Chris@109 | 140         text = buildFilesText(introText, files); | 
| Chris@102 | 141     } else { | 
| Chris@155 | 142         text = "<qt>" + introTextWithCount + "</qt>"; | 
| Chris@102 | 143     } | 
| Chris@298 | 144     return confirm(parent, title, text, "", okButtonText); | 
| Chris@102 | 145 } | 
| Chris@102 | 146 | 
| Chris@109 | 147 bool ConfirmCommentDialog::confirmDangerousFilesAction(QWidget *parent, | 
| Chris@109 | 148                                                        QString title, | 
| Chris@109 | 149                                                        QString introText, | 
| Chris@109 | 150                                                        QString introTextWithCount, | 
| Chris@193 | 151                                                        QStringList files, | 
| Chris@193 | 152                                                        QString okButtonText) | 
| Chris@109 | 153 { | 
| Chris@109 | 154     QString text; | 
| Chris@109 | 155     if (files.size() <= 10) { | 
| Chris@109 | 156         text = buildFilesText(introText, files); | 
| Chris@109 | 157     } else { | 
| Chris@155 | 158         text = "<qt>" + introTextWithCount + "</qt>"; | 
| Chris@109 | 159     } | 
| Chris@298 | 160     return confirmDangerous(parent, title, text, "", okButtonText); | 
| Chris@109 | 161 } | 
| Chris@109 | 162 | 
| Chris@109 | 163 bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent, | 
| Chris@109 | 164                                                      QString title, | 
| Chris@109 | 165                                                      QString introText, | 
| Chris@109 | 166                                                      QString introTextWithCount, | 
| Chris@109 | 167                                                      QStringList files, | 
| Chris@193 | 168                                                      QString &comment, | 
| Chris@193 | 169                                                      QString okButtonText) | 
| Chris@109 | 170 { | 
| Chris@109 | 171     return confirmAndComment(parent, title, introText, | 
| Chris@193 | 172                              introTextWithCount, files, comment, false, | 
| Chris@193 | 173                              okButtonText); | 
| Chris@109 | 174 } | 
| Chris@109 | 175 | 
| Chris@109 | 176 bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent, | 
| Chris@109 | 177                                                     QString title, | 
| Chris@109 | 178                                                     QString introText, | 
| Chris@109 | 179                                                     QString introTextWithCount, | 
| Chris@109 | 180                                                     QStringList files, | 
| Chris@193 | 181                                                     QString &comment, | 
| Chris@193 | 182                                                     QString okButtonText) | 
| Chris@109 | 183 { | 
| Chris@109 | 184     return confirmAndComment(parent, title, introText, | 
| Chris@193 | 185                              introTextWithCount, files, comment, true, | 
| Chris@193 | 186                              okButtonText); | 
| Chris@109 | 187 } | 
| Chris@109 | 188 | 
| Chris@102 | 189 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent, | 
| Chris@102 | 190                                              QString title, | 
| Chris@102 | 191                                              QString introText, | 
| Chris@102 | 192                                              QString introTextWithCount, | 
| Chris@102 | 193                                              QStringList files, | 
| Chris@104 | 194                                              QString &comment, | 
| Chris@193 | 195                                              bool longComment, | 
| Chris@193 | 196                                              QString okButtonText) | 
| Chris@102 | 197 { | 
| Chris@102 | 198     QString text; | 
| Chris@102 | 199     if (files.size() <= 10) { | 
| Chris@109 | 200         text = buildFilesText(introText, files); | 
| Chris@102 | 201     } else { | 
| Chris@155 | 202         text = "<qt>" + introTextWithCount; | 
| Chris@102 | 203     } | 
| Chris@109 | 204     text += tr("<p>Please enter your comment:</qt>"); | 
| Chris@193 | 205     return confirmAndComment(parent, title, text, comment, longComment, | 
| Chris@193 | 206                              okButtonText); | 
| Chris@102 | 207 } | 
| Chris@102 | 208 | 
| Chris@109 | 209 bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent, | 
| Chris@109 | 210                                                      QString title, | 
| Chris@109 | 211                                                      QString introText, | 
| Chris@193 | 212                                                      QString &comment, | 
| Chris@193 | 213                                                      QString okButtonText) | 
| Chris@109 | 214 { | 
| Chris@193 | 215     return confirmAndComment(parent, title, introText, comment, false, | 
| Chris@193 | 216                              okButtonText); | 
| Chris@109 | 217 } | 
| Chris@109 | 218 | 
| Chris@109 | 219 bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent, | 
| Chris@109 | 220                                                     QString title, | 
| Chris@109 | 221                                                     QString introText, | 
| Chris@193 | 222                                                     QString &comment, | 
| Chris@193 | 223                                                     QString okButtonText) | 
| Chris@109 | 224 { | 
| Chris@193 | 225     return confirmAndComment(parent, title, introText, comment, true, | 
| Chris@193 | 226                              okButtonText); | 
| Chris@109 | 227 } | 
| Chris@109 | 228 | 
| Chris@102 | 229 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent, | 
| Chris@102 | 230                                              QString title, | 
| Chris@102 | 231                                              QString introText, | 
| Chris@104 | 232                                              QString &comment, | 
| Chris@193 | 233                                              bool longComment, | 
| Chris@193 | 234                                              QString okButtonText) | 
| Chris@102 | 235 { | 
| Chris@102 | 236     bool ok = false; | 
| Chris@104 | 237     if (!longComment) { | 
| Chris@193 | 238         QInputDialog d(parent); | 
| Chris@193 | 239         d.setWindowTitle(title); | 
| Chris@193 | 240         d.setLabelText(introText); | 
| Chris@193 | 241         d.setTextValue(comment); | 
| Chris@193 | 242         d.setOkButtonText(okButtonText); | 
| Chris@193 | 243         d.setTextEchoMode(QLineEdit::Normal); | 
| Chris@193 | 244         if (d.exec() == QDialog::Accepted) { | 
| Chris@193 | 245             comment = d.textValue(); | 
| Chris@193 | 246             ok = true; | 
| Chris@193 | 247         } | 
| Chris@104 | 248     } else { | 
| Chris@193 | 249         ConfirmCommentDialog d(parent, title, introText, comment, okButtonText); | 
| Chris@193 | 250         if (d.exec() == QDialog::Accepted) { | 
| Chris@193 | 251             comment = d.getComment(); | 
| Chris@104 | 252             ok = true; | 
| Chris@104 | 253         } | 
| Chris@104 | 254     } | 
| Chris@193 | 255 | 
| Chris@102 | 256     return ok; | 
| Chris@102 | 257 } |