1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-07-31 19:30:21 +02:00

* Save / restore playlist column state.

* Pixel-wise scrolling for our item views.
This commit is contained in:
Christian Muehlhaeuser
2010-12-02 06:40:58 +01:00
parent 77400f543c
commit b67cc0fcdf
6 changed files with 182 additions and 113 deletions

View File

@@ -29,10 +29,10 @@ AlbumView::AlbumView( QWidget* parent )
setDragDropOverwriteMode( false );
setUniformItemSizes( true );
setSpacing( 20 );
setWordWrap( true );
setResizeMode( Adjust );
setViewMode( IconMode );
setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
// setIconSize( QSize( 64, 64 ) );
setProxyModel( new AlbumProxyModel( this ) );

View File

@@ -1,7 +1,6 @@
#include "trackview.h"
#include <QDebug>
#include <QHeaderView>
#include <QKeyEvent>
#include <QPainter>
#include <QScrollBar>
@@ -22,6 +21,7 @@ TrackView::TrackView( QWidget* parent )
, m_model( 0 )
, m_proxyModel( 0 )
, m_delegate( 0 )
, m_header( new TrackHeader( this ) )
, m_resizing( false )
{
setSortingEnabled( false );
@@ -33,27 +33,24 @@ TrackView::TrackView( QWidget* parent )
setDragDropMode( QAbstractItemView::InternalMove );
setDragDropOverwriteMode( false );
setAllColumnsShowFocus( true );
setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
setMinimumWidth( 690 );
setHeader( m_header );
#ifndef Q_WS_WIN
QFont f = font();
f.setPointSize( f.pointSize() - 1 );
setFont( f );
#endif
header()->setMinimumSectionSize( 60 );
restoreColumnsState();
connect( this, SIGNAL( doubleClicked( QModelIndex ) ), SLOT( onItemActivated( QModelIndex ) ) );
connect( header(), SIGNAL( sectionResized( int, int, int ) ), SLOT( onSectionResized( int, int, int ) ) );
}
TrackView::~TrackView()
{
qDebug() << Q_FUNC_INFO;
saveColumnsState();
}
@@ -86,51 +83,6 @@ TrackView::setModel( TrackModel* model )
}
void
TrackView::restoreColumnsState()
{
TomahawkSettings* s = APP->settings();
QList<QVariant> list = s->playlistColumnSizes();
if ( list.count() != 7 ) // FIXME: const
{
m_columnWeights << 0.19 << 0.24 << 0.18 << 0.07 << 0.07 << 0.11 << 0.14;
}
else
{
foreach( const QVariant& v, list )
m_columnWeights << v.toDouble();
}
for ( int i = 0; i < m_columnWeights.count(); i++ )
m_columnWidths << 0;
}
void
TrackView::saveColumnsState()
{
TomahawkSettings *s = APP->settings();
QList<QVariant> wlist;
// int i = 0;
foreach( double w, m_columnWeights )
{
wlist << QVariant( w );
// qDebug() << "Storing weight for column" << i++ << w;
}
s->setPlaylistColumnSizes( wlist );
}
void
TrackView::onSectionResized( int logicalIndex, int oldSize, int newSize )
{
return;
}
void
TrackView::onItemActivated( const QModelIndex& index )
{
@@ -196,43 +148,8 @@ TrackView::addItemsToQueue()
void
TrackView::resizeEvent( QResizeEvent* event )
{
// qDebug() << Q_FUNC_INFO;
resizeColumns();
}
void
TrackView::resizeColumns()
{
double cw = contentsRect().width();
int i = 0;
int total = 0;
QList<double> mcw = m_columnWeights;
if ( verticalScrollBar() && verticalScrollBar()->isVisible() )
{
cw -= verticalScrollBar()->width() + 1;
}
m_resizing = true;
foreach( double w, mcw )
{
int fw = (int)( cw * w );
if ( fw < header()->minimumSectionSize() )
fw = header()->minimumSectionSize();
if ( i + 1 == header()->count() )
fw = cw - total;
total += fw;
// qDebug() << "Resizing column:" << i << fw;
m_columnWidths[ i ] = fw;
header()->resizeSection( i++, fw );
}
m_resizing = false;
qDebug() << Q_FUNC_INFO;
m_header->onResized();
}
@@ -451,3 +368,137 @@ TrackView::createDragPixmap( int itemCount ) const
return dragPixmap;
}
TrackHeader::TrackHeader( TrackView* parent )
: QHeaderView( Qt::Horizontal, parent )
, m_parent( parent )
, m_init( false )
{
setStretchLastSection( false );
setResizeMode( QHeaderView::Interactive );
setMinimumSectionSize( 60 );
connect( this, SIGNAL( sectionResized( int, int, int ) ), SLOT( onSectionResized( int, int, int ) ) );
}
TrackHeader::~TrackHeader()
{
saveColumnsState();
}
static uint negativeWidth = 0;
void
TrackHeader::onSectionResized( int logicalIndex, int oldSize, int newSize )
{
qDebug() << Q_FUNC_INFO;
if ( !m_init )
return;
blockSignals( true );
if ( newSize < 0 )
resizeSection( logicalIndex, 0 );
for ( int i = logicalIndex + 1; i < count(); i++ )
{
int ns = sectionSize( i ) + oldSize - newSize;
if ( ns < minimumSectionSize() )
{
resizeSection( logicalIndex, newSize - ( minimumSectionSize() - ns ) );
ns = minimumSectionSize();
}
resizeSection( i, ns );
break;
}
blockSignals( false );
negativeWidth = 0;
uint w = 0;
for ( int x = 0; x < m_columnWeights.count(); x++ )
{
w += sectionSize( x );
negativeWidth += sectionSize( x );
}
for ( int x = 0; x < m_columnWeights.count(); x++ )
{
m_columnWeights[x] = (double)sectionSize( x ) / double( w );
}
negativeWidth -= w;
}
void
TrackHeader::onResized()
{
qDebug() << Q_FUNC_INFO;
if ( !m_init && count() )
restoreColumnsState();
m_init = false;
blockSignals( true );
double width = m_parent->contentsRect().width();
#ifdef Q_WS_MAC
if ( m_parent->verticalScrollBar() && m_parent->verticalScrollBar()->isVisible() )
{
width -= m_parent->verticalScrollBar()->width() + 1;
}
#endif
blockSignals( false );
for ( int i = 0; i < m_columnWeights.count(); i++ )
{
if ( m_columnWeights[i] > 0 )
resizeSection( i, int( width * m_columnWeights[i] ) );
}
m_init = true;
}
void
TrackHeader::restoreColumnsState()
{
TomahawkSettings* s = APP->settings();
QList<QVariant> list = s->playlistColumnSizes();
qDebug() << "COOOOOOUNT:" << count() << list.count();
if ( list.count() != count() ) // FIXME: const
{
m_columnWeights << 0.19 << 0.24 << 0.18 << 0.07 << 0.07 << 0.11 << 0.14;
}
else
{
foreach( const QVariant& v, list )
m_columnWeights << v.toDouble();
}
}
void
TrackHeader::saveColumnsState()
{
TomahawkSettings *s = APP->settings();
QList<QVariant> wlist;
foreach( double w, m_columnWeights )
{
wlist << QVariant( w );
// qDebug() << "Storing weight for column" << i++ << w;
}
s->setPlaylistColumnSizes( wlist );
}

