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 #ifndef __regexpwidget
0008 #define __regexpwidget
0009 
0010 #include "regexpeditorwindow.h"
0011 
0012 class DragAccepter;
0013 class RegExp;
0014 class ConcWidget;
0015 class RegExpEditorWindow;
0016 
0017 /**
0018    Base class for all regular expression widgets.
0019    @internal
0020 */
0021 class RegExpWidget : public QWidget
0022 {
0023     Q_OBJECT
0024 
0025 public:
0026     RegExpWidget(RegExpEditorWindow *editorWindow, QWidget *parent);
0027 
0028     /*======================================================================
0029        Construction, child management
0030        ======================================================================*/
0031     /**
0032        Add `child' to the parent of this widget in place of `accepter'
0033        This method only applies to container widgets (see @ref
0034        SingleContainerWidget, and @ref MultiContainerWidget).
0035     */
0036     virtual void addNewChild(DragAccepter *accepter, RegExpWidget *child);
0037 
0038     /**
0039        Inserts all the children of `child' into this widget in place of
0040        `accepter'.
0041        This method only applies to container widgets (see @ref
0042        SingleContainerWidget, and @ref MultiContainerWidget).
0043     */
0044     virtual void addNewConcChild(DragAccepter *accepter, ConcWidget *child);
0045 
0046     /**
0047        Set `child' as the ConcWidget. This method should only be invoked when
0048        the widget has just been constructed.
0049        This method only applies to container widgets (see @ref
0050        SingleContainerWidget, and @ref MultiContainerWidget).
0051     */
0052     virtual void setConcChild(ConcWidget *child);
0053 
0054     /* ======================================================================
0055        Selection
0056        ======================================================================*/
0057 
0058     /**
0059        Marks this widget as being the immediate child of the editor
0060        window. That is the topmost regular expression widget.
0061 
0062        This information is necessary when drawing the widget, and when
0063        drawing the selection.
0064     */
0065     void setToplevel()
0066     {
0067         _isToplevel = true;
0068     }
0069 
0070     /**
0071        Returns true if this widget is selected.
0072     */
0073     virtual bool isSelected() const;
0074 
0075     /**
0076        Returns true if this widget or one of its children is selected.
0077     */
0078     virtual bool hasSelection() const;
0079 
0080     virtual void updateAll();
0081 
0082     /**
0083        Update selection information for this widget.
0084        @param parentSelected indicates whether the parent is selected.
0085        @return true if the selection state has changed for the widget since
0086        the last time the widget was painted. `repaint' is invoked on the widget if
0087        selection state is changed, to ensure that selection is visible.
0088     */
0089     virtual bool updateSelection(bool parentSelected);
0090 
0091     /**
0092        Clears the selection and repaints the widget if `update' is true.
0093     */
0094     virtual void clearSelection();
0095 
0096     /**
0097        Deletes the regexp widgets containing selection.
0098     */
0099     virtual void deleteSelection();
0100 
0101     /**
0102        See @ref RegExpEditorWindow::applyRegExpToSelection
0103     */
0104     virtual void applyRegExpToSelection(RegExpType type);
0105 
0106     /**
0107        Validate that selection is OK. Returns false if not.
0108 
0109        Only one alternative may be selected in @ref AltnWidget. This
0110        limitation is to make implementation of a number of functions simpler.
0111     */
0112     virtual bool validateSelection() const
0113     {
0114         return true;
0115     }
0116 
0117     /**
0118        Returns the rectangle which makes up the selection.
0119     */
0120     virtual QRect selectionRect() const;
0121 
0122     /** Selects the RegExp widget and all its chidlren*/
0123     virtual void selectWidget(bool sel);
0124 
0125     /*======================================================================
0126       Misc
0127       ======================================================================*/
0128 
0129     /** Returns the RegExp widget, which is rooted in this regexp widget. */
0130     virtual RegExp *regExp() const = 0;
0131 
0132     /** returns the current selection */
0133     virtual RegExp *selection() const;
0134 
0135     /** returns the type of this regular expression widget. */
0136     virtual RegExpType type() const = 0;
0137 
0138     /**
0139        updates the cursor to be one of
0140        @li normal selection cursor
0141        @li accept cursor for insert/paste
0142        @li reject cursor for insert/paste
0143     */
0144     void updateCursorShape();
0145 
0146     virtual void updateCursorRecursively();
0147 
0148     /**
0149        Returns the regexp widget under point. If `justVisibleWidgets' is
0150        true, @ref ConcWidget is ignored. That is, a ConcWidget will not be returned.
0151     */
0152     virtual RegExpWidget *widgetUnderPoint(QPointF globalPos, bool justVisibleWidgets);
0153 
0154     /**
0155        Returns the widget under point which can be edited. That is the
0156        "innermost" widget editable.
0157     */
0158     virtual RegExpWidget *findWidgetToEdit(QPointF /* globalPos */)
0159     {
0160         return nullptr;
0161     }
0162 
0163     /**
0164        Edits the current widget. That is, bring up the dialog, which is
0165        available for @ref ConcWidget, @ref CompoundWidget, and @ref
0166        CharactersWidget.
0167     */
0168     virtual int edit();
0169 
0170 protected:
0171     /** Draws a selection rectangle for the current widget.  */
0172     virtual void drawPossibleSelection(QPainter &painter, QSize mySize);
0173 
0174     /** Returns true if the widget accepts paste actions. */
0175     virtual bool acceptWidgetPaste() const;
0176 
0177     /**
0178        Returns true if the widget accept the given RegExpType to be inserted
0179      */
0180     virtual bool acceptWidgetInsert(RegExpType) const;
0181 
0182     void mousePressEvent(QMouseEvent *event) override;
0183 
0184     void mouseReleaseEvent(QMouseEvent *) override;
0185     void enterEvent(QEnterEvent *) override;
0186 
0187     RegExpEditorWindow *_editorWindow = nullptr;
0188 
0189     /** True if the widget is selected. */
0190     bool _isSelected;
0191 
0192     /** See @ref setToplevel */
0193     bool _isToplevel;
0194 
0195     static const int pw; // width of pen
0196     static const int bdSize; // width between borders and text etc.
0197     static const int space; // TODO.
0198 };
0199 
0200 #endif // __regexpwidget