Chris@331: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@331: 
Chris@331: /*
Chris@331:     EasyMercurial
Chris@331: 
Chris@331:     Based on hgExplorer by Jari Korhonen
Chris@331:     Copyright (c) 2010 Jari Korhonen
Chris@644:     Copyright (c) 2013 Chris Cannam
Chris@644:     Copyright (c) 2013 Queen Mary, University of London
Chris@331: 
Chris@331:     This program is free software; you can redistribute it and/or
Chris@331:     modify it under the terms of the GNU General Public License as
Chris@331:     published by the Free Software Foundation; either version 2 of the
Chris@331:     License, or (at your option) any later version.  See the file
Chris@331:     COPYING included with this distribution for more information.
Chris@331: */
Chris@331: 
Chris@331: #include "annotatedialog.h"
Chris@331: #include "common.h"
Chris@332: #include "colourset.h"
Chris@331: #include "debug.h"
Chris@331: 
Chris@331: #include <QDialogButtonBox>
Chris@331: #include <QLabel>
Chris@331: #include <QTableWidget>
Chris@331: #include <QHeaderView>
Chris@331: #include <QGridLayout>
Chris@331: 
Chris@331: AnnotateDialog::AnnotateDialog(QWidget *w, QString text) :
Chris@331:     QDialog(w)
Chris@331: {
Chris@332:     setMinimumWidth(800);
Chris@332:     setMinimumHeight(500);
Chris@331: 
Chris@331:     text.replace("\r\n", "\n");
Chris@331:     QStringList lines = text.split("\n");
Chris@331: 
Chris@331:     QGridLayout *layout = new QGridLayout;
Chris@331:     QTableWidget *table = new QTableWidget;
Chris@331: 
Chris@331:     QRegExp annotateLineRE = QRegExp("^([^:]+) ([a-z0-9]{12}) ([0-9-]+): (.*)$");
Chris@331: 
Chris@331:     table->setRowCount(lines.size());
Chris@331:     table->setColumnCount(4);
Chris@331:     table->horizontalHeader()->setStretchLastSection(true);
Chris@332:     table->verticalHeader()->setDefaultSectionSize
Chris@332: 	(table->verticalHeader()->fontMetrics().height() + 2);
Chris@331: 
Chris@331:     QStringList labels;
Chris@331:     labels << tr("User") << tr("Revision") << tr("Date") << tr("Content");
Chris@331:     table->setHorizontalHeaderLabels(labels);
Chris@331: 
Chris@332:     table->setShowGrid(false);
Chris@332: 
Chris@332:     QFont monofont("Monospace");
Chris@332:     monofont.setStyleHint(QFont::TypeWriter);
Chris@332: 
Chris@331:     int row = 0;
Chris@331: 
Chris@331:     foreach (QString line, lines) {
Chris@331: 	if (annotateLineRE.indexIn(line) == 0) {
Chris@331: 	    QStringList items = annotateLineRE.capturedTexts();
Chris@332: 	    QString id = items[2];
Chris@332: 	    QColor colour = ColourSet::instance()->getColourFor(id);
Chris@332: 	    QColor bg = QColor::fromHsv(colour.hue(),
Chris@332: 					30,
Chris@332: 					230);
Chris@331: 	    // note items[0] is the whole match, so we want 1-4
Chris@331: 	    for (int col = 0; col+1 < items.size(); ++col) {
Chris@332: 		QString item = items[col+1];
Chris@332: 		if (col == 0) item = item.trimmed();
Chris@332: 		QTableWidgetItem *wi = new QTableWidgetItem(item);
Chris@332: 		wi->setFlags(Qt::ItemIsEnabled);
Chris@332: 		wi->setBackground(bg);
Chris@332: 		if (col == 3) { // id, content
Chris@332: 		    wi->setFont(monofont);
Chris@332: 		}
Chris@332: 		table->setItem(row, col, wi);
Chris@331: 	    }
Chris@331: 	} else {
Chris@331: 	    DEBUG << "AnnotateDialog: Failed to match RE in line: " << line << " at row " << row << endl;
Chris@331: 	}
Chris@331: 	++row;
Chris@331:     }
Chris@331: 
Chris@331:     QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok);
Chris@331:     connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
Chris@331: 
Chris@331:     layout->addWidget(table, 0, 0);
Chris@331:     layout->addWidget(bb, 1, 0);
Chris@331: 
Chris@331:     setLayout(layout);
Chris@331: }
Chris@331: