File indexing completed on 2024-04-14 05:44:24

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 "lookaheadwidget.h"
0008 
0009 #include <QPainter>
0010 
0011 #include <KLocalizedString>
0012 
0013 #include "concwidget.h"
0014 #include "lookaheadregexp.h"
0015 
0016 LookAheadWidget::LookAheadWidget(RegExpEditorWindow *editorWindow, RegExpType tp, QWidget *parent)
0017     : SingleContainerWidget(editorWindow, parent)
0018     , _tp(tp)
0019 {
0020     _child = new ConcWidget(editorWindow, this);
0021     init();
0022 }
0023 
0024 LookAheadWidget::LookAheadWidget(LookAheadRegExp *regexp, RegExpEditorWindow *editorWindow, RegExpType tp, QWidget *parent)
0025     : SingleContainerWidget(editorWindow, parent)
0026     , _tp(tp)
0027 {
0028     RegExpWidget *child = WidgetFactory::createWidget(regexp->child(), editorWindow, this);
0029     if (!(_child = dynamic_cast<ConcWidget *>(child))) {
0030         _child = new ConcWidget(editorWindow, child, this);
0031     }
0032 
0033     init();
0034 }
0035 
0036 void LookAheadWidget::init()
0037 {
0038     if (_tp == POSLOOKAHEAD) {
0039         _text = i18n("Pos. Look Ahead");
0040     } else {
0041         _text = i18n("Neg. Look Ahead");
0042     }
0043 }
0044 
0045 RegExp *LookAheadWidget::regExp() const
0046 {
0047     return new LookAheadRegExp(isSelected(), ((_tp == POSLOOKAHEAD) ? LookAheadRegExp::POSITIVE : LookAheadRegExp::NEGATIVE), _child->regExp());
0048 }
0049 
0050 QSize LookAheadWidget::sizeHint() const
0051 {
0052     // TODO: Merge with RepeatWidget::sizeHint
0053     QFontMetrics metrics = fontMetrics();
0054     _textSize = metrics.size(0, _text);
0055 
0056     _childSize = _child->sizeHint();
0057 
0058     int height = _textSize.height() + bdSize + _childSize.height() + bdSize + 2 * pw;
0059     int width = 2 * pw + qMax(_childSize.width(), 4 * bdSize + _textSize.width());
0060     return QSize(width, height);
0061 }
0062 
0063 void LookAheadWidget::paintEvent(QPaintEvent *e)
0064 {
0065     // TODO: Merge with RepeatWidget::paintEvent
0066     QSize mySize = sizeHint();
0067     QPainter painter(this);
0068 
0069     drawPossibleSelection(painter, mySize);
0070 
0071     // move the child to its position and resize it.
0072     _child->move(pw, _textSize.height() + bdSize);
0073     _child->resize(mySize.width() - 2 * pw, _childSize.height());
0074 
0075     // Draw the border and the text.
0076     int startY = _textSize.height() / 2;
0077 
0078     // Top lines and text
0079     painter.drawLine(pw, startY, bdSize, startY);
0080     painter.drawText(pw + 2 * bdSize, 0, _textSize.width(), _textSize.height(), 0, _text);
0081     int offset = pw + 3 * bdSize + _textSize.width();
0082     painter.drawLine(offset, startY, mySize.width() - pw, startY);
0083 
0084     // horizontal lines
0085     painter.drawLine(0, startY, 0, mySize.height() - pw);
0086     painter.drawLine(mySize.width() - pw, startY, mySize.width() - pw, mySize.height() - pw);
0087 
0088     // buttom line
0089     painter.drawLine(0, mySize.height() - pw, mySize.width() - pw, mySize.height() - pw);
0090 
0091     SingleContainerWidget::paintEvent(e);
0092 }
0093 
0094 RegExpWidget *LookAheadWidget::findWidgetToEdit(QPointF globalPos)
0095 {
0096     return _child->findWidgetToEdit(globalPos);
0097 }
0098 
0099 #include "moc_lookaheadwidget.cpp"