summaryrefslogtreecommitdiffstats
blob: 662f070bc96d0f988c11b8edd57df02a22ea99f7 (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
#include "QtXMLConsoleWidget.h"

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

#include "QtSwiftUtil.h"
#include "Swiften/Base/String.h"

namespace Swift {

QtXMLConsoleWidget::QtXMLConsoleWidget() {
	setWindowTitle("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);

	QHBoxLayout* buttonLayout = new QHBoxLayout(bottom);
	buttonLayout->setContentsMargins(0,0,20,0);
	buttonLayout->setSpacing(0);
	buttonLayout->addStretch();
	QPushButton* clearButton = new QPushButton("Clear", bottom);
	connect(clearButton, SIGNAL(clicked()), textEdit, SLOT(clear()));
	buttonLayout->addWidget(clearButton);
}

void QtXMLConsoleWidget::showEvent(QShowEvent* event) {
	emit windowOpening();
	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 String& data) {
	textEdit->setTextColor(QColor(33,98,33));
	appendText(data);
}

void QtXMLConsoleWidget::handleDataWritten(const String& data) {
	textEdit->setTextColor(QColor(155,1,0));
	appendText(data);
}

void QtXMLConsoleWidget::appendText(const String& data) {
	QScrollBar* scrollBar = textEdit->verticalScrollBar();
	bool scrollToBottom = (!scrollBar || scrollBar->value() == scrollBar->maximum());
	textEdit->append(P2QSTRING(data));
	if (scrollToBottom) {
		textEdit->ensureCursorVisible();
	}
}

}