1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2015, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 #ifndef _LUMINA_PLAIN_TEXT_EDITOR_WIDGET_H
8 #define _LUMINA_PLAIN_TEXT_EDITOR_WIDGET_H
9 
10 #include <QPlainTextEdit>
11 #include <QWidget>
12 #include <QResizeEvent>
13 #include <QPaintEvent>
14 #include <QFileSystemWatcher>
15 
16 //#include <hunspell/hunspell.hxx>
17 
18 #include "syntaxSupport.h"
19 #include "Word.h"
20 
21 //QPlainTextEdit subclass for providing the actual text editor functionality
22 class PlainTextEditor : public QPlainTextEdit{
23 	Q_OBJECT
24 public:
25 	PlainTextEditor(QSettings *set, QWidget *parent = 0);
26 	~PlainTextEditor();
27 
28 	//Functions for setting up the editor
29 	void showLineNumbers(bool show = true);
30 	void LoadSyntaxRule(QString type);
31 	void updateSyntaxColors();
32 
33 	//File loading/setting options
34 	void LoadFile(QString filepath);
35 	bool SaveFile(bool newname = false);
36 	QString currentFile();
37 	Word *wordAtPosition(int, int);
38 
39 	bool hasChange();
40 	bool readOnlyFile();
41 
42 	//Functions for managing the line number widget (internal - do not need to run directly)
43 	int LNWWidth(); //replacing the LNW size hint detection
44 	void paintLNW(QPaintEvent *ev); //forwarded from the LNW paint event
45 	void updateLNW();
setWordList(QList<Word * > _wordList)46 	void setWordList(QList<Word*> _wordList) { wordList = _wordList; }
47 	//void setDictionary(Hunspell *_hunspell) { hunspell = _hunspell; }
48 
49 	QFontMetrics *metrics;
50 
51 private:
52 	QWidget *LNW; //Line Number Widget
53 	bool showLNW;
54 	QSettings *settings;
55 	QString lastSaveContents;
56 	QFileSystemWatcher *watcher;
57 	QList<Word*> wordList;
58 	//Syntax Highlighting class
59 	Custom_Syntax *SYNTAX;
60 	//Hunspell *hunspell;
61 
62 	//Bracket/Perentheses matching functions
63 	int matchleft, matchright; //positions within the document
64 	void clearMatchData();
65 	void highlightMatch(QChar ch, bool forward, int fromPos, QChar startch);
66 
67 	//Flags to keep track of changes/status
68 	bool hasChanges, readonly;
69 
70 private slots:
71 	//Functions for managing the line number widget
72 	void LNW_updateWidth();  	// Tied to the QPlainTextEdit::blockCountChanged() signal
73 	void LNW_highlightLine();  		// Tied to the QPlainTextEdit::cursorPositionChanged() signal
74 	void LNW_update(const QRect&, int); 	// Tied to the QPlainTextEdit::updateRequest() signal
75 	//Function for running the matching routine
76 	void checkMatchChar();
77 	//Functions for notifying the parent widget of changes
78 	void textChanged();
79 	void cursorMoved();
80 	//Function for prompting the user if the file changed externally
81 	void fileChanged();
82 
83 protected:
84 	void resizeEvent(QResizeEvent *ev);
85   void contextMenuEvent(QContextMenuEvent *ev);
86   void keyPressEvent(QKeyEvent *ev);
87 
88 signals:
89 	void UnsavedChanges(QString); //filename
90 	void FileLoaded(QString);
91 	void CheckSpelling(int, int);
92 	void statusTipChanged();
93 };
94 
95 //===========================================================
96 // Small Widget for painting the line numbers in the PlainTextEditor
97 //===========================================================
98 class LNWidget : public QWidget{
99 	Q_OBJECT
100 private:
101 	PlainTextEditor *TE;
102 public:
LNWidget(PlainTextEditor * edit)103 	LNWidget( PlainTextEditor *edit) : QWidget(edit){
104 	  TE = edit;
105 	}
~LNWidget()106 	~LNWidget(){}
107 	//Replace the virtual QWidget size hint function
108 	//  since the main text editor controls the size/location of this widget
sizeHint()109 	QSize sizeHint() const{
110 	  return QSize(TE->LNWWidth(),0);
111 	}
112 protected:
113 	//Replace the virtual QWidget paint event function
114 	// since the main text editor control the size/location of this widget
paintEvent(QPaintEvent * ev)115 	void paintEvent(QPaintEvent *ev){
116 	  TE->paintLNW(ev);
117 	}
118 };
119 #endif
120