annotate grapher.cpp @ 114:bb2d2eecdd60

* minor output fixes
author Chris Cannam
date Fri, 26 Nov 2010 21:17:24 +0000
parents 4bd17f36d059
children 005a54380502
rev   line source
Chris@57 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@57 2
Chris@57 3 /*
Chris@57 4 EasyMercurial
Chris@57 5
Chris@57 6 Based on HgExplorer by Jari Korhonen
Chris@57 7 Copyright (c) 2010 Jari Korhonen
Chris@57 8 Copyright (c) 2010 Chris Cannam
Chris@57 9 Copyright (c) 2010 Queen Mary, University of London
Chris@57 10
Chris@57 11 This program is free software; you can redistribute it and/or
Chris@57 12 modify it under the terms of the GNU General Public License as
Chris@57 13 published by the Free Software Foundation; either version 2 of the
Chris@57 14 License, or (at your option) any later version. See the file
Chris@57 15 COPYING included with this distribution for more information.
Chris@57 16 */
Chris@44 17
Chris@44 18 #include "grapher.h"
Chris@46 19 #include "connectionitem.h"
Chris@53 20 #include "dateitem.h"
Chris@114 21 #include "debug.h"
Chris@44 22
cannam@45 23 #include <QGraphicsScene>
Chris@44 24
Chris@44 25 #include <iostream>
Chris@44 26
Chris@106 27 int Grapher::findAvailableColumn(int row, int parent, bool preferParentCol)
cannam@45 28 {
cannam@45 29 int col = parent;
cannam@45 30 if (preferParentCol) {
Chris@106 31 if (!m_alloc[row].contains(col)) {
Chris@106 32 return col;
Chris@106 33 }
cannam@45 34 }
cannam@45 35 while (col > 0) {
Chris@106 36 if (!m_alloc[row].contains(--col)) return col;
cannam@45 37 }
cannam@45 38 while (col < 0) {
Chris@106 39 if (!m_alloc[row].contains(++col)) return col;
cannam@45 40 }
cannam@45 41 col = parent;
cannam@45 42 int sign = (col < 0 ? -1 : 1);
cannam@45 43 while (1) {
Chris@106 44 col += sign;
Chris@106 45 if (!m_alloc[row].contains(col)) return col;
cannam@45 46 }
cannam@45 47 }
Chris@44 48
Chris@106 49 void Grapher::layoutRow(QString id)
Chris@44 50 {
cannam@45 51 if (m_handled.contains(id)) {
Chris@106 52 return;
Chris@44 53 }
Chris@46 54 if (!m_changesets.contains(id)) {
Chris@106 55 throw LayoutException(QString("Changeset %1 not in ID map").arg(id));
Chris@44 56 }
cannam@45 57 if (!m_items.contains(id)) {
Chris@106 58 throw LayoutException(QString("Changeset %1 not in item map").arg(id));
Chris@44 59 }
Chris@46 60 Changeset *cs = m_changesets[id];
cannam@45 61 ChangesetItem *item = m_items[id];
Chris@114 62 DEBUG << "layoutRow: Looking at " << id.toStdString() << endl;
cannam@45 63
Chris@44 64 int row = 0;
cannam@45 65 int nparents = cs->parents().size();
cannam@45 66
cannam@45 67 if (nparents > 0) {
Chris@106 68 bool haveRow = false;
Chris@106 69 foreach (QString parentId, cs->parents()) {
cannam@45 70
Chris@106 71 if (!m_changesets.contains(parentId)) continue;
Chris@106 72 if (!m_items.contains(parentId)) continue;
cannam@45 73
Chris@106 74 if (!m_handled.contains(parentId)) {
Chris@106 75 layoutRow(parentId);
Chris@106 76 }
cannam@45 77
Chris@106 78 ChangesetItem *parentItem = m_items[parentId];
Chris@106 79 if (!haveRow || parentItem->row() < row) {
Chris@106 80 row = parentItem->row();
Chris@106 81 haveRow = true;
Chris@106 82 }
Chris@106 83 }
Chris@106 84 row = row - 1;
cannam@45 85 }
cannam@45 86
Chris@52 87 // row is now an upper bound on our eventual row (because we want
Chris@52 88 // to be above all parents). But we also want to ensure we are
Chris@52 89 // above all nodes that have earlier dates (to the nearest day).
Chris@52 90 // m_rowDates maps each row to a date: use that.
Chris@52 91
Chris@53 92 QString date = cs->age();
Chris@53 93 while (m_rowDates.contains(row) && m_rowDates[row] != date) {
Chris@106 94 --row;
Chris@52 95 }
Chris@52 96
Chris@52 97 // We have already laid out all nodes that have earlier timestamps
Chris@52 98 // than this one, so we know (among other things) that we can
Chris@52 99 // safely fill in this row has having this date, if it isn't in
Chris@52 100 // the map yet (it cannot have an earlier date)
Chris@52 101
Chris@52 102 if (!m_rowDates.contains(row)) {
Chris@106 103 m_rowDates[row] = date;
Chris@52 104 }
Chris@52 105
Chris@114 106 DEBUG << "putting " << cs->id().toStdString() << " at row " << row
Chris@114 107 << endl;
cannam@45 108
Chris@44 109 item->setRow(row);
cannam@45 110 m_handled.insert(id);
Chris@44 111 }
Chris@44 112
Chris@106 113 void Grapher::layoutCol(QString id)
Chris@44 114 {
cannam@45 115 if (m_handled.contains(id)) {
Chris@114 116 DEBUG << "Already looked at " << id.toStdString() << endl;
Chris@106 117 return;
cannam@45 118 }
Chris@46 119 if (!m_changesets.contains(id)) {
Chris@106 120 throw LayoutException(QString("Changeset %1 not in ID map").arg(id));
cannam@45 121 }
cannam@45 122 if (!m_items.contains(id)) {
Chris@106 123 throw LayoutException(QString("Changeset %1 not in item map").arg(id));
cannam@45 124 }
Chris@53 125
Chris@46 126 Changeset *cs = m_changesets[id];
Chris@114 127 // DEBUG << "layoutCol: Looking at " << id.toStdString() << endl;
Chris@53 128
cannam@45 129 ChangesetItem *item = m_items[id];
Chris@47 130
cannam@45 131 int col = 0;
Chris@46 132 int row = item->row();
Chris@46 133 QString branch = cs->branch();
Chris@47 134
cannam@45 135 int nparents = cs->parents().size();
Chris@46 136 QString parentId;
Chris@46 137 int parentsOnSameBranch = 0;
cannam@45 138
Chris@46 139 switch (nparents) {
cannam@45 140
Chris@46 141 case 0:
Chris@106 142 col = m_branchHomes[cs->branch()];
Chris@106 143 col = findAvailableColumn(row, col, true);
Chris@106 144 break;
cannam@45 145
Chris@46 146 case 1:
Chris@106 147 parentId = cs->parents()[0];
cannam@45 148
Chris@106 149 if (!m_changesets.contains(parentId) ||
Chris@106 150 m_changesets[parentId]->branch() != branch) {
Chris@106 151 // new branch
Chris@106 152 col = m_branchHomes[branch];
Chris@106 153 } else {
Chris@106 154 col = m_items[parentId]->column();
Chris@106 155 }
cannam@45 156
Chris@106 157 col = findAvailableColumn(row, col, true);
Chris@106 158 break;
Chris@46 159
Chris@46 160 case 2:
Chris@106 161 // a merge: behave differently if parents are both on the same
Chris@106 162 // branch (we also want to behave differently for nodes that
Chris@106 163 // have multiple children on the same branch -- spreading them
Chris@106 164 // out rather than having affinity to a specific branch)
Chris@46 165
Chris@106 166 foreach (QString parentId, cs->parents()) {
Chris@106 167 if (!m_changesets.contains(parentId)) continue;
Chris@106 168 if (m_changesets[parentId]->branch() == branch) {
Chris@106 169 ChangesetItem *parentItem = m_items[parentId];
Chris@106 170 col += parentItem->column();
Chris@106 171 parentsOnSameBranch++;
Chris@106 172 }
Chris@106 173 }
Chris@46 174
Chris@106 175 if (parentsOnSameBranch > 0) {
Chris@106 176 col /= parentsOnSameBranch;
Chris@106 177 col = findAvailableColumn(item->row(), col, true);
Chris@106 178 } else {
Chris@106 179 col = findAvailableColumn(item->row(), col, false);
Chris@106 180 }
Chris@106 181 break;
Chris@44 182 }
Chris@44 183
Chris@114 184 // DEBUG << "putting " << cs->id().toStdString() << " at col " << col << endl;
cannam@45 185
Chris@46 186 m_alloc[row].insert(col);
cannam@45 187 item->setColumn(col);
cannam@45 188 m_handled.insert(id);
Chris@47 189
Chris@51 190 // Normally the children will lay out themselves, but we can do
Chris@51 191 // a better job in some special cases:
Chris@51 192
Chris@47 193 int nchildren = cs->children().size();
Chris@49 194
Chris@53 195 // look for merging children and children distant from us but in a
Chris@53 196 // straight line, and make sure nobody is going to overwrite their
Chris@53 197 // connection lines
Chris@51 198
Chris@49 199 foreach (QString childId, cs->children()) {
Chris@49 200 if (!m_changesets.contains(childId)) continue;
Chris@49 201 Changeset *child = m_changesets[childId];
Chris@106 202 int childRow = m_items[childId]->row();
Chris@55 203 if (child->parents().size() > 1 ||
Chris@106 204 child->branch() == cs->branch()) {
Chris@55 205 for (int r = row-1; r > childRow; --r) {
Chris@49 206 m_alloc[r].insert(col);
Chris@49 207 }
Chris@106 208 }
Chris@49 209 }
Chris@49 210
Chris@51 211 // look for the case where exactly two children have the same
Chris@51 212 // branch as us: split them to a little either side of our position
Chris@51 213
Chris@47 214 if (nchildren > 1) {
Chris@51 215
Chris@106 216 QList<QString> special;
Chris@106 217 foreach (QString childId, cs->children()) {
Chris@106 218 if (!m_changesets.contains(childId)) continue;
Chris@106 219 Changeset *child = m_changesets[childId];
Chris@106 220 if (child->branch() == branch &&
Chris@106 221 child->parents().size() == 1) {
Chris@106 222 special.push_back(childId);
Chris@106 223 }
Chris@106 224 }
Chris@106 225 if (special.size() == 2) {
Chris@106 226 for (int i = 0; i < 2; ++i) {
Chris@106 227 int off = i * 2 - 1; // 0 -> -1, 1 -> 1
Chris@106 228 ChangesetItem *it = m_items[special[i]];
Chris@106 229 it->setColumn(findAvailableColumn(it->row(), col + off, true));
Chris@106 230 for (int r = row-1; r >= it->row(); --r) {
Chris@106 231 m_alloc[r].insert(it->column());
Chris@106 232 }
Chris@106 233 m_handled.insert(special[i]);
Chris@106 234 }
Chris@106 235 }
Chris@47 236 }
cannam@45 237 }
cannam@45 238
Chris@106 239 bool Grapher::rangesConflict(const Range &r1, const Range &r2)
Chris@46 240 {
Chris@46 241 // allow some additional space at edges. we really ought also to
Chris@46 242 // permit space at the end of a branch up to the point where the
Chris@46 243 // merge happens
Chris@46 244 int a1 = r1.first - 2, b1 = r1.second + 2;
Chris@46 245 int a2 = r2.first - 2, b2 = r2.second + 2;
Chris@46 246 if (a1 > b2 || b1 < a2) return false;
Chris@46 247 if (a2 > b1 || b2 < a1) return false;
Chris@46 248 return true;
Chris@46 249 }
Chris@46 250
Chris@106 251 void Grapher::allocateBranchHomes(Changesets csets)
Chris@46 252 {
Chris@46 253 foreach (Changeset *cs, csets) {
Chris@106 254 QString branch = cs->branch();
Chris@106 255 ChangesetItem *item = m_items[cs->id()];
Chris@106 256 if (!item) continue;
Chris@106 257 int row = item->row();
Chris@106 258 if (!m_branchRanges.contains(branch)) {
Chris@106 259 m_branchRanges[branch] = Range(row, row);
Chris@106 260 } else {
Chris@106 261 Range p = m_branchRanges[branch];
Chris@106 262 if (row < p.first) p.first = row;
Chris@106 263 if (row > p.second) p.second = row;
Chris@106 264 m_branchRanges[branch] = p;
Chris@106 265 }
Chris@46 266 }
Chris@46 267
Chris@46 268 m_branchHomes[""] = 0;
Chris@46 269
Chris@46 270 foreach (QString branch, m_branchRanges.keys()) {
Chris@106 271 if (branch == "") continue;
Chris@106 272 QSet<int> taken;
Chris@106 273 taken.insert(0);
Chris@106 274 Range myRange = m_branchRanges[branch];
Chris@106 275 foreach (QString other, m_branchRanges.keys()) {
Chris@106 276 if (other == branch || other == "") continue;
Chris@106 277 Range otherRange = m_branchRanges[other];
Chris@106 278 if (rangesConflict(myRange, otherRange)) {
Chris@106 279 if (m_branchHomes.contains(other)) {
Chris@106 280 taken.insert(m_branchHomes[other]);
Chris@106 281 }
Chris@106 282 }
Chris@106 283 }
Chris@106 284 int home = 2;
Chris@106 285 while (taken.contains(home)) {
Chris@106 286 if (home > 0) {
Chris@106 287 if (home % 2 == 1) {
Chris@106 288 home = -home;
Chris@106 289 } else {
Chris@106 290 home = home + 1;
Chris@106 291 }
Chris@106 292 } else {
Chris@106 293 if ((-home) % 2 == 1) {
Chris@106 294 home = home + 1;
Chris@106 295 } else {
Chris@106 296 home = -(home-2);
Chris@106 297 }
Chris@106 298 }
Chris@106 299 }
Chris@106 300 m_branchHomes[branch] = home;
Chris@46 301 }
Chris@46 302
Chris@46 303 foreach (QString branch, m_branchRanges.keys()) {
Chris@114 304 DEBUG << branch.toStdString() << ": " << m_branchRanges[branch].first << " - " << m_branchRanges[branch].second << ", home " << m_branchHomes[branch] << endl;
Chris@46 305 }
Chris@46 306 }
Chris@46 307
Chris@52 308 static bool
Chris@106 309 compareChangesetsByDate(Changeset *const &a, Changeset *const &b)
Chris@52 310 {
Chris@52 311 return a->timestamp() < b->timestamp();
Chris@52 312 }
Chris@52 313
Chris@53 314 ChangesetItem *
Chris@106 315 Grapher::getItemFor(Changeset *cs)
Chris@53 316 {
Chris@53 317 if (!cs || !m_items.contains(cs->id())) return 0;
Chris@53 318 return m_items[cs->id()];
Chris@53 319 }
Chris@53 320
Chris@106 321 void Grapher::layout(Changesets csets)
cannam@45 322 {
Chris@46 323 m_changesets.clear();
cannam@45 324 m_items.clear();
cannam@45 325 m_alloc.clear();
Chris@46 326 m_branchHomes.clear();
cannam@45 327
Chris@53 328 if (csets.empty()) return;
Chris@53 329
Chris@44 330 foreach (Changeset *cs, csets) {
cannam@45 331
Chris@106 332 QString id = cs->id();
Chris@114 333 DEBUG << id.toStdString() << endl;
cannam@45 334
Chris@106 335 if (id == "") {
Chris@106 336 throw LayoutException("Changeset has no ID");
Chris@106 337 }
Chris@106 338 if (m_changesets.contains(id)) {
Chris@106 339 throw LayoutException(QString("Duplicate changeset ID %1").arg(id));
Chris@106 340 }
cannam@45 341
Chris@106 342 m_changesets[id] = cs;
cannam@45 343
cannam@45 344 ChangesetItem *item = new ChangesetItem(cs);
cannam@45 345 item->setX(0);
cannam@45 346 item->setY(0);
Chris@106 347 m_items[id] = item;
cannam@45 348 m_scene->addItem(item);
cannam@45 349 }
cannam@45 350
Chris@74 351 // Add the connecting lines
Chris@74 352
Chris@46 353 foreach (Changeset *cs, csets) {
Chris@106 354 QString id = cs->id();
Chris@106 355 ChangesetItem *item = m_items[id];
Chris@106 356 bool merge = (cs->parents().size() > 1);
Chris@106 357 foreach (QString parentId, cs->parents()) {
Chris@106 358 if (!m_changesets.contains(parentId)) continue;
Chris@106 359 Changeset *parent = m_changesets[parentId];
Chris@106 360 parent->addChild(id);
Chris@106 361 ConnectionItem *conn = new ConnectionItem();
Chris@106 362 if (merge) conn->setConnectionType(ConnectionItem::Merge);
Chris@106 363 conn->setChild(item);
Chris@106 364 conn->setParent(m_items[parentId]);
Chris@106 365 m_scene->addItem(conn);
Chris@106 366 }
Chris@46 367 }
Chris@46 368
Chris@74 369 // Add the branch labels
Chris@74 370 foreach (Changeset *cs, csets) {
Chris@74 371 QString id = cs->id();
Chris@74 372 ChangesetItem *item = m_items[id];
Chris@74 373 bool haveChildOnSameBranch = false;
Chris@74 374 foreach (QString childId, cs->children()) {
Chris@74 375 Changeset *child = m_changesets[childId];
Chris@74 376 if (child->branch() == cs->branch()) {
Chris@74 377 haveChildOnSameBranch = true;
Chris@74 378 break;
Chris@74 379 }
Chris@74 380 }
Chris@74 381 item->setShowBranch(!haveChildOnSameBranch);
Chris@74 382 }
Chris@74 383
Chris@52 384 // We need to lay out the changesets in forward chronological
Chris@52 385 // order. We have no guarantees about the order in which
Chris@52 386 // changesets appear in the list -- in a simple repository they
Chris@52 387 // will generally be reverse chronological, but that's far from
Chris@52 388 // guaranteed. So, sort explicitly using the date comparator
Chris@52 389 // above
Chris@51 390
Chris@52 391 qStableSort(csets.begin(), csets.end(), compareChangesetsByDate);
Chris@51 392
Chris@53 393 foreach (Changeset *cs, csets) {
Chris@114 394 DEBUG << "id " << cs->id().toStdString() << ", ts " << cs->timestamp() << ", date " << cs->datetime().toStdString() << endl;
Chris@53 395 }
Chris@53 396
cannam@45 397 m_handled.clear();
Chris@53 398 foreach (Changeset *cs, csets) {
Chris@106 399 layoutRow(cs->id());
cannam@45 400 }
Chris@46 401
Chris@46 402 allocateBranchHomes(csets);
Chris@46 403
cannam@45 404 m_handled.clear();
Chris@53 405 foreach (Changeset *cs, csets) {
Chris@106 406 foreach (QString parentId, cs->parents()) {
Chris@106 407 if (!m_handled.contains(parentId) &&
Chris@106 408 m_changesets.contains(parentId)) {
Chris@106 409 layoutCol(parentId);
Chris@106 410 }
Chris@106 411 }
Chris@106 412 layoutCol(cs->id());
Chris@53 413 }
Chris@53 414
Chris@55 415 foreach (Changeset *cs, csets) {
Chris@106 416 ChangesetItem *item = m_items[cs->id()];
Chris@106 417 if (!m_alloc[item->row()].contains(item->column()-1) &&
Chris@106 418 !m_alloc[item->row()].contains(item->column()+1)) {
Chris@106 419 item->setWide(true);
Chris@106 420 }
Chris@55 421 }
Chris@55 422
Chris@53 423 // we know that 0 is an upper bound on row, and that mincol must
Chris@53 424 // be <= 0 and maxcol >= 0, so these initial values are good
Chris@53 425 int minrow = 0, maxrow = 0;
Chris@53 426 int mincol = 0, maxcol = 0;
Chris@53 427
Chris@53 428 foreach (int r, m_alloc.keys()) {
Chris@106 429 if (r < minrow) minrow = r;
Chris@106 430 if (r > maxrow) maxrow = r;
Chris@106 431 ColumnSet &c = m_alloc[r];
Chris@106 432 foreach (int i, c) {
Chris@106 433 if (i < mincol) mincol = i;
Chris@106 434 if (i > maxcol) maxcol = i;
Chris@106 435 }
Chris@53 436 }
Chris@53 437
Chris@53 438 QString prevDate;
Chris@53 439 int changeRow = 0;
Chris@53 440
Chris@53 441 bool even = false;
Chris@53 442 int n = 0;
Chris@53 443
Chris@53 444 for (int row = minrow; row <= maxrow; ++row) {
Chris@53 445
Chris@106 446 QString date = m_rowDates[row];
Chris@106 447 n++;
Chris@106 448
Chris@106 449 if (date != prevDate) {
Chris@106 450 if (prevDate != "") {
Chris@106 451 DateItem *item = new DateItem();
Chris@106 452 item->setDateString(prevDate);
Chris@106 453 item->setCols(mincol, maxcol - mincol + 1);
Chris@106 454 item->setRows(changeRow, n);
Chris@106 455 item->setEven(even);
Chris@106 456 item->setZValue(-1);
Chris@106 457 m_scene->addItem(item);
Chris@106 458 even = !even;
Chris@106 459 }
Chris@106 460 prevDate = date;
Chris@106 461 changeRow = row;
Chris@106 462 n = 0;
Chris@106 463 }
Chris@53 464 }
Chris@53 465
Chris@53 466 if (n > 0) {
Chris@106 467 DateItem *item = new DateItem();
Chris@106 468 item->setDateString(prevDate);
Chris@106 469 item->setCols(mincol, maxcol - mincol + 1);
Chris@106 470 item->setRows(changeRow, n+1);
Chris@106 471 item->setEven(even);
Chris@106 472 item->setZValue(-1);
Chris@106 473 m_scene->addItem(item);
Chris@106 474 even = !even;
cannam@45 475 }
Chris@44 476 }
Chris@44 477