| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | #include "tagwidget.h"
 | 
					
						
							|  |  |  | #include <QPair>
 | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | #include <QAbstractItemView>
 | 
					
						
							| 
									
										
										
										
											2013-12-19 17:16:57 -02:00
										 |  |  | #include <QSettings>
 | 
					
						
							| 
									
										
										
										
											2014-06-26 14:01:31 -03:00
										 |  |  | #include <QCompleter>
 | 
					
						
							| 
									
										
										
										
											2013-12-19 17:16:57 -02:00
										 |  |  | #include <QFont>
 | 
					
						
							| 
									
										
										
										
											2014-05-16 15:12:46 +09:00
										 |  |  | #include "mainwindow.h"
 | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 15:12:46 +09:00
										 |  |  | TagWidget::TagWidget(QWidget *parent) : GroupedLineEdit(parent), m_completer(NULL), lastFinishedTag(false) | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(reparse())); | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 	connect(this, SIGNAL(textChanged()), this, SLOT(reparse())); | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-19 17:16:57 -02:00
										 |  |  | 	QColor textColor = palette().color(QPalette::Text); | 
					
						
							|  |  |  | 	qreal h, s, l, a; | 
					
						
							|  |  |  | 	textColor.getHslF(&h, &s, &l, &a); | 
					
						
							|  |  |  | 	// I use dark themes
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	if (l <= 0.3) { // very dark text. get a brigth background
 | 
					
						
							|  |  |  | 		addColor(QColor(Qt::red).lighter(120)); | 
					
						
							|  |  |  | 		addColor(QColor(Qt::green).lighter(120)); | 
					
						
							|  |  |  | 		addColor(QColor(Qt::blue).lighter(120)); | 
					
						
							| 
									
										
										
										
											2014-07-11 18:39:03 +01:00
										 |  |  | 	} else if (l <= 0.6) { // moderated dark text. get a somewhat bright background
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		addColor(QColor(Qt::red).lighter(60)); | 
					
						
							|  |  |  | 		addColor(QColor(Qt::green).lighter(60)); | 
					
						
							|  |  |  | 		addColor(QColor(Qt::blue).lighter(60)); | 
					
						
							| 
									
										
										
										
											2014-01-16 11:50:56 +07:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		addColor(QColor(Qt::red).darker(120)); | 
					
						
							|  |  |  | 		addColor(QColor(Qt::green).darker(120)); | 
					
						
							|  |  |  | 		addColor(QColor(Qt::blue).darker(120)); | 
					
						
							| 
									
										
										
										
											2013-12-19 17:16:57 -02:00
										 |  |  | 	} // light text. get a dark background.
 | 
					
						
							| 
									
										
										
										
											2014-01-19 17:33:24 +02:00
										 |  |  | 	setFocusPolicy(Qt::StrongFocus); | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TagWidget::setCompleter(QCompleter *completer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_completer = completer; | 
					
						
							|  |  |  | 	m_completer->setWidget(this); | 
					
						
							|  |  |  | 	connect(m_completer, SIGNAL(activated(QString)), this, SLOT(completionSelected(QString))); | 
					
						
							| 
									
										
										
										
											2014-03-19 18:24:53 +02:00
										 |  |  | 	connect(m_completer, SIGNAL(highlighted(QString)), this, SLOT(completionHighlighted(QString))); | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | QPair<int, int> TagWidget::getCursorTagPosition() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	int i = 0, start = 0, end = 0; | 
					
						
							|  |  |  | 	/* Parse string near cursor */ | 
					
						
							|  |  |  | 	i = cursorPosition(); | 
					
						
							|  |  |  | 	while (--i > 0) { | 
					
						
							|  |  |  | 		if (text().at(i) == ',') { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 			if (i > 0 && text().at(i - 1) != '\\') { | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 				i++; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	start = i; | 
					
						
							|  |  |  | 	while (++i < text().length()) { | 
					
						
							|  |  |  | 		if (text().at(i) == ',') { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 			if (i > 0 && text().at(i - 1) != '\\') | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	end = i; | 
					
						
							|  |  |  | 	if (start < 0 || end < 0) { | 
					
						
							|  |  |  | 		start = 0; | 
					
						
							|  |  |  | 		end = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	return qMakePair(start, end); | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TagWidget::highlight() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	removeAllBlocks(); | 
					
						
							| 
									
										
										
										
											2014-05-27 20:06:24 -03:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TagWidget::reparse() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	highlight(); | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	QPair<int, int> pos = getCursorTagPosition(); | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	QString currentText; | 
					
						
							|  |  |  | 	if (pos.first >= 0 && pos.second > 0) | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		currentText = text().mid(pos.first, pos.second - pos.first).trimmed(); | 
					
						
							| 
									
										
										
										
											2014-03-19 15:36:09 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	if (m_completer) { | 
					
						
							|  |  |  | 		m_completer->setCompletionPrefix(currentText); | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 		if (m_completer->completionCount() == 1) { | 
					
						
							|  |  |  | 			if (m_completer->currentCompletion() == currentText) { | 
					
						
							|  |  |  | 				QAbstractItemView *popup = m_completer->popup(); | 
					
						
							|  |  |  | 				if (popup) | 
					
						
							|  |  |  | 					popup->hide(); | 
					
						
							| 
									
										
										
										
											2014-01-16 11:50:56 +07:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 				m_completer->complete(); | 
					
						
							| 
									
										
										
										
											2014-01-16 11:50:56 +07:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			m_completer->complete(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | void TagWidget::completionSelected(const QString &completion) | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-19 15:23:58 -03:00
										 |  |  | 	completionHighlighted(completion); | 
					
						
							|  |  |  | 	emit textChanged(); | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | void TagWidget::completionHighlighted(const QString &completion) | 
					
						
							| 
									
										
										
										
											2014-03-19 18:24:53 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-19 16:30:13 -03:00
										 |  |  | 	QPair<int, int> pos = getCursorTagPosition(); | 
					
						
							| 
									
										
										
										
											2014-04-23 10:06:45 +02:00
										 |  |  | 	setText(text().remove(pos.first, pos.second - pos.first).insert(pos.first, completion)); | 
					
						
							|  |  |  | 	setCursorPosition(pos.first + completion.length()); | 
					
						
							| 
									
										
										
										
											2014-03-19 18:24:53 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TagWidget::setCursorPosition(int position) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	blockSignals(true); | 
					
						
							|  |  |  | 	GroupedLineEdit::setCursorPosition(position); | 
					
						
							|  |  |  | 	blockSignals(false); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | void TagWidget::setText(const QString &text) | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	blockSignals(true); | 
					
						
							|  |  |  | 	GroupedLineEdit::setText(text); | 
					
						
							|  |  |  | 	blockSignals(false); | 
					
						
							|  |  |  | 	highlight(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TagWidget::clear() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	blockSignals(true); | 
					
						
							|  |  |  | 	GroupedLineEdit::clear(); | 
					
						
							|  |  |  | 	blockSignals(false); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TagWidget::keyPressEvent(QKeyEvent *e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-20 17:04:29 +02:00
										 |  |  | 	QPair<int, int> pos; | 
					
						
							|  |  |  | 	QAbstractItemView *popup; | 
					
						
							| 
									
										
										
										
											2014-05-16 15:12:46 +09:00
										 |  |  | 	bool finishedTag = false; | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 	switch (e->key()) { | 
					
						
							| 
									
										
										
										
											2014-03-20 17:04:29 +02:00
										 |  |  | 	case Qt::Key_Escape: | 
					
						
							|  |  |  | 		pos = getCursorTagPosition(); | 
					
						
							|  |  |  | 		if (pos.first >= 0 && pos.second > 0) { | 
					
						
							|  |  |  | 			setText(text().remove(pos.first, pos.second - pos.first)); | 
					
						
							|  |  |  | 			setCursorPosition(pos.first); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | 		popup = m_completer->popup(); | 
					
						
							| 
									
										
										
										
											2014-03-20 17:04:29 +02:00
										 |  |  | 		if (popup) | 
					
						
							|  |  |  | 			popup->hide(); | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 	case Qt::Key_Return: | 
					
						
							|  |  |  | 	case Qt::Key_Enter: | 
					
						
							| 
									
										
										
										
											2013-11-07 13:40:57 +09:00
										 |  |  | 	case Qt::Key_Tab: | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * Fake the QLineEdit behaviour by simply | 
					
						
							|  |  |  | 		 * closing the QAbstractViewitem | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		if (m_completer) { | 
					
						
							| 
									
										
										
										
											2014-03-20 17:04:29 +02:00
										 |  |  | 			popup = m_completer->popup(); | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 			if (popup) | 
					
						
							|  |  |  | 				popup->hide(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-16 15:12:46 +09:00
										 |  |  | 		finishedTag = true; | 
					
						
							| 
									
										
										
										
											2014-07-10 13:19:07 -03:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case Qt::Key_Comma: { /* if this is the last key, and the previous string is empty, ignore the comma. */ | 
					
						
							|  |  |  | 		QString temp = text(); | 
					
						
							|  |  |  | 		if (temp.split(QChar(',')).last().trimmed().isEmpty()){ | 
					
						
							|  |  |  | 			e->ignore(); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	  } | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | 	if (e->key() == Qt::Key_Tab && lastFinishedTag) {		    // if we already end in comma, go to next/prev field
 | 
					
						
							|  |  |  | 		MainWindow::instance()->information()->nextInputField(e);   // by sending the key event to the MainTab widget
 | 
					
						
							| 
									
										
										
										
											2014-05-16 15:12:46 +09:00
										 |  |  | 	} else if (e->key() == Qt::Key_Tab || e->key() == Qt::Key_Return) { // otherwise let's pretend this is a comma instead
 | 
					
						
							| 
									
										
										
										
											2013-12-19 18:56:43 -02:00
										 |  |  | 		QKeyEvent fakeEvent(e->type(), Qt::Key_Comma, e->modifiers(), QString(",")); | 
					
						
							| 
									
										
										
										
											2014-07-10 13:19:58 -03:00
										 |  |  | 		keyPressEvent(&fakeEvent); | 
					
						
							| 
									
										
										
										
											2013-11-07 13:40:57 +09:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		GroupedLineEdit::keyPressEvent(e); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-16 15:12:46 +09:00
										 |  |  | 	lastFinishedTag = finishedTag; | 
					
						
							| 
									
										
										
										
											2013-11-02 17:04:18 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TagWidget::wheelEvent(QWheelEvent *event) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-19 17:33:24 +02:00
										 |  |  | 	if (hasFocus()) { | 
					
						
							|  |  |  | 		GroupedLineEdit::wheelEvent(event); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |