Make playlist items use the full width available to them and adjust correctly when...
[amarok.git] / src / columnlist.cpp
blob2e21f55508db67a0f5c55462a5f16e4e21973cc4
1 /*
2 Copyright (c) 2006 Gábor Lehel <illissius@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "columnlist.h"
22 #include "config-amarok.h"
23 #include "amarokconfig.h"
25 #include "metabundle.h"
26 #include "playlist.h"
28 #include <KVBox>
29 #include <k3listview.h>
30 #include <kpushbutton.h>
32 #include <q3header.h>
33 #include <QPainter>
34 #include <QToolTip>
35 //Added by qt3to4:
36 #include <Q3ValueList>
39 class MyCheckListItem: public Q3CheckListItem
41 typedef Q3CheckListItem super;
42 ColumnList *m_list;
43 public:
44 int column;
45 MyCheckListItem( int c, Q3ListView *v, const QString &s, Type t, ColumnList *list ):
46 super( v, s, t ), m_list( list ), column( c ) { }
47 virtual void paintCell( QPainter * p, const QColorGroup &cg, int c, int w, int a )
49 QFont f = p->font();
50 if( isOn() )
51 f.setBold( !f.bold() );
52 p->setFont( f );
53 super::paintCell( p, cg, c, w, a );
55 virtual void stateChange( bool b )
57 super::stateChange( b );
58 m_list->setChanged();
60 MyCheckListItem *itemAbove() { return static_cast<MyCheckListItem*>( Q3CheckListItem::itemAbove() ); }
61 MyCheckListItem *itemBelow() { return static_cast<MyCheckListItem*>( Q3CheckListItem::itemBelow() ); }
64 ColumnList::ColumnList( QWidget *parent, const char *name )
65 : KHBox( parent), m_changed( true )
67 setObjectName( name );
68 setSpacing( 5 );
70 KVBox *buttonbox = new KVBox( this );
72 m_up = new KPushButton( KGuiItem( QString(), "up" ), buttonbox );
73 m_up->setToolTip( i18n( "Move column up" ) );
74 connect( m_up, SIGNAL( clicked() ), this, SLOT( moveUp() ) );
76 m_down = new KPushButton( KGuiItem( QString(), "down" ), buttonbox );
77 m_down->setToolTip( i18n( "Move column down" ) );
78 connect( m_down, SIGNAL( clicked() ), this, SLOT( moveDown() ) );
80 m_list = new K3ListView( this );
81 m_list->addColumn("");
82 m_list->header()->hide();
83 m_list->setSelectionMode( Q3ListView::Single );
84 m_list->setResizeMode( Q3ListView::LastColumn );
85 m_list->setSorting( -1 );
86 m_list->setAcceptDrops( true );
87 m_list->setDragEnabled( true );
88 m_list->setDropVisualizer( true );
89 m_list->setDropVisualizerWidth( 3 );
90 connect( m_list, SIGNAL( moved() ), this, SLOT( updateUI() ) );
91 connect( m_list, SIGNAL( moved() ), this, SLOT( setChanged() ) );
92 connect( m_list, SIGNAL( currentChanged( Q3ListViewItem* ) ), this, SLOT( updateUI() ) );
94 Q3Header* const h = Playlist::instance()->header();
95 for( int i = h->count() - 1; i >= 0; --i )
97 const int s = h->mapToSection( i );
98 if( ( s != MetaBundle::Rating || AmarokConfig::useRatings() ) &&
99 ( s != MetaBundle::Mood || AmarokConfig::showMoodbar() ) &&
100 ( s != MetaBundle::Score || AmarokConfig::useScores() ) )
102 ( new MyCheckListItem( s, m_list, MetaBundle::prettyColumnName( s ), Q3CheckListItem::CheckBox, this ) )
103 ->setOn( h->sectionSize( s ) );
107 m_list->setCurrentItem( m_list->firstChild() );
108 updateUI();
109 resetChanged();
112 Q3ValueList<int> ColumnList::visibleColumns() const
114 Q3ValueList<int> v;
115 for( MyCheckListItem *item = static_cast<MyCheckListItem*>( m_list->firstChild() ); item; item = item->itemBelow() )
116 if( item->isOn() )
117 v.append( item->column );
118 return v;
121 Q3ValueList<int> ColumnList::columnOrder() const
123 Q3ValueList<int> v;
124 for( MyCheckListItem *item = static_cast<MyCheckListItem*>( m_list->firstChild() ); item; item = item->itemBelow() )
125 v.append( item->column );
126 return v;
129 bool ColumnList::isChanged() const
131 return m_changed;
134 void ColumnList::resetChanged()
136 m_changed = false;
139 void ColumnList::moveUp()
141 if( Q3ListViewItem *item = m_list->currentItem() )
142 if( item->itemAbove() )
144 m_list->moveItem( item, 0, item->itemAbove()->itemAbove() );
145 m_list->setCurrentItem( item );
146 m_list->ensureItemVisible( item );
147 updateUI();
148 setChanged();
152 void ColumnList::moveDown()
154 if( Q3ListViewItem *item = m_list->currentItem() )
156 m_list->moveItem( item, 0, item->itemBelow() );
157 m_list->setCurrentItem( item );
158 m_list->ensureItemVisible( item );
159 updateUI();
160 setChanged();
164 void ColumnList::updateUI()
166 m_up->setEnabled( m_list->currentItem() && m_list->currentItem()->itemAbove() );
167 m_down->setEnabled( m_list->currentItem() && m_list->currentItem()->itemBelow() );
170 void ColumnList::setChanged() //slot
172 if( !m_changed )
174 m_changed = true;
175 emit changed();
179 ColumnsDialog::ColumnsDialog()
180 : KDialog( MainWindow::self() ),
181 m_list( new ColumnList( this ) )
183 setCaption( i18n( "Playlist Columns" ) );
184 setModal( false );
186 setMainWidget( m_list );
187 enableButtonApply( false );
188 connect( m_list, SIGNAL( changed() ), this, SLOT( slotChanged() ) );
191 ColumnsDialog::~ColumnsDialog()
193 s_instance = 0;
196 void ColumnsDialog::slotApply()
198 apply();
199 KDialog::slotButtonClicked( KDialog::Apply );
202 void ColumnsDialog::slotOk()
204 apply();
205 KDialog::slotButtonClicked( KDialog::Ok );
208 void ColumnsDialog::hide()
210 KDialog::hide();
211 delete this;
214 void ColumnsDialog::apply()
216 Playlist::instance()->setColumns( m_list->columnOrder(), m_list->visibleColumns() );
217 m_list->resetChanged();
218 enableButtonApply( false );
221 void ColumnsDialog::display() //static
223 if( !s_instance )
224 s_instance = new ColumnsDialog;
225 s_instance->show();
228 void ColumnsDialog::slotChanged() //slot
230 enableButtonApply( true );
233 ColumnsDialog *ColumnsDialog::s_instance = 0;
235 #include "columnlist.moc"