annotate src/confirmcommentdialog.cpp @ 493:75ebcacd3f75

If the locale is C, load English translations
author Chris Cannam
date Thu, 18 Aug 2011 16:37:11 +0100
parents 50920723dd16
children 1c05e7576ea5
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@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@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@109 94 foreach (QString file, files) {
Chris@109 95 text += "&nbsp;&nbsp;&nbsp;" + xmlEncode(file) + "<br>";
Chris@109 96 }
Chris@109 97 text += "</code></qt>";
Chris@298 98
Chris@109 99 return text;
Chris@109 100 }
Chris@109 101
Chris@193 102 bool ConfirmCommentDialog::confirm(QWidget *parent,
Chris@193 103 QString title,
Chris@298 104 QString head,
Chris@193 105 QString text,
Chris@193 106 QString okButtonText)
Chris@193 107 {
Chris@193 108 QMessageBox box(QMessageBox::Question,
Chris@193 109 title,
Chris@298 110 head,
Chris@193 111 QMessageBox::Cancel,
Chris@193 112 parent);
Chris@193 113
Chris@298 114 box.setInformativeText(text);
Chris@298 115
Chris@193 116 QPushButton *ok = box.addButton(QMessageBox::Ok);
Chris@193 117 ok->setText(okButtonText);
Chris@275 118 box.setDefaultButton(QMessageBox::Ok);
Chris@194 119 if (box.exec() == -1) return false;
Chris@194 120 return box.standardButton(box.clickedButton()) == QMessageBox::Ok;
Chris@193 121 }
Chris@193 122
Chris@193 123 bool ConfirmCommentDialog::confirmDangerous(QWidget *parent,
Chris@193 124 QString title,
Chris@298 125 QString head,
Chris@193 126 QString text,
Chris@193 127 QString okButtonText)
Chris@193 128 {
Chris@193 129 QMessageBox box(QMessageBox::Warning,
Chris@193 130 title,
Chris@298 131 head,
Chris@193 132 QMessageBox::Cancel,
Chris@193 133 parent);
Chris@193 134
Chris@298 135 box.setInformativeText(text);
Chris@298 136
Chris@193 137 QPushButton *ok = box.addButton(QMessageBox::Ok);
Chris@193 138 ok->setText(okButtonText);
Chris@275 139 box.setDefaultButton(QMessageBox::Cancel);
Chris@194 140 if (box.exec() == -1) return false;
Chris@194 141 return box.standardButton(box.clickedButton()) == QMessageBox::Ok;
Chris@193 142 }
Chris@193 143
Chris@102 144 bool ConfirmCommentDialog::confirmFilesAction(QWidget *parent,
Chris@102 145 QString title,
Chris@102 146 QString introText,
Chris@102 147 QString introTextWithCount,
Chris@193 148 QStringList files,
Chris@193 149 QString okButtonText)
Chris@102 150 {
Chris@102 151 QString text;
Chris@102 152 if (files.size() <= 10) {
Chris@109 153 text = buildFilesText(introText, files);
Chris@102 154 } else {
Chris@155 155 text = "<qt>" + introTextWithCount + "</qt>";
Chris@102 156 }
Chris@298 157 return confirm(parent, title, text, "", okButtonText);
Chris@102 158 }
Chris@102 159
Chris@109 160 bool ConfirmCommentDialog::confirmDangerousFilesAction(QWidget *parent,
Chris@109 161 QString title,
Chris@109 162 QString introText,
Chris@109 163 QString introTextWithCount,
Chris@193 164 QStringList files,
Chris@193 165 QString okButtonText)
Chris@109 166 {
Chris@109 167 QString text;
Chris@109 168 if (files.size() <= 10) {
Chris@109 169 text = buildFilesText(introText, files);
Chris@109 170 } else {
Chris@155 171 text = "<qt>" + introTextWithCount + "</qt>";
Chris@109 172 }
Chris@298 173 return confirmDangerous(parent, title, text, "", okButtonText);
Chris@109 174 }
Chris@109 175
Chris@109 176 bool ConfirmCommentDialog::confirmAndGetShortComment(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, false,
Chris@193 186 okButtonText);
Chris@109 187 }
Chris@109 188
Chris@109 189 bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent,
Chris@109 190 QString title,
Chris@109 191 QString introText,
Chris@109 192 QString introTextWithCount,
Chris@109 193 QStringList files,
Chris@193 194 QString &comment,
Chris@193 195 QString okButtonText)
Chris@109 196 {
Chris@109 197 return confirmAndComment(parent, title, introText,
Chris@193 198 introTextWithCount, files, comment, true,
Chris@193 199 okButtonText);
Chris@109 200 }
Chris@109 201
Chris@102 202 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent,
Chris@102 203 QString title,
Chris@102 204 QString introText,
Chris@102 205 QString introTextWithCount,
Chris@102 206 QStringList files,
Chris@104 207 QString &comment,
Chris@193 208 bool longComment,
Chris@193 209 QString okButtonText)
Chris@102 210 {
Chris@102 211 QString text;
Chris@102 212 if (files.size() <= 10) {
Chris@109 213 text = buildFilesText(introText, files);
Chris@102 214 } else {
Chris@155 215 text = "<qt>" + introTextWithCount;
Chris@102 216 }
Chris@109 217 text += tr("<p>Please enter your comment:</qt>");
Chris@193 218 return confirmAndComment(parent, title, text, comment, longComment,
Chris@193 219 okButtonText);
Chris@102 220 }
Chris@102 221
Chris@109 222 bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent,
Chris@109 223 QString title,
Chris@109 224 QString introText,
Chris@193 225 QString &comment,
Chris@193 226 QString okButtonText)
Chris@109 227 {
Chris@193 228 return confirmAndComment(parent, title, introText, comment, false,
Chris@193 229 okButtonText);
Chris@109 230 }
Chris@109 231
Chris@109 232 bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent,
Chris@109 233 QString title,
Chris@109 234 QString introText,
Chris@193 235 QString &comment,
Chris@193 236 QString okButtonText)
Chris@109 237 {
Chris@193 238 return confirmAndComment(parent, title, introText, comment, true,
Chris@193 239 okButtonText);
Chris@109 240 }
Chris@109 241
Chris@102 242 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent,
Chris@102 243 QString title,
Chris@102 244 QString introText,
Chris@104 245 QString &comment,
Chris@193 246 bool longComment,
Chris@193 247 QString okButtonText)
Chris@102 248 {
Chris@102 249 bool ok = false;
Chris@104 250 if (!longComment) {
Chris@193 251 QInputDialog d(parent);
Chris@193 252 d.setWindowTitle(title);
Chris@193 253 d.setLabelText(introText);
Chris@193 254 d.setTextValue(comment);
Chris@193 255 d.setOkButtonText(okButtonText);
Chris@193 256 d.setTextEchoMode(QLineEdit::Normal);
Chris@193 257 if (d.exec() == QDialog::Accepted) {
Chris@193 258 comment = d.textValue();
Chris@193 259 ok = true;
Chris@193 260 }
Chris@104 261 } else {
Chris@193 262 ConfirmCommentDialog d(parent, title, introText, comment, okButtonText);
Chris@193 263 if (d.exec() == QDialog::Accepted) {
Chris@193 264 comment = d.getComment();
Chris@104 265 ok = true;
Chris@104 266 }
Chris@104 267 }
Chris@193 268
Chris@102 269 return ok;
Chris@102 270 }