File indexing completed on 2024-04-21 05:51:40

0001 /*
0002  *  SPDX-FileCopyrightText: 2002-2003 Jesper K. Pedersen <blackie@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-only
0005  **/
0006 
0007 #include "textwidget.h"
0008 
0009 #include <QApplication>
0010 #include <QHBoxLayout>
0011 #include <QMouseEvent>
0012 
0013 #include "selectablelineedit.h"
0014 #include "textregexp.h"
0015 
0016 TextWidget::TextWidget(RegExpEditorWindow *editorWindow, QWidget *parent)
0017     : RegExpWidget(editorWindow, parent)
0018 {
0019     init(QString());
0020 }
0021 
0022 TextWidget::TextWidget(TextRegExp *regexp, RegExpEditorWindow *editorWindow, QWidget *parent)
0023     : RegExpWidget(editorWindow, parent)
0024 {
0025     init(regexp->text());
0026 }
0027 
0028 void TextWidget::init(const QString &txt)
0029 {
0030     QHBoxLayout *lay = new QHBoxLayout(this);
0031     _edit = new SelectableLineEdit(this, this, QStringLiteral("TextWidget::edit"));
0032     _edit->setDragEnabled(false); // otherwise QLineEdit::mouseMoveEvent will set the cursor over and over again.
0033     lay->addWidget(_edit);
0034 
0035     _edit->setText(txt);
0036 
0037     connect(_edit, &SelectableLineEdit::parentPleaseUpdate, this, &TextWidget::slotUpdate);
0038     setFocusProxy(_edit);
0039     _edit->installEventFilter(this);
0040     connect(_edit, &QLineEdit::textChanged, _editorWindow, &RegExpEditorWindow::emitChange);
0041 }
0042 
0043 void TextWidget::slotUpdate()
0044 {
0045     // I need to force the parent to repaint, as the size change of this
0046     // widget may not be enough for the parent to change size, and in that
0047     // case the parent would not repaint, and the text widget would not be
0048     // resized.
0049     QWidget *p = static_cast<QWidget *>(parent());
0050     if (p) {
0051         p->repaint();
0052     }
0053     _editorWindow->updateContent(this);
0054 }
0055 
0056 void TextWidget::paintEvent(QPaintEvent *e)
0057 {
0058     RegExpWidget::paintEvent(e);
0059 }
0060 
0061 void TextWidget::selectWidget(bool sel)
0062 {
0063     _edit->setSelected(sel);
0064 }
0065 
0066 bool TextWidget::updateSelection(bool parentSelected)
0067 {
0068     bool changed = RegExpWidget::updateSelection(parentSelected);
0069 
0070     // I need to call this function all the time, else the rubber band will
0071     // not be correctly deleted in the line edit.
0072     _edit->setSelected(_isSelected);
0073     return changed;
0074 }
0075 
0076 void TextWidget::updateAll()
0077 {
0078     _edit->update();
0079     update();
0080 }
0081 
0082 void TextWidget::clearSelection()
0083 {
0084     _isSelected = false;
0085     _edit->setSelected(false);
0086 }
0087 
0088 RegExp *TextWidget::regExp() const
0089 {
0090     return new TextRegExp(isSelected(), _edit->text());
0091 }
0092 
0093 bool TextWidget::eventFilter(QObject *, QEvent *event)
0094 {
0095     // This is an event filter (in contrast to methods in SelectableLineEdit),
0096     // otherwise lots of functions would need to be exported from TextWidget.
0097     if (event->type() == QEvent::MouseButtonRelease) {
0098         if (_editorWindow->isInserting()) {
0099             if (acceptWidgetInsert(_editorWindow->insertType())) {
0100                 mouseReleaseEvent(static_cast<QMouseEvent *>(event));
0101             }
0102             return true;
0103         }
0104     } else if (event->type() == QEvent::MouseButtonPress) {
0105         if (_editorWindow->isInserting()) {
0106             return true;
0107         } else if (isSelected()) {
0108             QMouseEvent *e = static_cast<QMouseEvent *>(event);
0109             QMouseEvent ev(event->type(), mapTo(_editorWindow, e->pos()), e->button(), e->buttons(), e->modifiers());
0110             QApplication::sendEvent(_editorWindow, &ev);
0111             return true;
0112         }
0113     } else if (event->type() == QEvent::Enter) {
0114         if (_editorWindow->isInserting()) {
0115             if (acceptWidgetInsert(_editorWindow->insertType())) {
0116                 _edit->setCursor(Qt::CrossCursor);
0117             } else {
0118                 _edit->setCursor(Qt::ForbiddenCursor);
0119             }
0120         } else if (isSelected()) {
0121             _edit->setCursor(Qt::ArrowCursor);
0122         } else {
0123             _edit->setCursor(Qt::IBeamCursor);
0124         }
0125     } else if (event->type() == QEvent::MouseButtonDblClick && _editorWindow->isInserting()) {
0126         return true;
0127     }
0128     return false;
0129 }
0130 
0131 #include "moc_textwidget.cpp"