From 522de5cc6e74e92d9cd7497c70705dfeed89c91d Mon Sep 17 00:00:00 2001 From: hama Date: Wed, 2 Sep 2015 21:48:12 +0200 Subject: [PATCH] ReEditor: tab handling works --- appl/reide/projectselection.cpp | 2 +- cunit/cuReEdit.cpp | 40 +++++++++++++++++++++++++++++---- gui/ReEdit.cpp | 27 +++++++++++----------- gui/ReEdit.hpp | 3 +-- resources/tab4.txt | 11 +++++++++ 5 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 resources/tab4.txt diff --git a/appl/reide/projectselection.cpp b/appl/reide/projectselection.cpp index 742352f..e8972a7 100644 --- a/appl/reide/projectselection.cpp +++ b/appl/reide/projectselection.cpp @@ -127,7 +127,7 @@ void ProjectSelection::buildTableInfo(ReSettings* settings, const char* key, for (it = files.cbegin(); it != files.cend(); ++it) { QFileInfo file(*it); if (file.exists()) { - QString info = file.baseName(); + QString info = file.fileName(); if (withDate) info.append("\t").append( file.lastModified().toString("yyyy.mm.dd/HH:MM:SS")); diff --git a/cunit/cuReEdit.cpp b/cunit/cuReEdit.cpp index ea7f7eb..acb57ae 100644 --- a/cunit/cuReEdit.cpp +++ b/cunit/cuReEdit.cpp @@ -18,8 +18,8 @@ class TestReEdit: public ReTest, protected ReEdit { public: TestReEdit() : - ReTest("ReEdit"), - ReEdit(NULL) { + ReTest("ReEdit"), + ReEdit(NULL) { doIt(); } @@ -31,7 +31,7 @@ public: setLines(&m_lines); } void pushKey(int key, const QString& text = "", - Qt::KeyboardModifiers modifiers = Qt::NoModifier) { + Qt::KeyboardModifiers modifiers = Qt::NoModifier) { QKeyEvent event(QEvent::KeyPress, key, modifiers, text); keyPressEvent(&event); } @@ -69,7 +69,7 @@ public: checkEqu(expectedCol, m_cursorCol); } void checkCursorPos(EditorAction action, int expectedLineNo, - int expectedCol) { + int expectedCol) { editorAction(action); checkEqu(expectedLineNo, m_cursorLineNo); checkEqu(expectedCol, m_cursorCol); @@ -314,7 +314,39 @@ public: log("ok"); } + void testIndexToColumn(){ + /* + *
tabulator width: 4
+		 * _01230123
+		 * _	x	index: 0 blanks: 3
+		 * _x	x	index: 1 blanks: 2
+		 * _xx	x	index: 2 blanks: 1
+		 * _xxx	x	index: 3 blanks: 0
+		 * 
+ */ + int tabWidth = 4; + checkEqu(0, ReParagraphs::indexToColumn(0, tabWidth, "\tx")); + checkEqu(4, ReParagraphs::indexToColumn(1, tabWidth, "\tx")); + checkEqu(5, ReParagraphs::indexToColumn(2, tabWidth, "\tx")); + checkEqu(5, ReParagraphs::indexToColumn(3, tabWidth, "\tx")); + + checkEqu(1, ReParagraphs::indexToColumn(1, tabWidth, "1\tx")); + checkEqu(4, ReParagraphs::indexToColumn(2, tabWidth, "1\tx")); + checkEqu(5, ReParagraphs::indexToColumn(3, tabWidth, "1\tx")); + + checkEqu(1, ReParagraphs::indexToColumn(1, tabWidth, "12\tx")); + checkEqu(2, ReParagraphs::indexToColumn(2, tabWidth, "12\tx")); + checkEqu(4, ReParagraphs::indexToColumn(3, tabWidth, "12\tx")); + checkEqu(5, ReParagraphs::indexToColumn(5, tabWidth, "12\tx")); + + checkEqu(1, ReParagraphs::indexToColumn(1, tabWidth, "123\tx")); + checkEqu(2, ReParagraphs::indexToColumn(2, tabWidth, "123\tx")); + checkEqu(3, ReParagraphs::indexToColumn(3, tabWidth, "123\tx")); + checkEqu(4, ReParagraphs::indexToColumn(4, tabWidth, "123\tx")); + } + virtual void run() { + testIndexToColumn(); testDeleteLine(); testDeleteText(); testEnterText(); diff --git a/gui/ReEdit.cpp b/gui/ReEdit.cpp index a787dc0..3007ba8 100644 --- a/gui/ReEdit.cpp +++ b/gui/ReEdit.cpp @@ -16,7 +16,7 @@ QStringList ReParagraphs::m_tabStrings; // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK QChar ReParagraphs::m_tabChar = QChar(0xBB); -int ReParagraphs::m_tabWidth = 3; +int ReParagraphs::m_tabWidth = 4; /** * Calculates the full line height (with gap between lines) @@ -140,7 +140,7 @@ ReEdit::ReEdit(QWidget* parent) : assignColorsStandard(); - setTabStrings(3); + setTabStrings(4); appendBuilder(new ReParagraphBuilder()); appendBuilder(new ReCursortLineBuilder()); assignKeysStandard(); @@ -678,7 +678,7 @@ void ReEdit::paintEvent(QPaintEvent* event) { ReLook* lookStd = lookOf(ReLook::FG_STANDARD, ReLook::BG_STANDARD); int maxIx = min(m_list.length(), m_lines->lineCount() - m_firstLine); for (int ix = 0; ix < maxIx; ix++, lineNo++) { - QString number = QString::number(lineNo) + ":"; + QString number = QString::number(lineNo); ReLook* look = lineNo == m_cursorLineNo + 1 ? lookOf(ReLook::FG_CURRENT_LINE, ReLook::BG_CURRENT_LINE) : @@ -697,11 +697,10 @@ void ReEdit::paintEvent(QPaintEvent* event) { && m_cursorLineNo < firstLine + pageSize) { ReParagraph* cursorPara = cursorParagraph(); int col = min(m_cursorCol, cursorPara->m_columns - 1); - if (col != -1) - col = indexToColumn(col, m_tabWidth, - m_lines->lineAt(m_cursorLineNo)) - m_firstCol; + col = indexToColumn(col + 1, m_tabWidth, + m_lines->lineAt(m_cursorLineNo)) - m_firstCol; int x = rect.left() + m_widthLineNumbers + 1 - + (col + 1) * lookStd->m_metrics->width('x'); + + col * 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); @@ -764,7 +763,7 @@ void ReEdit::setTabStrings(int tabWidth) { m_tabStrings.clear(); QString blanks; blanks.fill(' ', tabWidth); - for (int ix = 0; ix <= tabWidth - 1; ix++) { + for (int ix = 0; ix < tabWidth; ix++) { m_tabStrings.append(m_tabChar + blanks.mid(0, tabWidth - 1 - ix)); } } @@ -828,6 +827,8 @@ int ReParagraphs::columnToIndex(int column, int tabWidth, int rc = 0; if (column < 0) rc = -1; + else if (column == 0) + rc = 0; else { int cursor = 0; int length = string.length(); @@ -908,15 +909,15 @@ void ReParagraphs::draw(QPainter& painter, int top, int left) { int ReParagraphs::indexToColumn(int index, int tabWidth, const QString& string) { int rc = 0; - if (index >= 0) { + if (index > 0) { int length = string.length(); for (int cursor = 0; cursor < index && cursor < length; cursor++) { - if (string.at(cursor) != m_tabChar) + if (string.at(cursor) != '\t') rc++; else { // if the cursor points to the last char (tab): do not expand! - if (cursor < index - 1) - rc += tabWidth - 1 - (cursor % tabWidth); + if (cursor < index) + rc += tabWidth - (cursor % tabWidth); } } } @@ -1002,7 +1003,7 @@ void ReParagraphBuilder::buildParagraph(ReParagraph& paragraph, int lineNo, } cursor += length; } - QString tabs = ReEdit::tabString(cursor + 1); + QString tabs = ReEdit::tabString(cursor); length = tabs.length(); if (cursor + length > firstCol && cursor < maxCol) { if (cursor < firstCol) diff --git a/gui/ReEdit.hpp b/gui/ReEdit.hpp index a1d14da..127be85 100644 --- a/gui/ReEdit.hpp +++ b/gui/ReEdit.hpp @@ -344,8 +344,7 @@ protected: void drawScrollbars(QPainter& painter, const QRect& rect, double sizeVertical, double posVertical, double sizeHorizontal, double posHorizontal); - void ensureCursorVisible(); -protected slots: + void ensureCursorVisible();protected slots: void keyPressEvent(QKeyEvent* event); void paintEvent(QPaintEvent *); void mouseMoveEvent(QMouseEvent* event); diff --git a/resources/tab4.txt b/resources/tab4.txt new file mode 100644 index 0000000..4006733 --- /dev/null +++ b/resources/tab4.txt @@ -0,0 +1,11 @@ +Tabwidth 4 + x +1 x +12 x +123 x +1234 x + y +1 y +12 y +123 y +1234 x -- 2.39.5