From 25551b8652710aeb3c943234c73c1e4797b2a281 Mon Sep 17 00:00:00 2001 From: hama Date: Sun, 26 Jul 2015 02:27:06 +0200 Subject: [PATCH] cursor handling --- appl/reditor/main.cpp | 19 +-- base/ReFile.cpp | 103 ++++++++-------- base/rebase.hpp | 1 + cunit/cuReEdit.cpp | 70 +++++++++++ cunit/cuReException.cpp | 2 +- cunit/cunit.pro | 7 +- gui/ReEdit.cpp | 253 +++++++++++++++++++++------------------- gui/ReEdit.hpp | 5 +- gui/regui.hpp | 7 +- 9 files changed, 276 insertions(+), 191 deletions(-) create mode 100644 cunit/cuReEdit.cpp diff --git a/appl/reditor/main.cpp b/appl/reditor/main.cpp index 302401e..1e12b34 100644 --- a/appl/reditor/main.cpp +++ b/appl/reditor/main.cpp @@ -12,14 +12,15 @@ #include "../reditor/mainwindow.hpp" #include -int main(int argc, char *argv[]){ - QApplication a(argc, argv); - ReLogger logger; - ReDebugAppender appender; - logger.addAppender(&appender); - MainWindow w; - logger.log(LOG_INFO, 1, "start"); - w.show(); +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + ReLogger logger; + ReDebugAppender appender; + appender.setAutoDelete(false); + logger.addAppender(&appender); + MainWindow w; + logger.log(LOG_INFO, 1, "start"); + w.show(); - return a.exec(); + return a.exec(); } diff --git a/base/ReFile.cpp b/base/ReFile.cpp index c616cc8..d22a585 100644 --- a/base/ReFile.cpp +++ b/base/ReFile.cpp @@ -44,8 +44,8 @@ int memicmp(const void* str1, const void* str2, size_t length) { * Constructor. */ ReLines::ReLines() : - QStringList(), - m_empty() { + QStringList(), + m_empty() { } /** * Destructor. @@ -134,7 +134,7 @@ void ReLines::insertLines(int lineNo, const QString& text, bool withUndo) { * false: undo is impossible */ void ReLines::insertPart(int lineNo, int col, const QString& text, - bool withUndo) { + bool withUndo) { if (lineNo >= 0 && lineNo < lineCount() && col >= 0) { if (withUndo) storeInsertPart(lineNo, col, text.length()); @@ -175,7 +175,7 @@ void ReLines::insertText(int lineNo, int col, const QString& text) { if (lastEoLn != endOfLine) { int oldCount = lineCount(); insertLines(lineNo + 1, - text.mid(endOfLine + 1, lastEoLn - endOfLine), true); + text.mid(endOfLine + 1, lastEoLn - endOfLine), true); newLines = lineCount() - oldCount; } if (lastEoLn != text.length() - 1) { @@ -244,7 +244,7 @@ bool ReLines::removePart(int lineNo, int col, int count, bool withUndo) { replace(lineNo, current.mid(0, col)); else replace(lineNo, - current.mid(0, col) + current.mid(col + count)); + current.mid(0, col) + current.mid(col + count)); } } } @@ -342,26 +342,26 @@ void ReLines::undo(int& lineNo, int& col) { * @param filename name of the file */ ReFile::ReFile(const QString& filename, bool readOnly, ReLogger* logger) : - ReLineSource(), - ReLines(), - m_endOfLine(), - m_filename(filename), - m_file(filename), - m_block(NULL), - // in 32-bit address space we allocate only 10 MByte, in 64-bit environments 100 GByte - m_blocksize( - sizeof(void*) <= 4 ? - 10 * 1024 * 1024ll : 0x100ll * 0x10000 * 0x10000), - m_blockOffset(0), - m_filesize(0), - m_startOfLine(NULL), - m_lineLength(0), - m_lineOffset(0), - m_currentLineNo(0), - m_maxLineLength(0x10000), - m_content(), - m_readOnly(readOnly), - m_logger(logger) { + ReLineSource(), + ReLines(), + m_endOfLine(), + m_filename(filename), + m_file(filename), + m_block(NULL), + // in 32-bit address space we allocate only 10 MByte, in 64-bit environments 100 GByte + m_blocksize( + sizeof(void*) <= 4 ? + 10 * 1024 * 1024ll : 0x100ll * 0x10000 * 0x10000), + m_blockOffset(0), + m_filesize(0), + m_startOfLine(NULL), + m_lineLength(0), + m_lineOffset(0), + m_currentLineNo(0), + m_maxLineLength(0x10000), + m_content(), + m_readOnly(readOnly), + m_logger(logger) { #if defined __linux__ setEndOfLine("\n"); #elif defined __WIN32__ @@ -418,7 +418,7 @@ QString ReFile::filename() const { * false: a line has not been found */ bool ReFile::findLine(const char* toFind, bool ignoreCase, int& lineNo, - QString* line) { + QString* line) { bool rc = false; int length; int sourceLength = strlen(toFind); @@ -429,14 +429,14 @@ bool ReFile::findLine(const char* toFind, bool ignoreCase, int& lineNo, const char* ptr = start; int restLength = length - sourceLength + 1; while (restLength > 0 - && (ptr = reinterpret_cast( - ignoreCase ? - memchr(start, first, restLength) : - memichr(start, first, restLength))) != NULL) { + && (ptr = reinterpret_cast( + ignoreCase ? + memchr(start, first, restLength) : + memichr(start, first, restLength))) != NULL) { if (( - ignoreCase ? - _memicmp(ptr, toFind, sourceLength) : - memcmp(ptr, toFind, sourceLength)) == 0) { + ignoreCase ? + _memicmp(ptr, toFind, sourceLength) : + memcmp(ptr, toFind, sourceLength)) == 0) { rc = true; lineNo = m_currentLineNo; QByteArray buffer(m_startOfLine, m_lineLength); @@ -449,7 +449,6 @@ bool ReFile::findLine(const char* toFind, bool ignoreCase, int& lineNo, } return rc; } - /** * Finds the next line with the given conditions. * @@ -467,8 +466,8 @@ bool ReFile::findLine(const char* toFind, bool ignoreCase, int& lineNo, * false: a line has not been found */ bool ReFile::findLine(const QString& includePattern, bool includeIsRegExpr, - bool includeIgnoreCase, const QString& excludePattern, - bool excludeIsRegExpr, bool excludeIgnoreCase, int& lineNo, QString* line) { + bool includeIgnoreCase, const QString& excludePattern, + bool excludeIsRegExpr, bool excludeIgnoreCase, int& lineNo, QString* line) { bool rc = false; if (line != NULL) *line = ""; @@ -481,7 +480,9 @@ bool ReFile::findLine(const QString& includePattern, bool includeIsRegExpr, int ReFile::hasMoreLines(int index) { bool rc = false; -#if 0 +#if 1 + ReUseParameter(index); +#else if (m_countLines >= 0) { rc = index < m_countLines - 1; } else { @@ -512,9 +513,9 @@ char* ReFile::nextLine(int& length) { if (m_currentLineNo == 65639) m_currentLineNo += 0; rc = m_startOfLine = remap(m_lineOffset += m_lineLength, - m_maxLineLength, lineLength); + m_maxLineLength, lineLength); const char* ptr = reinterpret_cast(memchr(rc, '\n', - lineLength)); + lineLength)); if (ptr != NULL) lineLength = ptr - rc + 1; length = m_lineLength = lineLength; @@ -636,7 +637,7 @@ char* ReFile::remap(int64_t offset, int size, int& length) { size = m_filesize - offset; // Note: size <= m_blocksize if (m_block != NULL && offset >= m_blockOffset - && offset + size <= m_blockOffset + m_blocksize) { + && offset + size <= m_blockOffset + m_blocksize) { // new block is inside the internal block: // no remapping needed rc = m_block + (offset - m_blockOffset); @@ -651,7 +652,7 @@ char* ReFile::remap(int64_t offset, int size, int& length) { if (m_block != NULL) m_file.unmap(reinterpret_cast(m_block)); m_block = reinterpret_cast(m_file.map(m_blockOffset, - m_blocksize)); + m_blocksize)); rc = m_block + (offset - m_blockOffset); length = m_blocksize - (rc - m_block); if (length > size) @@ -702,7 +703,7 @@ void ReFile::setFilename(const QString &filename) { * @return the name of an existing directory */ QByteArray ReFile::tempDir(const char* node, const char* parent, - bool withSeparator) { + bool withSeparator) { #if defined __linux__ QByteArray temp("/tmp"); static const char* firstVar = "TMP"; @@ -749,7 +750,7 @@ QByteArray ReFile::tempDir(const char* node, const char* parent, * @return the full name of a temporary file */ QByteArray ReFile::tempFile(const char* node, const char* parent, - bool deleteIfExists) { + bool deleteIfExists) { QByteArray rc(tempDir(parent)); if (!rc.endsWith('/')) rc += '/'; @@ -797,7 +798,7 @@ bool ReFile::write(const QString& filename) { * @param mode file write mode: "w" (write) or "a" (append) */ void ReFile::writeToFile(const char* filename, const char* content, - size_t contentLength, const char* mode) { + size_t contentLength, const char* mode) { FILE* fp = fopen(filename, mode); if (fp != NULL) { if (contentLength == (size_t) - 1) @@ -811,12 +812,12 @@ void ReFile::writeToFile(const char* filename, const char* content, * Constructor. */ ReUndoList::ReUndoList() : - m_list(), - m_current(NULL), - m_lastLine(-1), - m_lastPosition(-1), - m_maxUndoSize(10 * 1024 * 1024), - m_currentUndoSize(0) { + m_list(), + m_current(NULL), + m_lastLine(-1), + m_lastPosition(-1), + m_maxUndoSize(10 * 1024 * 1024), + m_currentUndoSize(0) { } /** @@ -955,7 +956,7 @@ void ReUndoList::storeSplit(int lineNo, int col) { * @param list the list containing the lines for extracting the line content */ void ReUndoList::storeRemoveLines(int lineNo, int count, - const QStringList& list) { + const QStringList& list) { qint64 size = 0; // Calculate the additional space for (int ii = lineNo + count - 1; ii >= lineNo; ii--) diff --git a/base/rebase.hpp b/base/rebase.hpp index 6f98968..ba1697e 100644 --- a/base/rebase.hpp +++ b/base/rebase.hpp @@ -85,6 +85,7 @@ inline double min(double a, double b) { inline int roundInt(double value) { return (int) round(value); } +#define ReUseParameter(var) (void) var #include "remodules.hpp" #include "base/ReByteStorage.hpp" diff --git a/cunit/cuReEdit.cpp b/cunit/cuReEdit.cpp new file mode 100644 index 0000000..ed0564c --- /dev/null +++ b/cunit/cuReEdit.cpp @@ -0,0 +1,70 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. +*/ +#include "base/rebase.hpp" +#include "gui/regui.hpp" +/** @file + * @brief Unit test of the basic exceptions. + */ + +class TestReEdit: public ReTest, protected ReEdit { +public: + TestReEdit() : + ReTest("ReEdit") { + doIt(); + } + +public: + ReLines m_lines; + void init(const QString& lines){ + m_lines.clear(); + m_lines.append(lines); + setLines(&m_lines); + } + + void testEnsureCursorVisible() { + init("abc\n123456789_12\nxyz"); + m_screenWidth = 5; + m_firstLine = 0; + m_firstCol = 0; + m_cursorLineNo = -1; + ensureCursorVisible(); + checkEqu(0, m_firstLineNo); + checkEqu(-1, m_firstCol); + + m_cursorLine = 1; + for (col = 0; col < m_screenWidth; col++){ + m_cursorCol = col; + ensureCursorVisible(); + checkEqu(1, m_firstLineNo); + checkEqu(0, m_firstCol); + } + for (col = m_screenWidth; col < 2* m_screenWidth; col++){ + m_cursorCol = col; + ensureCursorVisible(); + checkEqu(1, m_firstLineNo); + checkEqu(m_screenWidth, m_firstCol); + } + for (col = 2*m_screenWidth; col < 13; col++){ + m_cursorCol = col; + ensureCursorVisible(); + checkEqu(1, m_firstLineNo); + checkEqu(2*m_screenWidth, m_firstCol); + } + log("ok"); + } + virtual void run() { + testBasic(); + } +}; +void TestReEdit() { + TestReEdit test; +} + + + + diff --git a/cunit/cuReException.cpp b/cunit/cuReException.cpp index c597052..eeb107c 100644 --- a/cunit/cuReException.cpp +++ b/cunit/cuReException.cpp @@ -17,7 +17,7 @@ class TestReException: public ReTest { public: TestReException() : - ReTest("RplException") { + ReTest("ReException") { doIt(); } diff --git a/cunit/cunit.pro b/cunit/cunit.pro index 34f1948..b607cd1 100644 --- a/cunit/cunit.pro +++ b/cunit/cunit.pro @@ -4,9 +4,7 @@ # #------------------------------------------------- -QT += core network - -QT -= gui +QT += core network gui TARGET = cunit CONFIG += console @@ -38,7 +36,8 @@ SOURCES += main.cpp \ cuReContainer.cpp \ cuReWriter.cpp \ cuReCharPtrMap.cpp \ - cuReFile.cpp + cuReFile.cpp \ + cuReEdit.cpp HEADERS += \ ../base/ReFile.hpp \ diff --git a/gui/ReEdit.cpp b/gui/ReEdit.cpp index 3ef6213..f20d609 100644 --- a/gui/ReEdit.cpp +++ b/gui/ReEdit.cpp @@ -36,11 +36,11 @@ inline int heightToFullHeight(int height) { * @param edit the parent */ ReLook::ReLook() : - m_font(NULL), - m_metrics(NULL), - m_foreground(FG_STANDARD), - m_background(BG_STANDARD), - m_edit(NULL) { + m_font(NULL), + m_metrics(NULL), + m_foreground(FG_STANDARD), + m_background(BG_STANDARD), + m_edit(NULL) { } /** @@ -50,8 +50,8 @@ ReLook::ReLook() : * @param look the presentation of the text */ ReEditText::ReEditText(const QString& text, ReLook* look) : - m_text(text), - m_look(look) { + m_text(text), + m_look(look) { } @@ -64,7 +64,7 @@ ReEditText::ReEditText(const QString& text, ReLook* look) : * @param edit the edit field (parent) */ void ReCursortLineBuilder::buildParagraph(ReParagraph& paragraph, int lineNo, - ReEdit* edit) { + ReEdit* edit) { if (lineNo == edit->cursorLineNo()) { for (int ix = 0; ix < paragraph.length(); ix++) { ReEditText* text = paragraph.at(ix); @@ -100,32 +100,32 @@ void ReCursortLineBuilder::buildParagraph(ReParagraph& paragraph, int lineNo, * @param parent NULL or a widget which destroy the instance */ ReEdit::ReEdit(QWidget* parent) : - QWidget(parent), - ReMouseCatcher(), - m_widthEdit(0), - m_heightEdit(0), - m_insertMode(true), - m_breakLines(false), - m_widthLineNo(50), - m_widthVScrollBar(16), - m_heightHScrollBar(16), - m_looks(), - m_standardBrush(new QBrush(Qt::SolidPattern)), - m_scrollbarBrush(new QBrush(Qt::SolidPattern)), - m_sliderBrush(new QBrush(Qt::ConicalGradientPattern)), - m_brushColors(), - m_standardPen(new QPen(Qt::SolidLine)), - m_standardFont(NULL), - m_standardMetrics(NULL), - m_fontColors(), - m_keyAlt(), - m_keyAltControl(), - m_keyAltControlShift(), - m_keyAltShift(), - m_keyControl(), - m_keyControlShift(), - m_keyRaw(), - m_keyShift() { + QWidget(parent), + ReMouseCatcher(), + m_widthEdit(0), + m_heightEdit(0), + m_insertMode(true), + m_breakLines(false), + m_widthLineNumbers(50), + m_widthVScrollBar(16), + m_heightHScrollBar(16), + m_looks(), + m_standardBrush(new QBrush(Qt::SolidPattern)), + m_scrollbarBrush(new QBrush(Qt::SolidPattern)), + m_sliderBrush(new QBrush(Qt::ConicalGradientPattern)), + m_brushColors(), + m_standardPen(new QPen(Qt::SolidLine)), + m_standardFont(NULL), + m_standardMetrics(NULL), + m_fontColors(), + m_keyAlt(), + m_keyAltControl(), + m_keyAltControlShift(), + m_keyAltShift(), + m_keyControl(), + m_keyControlShift(), + m_keyRaw(), + m_keyShift() { setFocusPolicy(Qt::WheelFocus); m_standardFont = new QFont("Courier"); m_standardFont->setStyleHint(QFont::TypeWriter); @@ -222,7 +222,7 @@ void ReEdit::assignKeysStandard() { * @param length OUT: the slider length in pixel */ void calcSliderSize(int size, int minSize, double sizeFactor, double posFactor, - int& position, int& length) { + int& position, int& length) { if (sizeFactor > 1.0) sizeFactor = 1.0; if (posFactor > 100) @@ -266,8 +266,8 @@ int ReEdit::cursorLineNo() const { * @param posHorizontal the position of the scrollbar as factor [0..1] */ void ReEdit::drawScrollbars(QPainter& painter, const QRect& rect, - double sizeVertical, double posVertical, double sizeHorizontal, - double posHorizontal) { + double sizeVertical, double posVertical, double sizeHorizontal, + double posHorizontal) { // We paint the vertical scrollbar: QBrush brush(*m_brushColors[ReLook::BG_SCROLLBAR], Qt::SolidPattern); painter.setBrush(brush); @@ -276,14 +276,14 @@ void ReEdit::drawScrollbars(QPainter& painter, const QRect& rect, static int width = 1; static int width2 = 2 * width; m_vScrollBar->setRect(x + width2, rect.top(), m_widthVScrollBar - width2, - rect.height() - m_heightHScrollBar - width); + rect.height() - m_heightHScrollBar - width); painter.drawRect(*m_vScrollBar); // We paint the horizontal scrollbar: - m_hScrollBar->setRect(rect.left() + m_widthLineNo, - rect.bottom() - m_heightHScrollBar + width, - rect.width() - m_widthVScrollBar - m_widthLineNo, - m_heightHScrollBar - width2); + m_hScrollBar->setRect(rect.left() + m_widthLineNumbers, + rect.bottom() - m_heightHScrollBar + width, + rect.width() - m_widthVScrollBar - m_widthLineNumbers, + m_heightHScrollBar - width2); painter.drawRect(*m_hScrollBar); // Slider (vertical) @@ -292,17 +292,17 @@ void ReEdit::drawScrollbars(QPainter& painter, const QRect& rect, int sliderSize = 0; int sliderPos = 0; calcSliderSize(rect.height() - m_heightHScrollBar, m_heightHScrollBar, - sizeVertical, posVertical, sliderPos, sliderSize); + sizeVertical, posVertical, sliderPos, sliderSize); m_vSlider->setRect(x + width2, rect.top() + sliderPos + width, - m_widthVScrollBar - width2, sliderSize - width2); + m_widthVScrollBar - width2, sliderSize - width2); painter.drawRect(*m_vSlider); // Slider (horizontal) - calcSliderSize(rect.width() - m_widthLineNo - m_widthVScrollBar, - m_heightHScrollBar, sizeHorizontal, posHorizontal, sliderPos, - sliderSize); - m_hSlider->setRect(rect.left() + m_widthLineNo + sliderPos, - rect.bottom() - m_heightHScrollBar + width, sliderSize - width, - m_heightHScrollBar - width2); + calcSliderSize(rect.width() - m_widthLineNumbers - m_widthVScrollBar, + m_heightHScrollBar, sizeHorizontal, posHorizontal, sliderPos, + sliderSize); + m_hSlider->setRect(rect.left() + m_widthLineNumbers + sliderPos, + rect.bottom() - m_heightHScrollBar + width, sliderSize - width, + m_heightHScrollBar - width2); painter.drawRect(*m_hSlider); } @@ -345,10 +345,13 @@ void ReEdit::editorAction(ReEdit::EditorAction action) { ensureCursorVisible(); break; case EA_BEGIN_OF_FILE: - ensureCursorVisible(0); + m_cursorLineNo = 0; + m_cursorCol = -1; + ensureCursorVisible(); break; case EA_END_OF_FILE: - ensureCursorVisible(m_lines->lineCount() - 1); + m_cursorLineNo = m_lines->lineCount() - 1; + ensureCursorVisible(); break; case EA_PAGE_UP: // Do not change cursor line! @@ -392,17 +395,21 @@ void ReEdit::editorAction(ReEdit::EditorAction action) { } /** - * Ensures that the cursor line is visible. - * - * @param cursorLine -1 or number of the new cursor line + * Ensures that the cursor is visible. */ -void ReEdit::ensureCursorVisible(int cursorLine) { - if (cursorLine >= 0) - m_cursorLineNo = cursorLine; +void ReEdit::ensureCursorVisible() { if (m_cursorLineNo < m_firstLine - || m_cursorLineNo >= m_firstLine + pageSize()) { + || m_cursorLineNo >= m_firstLine + pageSize()) { reposition(m_cursorLineNo, m_cursorCol); } + if (m_cursorCol < 0) + m_firstCol = 0; + else if ( m_cursorCol < m_firstCol) { + m_firstCol = m_cursorCol; + } else if (m_cursorCol >= m_firstCol + m_screenWidth){ + int length = m_lines->lineAt(m_cursorLineNo).length(); + m_firstCol = max(0, min(m_cursorCol, length - m_screenWidth)); + } } /** @@ -433,7 +440,7 @@ void ReEdit::keyPressEvent(QKeyEvent* event) { m_cursorCol = m_lines->lineAt(m_cursorLineNo - 1).length() - 1; } if (m_lines->removePart(m_cursorLineNo, columnToIndex(currentCol), - 1, true)) + 1, true)) m_cursorLineNo = max(0, m_cursorLineNo - 1); break; } @@ -446,7 +453,7 @@ void ReEdit::keyPressEvent(QKeyEvent* event) { break; } } else if (shift && !keyText.isEmpty() && key != Qt::Key_Delete - && key != Qt::Key_Backspace) { + && key != Qt::Key_Backspace) { m_lines->insertText(m_cursorLineNo, m_cursorCol + 1, keyText); m_cursorCol++; } else { @@ -492,7 +499,7 @@ ReLines& ReEdit::lines() { * @return */ ReLook* ReEdit::lookOf(ReLook::ForeGround foreground, - ReLook::BackGround background) { + ReLook::BackGround background) { int index = foreground * ReLook::BG_COUNT + background; ReLook* rc = m_looks[index]; if (rc == NULL) { @@ -519,8 +526,8 @@ ReLook* ReEdit::lookOf(ReLook::ForeGround foreground, */ void ReEdit::mouseMoveEvent(QMouseEvent* event) { if (m_lastMousePosition.x() >= 0 - && (handleHScrollBar(event, true, this) - || handleVScrollBar(event, true, this))) { + && (handleHScrollBar(event, true, this) + || handleVScrollBar(event, true, this))) { emit repaint(); } } @@ -538,14 +545,15 @@ void ReEdit::mousePressEvent(QMouseEvent* event) { } else { QPoint position = event->pos(); m_cursorLineNo = position.y() - / heightToFullHeight(m_standardMetrics->height()) + m_firstLine; + / heightToFullHeight(m_standardMetrics->height()) + m_firstLine; int x = position.x(); int charWidth = m_standardMetrics->width('x'); - if (x >= m_widthLineNo && x < m_widthEdit - m_widthVScrollBar) { - if (x <= m_widthLineNo + charWidth / 2) - m_cursorCol = -1; + x -= m_widthLineNumbers; + if (x >= 0 && x < m_widthEdit - m_widthLineNumbers - m_widthVScrollBar) { + if (x <= + charWidth / 2) + m_cursorCol = m_firstCol - 1; else - m_cursorCol = columnToIndex((x - m_widthLineNo) / charWidth); + m_cursorCol = m_firstCol + columnToIndex(x / charWidth); } } m_lastMousePosition = event->pos(); @@ -560,6 +568,8 @@ void ReEdit::mousePressEvent(QMouseEvent* event) { * @param event the description of the mouse click */ void ReEdit::mouseReleaseEvent(QMouseEvent* event) { + // avoid warning: + ReUseParameter(event); m_lastMousePosition.setX(-1); m_lastMousePosition.setY(-1); } @@ -577,18 +587,18 @@ void ReEdit::paintEvent(QPaintEvent* event) { int lineHeight = heightToFullHeight(m_standardMetrics->height()); int pageSize = (rect.height() - m_heightHScrollBar) / lineHeight; int charWidth = m_standardMetrics->averageCharWidth(); - int pageWidth = (rect.width() - m_widthVScrollBar - m_widthLineNo) - / charWidth; + int pageWidth = (rect.width() - m_widthVScrollBar - m_widthLineNumbers) + / charWidth; int firstLine = m_firstLine; load(firstLine, pageSize, pageWidth, this); QPainter painter(this); ReLook* look = lookOf(ReLook::FG_STANDARD, ReLook::BG_STANDARD); painter.setBrush(*look->m_brush); - QRect editArea(rect.left() + m_widthLineNo, rect.top(), - rect.right() - m_widthVScrollBar, rect.bottom() - m_heightHScrollBar); + QRect editArea(rect.left() + m_widthLineNumbers, rect.top(), + rect.right() - m_widthVScrollBar, rect.bottom() - m_heightHScrollBar); painter.drawRect(editArea); - draw(painter, rect.top(), rect.left() + m_widthLineNo); - int left = rect.left() + m_widthLineNo - 3; + draw(painter, rect.top(), rect.left() + m_widthLineNumbers); + int left = rect.left() + m_widthLineNumbers - 3; left = rect.left(); int y = 0; int lineNo = firstLine + 1; @@ -596,38 +606,39 @@ void ReEdit::paintEvent(QPaintEvent* event) { for (int ix = 0; ix < m_list.length(); ix++, lineNo++) { QString number = QString::number(lineNo) + ":"; ReLook* look = - lineNo == m_cursorLineNo + 1 ? - lookOf(ReLook::FG_CURRENT_LINE, ReLook::BG_CURRENT_LINE) : - lookStd; + lineNo == m_cursorLineNo + 1 ? + lookOf(ReLook::FG_CURRENT_LINE, ReLook::BG_CURRENT_LINE) : + lookStd; int width = look->m_metrics->width(number); if (ix == 0) y = rect.top() + look->m_metrics->height() - - look->m_metrics->descent(); + - look->m_metrics->descent(); painter.setFont(*look->m_font); painter.setPen(*look->m_pen); - painter.drawText(left + m_widthLineNo - width - 5, y, number); + painter.drawText(left + m_widthLineNumbers - width - 5, y, number); y += lineHeight; } // We paint the cursor: if (m_cursorVisible && m_cursorLineNo >= firstLine - && m_cursorLineNo < firstLine + pageSize) { + && m_cursorLineNo < firstLine + pageSize) { ReParagraph* cursorPara = cursorParagraph(); - int col = max(m_cursorCol, cursorPara->m_columns - 1); - col = indexToColumn(col, m_tabWidth, m_lines->lineAt(m_cursorLineNo)) - - m_firstCol; - int x = rect.left() + m_widthLineNo + 1 - + (col + 1) * lookStd->m_metrics->width('x'); + int col = min(m_cursorCol, cursorPara->m_columns - 1); + if (col != -1) + col = indexToColumn(col, m_tabWidth, m_lines->lineAt(m_cursorLineNo)) + - m_firstCol; + int x = rect.left() + m_widthLineNumbers + 1 + + (col + 1) * lookStd->m_metrics->width('x'); int y = rect.top() + (m_cursorLineNo - firstLine) * lineHeight; painter.setPen(*look->m_pen); painter.drawLine(x, y, x, y + lineHeight); } int maxLines = max(1, m_lines->lineCount() - pageSize); drawScrollbars(painter, rect, double(pageSize) / maxLines, - double(m_firstLine) / maxLines, - m_maxCols == 0 ? 1.0 : (double) m_screenWidth / m_maxCols, - m_maxCols == 0 ? 0.0 : (double) m_firstCol / m_maxCols); + double(m_firstLine) / maxLines, + m_maxCols == 0 ? 1.0 : (double) m_screenWidth / m_maxCols, + m_maxCols == 0 ? 0.0 : (double) m_firstCol / m_maxCols); ReLogger::globalLogger()->logv(LOG_INFO, 3, "draw: %.4f", - double(clock() - start) / CLOCKS_PER_SEC); + double(clock() - start) / CLOCKS_PER_SEC); } /** @@ -687,16 +698,16 @@ void ReEdit::setTabStrings(int tabWidth) { * Constructor. */ ReParagraphs::ReParagraphs() : - m_builders(), - m_firstLine(0), - m_firstCol(0), - m_cursorLineNo(0), - m_cursorCol(-1), - m_lines(NULL), - m_list(), - m_cursorVisible(true), - m_maxCols(0), - m_screenWidth(0) { + m_builders(), + m_firstLine(0), + m_firstCol(0), + m_cursorLineNo(0), + m_cursorCol(-1), + m_lines(NULL), + m_list(), + m_maxCols(0), + m_screenWidth(0), + m_cursorVisible(true) { } /** @@ -738,7 +749,7 @@ void ReParagraphs::clear() { * expanded tabs */ int ReParagraphs::columnToIndex(int column, int tabWidth, - const QString& string) { + const QString& string) { int rc = 0; if (column < 0) rc = -1; @@ -768,7 +779,7 @@ int ReParagraphs::columnToIndex(int column, int tabWidth, ReParagraph* ReParagraphs::cursorParagraph() { ReParagraph* rc = NULL; if (m_cursorLineNo >= m_firstLine - && m_cursorLineNo < m_firstLine + m_list.length()) { + && m_cursorLineNo < m_firstLine + m_list.length()) { rc = m_list.at(m_cursorLineNo - m_firstLine); } return rc; @@ -782,7 +793,7 @@ ReParagraph* ReParagraphs::cursorParagraph() { */ int ReParagraphs::columnToIndex(int cursorCol) { int rc = columnToIndex(cursorCol, m_tabWidth, - m_lines->lineAt(m_cursorLineNo)); + m_lines->lineAt(m_cursorLineNo)); return rc; } @@ -820,7 +831,7 @@ void ReParagraphs::draw(QPainter& painter, int top, int left) { * @return */ int ReParagraphs::indexToColumn(int index, int tabWidth, - const QString& string) { + const QString& string) { int rc = 0; if (index >= 0) { int length = string.length(); @@ -887,13 +898,13 @@ void ReParagraphs::setLines(ReLines* lines) { * @param edit the parent, the edit field */ void ReParagraphBuilder::buildParagraph(ReParagraph& paragraph, int lineNo, - ReEdit* edit) { + ReEdit* edit) { if (paragraph.length() == 0) { int firstCol = edit->m_firstCol; const QString& text = edit->lines().lineAt(lineNo); ReLook* look = edit->lookOf(ReLook::FG_STANDARD, ReLook::BG_STANDARD); ReLook* lookTab = edit->lookOf(ReLook::FG_GREY_LIGHT, - ReLook::BG_STANDARD); + ReLook::BG_STANDARD); paragraph.m_columns = 0; int ixTab; ReEditText* part; @@ -980,14 +991,14 @@ void ReParagraph::draw(QPainter& painter, int& top, int left) { * Constructor. */ ReMouseCatcher::ReMouseCatcher() : - m_clickObjects(), - m_vScrollBar(new ClickPosition(CO_VSCROLLBAR)), - m_hScrollBar(new ClickPosition(CO_HSCROLLBAR)), - m_hSlider(new ClickPosition(CO_HSLIDER)), - m_vSlider(new ClickPosition(CO_VSLIDER)), - m_lastMousePosition(), - m_lastTopVSlider(0), - m_lastLeftHSlider(0) { + m_clickObjects(), + m_vScrollBar(new ClickPosition(CO_VSCROLLBAR)), + m_hScrollBar(new ClickPosition(CO_HSCROLLBAR)), + m_hSlider(new ClickPosition(CO_HSLIDER)), + m_vSlider(new ClickPosition(CO_VSLIDER)), + m_lastMousePosition(), + m_lastTopVSlider(0), + m_lastLeftHSlider(0) { } /** @@ -1020,7 +1031,7 @@ void ReMouseCatcher::insertClickObject(ReMouseCatcher::ClickPosition* object) { * @return true: the mouse click is inside the horizontal sb */ bool ReMouseCatcher::handleHScrollBar(QMouseEvent* event, bool isDragged, - ReEdit* edit) { + ReEdit* edit) { QPoint pos = event->pos(); bool rc = rectContains(*m_hScrollBar, pos, "hScrollBar"); if (rc) { @@ -1030,8 +1041,8 @@ bool ReMouseCatcher::handleHScrollBar(QMouseEvent* event, bool isDragged, int moveGap = m_hScrollBar->width() - m_hSlider->width(); double position = moveGap == 0 ? 0.0 : double(sliderPos) / moveGap; int col = roundInt( - (edit->m_maxCols - edit->m_screenWidth) - * max(0.0, min(position, 1.0))); + (edit->m_maxCols - edit->m_screenWidth) + * max(0.0, min(position, 1.0))); edit->reposition(edit->m_cursorLineNo, col); } else { if (pos.y() < m_hSlider->left()) @@ -1052,10 +1063,10 @@ bool ReMouseCatcher::handleHScrollBar(QMouseEvent* event, bool isDragged, * @return true: the mouse click is inside the vertical sb */ bool ReMouseCatcher::handleVScrollBar(QMouseEvent* event, bool isDragged, - ReEdit* edit) { + ReEdit* edit) { QPoint pos = event->pos(); bool rc = rectContains(*m_vScrollBar, pos, "vScrollBar") - || (isDragged && m_vScrollBar->contains(m_lastMousePosition)); + || (isDragged && m_vScrollBar->contains(m_lastMousePosition)); if (rc) { if (isDragged) { int distance = pos.y() - m_lastMousePosition.y(); @@ -1063,8 +1074,8 @@ bool ReMouseCatcher::handleVScrollBar(QMouseEvent* event, bool isDragged, int moveGap = m_vScrollBar->height() - m_vSlider->height(); double position = moveGap == 0 ? 0.0 : double(sliderPos) / moveGap; int line = roundInt( - (edit->lines().lineCount() - edit->pageSize()) - * max(0.0, min(position, 1.0))); + (edit->lines().lineCount() - edit->pageSize()) + * max(0.0, min(position, 1.0))); edit->reposition(line, edit->m_cursorCol); } else { if (pos.y() < m_vSlider->top()) diff --git a/gui/ReEdit.hpp b/gui/ReEdit.hpp index 591c58b..f56fd3f 100644 --- a/gui/ReEdit.hpp +++ b/gui/ReEdit.hpp @@ -335,7 +335,8 @@ protected: void drawScrollbars(QPainter& painter, const QRect& rect, double sizeVertical, double posVertical, double sizeHorizontal, double posHorizontal); - void ensureCursorVisible(int cursorParagraph = -1);protected slots: + void ensureCursorVisible(); +protected slots: void keyPressEvent(QKeyEvent* event); void paintEvent(QPaintEvent *); void mouseMoveEvent(QMouseEvent* event); @@ -360,7 +361,7 @@ protected: /// true: a file line will be displayed in multiple lines (if long enough) bool m_breakLines; /// number of pixels for the line number - int m_widthLineNo; + int m_widthLineNumbers; /// number of pixels for the right scroll bar int m_widthVScrollBar; /// number of pixels for the bottom scroll bar diff --git a/gui/regui.hpp b/gui/regui.hpp index cb54fac..ba1fc14 100644 --- a/gui/regui.hpp +++ b/gui/regui.hpp @@ -22,11 +22,12 @@ * @return true: the point lays inside the rectangle */ inline bool rectContains(const QRect& rect, const QPoint& point, - const char* what = "") { + const char* what = "") { #if 1 + ReUseParameter(what); return point.x() >= rect.x() && point.y() >= rect.y() - && point.x() < rect.x() + rect.width() - && point.y() < rect.y() + rect.height(); + && point.x() < rect.x() + rect.width() + && point.y() < rect.y() + rect.height(); #else bool rc = point.x() >= rect.x(); char reason = ' '; -- 2.39.5