1 /***************************************************************************
2  *   Copyright (C) 2004-2005 by David Saxton                               *
3  *   david@bluehaze.org                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #ifndef TEXTDOCUMENT_H
12 #define TEXTDOCUMENT_H
13 
14 #include "config.h"
15 #include "document.h"
16 #include "gpsimprocessor.h"
17 
18 #include <QPointer>
19 // #include <q3ptrlist.h>
20 
21 //#include <kate/document.h>
22 #include <KTextEditor/Document>
23 #include <KTextEditor/MarkInterface>
24 
25 class GpsimDebugger;
26 class SourceLine;
27 class TextView;
28 
29 namespace KTextEditor {
30     class View;
31     class Document;
32 }
33 
34 typedef QList<int> IntList;
35 
36 /**
37 @author David Saxton
38 */
39 class TextDocument : public Document
40 {
41 Q_OBJECT
42 public:
43 	~TextDocument() override;
44 
45 	enum CodeType
46 	{
47 		ct_unknown,
48 		ct_asm,
49 		ct_c,
50 		ct_hex,
51 		ct_microbe
52 	};
53 
54 	enum MarkType {
55 		Bookmark           = KTextEditor::MarkInterface::markType01,
56 		Breakpoint         = KTextEditor::MarkInterface::markType02,
57 		ActiveBreakpoint   = KTextEditor::MarkInterface::markType03,
58 		ReachedBreakpoint  = KTextEditor::MarkInterface::markType04,
59 		DisabledBreakpoint = KTextEditor::MarkInterface::markType05,
60 		ExecutionPoint     = KTextEditor::MarkInterface::markType06
61 	};
62 
63 	View *createView( ViewContainer *viewContainer, uint viewAreaId, const char *name = nullptr ) override;
64 
65 
66 	/**
67 	 * Attempts to construct a new TextDocument object and returns a pointer to
68 	 * it if successful, or 0 if it failed.
69 	 * @returns pointer to constructed object, or 0 if there was a problem
70 	 */
71 	static TextDocument *constructTextDocument( const QString& caption, const char *name = nullptr );
72 	/**
73 	 * @returns the guessed code type that this file is
74 	 */
guessedCodeType()75 	CodeType guessedCodeType() const { return m_guessedCodeType; }
76 	/**
77 	 * Set the given lines as all bookmarks
78 	 */
79 	void setBookmarks( const IntList &lines );
80 	/**
81 	 * Set the given line to a bookmark (or not)
82 	 */
83 	void setBookmark( uint line, bool isBookmark );
84 	/**
85 	 * @return List of bookmarks
86 	 */
87 	IntList bookmarkList() const;
88 
89 	/**
90 	 * Set the given lines as all breakpoints
91 	 */
92 	void setBreakpoints( const IntList &lines );
93 	/**
94 	 * Set the given line to a breakpoint (or not )
95 	 */
96 	void setBreakpoint( uint line, bool isBreakpoint );
97 	/**
98 	 * @return List of breakpoints
99 	 */
100 	IntList breakpointList() const;
101 
102 #ifndef NO_GPSIM
103 	/**
104 	 * Attach ourselves to the given debugger.
105 	 * @param ownDebugger whether we have permission to delete it.
106 	 */
107 	void setDebugger( GpsimDebugger * debugger, bool ownDebugger );
debugger()108 	GpsimDebugger * debugger() const { return m_pDebugger; }
ownDebugger()109 	bool ownDebugger() const { return m_bOwnDebugger; }
110 	/**
111 	 * Returns true if the debugger is running (this includes when we are in
112 	 * stepping mode)
113 	 */
114 	bool debuggerIsRunning() const;
115 	/**
116 	 * Returns true if we are in stepping more
117 	 */
118 	bool debuggerIsStepping() const;
debugFile()119 	QString debugFile() const { return m_debugFile; }
120 	virtual void clearBreakpoints();
121 #endif
122 
123 	bool openURL(const QUrl& url) override;
124 
125 	void fileSave(const QUrl& url);
126 	/**
127 	 * Set the document to the given text, making the document unmodified, and
128 	 * reseting the undo/redo history/
129 	 * @param asInitial whether the next should be treated as if we had just
130 	 * opened the file (no undo/redo history, and unmodified).
131 	 */
132 	void setText( const QString & text, bool asInitial );
133 	/**
134 	 * Attempts to guess the filetype from the file extension, and load an
135 	 * appropriate highlighting/etc
136 	 * @param allowDisable If false, will simply keep the old scheme if nothing
137 	 *					   appropriate is found
138 	 */
139 	void guessScheme( bool allowDisable = true );
140 
fileSave()141 	void fileSave() override { fileSave(url()); }
142 	void fileSaveAs() override;
143 	void print() override;
144 	void setModified( bool modified ) override;
145 
146 	KTextEditor::View* createKateView( QWidget *parent, const char *name = nullptr );
147 
148 	void undo() override;
149 	void redo() override;
150 	void cut() override;
151 	void copy() override;
152 	void paste() override;
153 
isModified()154 	bool isModified() const override { return m_doc->isModified(); }
155 	bool isUndoAvailable() const override ; // { return (m_doc->undoCount() != 0); }
156 	bool isRedoAvailable() const override ; // { return (m_doc->redoCount() != 0); }
157 
158 	void clearBookmarks();
159 	bool fileClose() override;
160 
161 
162 	static const QPixmap* inactiveBreakpointPixmap();
163 	static const QPixmap* activeBreakpointPixmap();
164 	static const QPixmap* reachedBreakpointPixmap();
165 	static const QPixmap* disabledBreakpointPixmap();
166 	static const QPixmap* executionPointPixmap();
167 	/**
168 	 * Returns a TextView pointer to the active TextView (if there is one)
169 	 */
170 	TextView *textView() const;
171 
172 	enum ConvertToTarget
173 	{
174 		MicrobeOutput, // (not used)
175 		AssemblyOutput,
176 		HexOutput,
177 		PICOutput
178 	};
179 
kateDocument()180 	KTextEditor::Document * kateDocument() const { return m_doc; }
181 
182 public slots:
183 	/**
184 	 * @param target as ConvertToTarget
185 	 */
186 	void slotConvertTo( QAction *action );
187 	void convertToAssembly() override;
188 	void convertToHex() override;
189 	void convertToPIC() override;
190 	void formatAssembly();
191 	void debugRun() override;
192 	void debugInterrupt() override;
193 	void debugStep() override;
194 	void debugStepOver();
195 	void debugStepOut();
196 	void debugStop() override;
197 	void slotInitLanguage( CodeType type );
198 	/**
199 	 * Called when change line / toggle marks
200 	 */
201 	void slotUpdateMarksInfo();
202 
203 	void slotDebugSetCurrentLine( const SourceLine & line );
204 	/**
205 	 * Initialize the actions appropriate for when the debugger is running
206 	 * or stepping
207 	 */
208 	void slotInitDebugActions();
209 
210 protected:
211 	/**
212 	 * Returns a filepath with the editor's contents in. If the url of this file
213 	 * is non-empty (i.e. the user has already saved the file), then that url is
214 	 * returned. Otherwise, a temporary file with the given extension (ext) is
215 	 * created, and the location of this file is returned.
216 	 */
217 	QString outputFilePath( const QString &ext );
218 	void saveDone();
219 #ifndef NO_GPSIM
220 	/**
221 	 * Looks at the list of marks returned by Kate, and syncs them with the
222 	 * marks that we know about
223 	 */
224 	void syncBreakpoints();
225 
226 	int m_lastDebugLineAt; // Last line with a debug point reached mark
227 	bool m_bLoadDebuggerAsHLL;
228 #endif
229 
230 	KTextEditor::Document *m_doc;
231 	QPointer<TextDocument> m_pLastTextOutputTarget;
232 
233 private slots:
234 	void setLastTextOutputTarget( TextDocument * target );
235 	void slotSyncModifiedStates();
236 	void slotCODCreationSucceeded();
237 	void slotCODCreationFailed();
238 	void slotDebuggerDestroyed();
239 	void slotBookmarkRequested();
240 // 	void slotSelectionmChanged(); // 2016.09.08 - moved to TextView
241 
242 private:
243 	TextDocument( const QString& caption, const char *name = nullptr );
244 	bool m_constructorSuccessful;
245 	CodeType m_guessedCodeType;
246     QList<QAction*> m_bookmarkActions;
247 
248 #ifndef NO_GPSIM
249 	bool b_lockSyncBreakpoints; // Used to avoid calling syncMarks() when we are currently doing so
250 	bool m_bOwnDebugger;
251 	QPointer<GpsimDebugger> m_pDebugger;
252 	QString m_symbolFile;
253 	QString m_debugFile;
254 #endif
255 };
256 
257 #endif
258