~ubuntu-branches/ubuntu/trusty/ktp-kded-integration-module/trusty-proposed

« back to all changes in this revision

Viewing changes to config/nowplaying-lineedit.cpp

  • Committer: Package Import Robot
  • Author(s): George Kiagiadakis
  • Date: 2012-06-19 12:23:53 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120619122353-yx283wesj3dmuh10
Tags: 0.4.0-0ubuntu1
Upload to Kubuntu

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2012 Othmane Moustaouda <othmane.moustaouda@gmail.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
*/
 
18
 
 
19
#include "nowplaying-lineedit.h"
 
20
 
 
21
#include <QLineEdit>
 
22
#include <QMimeData>
 
23
#include <QMouseEvent>
 
24
#include <QDropEvent>
 
25
 
 
26
NowPlayingLineEdit::NowPlayingLineEdit(QWidget *parent)
 
27
: QLineEdit(parent)
 
28
{
 
29
 
 
30
}
 
31
 
 
32
NowPlayingLineEdit::~NowPlayingLineEdit()
 
33
{
 
34
 
 
35
}
 
36
 
 
37
void NowPlayingLineEdit::setLocalizedTagNames(QStringList tagNames)
 
38
{
 
39
    m_localizedTagNames = tagNames;
 
40
}
 
41
 
 
42
void NowPlayingLineEdit::dropEvent(QDropEvent *event)
 
43
{
 
44
    const QMimeData *content = event->mimeData();
 
45
 
 
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('%'));
 
51
        insert(text);
 
52
 
 
53
        setFocus();
 
54
        event->accept();
 
55
    } else
 
56
        event->ignore();
 
57
}
 
58
 
 
59
void NowPlayingLineEdit::mousePressEvent(QMouseEvent *event)
 
60
{
 
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());
 
65
 
 
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()) {
 
70
 
 
71
                setSelection(text().indexOf(tag), tag.size());
 
72
                break;
 
73
            }
 
74
            else {
 
75
                //since we are intercepting mouse events, setting manually the cursor's position is needed
 
76
                setCursorPosition(currentCursorPosition);
 
77
            }
 
78
        }
 
79
    }
 
80
}