GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Filter.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007, 2013 by Robert Knight <robertknight@gmail.com>
3 
4  Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
5  Adoption to octave by Torsten <mttl@mailbox.org>, Copyright (c) 2017
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  02110-1301 USA.
21 */
22 
23 #ifndef FILTER_H
24 #define FILTER_H
25 
26 // Qt
27 #include <QAction>
28 #include <QtCore/QList>
29 #include <QtCore/QObject>
30 #include <QtCore/QStringList>
31 #include <QtCore/QHash>
32 #include <QtCore/QRegExp>
33 
34 // Local
35 #include "unix/Character.h"
36 
37 /**
38  * A filter processes blocks of text looking for certain patterns (such as URLs or keywords from a list)
39  * and marks the areas which match the filter's patterns as 'hotspots'.
40  *
41  * Each hotspot has a type identifier associated with it ( such as a link or a highlighted section ),
42  * and an action. When the user performs some activity such as a mouse-click in a hotspot area ( the exact
43  * action will depend on what is displaying the block of text which the filter is processing ), the hotspot's
44  * activate() method should be called. Depending on the type of hotspot this will trigger a suitable response.
45  *
46  * For example, if a hotspot represents a URL then a suitable action would be opening that URL in a web browser.
47  * Hotspots may have more than one action, in which case the list of actions can be obtained using the
48  * actions() method.
49  *
50  * Different subclasses of filter will return different types of hotspot.
51  * Subclasses must reimplement the process() method to examine a block of text and identify sections of interest.
52  * When processing the text they should create instances of Filter::HotSpot subclasses for sections of interest
53  * and add them to the filter's list of hotspots using addHotSpot()
54  */
55 class Filter : public QObject
56 {
57 
58  Q_OBJECT
59 
60 public:
61  /**
62  * Represents an area of text which matched the pattern a particular filter has been looking for.
63  *
64  * Each hotspot has a type identifier associated with it ( such as a link or a highlighted section ),
65  * and an action. When the user performs some activity such as a mouse-click in a hotspot area ( the exact
66  * action will depend on what is displaying the block of text which the filter is processing ), the hotspot's
67  * activate() method should be called. Depending on the type of hotspot this will trigger a suitable response.
68  *
69  * For example, if a hotspot represents a URL then a suitable action would be opening that URL in a web browser.
70  * Hotspots may have more than one action, in which case the list of actions can be obtained using the
71  * actions() method. These actions may then be displayed in a popup menu or toolbar for example.
72  */
73 
74  enum Type
75  {
76  // the type of the hotspot is not specified
78  // this hotspot represents a clickable link
80  // this hotspot represents a marker
82  // this hotspot represents a clickable link to an erroneous file
83  ErrorLink
84  };
85 
86  class HotSpot : public QObject
87  {
88 
89  public:
90  /**
91  * Constructs a new hotspot which covers the area from (@p startLine,@p startColumn) to (@p endLine,@p endColumn)
92  * in a block of text.
93  */
94  HotSpot(int startLine , int startColumn , int endLine , int endColumn);
95  virtual ~HotSpot();
96 
97  /** Returns the line when the hotspot area starts */
98  int startLine() const;
99  /** Returns the line where the hotspot area ends */
100  int endLine() const;
101  /** Returns the column on startLine() where the hotspot area starts */
102  int startColumn() const;
103  /** Returns the column on endLine() where the hotspot area ends */
104  int endColumn() const;
105  /**
106  * Returns the type of the hotspot. This is usually used as a hint for views on how to represent
107  * the hotspot graphically. eg. Link hotspots are typically underlined when the user mouses over them
108  */
109  Type type() const;
110  /**
111  * Causes the an action associated with a hotspot to be triggered.
112  *
113  * @param object The object which caused the hotspot to be triggered. This is
114  * typically null ( in which case the default action should be performed ) or
115  * one of the objects from the actions() list. In which case the associated
116  * action should be performed.
117  */
118  virtual void activate(QObject* object = nullptr) = 0;
119  /**
120  * Returns a list of actions associated with the hotspot which can be used in a
121  * menu or toolbar
122  */
123  virtual QList<QAction*> actions();
124 
125  /**
126  * Returns the text of a tooltip to be shown when the mouse moves over the hotspot, or
127  * an empty string if there is no tooltip associated with this hotspot.
128  *
129  * The default implementation returns an empty string.
130  */
131  virtual QString tooltip() const;
132 
133  protected:
134  /** Sets the type of a hotspot. This should only be set once */
135  void setType(Type type);
136 
137  private:
140  int _endLine;
143 
144  };
145 
146  /** Constructs a new filter. */
147  Filter();
148  virtual ~Filter();
149 
150  /** Causes the filter to process the block of text currently in its internal buffer */
151  virtual void process() = 0;
152 
153  /**
154  * Empties the filters internal buffer and resets the line count back to 0.
155  * All hotspots are deleted.
156  */
157  void reset();
158 
159  /** Adds a new line of text to the filter and increments the line count */
160  //void addLine(const QString& string);
161 
162  /** Returns the hotspot which covers the given @p line and @p column, or 0 if no hotspot covers that area */
163  HotSpot* hotSpotAt(int line , int column) const;
164 
165  /** Returns the list of hotspots identified by the filter */
166  QList<HotSpot*> hotSpots() const;
167 
168  /** Returns the list of hotspots identified by the filter which occur on a given line */
170 
171  /**
172  * TODO: Document me
173  */
174  void setBuffer(const QString* buffer , const QList<int>* linePositions);
175 
176 protected:
177  /** Adds a new hotspot to the list */
178  void addHotSpot(HotSpot*);
179  /** Returns the internal buffer */
180  const QString* buffer();
181  /** Converts a character position within buffer() to a line and column */
182  void getLineColumn(int position , int& startLine , int& startColumn);
183 
184 private:
185  QMultiHash<int,HotSpot*> _hotspots;
187 
189  const QString* _buffer;
190 };
191 
192 /**
193  * A filter which searches for sections of text matching a regular expression and creates a new RegExpFilter::HotSpot
194  * instance for them.
195  *
196  * Subclasses can reimplement newHotSpot() to return custom hotspot types when matches for the regular expression
197  * are found.
198  */
199 class RegExpFilter : public Filter
200 {
201 
202  Q_OBJECT
203 
204 public:
205  /**
206  * Type of hotspot created by RegExpFilter. The capturedTexts() method can be used to find the text
207  * matched by the filter's regular expression.
208  */
209  class HotSpot : public Filter::HotSpot
210  {
211  public:
212  HotSpot(int startLine, int startColumn,
213  int endLine , int endColumn, Filter::Type);
214  virtual void activate(QObject* object = nullptr);
215 
216  /** Sets the captured texts associated with this hotspot */
217  void setCapturedTexts(const QStringList& texts);
218  /** Returns the texts found by the filter when matching the filter's regular expression */
219  QStringList capturedTexts() const;
220  private:
221  QStringList _capturedTexts;
222  };
223 
224  /** Constructs a new regular expression filter */
225  RegExpFilter (Type);
226 
227  /**
228  * Sets the regular expression which the filter searches for in blocks of text.
229  *
230  * Regular expressions which match the empty string are treated as not matching
231  * anything.
232  */
233  void setRegExp(const QRegExp& text);
234  /** Returns the regular expression which the filter searches for in blocks of text */
235  QRegExp regExp() const;
236 
237  /**
238  * Reimplemented to search the filter's text buffer for text matching regExp()
239  *
240  * If regexp matches the empty string, then process() will return immediately
241  * without finding results.
242  */
243  virtual void process();
244 
245 signals:
246 
247  void request_edit_mfile_signal (const QString&, int);
248  void request_open_file_signal (const QString&, int);
249 
250 protected:
251  /**
252  * Called when a match for the regular expression is encountered. Subclasses should reimplement this
253  * to return custom hotspot types
254  */
255  virtual RegExpFilter::HotSpot* newHotSpot(int startLine,int startColumn,
256  int endLine,int endColumn, Type);
258 
259  QRegExp _searchText;
260 };
261 
262 class FilterObject;
263 
264 /** A filter which matches URLs in blocks of text */
265 class UrlFilter : public RegExpFilter
266 {
267 
268  Q_OBJECT
269 
270 public:
271  /**
272  * Hotspot type created by UrlFilter instances. The activate() method opens a web browser
273  * at the given URL when called.
274  */
276  {
277  public:
278  HotSpot(int startLine,int startColumn,int endLine,int endColumn,Type t);
279  virtual ~HotSpot();
280 
281  virtual QList<QAction*> actions();
282 
283  /**
284  * Open a web browser at the current URL. The url itself can be determined using
285  * the capturedTexts() method.
286  */
287  virtual void activate(QObject* object = nullptr);
288 
289  virtual QString tooltip() const;
290 
292 
293  private:
294  enum UrlType
295  {
300  Unknown
301  };
302  UrlType urlType() const;
303 
305  };
306 
307  UrlFilter (Type t = Link);
308 
309  virtual void process();
310 
311 public slots:
312  void request_open_file (const QString&, int);
313 
314 protected:
315  virtual HotSpot* newHotSpot(int,int,int,int,Type);
316 
317 private:
318 
319  static const QRegExp FullUrlRegExp;
320  static const QRegExp EmailAddressRegExp;
321  static const QRegExp ErrorLinkRegExp;
322  static const QRegExp ParseErrorLinkRegExp;
323  static const QRegExp CompleteErrorLinkRegExp;
324 
325  // combined OR of FullUrlRegExp and EmailAddressRegExp
326  static const QRegExp CompleteUrlRegExp;
327 };
328 
329 class FilterObject : public QObject
330 {
331 Q_OBJECT
332 public:
334  void request_open_file (const QString& file, int line)
335  { emit request_open_file_signal (file, line); }
336 signals:
337  void request_open_file_signal (const QString&, int);
338 private slots:
339  void activated();
340 private:
342 };
343 
344 /**
345  * A chain which allows a group of filters to be processed as one.
346  * The chain owns the filters added to it and deletes them when the chain itself is destroyed.
347  *
348  * Use addFilter() to add a new filter to the chain.
349  * When new text to be filtered arrives, use addLine() to add each additional
350  * line of text which needs to be processed and then after adding the last line, use
351  * process() to cause each filter in the chain to process the text.
352  *
353  * After processing a block of text, the reset() method can be used to set the filter chain's
354  * internal cursor back to the first line.
355  *
356  * The hotSpotAt() method will return the first hotspot which covers a given position.
357  *
358  * The hotSpots() and hotSpotsAtLine() method return all of the hotspots in the text and on
359  * a given line respectively.
360  */
361 class FilterChain : protected QList<Filter*>
362 {
363 public:
364  virtual ~FilterChain();
365 
366  /** Adds a new filter to the chain. The chain will delete this filter when it is destroyed */
367  void addFilter(Filter* filter);
368  /** Removes a filter from the chain. The chain will no longer delete the filter when destroyed */
369  void removeFilter(Filter* filter);
370  /** Returns true if the chain contains @p filter */
372  /** Removes all filters from the chain */
373  void clear();
374 
375  /** Resets each filter in the chain */
376  void reset();
377  /**
378  * Processes each filter in the chain
379  */
380  void process();
381 
382  /** Sets the buffer for each filter in the chain to process. */
383  void setBuffer(const QString* buffer , const QList<int>* linePositions);
384 
385  /** Returns the first hotspot which occurs at @p line, @p column or 0 if no hotspot was found */
386  Filter::HotSpot* hotSpotAt(int line , int column) const;
387  /** Returns a list of all the hotspots in all the chain's filters */
389  /** Returns a list of all hotspots at the given line in all the chain's filters */
391 
392 };
393 
394 /** A filter chain which processes character images from terminal displays */
396 {
397 public:
399  virtual ~TerminalImageFilterChain();
400 
401  /**
402  * Set the current terminal image to @p image.
403  *
404  * @param image The terminal image
405  * @param lines The number of lines in the terminal image
406  * @param columns The number of columns in the terminal image
407  */
408  void setImage(const Character* const image , int lines , int columns,
409  const QVector<LineProperty>& lineProperties);
410 
411 private:
412  QString* _buffer;
414 };
415 
416 #endif //FILTER_H
A single character in the terminal which consists of a unicode character value, foreground and backgr...
Definition: Character.h:56
A chain which allows a group of filters to be processed as one.
Definition: Filter.h:362
void process()
Processes each filter in the chain.
Definition: Filter.cpp:75
bool containsFilter(Filter *filter)
Returns true if the chain contains filter.
Definition: Filter.cpp:59
void addFilter(Filter *filter)
Adds a new filter to the chain.
Definition: Filter.cpp:51
void removeFilter(Filter *filter)
Removes a filter from the chain.
Definition: Filter.cpp:55
void reset()
Resets each filter in the chain.
Definition: Filter.cpp:63
QList< Filter::HotSpot > hotSpotsAtLine(int line) const
Returns a list of all hotspots at the given line in all the chain's filters.
Filter::HotSpot * hotSpotAt(int line, int column) const
Returns the first hotspot which occurs at line, column or 0 if no hotspot was found.
Definition: Filter.cpp:85
virtual ~FilterChain()
Definition: Filter.cpp:39
QList< Filter::HotSpot * > hotSpots() const
Returns a list of all the hotspots in all the chain's filters.
Definition: Filter.cpp:101
void setBuffer(const QString *buffer, const QList< int > *linePositions)
Sets the buffer for each filter in the chain to process.
Definition: Filter.cpp:69
void clear()
Removes all filters from the chain.
Definition: Filter.cpp:81
void request_open_file_signal(const QString &, int)
void activated()
Definition: Filter.cpp:606
Filter::HotSpot * _filter
Definition: Filter.h:341
void request_open_file(const QString &file, int line)
Definition: Filter.h:334
FilterObject(Filter::HotSpot *filter)
Definition: Filter.h:333
virtual QString tooltip() const
Returns the text of a tooltip to be shown when the mouse moves over the hotspot, or an empty string i...
Definition: Filter.cpp:294
int endLine() const
Returns the line where the hotspot area ends.
Definition: Filter.cpp:306
int _startLine
Definition: Filter.h:138
int startLine() const
Returns the line when the hotspot area starts.
Definition: Filter.cpp:302
int _endColumn
Definition: Filter.h:141
virtual ~HotSpot()
Definition: Filter.cpp:246
int endColumn() const
Returns the column on endLine() where the hotspot area ends.
Definition: Filter.cpp:314
virtual void activate(QObject *object=nullptr)=0
Causes the an action associated with a hotspot to be triggered.
virtual QList< QAction * > actions()
Returns a list of actions associated with the hotspot which can be used in a menu or toolbar.
Definition: Filter.cpp:298
void setType(Type type)
Sets the type of a hotspot.
Definition: Filter.cpp:322
Type type() const
Returns the type of the hotspot.
Definition: Filter.cpp:318
int _startColumn
Definition: Filter.h:139
HotSpot(int startLine, int startColumn, int endLine, int endColumn)
Constructs a new hotspot which covers the area from (startLine,startColumn) to (endLine,...
Definition: Filter.cpp:286
int startColumn() const
Returns the column on startLine() where the hotspot area starts.
Definition: Filter.cpp:310
A filter processes blocks of text looking for certain patterns (such as URLs or keywords from a list)...
Definition: Filter.h:56
QList< HotSpot * > _hotspotList
Definition: Filter.h:186
virtual void process()=0
Causes the filter to process the block of text currently in its internal buffer.
const QString * _buffer
Definition: Filter.h:189
void setBuffer(const QString *buffer, const QList< int > *linePositions)
TODO: Document me.
Definition: Filter.cpp:197
virtual ~Filter()
Definition: Filter.cpp:183
QList< HotSpot * > hotSpotsAtLine(int line) const
Returns the list of hotspots identified by the filter which occur on a given line.
Definition: Filter.cpp:262
HotSpot * hotSpotAt(int line, int column) const
Adds a new line of text to the filter and increments the line count.
Definition: Filter.cpp:267
const QString * buffer()
Returns the internal buffer.
Definition: Filter.cpp:242
QMultiHash< int, HotSpot * > _hotspots
Definition: Filter.h:185
Type
Represents an area of text which matched the pattern a particular filter has been looking for.
Definition: Filter.h:75
@ Link
Definition: Filter.h:79
@ NotSpecified
Definition: Filter.h:77
@ Marker
Definition: Filter.h:81
@ ErrorLink
Definition: Filter.h:83
const QList< int > * _linePositions
Definition: Filter.h:188
QList< HotSpot * > hotSpots() const
Returns the list of hotspots identified by the filter.
Definition: Filter.cpp:258
Filter()
Constructs a new filter.
Definition: Filter.cpp:177
void reset()
Empties the filters internal buffer and resets the line count back to 0.
Definition: Filter.cpp:191
void getLineColumn(int position, int &startLine, int &startColumn)
Converts a character position within buffer() to a line and column.
Definition: Filter.cpp:203
void addHotSpot(HotSpot *)
Adds a new hotspot to the list.
Definition: Filter.cpp:249
Type of hotspot created by RegExpFilter.
Definition: Filter.h:210
QStringList _capturedTexts
Definition: Filter.h:221
virtual void activate(QObject *object=nullptr)
Causes the an action associated with a hotspot to be triggered.
Definition: Filter.cpp:339
HotSpot(int startLine, int startColumn, int endLine, int endColumn, Filter::Type)
Definition: Filter.cpp:332
QStringList capturedTexts() const
Returns the texts found by the filter when matching the filter's regular expression.
Definition: Filter.cpp:347
void setCapturedTexts(const QStringList &texts)
Sets the captured texts associated with this hotspot.
Definition: Filter.cpp:343
A filter which searches for sections of text matching a regular expression and creates a new RegExpFi...
Definition: Filter.h:200
virtual void process()
Reimplemented to search the filter's text buffer for text matching regExp()
Definition: Filter.cpp:364
void setRegExp(const QRegExp &text)
Sets the regular expression which the filter searches for in blocks of text.
Definition: Filter.cpp:352
void request_edit_mfile_signal(const QString &, int)
QRegExp regExp() const
Returns the regular expression which the filter searches for in blocks of text.
Definition: Filter.cpp:356
RegExpFilter(Type)
Constructs a new regular expression filter.
Definition: Filter.cpp:327
Type _type
Definition: Filter.h:257
QRegExp _searchText
Definition: Filter.h:259
void request_open_file_signal(const QString &, int)
virtual RegExpFilter::HotSpot * newHotSpot(int startLine, int startColumn, int endLine, int endColumn, Type)
Called when a match for the regular expression is encountered.
Definition: Filter.cpp:408
A filter chain which processes character images from terminal displays.
Definition: Filter.h:396
QList< int > * _linePositions
Definition: Filter.h:413
void setImage(const Character *const image, int lines, int columns, const QVector< LineProperty > &lineProperties)
Set the current terminal image to image.
Definition: Filter.cpp:125
virtual ~TerminalImageFilterChain()
Definition: Filter.cpp:119
Hotspot type created by UrlFilter instances.
Definition: Filter.h:276
FilterObject * get_urlObject()
Definition: Filter.h:291
FilterObject * _urlObject
Definition: Filter.h:304
UrlType urlType() const
Definition: Filter.cpp:492
virtual void activate(QObject *object=nullptr)
Open a web browser at the current URL.
Definition: Filter.cpp:508
virtual QString tooltip() const
Returns the text of a tooltip to be shown when the mouse moves over the hotspot, or an empty string i...
Definition: Filter.cpp:479
virtual ~HotSpot()
Definition: Filter.cpp:602
HotSpot(int startLine, int startColumn, int endLine, int endColumn, Type t)
Definition: Filter.cpp:472
virtual QList< QAction * > actions()
Returns a list of actions associated with the hotspot which can be used in a menu or toolbar.
Definition: Filter.cpp:610
A filter which matches URLs in blocks of text.
Definition: Filter.h:266
void request_open_file(const QString &, int)
Definition: Filter.cpp:677
static const QRegExp FullUrlRegExp
Definition: Filter.h:319
virtual void process()
Reimplemented to search the filter's text buffer for text matching regExp()
Definition: Filter.cpp:422
UrlFilter(Type t=Link)
Definition: Filter.cpp:594
static const QRegExp CompleteUrlRegExp
Definition: Filter.h:326
static const QRegExp ErrorLinkRegExp
Definition: Filter.h:321
static const QRegExp ParseErrorLinkRegExp
Definition: Filter.h:322
virtual HotSpot * newHotSpot(int, int, int, int, Type)
Called when a match for the regular expression is encountered.
Definition: Filter.cpp:415
static const QRegExp CompleteErrorLinkRegExp
Definition: Filter.h:323
static const QRegExp EmailAddressRegExp
Definition: Filter.h:320
MArray< T > filter(MArray< T > &b, MArray< T > &a, MArray< T > &x, MArray< T > &si, int dim=0)
Definition: filter.cc:46