1 /************************************************************************
2 **
3 **  Copyright (C) 2020 Kevin B. Hendricks Stratford, ON Canada
4 **    Based on CodeViewEditor portions of which are Copyright
5 **    (C) 2009-2011  Strahinja Markovic  <strahinja.markovic@gmail.com>
6 **
7 **  This file is part of Sigil.
8 **
9 **  Sigil is free software: you can redistribute it and/or modify
10 **  it under the terms of the GNU General Public License as published by
11 **  the Free Software Foundation, either version 3 of the License, or
12 **  (at your option) any later version.
13 **
14 **  Sigil is distributed in the hope that it will be useful,
15 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 **  GNU General Public License for more details.
18 **
19 **  You should have received a copy of the GNU General Public License
20 **  along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
21 **
22 *************************************************************************/
23 
24 #pragma once
25 #ifndef TEXTVIEW_H
26 #define TEXTVIEW_H
27 
28 #include <QList>
29 #include <QPlainTextEdit>
30 #include <QUrl>
31 
32 #include "Misc/SettingsStore.h"
33 #include "Misc/Utility.h"
34 
35 class QResizeEvent;
36 class QSize;
37 class QWidget;
38 class QShortcut;
39 class TVLineNumberArea;
40 class QContextMenuEvent;
41 class QSyntaxHighlighter;
42 
43 /**
44  * A text viewer for source code and general text.
45  */
46 class TextView : public QPlainTextEdit
47 {
48     Q_OBJECT
49 
50 public:
51 
52     enum HighlighterType {
53         Highlight_NONE,  /**< No source code highlighting */
54         Highlight_XHTML, /**< XHTML source code highlighting */
55         Highlight_CSS    /**< CSS source code highlighting */
56     };
57 
58     TextView(QWidget *parent = 0);
59     ~TextView();
60 
61     void SetAppearance();
62     void SetAppearanceColors();
63 
64     QSize sizeHint() const;
65 
66     void LineNumberAreaPaintEvent(QPaintEvent *event);
67     void LineNumberAreaMouseEvent(QMouseEvent *event);
68     int CalculateLineNumberAreaWidth();
69 
70     void ScrollToTop();
71     void ScrollToPosition(int cursor_position, bool center_screen = true);
72     void ScrollToBlock(int bnum);
73 
74     void DoHighlightDocument(HighlighterType highlighter_type);
75     void RehighlightDocument();
76 
77     static QString rstrip_pad(const QString& str);
78     QString selected_text_from_cursor(const QTextCursor& cursor) const;
79 
80     // override and hide the toPlainText() call to prevent
81     // issues with lost non-breaking spaces (improperly
82     // converted to normal spaces)
83     QString toPlainText() const;
84 
85     // override the createMimeDataFromSelection() to
86     // prevent copy and cut from losing nbsp
87     // ala Kovid's solution in calibre PlainTextEdit
88     virtual QMimeData *createMimeDataFromSelection() const;
89 
90     int GetCursorPosition() const;
91     int GetCursorBlockNumber() const;
92     int GetCursorColumn() const;
93 
94     QScrollBar* GetVerticalScrollBar();
95 
96     QString GetSelectedText();
97 
98     void setBlockMap(const QStringList& blockmap);
99 
100 public slots:
101     void Refresh(HighlighterType hightype);
102 
103 signals:
104 
105     void FocusLost(QWidget *editor);
106     void FocusGained(QWidget *editor);
107 
108     void PageClicked();
109     void PageUpdated();
110 
111     void NextChange(int);
112 
113     void LinkClicked(const QUrl &url);
114 
115 protected:
116 
117     void keyPressEvent(QKeyEvent* ev);
118     void mousePressEvent(QMouseEvent *event);
119     void mouseReleaseEvent(QMouseEvent *event);
120 
121     void resizeEvent(QResizeEvent *event);
122     void contextMenuEvent(QContextMenuEvent *event);
123 
124     void focusInEvent(QFocusEvent *event);
125     void focusOutEvent(QFocusEvent *event);
126 
127 private slots:
128 
129     void UpdateLineNumberAreaMargin();
130     void UpdateLineNumberArea(const QRect &rectangle, int vertical_delta);
131 
132 private:
133 
134     void UpdateLineNumberAreaFont(const QFont &font);
135 
136     // void SetAppearanceColors();
137 
138     void ScrollByLine(bool down);
139 
140     void ConnectSignalsToSlots();
141 
142     SettingsStore::CodeViewAppearance m_codeViewAppearance;
143     TVLineNumberArea *m_LineNumberArea;
144     QStringList  m_blockmap;
145     QScrollBar* m_verticalScrollBar;
146     QSyntaxHighlighter * m_Highlighter;
147 };
148 
149 #endif // TEXTVIEW_H
150