comparison filestatuswidget.cpp @ 95:d1be9712818a

* Update actions appropriately when selections are changed
author Chris Cannam
date Wed, 24 Nov 2010 16:29:05 +0000
parents 44ed7766d55a
children a5a2d74a83a7
comparison
equal deleted inserted replaced
94:44ed7766d55a 95:d1be9712818a
71 QListWidget *w = new QListWidget; 71 QListWidget *w = new QListWidget;
72 m_stateListMap[s] = w; 72 m_stateListMap[s] = w;
73 w->setSelectionMode(QListWidget::ExtendedSelection); 73 w->setSelectionMode(QListWidget::ExtendedSelection);
74 boxlayout->addWidget(w, 1, 0); 74 boxlayout->addWidget(w, 1, 0);
75 75
76 connect(w, SIGNAL(itemSelectionChanged()),
77 this, SLOT(itemSelectionChanged()));
78
76 layout->addWidget(box, ++row, 0, 1, 2); 79 layout->addWidget(box, ++row, 0, 1, 2);
77 box->hide(); 80 box->hide();
78 } 81 }
79 82
80 layout->setRowStretch(++row, 20); 83 layout->setRowStretch(++row, 20);
83 FileStatusWidget::~FileStatusWidget() 86 FileStatusWidget::~FileStatusWidget()
84 { 87 {
85 delete m_dateReference; 88 delete m_dateReference;
86 } 89 }
87 90
91 void FileStatusWidget::itemSelectionChanged()
92 {
93 m_selectedFiles.clear();
94
95 DEBUG << "FileStatusWidget::itemSelectionChanged" << endl;
96
97 foreach (QListWidget *w, m_stateListMap) {
98 QList<QListWidgetItem *> sel = w->selectedItems();
99 foreach (QListWidgetItem *i, sel) {
100 m_selectedFiles.push_back(i->text());
101 DEBUG << "file " << i->text() << " is selected" << endl;
102 }
103 }
104
105 emit selectionChanged();
106 }
107
88 void FileStatusWidget::clearSelections() 108 void FileStatusWidget::clearSelections()
89 { 109 {
110 m_selectedFiles.clear();
90 foreach (QListWidget *w, m_stateListMap) { 111 foreach (QListWidget *w, m_stateListMap) {
91 w->clearSelection(); 112 w->clearSelection();
92 } 113 }
114 }
115
116 bool FileStatusWidget::haveChangesToCommit() const
117 {
118 return !m_fileStates.added().empty() ||
119 !m_fileStates.removed().empty() ||
120 !m_fileStates.modified().empty();
121 }
122
123 bool FileStatusWidget::haveSelection() const
124 {
125 return !m_selectedFiles.empty();
126 }
127
128 QStringList FileStatusWidget::getAllSelectedFiles() const
129 {
130 return m_selectedFiles;
131 }
132
133 QStringList FileStatusWidget::getSelectedCommittableFiles() const
134 {
135 QStringList files;
136 foreach (QString f, m_selectedFiles) {
137 switch (m_fileStates.getStateOfFile(f)) {
138 case FileStates::Added:
139 case FileStates::Modified:
140 case FileStates::Removed:
141 files.push_back(f);
142 break;
143 default: break;
144 }
145 }
146 return files;
147 }
148
149 QStringList FileStatusWidget::getSelectedAddableFiles() const
150 {
151 QStringList files;
152 foreach (QString f, m_selectedFiles) {
153 switch (m_fileStates.getStateOfFile(f)) {
154 case FileStates::Unknown:
155 case FileStates::Removed:
156 files.push_back(f);
157 break;
158 default: break;
159 }
160 }
161 return files;
162 }
163
164 QStringList FileStatusWidget::getSelectedRemovableFiles() const
165 {
166 QStringList files;
167 foreach (QString f, m_selectedFiles) {
168 switch (m_fileStates.getStateOfFile(f)) {
169 case FileStates::Clean:
170 case FileStates::Added:
171 case FileStates::Modified:
172 case FileStates::Missing:
173 files.push_back(f);
174 break;
175 default: break;
176 }
177 }
178 return files;
93 } 179 }
94 180
95 void 181 void
96 FileStatusWidget::setLocalPath(QString p) 182 FileStatusWidget::setLocalPath(QString p)
97 { 183 {
124 m_fileStates = p; 210 m_fileStates = p;
125 updateWidgets(); 211 updateWidgets();
126 } 212 }
127 213
128 void 214 void
129 FileStatusWidget::highlightFile(QListWidget *w, int i)
130 {
131 DEBUG << "FileStatusWidget: highlighting file at " << i << endl;
132 QListWidgetItem *item = w->item(i);
133 item->setForeground(Qt::red);
134 //!!! and a nice gold star
135 }
136
137 void
138 FileStatusWidget::updateWidgets() 215 FileStatusWidget::updateWidgets()
139 { 216 {
217 QDateTime lastInteractionTime;
218 if (m_dateReference) {
219 lastInteractionTime = m_dateReference->lastModified();
220 DEBUG << "reference time: " << lastInteractionTime << endl;
221 }
222
223 QSet<QString> selectedFiles;
224 foreach (QString f, m_selectedFiles) selectedFiles.insert(f);
225
140 foreach (FileStates::State s, m_stateListMap.keys()) { 226 foreach (FileStates::State s, m_stateListMap.keys()) {
227
141 QListWidget *w = m_stateListMap[s]; 228 QListWidget *w = m_stateListMap[s];
142 w->clear(); 229 w->clear();
143 QStringList sl = m_fileStates.getFilesInState(s); 230 QStringList files = m_fileStates.getFilesInState(s);
144 w->addItems(sl); 231
145 w->parentWidget()->setVisible(!sl.empty()); 232 QStringList highPriority, lowPriority;
146 } 233
147 234 foreach (QString file, files) {
148 if (m_dateReference) { 235
149 // Highlight untracked files that have appeared since the 236 bool highlighted = false;
150 // last interaction with the repo 237
151 QDateTime refTime = m_dateReference->lastModified(); 238 if (s == FileStates::Unknown) {
152 DEBUG << "reference time: " << refTime << endl; 239 // We want to highlight untracked files that have appeared
153 QListWidget *ul = m_stateListMap[FileStates::Unknown]; 240 // since the last interaction with the repo
154 for (int i = 0; i < ul->count(); ++i) { 241 QString fn(m_localPath + "/" + file);
155 QString fn(m_localPath + "/" + ul->item(i)->text()); 242 DEBUG << "comparing with " << fn << endl;
156 DEBUG << "comparing with " << fn << endl; 243 QFileInfo fi(fn);
157 QFileInfo fi(fn); 244 if (fi.exists() && fi.lastModified() > lastInteractionTime) {
158 if (fi.exists() && fi.lastModified() > refTime) { 245 DEBUG << "file " << fn << " is newer (" << fi.lastModified()
159 DEBUG << "file " << fn << " is newer (" << fi.lastModified() 246 << ") than reference" << endl;
160 << ") than reference" << endl; 247 highlighted = true;
161 highlightFile(ul, i); 248 }
162 } 249 }
163 } 250
164 } 251 if (highlighted) {
165 } 252 highPriority.push_back(file);
166 253 } else {
254 lowPriority.push_back(file);
255 }
256 }
257
258 foreach (QString file, highPriority) {
259 QListWidgetItem *item = new QListWidgetItem(file);
260 w->addItem(item);
261 item->setForeground(Qt::red); //!!! and a nice gold star
262 item->setSelected(selectedFiles.contains(file));
263 }
264
265 foreach (QString file, lowPriority) {
266 QListWidgetItem *item = new QListWidgetItem(file);
267 w->addItem(item);
268 item->setSelected(selectedFiles.contains(file));
269 }
270
271 w->parentWidget()->setVisible(!files.empty());
272 }
273 }
274