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