File indexing completed on 2024-05-19 05:54:09

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_H
0009 #define FILTER_H
0010 
0011 // Qt
0012 #include <QList>
0013 #include <QMultiHash>
0014 #include <QPoint>
0015 
0016 // KDE
0017 #include <KFileItem>
0018 
0019 #include <memory>
0020 
0021 // Konsole
0022 #include "../characters/Character.h"
0023 #include "konsoleprivate_export.h"
0024 
0025 class QAction;
0026 class QMenu;
0027 class QMouseEvent;
0028 
0029 namespace Konsole
0030 {
0031 class Session;
0032 class HotSpot;
0033 
0034 /**
0035  * A filter processes blocks of text looking for certain patterns (such as URLs or keywords from a list)
0036  * and marks the areas which match the filter's patterns as 'hotspots'.
0037  *
0038  * Each hotspot has a type identifier associated with it ( such as a link or a highlighted section ),
0039  * and an action.  When the user performs some activity such as a mouse-click in a hotspot area ( the exact
0040  * action will depend on what is displaying the block of text which the filter is processing ), the hotspot's
0041  * activate() method should be called.  Depending on the type of hotspot this will trigger a suitable response.
0042  *
0043  * For example, if a hotspot represents a URL then a suitable action would be opening that URL in a web browser.
0044  * Hotspots may have more than one action, in which case the list of actions can be obtained using the
0045  * actions() method.
0046  *
0047  * Different subclasses of filter will return different types of hotspot.
0048  * Subclasses must reimplement the process() method to examine a block of text and identify sections of interest.
0049  * When processing the text they should create instances of Filter::HotSpot subclasses for sections of interest
0050  * and add them to the filter's list of hotspots using addHotSpot()
0051  */
0052 class KONSOLEPRIVATE_EXPORT Filter
0053 {
0054 public:
0055     /** Constructs a new filter. */
0056     Filter();
0057     virtual ~Filter();
0058 
0059     /** Causes the filter to process the block of text currently in its internal buffer */
0060     virtual void process() = 0;
0061 
0062     /**
0063      * Empties the filters internal buffer and resets the line count back to 0.
0064      * All hotspots are deleted.
0065      */
0066     void reset();
0067 
0068     /** Returns the hotspot which covers the given @p line and @p column, or 0 if no hotspot covers that area */
0069     QSharedPointer<HotSpot> hotSpotAt(int line, int column) const;
0070 
0071     /** Returns the list of hotspots identified by the filter */
0072     QList<QSharedPointer<HotSpot>> hotSpots() const;
0073 
0074     /** Returns the list of hotspots identified by the filter which occur on a given line */
0075 
0076     /**
0077      * TODO: Document me
0078      */
0079     void setBuffer(const QString *buffer, const QList<int> *linePositions);
0080 
0081 protected:
0082     /** Adds a new hotspot to the list */
0083     void addHotSpot(QSharedPointer<HotSpot> spot);
0084     /** Returns the internal buffer */
0085     const QString *buffer();
0086     /** Converts a character position within buffer() to a line and column */
0087     std::pair<int, int> getLineColumn(int prevline, int position);
0088 
0089 private:
0090     Q_DISABLE_COPY(Filter)
0091 
0092     QMultiHash<int, QSharedPointer<HotSpot>> _hotspots;
0093     QList<QSharedPointer<HotSpot>> _hotspotList;
0094 
0095     const QList<int> *_linePositions;
0096     const QString *_buffer;
0097 };
0098 
0099 } // namespace Konsole
0100 #endif