File indexing completed on 2024-05-05 04:49:25

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Casey Link <unnamedrambler@gmail.com>                             *
0003  * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org                             *
0004  * Copyright (c) 2009 Simon Bühler <simon@aktionspotenzial.de>                          *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) any later           *
0009  * version.                                                                             *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #include "BookmarkTriangle.h"
0020 
0021 #include "EngineController.h"
0022 #include "MainWindow.h"
0023 #include "SvgHandler.h"
0024 #include "amarokurls/BookmarkModel.h"
0025 #include "amarokurls/PlayUrlGenerator.h"
0026 #include "core/meta/Meta.h"
0027 #include "core/meta/support/MetaUtility.h"
0028 #include "core/support/Debug.h"
0029 
0030 #include <KLocalizedString>
0031 
0032 #include <QMenu>
0033 #include <QPainter>
0034 #include <QSize>
0035 #include <QSizePolicy>
0036 
0037 BookmarkTriangle::BookmarkTriangle (QWidget *parent, int milliseconds, const QString &name,
0038                                      int sliderwidth, bool showPopup )
0039     : QWidget ( parent ),
0040     m_mseconds ( milliseconds ),
0041     m_name ( name ),
0042     m_sliderwidth ( sliderwidth ),
0043     m_showPopup ( showPopup ),
0044     m_tooltip ( nullptr )
0045 {
0046 }
0047 
0048 BookmarkTriangle::~BookmarkTriangle()
0049 {
0050     DEBUG_BLOCK
0051     if (m_tooltip)
0052       m_tooltip->deleteLater();
0053 }
0054 
0055 QSize BookmarkTriangle::sizeHint() const
0056 {
0057     return QSize ( 10, 10 );
0058 }
0059 
0060 QSizePolicy BookmarkTriangle::sizePolicy() const
0061 {
0062     return QSizePolicy ( QSizePolicy::Fixed, QSizePolicy::Fixed );
0063 }
0064 
0065 QSize BookmarkTriangle::minimumSizeHint() const
0066 {
0067     return QSize ( 10, 10 );
0068 }
0069 
0070 int BookmarkTriangle::getTimeValue()
0071 {
0072     return m_mseconds;
0073 }
0074 
0075 void BookmarkTriangle::paintEvent ( QPaintEvent* )
0076 {
0077     QPainter p ( this );
0078     p.drawPixmap ( 0, 0, The::svgHandler()->renderSvg ( "blue_triangle", 10 , 10, "blue_triangle" ) ); // TODO: This doesn't work
0079 }
0080 
0081 void BookmarkTriangle::showEvent ( QShowEvent * event )
0082 {
0083     Q_UNUSED( event );  //FIXME: event->accept() should probably be called
0084 
0085     if ( m_showPopup )
0086     {
0087         m_showPopup = false; // Force immediate Popup Display after editing
0088         initPopup();
0089     }
0090 }
0091 
0092 void BookmarkTriangle::mousePressEvent ( QMouseEvent * event )
0093 {
0094     event->accept();
0095     m_offset = event->pos();
0096     m_pos = this->x();
0097 }
0098 
0099 void BookmarkTriangle::mouseMoveEvent ( QMouseEvent * event )
0100 {
0101     event->accept();
0102     int distance_x = event->x() - m_offset.x();
0103     QPoint pt(distance_x, 0);
0104     move(mapToParent( pt ));
0105 }
0106 
0107 void BookmarkTriangle::mouseReleaseEvent ( QMouseEvent * event )
0108 {
0109     event->accept();
0110 
0111     if( this->x() == m_pos ){
0112         Q_EMIT clicked ( m_mseconds );
0113     }
0114     else
0115     {
0116         if( this->x() < 0 || this->x() > m_sliderwidth )
0117         {
0118             this->setGeometry(m_pos, 1, 11, 11);
0119             this->update();
0120         }
0121         else{
0122             qreal percentage = (qreal) ( this->x() ) / (qreal) m_sliderwidth;
0123             long trackLength = The::engineController()->trackLength();
0124             qint64 trackPosition = trackLength * percentage;
0125             moveBookmark( trackPosition, m_name );
0126         }
0127     }
0128 }
0129 
0130 void BookmarkTriangle::moveBookmark ( qint64 newMilliseconds, const QString &name )
0131 {
0132     hidePopup();
0133     Meta::TrackPtr track = The::engineController()->currentTrack();
0134     PlayUrlGenerator::instance()->moveTrackBookmark( track, newMilliseconds, name );
0135 }
0136 
0137 void BookmarkTriangle::deleteBookmark ()
0138 {
0139     DEBUG_BLOCK
0140 
0141     debug() << "Name: " << m_name;
0142     hidePopup();
0143     BookmarkModel::instance()->deleteBookmark ( m_name );
0144 
0145 }
0146 
0147 void BookmarkTriangle::enterEvent ( QEvent * event )
0148 {
0149     DEBUG_BLOCK
0150     Q_UNUSED ( event )
0151 
0152     Q_EMIT focused ( m_mseconds );
0153     initPopup();
0154 }
0155 
0156 void BookmarkTriangle::leaveEvent ( QEvent * event )
0157 {
0158     DEBUG_BLOCK
0159     Q_UNUSED ( event )
0160     if (m_tooltip)
0161         m_tooltip->displayNeeded(false);
0162 }
0163 
0164 void BookmarkTriangle::initPopup()
0165 {
0166     if ( !m_tooltip )  m_tooltip = new BookmarkPopup ( The::mainWindow(), m_name , this );
0167     // Keep existing tooltip alive
0168     m_tooltip->displayNeeded(true);
0169 
0170     QPoint pt = mapTo ( The::mainWindow(), QPoint ( 0, 0 ) );
0171     // Calculate x position where the tooltip is fully visible
0172     int offsetX = pt.x() + m_tooltip->width() - The::mainWindow()->width();
0173     if ( offsetX < 0 ) offsetX = 0;
0174     // Calculate y position above
0175     int offsetY =  - m_tooltip->height() - 2;
0176     // Not enough space? put it below
0177     if ( pt.y() <= m_tooltip->height() + 2 ) offsetY =  this->height() + 2;
0178     m_tooltip->move ( pt.x() - offsetX, pt.y() + offsetY );
0179 
0180     m_tooltip->show();
0181 }
0182 
0183 void BookmarkTriangle::hidePopup()
0184 {
0185     if ( m_tooltip )  m_tooltip->hide();
0186 }
0187