1 /*
2   This file is part of the Ofi Labs X2 project.
3 
4   Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
5   Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
6 
7   Redistribution and use in source and binary forms, with or without
8   modification, are permitted provided that the following conditions are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15     * Neither the name of the <organization> nor the
16       names of its contributors may be used to endorse or promote products
17       derived from this software without specific prior written permission.
18 
19   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #ifndef OFILABS_JSEDIT
32 #define OFILABS_JSEDIT
33 
34 #include <QColor>
35 #include <QPlainTextEdit>
36 #include <QScopedPointer>
37 
38 class JSEditPrivate;
39 
40 class JSEdit: public QPlainTextEdit
41 {
42     Q_OBJECT
43     Q_PROPERTY(bool bracketsMatchingEnabled READ isBracketsMatchingEnabled WRITE setBracketsMatchingEnabled)
44     Q_PROPERTY(bool codeFoldingEnabled READ isCodeFoldingEnabled WRITE setCodeFoldingEnabled)
45     Q_PROPERTY(bool lineNumbersVisible READ isLineNumbersVisible WRITE setLineNumbersVisible)
46     Q_PROPERTY(bool textWrapEnabled READ isTextWrapEnabled WRITE setTextWrapEnabled)
47 
48 public:
49 
50     typedef enum {
51         Background,
52         Normal,
53         Comment,
54         Number,
55         String,
56         Operator,
57         Identifier,
58         Keyword,
59         BuiltIn,
60         Sidebar,
61         LineNumber,
62         Cursor,
63         Marker,
64         BracketMatch,
65         BracketError,
66         FoldIndicator
67     } ColorComponent;
68 
69     JSEdit(QWidget *parent = 0);
70     ~JSEdit();
71 
72     void setColor(ColorComponent component, const QColor &color);
73 
74     QStringList keywords() const;
75     void setKeywords(const QStringList &keywords);
76 
77     bool isBracketsMatchingEnabled() const;
78     bool isCodeFoldingEnabled() const;
79     bool isLineNumbersVisible() const;
80     bool isTextWrapEnabled() const;
81 
82     bool isFoldable(int line) const;
83     bool isFolded(int line) const;
84 
85 public slots:
86     void updateSidebar();
87     void mark(const QString &str, Qt::CaseSensitivity sens = Qt::CaseInsensitive);
88     void setBracketsMatchingEnabled(bool enable);
89     void setCodeFoldingEnabled(bool enable);
90     void setLineNumbersVisible(bool visible);
91     void setTextWrapEnabled(bool enable);
92 
93     void fold(int line);
94     void unfold(int line);
95     void toggleFold(int line);
96 
97 protected:
98     void resizeEvent(QResizeEvent *e);
99     void wheelEvent(QWheelEvent *e);
100 
101 private slots:
102     void updateCursor();
103     void updateSidebar(const QRect &rect, int d);
104 
105 private:
106     QScopedPointer<JSEditPrivate> d_ptr;
107     Q_DECLARE_PRIVATE(JSEdit);
108     Q_DISABLE_COPY(JSEdit);
109 };
110 
111 #endif // OFILABS_JSEDIT
112