File indexing completed on 2024-04-14 05:37:10

0001 /***************************************************************************
0002  *   Copyright (C) 2004-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef TEXTDOCUMENT_H
0012 #define TEXTDOCUMENT_H
0013 
0014 #include "config.h"
0015 #include "document.h"
0016 #include "gpsimprocessor.h"
0017 
0018 #include <QPointer>
0019 // #include <q3ptrlist.h>
0020 
0021 //#include <kate/document.h>
0022 #include <KTextEditor/Document>
0023 #include <KTextEditor/MarkInterface>
0024 
0025 class GpsimDebugger;
0026 class SourceLine;
0027 class TextView;
0028 
0029 namespace KTextEditor
0030 {
0031 class View;
0032 class Document;
0033 }
0034 
0035 typedef QList<int> IntList;
0036 
0037 /**
0038 @author David Saxton
0039 */
0040 class TextDocument : public Document
0041 {
0042     Q_OBJECT
0043 public:
0044     ~TextDocument() override;
0045 
0046     enum CodeType { ct_unknown, ct_asm, ct_c, ct_hex, ct_microbe };
0047 
0048     enum MarkType {
0049         Breakpoint = KTextEditor::MarkInterface::markType10,
0050     };
0051 
0052     View *createView(ViewContainer *viewContainer, uint viewAreaId) override;
0053 
0054     /**
0055      * Attempts to construct a new TextDocument object and returns a pointer to
0056      * it if successful, or 0 if it failed.
0057      * @returns pointer to constructed object, or 0 if there was a problem
0058      */
0059     static TextDocument *constructTextDocument(const QString &caption);
0060     /**
0061      * @returns the guessed code type that this file is
0062      */
0063     CodeType guessedCodeType() const
0064     {
0065         return m_guessedCodeType;
0066     }
0067     /**
0068      * Set the given lines as all bookmarks
0069      */
0070     void setBookmarks(const IntList &lines);
0071     /**
0072      * Set the given line to a bookmark (or not)
0073      */
0074     void setBookmark(uint line, bool isBookmark);
0075     /**
0076      * @return List of bookmarks
0077      */
0078     IntList bookmarkList() const;
0079 
0080     /**
0081      * Set the given lines as all breakpoints
0082      */
0083     void setBreakpoints(const IntList &lines);
0084     /**
0085      * Set the given line to a breakpoint (or not )
0086      */
0087     void setBreakpoint(uint line, bool isBreakpoint);
0088     /**
0089      * @return List of breakpoints
0090      */
0091     IntList breakpointList() const;
0092 
0093 #ifndef NO_GPSIM
0094     /**
0095      * Attach ourselves to the given debugger.
0096      * @param ownDebugger whether we have permission to delete it.
0097      */
0098     void setDebugger(GpsimDebugger *debugger, bool ownDebugger);
0099     GpsimDebugger *debugger() const
0100     {
0101         return m_pDebugger;
0102     }
0103     bool ownDebugger() const
0104     {
0105         return m_bOwnDebugger;
0106     }
0107     /**
0108      * Returns true if the debugger is running (this includes when we are in
0109      * stepping mode)
0110      */
0111     bool debuggerIsRunning() const;
0112     /**
0113      * Returns true if we are in stepping more
0114      */
0115     bool debuggerIsStepping() const;
0116     QString debugFile() const
0117     {
0118         return m_debugFile;
0119     }
0120     virtual void clearBreakpoints();
0121 #endif
0122 
0123     bool openURL(const QUrl &url) override;
0124 
0125     void fileSave(const QUrl &url);
0126     /**
0127      * Set the document to the given text, making the document unmodified, and
0128      * reseting the undo/redo history/
0129      * @param asInitial whether the next should be treated as if we had just
0130      * opened the file (no undo/redo history, and unmodified).
0131      */
0132     void setText(const QString &text, bool asInitial);
0133     /**
0134      * Attempts to guess the filetype from the file extension, and load an
0135      * appropriate highlighting/etc
0136      * @param allowDisable If false, will simply keep the old scheme if nothing
0137      *                     appropriate is found
0138      */
0139     void guessScheme(bool allowDisable = true);
0140 
0141     void fileSave() override
0142     {
0143         fileSave(url());
0144     }
0145     void fileSaveAs() override;
0146     void print() override;
0147     void setModified(bool modified) override;
0148 
0149     KTextEditor::View *createKateView(QWidget *parent);
0150 
0151     void undo() override;
0152     void redo() override;
0153     void cut() override;
0154     void copy() override;
0155     void paste() override;
0156 
0157     bool isModified() const override
0158     {
0159         return m_doc->isModified();
0160     }
0161     bool isUndoAvailable() const override; // { return (m_doc->undoCount() != 0); }
0162     bool isRedoAvailable() const override; // { return (m_doc->redoCount() != 0); }
0163 
0164     void clearBookmarks();
0165     bool fileClose() override;
0166 
0167     static const QPixmap *inactiveBreakpointPixmap();
0168     static const QPixmap *activeBreakpointPixmap();
0169     static const QPixmap *reachedBreakpointPixmap();
0170     static const QPixmap *disabledBreakpointPixmap();
0171     static const QPixmap *executionPointPixmap();
0172     /**
0173      * Returns a TextView pointer to the active TextView (if there is one)
0174      */
0175     TextView *textView() const;
0176 
0177     enum ConvertToTarget {
0178         MicrobeOutput, // (not used)
0179         AssemblyOutput,
0180         HexOutput,
0181         PICOutput
0182     };
0183 
0184     KTextEditor::Document *kateDocument() const
0185     {
0186         return m_doc;
0187     }
0188 
0189 public slots:
0190     /**
0191      * @param target as ConvertToTarget
0192      */
0193     void slotConvertTo(QAction *action);
0194     void convertToAssembly() override;
0195     void convertToHex() override;
0196     void convertToPIC() override;
0197     void formatAssembly();
0198     void debugRun() override;
0199     void debugInterrupt() override;
0200     void debugStep() override;
0201     void debugStepOver();
0202     void debugStepOut();
0203     void debugStop() override;
0204     void slotInitLanguage(CodeType type);
0205     /**
0206      * Called when change line / toggle marks
0207      */
0208     void slotUpdateMarksInfo();
0209 
0210     void slotDebugSetCurrentLine(const SourceLine &line);
0211     /**
0212      * Initialize the actions appropriate for when the debugger is running
0213      * or stepping
0214      */
0215     void slotInitDebugActions();
0216 
0217 protected:
0218     /**
0219      * Returns a filepath with the editor's contents in. If the url of this file
0220      * is non-empty (i.e. the user has already saved the file), then that url is
0221      * returned. Otherwise, a temporary file with the given extension (ext) is
0222      * created, and the location of this file is returned.
0223      */
0224     QString outputFilePath(const QString &ext);
0225     void saveDone();
0226 #ifndef NO_GPSIM
0227     /**
0228      * Looks at the list of marks returned by Kate, and syncs them with the
0229      * marks that we know about
0230      */
0231     void syncBreakpoints();
0232 
0233     int m_lastDebugLineAt; // Last line with a debug point reached mark
0234     bool m_bLoadDebuggerAsHLL;
0235 #endif
0236 
0237     KTextEditor::Document *m_doc;
0238     QPointer<TextDocument> m_pLastTextOutputTarget;
0239 
0240 private slots:
0241     void setLastTextOutputTarget(TextDocument *target);
0242     void slotSyncModifiedStates();
0243     void slotCODCreationSucceeded();
0244     void slotCODCreationFailed();
0245     void slotDebuggerDestroyed();
0246     void slotBookmarkRequested();
0247     //  void slotSelectionmChanged(); // 2016.09.08 - moved to TextView
0248 
0249 private:
0250     TextDocument(const QString &caption);
0251     bool m_constructorSuccessful;
0252     CodeType m_guessedCodeType;
0253     QList<QAction *> m_bookmarkActions;
0254 
0255 #ifndef NO_GPSIM
0256     bool b_lockSyncBreakpoints; // Used to avoid calling syncMarks() when we are currently doing so
0257     bool m_bOwnDebugger;
0258     QPointer<GpsimDebugger> m_pDebugger;
0259     QString m_symbolFile;
0260     QString m_debugFile;
0261 #endif
0262 };
0263 
0264 #endif