mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-01-18 23:17:59 +01:00
* QueryLabel now supports dragging.
* Needs support for dragging Artist / Album.
This commit is contained in:
parent
81c4267263
commit
6ebafa26a6
@ -4,6 +4,7 @@
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
|
||||
Query::Query( const QVariant& v )
|
||||
: m_v( v )
|
||||
, m_solved( false )
|
||||
@ -40,7 +41,8 @@ Query::addResults( const QList< Tomahawk::result_ptr >& newresults )
|
||||
}
|
||||
}
|
||||
emit resultsAdded( newresults );
|
||||
if( becameSolved ) emit solvedStateChanged( true );
|
||||
if( becameSolved )
|
||||
emit solvedStateChanged( true );
|
||||
}
|
||||
|
||||
|
||||
@ -97,7 +99,8 @@ Query::numResults() const
|
||||
}
|
||||
|
||||
|
||||
QID Query::id() const
|
||||
QID
|
||||
Query::id() const
|
||||
{
|
||||
if ( m_qid.isEmpty() )
|
||||
{
|
||||
@ -112,7 +115,8 @@ QID Query::id() const
|
||||
}
|
||||
|
||||
|
||||
bool Query::resultSorter( const result_ptr& left, const result_ptr& right )
|
||||
bool
|
||||
Query::resultSorter( const result_ptr& left, const result_ptr& right )
|
||||
{
|
||||
return left->score() > right->score();
|
||||
}
|
||||
|
@ -56,6 +56,14 @@ Result::toString() const
|
||||
}
|
||||
|
||||
|
||||
Tomahawk::query_ptr
|
||||
Result::toQuery() const
|
||||
{
|
||||
Tomahawk::query_ptr query = Tomahawk::query_ptr( new Tomahawk::Query( toVariant() ) );
|
||||
return query;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Result::updateAttributes()
|
||||
{
|
||||
|
@ -19,7 +19,10 @@ Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Result( const QVariant& v, const collection_ptr& collection );
|
||||
|
||||
QVariant toVariant() const { return m_v; }
|
||||
QString toString() const;
|
||||
Tomahawk::query_ptr toQuery() const;
|
||||
|
||||
float score() const;
|
||||
RID id() const;
|
||||
@ -44,9 +47,6 @@ public:
|
||||
|
||||
unsigned int dbid() const { return m_id; }
|
||||
|
||||
// for debug output:
|
||||
QString toString() const;
|
||||
|
||||
signals:
|
||||
// emitted when the collection this result comes from is going offline:
|
||||
void becomingUnavailable();
|
||||
|
@ -239,7 +239,9 @@ PlaylistModel::dropMimeData( const QMimeData* data, Qt::DropAction action, int r
|
||||
if ( action == Qt::IgnoreAction || isReadOnly() )
|
||||
return true;
|
||||
|
||||
if ( !data->hasFormat( "application/tomahawk.query.list" ) && !data->hasFormat( "application/tomahawk.plentry.list" ) )
|
||||
if ( !data->hasFormat( "application/tomahawk.query.list" )
|
||||
&& !data->hasFormat( "application/tomahawk.plentry.list" )
|
||||
&& !data->hasFormat( "application/tomahawk.result.list" ) )
|
||||
return false;
|
||||
|
||||
int beginRow;
|
||||
|
@ -161,7 +161,9 @@ TrackView::dragEnterEvent( QDragEnterEvent* event )
|
||||
qDebug() << Q_FUNC_INFO;
|
||||
QTreeView::dragEnterEvent( event );
|
||||
|
||||
if ( event->mimeData()->hasFormat( "application/tomahawk.query.list" ) || event->mimeData()->hasFormat( "application/tomahawk.plentry.list" ) )
|
||||
if ( event->mimeData()->hasFormat( "application/tomahawk.query.list" )
|
||||
|| event->mimeData()->hasFormat( "application/tomahawk.plentry.list" )
|
||||
|| event->mimeData()->hasFormat( "application/tomahawk.result.list" ) )
|
||||
{
|
||||
m_dragging = true;
|
||||
m_dropRect = QRect();
|
||||
@ -183,7 +185,9 @@ TrackView::dragMoveEvent( QDragMoveEvent* event )
|
||||
return;
|
||||
}
|
||||
|
||||
if ( event->mimeData()->hasFormat( "application/tomahawk.query.list" ) || event->mimeData()->hasFormat( "application/tomahawk.plentry.list" ) )
|
||||
if ( event->mimeData()->hasFormat( "application/tomahawk.query.list" )
|
||||
|| event->mimeData()->hasFormat( "application/tomahawk.plentry.list" )
|
||||
|| event->mimeData()->hasFormat( "application/tomahawk.result.list" ) )
|
||||
{
|
||||
setDirtyRegion( m_dropRect );
|
||||
const QPoint pos = event->pos();
|
||||
@ -216,6 +220,7 @@ TrackView::dropEvent( QDropEvent* event )
|
||||
qDebug() << "Ignoring accepted event!";
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( event->mimeData()->hasFormat( "application/tomahawk.query.list" ) )
|
||||
{
|
||||
const QPoint pos = event->pos();
|
||||
@ -229,6 +234,7 @@ TrackView::dropEvent( QDropEvent* event )
|
||||
model()->dropMimeData( event->mimeData(), event->proposedAction(), index.row(), 0, index.parent() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_dragging = false;
|
||||
}
|
||||
@ -298,7 +304,7 @@ TrackView::startDrag( Qt::DropActions supportedActions )
|
||||
|
||||
QDrag* drag = new QDrag( this );
|
||||
drag->setMimeData( data );
|
||||
const QPixmap p = createDragPixmap( indexes.count() );
|
||||
const QPixmap p = TomahawkUtils::createDragPixmap( indexes.count() );
|
||||
drag->setPixmap( p );
|
||||
drag->setHotSpot( QPoint( -20, -20 ) );
|
||||
|
||||
@ -308,65 +314,3 @@ TrackView::startDrag( Qt::DropActions supportedActions )
|
||||
m_proxyModel->removeIndexes( pindexes );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Inspired from dolphin's draganddrophelper.cpp
|
||||
QPixmap
|
||||
TrackView::createDragPixmap( int itemCount ) const
|
||||
{
|
||||
// If more than one item is dragged, align the items inside a
|
||||
// rectangular grid. The maximum grid size is limited to 5 x 5 items.
|
||||
int xCount = 3;
|
||||
int size = 32;
|
||||
|
||||
if ( itemCount > 16 )
|
||||
{
|
||||
xCount = 5;
|
||||
size = 16;
|
||||
} else if( itemCount > 9 )
|
||||
{
|
||||
xCount = 4;
|
||||
size = 22;
|
||||
}
|
||||
|
||||
if( itemCount < xCount )
|
||||
{
|
||||
xCount = itemCount;
|
||||
}
|
||||
|
||||
int yCount = itemCount / xCount;
|
||||
if( itemCount % xCount != 0 )
|
||||
{
|
||||
++yCount;
|
||||
}
|
||||
if( yCount > xCount )
|
||||
{
|
||||
yCount = xCount;
|
||||
}
|
||||
// Draw the selected items into the grid cells
|
||||
QPixmap dragPixmap( xCount * size + xCount - 1, yCount * size + yCount - 1 );
|
||||
dragPixmap.fill( Qt::transparent );
|
||||
|
||||
QPainter painter( &dragPixmap );
|
||||
painter.setRenderHint( QPainter::Antialiasing );
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
for( int i = 0; i < itemCount; ++i )
|
||||
{
|
||||
const QPixmap pixmap = QPixmap( QString( ":/data/icons/audio-x-generic-%2x%2.png" ).arg( size ) );
|
||||
painter.drawPixmap( x, y, pixmap );
|
||||
|
||||
x += size + 1;
|
||||
if ( x >= dragPixmap.width() )
|
||||
{
|
||||
x = 0;
|
||||
y += size + 1;
|
||||
}
|
||||
if ( y >= dragPixmap.height() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return dragPixmap;
|
||||
}
|
||||
|
@ -55,8 +55,6 @@ private slots:
|
||||
void onFilterChanged( const QString& filter );
|
||||
|
||||
private:
|
||||
QPixmap createDragPixmap( int itemCount ) const;
|
||||
|
||||
TrackModel* m_model;
|
||||
TrackProxyModel* m_proxyModel;
|
||||
PlaylistItemDelegate* m_delegate;
|
||||
|
@ -6,6 +6,10 @@
|
||||
#include <QMouseEvent>
|
||||
#include <QPainter>
|
||||
|
||||
#include "tomahawkutils.h"
|
||||
|
||||
#define BOXMARGIN 2
|
||||
|
||||
|
||||
QueryLabel::QueryLabel( QWidget* parent, Qt::WindowFlags flags )
|
||||
: QFrame( parent, flags )
|
||||
@ -161,12 +165,17 @@ QueryLabel::setResult( const Tomahawk::result_ptr& result )
|
||||
if ( result.isNull() )
|
||||
return;
|
||||
|
||||
setContentsMargins( 2, 0, 2, 0 );
|
||||
setContentsMargins( BOXMARGIN, 0, BOXMARGIN, 0 );
|
||||
|
||||
if ( m_result.isNull() || m_result.data() != result.data() )
|
||||
{
|
||||
m_result = result;
|
||||
m_query.clear();
|
||||
|
||||
m_query = m_result->toQuery();
|
||||
QList<Tomahawk::result_ptr> rl;
|
||||
rl << m_result;
|
||||
m_query->addResults( rl );
|
||||
|
||||
updateLabel();
|
||||
|
||||
emit textChanged( text() );
|
||||
@ -181,7 +190,7 @@ QueryLabel::setQuery( const Tomahawk::query_ptr& query )
|
||||
if ( query.isNull() )
|
||||
return;
|
||||
|
||||
setContentsMargins( 2, 0, 2, 0 );
|
||||
setContentsMargins( BOXMARGIN, 0, BOXMARGIN, 0 );
|
||||
|
||||
if ( m_query.isNull() || m_query.data() != query.data() )
|
||||
{
|
||||
@ -388,6 +397,8 @@ void
|
||||
QueryLabel::mouseReleaseEvent( QMouseEvent* event )
|
||||
{
|
||||
QFrame::mouseReleaseEvent( event );
|
||||
|
||||
m_dragPos = QPoint();
|
||||
if ( time.elapsed() < qApp->doubleClickInterval() )
|
||||
emit clicked();
|
||||
}
|
||||
@ -399,6 +410,14 @@ QueryLabel::mouseMoveEvent( QMouseEvent* event )
|
||||
QFrame::mouseMoveEvent( event );
|
||||
int x = event->x();
|
||||
|
||||
if ( event->buttons() & Qt::LeftButton &&
|
||||
( m_dragPos - event->pos() ).manhattanLength() >= QApplication::startDragDistance() )
|
||||
{
|
||||
startDrag();
|
||||
leaveEvent( 0 );
|
||||
return;
|
||||
}
|
||||
|
||||
const QFontMetrics& fm = fontMetrics();
|
||||
int dashX = fm.width( " - " );
|
||||
int artistX = m_type & Artist ? fm.width( artist() ) : 0;
|
||||
@ -455,6 +474,31 @@ QueryLabel::leaveEvent( QEvent* event )
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
QueryLabel::startDrag()
|
||||
{
|
||||
if ( m_query.isNull() )
|
||||
return;
|
||||
|
||||
QByteArray queryData;
|
||||
QDataStream queryStream( &queryData, QIODevice::WriteOnly );
|
||||
QMimeData* mimeData = new QMimeData();
|
||||
mimeData->setText( text() );
|
||||
|
||||
queryStream << qlonglong( &m_query );
|
||||
|
||||
mimeData->setData( "application/tomahawk.query.list", queryData );
|
||||
QDrag *drag = new QDrag( this );
|
||||
drag->setMimeData( mimeData );
|
||||
drag->setPixmap( TomahawkUtils::createDragPixmap() );
|
||||
|
||||
// QPoint hotSpot = event->pos() - child->pos();
|
||||
// drag->setHotSpot( hotSpot );
|
||||
|
||||
drag->exec( Qt::CopyAction );
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
QueryLabel::smartAppend( QString& text, const QString& appendage ) const
|
||||
{
|
||||
|
@ -71,6 +71,8 @@ protected:
|
||||
|
||||
virtual void changeEvent( QEvent* event );
|
||||
virtual void paintEvent( QPaintEvent* event );
|
||||
|
||||
virtual void startDrag();
|
||||
|
||||
private:
|
||||
QString smartAppend( QString& text, const QString& appendage ) const;
|
||||
@ -85,6 +87,7 @@ private:
|
||||
Qt::TextElideMode mode;
|
||||
|
||||
QRect m_hoverArea;
|
||||
QPoint m_dragPos;
|
||||
};
|
||||
|
||||
#endif // QUERYLABEL_H
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
@ -210,4 +212,65 @@ filesizeToString( unsigned int size )
|
||||
return QString::number( size );
|
||||
}
|
||||
|
||||
|
||||
QPixmap
|
||||
createDragPixmap( int itemCount )
|
||||
{
|
||||
// If more than one item is dragged, align the items inside a
|
||||
// rectangular grid. The maximum grid size is limited to 5 x 5 items.
|
||||
int xCount = 3;
|
||||
int size = 32;
|
||||
|
||||
if ( itemCount > 16 )
|
||||
{
|
||||
xCount = 5;
|
||||
size = 16;
|
||||
} else if( itemCount > 9 )
|
||||
{
|
||||
xCount = 4;
|
||||
size = 22;
|
||||
}
|
||||
|
||||
if( itemCount < xCount )
|
||||
{
|
||||
xCount = itemCount;
|
||||
}
|
||||
|
||||
int yCount = itemCount / xCount;
|
||||
if( itemCount % xCount != 0 )
|
||||
{
|
||||
++yCount;
|
||||
}
|
||||
if( yCount > xCount )
|
||||
{
|
||||
yCount = xCount;
|
||||
}
|
||||
// Draw the selected items into the grid cells
|
||||
QPixmap dragPixmap( xCount * size + xCount - 1, yCount * size + yCount - 1 );
|
||||
dragPixmap.fill( Qt::transparent );
|
||||
|
||||
QPainter painter( &dragPixmap );
|
||||
painter.setRenderHint( QPainter::Antialiasing );
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
for( int i = 0; i < itemCount; ++i )
|
||||
{
|
||||
const QPixmap pixmap = QPixmap( QString( ":/data/icons/audio-x-generic-%2x%2.png" ).arg( size ) );
|
||||
painter.drawPixmap( x, y, pixmap );
|
||||
|
||||
x += size + 1;
|
||||
if ( x >= dragPixmap.width() )
|
||||
{
|
||||
x = 0;
|
||||
y += size + 1;
|
||||
}
|
||||
if ( y >= dragPixmap.height() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return dragPixmap;
|
||||
}
|
||||
|
||||
} // ns
|
||||
|
@ -4,6 +4,7 @@
|
||||
class QDir;
|
||||
class QDateTime;
|
||||
class QString;
|
||||
class QPixmap;
|
||||
|
||||
namespace TomahawkUtils
|
||||
{
|
||||
@ -13,6 +14,8 @@ namespace TomahawkUtils
|
||||
QString timeToString( int seconds );
|
||||
QString ageToString( const QDateTime& time );
|
||||
QString filesizeToString( unsigned int size );
|
||||
|
||||
QPixmap createDragPixmap( int itemCount = 1 );
|
||||
}
|
||||
|
||||
#endif // TOMAHAWKUTILS_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user