From 2e22e2ccf7482071bbfd7dc6e44c6606b0d86364 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 28 Apr 2007 22:37:12 +0200 Subject: [PATCH] Prepare for QTextEdit transition: Bring KTextView closer to QTextEdit. This implements text() and setText() as well as insertParagraph() and removeParagraph(). --- kdbg/sourcewnd.cpp | 95 ++++++++---------------------------------------------- kdbg/textvw.cpp | 71 ++++++++++++++++++++++++++++++++++++++-- kdbg/textvw.h | 8 ++++- 3 files changed, 88 insertions(+), 86 deletions(-) diff --git a/kdbg/sourcewnd.cpp b/kdbg/sourcewnd.cpp index 080a293..13d072a 100644 --- a/kdbg/sourcewnd.cpp +++ b/kdbg/sourcewnd.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -49,28 +48,16 @@ bool SourceWindow::loadFile() return false; } - bool upd = autoUpdate(); - setAutoUpdate(false); - QTextStream t(&f); - QString s; - while (!t.eof()) { - s = t.readLine(); - insertLine(s); - } + setText(t.read()); f.close(); - setAutoUpdate(upd); - if (upd) { - updateTableSize(); - } - // then we copy it into our own m_sourceCode - int n = m_texts.size(); + int n = paragraphs(); m_sourceCode.resize(n); m_rowToLine.resize(n); for (int i = 0; i < n; i++) { - m_sourceCode[i].code = m_texts[i]; + m_sourceCode[i].code = text(i); m_rowToLine[i] = i; } m_lineItems.resize(n, 0); @@ -90,53 +77,19 @@ void SourceWindow::reloadFile() m_sourceCode.clear(); /* clear old text */ QTextStream t(&f); - std::back_insert_iterator > it(m_sourceCode); - SourceLine s; - while (!t.eof()) { - s.code = t.readLine(); - *it = s; - } + setText(t.read()); f.close(); - bool autoU = autoUpdate(); - setAutoUpdate(false); - - int lineNo = QMIN(m_texts.size(), m_sourceCode.size()); - for (int i = 0; i < lineNo; i++) { - replaceLine(i, m_sourceCode[i].code); - } - if (m_sourceCode.size() > m_texts.size()) { - // the new file has more lines than the old one - // here lineNo is the number of lines of the old file - for (size_t i = lineNo; i < m_sourceCode.size(); i++) { - insertLine(m_sourceCode[i].code); - } - } else { - // the new file has fewer lines - // here lineNo is the number of lines of the new file - // remove the excessive lines - m_texts.resize(lineNo); + m_sourceCode.resize(paragraphs()); + for (size_t i = 0; i < m_sourceCode.size(); i++) { + m_sourceCode[i].code = text(i); } // allocate line items m_lineItems.resize(m_sourceCode.size(), 0); - setNumRows(m_sourceCode.size()); - m_rowToLine.resize(m_sourceCode.size()); for (size_t i = 0; i < m_sourceCode.size(); i++) m_rowToLine[i] = i; - - setAutoUpdate(autoU); - if (autoU && isVisible()) { - updateTableSize(); - repaint(); - } - - // if the cursor is in the deleted lines, move it to the last line - if (m_curRow >= int(m_texts.size())) { - m_curRow = -1; /* at this point don't have an active row */ - activateLine(m_texts.size()-1); /* now we have */ - } } void SourceWindow::scrollTo(int lineNo, const DbgAddr& address) @@ -453,7 +406,7 @@ bool SourceWindow::wordAtPoint(const QPoint& p, QString& word, QRect& r) return false; // isolate the word at row, col - QString line = m_texts[row]; + QString line = text(row); if (!isident(line[col])) return false; @@ -633,25 +586,12 @@ void SourceWindow::expandRow(int row) ++row; m_rowToLine.insert(m_rowToLine.begin()+row, disass.size(), line); m_lineItems.insert(m_lineItems.begin()+row, disass.size(), 0); - m_texts.insert(m_texts.begin()+row, disass.begin(), disass.end()); - - bool autoU = autoUpdate(); - setAutoUpdate(false); - - // update line widths for (size_t i = 0; i < disass.size(); i++) { - updateCellSize(disass[i]); + insertParagraph(disass[i], row++); } - - setNumRows(m_texts.size()); + update(); // line items emit expanded(line); /* must set PC */ - - setAutoUpdate(autoU); - if (autoU && isVisible()) { - updateTableSize(); - update(); - } } void SourceWindow::collapseRow(int row) @@ -667,20 +607,11 @@ void SourceWindow::collapseRow(int row) ++row; m_rowToLine.erase(m_rowToLine.begin()+row, m_rowToLine.begin()+end); m_lineItems.erase(m_lineItems.begin()+row, m_lineItems.begin()+end); - m_texts.erase(m_texts.begin()+row, m_texts.begin()+end); - - bool autoU = autoUpdate(); - setAutoUpdate(false); - - setNumRows(m_texts.size()); + while (--end >= row) + removeParagraph(end); + update(); // line items emit collapsed(line); - - setAutoUpdate(autoU); - if (autoU && isVisible()) { - updateTableSize(); - update(); - } } void SourceWindow::activeLine(int& line, DbgAddr& address) diff --git a/kdbg/textvw.cpp b/kdbg/textvw.cpp index cc79730..e2310df 100644 --- a/kdbg/textvw.cpp +++ b/kdbg/textvw.cpp @@ -11,6 +11,7 @@ #include "config.h" #endif #include "mydebug.h" +#include #define DEFAULT_WIDTH 100 #define DEFAULT_LINEHEIGHT 1 @@ -57,6 +58,44 @@ bool KTextView::updateCellSize(const QString& text) return update; } +void KTextView::setText(const QString& text) +{ + QStringList l = QStringList::split('\n', text, true); + + bool autoU = autoUpdate(); + setAutoUpdate(false); + + int lineNo = QMIN(m_texts.size(), l.size()); + for (int i = 0; i < lineNo; i++) { + replaceLine(i, l[i]); + } + if (l.size() > m_texts.size()) { + // the new text has more lines than the old one + // here lineNo is the number of lines of the old text + for (size_t i = lineNo; i < l.size(); i++) { + insertLine(l[i]); + } + } else { + // the new file has fewer lines + // here lineNo is the number of lines of the new file + // remove the excessive lines + m_texts.resize(lineNo); + setNumRows(lineNo); + } + + setAutoUpdate(autoU); + if (autoU) { + updateTableSize(); + update(); + } + + // if the cursor is in the deleted lines, move it to the last line + if (m_curRow >= int(m_texts.size())) { + m_curRow = -1; /* at this point don't have an active row */ + activateLine(m_texts.size()-1); /* now we have */ + } +} + void KTextView::insertLine(const QString& text) { m_texts.push_back(text); @@ -91,6 +130,32 @@ void KTextView::replaceLine(int line, const QString& text) } } +void KTextView::insertParagraph(const QString& text, int row) +{ + m_texts.insert(m_texts.begin()+row, text); + + // update line widths + updateCellSize(text); + + setNumRows(m_texts.size()); + + if (autoUpdate() && isVisible()) { + updateTableSize(); + update(); + } +} + +void KTextView::removeParagraph(int row) +{ + m_texts.erase(m_texts.begin()+row); + setNumRows(m_texts.size()); + + if (autoUpdate() && isVisible()) { + updateTableSize(); + update(); + } +} + void KTextView::setCursorPosition(int row, int) { activateLine(row); @@ -120,7 +185,7 @@ int KTextView::textCol() const void KTextView::paintCell(QPainter* p, int row, int /*col*/) { - if (row >= m_texts.size()) { + if (row >= int(m_texts.size())) { return; } if (row == m_curRow) { @@ -254,7 +319,7 @@ void KTextView::paletteChange(const QPalette& oldPal) // recompute window size m_width = DEFAULT_WIDTH; m_height = DEFAULT_LINEHEIGHT; - for (int i = 0; i < m_texts.size(); i++) { + for (size_t i = 0; i < m_texts.size(); i++) { updateCellSize(m_texts[i]); } updateTableSize(); @@ -270,7 +335,7 @@ void KTextView::setTabWidth(int numChars) // recompute window width m_width = DEFAULT_WIDTH; - for (int i = 0; i < m_texts.size(); i++) { + for (size_t i = 0; i < m_texts.size(); i++) { updateCellSize(m_texts[i]); } diff --git a/kdbg/textvw.h b/kdbg/textvw.h index 38774e1..f992022 100644 --- a/kdbg/textvw.h +++ b/kdbg/textvw.h @@ -19,6 +19,11 @@ public: void replaceLine(int line, const QString& text); virtual void setCursorPosition(int row, int col); virtual void cursorPosition(int* row, int* col); + void setText(const QString& t); + const QString& text(int i) const { return m_texts[i]; } + int paragraphs() const { return m_texts.size(); } + void insertParagraph(const QString& text, int row); + void removeParagraph(int row); int charAt(const QPoint& p, int* para); protected: virtual int cellWidth(int col) const; @@ -48,8 +53,9 @@ protected: int m_height; /* line height */ int m_tabWidth; /* in pixels */ - std::vector m_texts; int m_curRow; /* cursor position */ +private: + std::vector m_texts; }; #endif // TEXTVW_H -- 2.11.4.GIT