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@88
|
19
|
Chris@88
|
20 #include <QLabel>
|
Chris@88
|
21 #include <QListWidget>
|
Chris@88
|
22 #include <QGridLayout>
|
Chris@93
|
23 #include <QFileInfo>
|
Chris@93
|
24 #include <QApplication>
|
Chris@93
|
25 #include <QDateTime>
|
Chris@93
|
26
|
Chris@93
|
27 #include "debug.h"
|
Chris@88
|
28
|
Chris@88
|
29 FileStatusWidget::FileStatusWidget(QWidget *parent) :
|
Chris@93
|
30 QWidget(parent),
|
Chris@93
|
31 m_dateReference(0)
|
Chris@88
|
32 {
|
Chris@88
|
33 QGridLayout *layout = new QGridLayout;
|
Chris@88
|
34 setLayout(layout);
|
Chris@88
|
35
|
Chris@88
|
36 int row = 0;
|
Chris@88
|
37
|
Chris@88
|
38 layout->addWidget(new QLabel(tr("Local:")), row, 0);
|
Chris@88
|
39 m_localPathLabel = new QLabel;
|
Chris@89
|
40 QFont f(m_localPathLabel->font());
|
Chris@89
|
41 f.setBold(true);
|
Chris@89
|
42 m_localPathLabel->setFont(f);
|
Chris@88
|
43 layout->addWidget(m_localPathLabel, row, 1);
|
Chris@88
|
44
|
Chris@88
|
45 ++row;
|
Chris@88
|
46 layout->addWidget(new QLabel(tr("Remote:")), row, 0);
|
Chris@88
|
47 m_remoteURLLabel = new QLabel;
|
Chris@88
|
48 layout->addWidget(m_remoteURLLabel, row, 1);
|
Chris@88
|
49
|
Chris@106
|
50 ++row;
|
Chris@106
|
51 layout->addWidget(new QLabel(tr("Branch:")), row, 0);
|
Chris@106
|
52 m_branchLabel = new QLabel;
|
Chris@106
|
53 layout->addWidget(m_branchLabel, row, 1);
|
Chris@106
|
54
|
Chris@89
|
55 layout->setColumnStretch(1, 20);
|
Chris@88
|
56
|
Chris@106
|
57 layout->addWidget(new QLabel("<qt><hr></qt>"), ++row, 0, 1, 2);
|
Chris@106
|
58
|
Chris@100
|
59 m_simpleLabels[FileStates::Clean] = tr("Unmodified:");
|
Chris@100
|
60 m_simpleLabels[FileStates::Modified] = tr("Modified:");
|
Chris@100
|
61 m_simpleLabels[FileStates::Added] = tr("Added:");
|
Chris@100
|
62 m_simpleLabels[FileStates::Removed] = tr("Removed:");
|
Chris@100
|
63 m_simpleLabels[FileStates::Missing] = tr("Missing:");
|
Chris@100
|
64 m_simpleLabels[FileStates::Unknown] = tr("Untracked:");
|
Chris@99
|
65
|
Chris@100
|
66 m_descriptions[FileStates::Clean] = tr("You have not changed these files.");
|
Chris@100
|
67 m_descriptions[FileStates::Modified] = tr("You have changed these files since you last committed them.");
|
Chris@100
|
68 m_descriptions[FileStates::Added] = tr("These files will be added to version control next time you commit.");
|
Chris@100
|
69 m_descriptions[FileStates::Removed] = tr("These files will be removed from version control next time you commit.<br>"
|
Chris@100
|
70 "They will not be deleted from the local folder.");
|
Chris@100
|
71 m_descriptions[FileStates::Missing] = tr("These files are recorded in the version control but absent from your working folder.<br>"
|
Chris@100
|
72 "If you deleted them intentionally, select them here and use <b>Remove</b> to tell the version control system about it.");
|
Chris@100
|
73 m_descriptions[FileStates::Unknown] = tr("These files are in your working folder but are not under version control.<br>"
|
Chris@100
|
74 "Select a file and use Add to place it under version control or Ignore to remove it from this list.");
|
Chris@100
|
75
|
Chris@102
|
76 m_highlightExplanation = tr("Files highlighted <font color=red>in red</font> "
|
Chris@100
|
77 "have appeared since your most recent commit or update.");
|
Chris@89
|
78
|
Chris@94
|
79 for (int i = int(FileStates::FirstState);
|
Chris@94
|
80 i <= int(FileStates::LastState); ++i) {
|
Chris@89
|
81
|
Chris@94
|
82 FileStates::State s = FileStates::State(i);
|
Chris@89
|
83
|
Chris@89
|
84 QWidget *box = new QWidget;
|
Chris@89
|
85 QGridLayout *boxlayout = new QGridLayout;
|
Chris@99
|
86 boxlayout->setMargin(0);
|
Chris@89
|
87 box->setLayout(boxlayout);
|
Chris@89
|
88
|
Chris@106
|
89 boxlayout->addItem(new QSpacerItem(5, 5), 0, 0);
|
Chris@101
|
90
|
Chris@101
|
91 boxlayout->addWidget(new QLabel(labelFor(s)), 1, 0);
|
Chris@89
|
92
|
Chris@94
|
93 QListWidget *w = new QListWidget;
|
Chris@94
|
94 m_stateListMap[s] = w;
|
Chris@94
|
95 w->setSelectionMode(QListWidget::ExtendedSelection);
|
Chris@101
|
96 boxlayout->addWidget(w, 2, 0);
|
Chris@89
|
97
|
Chris@95
|
98 connect(w, SIGNAL(itemSelectionChanged()),
|
Chris@95
|
99 this, SLOT(itemSelectionChanged()));
|
Chris@95
|
100
|
Chris@89
|
101 layout->addWidget(box, ++row, 0, 1, 2);
|
Chris@89
|
102 box->hide();
|
Chris@89
|
103 }
|
Chris@89
|
104
|
Chris@89
|
105 layout->setRowStretch(++row, 20);
|
Chris@88
|
106 }
|
Chris@88
|
107
|
Chris@93
|
108 FileStatusWidget::~FileStatusWidget()
|
Chris@93
|
109 {
|
Chris@93
|
110 delete m_dateReference;
|
Chris@93
|
111 }
|
Chris@93
|
112
|
Chris@100
|
113 QString FileStatusWidget::labelFor(FileStates::State s, bool addHighlightExplanation)
|
Chris@100
|
114 {
|
Chris@100
|
115 if (addHighlightExplanation) {
|
Chris@100
|
116 return QString("<qt><b>%1</b><br>%2<br>%3</qt>")
|
Chris@100
|
117 .arg(m_simpleLabels[s])
|
Chris@100
|
118 .arg(m_descriptions[s])
|
Chris@100
|
119 .arg(m_highlightExplanation);
|
Chris@100
|
120 } else {
|
Chris@100
|
121 return QString("<qt><b>%1</b><br>%2</qt>")
|
Chris@100
|
122 .arg(m_simpleLabels[s])
|
Chris@100
|
123 .arg(m_descriptions[s]);
|
Chris@100
|
124 }
|
Chris@100
|
125 }
|
Chris@100
|
126
|
Chris@95
|
127 void FileStatusWidget::itemSelectionChanged()
|
Chris@95
|
128 {
|
Chris@95
|
129 m_selectedFiles.clear();
|
Chris@95
|
130
|
Chris@95
|
131 DEBUG << "FileStatusWidget::itemSelectionChanged" << endl;
|
Chris@95
|
132
|
Chris@95
|
133 foreach (QListWidget *w, m_stateListMap) {
|
Chris@95
|
134 QList<QListWidgetItem *> sel = w->selectedItems();
|
Chris@95
|
135 foreach (QListWidgetItem *i, sel) {
|
Chris@95
|
136 m_selectedFiles.push_back(i->text());
|
Chris@95
|
137 DEBUG << "file " << i->text() << " is selected" << endl;
|
Chris@95
|
138 }
|
Chris@95
|
139 }
|
Chris@95
|
140
|
Chris@95
|
141 emit selectionChanged();
|
Chris@95
|
142 }
|
Chris@95
|
143
|
Chris@94
|
144 void FileStatusWidget::clearSelections()
|
Chris@94
|
145 {
|
Chris@95
|
146 m_selectedFiles.clear();
|
Chris@94
|
147 foreach (QListWidget *w, m_stateListMap) {
|
Chris@94
|
148 w->clearSelection();
|
Chris@94
|
149 }
|
Chris@94
|
150 }
|
Chris@94
|
151
|
Chris@95
|
152 bool FileStatusWidget::haveChangesToCommit() const
|
Chris@95
|
153 {
|
Chris@95
|
154 return !m_fileStates.added().empty() ||
|
Chris@95
|
155 !m_fileStates.removed().empty() ||
|
Chris@95
|
156 !m_fileStates.modified().empty();
|
Chris@95
|
157 }
|
Chris@95
|
158
|
Chris@95
|
159 bool FileStatusWidget::haveSelection() const
|
Chris@95
|
160 {
|
Chris@95
|
161 return !m_selectedFiles.empty();
|
Chris@95
|
162 }
|
Chris@95
|
163
|
Chris@95
|
164 QStringList FileStatusWidget::getAllSelectedFiles() const
|
Chris@95
|
165 {
|
Chris@95
|
166 return m_selectedFiles;
|
Chris@95
|
167 }
|
Chris@95
|
168
|
Chris@95
|
169 QStringList FileStatusWidget::getSelectedCommittableFiles() const
|
Chris@95
|
170 {
|
Chris@95
|
171 QStringList files;
|
Chris@95
|
172 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
173 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
174 case FileStates::Added:
|
Chris@95
|
175 case FileStates::Modified:
|
Chris@95
|
176 case FileStates::Removed:
|
Chris@95
|
177 files.push_back(f);
|
Chris@95
|
178 break;
|
Chris@95
|
179 default: break;
|
Chris@95
|
180 }
|
Chris@95
|
181 }
|
Chris@95
|
182 return files;
|
Chris@95
|
183 }
|
Chris@95
|
184
|
Chris@103
|
185 QStringList FileStatusWidget::getAllCommittableFiles() const
|
Chris@103
|
186 {
|
Chris@103
|
187 QStringList files;
|
Chris@103
|
188 files << m_fileStates.getFilesInState(FileStates::Modified);
|
Chris@103
|
189 files << m_fileStates.getFilesInState(FileStates::Added);
|
Chris@103
|
190 files << m_fileStates.getFilesInState(FileStates::Removed);
|
Chris@103
|
191 return files;
|
Chris@103
|
192 }
|
Chris@103
|
193
|
Chris@95
|
194 QStringList FileStatusWidget::getSelectedAddableFiles() const
|
Chris@95
|
195 {
|
Chris@95
|
196 QStringList files;
|
Chris@95
|
197 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
198 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
199 case FileStates::Unknown:
|
Chris@95
|
200 case FileStates::Removed:
|
Chris@95
|
201 files.push_back(f);
|
Chris@95
|
202 break;
|
Chris@95
|
203 default: break;
|
Chris@95
|
204 }
|
Chris@95
|
205 }
|
Chris@95
|
206 return files;
|
Chris@95
|
207 }
|
Chris@95
|
208
|
Chris@103
|
209 QStringList FileStatusWidget::getAllAddableFiles() const
|
Chris@103
|
210 {
|
Chris@103
|
211 QStringList files;
|
Chris@103
|
212 files << m_fileStates.getFilesInState(FileStates::Removed);
|
Chris@103
|
213 files << m_fileStates.getFilesInState(FileStates::Unknown);
|
Chris@103
|
214 return files;
|
Chris@103
|
215 }
|
Chris@103
|
216
|
Chris@95
|
217 QStringList FileStatusWidget::getSelectedRemovableFiles() const
|
Chris@95
|
218 {
|
Chris@95
|
219 QStringList files;
|
Chris@95
|
220 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
221 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
222 case FileStates::Clean:
|
Chris@95
|
223 case FileStates::Added:
|
Chris@95
|
224 case FileStates::Modified:
|
Chris@95
|
225 case FileStates::Missing:
|
Chris@95
|
226 files.push_back(f);
|
Chris@95
|
227 break;
|
Chris@95
|
228 default: break;
|
Chris@95
|
229 }
|
Chris@95
|
230 }
|
Chris@95
|
231 return files;
|
Chris@95
|
232 }
|
Chris@95
|
233
|
Chris@103
|
234 QStringList FileStatusWidget::getAllRemovableFiles() const
|
Chris@103
|
235 {
|
Chris@103
|
236 QStringList files;
|
Chris@103
|
237 files << m_fileStates.getFilesInState(FileStates::Clean);
|
Chris@103
|
238 files << m_fileStates.getFilesInState(FileStates::Added);
|
Chris@103
|
239 files << m_fileStates.getFilesInState(FileStates::Modified);
|
Chris@103
|
240 files << m_fileStates.getFilesInState(FileStates::Missing);
|
Chris@103
|
241 return files;
|
Chris@103
|
242 }
|
Chris@103
|
243
|
Chris@88
|
244 void
|
Chris@88
|
245 FileStatusWidget::setLocalPath(QString p)
|
Chris@88
|
246 {
|
Chris@88
|
247 m_localPath = p;
|
Chris@88
|
248 m_localPathLabel->setText(p);
|
Chris@93
|
249 delete m_dateReference;
|
Chris@93
|
250 m_dateReference = new QFileInfo(p + "/.hg/dirstate");
|
Chris@93
|
251 if (!m_dateReference->exists() ||
|
Chris@93
|
252 !m_dateReference->isFile() ||
|
Chris@93
|
253 !m_dateReference->isReadable()) {
|
Chris@93
|
254 DEBUG << "FileStatusWidget::setLocalPath: date reference file "
|
Chris@93
|
255 << m_dateReference->absoluteFilePath()
|
Chris@93
|
256 << " does not exist, is not a file, or cannot be read"
|
Chris@93
|
257 << endl;
|
Chris@93
|
258 delete m_dateReference;
|
Chris@93
|
259 m_dateReference = 0;
|
Chris@93
|
260 }
|
Chris@88
|
261 }
|
Chris@88
|
262
|
Chris@88
|
263 void
|
Chris@88
|
264 FileStatusWidget::setRemoteURL(QString r)
|
Chris@88
|
265 {
|
Chris@88
|
266 m_remoteURL = r;
|
Chris@88
|
267 m_remoteURLLabel->setText(r);
|
Chris@88
|
268 }
|
Chris@88
|
269
|
Chris@88
|
270 void
|
Chris@92
|
271 FileStatusWidget::setFileStates(FileStates p)
|
Chris@88
|
272 {
|
Chris@92
|
273 m_fileStates = p;
|
Chris@88
|
274 updateWidgets();
|
Chris@88
|
275 }
|
Chris@88
|
276
|
Chris@88
|
277 void
|
Chris@106
|
278 FileStatusWidget::setBranch(QString b)
|
Chris@106
|
279 {
|
Chris@106
|
280 m_branch = b;
|
Chris@106
|
281 m_branchLabel->setText(b);
|
Chris@106
|
282 }
|
Chris@106
|
283
|
Chris@106
|
284 void
|
Chris@88
|
285 FileStatusWidget::updateWidgets()
|
Chris@88
|
286 {
|
Chris@95
|
287 QDateTime lastInteractionTime;
|
Chris@95
|
288 if (m_dateReference) {
|
Chris@95
|
289 lastInteractionTime = m_dateReference->lastModified();
|
Chris@95
|
290 DEBUG << "reference time: " << lastInteractionTime << endl;
|
Chris@95
|
291 }
|
Chris@95
|
292
|
Chris@95
|
293 QSet<QString> selectedFiles;
|
Chris@95
|
294 foreach (QString f, m_selectedFiles) selectedFiles.insert(f);
|
Chris@95
|
295
|
Chris@94
|
296 foreach (FileStates::State s, m_stateListMap.keys()) {
|
Chris@95
|
297
|
Chris@94
|
298 QListWidget *w = m_stateListMap[s];
|
Chris@94
|
299 w->clear();
|
Chris@95
|
300 QStringList files = m_fileStates.getFilesInState(s);
|
Chris@93
|
301
|
Chris@95
|
302 QStringList highPriority, lowPriority;
|
Chris@95
|
303
|
Chris@95
|
304 foreach (QString file, files) {
|
Chris@95
|
305
|
Chris@95
|
306 bool highlighted = false;
|
Chris@95
|
307
|
Chris@95
|
308 if (s == FileStates::Unknown) {
|
Chris@95
|
309 // We want to highlight untracked files that have appeared
|
Chris@95
|
310 // since the last interaction with the repo
|
Chris@95
|
311 QString fn(m_localPath + "/" + file);
|
Chris@95
|
312 DEBUG << "comparing with " << fn << endl;
|
Chris@95
|
313 QFileInfo fi(fn);
|
Chris@100
|
314 if (fi.exists() && fi.created() > lastInteractionTime) {
|
Chris@95
|
315 DEBUG << "file " << fn << " is newer (" << fi.lastModified()
|
Chris@95
|
316 << ") than reference" << endl;
|
Chris@95
|
317 highlighted = true;
|
Chris@95
|
318 }
|
Chris@95
|
319 }
|
Chris@95
|
320
|
Chris@95
|
321 if (highlighted) {
|
Chris@95
|
322 highPriority.push_back(file);
|
Chris@95
|
323 } else {
|
Chris@95
|
324 lowPriority.push_back(file);
|
Chris@93
|
325 }
|
Chris@93
|
326 }
|
Chris@95
|
327
|
Chris@95
|
328 foreach (QString file, highPriority) {
|
Chris@95
|
329 QListWidgetItem *item = new QListWidgetItem(file);
|
Chris@95
|
330 w->addItem(item);
|
Chris@95
|
331 item->setForeground(Qt::red); //!!! and a nice gold star
|
Chris@95
|
332 item->setSelected(selectedFiles.contains(file));
|
Chris@95
|
333 }
|
Chris@95
|
334
|
Chris@95
|
335 foreach (QString file, lowPriority) {
|
Chris@95
|
336 QListWidgetItem *item = new QListWidgetItem(file);
|
Chris@95
|
337 w->addItem(item);
|
Chris@95
|
338 item->setSelected(selectedFiles.contains(file));
|
Chris@95
|
339 }
|
Chris@95
|
340
|
Chris@100
|
341 setLabelFor(w, s, !highPriority.empty());
|
Chris@100
|
342
|
Chris@95
|
343 w->parentWidget()->setVisible(!files.empty());
|
Chris@93
|
344 }
|
Chris@88
|
345 }
|
Chris@88
|
346
|
Chris@100
|
347 void FileStatusWidget::setLabelFor(QWidget *w, FileStates::State s, bool addHighlight)
|
Chris@100
|
348 {
|
Chris@100
|
349 QString text = labelFor(s, addHighlight);
|
Chris@100
|
350 QWidget *p = w->parentWidget();
|
Chris@100
|
351 QList<QLabel *> ql = p->findChildren<QLabel *>();
|
Chris@100
|
352 if (!ql.empty()) ql[0]->setText(text);
|
Chris@100
|
353 }
|