File indexing completed on 2024-04-28 15:28:40

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef NUMBERED_TEXT_VIEW_H
0024 #define NUMBERED_TEXT_VIEW_H
0025 
0026 #include <QFrame>
0027 #include <QPixmap>
0028 #include <QTextCursor>
0029 
0030 class QTextEdit;
0031 class QHBoxLayout;
0032 
0033 /**
0034  * @internal Used to display the numbers.
0035  */
0036 class NumberBar : public QWidget
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041     NumberBar(QWidget *parent);
0042     ~NumberBar() override;
0043 
0044     void setCurrentLine(int lineno);
0045     void setStopLine(int lineno);
0046     void setBugLine(int lineno);
0047 
0048     int currentLine() const;
0049     int stopLine() const;
0050     int bugLine() const;
0051 
0052     void setTextEdit(QTextEdit *edit);
0053     void paintEvent(QPaintEvent *ev) override;
0054 
0055 protected:
0056     bool event(QEvent *ev) override;
0057 
0058 private:
0059     QTextEdit *edit;
0060     QPixmap stopMarker;
0061     QPixmap currentMarker;
0062     QPixmap bugMarker;
0063     int m_stopLine;
0064     int m_currentLine;
0065     int m_bugLine;
0066     QRect stopRect;
0067     QRect currentRect;
0068     QRect bugRect;
0069 };
0070 
0071 /**
0072  * Displays a QTextEdit with line numbers.
0073  */
0074 class NumberedTextView : public QFrame
0075 {
0076     Q_OBJECT
0077     Q_PROPERTY(QString text READ text WRITE setText)
0078     Q_PROPERTY(int currentLine READ currentLine WRITE setCurrentLine)
0079     Q_PROPERTY(int stopLine READ stopLine WRITE setStopLine)
0080     Q_PROPERTY(int bugLine READ bugLine WRITE setBugLine)
0081 public:
0082     NumberedTextView(QWidget *parent = nullptr);
0083     ~NumberedTextView() override;
0084 
0085     /** Returns the QTextEdit of the main view. */
0086     QTextEdit *textEdit() const
0087     {
0088         return view;
0089     }
0090 
0091     /**
0092      * Sets the line that should have the current line indicator.
0093      * A value of -1 indicates no line should show the indicator.
0094      */
0095     void setCurrentLine(int lineno);
0096 
0097     /**
0098      * Sets the line that should have the stop line indicator.
0099      * A value of -1 indicates no line should show the indicator.
0100      */
0101     void setStopLine(int lineno);
0102 
0103     /**
0104      * Sets the line that should have the bug line indicator.
0105      * A value of -1 indicates no line should show the indicator.
0106      */
0107     void setBugLine(int lineno);
0108 
0109     int currentLine() const;
0110     int stopLine() const;
0111     int bugLine() const;
0112 
0113     /** @internal Used to get tooltip events from the view for the hover signal. */
0114     bool eventFilter(QObject *obj, QEvent *event) override;
0115 
0116     QString text() const;
0117     void setText(const QString &text);
0118 
0119 Q_SIGNALS:
0120     /**
0121      * Emitted when the mouse is hovered over the text edit component.
0122      * @param word The word under the mouse pointer
0123      */
0124     void mouseHover(const QString &word);
0125 
0126     /**
0127      * Emitted when the mouse is hovered over the text edit component.
0128      * @param pos The position of the mouse pointer.
0129      * @param word The word under the mouse pointer
0130      */
0131     void mouseHover(const QPoint &pos, const QString &word);
0132 
0133 protected Q_SLOTS:
0134     /** @internal Used to update the highlight on the current line. */
0135     void textChanged(int pos, int added, int removed);
0136 
0137 private:
0138     QTextEdit *view;
0139     NumberBar *numbers;
0140     QHBoxLayout *box;
0141     //int m_currentLine;
0142     QTextCursor highlight;
0143 };
0144 
0145 #endif // NUMBERED_TEXT_VIEW_H
0146