1 /****************************************************************************************
2  * Copyright (c) 2009 Casey Link <unnamedrambler@gmail.com>                             *
3  * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org                             *
4  * Copyright (c) 2009 Simon Bühler <simon@aktionspotenzial.de>                          *
5  *                                                                                      *
6  * This program is free software; you can redistribute it and/or modify it under        *
7  * the terms of the GNU General Public License as published by the Free Software        *
8  * Foundation; either version 2 of the License, or (at your option) any later           *
9  * version.                                                                             *
10  *                                                                                      *
11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
13  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
14  *                                                                                      *
15  * You should have received a copy of the GNU General Public License along with         *
16  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
17  ****************************************************************************************/
18 
19 #include "BookmarkTriangle.h"
20 
21 #include "EngineController.h"
22 #include "MainWindow.h"
23 #include "SvgHandler.h"
24 #include "amarokurls/BookmarkModel.h"
25 #include "amarokurls/PlayUrlGenerator.h"
26 #include "core/meta/Meta.h"
27 #include "core/meta/support/MetaUtility.h"
28 #include "core/support/Debug.h"
29 
30 #include <KLocalizedString>
31 
32 #include <QMenu>
33 #include <QPainter>
34 #include <QSize>
35 #include <QSizePolicy>
36 
BookmarkTriangle(QWidget * parent,int milliseconds,const QString & name,int sliderwidth,bool showPopup)37 BookmarkTriangle::BookmarkTriangle (QWidget *parent, int milliseconds, const QString &name,
38                                      int sliderwidth, bool showPopup )
39     : QWidget ( parent ),
40     m_mseconds ( milliseconds ),
41     m_name ( name ),
42     m_sliderwidth ( sliderwidth ),
43     m_showPopup ( showPopup ),
44     m_tooltip ( 0 )
45 {
46 }
47 
~BookmarkTriangle()48 BookmarkTriangle::~BookmarkTriangle()
49 {
50     DEBUG_BLOCK
51     if (m_tooltip)
52       m_tooltip->deleteLater();
53 }
54 
sizeHint() const55 QSize BookmarkTriangle::sizeHint() const
56 {
57     return QSize ( 10, 10 );
58 }
59 
sizePolicy() const60 QSizePolicy BookmarkTriangle::sizePolicy() const
61 {
62     return QSizePolicy ( QSizePolicy::Fixed, QSizePolicy::Fixed );
63 }
64 
minimumSizeHint() const65 QSize BookmarkTriangle::minimumSizeHint() const
66 {
67     return QSize ( 10, 10 );
68 }
69 
getTimeValue()70 int BookmarkTriangle::getTimeValue()
71 {
72     return m_mseconds;
73 }
74 
paintEvent(QPaintEvent *)75 void BookmarkTriangle::paintEvent ( QPaintEvent* )
76 {
77     QPainter p ( this );
78     p.drawPixmap ( 0, 0, The::svgHandler()->renderSvg ( "blue_triangle", 10 , 10, "blue_triangle" ) ); // TODO: This doesn't work
79 }
80 
showEvent(QShowEvent * event)81 void BookmarkTriangle::showEvent ( QShowEvent * event )
82 {
83     Q_UNUSED( event );  //FIXME: event->accept() should probably be called
84 
85     if ( m_showPopup )
86     {
87         m_showPopup = false; // Force immediate Popup Display after editing
88         initPopup();
89     }
90 }
91 
mousePressEvent(QMouseEvent * event)92 void BookmarkTriangle::mousePressEvent ( QMouseEvent * event )
93 {
94     event->accept();
95     m_offset = event->pos();
96     m_pos = this->x();
97 }
98 
mouseMoveEvent(QMouseEvent * event)99 void BookmarkTriangle::mouseMoveEvent ( QMouseEvent * event )
100 {
101     event->accept();
102     int distance_x = event->x() - m_offset.x();
103     QPoint pt(distance_x, 0);
104     move(mapToParent( pt ));
105 }
106 
mouseReleaseEvent(QMouseEvent * event)107 void BookmarkTriangle::mouseReleaseEvent ( QMouseEvent * event )
108 {
109     event->accept();
110 
111     if( this->x() == m_pos ){
112         Q_EMIT clicked ( m_mseconds );
113     }
114     else
115     {
116         if( this->x() < 0 || this->x() > m_sliderwidth )
117         {
118             this->setGeometry(m_pos, 1, 11, 11);
119             this->update();
120         }
121         else{
122             qreal percentage = (qreal) ( this->x() ) / (qreal) m_sliderwidth;
123             long trackLength = The::engineController()->trackLength();
124             qint64 trackPosition = trackLength * percentage;
125             moveBookmark( trackPosition, m_name );
126         }
127     }
128 }
129 
moveBookmark(qint64 newMilliseconds,const QString & name)130 void BookmarkTriangle::moveBookmark ( qint64 newMilliseconds, const QString &name )
131 {
132     hidePopup();
133     Meta::TrackPtr track = The::engineController()->currentTrack();
134     PlayUrlGenerator::instance()->moveTrackBookmark( track, newMilliseconds, name );
135 }
136 
deleteBookmark()137 void BookmarkTriangle::deleteBookmark ()
138 {
139     DEBUG_BLOCK
140 
141     debug() << "Name: " << m_name;
142     hidePopup();
143     BookmarkModel::instance()->deleteBookmark ( m_name );
144 
145 }
146 
enterEvent(QEvent * event)147 void BookmarkTriangle::enterEvent ( QEvent * event )
148 {
149     DEBUG_BLOCK
150     Q_UNUSED ( event )
151 
152     Q_EMIT focused ( m_mseconds );
153     initPopup();
154 }
155 
leaveEvent(QEvent * event)156 void BookmarkTriangle::leaveEvent ( QEvent * event )
157 {
158     DEBUG_BLOCK
159     Q_UNUSED ( event )
160     if (m_tooltip)
161         m_tooltip->displayNeeded(false);
162 }
163 
initPopup()164 void BookmarkTriangle::initPopup()
165 {
166     if ( !m_tooltip )  m_tooltip = new BookmarkPopup ( The::mainWindow(), m_name , this );
167     // Keep existing tooltip alive
168     m_tooltip->displayNeeded(true);
169 
170     QPoint pt = mapTo ( The::mainWindow(), QPoint ( 0, 0 ) );
171     // Calculate x position where the tooltip is fully visible
172     int offsetX = pt.x() + m_tooltip->width() - The::mainWindow()->width();
173     if ( offsetX < 0 ) offsetX = 0;
174     // Calculate y position above
175     int offsetY =  - m_tooltip->height() - 2;
176     // Not enough space? put it below
177     if ( pt.y() <= m_tooltip->height() + 2 ) offsetY =  this->height() + 2;
178     m_tooltip->move ( pt.x() - offsetX, pt.y() + offsetY );
179 
180     m_tooltip->show();
181 }
182 
hidePopup()183 void BookmarkTriangle::hidePopup()
184 {
185     if ( m_tooltip )  m_tooltip->hide();
186 }
187 
188