annotate filestatuswidget.cpp @ 203:c77c4d00a4fe

Merge
author Chris Cannam
date Tue, 04 Jan 2011 14:31:30 +0000
parents 3d4291d4226c 0844b4d8d911
children c5fceb3fe5b4
rev   line source
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@186 21 #include "clickablelabel.h"
Chris@88 22
Chris@88 23 #include <QLabel>
Chris@88 24 #include <QListWidget>
Chris@88 25 #include <QGridLayout>
Chris@93 26 #include <QFileInfo>
Chris@93 27 #include <QApplication>
Chris@93 28 #include <QDateTime>
Chris@115 29 #include <QPushButton>
Chris@186 30 #include <QToolButton>
Chris@186 31 #include <QDir>
Chris@186 32 #include <QProcess>
Chris@199 33 #include <QCheckBox>
Chris@88 34
Chris@88 35 FileStatusWidget::FileStatusWidget(QWidget *parent) :
Chris@93 36 QWidget(parent),
Chris@93 37 m_dateReference(0)
Chris@88 38 {
Chris@88 39 QGridLayout *layout = new QGridLayout;
Chris@88 40 setLayout(layout);
Chris@88 41
Chris@88 42 int row = 0;
Chris@88 43
Chris@115 44 layout->addItem(new QSpacerItem(1, 1), row, 0);
Chris@115 45
Chris@115 46 ++row;
Chris@88 47 layout->addWidget(new QLabel(tr("Local:")), row, 0);
Chris@186 48
Chris@186 49 m_openButton = new ClickableLabel;
Chris@186 50 QFont f(m_openButton->font());
Chris@89 51 f.setBold(true);
Chris@186 52 m_openButton->setFont(f);
Chris@186 53 m_openButton->setMouseUnderline(true);
Chris@186 54 connect(m_openButton, SIGNAL(clicked()), this, SLOT(openButtonClicked()));
Chris@187 55 layout->addWidget(m_openButton, row, 1, 1, 2, Qt::AlignLeft);
Chris@88 56
Chris@88 57 ++row;
Chris@88 58 layout->addWidget(new QLabel(tr("Remote:")), row, 0);
Chris@88 59 m_remoteURLLabel = new QLabel;
Chris@186 60 layout->addWidget(m_remoteURLLabel, row, 1, 1, 2);
Chris@88 61
Chris@106 62 ++row;
Chris@115 63 layout->addWidget(new QLabel(tr("State:")), row, 0);
Chris@115 64 m_stateLabel = new QLabel;
Chris@115 65 layout->addWidget(m_stateLabel, row, 1, 1, 2);
Chris@106 66
Chris@89 67 layout->setColumnStretch(1, 20);
Chris@88 68
Chris@115 69 layout->addWidget(new QLabel("<qt><hr></qt>"), ++row, 0, 1, 3);
Chris@115 70
Chris@115 71 ++row;
Chris@115 72 m_noModificationsLabel = new QLabel
Chris@202 73 (tr("<qt>This area will list files in your working folder that you have changed.<br>At the moment you have no uncommitted changes.<br><br>To see changes made to the repository previously,<br>switch to the History tab.<br><br>%1</qt>")
Chris@202 74 #if defined Q_OS_MAC
Chris@202 75 .arg(tr("To open the working folder in Finder,<br>click on the &ldquo;Local&rdquo; folder path shown above."))
Chris@202 76 #elif defined Q_OS_WIN32
Chris@202 77 .arg(tr("To open the working folder in Windows Explorer,<br>click on the &ldquo;Local&rdquo; folder path shown above."))
Chris@202 78 #else
Chris@202 79 .arg(tr("To open the working folder in your system file manager,<br>click the &ldquo;Local&rdquo; folder path shown above."))
Chris@202 80 #endif
Chris@202 81 );
Chris@115 82 layout->addWidget(m_noModificationsLabel, row, 1, 1, 2);
Chris@202 83 m_noModificationsLabel->hide();
Chris@106 84
Chris@100 85 m_simpleLabels[FileStates::Clean] = tr("Unmodified:");
Chris@100 86 m_simpleLabels[FileStates::Modified] = tr("Modified:");
Chris@100 87 m_simpleLabels[FileStates::Added] = tr("Added:");
Chris@100 88 m_simpleLabels[FileStates::Removed] = tr("Removed:");
Chris@100 89 m_simpleLabels[FileStates::Missing] = tr("Missing:");
Chris@163 90 m_simpleLabels[FileStates::InConflict] = tr("In Conflict:");
Chris@100 91 m_simpleLabels[FileStates::Unknown] = tr("Untracked:");
Chris@199 92 m_simpleLabels[FileStates::Ignored] = tr("Ignored:");
Chris@99 93
Chris@100 94 m_descriptions[FileStates::Clean] = tr("You have not changed these files.");
Chris@100 95 m_descriptions[FileStates::Modified] = tr("You have changed these files since you last committed them.");
Chris@100 96 m_descriptions[FileStates::Added] = tr("These files will be added to version control next time you commit.");
Chris@100 97 m_descriptions[FileStates::Removed] = tr("These files will be removed from version control next time you commit.<br>"
Chris@100 98 "They will not be deleted from the local folder.");
Chris@109 99 m_descriptions[FileStates::Missing] = tr("These files are recorded in the version control, but absent from your working folder.<br>"
Chris@153 100 "If you intended to delete them, select them and use Remove to tell the version control system about it.<br>"
Chris@153 101 "If you deleted them by accident, select them and use Revert to restore their previous contents.");
Chris@163 102 m_descriptions[FileStates::InConflict] = tr("These files are unresolved following an incomplete merge.<br>Select a file and use Merge to try to resolve the merge again.");
Chris@100 103 m_descriptions[FileStates::Unknown] = tr("These files are in your working folder but are not under version control.<br>"
Chris@186 104 // "Select a file and use Add to place it under version control or Ignore to remove it from this list.");
Chris@186 105 "Select a file and use Add to place it under version control.");
Chris@199 106 m_descriptions[FileStates::Ignored] = tr("These files have names that match entries in the working folder's .hgignore file,<br>"
Chris@199 107 "and so will be ignored by the version control system.");
Chris@100 108
Chris@118 109 m_highlightExplanation = tr("Files highlighted <font color=#d40000>in red</font> "
Chris@100 110 "have appeared since your most recent commit or update.");
Chris@89 111
Chris@94 112 for (int i = int(FileStates::FirstState);
Chris@163 113 i <= int(FileStates::LastState); ++i) {
Chris@89 114
Chris@94 115 FileStates::State s = FileStates::State(i);
Chris@89 116
Chris@89 117 QWidget *box = new QWidget;
Chris@89 118 QGridLayout *boxlayout = new QGridLayout;
Chris@99 119 boxlayout->setMargin(0);
Chris@89 120 box->setLayout(boxlayout);
Chris@89 121
Chris@106 122 boxlayout->addItem(new QSpacerItem(5, 5), 0, 0);
Chris@101 123
Chris@101 124 boxlayout->addWidget(new QLabel(labelFor(s)), 1, 0);
Chris@89 125
Chris@94 126 QListWidget *w = new QListWidget;
Chris@94 127 m_stateListMap[s] = w;
Chris@94 128 w->setSelectionMode(QListWidget::ExtendedSelection);
Chris@101 129 boxlayout->addWidget(w, 2, 0);
Chris@89 130
Chris@95 131 connect(w, SIGNAL(itemSelectionChanged()),
Chris@95 132 this, SLOT(itemSelectionChanged()));
Chris@95 133
Chris@115 134 layout->addWidget(box, ++row, 0, 1, 3);
Chris@89 135 box->hide();
Chris@89 136 }
Chris@89 137
Chris@89 138 layout->setRowStretch(++row, 20);
Chris@186 139
Chris@199 140 layout->addItem(new QSpacerItem(1, 1), ++row, 0);
Chris@199 141
Chris@199 142 m_showAllFiles = new QCheckBox(tr("Show all files"), this);
Chris@199 143 layout->addWidget(m_showAllFiles, ++row, 0, 1, 3, Qt::AlignLeft);
Chris@199 144 connect(m_showAllFiles, SIGNAL(toggled(bool)),
Chris@199 145 this, SIGNAL(showAllChanged(bool)));
Chris@88 146 }
Chris@88 147
Chris@93 148 FileStatusWidget::~FileStatusWidget()
Chris@93 149 {
Chris@93 150 delete m_dateReference;
Chris@93 151 }
Chris@93 152
Chris@186 153 void FileStatusWidget::openButtonClicked()
Chris@186 154 {
Chris@186 155 QDir d(m_localPath);
Chris@186 156 if (d.exists()) {
Chris@186 157 QStringList args;
Chris@201 158 QString path = d.canonicalPath();
Chris@201 159 #if defined Q_OS_WIN32
Chris@201 160 // Although the Win32 API is quite happy to have
Chris@201 161 // forward slashes as directory separators, Windows
Chris@201 162 // Explorer is not
Chris@201 163 path = path.replace('/', '\\');
Chris@201 164 args << path;
Chris@201 165 QProcess::execute("c:/windows/explorer.exe", args);
Chris@201 166 #else
Chris@201 167 args << path;
Chris@186 168 QProcess::execute(
Chris@201 169 #if defined Q_OS_MAC
Chris@186 170 "/usr/bin/open",
Chris@186 171 #else
Chris@186 172 "/usr/bin/xdg-open",
Chris@186 173 #endif
Chris@186 174 args);
Chris@201 175 #endif
Chris@186 176 }
Chris@186 177 }
Chris@186 178
Chris@100 179 QString FileStatusWidget::labelFor(FileStates::State s, bool addHighlightExplanation)
Chris@100 180 {
Chris@100 181 if (addHighlightExplanation) {
Chris@100 182 return QString("<qt><b>%1</b><br>%2<br>%3</qt>")
Chris@100 183 .arg(m_simpleLabels[s])
Chris@100 184 .arg(m_descriptions[s])
Chris@100 185 .arg(m_highlightExplanation);
Chris@100 186 } else {
Chris@100 187 return QString("<qt><b>%1</b><br>%2</qt>")
Chris@100 188 .arg(m_simpleLabels[s])
Chris@100 189 .arg(m_descriptions[s]);
Chris@100 190 }
Chris@100 191 }
Chris@100 192
Chris@95 193 void FileStatusWidget::itemSelectionChanged()
Chris@95 194 {
Chris@135 195 DEBUG << "FileStatusWidget::itemSelectionChanged" << endl;
Chris@135 196
Chris@135 197 QListWidget *list = qobject_cast<QListWidget *>(sender());
Chris@135 198
Chris@135 199 if (list) {
Chris@135 200 foreach (QListWidget *w, m_stateListMap) {
Chris@135 201 if (w != list) {
Chris@135 202 w->blockSignals(true);
Chris@135 203 w->clearSelection();
Chris@135 204 w->blockSignals(false);
Chris@135 205 }
Chris@135 206 }
Chris@135 207 }
Chris@135 208
Chris@95 209 m_selectedFiles.clear();
Chris@95 210
Chris@95 211 foreach (QListWidget *w, m_stateListMap) {
Chris@95 212 QList<QListWidgetItem *> sel = w->selectedItems();
Chris@95 213 foreach (QListWidgetItem *i, sel) {
Chris@95 214 m_selectedFiles.push_back(i->text());
Chris@95 215 DEBUG << "file " << i->text() << " is selected" << endl;
Chris@95 216 }
Chris@95 217 }
Chris@95 218
Chris@95 219 emit selectionChanged();
Chris@95 220 }
Chris@95 221
Chris@94 222 void FileStatusWidget::clearSelections()
Chris@94 223 {
Chris@95 224 m_selectedFiles.clear();
Chris@94 225 foreach (QListWidget *w, m_stateListMap) {
Chris@94 226 w->clearSelection();
Chris@94 227 }
Chris@94 228 }
Chris@94 229
Chris@95 230 bool FileStatusWidget::haveChangesToCommit() const
Chris@95 231 {
Chris@95 232 return !m_fileStates.added().empty() ||
Chris@95 233 !m_fileStates.removed().empty() ||
Chris@95 234 !m_fileStates.modified().empty();
Chris@95 235 }
Chris@95 236
Chris@95 237 bool FileStatusWidget::haveSelection() const
Chris@95 238 {
Chris@95 239 return !m_selectedFiles.empty();
Chris@95 240 }
Chris@95 241
Chris@95 242 QStringList FileStatusWidget::getAllSelectedFiles() const
Chris@95 243 {
Chris@95 244 return m_selectedFiles;
Chris@95 245 }
Chris@95 246
Chris@95 247 QStringList FileStatusWidget::getSelectedCommittableFiles() const
Chris@95 248 {
Chris@95 249 QStringList files;
Chris@95 250 foreach (QString f, m_selectedFiles) {
Chris@95 251 switch (m_fileStates.getStateOfFile(f)) {
Chris@95 252 case FileStates::Added:
Chris@95 253 case FileStates::Modified:
Chris@95 254 case FileStates::Removed:
Chris@95 255 files.push_back(f);
Chris@95 256 break;
Chris@95 257 default: break;
Chris@95 258 }
Chris@95 259 }
Chris@95 260 return files;
Chris@95 261 }
Chris@95 262
Chris@103 263 QStringList FileStatusWidget::getAllCommittableFiles() const
Chris@103 264 {
Chris@103 265 QStringList files;
Chris@103 266 files << m_fileStates.getFilesInState(FileStates::Modified);
Chris@103 267 files << m_fileStates.getFilesInState(FileStates::Added);
Chris@103 268 files << m_fileStates.getFilesInState(FileStates::Removed);
Chris@103 269 return files;
Chris@103 270 }
Chris@103 271
Chris@109 272 QStringList FileStatusWidget::getSelectedRevertableFiles() const
Chris@109 273 {
Chris@109 274 QStringList files;
Chris@109 275 foreach (QString f, m_selectedFiles) {
Chris@109 276 switch (m_fileStates.getStateOfFile(f)) {
Chris@109 277 case FileStates::Added:
Chris@109 278 case FileStates::Modified:
Chris@109 279 case FileStates::Removed:
Chris@109 280 case FileStates::Missing:
Chris@163 281 case FileStates::InConflict:
Chris@109 282 files.push_back(f);
Chris@109 283 break;
Chris@109 284 default: break;
Chris@109 285 }
Chris@109 286 }
Chris@109 287 return files;
Chris@109 288 }
Chris@109 289
Chris@109 290 QStringList FileStatusWidget::getAllRevertableFiles() const
Chris@109 291 {
Chris@109 292 QStringList files;
Chris@109 293 files << m_fileStates.getFilesInState(FileStates::Modified);
Chris@109 294 files << m_fileStates.getFilesInState(FileStates::Added);
Chris@109 295 files << m_fileStates.getFilesInState(FileStates::Removed);
Chris@109 296 files << m_fileStates.getFilesInState(FileStates::Missing);
Chris@163 297 files << m_fileStates.getFilesInState(FileStates::InConflict);
Chris@163 298 return files;
Chris@163 299 }
Chris@163 300
Chris@163 301 QStringList FileStatusWidget::getSelectedUnresolvedFiles() const
Chris@163 302 {
Chris@163 303 QStringList files;
Chris@163 304 foreach (QString f, m_selectedFiles) {
Chris@163 305 switch (m_fileStates.getStateOfFile(f)) {
Chris@163 306 case FileStates::InConflict:
Chris@163 307 files.push_back(f);
Chris@163 308 break;
Chris@163 309 default: break;
Chris@163 310 }
Chris@163 311 }
Chris@163 312 return files;
Chris@163 313 }
Chris@163 314
Chris@163 315 QStringList FileStatusWidget::getAllUnresolvedFiles() const
Chris@163 316 {
Chris@163 317 QStringList files;
Chris@163 318 files << m_fileStates.getFilesInState(FileStates::InConflict);
Chris@109 319 return files;
Chris@109 320 }
Chris@109 321
Chris@95 322 QStringList FileStatusWidget::getSelectedAddableFiles() const
Chris@95 323 {
Chris@95 324 QStringList files;
Chris@95 325 foreach (QString f, m_selectedFiles) {
Chris@95 326 switch (m_fileStates.getStateOfFile(f)) {
Chris@95 327 case FileStates::Unknown:
Chris@95 328 case FileStates::Removed:
Chris@95 329 files.push_back(f);
Chris@95 330 break;
Chris@95 331 default: break;
Chris@95 332 }
Chris@95 333 }
Chris@95 334 return files;
Chris@95 335 }
Chris@95 336
Chris@103 337 QStringList FileStatusWidget::getAllAddableFiles() const
Chris@103 338 {
Chris@103 339 QStringList files;
Chris@103 340 files << m_fileStates.getFilesInState(FileStates::Removed);
Chris@103 341 files << m_fileStates.getFilesInState(FileStates::Unknown);
Chris@103 342 return files;
Chris@103 343 }
Chris@103 344
Chris@95 345 QStringList FileStatusWidget::getSelectedRemovableFiles() const
Chris@95 346 {
Chris@95 347 QStringList files;
Chris@95 348 foreach (QString f, m_selectedFiles) {
Chris@95 349 switch (m_fileStates.getStateOfFile(f)) {
Chris@95 350 case FileStates::Clean:
Chris@95 351 case FileStates::Added:
Chris@95 352 case FileStates::Modified:
Chris@95 353 case FileStates::Missing:
Chris@163 354 case FileStates::InConflict:
Chris@95 355 files.push_back(f);
Chris@95 356 break;
Chris@95 357 default: break;
Chris@95 358 }
Chris@95 359 }
Chris@95 360 return files;
Chris@95 361 }
Chris@95 362
Chris@103 363 QStringList FileStatusWidget::getAllRemovableFiles() const
Chris@103 364 {
Chris@103 365 QStringList files;
Chris@103 366 files << m_fileStates.getFilesInState(FileStates::Clean);
Chris@103 367 files << m_fileStates.getFilesInState(FileStates::Added);
Chris@103 368 files << m_fileStates.getFilesInState(FileStates::Modified);
Chris@103 369 files << m_fileStates.getFilesInState(FileStates::Missing);
Chris@163 370 files << m_fileStates.getFilesInState(FileStates::InConflict);
Chris@103 371 return files;
Chris@103 372 }
Chris@103 373
Chris@88 374 void
Chris@88 375 FileStatusWidget::setLocalPath(QString p)
Chris@88 376 {
Chris@88 377 m_localPath = p;
Chris@186 378 m_openButton->setText(p);
Chris@93 379 delete m_dateReference;
Chris@93 380 m_dateReference = new QFileInfo(p + "/.hg/dirstate");
Chris@93 381 if (!m_dateReference->exists() ||
Chris@93 382 !m_dateReference->isFile() ||
Chris@93 383 !m_dateReference->isReadable()) {
Chris@93 384 DEBUG << "FileStatusWidget::setLocalPath: date reference file "
Chris@93 385 << m_dateReference->absoluteFilePath()
Chris@93 386 << " does not exist, is not a file, or cannot be read"
Chris@93 387 << endl;
Chris@93 388 delete m_dateReference;
Chris@93 389 m_dateReference = 0;
Chris@93 390 }
Chris@186 391 m_openButton->setEnabled(QDir(m_localPath).exists());
Chris@88 392 }
Chris@88 393
Chris@88 394 void
Chris@88 395 FileStatusWidget::setRemoteURL(QString r)
Chris@88 396 {
Chris@88 397 m_remoteURL = r;
Chris@88 398 m_remoteURLLabel->setText(r);
Chris@88 399 }
Chris@88 400
Chris@88 401 void
Chris@92 402 FileStatusWidget::setFileStates(FileStates p)
Chris@88 403 {
Chris@92 404 m_fileStates = p;
Chris@88 405 updateWidgets();
Chris@88 406 }
Chris@88 407
Chris@88 408 void
Chris@115 409 FileStatusWidget::setState(QString b)
Chris@106 410 {
Chris@115 411 m_state = b;
Chris@115 412 updateStateLabel();
Chris@106 413 }
Chris@106 414
Chris@106 415 void
Chris@88 416 FileStatusWidget::updateWidgets()
Chris@88 417 {
Chris@95 418 QDateTime lastInteractionTime;
Chris@95 419 if (m_dateReference) {
Chris@95 420 lastInteractionTime = m_dateReference->lastModified();
Chris@95 421 DEBUG << "reference time: " << lastInteractionTime << endl;
Chris@95 422 }
Chris@95 423
Chris@95 424 QSet<QString> selectedFiles;
Chris@95 425 foreach (QString f, m_selectedFiles) selectedFiles.insert(f);
Chris@95 426
Chris@115 427 bool haveAnything = false;
Chris@115 428
Chris@94 429 foreach (FileStates::State s, m_stateListMap.keys()) {
Chris@95 430
Chris@94 431 QListWidget *w = m_stateListMap[s];
Chris@94 432 w->clear();
Chris@95 433 QStringList files = m_fileStates.getFilesInState(s);
Chris@93 434
Chris@95 435 QStringList highPriority, lowPriority;
Chris@95 436
Chris@95 437 foreach (QString file, files) {
Chris@95 438
Chris@95 439 bool highlighted = false;
Chris@95 440
Chris@95 441 if (s == FileStates::Unknown) {
Chris@95 442 // We want to highlight untracked files that have appeared
Chris@95 443 // since the last interaction with the repo
Chris@95 444 QString fn(m_localPath + "/" + file);
Chris@95 445 DEBUG << "comparing with " << fn << endl;
Chris@95 446 QFileInfo fi(fn);
Chris@100 447 if (fi.exists() && fi.created() > lastInteractionTime) {
Chris@95 448 DEBUG << "file " << fn << " is newer (" << fi.lastModified()
Chris@95 449 << ") than reference" << endl;
Chris@95 450 highlighted = true;
Chris@95 451 }
Chris@95 452 }
Chris@95 453
Chris@95 454 if (highlighted) {
Chris@95 455 highPriority.push_back(file);
Chris@95 456 } else {
Chris@95 457 lowPriority.push_back(file);
Chris@93 458 }
Chris@93 459 }
Chris@95 460
Chris@95 461 foreach (QString file, highPriority) {
Chris@95 462 QListWidgetItem *item = new QListWidgetItem(file);
Chris@95 463 w->addItem(item);
Chris@118 464 item->setForeground(QColor("#d40000")); //!!! and a nice gold star
Chris@95 465 item->setSelected(selectedFiles.contains(file));
Chris@95 466 }
Chris@95 467
Chris@95 468 foreach (QString file, lowPriority) {
Chris@95 469 QListWidgetItem *item = new QListWidgetItem(file);
Chris@95 470 w->addItem(item);
Chris@95 471 item->setSelected(selectedFiles.contains(file));
Chris@95 472 }
Chris@95 473
Chris@100 474 setLabelFor(w, s, !highPriority.empty());
Chris@100 475
Chris@115 476 if (files.empty()) {
Chris@115 477 w->parentWidget()->hide();
Chris@115 478 } else {
Chris@115 479 w->parentWidget()->show();
Chris@115 480 haveAnything = true;
Chris@115 481 }
Chris@93 482 }
Chris@115 483
Chris@115 484 m_noModificationsLabel->setVisible(!haveAnything);
Chris@115 485
Chris@115 486 updateStateLabel();
Chris@88 487 }
Chris@88 488
Chris@100 489 void FileStatusWidget::setLabelFor(QWidget *w, FileStates::State s, bool addHighlight)
Chris@100 490 {
Chris@100 491 QString text = labelFor(s, addHighlight);
Chris@100 492 QWidget *p = w->parentWidget();
Chris@100 493 QList<QLabel *> ql = p->findChildren<QLabel *>();
Chris@100 494 if (!ql.empty()) ql[0]->setText(text);
Chris@100 495 }
Chris@115 496
Chris@115 497 void FileStatusWidget::updateStateLabel()
Chris@115 498 {
Chris@115 499 m_stateLabel->setText(m_state);
Chris@115 500 }