summaryrefslogtreecommitdiffstats
blob: f125fc025c79431b79546451b5612f434d6b325e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2010-2014 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "QtXMLConsoleWidget.h"

#include <string>

#include <QCloseEvent>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QPushButton>
#include <QScrollBar>
#include <QCheckBox>

#include <Swiften/Base/format.h>

#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

QtXMLConsoleWidget::QtXMLConsoleWidget() {
	setWindowTitle(tr("Console"));

	QVBoxLayout* layout = new QVBoxLayout(this);
	layout->setSpacing(0);
	layout->setContentsMargins(0,0,0,0);

	textEdit = new QTextEdit(this);
	textEdit->setReadOnly(true);
	layout->addWidget(textEdit);

	QWidget* bottom = new QWidget(this);
	layout->addWidget(bottom);
	bottom->setAutoFillBackground(true);

	QHBoxLayout* buttonLayout = new QHBoxLayout(bottom);
	buttonLayout->setContentsMargins(10,0,20,0);
	buttonLayout->setSpacing(0);

	enabled = new QCheckBox(tr("Trace input/output"), bottom);
	enabled->setChecked(true);
	buttonLayout->addWidget(enabled);

	buttonLayout->addStretch();

	QPushButton* clearButton = new QPushButton(tr("Clear"), bottom);
	connect(clearButton, SIGNAL(clicked()), textEdit, SLOT(clear()));
	buttonLayout->addWidget(clearButton);

	setWindowTitle(tr("Debug Console"));
	emit titleUpdated();
}

QtXMLConsoleWidget::~QtXMLConsoleWidget() {
}

void QtXMLConsoleWidget::showEvent(QShowEvent* event) {
	emit windowOpening();
	emit titleUpdated(); /* This just needs to be somewhere after construction */
	QWidget::showEvent(event);
}

void QtXMLConsoleWidget::show() {
	QWidget::show();
	emit windowOpening();
}

void QtXMLConsoleWidget::activate() {
	emit wantsToActivate();
}

void QtXMLConsoleWidget::closeEvent(QCloseEvent* event) {
	emit windowClosing();
	event->accept();
}

void QtXMLConsoleWidget::handleDataRead(const SafeByteArray& data) {
	boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
	std::string tag = Q2PSTRING(tr("<!-- IN %1 -->").arg(P2QSTRING(boost::posix_time::to_iso_extended_string(now))));
	appendTextIfEnabled(tag + "\n" + safeByteArrayToString(data) + "\n", QColor(33,98,33));
}

void QtXMLConsoleWidget::handleDataWritten(const SafeByteArray& data) {
	boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
	std::string tag = Q2PSTRING(tr("<!-- OUT %1 -->").arg(P2QSTRING(boost::posix_time::to_iso_extended_string(now))));
	appendTextIfEnabled(tag + "\n" + safeByteArrayToString(data) + "\n", QColor(155,1,0));
}

std::string QtXMLConsoleWidget::getID() const {
	return "QtXMLConsoleWidget";
}

void QtXMLConsoleWidget::appendTextIfEnabled(const std::string& data, const QColor& color) {
	if (enabled->isChecked()) {
		QScrollBar* scrollBar = textEdit->verticalScrollBar();
		bool scrollToBottom = (!scrollBar || scrollBar->value() == scrollBar->maximum());

		QTextCursor cursor(textEdit->document());
		cursor.beginEditBlock();
		cursor.movePosition(QTextCursor::End);
		QTextCharFormat format;
		format.setForeground(QBrush(color));
		cursor.mergeCharFormat(format);
		cursor.insertText(P2QSTRING(data));
		cursor.endEditBlock();

		// Checking for the scrollbar again, because it could have appeared after inserting text.
		// In practice, I suspect that the scrollbar is always there, but hidden, but since we were
		// explicitly testing for this already above, I'm leaving the code in.
		scrollBar = textEdit->verticalScrollBar();
		if (scrollToBottom && scrollBar) {
			scrollBar->setValue(scrollBar->maximum());
		}
	}
}

}