File indexing completed on 2024-04-21 05:51:39
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 "regexpwidget.h" 0008 0009 #include <QApplication> 0010 #include <QMouseEvent> 0011 #include <QPainter> 0012 0013 #include "concwidget.h" 0014 #include "dragaccepter.h" 0015 0016 const int RegExpWidget::pw = 1; 0017 const int RegExpWidget::bdSize = 5; 0018 const int RegExpWidget::space = 5; 0019 0020 RegExpWidget::RegExpWidget(RegExpEditorWindow *editorWindow, QWidget *parent) 0021 : QWidget(parent) 0022 , _editorWindow(editorWindow) 0023 , _isSelected(false) 0024 , _isToplevel(false) 0025 { 0026 setAttribute(Qt::WA_NoMousePropagation); 0027 } 0028 0029 void RegExpWidget::addNewChild(DragAccepter *, RegExpWidget *) 0030 { 0031 qFatal("This widget should not expect any children"); 0032 } 0033 0034 void RegExpWidget::addNewConcChild(DragAccepter *, ConcWidget *) 0035 { 0036 qFatal("This widget should not expect any children"); 0037 } 0038 0039 void RegExpWidget::setConcChild(ConcWidget *) 0040 { 0041 qFatal("This widget should not expect any children"); 0042 } 0043 0044 bool RegExpWidget::updateSelection(bool parentSelected) 0045 { 0046 bool selected = (parentSelected || _editorWindow->selectionOverlap(mapToGlobal(QPoint(0, 0)), size())) && !_isToplevel; 0047 0048 if (_isSelected != selected) { 0049 // Selection state changed 0050 _isSelected = selected; 0051 repaint(); 0052 return true; 0053 } 0054 return false; 0055 } 0056 0057 void RegExpWidget::drawPossibleSelection(QPainter &painter, QSize mySize) 0058 { 0059 if (_isSelected) { 0060 painter.fillRect(0, 0, mySize.width(), mySize.height(), QBrush(Qt::gray)); 0061 } 0062 } 0063 0064 bool RegExpWidget::isSelected() const 0065 { 0066 return _isSelected; 0067 } 0068 0069 bool RegExpWidget::hasSelection() const 0070 { 0071 return _isSelected; 0072 } 0073 0074 void RegExpWidget::clearSelection() 0075 { 0076 _isSelected = false; 0077 } 0078 0079 void RegExpWidget::applyRegExpToSelection(RegExpType) 0080 { 0081 qFatal("This method should not be called for this widget"); 0082 } 0083 0084 void RegExpWidget::deleteSelection() 0085 { 0086 qFatal("This method should be overridden if needed!"); 0087 } 0088 0089 RegExp *RegExpWidget::selection() const 0090 { 0091 return regExp(); 0092 } 0093 0094 int RegExpWidget::edit() 0095 { 0096 qFatal("This method should be overridden if needed!"); 0097 return 0; // Compiler shut up 0098 } 0099 0100 void RegExpWidget::mousePressEvent(QMouseEvent *event) 0101 { 0102 if (_editorWindow->isPasteing() || _editorWindow->isInserting()) { 0103 return; 0104 } 0105 0106 if (event->button() == Qt::LeftButton) { 0107 if (!_editorWindow->pointSelected(QCursor::pos())) { 0108 _editorWindow->clearSelection(true); 0109 if (dynamic_cast<DragAccepter *>(this) == nullptr && dynamic_cast<ConcWidget *>(this) == nullptr) { 0110 selectWidget(true); 0111 } 0112 } 0113 0114 QMouseEvent ev(event->type(), mapTo(_editorWindow, event->pos()), event->button(), event->buttons(), event->modifiers()); 0115 QApplication::sendEvent(_editorWindow, &ev); 0116 } else if (event->button() == Qt::RightButton) { 0117 _editorWindow->showRMBMenu(true); 0118 } 0119 0120 // currently (Qt3.0) it seems like qt do not accept that the accept flag is set, 0121 // and thus sends the event to the parent - given that the following line is in. 0122 // It doesn't make any change to leave it out. 0123 // 25 Oct. 2001 19:03 -- Jesper K. Pedersen 0124 // QWidget::mousePressEvent( event ); 0125 } 0126 0127 void RegExpWidget::mouseReleaseEvent(QMouseEvent *) 0128 { 0129 if (_editorWindow->isInserting() && acceptWidgetInsert(_editorWindow->insertType())) { 0130 if (!_editorWindow->hasSelection()) { 0131 _isSelected = true; 0132 } 0133 0134 _editorWindow->applyRegExpToSelection(_editorWindow->insertType()); 0135 _editorWindow->clearSelection(true); 0136 _editorWindow->updateContent(this); 0137 _editorWindow->slotEndActions(); 0138 _editorWindow->updateCursorUnderPoint(); 0139 } 0140 } 0141 0142 QRect RegExpWidget::selectionRect() const 0143 { 0144 return QRect(mapToGlobal(QPoint(0, 0)), size()); 0145 } 0146 0147 void RegExpWidget::enterEvent(QEnterEvent *) 0148 { 0149 updateCursorShape(); 0150 } 0151 0152 void RegExpWidget::updateCursorShape() 0153 { 0154 QCursor cursor; 0155 0156 if (_editorWindow->isPasteing()) { 0157 if (acceptWidgetPaste()) { 0158 cursor = Qt::CrossCursor; 0159 } else { 0160 cursor = Qt::ForbiddenCursor; 0161 } 0162 } else if (_editorWindow->isInserting()) { 0163 if (acceptWidgetInsert(_editorWindow->insertType())) { 0164 cursor = Qt::CrossCursor; 0165 } else { 0166 cursor = Qt::ForbiddenCursor; 0167 } 0168 } else { 0169 cursor = Qt::ArrowCursor; 0170 } 0171 0172 setCursor(cursor); 0173 } 0174 0175 void RegExpWidget::updateCursorRecursively() 0176 { 0177 updateCursorShape(); 0178 } 0179 0180 bool RegExpWidget::acceptWidgetPaste() const 0181 { 0182 return false; 0183 } 0184 0185 bool RegExpWidget::acceptWidgetInsert(RegExpType tp) const 0186 { 0187 return WidgetFactory::isContainer(tp); 0188 } 0189 0190 RegExpWidget *RegExpWidget::widgetUnderPoint(QPointF globalPos, bool) 0191 { 0192 if (QRectF(mapToGlobal(QPointF(0, 0)), size()).contains(globalPos)) { 0193 return this; 0194 } else { 0195 return nullptr; 0196 } 0197 } 0198 0199 void RegExpWidget::selectWidget(bool sel) 0200 { 0201 _isSelected = sel; 0202 update(); 0203 } 0204 0205 void RegExpWidget::updateAll() 0206 { 0207 update(); 0208 } 0209 0210 #include "moc_regexpwidget.cpp"