aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.h
blob: 8f2d7e987c46de12bb0a4c0d8e0e2479184b4620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#ifndef _LUMINA_PLAIN_TEXT_EDITOR_WIDGET_H
#define _LUMINA_PLAIN_TEXT_EDITOR_WIDGET_H

#include <QPlainTextEdit>
#include <QWidget>
#include <QResizeEvent>
#include <QPaintEvent>
#include <QFileSystemWatcher>

//#include <hunspell/hunspell.hxx>

#include "syntaxSupport.h"
#include "Word.h"

//QPlainTextEdit subclass for providing the actual text editor functionality
class PlainTextEditor : public QPlainTextEdit{
	Q_OBJECT
public:
	PlainTextEditor(QSettings *set, QWidget *parent = 0);
	~PlainTextEditor();

	//Functions for setting up the editor
	void showLineNumbers(bool show = true);
	void LoadSyntaxRule(QString type);
	void updateSyntaxColors();

	//File loading/setting options
	void LoadFile(QString filepath);
	bool SaveFile(bool newname = false);
	QString currentFile();
	Word *wordAtPosition(int, int);

	bool hasChange();
	bool readOnlyFile();

	//Functions for managing the line number widget (internal - do not need to run directly)
	int LNWWidth(); //replacing the LNW size hint detection
	void paintLNW(QPaintEvent *ev); //forwarded from the LNW paint event
	void updateLNW();
	void setWordList(QList<Word*> _wordList) { wordList = _wordList; }
	//void setDictionary(Hunspell *_hunspell) { hunspell = _hunspell; }

	QFontMetrics *metrics;

private:
	QWidget *LNW; //Line Number Widget
	bool showLNW;
	QSettings *settings;
	QString lastSaveContents;
	QFileSystemWatcher *watcher;
	QList<Word*> wordList;
	QTimer *FTIMER;
	//Syntax Highlighting class
	Custom_Syntax *SYNTAX;
	//Hunspell *hunspell;

	//Bracket/Perentheses matching functions
	int matchleft, matchright; //positions within the document
	void clearMatchData();
	void highlightMatch(QChar ch, bool forward, int fromPos, QChar startch);

	//Flags to keep track of changes/status
	bool hasChanges, readonly;

private slots:
	//Functions for managing the line number widget
	void LNW_updateWidth();  	// Tied to the QPlainTextEdit::blockCountChanged() signal
	void LNW_highlightLine();  		// Tied to the QPlainTextEdit::cursorPositionChanged() signal
	void LNW_update(const QRect&, int); 	// Tied to the QPlainTextEdit::updateRequest() signal
	//Function for running the matching routine
	void checkMatchChar();
	//Functions for notifying the parent widget of changes
	void textChanged();
	void cursorMoved();
	//Function for prompting the user if the file changed externally
	void fileChanged();

protected:
	void resizeEvent(QResizeEvent *ev);
  void contextMenuEvent(QContextMenuEvent *ev);
  void keyPressEvent(QKeyEvent *ev);

signals:
	void UnsavedChanges(QString); //filename
	void FileLoaded(QString);
	void CheckSpelling(int, int);
	void statusTipChanged();
};

//===========================================================
// Small Widget for painting the line numbers in the PlainTextEditor
//===========================================================
class LNWidget : public QWidget{
	Q_OBJECT
private:
	PlainTextEditor *TE;
public:
	LNWidget( PlainTextEditor *edit) : QWidget(edit){
	  TE = edit;
	}
	~LNWidget(){}
	//Replace the virtual QWidget size hint function
	//  since the main text editor controls the size/location of this widget
	QSize sizeHint() const{
	  return QSize(TE->LNWWidth(),0);
	}
protected:
	//Replace the virtual QWidget paint event function
	// since the main text editor control the size/location of this widget
	void paintEvent(QPaintEvent *ev){
	  TE->paintLNW(ev);
	}
};
#endif
bgstack15