File indexing completed on 2024-05-12 05:40:37

0001 /*
0002     Cahoots is a crossplatform real-time collaborative text editor.
0003 
0004     Copyright (C) 2010 Chris Dimpfl, Anandi Hira, David Vega
0005 
0006     This program is free software: you can redistribute it and/or modify
0007     it under the terms of the GNU General Public License as published by
0008     the Free Software Foundation, either version 3 of the License, or
0009     (at your option) any later version.
0010 
0011     This program is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014     GNU General Public License for more details.
0015 
0016     You should have received a copy of the GNU General Public License
0017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 #ifndef SHAREDNOTES_CODEEDITOR_H
0020 #define SHAREDNOTES_CODEEDITOR_H
0021 
0022 #include "enu.h"
0023 #include "rwidgets_global.h"
0024 #include <QObject>
0025 #include <QPlainTextEdit>
0026 #include <QSettings>
0027 
0028 class QPaintEvent;
0029 class QResizeEvent;
0030 class QSize;
0031 class QWidget;
0032 
0033 class LineNumberArea;
0034 class SharedNoteController;
0035 namespace sharedNotes
0036 {
0037 class RWIDGET_EXPORT CodeEditor : public QPlainTextEdit
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     CodeEditor(SharedNoteController* ctrl, QWidget* parent= nullptr);
0043 
0044     void lineNumberAreaPaintEvent(QPaintEvent* event);
0045     int lineNumberAreaWidth();
0046 
0047     void collabTextChange(int pos, int charsRemoved, int charsAdded, QString data);
0048 
0049     void unCommentSelection();
0050     void shiftLeft();
0051     void shiftRight();
0052 
0053     // Find functions
0054     bool findNext(QString searchString, Qt::CaseSensitivity sensitivity, bool wrapAround, Enu::FindMode mode);
0055     bool findPrev(QString searchString, Qt::CaseSensitivity sensitivity, bool wrapAround, Enu::FindMode mode);
0056     bool replaceAll(QString searchString, QString replaceString, Qt::CaseSensitivity sensitivity, Enu::FindMode mode);
0057     bool replace(QString replaceString);
0058     bool findReplace(QString searchString, QString replaceString, Qt::CaseSensitivity sensitivity, bool wrapAround,
0059                      Enu::FindMode mode);
0060 
0061 public slots:
0062     bool findAll(QString searchString);
0063 
0064 protected:
0065     void resizeEvent(QResizeEvent* event);
0066 
0067 private slots:
0068     void updateLineNumberAreaWidth(int newBlockCount);
0069     void highlightCurrentLine();
0070     void updateLineNumberArea(const QRect&, int);
0071 
0072 private:
0073     SharedNoteController* m_sharedCtrl;
0074     bool isFirstTime;
0075 
0076     QWidget* lineNumberArea;
0077 };
0078 
0079 
0080 class LineNumberArea : public QWidget
0081 {
0082 public:
0083     LineNumberArea(sharedNotes::CodeEditor* editor) : QWidget(editor) { codeEditor= editor; }
0084 
0085     QSize sizeHint() const { return QSize(codeEditor->lineNumberAreaWidth(), 0); }
0086 
0087 protected:
0088     void paintEvent(QPaintEvent* event) { codeEditor->lineNumberAreaPaintEvent(event); }
0089 
0090 private:
0091     sharedNotes::CodeEditor* codeEditor;
0092 };
0093 }
0094 #endif // SHAREDNOTES_CODEEDITOR_H