File indexing completed on 2024-05-12 15:28:20

0001 /***************************************************************************
0002     File                 : ResizableTextEdit.cpp
0003     Project              : LabPlot
0004     Description          : Extended TextEdit to allow the manual resize by the user
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2018 Alexander Semke (alexander.semke@web.de)
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 #include "ResizableTextEdit.h"
0029 
0030 #include <QApplication>
0031 #include <QMouseEvent>
0032 #include <QPainter>
0033 
0034 GrabBar::GrabBar(ResizableTextEdit* parent, bool vertResizeOnly) : QWidget(parent),
0035     m_parent(parent), m_vertResizeOnly(vertResizeOnly) {
0036 
0037     resize(20,10);
0038 }
0039 
0040 QSize GrabBar::sizeHint() const {
0041     return QSize{20, 10};
0042 }
0043 
0044 void GrabBar::paintEvent(QPaintEvent*) {
0045     QPainter p(this);
0046     p.setBrush(QApplication::palette().color(QPalette::AlternateBase));
0047     p.drawRect(rect());
0048 }
0049 
0050 void GrabBar::mousePressEvent(QMouseEvent* e) {
0051     if (e->button() == Qt::LeftButton) {
0052         m_pos = e->pos();
0053         m_pressed = true;
0054     }
0055 
0056     e->accept();
0057 }
0058 
0059 void GrabBar::mouseReleaseEvent(QMouseEvent* e) {
0060     if (e->button() == Qt::LeftButton)
0061         m_pressed = false;
0062 
0063     e->accept();
0064 }
0065 
0066 void GrabBar::mouseMoveEvent(QMouseEvent* e) {
0067     if (m_pressed) {
0068         const QPoint delta = e->pos() - m_pos;
0069         if (m_vertResizeOnly)
0070             m_parent->addSize( QSize( 0, delta.y() ) );
0071         else
0072             m_parent->addSize( QSize( delta.x(), delta.y() ) );
0073     }
0074 
0075     e->accept();
0076 }
0077 
0078 void GrabBar::enterEvent(QEvent* e) {
0079     if (m_vertResizeOnly)
0080         QApplication::setOverrideCursor(QCursor(Qt::SizeVerCursor));
0081     else
0082         QApplication::setOverrideCursor(QCursor(Qt::SizeFDiagCursor));
0083 
0084     e->accept();
0085 }
0086 
0087 void GrabBar::leaveEvent(QEvent* e) {
0088     QApplication::restoreOverrideCursor();
0089     e->accept();
0090 }
0091 
0092 /**
0093  * \class ResizableTextEdit
0094  * \brief Extended QTextEdit supporting the manual resize by the user.
0095  *
0096  * \ingroup frontend/widgets
0097  */
0098 ResizableTextEdit::ResizableTextEdit(QWidget* parent, bool vertResizeOnly) : QTextEdit(parent),
0099     m_grabBar(new GrabBar(this, vertResizeOnly)),
0100     m_size( QTextEdit::sizeHint() ),
0101     m_vertResizeOnly(vertResizeOnly) {
0102 
0103 }
0104 
0105 void ResizableTextEdit::addSize(QSize size) {
0106     m_size = QSize(m_size.width(), m_size.height() + size.height());
0107     updateGeometry();
0108 }
0109 
0110 QSize ResizableTextEdit::sizeHint() const {
0111     return m_size;
0112 }
0113 
0114 void ResizableTextEdit::resizeEvent(QResizeEvent* e) {
0115     if (m_vertResizeOnly)
0116         m_grabBar->move(width()/2 - m_grabBar->width()/2, height() - m_grabBar->height());
0117     else
0118         m_grabBar->move(width() - m_grabBar->width(), height() - m_grabBar->height());
0119 
0120     m_size = e->size();
0121 
0122     QTextEdit::resizeEvent(e);
0123 
0124     e->accept();
0125 }