equationeditwidget.cpp 2.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "equationeditwidget.h"

#include <QtGui/QFocusEvent>
#include <QtGui/QKeyEvent>
#include <QtGui/QStyle>
#include <QtGui/QStyleOptionFrame>
#include <QtGui/QWheelEvent>

#include "equationedit.h"


void EquationEditWidget::setClearSelectionOnFocusOut(bool doIt) {
	m_clearSelectionOnFocusOut = doIt;
}


EquationEditWidget::EquationEditWidget(EquationEdit* parent) 
18
	: KTextEdit(parent)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
{
	m_clearSelectionOnFocusOut = true;
	m_parent = parent;
	recalculateGeometry();
}


void EquationEditWidget::recalculateGeometry() {
	// Set fixed height
	ensurePolished();
	QFontMetrics fm(document()->defaultFont());
	int h = qMax(fm.lineSpacing(), 14) + 6;
	int m = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
	QStyleOptionFrame opt;
	opt.rect = rect();
	opt.palette = palette();
	opt.state = QStyle::State_None;
	setFixedHeight(h + (2 * m));
	
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	setContentsMargins(0, 0, 0, 0);
	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setWordWrapMode(QTextOption::NoWrap);
43
	setLineWrapMode(KTextEdit::NoWrap);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	setTabChangesFocus(true);
}


void EquationEditWidget::wheelEvent(QWheelEvent* e) {
	e->accept();
}


void EquationEditWidget::keyPressEvent(QKeyEvent* e) {
	if ((e->key() == Qt::Key_Return) || (e->key() == Qt::Key_Enter)) {
		e->accept();
		emit m_parent->editingFinished();
		emit m_parent->returnPressed();
	} else {
59
		// Still pass these keys to KTextEdit, in case the user has to scroll
60 61 62 63 64 65
		// up/down the text
		if (e->key() == Qt::Key_Up)
			emit m_parent->upPressed();
		else if (e->key() == Qt::Key_Down)
			emit m_parent->downPressed();
		
66
		KTextEdit::keyPressEvent(e);
67 68 69 70 71
	}
}


void EquationEditWidget::focusOutEvent(QFocusEvent* e) {
72
	KTextEdit::focusOutEvent(e);
73 74 75 76 77 78 79 80 81 82
	
	if (m_clearSelectionOnFocusOut)
		clearSelection();
	m_parent->reHighlight();
	
	emit m_parent->editingFinished();
}


void EquationEditWidget::focusInEvent(QFocusEvent* e) {
83
	KTextEdit::focusOutEvent(e);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	m_parent->reHighlight();
	if (e->reason() == Qt::TabFocusReason)
		selectAll();
}


void EquationEditWidget::clearSelection() {
	QTextCursor cursor = textCursor();
	if (!cursor.hasSelection())
		return;
	
	cursor.clearSelection();
	setTextCursor(cursor);
}