2013-11-02 01:20:02 +00:00
|
|
|
#include "tagwidget.h"
|
|
|
|
#include <QPair>
|
|
|
|
#include <QDebug>
|
2013-11-02 16:04:18 +00:00
|
|
|
#include <QAbstractItemView>
|
2013-12-19 19:16:57 +00:00
|
|
|
#include <QSettings>
|
|
|
|
#include <QFont>
|
2013-11-02 01:20:02 +00:00
|
|
|
|
|
|
|
TagWidget::TagWidget(QWidget *parent) : GroupedLineEdit(parent), m_completer(NULL)
|
|
|
|
{
|
|
|
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(reparse()));
|
2013-11-02 16:04:18 +00:00
|
|
|
connect(this, SIGNAL(textChanged()), this, SLOT(reparse()));
|
2013-11-02 01:20:02 +00:00
|
|
|
|
2013-12-19 19:16:57 +00:00
|
|
|
QColor textColor = palette().color(QPalette::Text);
|
|
|
|
qreal h, s, l, a;
|
|
|
|
textColor.getHslF(&h, &s, &l, &a);
|
|
|
|
// I use dark themes
|
2014-02-28 04:09:57 +00: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));
|
|
|
|
} else if (l <= 0.6) { // moderated dark text. get a somewhat brigth background
|
|
|
|
addColor(QColor(Qt::red).lighter(60));
|
|
|
|
addColor(QColor(Qt::green).lighter(60));
|
|
|
|
addColor(QColor(Qt::blue).lighter(60));
|
2014-01-16 04:50:56 +00:00
|
|
|
} else {
|
2014-02-28 04:09:57 +00:00
|
|
|
addColor(QColor(Qt::red).darker(120));
|
|
|
|
addColor(QColor(Qt::green).darker(120));
|
|
|
|
addColor(QColor(Qt::blue).darker(120));
|
2013-12-19 19:16:57 +00:00
|
|
|
} // light text. get a dark background.
|
2014-01-19 15:33:24 +00:00
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagWidget::setCompleter(QCompleter *completer)
|
|
|
|
{
|
|
|
|
m_completer = completer;
|
|
|
|
m_completer->setWidget(this);
|
|
|
|
connect(m_completer, SIGNAL(activated(QString)), this, SLOT(completionSelected(QString)));
|
2013-11-02 16:04:18 +00:00
|
|
|
connect(m_completer, SIGNAL(highlighted(QString)), this, SLOT(completionSelected(QString)));
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
QPair<int, int> TagWidget::getCursorTagPosition()
|
|
|
|
{
|
2013-11-02 01:20:02 +00:00
|
|
|
int i = 0, start = 0, end = 0;
|
|
|
|
/* Parse string near cursor */
|
|
|
|
i = cursorPosition();
|
|
|
|
while (--i > 0) {
|
|
|
|
if (text().at(i) == ',') {
|
2014-02-28 04:09:57 +00:00
|
|
|
if (i > 0 && text().at(i - 1) != '\\') {
|
2013-11-02 01:20:02 +00:00
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start = i;
|
|
|
|
while (++i < text().length()) {
|
|
|
|
if (text().at(i) == ',') {
|
2014-02-28 04:09:57 +00:00
|
|
|
if (i > 0 && text().at(i - 1) != '\\')
|
2013-11-02 01:20:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end = i;
|
|
|
|
if (start < 0 || end < 0) {
|
|
|
|
start = 0;
|
|
|
|
end = 0;
|
|
|
|
}
|
2014-02-28 04:09:57 +00:00
|
|
|
return qMakePair(start, end);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
enum ParseState {
|
|
|
|
FINDSTART,
|
|
|
|
FINDEND
|
|
|
|
};
|
2013-11-02 01:20:02 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TagWidget::highlight()
|
|
|
|
{
|
2013-11-02 01:20:02 +00:00
|
|
|
int i = 0, start = 0, end = 0;
|
|
|
|
ParseState state = FINDEND;
|
|
|
|
removeAllBlocks();
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
while (i < text().length()) {
|
2013-11-02 01:20:02 +00:00
|
|
|
if (text().at(i) == ',') {
|
|
|
|
if (state == FINDSTART) {
|
|
|
|
/* Detect empty tags */
|
|
|
|
} else if (state == FINDEND) {
|
|
|
|
/* Found end of tag */
|
|
|
|
if (i > 1) {
|
2014-02-28 04:09:57 +00:00
|
|
|
if (text().at(i - 1) != '\\') {
|
2013-11-02 01:20:02 +00:00
|
|
|
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)
|
2014-02-28 04:09:57 +00:00
|
|
|
end = text().length() - 1;
|
2013-11-02 01:20:02 +00:00
|
|
|
if (text().length() > 0)
|
|
|
|
addBlock(start, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TagWidget::reparse()
|
|
|
|
{
|
|
|
|
highlight();
|
2014-02-28 04:09:57 +00:00
|
|
|
QPair<int, int> pos = getCursorTagPosition();
|
2013-11-02 01:20:02 +00:00
|
|
|
QString currentText;
|
|
|
|
if (pos.first >= 0 && pos.second > 0)
|
2014-02-28 04:09:57 +00:00
|
|
|
currentText = text().mid(pos.first, pos.second - pos.first).trimmed();
|
2013-11-02 01:20:02 +00:00
|
|
|
else
|
|
|
|
currentText = "";
|
|
|
|
if (m_completer) {
|
|
|
|
m_completer->setCompletionPrefix(currentText);
|
2013-11-02 16:04:18 +00:00
|
|
|
if (m_completer->completionCount() == 1) {
|
|
|
|
if (m_completer->currentCompletion() == currentText) {
|
|
|
|
QAbstractItemView *popup = m_completer->popup();
|
|
|
|
if (popup)
|
|
|
|
popup->hide();
|
2014-01-16 04:50:56 +00:00
|
|
|
} else {
|
2013-11-02 16:04:18 +00:00
|
|
|
m_completer->complete();
|
2014-01-16 04:50:56 +00:00
|
|
|
}
|
2013-11-02 16:04:18 +00:00
|
|
|
} else {
|
|
|
|
m_completer->complete();
|
|
|
|
}
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TagWidget::completionSelected(QString completion)
|
|
|
|
{
|
|
|
|
QPair<int, int> pos;
|
2013-11-02 01:20:02 +00:00
|
|
|
pos = getCursorTagPosition();
|
|
|
|
if (pos.first >= 0 && pos.second > 0) {
|
2014-02-28 04:09:57 +00:00
|
|
|
setText(text().remove(pos.first, pos.second - pos.first).insert(pos.first, completion));
|
|
|
|
setCursorPosition(pos.first + completion.length());
|
2014-01-16 04:50:56 +00:00
|
|
|
} else {
|
2013-11-02 01:20:02 +00:00
|
|
|
setText(completion.append(", "));
|
|
|
|
setCursorPosition(text().length());
|
|
|
|
}
|
2014-01-15 20:35:15 +00:00
|
|
|
emit(textChanged());
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TagWidget::setCursorPosition(int position)
|
|
|
|
{
|
2013-11-02 01:20:02 +00:00
|
|
|
blockSignals(true);
|
|
|
|
GroupedLineEdit::setCursorPosition(position);
|
|
|
|
blockSignals(false);
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TagWidget::setText(QString text)
|
|
|
|
{
|
2013-11-02 01:20:02 +00:00
|
|
|
blockSignals(true);
|
|
|
|
GroupedLineEdit::setText(text);
|
|
|
|
blockSignals(false);
|
|
|
|
highlight();
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TagWidget::clear()
|
|
|
|
{
|
2013-11-02 01:20:02 +00:00
|
|
|
blockSignals(true);
|
|
|
|
GroupedLineEdit::clear();
|
|
|
|
blockSignals(false);
|
|
|
|
}
|
2013-11-02 16:04:18 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TagWidget::keyPressEvent(QKeyEvent *e)
|
|
|
|
{
|
2013-11-02 16:04:18 +00:00
|
|
|
switch (e->key()) {
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Enter:
|
2013-11-07 04:40:57 +00:00
|
|
|
case Qt::Key_Tab:
|
2013-11-02 16:04:18 +00:00
|
|
|
/*
|
|
|
|
* Fake the QLineEdit behaviour by simply
|
|
|
|
* closing the QAbstractViewitem
|
|
|
|
*/
|
|
|
|
if (m_completer) {
|
|
|
|
QAbstractItemView *popup = m_completer->popup();
|
|
|
|
if (popup)
|
|
|
|
popup->hide();
|
|
|
|
}
|
|
|
|
}
|
2013-11-07 04:40:57 +00:00
|
|
|
if (e->key() == Qt::Key_Tab) { // let's pretend this is a comma instead
|
2013-12-19 20:56:43 +00:00
|
|
|
QKeyEvent fakeEvent(e->type(), Qt::Key_Comma, e->modifiers(), QString(","));
|
|
|
|
GroupedLineEdit::keyPressEvent(&fakeEvent);
|
2013-11-07 04:40:57 +00:00
|
|
|
} else {
|
|
|
|
GroupedLineEdit::keyPressEvent(e);
|
|
|
|
}
|
2013-11-02 16:04:18 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void TagWidget::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
2014-01-19 15:33:24 +00:00
|
|
|
if (hasFocus()) {
|
|
|
|
GroupedLineEdit::wheelEvent(event);
|
|
|
|
}
|
|
|
|
}
|