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@109
|
76 text = "<qt>" + intro;
|
Chris@109
|
77 text += "<p><code>";
|
Chris@109
|
78 foreach (QString file, files) {
|
Chris@109
|
79 text += " " + xmlEncode(file) + "<br>";
|
Chris@109
|
80 }
|
Chris@109
|
81 text += "</code></qt>";
|
Chris@109
|
82 return text;
|
Chris@109
|
83 }
|
Chris@109
|
84
|
Chris@193
|
85 bool ConfirmCommentDialog::confirm(QWidget *parent,
|
Chris@193
|
86 QString title,
|
Chris@193
|
87 QString text,
|
Chris@193
|
88 QString okButtonText)
|
Chris@193
|
89 {
|
Chris@193
|
90 QMessageBox box(QMessageBox::Question,
|
Chris@193
|
91 title,
|
Chris@193
|
92 text,
|
Chris@193
|
93 QMessageBox::Cancel,
|
Chris@193
|
94 parent);
|
Chris@193
|
95
|
Chris@193
|
96 QPushButton *ok = box.addButton(QMessageBox::Ok);
|
Chris@193
|
97 ok->setText(okButtonText);
|
Chris@275
|
98 box.setDefaultButton(QMessageBox::Ok);
|
Chris@194
|
99 if (box.exec() == -1) return false;
|
Chris@194
|
100 return box.standardButton(box.clickedButton()) == QMessageBox::Ok;
|
Chris@193
|
101 }
|
Chris@193
|
102
|
Chris@193
|
103 bool ConfirmCommentDialog::confirmDangerous(QWidget *parent,
|
Chris@193
|
104 QString title,
|
Chris@193
|
105 QString text,
|
Chris@193
|
106 QString okButtonText)
|
Chris@193
|
107 {
|
Chris@193
|
108 QMessageBox box(QMessageBox::Warning,
|
Chris@193
|
109 title,
|
Chris@193
|
110 text,
|
Chris@193
|
111 QMessageBox::Cancel,
|
Chris@193
|
112 parent);
|
Chris@193
|
113
|
Chris@193
|
114 QPushButton *ok = box.addButton(QMessageBox::Ok);
|
Chris@193
|
115 ok->setText(okButtonText);
|
Chris@275
|
116 box.setDefaultButton(QMessageBox::Cancel);
|
Chris@194
|
117 if (box.exec() == -1) return false;
|
Chris@194
|
118 return box.standardButton(box.clickedButton()) == QMessageBox::Ok;
|
Chris@193
|
119 }
|
Chris@193
|
120
|
Chris@102
|
121 bool ConfirmCommentDialog::confirmFilesAction(QWidget *parent,
|
Chris@102
|
122 QString title,
|
Chris@102
|
123 QString introText,
|
Chris@102
|
124 QString introTextWithCount,
|
Chris@193
|
125 QStringList files,
|
Chris@193
|
126 QString okButtonText)
|
Chris@102
|
127 {
|
Chris@102
|
128 QString text;
|
Chris@102
|
129 if (files.size() <= 10) {
|
Chris@109
|
130 text = buildFilesText(introText, files);
|
Chris@102
|
131 } else {
|
Chris@155
|
132 text = "<qt>" + introTextWithCount + "</qt>";
|
Chris@102
|
133 }
|
Chris@193
|
134 return confirm(parent, title, text, okButtonText);
|
Chris@102
|
135 }
|
Chris@102
|
136
|
Chris@109
|
137 bool ConfirmCommentDialog::confirmDangerousFilesAction(QWidget *parent,
|
Chris@109
|
138 QString title,
|
Chris@109
|
139 QString introText,
|
Chris@109
|
140 QString introTextWithCount,
|
Chris@193
|
141 QStringList files,
|
Chris@193
|
142 QString okButtonText)
|
Chris@109
|
143 {
|
Chris@109
|
144 QString text;
|
Chris@109
|
145 if (files.size() <= 10) {
|
Chris@109
|
146 text = buildFilesText(introText, files);
|
Chris@109
|
147 } else {
|
Chris@155
|
148 text = "<qt>" + introTextWithCount + "</qt>";
|
Chris@109
|
149 }
|
Chris@193
|
150 return confirmDangerous(parent, title, text, okButtonText);
|
Chris@109
|
151 }
|
Chris@109
|
152
|
Chris@109
|
153 bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent,
|
Chris@109
|
154 QString title,
|
Chris@109
|
155 QString introText,
|
Chris@109
|
156 QString introTextWithCount,
|
Chris@109
|
157 QStringList files,
|
Chris@193
|
158 QString &comment,
|
Chris@193
|
159 QString okButtonText)
|
Chris@109
|
160 {
|
Chris@109
|
161 return confirmAndComment(parent, title, introText,
|
Chris@193
|
162 introTextWithCount, files, comment, false,
|
Chris@193
|
163 okButtonText);
|
Chris@109
|
164 }
|
Chris@109
|
165
|
Chris@109
|
166 bool ConfirmCommentDialog::confirmAndGetLongComment(QWidget *parent,
|
Chris@109
|
167 QString title,
|
Chris@109
|
168 QString introText,
|
Chris@109
|
169 QString introTextWithCount,
|
Chris@109
|
170 QStringList files,
|
Chris@193
|
171 QString &comment,
|
Chris@193
|
172 QString okButtonText)
|
Chris@109
|
173 {
|
Chris@109
|
174 return confirmAndComment(parent, title, introText,
|
Chris@193
|
175 introTextWithCount, files, comment, true,
|
Chris@193
|
176 okButtonText);
|
Chris@109
|
177 }
|
Chris@109
|
178
|
Chris@102
|
179 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent,
|
Chris@102
|
180 QString title,
|
Chris@102
|
181 QString introText,
|
Chris@102
|
182 QString introTextWithCount,
|
Chris@102
|
183 QStringList files,
|
Chris@104
|
184 QString &comment,
|
Chris@193
|
185 bool longComment,
|
Chris@193
|
186 QString okButtonText)
|
Chris@102
|
187 {
|
Chris@102
|
188 QString text;
|
Chris@102
|
189 if (files.size() <= 10) {
|
Chris@109
|
190 text = buildFilesText(introText, files);
|
Chris@102
|
191 } else {
|
Chris@155
|
192 text = "<qt>" + introTextWithCount;
|
Chris@102
|
193 }
|
Chris@109
|
194 text += tr("<p>Please enter your comment:</qt>");
|
Chris@193
|
195 return confirmAndComment(parent, title, text, comment, longComment,
|
Chris@193
|
196 okButtonText);
|
Chris@102
|
197 }
|
Chris@102
|
198
|
Chris@109
|
199 bool ConfirmCommentDialog::confirmAndGetShortComment(QWidget *parent,
|
Chris@109
|
200 QString title,
|
Chris@109
|
201 QString introText,
|
Chris@193
|
202 QString &comment,
|
Chris@193
|
203 QString okButtonText)
|
Chris@109
|
204 {
|
Chris@193
|
205 return confirmAndComment(parent, title, introText, comment, false,
|
Chris@193
|
206 okButtonText);
|
Chris@109
|
207 }
|
Chris@109
|
208
|
Chris@109
|
209 bool ConfirmCommentDialog::confirmAndGetLongComment(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, true,
|
Chris@193
|
216 okButtonText);
|
Chris@109
|
217 }
|
Chris@109
|
218
|
Chris@102
|
219 bool ConfirmCommentDialog::confirmAndComment(QWidget *parent,
|
Chris@102
|
220 QString title,
|
Chris@102
|
221 QString introText,
|
Chris@104
|
222 QString &comment,
|
Chris@193
|
223 bool longComment,
|
Chris@193
|
224 QString okButtonText)
|
Chris@102
|
225 {
|
Chris@102
|
226 bool ok = false;
|
Chris@104
|
227 if (!longComment) {
|
Chris@193
|
228 QInputDialog d(parent);
|
Chris@193
|
229 d.setWindowTitle(title);
|
Chris@193
|
230 d.setLabelText(introText);
|
Chris@193
|
231 d.setTextValue(comment);
|
Chris@193
|
232 d.setOkButtonText(okButtonText);
|
Chris@193
|
233 d.setTextEchoMode(QLineEdit::Normal);
|
Chris@193
|
234 if (d.exec() == QDialog::Accepted) {
|
Chris@193
|
235 comment = d.textValue();
|
Chris@193
|
236 ok = true;
|
Chris@193
|
237 }
|
Chris@104
|
238 } else {
|
Chris@193
|
239 ConfirmCommentDialog d(parent, title, introText, comment, okButtonText);
|
Chris@193
|
240 if (d.exec() == QDialog::Accepted) {
|
Chris@193
|
241 comment = d.getComment();
|
Chris@104
|
242 ok = true;
|
Chris@104
|
243 }
|
Chris@104
|
244 }
|
Chris@193
|
245
|
Chris@102
|
246 return ok;
|
Chris@102
|
247 }
|