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@89
|
50 layout->setColumnStretch(1, 20);
|
Chris@88
|
51
|
Chris@99
|
52 layout->addItem(new QSpacerItem(5, 8), ++row, 0);
|
Chris@99
|
53
|
Chris@94
|
54 QMap<FileStates::State, QString> labels;
|
Chris@94
|
55 labels[FileStates::Clean] = tr("Unmodified files:");
|
Chris@94
|
56 labels[FileStates::Modified] = tr("Modified files:");
|
Chris@94
|
57 labels[FileStates::Added] = tr("Added files:");
|
Chris@94
|
58 labels[FileStates::Removed] = tr("Removed files:");
|
Chris@94
|
59 labels[FileStates::Missing] = tr("Missing files:");
|
Chris@94
|
60 labels[FileStates::Unknown] = tr("Untracked files:");
|
Chris@89
|
61
|
Chris@94
|
62 for (int i = int(FileStates::FirstState);
|
Chris@94
|
63 i <= int(FileStates::LastState); ++i) {
|
Chris@89
|
64
|
Chris@94
|
65 FileStates::State s = FileStates::State(i);
|
Chris@89
|
66
|
Chris@89
|
67 QWidget *box = new QWidget;
|
Chris@89
|
68 QGridLayout *boxlayout = new QGridLayout;
|
Chris@99
|
69 boxlayout->setMargin(0);
|
Chris@89
|
70 box->setLayout(boxlayout);
|
Chris@89
|
71
|
Chris@94
|
72 boxlayout->addWidget(new QLabel(labels[s]), 0, 0);
|
Chris@89
|
73
|
Chris@94
|
74 QListWidget *w = new QListWidget;
|
Chris@94
|
75 m_stateListMap[s] = w;
|
Chris@94
|
76 w->setSelectionMode(QListWidget::ExtendedSelection);
|
Chris@94
|
77 boxlayout->addWidget(w, 1, 0);
|
Chris@89
|
78
|
Chris@95
|
79 connect(w, SIGNAL(itemSelectionChanged()),
|
Chris@95
|
80 this, SLOT(itemSelectionChanged()));
|
Chris@95
|
81
|
Chris@89
|
82 layout->addWidget(box, ++row, 0, 1, 2);
|
Chris@89
|
83 box->hide();
|
Chris@89
|
84 }
|
Chris@89
|
85
|
Chris@89
|
86 layout->setRowStretch(++row, 20);
|
Chris@88
|
87 }
|
Chris@88
|
88
|
Chris@93
|
89 FileStatusWidget::~FileStatusWidget()
|
Chris@93
|
90 {
|
Chris@93
|
91 delete m_dateReference;
|
Chris@93
|
92 }
|
Chris@93
|
93
|
Chris@95
|
94 void FileStatusWidget::itemSelectionChanged()
|
Chris@95
|
95 {
|
Chris@95
|
96 m_selectedFiles.clear();
|
Chris@95
|
97
|
Chris@95
|
98 DEBUG << "FileStatusWidget::itemSelectionChanged" << endl;
|
Chris@95
|
99
|
Chris@95
|
100 foreach (QListWidget *w, m_stateListMap) {
|
Chris@95
|
101 QList<QListWidgetItem *> sel = w->selectedItems();
|
Chris@95
|
102 foreach (QListWidgetItem *i, sel) {
|
Chris@95
|
103 m_selectedFiles.push_back(i->text());
|
Chris@95
|
104 DEBUG << "file " << i->text() << " is selected" << endl;
|
Chris@95
|
105 }
|
Chris@95
|
106 }
|
Chris@95
|
107
|
Chris@95
|
108 emit selectionChanged();
|
Chris@95
|
109 }
|
Chris@95
|
110
|
Chris@94
|
111 void FileStatusWidget::clearSelections()
|
Chris@94
|
112 {
|
Chris@95
|
113 m_selectedFiles.clear();
|
Chris@94
|
114 foreach (QListWidget *w, m_stateListMap) {
|
Chris@94
|
115 w->clearSelection();
|
Chris@94
|
116 }
|
Chris@94
|
117 }
|
Chris@94
|
118
|
Chris@95
|
119 bool FileStatusWidget::haveChangesToCommit() const
|
Chris@95
|
120 {
|
Chris@95
|
121 return !m_fileStates.added().empty() ||
|
Chris@95
|
122 !m_fileStates.removed().empty() ||
|
Chris@95
|
123 !m_fileStates.modified().empty();
|
Chris@95
|
124 }
|
Chris@95
|
125
|
Chris@95
|
126 bool FileStatusWidget::haveSelection() const
|
Chris@95
|
127 {
|
Chris@95
|
128 return !m_selectedFiles.empty();
|
Chris@95
|
129 }
|
Chris@95
|
130
|
Chris@95
|
131 QStringList FileStatusWidget::getAllSelectedFiles() const
|
Chris@95
|
132 {
|
Chris@95
|
133 return m_selectedFiles;
|
Chris@95
|
134 }
|
Chris@95
|
135
|
Chris@95
|
136 QStringList FileStatusWidget::getSelectedCommittableFiles() const
|
Chris@95
|
137 {
|
Chris@95
|
138 QStringList files;
|
Chris@95
|
139 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
140 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
141 case FileStates::Added:
|
Chris@95
|
142 case FileStates::Modified:
|
Chris@95
|
143 case FileStates::Removed:
|
Chris@95
|
144 files.push_back(f);
|
Chris@95
|
145 break;
|
Chris@95
|
146 default: break;
|
Chris@95
|
147 }
|
Chris@95
|
148 }
|
Chris@95
|
149 return files;
|
Chris@95
|
150 }
|
Chris@95
|
151
|
Chris@95
|
152 QStringList FileStatusWidget::getSelectedAddableFiles() const
|
Chris@95
|
153 {
|
Chris@95
|
154 QStringList files;
|
Chris@95
|
155 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
156 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
157 case FileStates::Unknown:
|
Chris@95
|
158 case FileStates::Removed:
|
Chris@95
|
159 files.push_back(f);
|
Chris@95
|
160 break;
|
Chris@95
|
161 default: break;
|
Chris@95
|
162 }
|
Chris@95
|
163 }
|
Chris@95
|
164 return files;
|
Chris@95
|
165 }
|
Chris@95
|
166
|
Chris@95
|
167 QStringList FileStatusWidget::getSelectedRemovableFiles() const
|
Chris@95
|
168 {
|
Chris@95
|
169 QStringList files;
|
Chris@95
|
170 foreach (QString f, m_selectedFiles) {
|
Chris@95
|
171 switch (m_fileStates.getStateOfFile(f)) {
|
Chris@95
|
172 case FileStates::Clean:
|
Chris@95
|
173 case FileStates::Added:
|
Chris@95
|
174 case FileStates::Modified:
|
Chris@95
|
175 case FileStates::Missing:
|
Chris@95
|
176 files.push_back(f);
|
Chris@95
|
177 break;
|
Chris@95
|
178 default: break;
|
Chris@95
|
179 }
|
Chris@95
|
180 }
|
Chris@95
|
181 return files;
|
Chris@95
|
182 }
|
Chris@95
|
183
|
Chris@88
|
184 void
|
Chris@88
|
185 FileStatusWidget::setLocalPath(QString p)
|
Chris@88
|
186 {
|
Chris@88
|
187 m_localPath = p;
|
Chris@88
|
188 m_localPathLabel->setText(p);
|
Chris@93
|
189 delete m_dateReference;
|
Chris@93
|
190 m_dateReference = new QFileInfo(p + "/.hg/dirstate");
|
Chris@93
|
191 if (!m_dateReference->exists() ||
|
Chris@93
|
192 !m_dateReference->isFile() ||
|
Chris@93
|
193 !m_dateReference->isReadable()) {
|
Chris@93
|
194 DEBUG << "FileStatusWidget::setLocalPath: date reference file "
|
Chris@93
|
195 << m_dateReference->absoluteFilePath()
|
Chris@93
|
196 << " does not exist, is not a file, or cannot be read"
|
Chris@93
|
197 << endl;
|
Chris@93
|
198 delete m_dateReference;
|
Chris@93
|
199 m_dateReference = 0;
|
Chris@93
|
200 }
|
Chris@88
|
201 }
|
Chris@88
|
202
|
Chris@88
|
203 void
|
Chris@88
|
204 FileStatusWidget::setRemoteURL(QString r)
|
Chris@88
|
205 {
|
Chris@88
|
206 m_remoteURL = r;
|
Chris@88
|
207 m_remoteURLLabel->setText(r);
|
Chris@88
|
208 }
|
Chris@88
|
209
|
Chris@88
|
210 void
|
Chris@92
|
211 FileStatusWidget::setFileStates(FileStates p)
|
Chris@88
|
212 {
|
Chris@92
|
213 m_fileStates = p;
|
Chris@88
|
214 updateWidgets();
|
Chris@88
|
215 }
|
Chris@88
|
216
|
Chris@88
|
217 void
|
Chris@88
|
218 FileStatusWidget::updateWidgets()
|
Chris@88
|
219 {
|
Chris@95
|
220 QDateTime lastInteractionTime;
|
Chris@95
|
221 if (m_dateReference) {
|
Chris@95
|
222 lastInteractionTime = m_dateReference->lastModified();
|
Chris@95
|
223 DEBUG << "reference time: " << lastInteractionTime << endl;
|
Chris@95
|
224 }
|
Chris@95
|
225
|
Chris@95
|
226 QSet<QString> selectedFiles;
|
Chris@95
|
227 foreach (QString f, m_selectedFiles) selectedFiles.insert(f);
|
Chris@95
|
228
|
Chris@94
|
229 foreach (FileStates::State s, m_stateListMap.keys()) {
|
Chris@95
|
230
|
Chris@94
|
231 QListWidget *w = m_stateListMap[s];
|
Chris@94
|
232 w->clear();
|
Chris@95
|
233 QStringList files = m_fileStates.getFilesInState(s);
|
Chris@93
|
234
|
Chris@95
|
235 QStringList highPriority, lowPriority;
|
Chris@95
|
236
|
Chris@95
|
237 foreach (QString file, files) {
|
Chris@95
|
238
|
Chris@95
|
239 bool highlighted = false;
|
Chris@95
|
240
|
Chris@95
|
241 if (s == FileStates::Unknown) {
|
Chris@95
|
242 // We want to highlight untracked files that have appeared
|
Chris@95
|
243 // since the last interaction with the repo
|
Chris@95
|
244 QString fn(m_localPath + "/" + file);
|
Chris@95
|
245 DEBUG << "comparing with " << fn << endl;
|
Chris@95
|
246 QFileInfo fi(fn);
|
Chris@95
|
247 if (fi.exists() && fi.lastModified() > lastInteractionTime) {
|
Chris@95
|
248 DEBUG << "file " << fn << " is newer (" << fi.lastModified()
|
Chris@95
|
249 << ") than reference" << endl;
|
Chris@95
|
250 highlighted = true;
|
Chris@95
|
251 }
|
Chris@95
|
252 }
|
Chris@95
|
253
|
Chris@95
|
254 if (highlighted) {
|
Chris@95
|
255 highPriority.push_back(file);
|
Chris@95
|
256 } else {
|
Chris@95
|
257 lowPriority.push_back(file);
|
Chris@93
|
258 }
|
Chris@93
|
259 }
|
Chris@95
|
260
|
Chris@95
|
261 foreach (QString file, highPriority) {
|
Chris@95
|
262 QListWidgetItem *item = new QListWidgetItem(file);
|
Chris@95
|
263 w->addItem(item);
|
Chris@95
|
264 item->setForeground(Qt::red); //!!! and a nice gold star
|
Chris@95
|
265 item->setSelected(selectedFiles.contains(file));
|
Chris@95
|
266 }
|
Chris@95
|
267
|
Chris@95
|
268 foreach (QString file, lowPriority) {
|
Chris@95
|
269 QListWidgetItem *item = new QListWidgetItem(file);
|
Chris@95
|
270 w->addItem(item);
|
Chris@95
|
271 item->setSelected(selectedFiles.contains(file));
|
Chris@95
|
272 }
|
Chris@95
|
273
|
Chris@95
|
274 w->parentWidget()->setVisible(!files.empty());
|
Chris@93
|
275 }
|
Chris@88
|
276 }
|
Chris@88
|
277
|