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 <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-05-27 20:06:24 -03:00 committed by Dirk Hohndel
parent f2ecb6d0eb
commit 7320748ace
2 changed files with 15 additions and 51 deletions

View file

@ -42,7 +42,6 @@
#include <QBrush> #include <QBrush>
#include <QColor> #include <QColor>
#include <QPalette> #include <QPalette>
#include <QDebug>
struct GroupedLineEdit::Private { struct GroupedLineEdit::Private {
struct Block { struct Block {
@ -66,7 +65,6 @@ GroupedLineEdit::GroupedLineEdit(QWidget *parent) : QPlainTextEdit(parent),
document()->setMaximumBlockCount(1); document()->setMaximumBlockCount(1);
} }
GroupedLineEdit::~GroupedLineEdit() GroupedLineEdit::~GroupedLineEdit()
{ {
delete d; delete d;
@ -86,7 +84,6 @@ int GroupedLineEdit::cursorPosition() const
void GroupedLineEdit::addBlock(int start, int end) void GroupedLineEdit::addBlock(int start, int end)
{ {
Private::Block block; Private::Block block;
block.start = start; block.start = start;
block.end = end; block.end = end;
block.text = text().mid(start, end - start + 1).trimmed(); block.text = text().mid(start, end - start + 1).trimmed();
@ -107,8 +104,7 @@ void GroupedLineEdit::removeAllColors()
QStringList GroupedLineEdit::getBlockStringList() QStringList GroupedLineEdit::getBlockStringList()
{ {
QStringList retList; QStringList retList;
Private::Block block; foreach (Private::Block block, d->blocks)
foreach (block, d->blocks)
retList.append(block.text); retList.append(block.text);
return retList; return retList;
} }
@ -134,9 +130,7 @@ void GroupedLineEdit::clear()
void GroupedLineEdit::selectAll() void GroupedLineEdit::selectAll()
{ {
QTextCursor c = textCursor(); QTextCursor c = textCursor();
c.select(QTextCursor::LineUnderCursor); c.select(QTextCursor::LineUnderCursor);
setTextCursor(c); setTextCursor(c);
} }
@ -153,7 +147,6 @@ QSize GroupedLineEdit::sizeHint() const
document()->findBlock(0).layout()->lineAt(0).height() + document()->findBlock(0).layout()->lineAt(0).height() +
document()->documentMargin() * 2 + document()->documentMargin() * 2 +
frameWidth() * 2); frameWidth() * 2);
return rs; return rs;
} }
@ -190,8 +183,12 @@ void GroupedLineEdit::paintEvent(QPaintEvent *e)
QVectorIterator<QColor> i(d->colors); QVectorIterator<QColor> i(d->colors);
i.toFront(); i.toFront();
foreach (const Private::Block &block, d->blocks) { foreach (const Private::Block &block, d->blocks) {
qreal start_x = line.cursorToX(block.start, QTextLine::Trailing); qreal start_x = line.cursorToX(block.start, QTextLine::Leading);
qreal end_x = line.cursorToX(block.end + 1, 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; QPainterPath path;
QRectF rectangle( QRectF rectangle(
start_x - 1.0 - double(horizontalScrollBar()->value()), start_x - 1.0 - double(horizontalScrollBar()->value()),

View file

@ -1,6 +1,5 @@
#include "tagwidget.h" #include "tagwidget.h"
#include <QPair> #include <QPair>
#include <QDebug>
#include <QAbstractItemView> #include <QAbstractItemView>
#include <QSettings> #include <QSettings>
#include <QFont> #include <QFont>
@ -67,50 +66,18 @@ QPair<int, int> TagWidget::getCursorTagPosition()
return qMakePair(start, end); return qMakePair(start, end);
} }
enum ParseState {
FINDSTART,
FINDEND
};
void TagWidget::highlight() void TagWidget::highlight()
{ {
int i = 0, start = 0, end = 0; int i = 0, start = 0, end = 0;
ParseState state = FINDEND;
removeAllBlocks(); removeAllBlocks();
int lastPos = 0;
while (i < text().length()) { Q_FOREACH (const QString& s, text().split(QChar(','), QString::SkipEmptyParts)) {
if (text().at(i) == ',') { QString trimmed = s.trimmed();
if (state == FINDSTART) { if (trimmed.isEmpty())
/* Detect empty tags */ continue;
} else if (state == FINDEND) { int start = text().indexOf(trimmed, lastPos);
/* Found end of tag */ addBlock(start, trimmed.size() + start);
if (i > 1) { lastPos = trimmed.size() + start;
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);
} }
} }