File indexing completed on 2024-12-01 06:33:18
0001 /* 0002 KmPlot - a math. function plotter for the KDE-Desktop 0003 0004 SPDX-FileCopyrightText: 2006 David Saxton <david@bluehaze.org> 0005 0006 This file is part of the KDE Project. 0007 KmPlot is part of the KDE-EDU Project. 0008 0009 SPDX-License-Identifier: GPL-2.0-or-later 0010 0011 */ 0012 0013 #include "equationeditwidget.h" 0014 0015 #include <QFocusEvent> 0016 #include <QKeyEvent> 0017 #include <QStyle> 0018 #include <QStyleOptionFrame> 0019 #include <QWheelEvent> 0020 0021 #include "equationedit.h" 0022 0023 void EquationEditWidget::setClearSelectionOnFocusOut(bool doIt) 0024 { 0025 m_clearSelectionOnFocusOut = doIt; 0026 } 0027 0028 EquationEditWidget::EquationEditWidget(EquationEdit *parent) 0029 : KTextEdit(parent) 0030 { 0031 m_clearSelectionOnFocusOut = true; 0032 m_parent = parent; 0033 recalculateGeometry(); 0034 } 0035 0036 void EquationEditWidget::recalculateGeometry() 0037 { 0038 // Set fixed height 0039 ensurePolished(); 0040 QFontMetrics fm(document()->defaultFont()); 0041 int h = qMax(fm.lineSpacing(), 14) + 6; 0042 int m = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); 0043 QStyleOptionFrame opt; 0044 opt.rect = rect(); 0045 opt.palette = palette(); 0046 opt.state = QStyle::State_None; 0047 setFixedHeight(h + (2 * m)); 0048 0049 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); 0050 setContentsMargins(0, 0, 0, 0); 0051 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0052 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0053 setWordWrapMode(QTextOption::NoWrap); 0054 setLineWrapMode(KTextEdit::NoWrap); 0055 setTabChangesFocus(true); 0056 } 0057 0058 void EquationEditWidget::wheelEvent(QWheelEvent *e) 0059 { 0060 e->accept(); 0061 } 0062 0063 void EquationEditWidget::keyPressEvent(QKeyEvent *e) 0064 { 0065 if ((e->key() == Qt::Key_Return) || (e->key() == Qt::Key_Enter)) { 0066 e->accept(); 0067 emit m_parent->editingFinished(); 0068 emit m_parent->returnPressed(); 0069 } else { 0070 // Still pass these keys to KTextEdit, in case the user has to scroll 0071 // up/down the text 0072 if (e->key() == Qt::Key_Up) 0073 emit m_parent->upPressed(); 0074 else if (e->key() == Qt::Key_Down) 0075 emit m_parent->downPressed(); 0076 0077 KTextEdit::keyPressEvent(e); 0078 } 0079 } 0080 0081 void EquationEditWidget::focusOutEvent(QFocusEvent *e) 0082 { 0083 KTextEdit::focusOutEvent(e); 0084 0085 if (m_clearSelectionOnFocusOut) 0086 clearSelection(); 0087 m_parent->reHighlight(); 0088 0089 emit m_parent->editingFinished(); 0090 } 0091 0092 void EquationEditWidget::focusInEvent(QFocusEvent *e) 0093 { 0094 KTextEdit::focusOutEvent(e); 0095 0096 m_parent->reHighlight(); 0097 if (e->reason() == Qt::TabFocusReason) 0098 selectAll(); 0099 } 0100 0101 void EquationEditWidget::clearSelection() 0102 { 0103 QTextCursor cursor = textCursor(); 0104 if (!cursor.hasSelection()) 0105 return; 0106 0107 cursor.clearSelection(); 0108 setTextCursor(cursor); 0109 }