View File

@@ -1,6 +1,7 @@
#ifndef TRACKVIEW_H
#define TRACKVIEW_H
#include <QHeaderView>
#include <QTreeView>
#include <QSortFilterProxyModel>
@@ -9,6 +10,31 @@
class PlaylistInterface;
class TrackModel;
class TrackProxyModel;
class TrackView;
class TrackHeader : public QHeaderView
{
Q_OBJECT
public:
explicit TrackHeader( TrackView* parent = 0 );
~TrackHeader();
public slots:
void onResized();
private slots:
void onSectionResized( int logicalIndex, int oldSize, int newSize );
private:
void restoreColumnsState();
void saveColumnsState();
TrackView* m_parent;
QList<double> m_columnWeights;
bool m_init;
};
class TrackView : public QTreeView
{
@@ -50,24 +76,16 @@ protected:
private slots:
void onItemResized( const QModelIndex& index );
void resizeColumns();
void onSectionResized( int logicalIndex, int oldSize, int newSize );
void onFilterChanged( const QString& filter );
private:
void restoreColumnsState();
void saveColumnsState();
QPixmap createDragPixmap( int itemCount ) const;
TrackModel* m_model;
TrackProxyModel* m_proxyModel;
PlaylistInterface* m_modelInterface;
PlaylistItemDelegate* m_delegate;
QList<double> m_columnWeights;
QList<int> m_columnWidths;
TrackHeader* m_header;
bool m_resizing;
bool m_dragging;

View File

@@ -183,7 +183,7 @@ TomahawkSettings::playlistColumnSizes() const
void
TomahawkSettings::setPlaylistColumnSizes( const QList<QVariant>& cols )
{
setValue( "ui/playlist/geometry", cols );
setValue( "ui/playlist/columnSize", cols );
}

View File

@@ -24,7 +24,7 @@ public:
QByteArray mainWindowState() const;
void setMainWindowState( const QByteArray& state );
QList<QVariant> playlistColumnSizes() const;
void setPlaylistColumnSizes( const QList<QVariant>& cols );

View File

@@ -143,49 +143,49 @@ ageToString( const QDateTime& time )
if ( years )
{
if ( years > 1 )
return QString( "%1 years ago" ).arg( years );
return QString( "%1 years" ).arg( years );
else
return QString( "%1 year ago" ).arg( years );
return QString( "%1 year" ).arg( years );
}
if ( months )
{
if ( months > 1 )
return QString( "%1 months ago" ).arg( months );
return QString( "%1 months" ).arg( months );
else
return QString( "%1 month ago" ).arg( months );
return QString( "%1 month" ).arg( months );
}
if ( weeks )
{
if ( weeks > 1 )
return QString( "%1 weeks ago" ).arg( weeks );
return QString( "%1 weeks" ).arg( weeks );
else
return QString( "%1 week ago" ).arg( weeks );
return QString( "%1 week" ).arg( weeks );
}
if ( days )
{
if ( days > 1 )
return QString( "%1 days ago" ).arg( days );
return QString( "%1 days" ).arg( days );
else
return QString( "%1 day ago" ).arg( days );
return QString( "%1 day" ).arg( days );
}
if ( hours )
{
if ( hours > 1 )
return QString( "%1 hours ago" ).arg( hours );
return QString( "%1 hours" ).arg( hours );
else
return QString( "%1 hour ago" ).arg( hours );
return QString( "%1 hour" ).arg( hours );
}
if ( mins )
{
if ( mins > 1 )
return QString( "%1 minutes ago" ).arg( mins );
return QString( "%1 minutes" ).arg( mins );
else
return QString( "%1 minute ago" ).arg( mins );
return QString( "%1 minute" ).arg( mins );
}
return QString();