2
Copyright (C) 2012 Othmane Moustaouda <othmane.moustaouda@gmail.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either
7
version 2.1 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public
15
License along with this library; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
#include "nowplaying-lineedit.h"
23
#include <QMouseEvent>
26
NowPlayingLineEdit::NowPlayingLineEdit(QWidget *parent)
32
NowPlayingLineEdit::~NowPlayingLineEdit()
37
void NowPlayingLineEdit::setLocalizedTagNames(QStringList tagNames)
39
m_localizedTagNames = tagNames;
42
void NowPlayingLineEdit::dropEvent(QDropEvent *event)
44
const QMimeData *content = event->mimeData();
46
//adapt the dropped text and insert it at current cursor position
47
if (content->hasText()) {
48
QString text = content->text();
49
text = text.toLower();
50
text = text.insert(0, QLatin1Char('%'));
59
void NowPlayingLineEdit::mousePressEvent(QMouseEvent *event)
61
//small usability improvement:
62
//if we detect that the click is inside a tag name then that tag will be auto selected
63
if (event->button() == Qt::LeftButton) {
64
int currentCursorPosition = cursorPositionAt(event->pos());
66
Q_FOREACH (const QString &tag, m_localizedTagNames) {
67
if(text().contains(tag)
68
&& currentCursorPosition >= text().indexOf(tag) //cursor must be inside tag's bounds
69
&& currentCursorPosition <= text().indexOf(tag) + tag.size()) {
71
setSelection(text().indexOf(tag), tag.size());
75
//since we are intercepting mouse events, setting manually the cursor's position is needed
76
setCursorPosition(currentCursorPosition);