itom  4.1.0
foldDetector.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 
22  Further hints:
23  ------------------------
24 
25  This file belongs to the code editor of itom. The code editor is
26  in major parts a fork / rewritten version of the python-based source
27  code editor PyQode from Colin Duquesnoy and others
28  (see https://github.com/pyQode). PyQode itself is licensed under
29  the MIT License (MIT).
30 
31  Some parts of the code editor of itom are also inspired by the
32  source code editor of the Spyder IDE (https://github.com/spyder-ide),
33  also licensed under the MIT License and developed by the Spyder Project
34  Contributors.
35 
36 *********************************************************************** */
37 
38 #ifndef FOLDDETECTOR_H
39 #define FOLDDETECTOR_H
40 
41 #include <qtextedit.h>
42 #include <qstring.h>
43 #include <qsharedpointer.h>
44 #include <QTextBlock>
45 
46 namespace ito {
47 
48 /*
49 This module contains the code folding API.
50 */
51 
52 class CodeEditor;
53 class FoldDetectorPrivate;
54 
55 
56 /*
57 Base class for fold detectors.
58 
59 A fold detector takes care of detecting the text blocks fold levels that
60 are used by the FoldingPanel to render the document outline.
61 
62 To use a FoldDetector, simply set it on a syntax_highlighter::
63 
64  editor.syntax_highlighter.fold_detector = my_fold_detector
65 */
66 class FoldDetector : public QObject
67 {
68  Q_OBJECT
69 public:
70  FoldDetector(QObject *parent = NULL);
71 
72  virtual ~FoldDetector();
73 
74  void processBlock(QTextBlock &currentBlock, QTextBlock &previousBlock, const QString &text);
75 
76  CodeEditor* editor() const;
77  void setEditor(CodeEditor *editor);
78 
79  /*
80  Detects the block fold level.
81 
82  The default implementation is based on the block **indentation**.
83 
84  .. note:: Blocks fold level must be contiguous, there cannot be
85  a difference greater than 1 between two successive block fold
86  levels.
87 
88  :param prev_block: first previous **non-blank** block or None if this
89  is the first line of the document
90  :param block: The block to process.
91  :return: Fold level
92  */
93  virtual int detectFoldLevel(const QTextBlock &previousBlock, const QTextBlock &block) = 0;
94 private:
95  FoldDetectorPrivate *d_ptr;
96  Q_DECLARE_PRIVATE(FoldDetector);
97 };
98 
99 
100 /*
101 Utility class for manipulating fold-able code scope (fold/unfold,
102 get range, child and parent scopes and so on).
103 
104 A scope is built from a fold trigger (QTextBlock).
105 */
107 {
108 public:
109  FoldScope();
110  FoldScope(const QTextBlock &block, bool &valid);
111  virtual ~FoldScope();
112 
113  int triggerLevel() const;
114  int scopeLevel() const;
115  bool collapsed() const;
116  bool isValid() const;
117 
118  //returns index of first and last line of entire fold range
119  QPair<int, int> getRange(bool ignoreBlankLines = true) const;
120  void fold();
121  void unfold(bool unfoldChildBlocks = true);
122  QString text(int maxLines) const;
123  QSharedPointer<FoldScope> parent() const;
124  QTextBlock trigger() const;
125  QList<FoldScope> childRegions() const;
126  QList<QTextBlock> blocks(bool ignoreBlankLines = true) const;
127 
128  static QTextBlock findParentScope(QTextBlock block);
129 
130 private:
131  QTextBlock m_trigger;
132 };
133 
134 } //end namespace ito
135 
136 #endif
Definition: codeEditor.h:110
Definition: foldDetector.h:106
Definition: apiFunctionsGraph.cpp:39
Definition: foldDetector.h:66
Definition: foldDetector.cpp:49