annotate src/confirmcommentdialog.cpp @ 571:012ba1b83328

Show cancel button with progress bar only when running an operation that it makes sense to cancel (we don't really want people cancelling e.g. initial folder scan because it would leave things in an inconsistent state)
author Chris Cannam
date Thu, 01 Mar 2012 22:53:54 +0000
parents 533519ebc0cb
children ae67ea0af696
rev   line source
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@560 8 Copyright (c) 2012 Chris Cannam
Chris@560 9 Copyright (c) 2012 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@419 20 #include "debug.h"
Chris@102 21
Chris@102 22 #include <QMessageBox>
Chris@102 23 #include <QInputDialog>
Chris@104 24 #include <QGridLayout>
Chris@104 25 #include <QLabel>
Chris@104 26 #include <QTextEdit>
Chris@104 27 #include <QDialogButtonBox>
Chris@102 28
Chris@105 29 ConfirmCommentDialog::ConfirmCommentDialog(QWidget *parent,
Chris@105 30 QString title,
Chris@105 31 QString introText,
Chris@193 32 QString initialComment,
Chris@193 33 QString okButtonText) :
Chris@105 34 QDialog(parent)
Chris@105 35 {
Chris@105 36 setWindowTitle(title);
Chris@105 37
Chris@105 38 QGridLayout *layout = new QGridLayout;
Chris@105 39 setLayout(layout);
Chris@105 40 QLabel *label = new QLabel(introText);
Chris@237 41 label->setWordWrap(true);
Chris@105 42 layout->addWidget(label, 0, 0);
Chris@105 43
Chris@105 44 m_textEdit = new QTextEdit;
Chris@105 45 m_textEdit->setAcceptRichText(false);
Chris@105 46 m_textEdit->document()->setPlainText(initialComment);
Chris@105 47 m_textEdit->setMinimumWidth(360);
Chris@105 48 connect(m_textEdit, SIGNAL(textChanged()), this, SLOT(commentChanged()));
Chris@105 49 layout->addWidget(m_textEdit, 1, 0);
Chris@105 50
Chris@105 51 QDialogButtonBox *bbox = new QDialogButtonBox(QDialogButtonBox::Ok |
Chris@105 52 QDialogButtonBox::Cancel);
Chris@105 53 layout->addWidget(bbox, 2, 0);
Chris@105 54 m_ok = bbox->button(QDialogButtonBox::Ok);
Chris@275 55 m_ok->setDefault(true);
Chris@105 56 m_ok->setEnabled(initialComment != "");
Chris@193 57 m_ok->setText(okButtonText);
Chris@275 58 bbox->button(QDialogButtonBox::Cancel)->setAutoDefault(false);
Chris@105 59
Chris@105 60 connect(bbox, SIGNAL(accepted()), this, SLOT(accept()));
Chris@105 61 connect(bbox, SIGNAL(rejected()), this, SLOT(reject()));
Chris@105 62 }
Chris@105 63
Chris@105 64 void ConfirmCommentDialog::commentChanged()
Chris@105 65 {
Chris@105 66 m_ok->setEnabled(getComment() != "");
Chris@105 67 }
Chris@105 68
Chris@105 69 QString ConfirmCommentDialog::getComment() const
Chris@105 70 {
Chris@419 71 /*
Chris@419 72 DEBUG << "ConfirmCommentDialog: as html is:" << endl;
Chris@419 73 DEBUG << m_textEdit->document()->toHtml() << endl;
Chris@419 74 DEBUG << "ConfirmCommentDialog: as plain text is:" << endl;
Chris@420 75 */
Chris@419 76 QString t = m_textEdit->document()->toPlainText();
Chris@420 77 /*
Chris@419 78 DEBUG << t << endl;
Chris@419 79 DEBUG << "Characters: " << endl;
Chris@419 80 for (int i = 0; i < t.length(); ++i) DEBUG << t[i].unicode();
Chris@419 81 DEBUG << endl;
Chris@420 82 */
Chris@419 83 return t;
Chris@105 84 }
Chris@105 85
Chris@109 86 QString ConfirmCommentDialog::buildFilesText(QString intro, QStringList files)
Chris@109 87 {
Chris@109 88 QString text;
Chris@298 89
Chris@298 90 if (intro == "") text = "<qt>";
Chris@298 91 else text = "<qt>" + intro + "<p>";
Chris@298 92
Chris@298 93 text += "<code>";
Chris@505 94 if (files.empty()) {
Chris@505 95 text += "&nbsp;&nbsp;&nbsp;</code>";
Chris@505 96 text += tr("(no files: metadata only)");
Chris@505 97 } else {
Chris@505 98 foreach (QString file, files) {
Chris@505 99 text += "&nbsp;&nbsp;&nbsp;" + xmlEncode(file) + "<br>";
Chris@505 100 }
Chris@505 101 text += "</code></qt>";
Chris@109 102 }
Chris@298 103
Chris@109 104 return text;
Chris@109 105 }
Chris@109 106
Chris@193 107 bool ConfirmCommentDialog::confirm(QWidget *parent,
Chris@193 108 QString title,
Chris@298 109 QString head,
Chris@193 110 QString text,
Chris@193 111 QString okButtonText)
Chris@193 112 {
Chris@193 113 QMessageBox box(QMessageBox::Question,
Chris@193 114 title,
Chris@298 115 head,
Chris@193 116 QMessageBox::Cancel,
Chris@193 117 parent);
Chris@193 118
Chris@298 119 box.setInformativeText(text);
Chris@298 120
Chris@193 121 QPushButton *ok = box.addButton(QMessageBox::Ok);
Chris@193 122 ok->setText(okButtonText);
Chris@275 123 box.setDefaultButton(QMessageBox::Ok);
Chris@194 124 if (box.exec() == -1) return false;
Chris@194 125 return box.standardButton(box.clickedButton()) == QMessageBox::Ok;
Chris@193 126 }
Chris@193 127
Chris@193 128 bool ConfirmCommentDialog::confirmDangerous(QWidget *parent,
Chris@193 129 QString title,
Chris@298 130 QString head,
Chris@193 131 QString text,
Chris@193 132 QString okButtonText)
Chris@193 133 {
Chris@193 134 QMessageBox box(QMessageBox::Warning,
Chris@193 135 title,
Chris@298 136 head,
Chris@193 137 QMessageBox::Cancel,
Chris@193 138 parent);
Chris@193 139
Chris@298 140 box.setInformativeText(text);
Chris@298 141
Chris@193 142 QPushButton *ok = box.addButton(QMessageBox::Ok);
Chris@193 143 ok->setText(okButtonText);
Chris@275 144 box.setDefaultButton(QMessageBox::Cancel);
Chris@194 145 if (box.exec() == -1) return false;
Chris@194 146 return box.standardButton(box.clickedButton()) == QMessageBox::Ok;
Chris@193 147 }
Chris@193 148
Chris@102 149 bool ConfirmCommentDialog::confirmFilesAction(QWidget *parent,
Chris@102 150 QString title,
Chris@102 151 QString introText,
Chris@102 152 QString introTextWithCount,
Chris@193 153 QStringList files,
Chris@193 154 QString okButtonText)
Chris@102 155 {
Chris@102 156 QString text;
Chris@102 157 if (files.size() <= 10) {
Chris@109 158 text = buildFilesText(introText, files);
Chris@102 159 } else {
Chris@155 160 text = "<qt>" + introTextWithCount + "</qt>";
Chris@102 161 }
Chris@298 162 return confirm(parent, title, text, "", okButtonText);
Chris@102 163 }
Chris@102 164
Chris@109 165 bool ConfirmCommentDialog::confirmDangerousFilesAction(QWidget *parent,
Chris@109 166 QString title,
Chris@109 167 QString introText,
Chris@109 168 QString introTextWithCount,
Chris@193 169 QStringList files,
Chris@193 170 QString okButtonText)
Chris@109 171 {
Chris@109 172 QString text;
Chris@109 173 if (files.size() <= 10) {
Chris@109 174 text = buildFilesText(introText, files);
Chris@109 175 } else {
Chris@155 176 text = "<qt>" + introTextWithCount + "</qt>";
Chris@109 177 }
Chris@298 178 return confirmDangerous(parent, title, text, "", okButtonText);
Chris@109 179 }
Chris@109 180
Chris@109 181 bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent,
Chris@109 182 QString title,
Chris@109 183 QString introText,
Chris@109 184 QString introTextWithCount,
Chris@109 185 QStringList files,
Chris@193 186 QString &comment,
Chris@193 187 QString okButtonText)
Chris@109 188 {
Chris@109 189 return confirmAndComment(parent, title, introText,
Chris@193 190 introTextWithCount, files, comment, false,
Chris@193 191 okButtonText);
Chris@109 192 }
Chris@109 193
Chris@109 194 bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent,
Chris@109 195 QString title,
Chris@109 196 QString introText,
Chris@109 197 QString introTextWithCount,
Chris@109 198 QStringList files,
Chris@193 199 QString &comment,
Chris@193 200 QString okButtonText)
Chris@109 201 {
Chris@109 202 return confirmAndComment(parent, title, introText,
Chris@193 203 introTextWithCount, files, comment, true,
Chris@193 204 okButtonText);
Chris@109 205 }
Chris@109 206
Chris@102 207 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent,
Chris@102 208 QString title,
Chris@102 209 QString introText,
Chris@102 210 QString introTextWithCount,
Chris@102 211 QStringList files,
Chris@104 212 QString &comment,
Chris@193 213 bool longComment,
Chris@193 214 QString okButtonText)
Chris@102 215 {
Chris@102 216 QString text;
Chris@102 217 if (files.size() <= 10) {
Chris@109 218 text = buildFilesText(introText, files);
Chris@102 219 } else {
Chris@155 220 text = "<qt>" + introTextWithCount;
Chris@102 221 }
Chris@109 222 text += tr("<p>Please enter your comment:</qt>");
Chris@193 223 return confirmAndComment(parent, title, text, comment, longComment,
Chris@193 224 okButtonText);
Chris@102 225 }
Chris@102 226
Chris@109 227 bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent,
Chris@109 228 QString title,
Chris@109 229 QString introText,
Chris@193 230 QString &comment,
Chris@193 231 QString okButtonText)
Chris@109 232 {
Chris@193 233 return confirmAndComment(parent, title, introText, comment, false,
Chris@193 234 okButtonText);
Chris@109 235 }
Chris@109 236
Chris@109 237 bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent,
Chris@109 238 QString title,
Chris@109 239 QString introText,
Chris@193 240 QString &comment,
Chris@193 241 QString okButtonText)
Chris@109 242 {
Chris@193 243 return confirmAndComment(parent, title, introText, comment, true,
Chris@193 244 okButtonText);
Chris@109 245 }
Chris@109 246
Chris@102 247 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent,
Chris@102 248 QString title,
Chris@102 249 QString introText,
Chris@104 250 QString &comment,
Chris@193 251 bool longComment,
Chris@193 252 QString okButtonText)
Chris@102 253 {
Chris@102 254 bool ok = false;
Chris@104 255 if (!longComment) {
Chris@193 256 QInputDialog d(parent);
Chris@193 257 d.setWindowTitle(title);
Chris@193 258 d.setLabelText(introText);
Chris@193 259 d.setTextValue(comment);
Chris@193 260 d.setOkButtonText(okButtonText);
Chris@193 261 d.setTextEchoMode(QLineEdit::Normal);
Chris@193 262 if (d.exec() == QDialog::Accepted) {
Chris@193 263 comment = d.textValue();
Chris@193 264 ok = true;
Chris@193 265 }
Chris@104 266 } else {
Chris@193 267 ConfirmCommentDialog d(parent, title, introText, comment, okButtonText);
Chris@193 268 if (d.exec() == QDialog::Accepted) {
Chris@193 269 comment = d.getComment();
Chris@104 270 ok = true;
Chris@104 271 }
Chris@104 272 }
Chris@193 273
Chris@102 274 return ok;
Chris@102 275 }