From 92f9a4fa1dc63992972250e4894652faf11e9fdf Mon Sep 17 00:00:00 2001 From: Tim Blechmann Date: Sun, 9 Sep 2012 10:55:01 +0200 Subject: [PATCH] scide: refactoring - move LineIndicator to separate source file Signed-off-by: Tim Blechmann --- editors/sc-ide/CMakeLists.txt | 2 + editors/sc-ide/widgets/code_editor/editor.cpp | 70 +--------------- editors/sc-ide/widgets/code_editor/editor.hpp | 33 +------- .../sc-ide/widgets/code_editor/line_indicator.cpp | 94 ++++++++++++++++++++++ .../sc-ide/widgets/code_editor/line_indicator.hpp | 55 +++++++++++++ 5 files changed, 155 insertions(+), 99 deletions(-) create mode 100644 editors/sc-ide/widgets/code_editor/line_indicator.cpp create mode 100644 editors/sc-ide/widgets/code_editor/line_indicator.hpp diff --git a/editors/sc-ide/CMakeLists.txt b/editors/sc-ide/CMakeLists.txt index 9af2064fd..836a65979 100644 --- a/editors/sc-ide/CMakeLists.txt +++ b/editors/sc-ide/CMakeLists.txt @@ -38,6 +38,7 @@ set ( ide_moc_hdr widgets/tool_box.hpp widgets/code_editor/editor.hpp widgets/code_editor/highlighter.hpp + widgets/code_editor/line_indicator.hpp widgets/code_editor/overlay.hpp widgets/code_editor/autocompleter.hpp widgets/settings/dialog.hpp @@ -77,6 +78,7 @@ set ( ide_src widgets/session_switch_dialog.cpp widgets/code_editor/editor.cpp widgets/code_editor/highlighter.cpp + widgets/code_editor/line_indicator.cpp widgets/code_editor/overlay.cpp widgets/code_editor/autocompleter.cpp widgets/settings/dialog.cpp diff --git a/editors/sc-ide/widgets/code_editor/editor.cpp b/editors/sc-ide/widgets/code_editor/editor.cpp index 9f3774f97..9a9293a6d 100644 --- a/editors/sc-ide/widgets/code_editor/editor.cpp +++ b/editors/sc-ide/widgets/code_editor/editor.cpp @@ -21,6 +21,7 @@ #include "autocompleter.hpp" #include "editor.hpp" #include "highlighter.hpp" +#include "line_indicator.hpp" #include "../sc_symbol_references.hpp" #include "../../core/doc_manager.hpp" #include "../../core/main.hpp" @@ -38,75 +39,6 @@ namespace ScIDE { -LineIndicator::LineIndicator( CodeEditor *editor ): - QWidget( editor ), mEditor(editor) -{ - setLineCount(1); -} - -void LineIndicator::setLineCount( int count ) -{ - mLineCount = count; - setFixedWidth( widthForLineCount(count) ); - Q_EMIT( widthChanged() ); -} - -void LineIndicator::changeEvent( QEvent *e ) -{ - if( e->type() == QEvent::FontChange ) { - setFixedWidth( widthForLineCount(mLineCount) ); - Q_EMIT( widthChanged() ); - } - else - QWidget::changeEvent(e); -} - -void LineIndicator::paintEvent( QPaintEvent *e ) -{ mEditor->paintLineIndicator(e); } - -void LineIndicator::mousePressEvent( QMouseEvent *e ) -{ - QTextCursor cursor = mEditor->cursorForPosition(QPoint(0, e->pos().y())); - if(cursor.isNull()) { - mLastCursorPos = -1; - return; - } - mEditor->setTextCursor(cursor); - mLastCursorPos = cursor.position(); -} - -void LineIndicator::mouseMoveEvent( QMouseEvent *e ) -{ - QTextCursor cursor = mEditor->cursorForPosition(QPoint(0, e->pos().y())); - if(cursor.isNull() || cursor.position() == mLastCursorPos) - return; - QTextCursor origCursor = mEditor->textCursor(); - origCursor.setPosition( cursor.position(), QTextCursor::KeepAnchor ); - mEditor->setTextCursor(origCursor); - mLastCursorPos = cursor.position(); - // The selectionChanged() signal of the editor does not always fire here! - // Qt bug? - update(); -} - -void LineIndicator::mouseDoubleClickEvent( QMouseEvent *e ) -{ - QTextCursor cursor = mEditor->cursorForPosition(QPoint(0, e->pos().y())); - cursor.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor ); - mEditor->setTextCursor(cursor); -} - -int LineIndicator::widthForLineCount( int lineCount ) -{ - int digits = 2; - while( lineCount >= 100 ) { - lineCount /= 10; - ++digits; - } - - return 6 + fontMetrics().width('9') * digits; -} - CodeEditor::CodeEditor( Document *doc, QWidget *parent ) : QPlainTextEdit( parent ), mLineIndicator( new LineIndicator(this) ), diff --git a/editors/sc-ide/widgets/code_editor/editor.hpp b/editors/sc-ide/widgets/code_editor/editor.hpp index d0744ca3e..6250bfaf2 100644 --- a/editors/sc-ide/widgets/code_editor/editor.hpp +++ b/editors/sc-ide/widgets/code_editor/editor.hpp @@ -28,38 +28,11 @@ #include #include -namespace ScIDE -{ +namespace ScIDE { namespace Settings { class Manager; } class Document; -class CodeEditor; -class AutoCompleter; - -class LineIndicator : public QWidget -{ - Q_OBJECT - -public: - LineIndicator( CodeEditor *editor ); -Q_SIGNALS: - void widthChanged(); -public Q_SLOTS: - void setLineCount( int ); -protected: - virtual void paintEvent( QPaintEvent *e ); - virtual void mousePressEvent( QMouseEvent * ); - virtual void mouseMoveEvent( QMouseEvent * ); - virtual void mouseDoubleClickEvent( QMouseEvent * ); - virtual void changeEvent( QEvent * ); -private: - int widthForLineCount( int lineCount ); - - CodeEditor *mEditor; - int mLineCount; - int mLastCursorPos; -}; class CodeEditor : public QPlainTextEdit { @@ -168,7 +141,7 @@ private: QTextCursor cursorAt( const TokenIterator, int offset = 0 ); - LineIndicator *mLineIndicator; + class LineIndicator *mLineIndicator; Document *mDoc; @@ -183,7 +156,7 @@ private: QGraphicsScene *mOverlay; - AutoCompleter *mAutoCompleter; + class AutoCompleter *mAutoCompleter; }; } // namespace ScIDE diff --git a/editors/sc-ide/widgets/code_editor/line_indicator.cpp b/editors/sc-ide/widgets/code_editor/line_indicator.cpp new file mode 100644 index 000000000..e922e54ea --- /dev/null +++ b/editors/sc-ide/widgets/code_editor/line_indicator.cpp @@ -0,0 +1,94 @@ +/* + SuperCollider Qt IDE + Copyright (c) 2012 Jakob Leben & Tim Blechmann + http://www.audiosynth.com + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "editor.hpp" +#include "line_indicator.hpp" + +using namespace ScIDE; + +LineIndicator::LineIndicator( CodeEditor *editor ): + QWidget( editor ), mEditor(editor) +{ + setLineCount(1); +} + +void LineIndicator::setLineCount( int count ) +{ + mLineCount = count; + setFixedWidth( widthForLineCount(count) ); + Q_EMIT( widthChanged() ); +} + +void LineIndicator::changeEvent( QEvent *e ) +{ + if( e->type() == QEvent::FontChange ) { + setFixedWidth( widthForLineCount(mLineCount) ); + Q_EMIT( widthChanged() ); + } + else + QWidget::changeEvent(e); +} + +void LineIndicator::paintEvent( QPaintEvent *e ) +{ mEditor->paintLineIndicator(e); } + +void LineIndicator::mousePressEvent( QMouseEvent *e ) +{ + QTextCursor cursor = mEditor->cursorForPosition(QPoint(0, e->pos().y())); + if(cursor.isNull()) { + mLastCursorPos = -1; + return; + } + mEditor->setTextCursor(cursor); + mLastCursorPos = cursor.position(); +} + +void LineIndicator::mouseMoveEvent( QMouseEvent *e ) +{ + QTextCursor cursor = mEditor->cursorForPosition(QPoint(0, e->pos().y())); + if(cursor.isNull() || cursor.position() == mLastCursorPos) + return; + QTextCursor origCursor = mEditor->textCursor(); + origCursor.setPosition( cursor.position(), QTextCursor::KeepAnchor ); + mEditor->setTextCursor(origCursor); + mLastCursorPos = cursor.position(); + // The selectionChanged() signal of the editor does not always fire here! + // Qt bug? + update(); +} + +void LineIndicator::mouseDoubleClickEvent( QMouseEvent *e ) +{ + QTextCursor cursor = mEditor->cursorForPosition(QPoint(0, e->pos().y())); + cursor.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor ); + mEditor->setTextCursor(cursor); +} + +int LineIndicator::widthForLineCount( int lineCount ) +{ + int digits = 2; + while( lineCount >= 100 ) { + lineCount /= 10; + ++digits; + } + + return 6 + fontMetrics().width('9') * digits; +} + diff --git a/editors/sc-ide/widgets/code_editor/line_indicator.hpp b/editors/sc-ide/widgets/code_editor/line_indicator.hpp new file mode 100644 index 000000000..b227c7017 --- /dev/null +++ b/editors/sc-ide/widgets/code_editor/line_indicator.hpp @@ -0,0 +1,55 @@ +/* + SuperCollider Qt IDE + Copyright (c) 2012 Jakob Leben & Tim Blechmann + http://www.audiosynth.com + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef SCIDE_WIDGETS_CODE_EDITOR_LINE_INDICATOR_HPP_INCLUDED +#define SCIDE_WIDGETS_CODE_EDITOR_LINE_INDICATOR_HPP_INCLUDED + +#include + +namespace ScIDE { + +class LineIndicator : public QWidget +{ + Q_OBJECT + +public: + LineIndicator( class CodeEditor *editor ); +Q_SIGNALS: + void widthChanged(); +public Q_SLOTS: + void setLineCount( int ); +protected: + virtual void paintEvent( QPaintEvent *e ); + virtual void mousePressEvent( QMouseEvent * ); + virtual void mouseMoveEvent( QMouseEvent * ); + virtual void mouseDoubleClickEvent( QMouseEvent * ); + virtual void changeEvent( QEvent * ); +private: + int widthForLineCount( int lineCount ); + + class CodeEditor *mEditor; + int mLineCount; + int mLastCursorPos; +}; + +} + + +#endif // SCIDE_WIDGETS_CODE_EDITOR_LINE_INDICATOR_HPP_INCLUDED -- 2.11.4.GIT