File indexing completed on 2024-05-12 05:53:26

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0003     SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef FILTER_CHAIN
0009 #define FILTER_CHAIN
0010 
0011 #include <QList>
0012 #include <QRegion>
0013 #include <QSharedPointer>
0014 #include <QString>
0015 
0016 #include "HotSpot.h"
0017 #include "konsoleprivate_export.h"
0018 
0019 class QLeaveEvent;
0020 class QPainter;
0021 
0022 namespace Konsole
0023 {
0024 class Filter;
0025 class HotSpot;
0026 class TerminalDisplay;
0027 
0028 /**
0029  * A chain which allows a group of filters to be processed as one.
0030  * The chain owns the filters added to it and deletes them when the chain itself is destroyed.
0031  *
0032  * Use addFilter() to add a new filter to the chain.
0033  * When new text to be filtered arrives, use addLine() to add each additional
0034  * line of text which needs to be processed and then after adding the last line, use
0035  * process() to cause each filter in the chain to process the text.
0036  *
0037  * After processing a block of text, the reset() method can be used to set the filter chain's
0038  * internal cursor back to the first line.
0039  *
0040  * The hotSpotAt() method will return the first hotspot which covers a given position.
0041  *
0042  * The hotSpots() method return all of the hotspots in the text and on
0043  * a given line respectively.
0044  */
0045 class KONSOLEPRIVATE_EXPORT FilterChain
0046 {
0047 public:
0048     explicit FilterChain(TerminalDisplay *terminalDisplay);
0049     virtual ~FilterChain();
0050 
0051     /** Adds a new filter to the chain.  The chain will delete this filter when it is destroyed */
0052     void addFilter(Filter *filter);
0053     /** Removes a filter from the chain.  The chain will no longer delete the filter when destroyed */
0054     void removeFilter(Filter *filter);
0055     /** Removes all filters from the chain */
0056     void clear();
0057 
0058     /** Resets each filter in the chain */
0059     void reset();
0060     /**
0061      * Processes each filter in the chain
0062      */
0063     void process();
0064 
0065     /** Sets the buffer for each filter in the chain to process. */
0066     void setBuffer(const QString *buffer, const QList<int> *linePositions);
0067 
0068     /** Returns the first hotspot which occurs at @p line, @p column or 0 if no hotspot was found */
0069     QSharedPointer<HotSpot> hotSpotAt(int line, int column) const;
0070     /** Returns a list of all the hotspots in all the chain's filters */
0071     QList<QSharedPointer<HotSpot>> hotSpots() const;
0072 
0073     /* Returns the region of the hotspot inside of the TerminalDisplay */
0074     QRegion hotSpotRegion() const;
0075 
0076     /* Returns the amount of hotspots of the given type */
0077     int count(HotSpot::Type type) const;
0078     QList<QSharedPointer<HotSpot>> filterBy(HotSpot::Type type) const;
0079 
0080     void mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev, int charLine, int charColumn);
0081     void mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev, int charLine, int charColumn);
0082     bool keyPressEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn);
0083     void keyReleaseEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn);
0084     void leaveEvent(TerminalDisplay *td, QEvent *ev);
0085 
0086     void paint(TerminalDisplay *td, QPainter &painter);
0087 
0088     void setReverseUrlHints(bool value);
0089     void setUrlHintsModifiers(Qt::KeyboardModifiers value);
0090     bool showUrlHint() const
0091     {
0092         return _showUrlHint;
0093     }
0094 
0095 protected:
0096     QList<Filter *> _filters;
0097     TerminalDisplay *_terminalDisplay;
0098     QSharedPointer<HotSpot> _hotSpotUnderMouse;
0099 
0100     /* TODO: this should be profile related, not here. but
0101      * currently this removes a bit of code from TerminalDisplay,
0102      * so it's a good compromise
0103      * */
0104     bool _showUrlHint;
0105     bool _reverseUrlHints;
0106     Qt::KeyboardModifiers _urlHintsModifiers;
0107 };
0108 
0109 }
0110 #endif