File indexing completed on 2024-12-08 08:10:47

0001 /***************************************************************************
0002  *   Copyright (C) 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 TEXTVIEW_H
0012 #define TEXTVIEW_H
0013 
0014 #include "config.h"
0015 #include "view.h"
0016 
0017 // #include <kate/view.h>
0018 //#include <ktexteditor/view.h>
0019 #include <KTextEditor/TextHintInterface>
0020 #include <KTextEditor/View>
0021 
0022 #include <QLabel>
0023 #include <QPointer>
0024 
0025 class QMouseEvent;
0026 class RegisterInfo;
0027 class TextDocument;
0028 class TextView;
0029 class VariableLabel;
0030 
0031 /**
0032 @author David Saxton
0033 */
0034 class TextView : public View
0035 {
0036     Q_OBJECT
0037 public:
0038     TextView(TextDocument *textDocument, ViewContainer *viewContainer, uint viewAreaId);
0039     ~TextView() override;
0040 
0041     bool closeView() override;
0042 
0043     /**
0044      * Brings up the goto line dialog.
0045      */
0046     bool gotoLine(const int line); // { return m_view->setCursorPosition( line, 0/*m_view->cursorColumn()*/ ); }
0047     /**
0048      * Returns a pointer to the document as a text document
0049      */
0050     TextDocument *textDocument() const;
0051     void undo();
0052     void redo();
0053     void cut();   // { m_view->cut(); }
0054     void copy();  // { m_view->copy(); }
0055     void paste(); // { m_view->paste(); }
0056     void saveCursorPosition();
0057     void restoreCursorPosition();
0058     /**
0059      * Enable code actions depending on the type of code being edited
0060      */
0061     void initCodeActions();
0062     void setCursorPosition(uint line, uint col); // { m_view->setCursorPosition( line, col ); }
0063     unsigned currentLine();
0064     unsigned currentColumn();
0065     void disableActions();
0066 
0067     KTextEditor::View *kateView() const
0068     {
0069         return m_view;
0070     }
0071 
0072     // KTextEditor::View::saveResult save(); // { return m_view->save(); }
0073     bool save();
0074     // KTextEditor::View::saveResult saveAs(); // { return m_view->saveAs(); }
0075     bool saveAs();
0076     void print();
0077 
0078 public slots:
0079     /**
0080      * Called when change line / toggle marks
0081      */
0082     void slotUpdateMarksInfo();
0083     void slotCursorPositionChanged();
0084     void toggleBreakpoint() override;
0085     /**
0086      * Initialize the actions appropriate for when the debugger is running
0087      * or stepping
0088      */
0089     void slotInitDebugActions();
0090 
0091 protected slots:
0092     void slotWordHoveredOver(const QString &word, int line, int col);
0093     void slotWordUnhovered();
0094     void gotFocus();
0095     void slotSelectionmChanged();
0096 
0097 protected:
0098     KTextEditor::View *m_view;
0099 #ifndef NO_GPSIM
0100     VariableLabel *m_pTextViewLabel; ///< Pops up when the user hovers his mouse over a word
0101 #endif
0102 
0103 private:
0104     uint m_savedCursorLine;
0105     uint m_savedCursorColumn;
0106 };
0107 
0108 /**
0109 This class is an event filter to be installed in the kate view, and is used to
0110 do stuff like poping up menus and telling TextView that a word is being hovered
0111 over (used in the debugger).
0112 
0113 @author David Saxton
0114 */
0115 class TextViewEventFilter : public QObject, public KTextEditor::TextHintProvider
0116 {
0117     Q_OBJECT
0118 public:
0119     TextViewEventFilter(TextView *textView);
0120     virtual ~TextViewEventFilter();
0121 
0122     bool eventFilter(QObject *watched, QEvent *e) override;
0123 
0124     // from TextHintInterface
0125     QString textHint(KTextEditor::View *view, const KTextEditor::Cursor &position) override;
0126 signals:
0127     /**
0128      * When the user hovers the mouse for more than 700 milliseconds over a
0129      * word, "hover mode" is entered. When the user presses a key, clicks
0130      * mouse, etc, this mode is left. During the mode, any word that is
0131      * under the mouse cursor will be emitted as hoveredOver( word ).
0132      */
0133     void wordHoveredOver(const QString &word, int line, int col);
0134     /**
0135      * Emitted when focus is lost, the mouse moves to a different word, etc.
0136      */
0137     void wordUnhovered();
0138 
0139 protected slots:
0140     void slotNeedTextHint(const KTextEditor::Cursor &position, QString &text);
0141     /**
0142      * Called when we are not in hover mode, but the user has had his mouse
0143      * in the same position for some time.
0144      */
0145     void hoverTimeout();
0146     /**
0147      * Called (from m_pSleepTimer) when we are in hover mode, but no word
0148      * has been hovered over for some time.
0149      */
0150     void gotoSleep();
0151     /**
0152      * @see m_pNoWordTimer
0153      */
0154     void slotNoWordTimeout();
0155 
0156 protected:
0157     enum HoverStatus {
0158         /**
0159          * We are currently hovering - wordHoveredOver was emitted, and
0160          * wordUnhovered hasn't been emitted yet.
0161          */
0162         Active,
0163         /**
0164          * A word was unhovered recently. We will go straight to PoppedUp
0165          * mode if a word is hovered over again.
0166          */
0167         Hidden,
0168         /**
0169          * A word was not unhovered recentely. There will be a short display
0170          * before going to PoppedUp mode if a word is hovered over.
0171          */
0172         Sleeping
0173     };
0174 
0175     /**
0176      * Starts / stops timers, emits signals, etc. See other functions for an
0177      * idea of what this does.
0178      */
0179     void updateHovering(const QString &currentWord, int line, int col);
0180     /**
0181      * Started when the user moves his mouse over a word, and we are in
0182      * Sleeping mode. Reset when the user moves his mouse, etc.
0183      */
0184     QTimer *m_pHoverTimer;
0185     /**
0186      * Started when a word is unhovered. When this timeouts, we will go to
0187      * Sleeping mode.
0188      */
0189     QTimer *m_pSleepTimer;
0190     /**
0191      * Activated by the user moving the mouse. Reset by a call to
0192      * slotNeedTextHint. This timer is needed as KateViewInternal doesn't
0193      * bother updating us if the mouse cursor isn't over text.
0194      */
0195     QTimer *m_pNoWordTimer;
0196 
0197     TextView *m_pTextView;
0198     QString m_lastWord;
0199     int m_lastLine;
0200     int m_lastCol;
0201     HoverStatus m_hoverStatus;
0202 };
0203 
0204 #endif