Chris@88
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@88
|
2
|
Chris@88
|
3 /*
|
Chris@88
|
4 EasyMercurial
|
Chris@88
|
5
|
Chris@88
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@88
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@88
|
8 Copyright (c) 2010 Chris Cannam
|
Chris@88
|
9 Copyright (c) 2010 Queen Mary, University of London
|
Chris@88
|
10
|
Chris@88
|
11 This program is free software; you can redistribute it and/or
|
Chris@88
|
12 modify it under the terms of the GNU General Public License as
|
Chris@88
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@88
|
14 License, or (at your option) any later version. See the file
|
Chris@88
|
15 COPYING included with this distribution for more information.
|
Chris@88
|
16 */
|
Chris@88
|
17
|
Chris@88
|
18 #include "filestatuswidget.h"
|
Chris@115
|
19 #include "debug.h"
|
Chris@115
|
20 #include "multichoicedialog.h"
|
Chris@88
|
21
|
Chris@88
|
22 #include <QLabel>
|
Chris@88
|
23 #include <QListWidget>
|
Chris@88
|
24 #include <QGridLayout>
|
Chris@93
|
25 #include <QFileInfo>
|
Chris@93
|
26 #include <QApplication>
|
Chris@93
|
27 #include <QDateTime>
|
Chris@115
|
28 #include <QPushButton>
|
Chris@88
|
29
|
Chris@88
|
30 FileStatusWidget::FileStatusWidget(QWidget *parent) :
|
Chris@93
|
31 QWidget(parent),
|
Chris@93
|
32 m_dateReference(0)
|
Chris@88
|
33 {
|
Chris@88
|
34 QGridLayout *layout = new QGridLayout;
|
Chris@88
|
35 setLayout(layout);
|
Chris@88
|
36
|
Chris@88
|
37 int row = 0;
|
Chris@88
|
38
|
Chris@115
|
39 layout->addItem(new QSpacerItem(1, 1), row, 0);
|
Chris@115
|
40
|
Chris@115
|
41 ++row;
|
Chris@88
|
42 layout->addWidget(new QLabel(tr("Local:")), row, 0);
|
Chris@88
|
43 m_localPathLabel = new QLabel;
|
Chris@89
|
44 QFont f(m_localPathLabel->font());
|
Chris@89
|
45 f.setBold(true);
|
Chris@89
|
46 m_localPathLabel->setFont(f);
|
Chris@88
|
47 layout->addWidget(m_localPathLabel, row, 1);
|
Chris@88
|
48
|
Chris@88
|
49 ++row;
|
Chris@88
|
50 layout->addWidget(new QLabel(tr("Remote:")), row, 0);
|
Chris@88
|
51 m_remoteURLLabel = new QLabel;
|
Chris@88
|
52 layout->addWidget(m_remoteURLLabel, row, 1);
|
Chris@88
|
53
|
Chris@106
|
54 ++row;
|
Chris@115
|
55 layout->addWidget(new QLabel(tr("State:")), row, 0);
|
Chris@115
|
56 m_stateLabel = new QLabel;
|
Chris@115
|
57 layout->addWidget(m_stateLabel, row, 1, 1, 2);
|
Chris@106
|
58
|
Chris@89
|
59 layout->setColumnStretch(1, 20);
|
Chris@88
|
60
|
Chris@115
|
61 layout->addWidget(new QLabel("<qt><hr></qt>"), ++row, 0, 1, 3);
|
Chris@115
|
62
|
Chris@115
|
63 ++row;
|
Chris@115
|
64 m_noModificationsLabel = new QLabel
|
Chris@162
|
65 (tr("You have no uncommitted changes."));
|
Chris@115
|
66 layout->addWidget(m_noModificationsLabel, row, 1, 1, 2);
|
Chris@106
|
67
|
Chris@100
|
68 m_simpleLabels[FileStates::Clean] = tr("Unmodified:");
|
Chris@100
|
69 m_simpleLabels[FileStates::Modified] = tr("Modified:");
|
Chris@100
|
70 m_simpleLabels[FileStates::Added] = tr("Added:");
|
Chris@100
|
71 m_simpleLabels[FileStates::Removed] = tr("Removed:");
|
Chris@100
|
72 m_simpleLabels[FileStates::Missing] = tr("Missing:");
|
Chris@100
|
73 m_simpleLabels[FileStates::Unknown] = tr("Untracked:");
|
Chris@99
|
74
|
Chris@100
|
75 m_descriptions[FileStates::Clean] = tr("You have not changed these files.");
|
Chris@100
|
76 m_descriptions[FileStates::Modified] = tr("You have changed these files since you last committed them.");
|
Chris@100
|
77 m_descriptions[FileStates::Added] = tr("These files will be added to version control next time you commit.");
|
Chris@100
|
78 m_descriptions[FileStates::Removed] = tr("These files will be removed from version control next time you commit.<br>"
|
Chris@100
|
79 "They will not be deleted from the local folder.");
|
Chris@109
|
80 m_descriptions[FileStates::Missing] = tr("These files are recorded in the version control, but absent from your working folder.<br>"
|
Chris@153
|
81 "If you intended to delete them, select them and use Remove to tell the version control system about it.<br>"
|
Chris@153
|
82 "If you deleted them by accident, select them and use Revert to restore their previous contents.");
|
Chris@100
|
83 m_descriptions[FileStates::Unknown] = tr("These files are in your working folder but are not under version control.<br>"
|
Chris@100
|
84 "Select a file and use Add to place it under version control or Ignore to remove it from this list.");
|
Chris@100
|
85
|
Chris@118
|
86 m_highlightExplanation = tr("Files highlighted <font color=#d40000>in red</font> "
|
Chris@100
|
87 "have appeared since your most recent commit or update.");
|
Chris@89
|
88
|
Chris@94
|
89 for (int i = int(FileStates::FirstState);
|
Chris@94
|
90 i <= int(FileStates::LastState); ++i) {
|
Chris@89
|
91
|
Chris@94
|
92 FileStates::State s = FileStates::State(i);
|
Chris@89
|
93
|
Chris@89
|
94 QWidget *box = new QWidget;
|
Chris@89
|
95 QGridLayout *boxlayout = new QGridLayout;
|
Chris@99
|
96 boxlayout->setMargin(0);
|
Chris@89
|
97 box->setLayout(boxlayout);
|
Chris@89
|
98
|
Chris@106
|
99 boxlayout->addItem(new QSpacerItem(5, 5), 0, 0);
|
Chris@101
|
100
|
Chris@101
|
101 boxlayout->addWidget(new QLabel(labelFor(s)), 1, 0);
|
Chris@89
|
102
|
Chris@94
|
103 QListWidget *w = new QListWidget;
|
Chris@94
|
104 m_stateListMap[s] = w;
|
Chris@94
|
105 w->setSelectionMode(QListWidget::ExtendedSelection);
|
Chris@101
|
106 boxlayout->addWidget(w, 2, 0);
|
Chris@89
|
107
|
Chris@95
|
108 connect(w, SIGNAL(itemSelectionChanged()),
|
Chris@95
|
109 this, SLOT(itemSelectionChanged()));
|
Chris@95
|
110
|
Chris@115
|
111 layout->addWidget(box, ++row, 0, 1, 3);
|
Chris@89
|
112 box->hide();
|
Chris@89
|
113 }
|
Chris@89
|
114
|
Chris@89
|
115 layout->setRowStretch(++row, 20);
|
Chris@88
|
116 }
|
Chris@88
|
117
|
Chris@93
|
118 FileStatusWidget::~FileStatusWidget()
|
Chris@93
|
119 {
|
Chris@93
|
120 delete m_dateReference;
|
Chris@93
|
121 }
|
Chris@93
|
122
|
Chris@100
|
123 QString FileStatusWidget::labelFor(FileStates::State s, bool addHighlightExplanation)
|
Chris@100
|
124 {
|
Chris@100
|
125 if (addHighlightExplanation) {
|
Chris@100
|
126 return QString("<qt><b>%1</b><br>%2<br>%3</qt>")
|
Chris@100
|
127 .arg(m_simpleLabels[s])
|
Chris@100
|
128 .arg(m_descriptions[s])
|
Chris@100
|
129 .arg(m_highlightExplanation);
|
Chris@100
|
130 } else {
|
Chris@100
|
131 return QString("<qt><b>%1</b><br>%2</qt>")
|
Chris@100
|
132 .arg(m_simpleLabels[s])
|
Chris@100
|
133 .arg(m_descriptions[s]);
|
Chris@100
|
134 }
|
Chris@100
|
135 }
|
Chris@100
|
136
|
Chris@95
|
137 void FileStatusWidget::itemSelectionChanged()
|
Chris@95
|
138 {
|
Chris@135
|
139 DEBUG << "FileStatusWidget::itemSelectionChanged" << endl;
|
Chris@135
|
140
|
Chris@135
|
141 QListWidget *list = qobject_cast<QListWidget *>(sender());
|
Chris@135
|
142
|
Chris@135
|
143 if (list) {
|
Chris@135
|
144 foreach (QListWidget *w, m_stateListMap) {
|
Chris@135
|
145 if (w != list) {
|
Chris@135
|
146 w->blockSignals(true);
|
Chris@135
|
147 w->clearSelection();
|
Chris@135
|
148 w->blockSignals(false);
|
Chris@135
|
149 }
|
Chris@135
|
150 }
|
Chris@135
|
151 }
|
Chris@135
|
152
|
Chris@95
|
153 m_selectedFiles.clear();
|
Chris@95
|
154
|
Chris@95
|
155 foreach (QListWidget *w, m_stateListMap) {
|
Chris@95
|
156 QList<QListWidgetItem *> sel = w->selectedItems();
|
Chris@95
|
157 foreach (QListWidgetItem *i, sel) {
|
Chris@95
|
158 m_selectedFiles.push_back(i->text());
|
Chris@95
|
159 DEBUG << "file " << i->text() << " is selected" << endl;
|
Chris@95
|
160 }
|
Chris@95
|
161 }
|
Chris@95
|
162
|
Chris@95
|
163 emit selectionChanged();
|
Chris@95
|
164 }
|
Chris@95
|
165
|
Chris@94
|
166 void FileStatusWidget::clearSelections()
|
Chris@94
|
167 {
|
Chris@95
|
168 m_selectedFiles.clear();
|
Chris@94
|
169 foreach (QListWidget *w, m_stateListMap) {
|
Chris@94
|
170 w->clearSelection();
|
Chris@94
|
171 }
|
Chris@94
|
172 }
|
Chris@94
|
173
|
Chris@95
|
174 bool FileStatusWidget::haveChangesToCommit() const
|
Chris@95
|
175 {
|
Chris@95
|
176 return !m_fileStates.added().empty() ||
|
Chris@95
|
177 !m_fileStates.removed().empty() ||
|
Chris@95
|
178 !m_fileStates.modified().empty();
|
Chris@95
|
179 }
|
Chris@95
|
180
|
Chris@95
|
181 bool FileStatusWidget::haveSelection() const
|
Chris@95
|
182 {
|
Chris@95
|
183 return !m_selectedFiles.empty();
|
Chris@95
|
184 }
|
Chris@95
|
185
|
Chris@95
|
186 QStringList FileStatusWidget::getAllSelectedFiles() const
|
Chris@95
|
187 {
|
Chris@95
|
188 return m_selectedFiles;
|
Chris@95
|
189 }
|
Chris@95
|
190
|
Chris@95
|
191 QStringList FileStatusWidget::getSelectedCommittableFiles() const
|
Chris@95
|
192 {
|
Chris@95
|
193 QStringList files;
|
Chris@95
|
194 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
195 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
196 case FileStates::Added:
|
Chris@95
|
197 case FileStates::Modified:
|
Chris@95
|
198 case FileStates::Removed:
|
Chris@95
|
199 files.push_back(f);
|
Chris@95
|
200 break;
|
Chris@95
|
201 default: break;
|
Chris@95
|
202 }
|
Chris@95
|
203 }
|
Chris@95
|
204 return files;
|
Chris@95
|
205 }
|
Chris@95
|
206
|
Chris@103
|
207 QStringList FileStatusWidget::getAllCommittableFiles() const
|
Chris@103
|
208 {
|
Chris@103
|
209 QStringList files;
|
Chris@103
|
210 files << m_fileStates.getFilesInState(FileStates::Modified);
|
Chris@103
|
211 files << m_fileStates.getFilesInState(FileStates::Added);
|
Chris@103
|
212 files << m_fileStates.getFilesInState(FileStates::Removed);
|
Chris@103
|
213 return files;
|
Chris@103
|
214 }
|
Chris@103
|
215
|
Chris@109
|
216 QStringList FileStatusWidget::getSelectedRevertableFiles() const
|
Chris@109
|
217 {
|
Chris@109
|
218 QStringList files;
|
Chris@109
|
219 foreach (QString f, m_selectedFiles) {
|
Chris@109
|
220 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@109
|
221 case FileStates::Added:
|
Chris@109
|
222 case FileStates::Modified:
|
Chris@109
|
223 case FileStates::Removed:
|
Chris@109
|
224 case FileStates::Missing:
|
Chris@109
|
225 files.push_back(f);
|
Chris@109
|
226 break;
|
Chris@109
|
227 default: break;
|
Chris@109
|
228 }
|
Chris@109
|
229 }
|
Chris@109
|
230 return files;
|
Chris@109
|
231 }
|
Chris@109
|
232
|
Chris@109
|
233 QStringList FileStatusWidget::getAllRevertableFiles() const
|
Chris@109
|
234 {
|
Chris@109
|
235 QStringList files;
|
Chris@109
|
236 files << m_fileStates.getFilesInState(FileStates::Modified);
|
Chris@109
|
237 files << m_fileStates.getFilesInState(FileStates::Added);
|
Chris@109
|
238 files << m_fileStates.getFilesInState(FileStates::Removed);
|
Chris@109
|
239 files << m_fileStates.getFilesInState(FileStates::Missing);
|
Chris@109
|
240 return files;
|
Chris@109
|
241 }
|
Chris@109
|
242
|
Chris@95
|
243 QStringList FileStatusWidget::getSelectedAddableFiles() const
|
Chris@95
|
244 {
|
Chris@95
|
245 QStringList files;
|
Chris@95
|
246 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
247 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
248 case FileStates::Unknown:
|
Chris@95
|
249 case FileStates::Removed:
|
Chris@95
|
250 files.push_back(f);
|
Chris@95
|
251 break;
|
Chris@95
|
252 default: break;
|
Chris@95
|
253 }
|
Chris@95
|
254 }
|
Chris@95
|
255 return files;
|
Chris@95
|
256 }
|
Chris@95
|
257
|
Chris@103
|
258 QStringList FileStatusWidget::getAllAddableFiles() const
|
Chris@103
|
259 {
|
Chris@103
|
260 QStringList files;
|
Chris@103
|
261 files << m_fileStates.getFilesInState(FileStates::Removed);
|
Chris@103
|
262 files << m_fileStates.getFilesInState(FileStates::Unknown);
|
Chris@103
|
263 return files;
|
Chris@103
|
264 }
|
Chris@103
|
265
|
Chris@95
|
266 QStringList FileStatusWidget::getSelectedRemovableFiles() const
|
Chris@95
|
267 {
|
Chris@95
|
268 QStringList files;
|
Chris@95
|
269 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
270 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
271 case FileStates::Clean:
|
Chris@95
|
272 case FileStates::Added:
|
Chris@95
|
273 case FileStates::Modified:
|
Chris@95
|
274 case FileStates::Missing:
|
Chris@95
|
275 files.push_back(f);
|
Chris@95
|
276 break;
|
Chris@95
|
277 default: break;
|
Chris@95
|
278 }
|
Chris@95
|
279 }
|
Chris@95
|
280 return files;
|
Chris@95
|
281 }
|
Chris@95
|
282
|
Chris@103
|
283 QStringList FileStatusWidget::getAllRemovableFiles() const
|
Chris@103
|
284 {
|
Chris@103
|
285 QStringList files;
|
Chris@103
|
286 files << m_fileStates.getFilesInState(FileStates::Clean);
|
Chris@103
|
287 files << m_fileStates.getFilesInState(FileStates::Added);
|
Chris@103
|
288 files << m_fileStates.getFilesInState(FileStates::Modified);
|
Chris@103
|
289 files << m_fileStates.getFilesInState(FileStates::Missing);
|
Chris@103
|
290 return files;
|
Chris@103
|
291 }
|
Chris@103
|
292
|
Chris@88
|
293 void
|
Chris@88
|
294 FileStatusWidget::setLocalPath(QString p)
|
Chris@88
|
295 {
|
Chris@88
|
296 m_localPath = p;
|
Chris@88
|
297 m_localPathLabel->setText(p);
|
Chris@93
|
298 delete m_dateReference;
|
Chris@93
|
299 m_dateReference = new QFileInfo(p + "/.hg/dirstate");
|
Chris@93
|
300 if (!m_dateReference->exists() ||
|
Chris@93
|
301 !m_dateReference->isFile() ||
|
Chris@93
|
302 !m_dateReference->isReadable()) {
|
Chris@93
|
303 DEBUG << "FileStatusWidget::setLocalPath: date reference file "
|
Chris@93
|
304 << m_dateReference->absoluteFilePath()
|
Chris@93
|
305 << " does not exist, is not a file, or cannot be read"
|
Chris@93
|
306 << endl;
|
Chris@93
|
307 delete m_dateReference;
|
Chris@93
|
308 m_dateReference = 0;
|
Chris@93
|
309 }
|
Chris@88
|
310 }
|
Chris@88
|
311
|
Chris@88
|
312 void
|
Chris@88
|
313 FileStatusWidget::setRemoteURL(QString r)
|
Chris@88
|
314 {
|
Chris@88
|
315 m_remoteURL = r;
|
Chris@88
|
316 m_remoteURLLabel->setText(r);
|
Chris@88
|
317 }
|
Chris@88
|
318
|
Chris@88
|
319 void
|
Chris@92
|
320 FileStatusWidget::setFileStates(FileStates p)
|
Chris@88
|
321 {
|
Chris@92
|
322 m_fileStates = p;
|
Chris@88
|
323 updateWidgets();
|
Chris@88
|
324 }
|
Chris@88
|
325
|
Chris@88
|
326 void
|
Chris@115
|
327 FileStatusWidget::setState(QString b)
|
Chris@106
|
328 {
|
Chris@115
|
329 m_state = b;
|
Chris@115
|
330 updateStateLabel();
|
Chris@106
|
331 }
|
Chris@106
|
332
|
Chris@106
|
333 void
|
Chris@88
|
334 FileStatusWidget::updateWidgets()
|
Chris@88
|
335 {
|
Chris@95
|
336 QDateTime lastInteractionTime;
|
Chris@95
|
337 if (m_dateReference) {
|
Chris@95
|
338 lastInteractionTime = m_dateReference->lastModified();
|
Chris@95
|
339 DEBUG << "reference time: " << lastInteractionTime << endl;
|
Chris@95
|
340 }
|
Chris@95
|
341
|
Chris@95
|
342 QSet<QString> selectedFiles;
|
Chris@95
|
343 foreach (QString f, m_selectedFiles) selectedFiles.insert(f);
|
Chris@95
|
344
|
Chris@115
|
345 bool haveAnything = false;
|
Chris@115
|
346
|
Chris@94
|
347 foreach (FileStates::State s, m_stateListMap.keys()) {
|
Chris@95
|
348
|
Chris@94
|
349 QListWidget *w = m_stateListMap[s];
|
Chris@94
|
350 w->clear();
|
Chris@95
|
351 QStringList files = m_fileStates.getFilesInState(s);
|
Chris@93
|
352
|
Chris@95
|
353 QStringList highPriority, lowPriority;
|
Chris@95
|
354
|
Chris@95
|
355 foreach (QString file, files) {
|
Chris@95
|
356
|
Chris@95
|
357 bool highlighted = false;
|
Chris@95
|
358
|
Chris@95
|
359 if (s == FileStates::Unknown) {
|
Chris@95
|
360 // We want to highlight untracked files that have appeared
|
Chris@95
|
361 // since the last interaction with the repo
|
Chris@95
|
362 QString fn(m_localPath + "/" + file);
|
Chris@95
|
363 DEBUG << "comparing with " << fn << endl;
|
Chris@95
|
364 QFileInfo fi(fn);
|
Chris@100
|
365 if (fi.exists() && fi.created() > lastInteractionTime) {
|
Chris@95
|
366 DEBUG << "file " << fn << " is newer (" << fi.lastModified()
|
Chris@95
|
367 << ") than reference" << endl;
|
Chris@95
|
368 highlighted = true;
|
Chris@95
|
369 }
|
Chris@95
|
370 }
|
Chris@95
|
371
|
Chris@95
|
372 if (highlighted) {
|
Chris@95
|
373 highPriority.push_back(file);
|
Chris@95
|
374 } else {
|
Chris@95
|
375 lowPriority.push_back(file);
|
Chris@93
|
376 }
|
Chris@93
|
377 }
|
Chris@95
|
378
|
Chris@95
|
379 foreach (QString file, highPriority) {
|
Chris@95
|
380 QListWidgetItem *item = new QListWidgetItem(file);
|
Chris@95
|
381 w->addItem(item);
|
Chris@118
|
382 item->setForeground(QColor("#d40000")); //!!! and a nice gold star
|
Chris@95
|
383 item->setSelected(selectedFiles.contains(file));
|
Chris@95
|
384 }
|
Chris@95
|
385
|
Chris@95
|
386 foreach (QString file, lowPriority) {
|
Chris@95
|
387 QListWidgetItem *item = new QListWidgetItem(file);
|
Chris@95
|
388 w->addItem(item);
|
Chris@95
|
389 item->setSelected(selectedFiles.contains(file));
|
Chris@95
|
390 }
|
Chris@95
|
391
|
Chris@100
|
392 setLabelFor(w, s, !highPriority.empty());
|
Chris@100
|
393
|
Chris@115
|
394 if (files.empty()) {
|
Chris@115
|
395 w->parentWidget()->hide();
|
Chris@115
|
396 } else {
|
Chris@115
|
397 w->parentWidget()->show();
|
Chris@115
|
398 haveAnything = true;
|
Chris@115
|
399 }
|
Chris@93
|
400 }
|
Chris@115
|
401
|
Chris@115
|
402 m_noModificationsLabel->setVisible(!haveAnything);
|
Chris@115
|
403
|
Chris@115
|
404 updateStateLabel();
|
Chris@88
|
405 }
|
Chris@88
|
406
|
Chris@100
|
407 void FileStatusWidget::setLabelFor(QWidget *w, FileStates::State s, bool addHighlight)
|
Chris@100
|
408 {
|
Chris@100
|
409 QString text = labelFor(s, addHighlight);
|
Chris@100
|
410 QWidget *p = w->parentWidget();
|
Chris@100
|
411 QList<QLabel *> ql = p->findChildren<QLabel *>();
|
Chris@100
|
412 if (!ql.empty()) ql[0]->setText(text);
|
Chris@100
|
413 }
|
Chris@115
|
414
|
Chris@115
|
415 void FileStatusWidget::updateStateLabel()
|
Chris@115
|
416 {
|
Chris@115
|
417 m_stateLabel->setText(m_state);
|
Chris@115
|
418 }
|