annotate historywidget.cpp @ 201:0844b4d8d911

* Ensure explorer.exe invocation gets backslashes as directory separators, not forward slashes
author Chris Cannam
date Tue, 04 Jan 2011 14:05:17 +0000
parents 4bad3c5c053a
children 8fd71f570884
rev   line source
Chris@116 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@116 2
Chris@116 3 /*
Chris@116 4 EasyMercurial
Chris@116 5
Chris@116 6 Based on HgExplorer by Jari Korhonen
Chris@116 7 Copyright (c) 2010 Jari Korhonen
Chris@116 8 Copyright (c) 2010 Chris Cannam
Chris@116 9 Copyright (c) 2010 Queen Mary, University of London
Chris@116 10
Chris@116 11 This program is free software; you can redistribute it and/or
Chris@116 12 modify it under the terms of the GNU General Public License as
Chris@116 13 published by the Free Software Foundation; either version 2 of the
Chris@116 14 License, or (at your option) any later version. See the file
Chris@116 15 COPYING included with this distribution for more information.
Chris@116 16 */
Chris@116 17
Chris@116 18 #include "historywidget.h"
Chris@116 19
Chris@119 20 #include "changesetscene.h"
Chris@116 21 #include "panned.h"
Chris@116 22 #include "panner.h"
Chris@116 23 #include "grapher.h"
Chris@116 24 #include "debug.h"
Chris@129 25 #include "uncommitteditem.h"
Chris@116 26
Chris@116 27 #include <iostream>
Chris@116 28
Chris@116 29 #include <QGridLayout>
Chris@116 30
Chris@154 31 HistoryWidget::HistoryWidget() :
Chris@154 32 m_showUncommitted(false),
Chris@154 33 m_refreshNeeded(false)
Chris@116 34 {
Chris@116 35 m_panned = new Panned;
Chris@116 36 m_panner = new Panner;
Chris@116 37
Chris@168 38 m_panned->setDragMode(QGraphicsView::ScrollHandDrag);
Chris@168 39
Chris@116 40 QGridLayout *layout = new QGridLayout;
Chris@116 41 layout->addWidget(m_panned, 0, 0);
Chris@116 42 layout->addWidget(m_panner, 0, 1);
Chris@116 43 m_panner->setMaximumWidth(80);
Chris@116 44 m_panner->connectToPanned(m_panned);
Chris@116 45
Chris@116 46 setLayout(layout);
Chris@116 47 }
Chris@116 48
Chris@116 49 HistoryWidget::~HistoryWidget()
Chris@116 50 {
Chris@116 51 clearChangesets();
Chris@145 52 }
Chris@145 53
Chris@145 54 QGraphicsScene *HistoryWidget::scene()
Chris@145 55 {
Chris@145 56 return m_panned->scene();
Chris@116 57 }
Chris@116 58
Chris@116 59 void HistoryWidget::clearChangesets()
Chris@116 60 {
Chris@116 61 foreach (Changeset *cs, m_changesets) delete cs;
Chris@116 62 m_changesets.clear();
Chris@116 63 }
Chris@128 64
Chris@153 65 void HistoryWidget::setCurrent(QStringList ids, QString branch,
Chris@153 66 bool showUncommitted)
Chris@128 67 {
Chris@153 68 if (m_currentIds == ids &&
Chris@153 69 m_currentBranch == branch &&
Chris@153 70 m_showUncommitted == showUncommitted) return;
Chris@145 71
Chris@145 72 DEBUG << "HistoryWidget::setCurrent: " << ids.size() << " ids, "
Chris@145 73 << "showUncommitted: " << showUncommitted << endl;
Chris@145 74
Chris@133 75 m_currentIds.clear();
Chris@153 76 m_currentBranch = branch;
Chris@145 77 m_showUncommitted = showUncommitted;
Chris@145 78
Chris@145 79 if (ids.empty()) return;
Chris@145 80
Chris@133 81 foreach (QString id, ids) {
Chris@133 82 m_currentIds.push_back(id);
Chris@133 83 }
Chris@128 84
Chris@154 85 m_refreshNeeded = true;
Chris@128 86 }
Chris@116 87
Chris@120 88 void HistoryWidget::parseNewLog(QString log)
Chris@120 89 {
Chris@120 90 DEBUG << "HistoryWidget::parseNewLog: log has " << log.length() << " chars" << endl;
Chris@122 91 Changesets csets = Changeset::parseChangesets(log);
Chris@120 92 DEBUG << "HistoryWidget::parseNewLog: log has " << csets.size() << " changesets" << endl;
Chris@133 93 replaceChangesets(csets);
Chris@154 94 m_refreshNeeded = true;
Chris@120 95 }
Chris@120 96
Chris@120 97 void HistoryWidget::parseIncrementalLog(QString log)
Chris@120 98 {
Chris@120 99 DEBUG << "HistoryWidget::parseIncrementalLog: log has " << log.length() << " chars" << endl;
Chris@122 100 Changesets csets = Changeset::parseChangesets(log);
Chris@120 101 DEBUG << "HistoryWidget::parseIncrementalLog: log has " << csets.size() << " changesets" << endl;
Chris@120 102 if (!csets.empty()) {
Chris@133 103 addChangesets(csets);
Chris@120 104 }
Chris@154 105 m_refreshNeeded = true;
Chris@120 106 }
Chris@120 107
Chris@133 108 void HistoryWidget::replaceChangesets(Changesets csets)
Chris@133 109 {
Chris@133 110 QSet<QString> oldIds;
Chris@133 111 foreach (Changeset *cs, m_changesets) {
Chris@133 112 oldIds.insert(cs->id());
Chris@133 113 }
Chris@133 114
Chris@133 115 QSet<QString> newIds;
Chris@133 116 foreach (Changeset *cs, csets) {
Chris@133 117 if (!oldIds.contains(cs->id())) {
Chris@133 118 newIds.insert(cs->id());
Chris@133 119 }
Chris@133 120 }
Chris@133 121
Chris@133 122 if (newIds.size() == csets.size()) {
Chris@133 123 // completely new set, unrelated to the old: don't mark new
Chris@133 124 m_newIds.clear();
Chris@133 125 } else {
Chris@133 126 m_newIds = newIds;
Chris@133 127 }
Chris@133 128
Chris@133 129 clearChangesets();
Chris@133 130 m_changesets = csets;
Chris@133 131 }
Chris@133 132
Chris@133 133 void HistoryWidget::addChangesets(Changesets csets)
Chris@133 134 {
Chris@133 135 m_newIds.clear();
Chris@138 136
Chris@138 137 if (csets.empty()) return;
Chris@138 138
Chris@133 139 foreach (Changeset *cs, csets) {
Chris@133 140 m_newIds.insert(cs->id());
Chris@133 141 }
Chris@133 142
Chris@138 143 DEBUG << "addChangesets: " << csets.size() << " new changesets" << endl;
Chris@138 144
Chris@133 145 csets << m_changesets;
Chris@133 146 m_changesets = csets;
Chris@133 147 }
Chris@133 148
Chris@154 149 void HistoryWidget::update()
Chris@154 150 {
Chris@154 151 if (m_refreshNeeded) {
Chris@154 152 layoutAll();
Chris@154 153 }
Chris@154 154 }
Chris@154 155
Chris@120 156 void HistoryWidget::layoutAll()
Chris@116 157 {
Chris@154 158 m_refreshNeeded = false;
Chris@154 159
Chris@122 160 setChangesetParents();
Chris@122 161
Chris@119 162 ChangesetScene *scene = new ChangesetScene();
Chris@116 163 ChangesetItem *tipItem = 0;
Chris@116 164
Chris@138 165 QGraphicsScene *oldScene = m_panned->scene();
Chris@138 166
Chris@138 167 m_panned->setScene(0);
Chris@138 168 m_panner->setScene(0);
Chris@138 169
Chris@138 170 delete oldScene;
Chris@138 171
Chris@145 172 QGraphicsItem *toFocus = 0;
Chris@145 173
Chris@120 174 if (!m_changesets.empty()) {
Chris@116 175 Grapher g(scene);
Chris@116 176 try {
Chris@153 177 g.layout(m_changesets,
Chris@153 178 m_showUncommitted ? m_currentIds : QStringList(),
Chris@153 179 m_currentBranch);
Chris@116 180 } catch (std::string s) {
Chris@116 181 std::cerr << "Internal error: Layout failed: " << s << std::endl;
Chris@116 182 }
Chris@145 183 toFocus = g.getUncommittedItem();
Chris@145 184 if (!toFocus) {
Chris@145 185 toFocus = g.getItemFor(m_changesets[0]);
Chris@145 186 }
Chris@134 187 }
Chris@134 188
Chris@116 189 m_panned->setScene(scene);
Chris@116 190 m_panner->setScene(scene);
Chris@116 191
Chris@133 192 updateNewAndCurrentItems();
Chris@134 193
Chris@145 194 if (toFocus) {
Chris@145 195 toFocus->ensureVisible();
Chris@134 196 }
Chris@141 197
Chris@141 198 connectSceneSignals();
Chris@116 199 }
Chris@116 200
Chris@122 201 void HistoryWidget::setChangesetParents()
Chris@116 202 {
Chris@139 203 for (int i = 0; i < m_changesets.size(); ++i) {
Chris@122 204 Changeset *cs = m_changesets[i];
Chris@123 205 // Need to reset this, as Grapher::layout will recalculate it
Chris@123 206 // and we don't want to end up with twice the children for
Chris@123 207 // each parent...
Chris@123 208 cs->setChildren(QStringList());
Chris@139 209 }
Chris@139 210 for (int i = 0; i+1 < m_changesets.size(); ++i) {
Chris@139 211 Changeset *cs = m_changesets[i];
Chris@116 212 if (cs->parents().empty()) {
Chris@116 213 QStringList list;
Chris@122 214 list.push_back(m_changesets[i+1]->id());
Chris@116 215 cs->setParents(list);
Chris@116 216 }
Chris@116 217 }
Chris@116 218 }
Chris@128 219
Chris@133 220 void HistoryWidget::updateNewAndCurrentItems()
Chris@128 221 {
Chris@128 222 QGraphicsScene *scene = m_panned->scene();
Chris@128 223 if (!scene) return;
Chris@133 224
Chris@128 225 QList<QGraphicsItem *> items = scene->items();
Chris@128 226 foreach (QGraphicsItem *it, items) {
Chris@133 227
Chris@128 228 ChangesetItem *csit = dynamic_cast<ChangesetItem *>(it);
Chris@133 229 if (!csit) continue;
Chris@133 230
Chris@133 231 QString id = csit->getChangeset()->id();
Chris@133 232
Chris@133 233 bool current = m_currentIds.contains(id);
Chris@133 234 if (current) {
Chris@133 235 DEBUG << "id " << id << " is current" << endl;
Chris@133 236 }
Chris@133 237 bool newid = m_newIds.contains(id);
Chris@133 238 if (newid) {
Chris@133 239 DEBUG << "id " << id << " is new" << endl;
Chris@133 240 }
Chris@147 241
Chris@147 242 if (csit->isCurrent() != current || csit->isNew() != newid) {
Chris@147 243 csit->setCurrent(current);
Chris@147 244 csit->setNew(newid);
Chris@147 245 csit->update();
Chris@147 246 }
Chris@128 247 }
Chris@128 248 }
Chris@141 249
Chris@141 250 void HistoryWidget::connectSceneSignals()
Chris@141 251 {
Chris@141 252 ChangesetScene *scene = qobject_cast<ChangesetScene *>(m_panned->scene());
Chris@141 253 if (!scene) return;
Chris@141 254
Chris@141 255 connect(scene, SIGNAL(commit()),
Chris@141 256 this, SIGNAL(commit()));
Chris@141 257
Chris@141 258 connect(scene, SIGNAL(revert()),
Chris@141 259 this, SIGNAL(revert()));
Chris@141 260
Chris@141 261 connect(scene, SIGNAL(diffWorkingFolder()),
Chris@141 262 this, SIGNAL(diffWorkingFolder()));
Chris@141 263
Chris@168 264 connect(scene, SIGNAL(showSummary()),
Chris@168 265 this, SIGNAL(showSummary()));
Chris@168 266
Chris@153 267 connect(scene, SIGNAL(showWork()),
Chris@153 268 this, SIGNAL(showWork()));
Chris@153 269
Chris@141 270 connect(scene, SIGNAL(updateTo(QString)),
Chris@141 271 this, SIGNAL(updateTo(QString)));
Chris@141 272
Chris@141 273 connect(scene, SIGNAL(diffToCurrent(QString)),
Chris@141 274 this, SIGNAL(diffToCurrent(QString)));
Chris@141 275
Chris@148 276 connect(scene, SIGNAL(diffToParent(QString, QString)),
Chris@148 277 this, SIGNAL(diffToParent(QString, QString)));
Chris@141 278
Chris@141 279 connect(scene, SIGNAL(mergeFrom(QString)),
Chris@141 280 this, SIGNAL(mergeFrom(QString)));
Chris@141 281
Chris@141 282 connect(scene, SIGNAL(tag(QString)),
Chris@141 283 this, SIGNAL(tag(QString)));
Chris@141 284 }