summaryrefslogtreecommitdiffstats
path: root/qt-ui/tagwidget.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tcanabrava@kde.org>2014-05-27 20:06:24 -0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-05-27 20:03:29 -0700
commit7320748acead7eb1853c32348c7cff26d7482e58 (patch)
tree470e3a236dc4bbca1ef5a8ee0c7b3c60c66ab7ea /qt-ui/tagwidget.cpp
parentf2ecb6d0ebb9183fad9fd2d33e1cb7dbd6a712d7 (diff)
downloadsubsurface-7320748acead7eb1853c32348c7cff26d7482e58.tar.gz
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>
Diffstat (limited to 'qt-ui/tagwidget.cpp')
-rw-r--r--qt-ui/tagwidget.cpp49
1 files changed, 8 insertions, 41 deletions
diff --git a/qt-ui/tagwidget.cpp b/qt-ui/tagwidget.cpp
index 8c45dceec..fe4044a85 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;
}
}