From 765c9edde2584288a964dd63c38b077b66f8b766 Mon Sep 17 00:00:00 2001 From: Hamatoma Date: Sun, 21 Jun 2015 18:23:08 +0200 Subject: [PATCH] reditor: navigation works --- appl/reditor/mainwindow.cpp | 4 ++ appl/reditor/reditor.pro | 3 +- gui/ReEdit.cpp | 75 ++++++++++++++++++++----------------- gui/ReEdit.hpp | 13 +++---- 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/appl/reditor/mainwindow.cpp b/appl/reditor/mainwindow.cpp index a9064a0..92b679e 100644 --- a/appl/reditor/mainwindow.cpp +++ b/appl/reditor/mainwindow.cpp @@ -19,7 +19,11 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); ReEdit* edit = ui->widget; +#if defined __linux__ m_file = new ReFile("/home/hm/lehrplan.txt", false); +#else + m_file = new ReFile("U:\\ws_cpp\\rplqt\\Doxyfile", false); +#endif edit->setLines(m_file); edit->setCursorLine(0); edit->paragraphs().load(0, 5, edit); diff --git a/appl/reditor/reditor.pro b/appl/reditor/reditor.pro index 24ec39a..7d966a5 100644 --- a/appl/reditor/reditor.pro +++ b/appl/reditor/reditor.pro @@ -21,7 +21,8 @@ SOURCES += main.cpp\ HEADERS += mainwindow.hpp \ ../../base/rebase.hpp \ - ../../gui/regui.hpp + ../../gui/regui.hpp \ + ../../gui/ReEdit.hpp FORMS += mainwindow.ui diff --git a/gui/ReEdit.cpp b/gui/ReEdit.cpp index 2bd3288..63ddd93 100644 --- a/gui/ReEdit.cpp +++ b/gui/ReEdit.cpp @@ -13,6 +13,12 @@ #include "gui/regui.hpp" #include +/** + * Calculates the full line height (with gap between lines) + * + * @param height the line height + * @return the full line height (with gap) + */ inline int heightToFullHeight(int height){ return height * 7 / 6; } @@ -220,7 +226,7 @@ QBrush* ReEdit::createBrush(ReLook::BackGround background){ void ReEdit::paintEvent(QPaintEvent* event){ QRect rect = event->rect(); int lineHeight = heightToFullHeight(m_standardMetrics->height()); - int count = (rect.height() + lineHeight - 1) / lineHeight; + int count = rect.height() / lineHeight; m_paragraphs.load(m_paragraphs.firstLine(), count, this); QPainter painter(this); @@ -280,8 +286,8 @@ void ReEdit::keyPressEvent(QKeyEvent* event){ map = &m_keyShift; else if (alt && !shift && !control) map = &m_keyAlt; - else if (alt && shift && !control) - map = &m_keyAltShift; + else if (control && ! alt && ! shift) + map = &m_keyControl; else if (alt && control && !shift) map = &m_keyAltControl; else if (control && shift && !alt) @@ -310,20 +316,15 @@ int ReEdit::cursorLine() const{ * * @param cursorLine -1 or number of the new cursor line */ -void ReEdit::ensureVisible(int cursorLine){ +void ReEdit::ensureCursorVisible(int cursorLine){ if (cursorLine >= 0) m_cursorLine = cursorLine; int firstLine = m_paragraphs.firstLine(); - int count = m_paragraphs.list().length(); - if (m_cursorLine < firstLine){ - m_paragraphs.load(m_cursorLine, count, this); - } else if (m_firstLine > firstLine + count - 1){ - int maxNo = m_lines->lineCount(); - if (m_firstLine < maxNo - count - 1) - m_paragraphs.load(m_cursorLine, count, this); - else - m_paragraphs.load(maxNo - count - 1, count, this); - } + int pageSize = m_paragraphs.list().length(); + if (m_cursorLine < firstLine + || m_cursorLine >= firstLine + pageSize){ + reposition(m_cursorLine); + } } /** @@ -339,53 +340,43 @@ void ReEdit::editorAction(ReEdit::EditorAction action){ case EA_CHAR_LEFT: if (m_cursorCol-- < -1) m_cursorCol = -1; + ensureCursorVisible(); break; case EA_CHAR_RIGHT: m_cursorCol++; + ensureCursorVisible(); break; case EA_LINE_UP: if (--m_cursorLine < 0) m_cursorLine = 0; - ensureVisible(); + ensureCursorVisible(); break; case EA_LINE_DOWN: if (++m_cursorLine >= m_lines->lineCount()) m_cursorLine = m_lines->lineCount() - 1; - ensureVisible(); + ensureCursorVisible(); break; case EA_BEGIN_OF_LINE: m_cursorCol = -1; - ensureVisible(); + ensureCursorVisible(); break; case EA_END_OF_LINE: m_cursorCol = 99; - ensureVisible(); + ensureCursorVisible(); break; case EA_BEGIN_OF_FILE: - ensureVisible(0); + ensureCursorVisible(0); break; case EA_END_OF_FILE: - ensureVisible(m_lines->lineCount() - 1); + ensureCursorVisible(m_lines->lineCount() - 1); break; case EA_PAGE_UP: // Do not change cursor line! - if (m_paragraphs.firstLine() > 0) - { - int line = m_paragraphs.firstLine() - pageSize; - if (line < 0) - line = 0; - m_paragraphs.load(0, pageSize + 1, this); - } + reposition(m_paragraphs.firstLine() - pageSize); break; case EA_PAGE_DOWN: // Do not change cursor line! - if (m_paragraphs.firstLine() < m_lines->lineCount() - pageSize) - { - int line = m_paragraphs.firstLine() + pageSize; - if (line > m_lines->lineCount() - pageSize) - line = m_lines->lineCount() - pageSize; - m_paragraphs.load(line, pageSize + 1, this); - } + reposition(m_paragraphs.firstLine() + pageSize); break; case EA_DEL_CHAR: break; @@ -403,6 +394,20 @@ void ReEdit::editorAction(ReEdit::EditorAction action){ emit repaint(); } +/** + * Ensures that a given line is visible. + * + * @param firstLine number of the line which should be visible + */ +void ReEdit::reposition(int firstLine){ + int pageSize = m_paragraphs.list().length(); + if (firstLine <= 0) + firstLine = 0; + else if (firstLine >= m_lines->lineCount() - pageSize) + firstLine = m_lines->lineCount() - pageSize + 1; + m_paragraphs.load(firstLine, pageSize, this); +} + /** * Sets the line number of the cursor line. * @param cursorLine the line number [0..N-1] @@ -431,7 +436,7 @@ void ReEdit::setLines(ReLines* lines){ m_lines = lines; m_paragraphs.setLines(lines); int count = height(); - count = count / heightToFullHeight(m_standardMetrics->height()) + 1; + count = count / heightToFullHeight(m_standardMetrics->height()); m_paragraphs.load(m_cursorLine, count, this); emit repaint(); } diff --git a/gui/ReEdit.hpp b/gui/ReEdit.hpp index e4e2a0a..ab71c94 100644 --- a/gui/ReEdit.hpp +++ b/gui/ReEdit.hpp @@ -255,13 +255,12 @@ public: protected: QBrush*createBrush(ReLook::BackGround background); - void ensureVisible(int cursorLine = -1); -//signals: - -public slots: - virtual void paintEvent(QPaintEvent *); - virtual void mousePressEvent(QMouseEvent* event); - virtual void keyPressEvent(QKeyEvent* event); + void ensureCursorVisible(int cursorLine = -1); + void reposition(int firstLine); +protected slots: + void paintEvent(QPaintEvent *); + void mousePressEvent(QMouseEvent* event); + void keyPressEvent(QKeyEvent* event); protected: /// the lines to display ReParagraphs m_paragraphs; -- 2.39.5