almost 50 lines replaced by 15 AND fixes bug AND adds more features. <3
From 1875f7c5bc8f8c9b1cf8893f80e1597c1daa3d37 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Tue, 27 May 2014 19:45:02 -0300
Subject: [PATCH] Much, Much smarter way of finding the Tags

The old way manually implemented a parser, where it could
simply call a regexp ( or, in my case, a QChar ) that will
split the QString into many, to find the beginning and end
of the strings on the tags.

This patch also fixes a Qt5 off-by-one bug on the tag
Visualization.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/groupedlineedit.cpp | 17 +++++++---------
 qt-ui/tagwidget.cpp       | 49 ++++++++---------------------------------------
 2 files changed, 15 insertions(+), 51 deletions(-)

diff --git a/qt-ui/groupedlineedit.cpp b/qt-ui/groupedlineedit.cpp
index 4ba6621..38f75ca 100644
--- a/qt-ui/groupedlineedit.cpp
+++ b/qt-ui/groupedlineedit.cpp
@@ -42,7 +42,6 @@
 #include <QBrush>
 #include <QColor>
 #include <QPalette>
-#include <QDebug>
 
 struct GroupedLineEdit::Private {
 	struct Block {
@@ -66,7 +65,6 @@ GroupedLineEdit::GroupedLineEdit(QWidget *parent) : QPlainTextEdit(parent),
 	document()->setMaximumBlockCount(1);
 }
 
-
 GroupedLineEdit::~GroupedLineEdit()
 {
 	delete d;
@@ -86,7 +84,6 @@ int GroupedLineEdit::cursorPosition() const
 void GroupedLineEdit::addBlock(int start, int end)
 {
 	Private::Block block;
-
 	block.start = start;
 	block.end = end;
 	block.text = text().mid(start, end - start + 1).trimmed();
@@ -107,8 +104,7 @@ void GroupedLineEdit::removeAllColors()
 QStringList GroupedLineEdit::getBlockStringList()
 {
 	QStringList retList;
-	Private::Block block;
-	foreach (block, d->blocks)
+	foreach (Private::Block block, d->blocks)
 		retList.append(block.text);
 	return retList;
 }
@@ -134,9 +130,7 @@ void GroupedLineEdit::clear()
 void GroupedLineEdit::selectAll()
 {
 	QTextCursor c = textCursor();
-
 	c.select(QTextCursor::LineUnderCursor);
-
 	setTextCursor(c);
 }
 
@@ -153,7 +147,6 @@ QSize GroupedLineEdit::sizeHint() const
 		document()->findBlock(0).layout()->lineAt(0).height() +
 			document()->documentMargin() * 2 +
 			frameWidth() * 2);
-
 	return rs;
 }
 
@@ -190,8 +183,12 @@ void GroupedLineEdit::paintEvent(QPaintEvent *e)
 	QVectorIterator<QColor> i(d->colors);
 	i.toFront();
 	foreach (const Private::Block &block, d->blocks) {
-		qreal start_x = line.cursorToX(block.start, QTextLine::Trailing);
-		qreal end_x = line.cursorToX(block.end + 1, QTextLine::Leading);
+		qreal start_x = line.cursorToX(block.start, QTextLine::Leading);
+#if QT_VERSION >= 0x050000
+		qreal end_x = line.cursorToX(block.end-1, QTextLine::Trailing);
+#else
+		qreal end_x = line.cursorToX(block.end, QTextLine::Trailing);
+#endif
 		QPainterPath path;
 		QRectF rectangle(
 			start_x - 1.0 - double(horizontalScrollBar()->value()),
diff --git a/qt-ui/tagwidget.cpp b/qt-ui/tagwidget.cpp
index 8c45dce..fe4044a 100644
--- a/qt-ui/tagwidget.cpp
+++ b/qt-ui/tagwidget.cpp
@@ -1,6 +1,5 @@
 #include "tagwidget.h"
 #include <QPair>
-#include <QDebug>
 #include <QAbstractItemView>
 #include <QSettings>
 #include <QFont>
@@ -67,50 +66,18 @@ QPair<int, int> TagWidget::getCursorTagPosition()
 	return qMakePair(start, end);
 }
 
-enum ParseState {
-	FINDSTART,
-	FINDEND
-};
-
 void TagWidget::highlight()
 {
 	int i = 0, start = 0, end = 0;
-	ParseState state = FINDEND;
 	removeAllBlocks();
-
-	while (i < text().length()) {
-		if (text().at(i) == ',') {
-			if (state == FINDSTART) {
-				/* Detect empty tags */
-			} else if (state == FINDEND) {
-				/* Found end of tag */
-				if (i > 1) {
-					if (text().at(i - 1) != '\\') {
-						addBlock(start, end);
-						state = FINDSTART;
-					}
-				} else {
-					state = FINDSTART;
-				}
-			}
-		} else if (text().at(i) == ' ') {
-			/* Handled */
-		} else {
-			/* Found start of tag */
-			if (state == FINDSTART) {
-				state = FINDEND;
-				start = i;
-			} else if (state == FINDEND) {
-				end = i;
-			}
-		}
-		i++;
-	}
-	if (state == FINDEND) {
-		if (end < start)
-			end = text().length() - 1;
-		if (text().length() > 0)
-			addBlock(start, end);
+	int lastPos = 0;
+	Q_FOREACH (const QString& s, text().split(QChar(','), QString::SkipEmptyParts)) {
+		QString trimmed = s.trimmed();
+		if (trimmed.isEmpty())
+			continue;
+		int start = text().indexOf(trimmed, lastPos);
+		addBlock(start, trimmed.size() + start);
+		lastPos = trimmed.size() + start;
 	}
 }
 
-- 
1.9.3

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to