Lots of work on album grouping in the playlist. Now handles ragging around stuff...
[amarok.git] / src / playlist / PlaylistAlbumGroup.cpp
blobbfe5a29c12828801a264ae888fdef05b66309a0d
1 /***************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program 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 *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 #include "PlaylistAlbumGroup.h"
22 #include "debug.h"
24 namespace Playlist {
26 AlbumGroup::AlbumGroup()
28 //DEBUG_BLOCK
32 AlbumGroup::~AlbumGroup()
34 //DEBUG_BLOCK
39 void Playlist::AlbumGroup::addRow(int row)
41 //DEBUG_BLOCK
43 //Does this row fit in any of our existing groups?
44 bool inGroup = false;
45 for ( int i = 0; i < m_groups.count(); i++ ) {
47 if ( m_groups[i].rows.contains( row ) ) {
48 inGroup = true;
49 break;
51 else if ( m_groups[i].rows.last() == row - 1 ) {
52 m_groups[i].rows.append( row );
53 inGroup = true;
54 break;
59 //no group found, create new one:
60 if ( !inGroup ) {
61 Group newGroup;
62 newGroup.collapsed = false;
63 newGroup.rows.append( row );
64 m_groups.append( newGroup );
68 int Playlist::AlbumGroup::groupMode( int row )
70 //DEBUG_BLOCK
72 foreach( Group group, m_groups ) {
73 if ( group.rows.contains( row ) ) {
75 //debug() << "row " << row << " is collapsed= " << group.collapsed;
77 if ( group.rows.count() < 2 )
78 return None;
79 else if ( group.rows.first() == row ) {
80 if ( !group.collapsed )
81 return Head;
82 else
83 return Head_Collapsed;
84 } else if ( group.rows.last() == row ) {
85 if ( !group.collapsed )
86 return End;
87 else
88 return Collapsed;
89 } else {
90 if ( !group.collapsed )
91 return Body;
92 else
93 return Collapsed;
98 return None;
102 void Playlist::AlbumGroup::setCollapsed(int row, bool collapsed)
104 //DEBUG_BLOCK
105 for (int i = 0; i < m_groups.count(); i++ ) {
106 if ( m_groups[ i ].rows.contains( row ) ) {
107 m_groups[ i ].collapsed = collapsed;
108 //debug() << "row " << row << " collapsed = " << m_groups[ i ].collapsed;
113 int Playlist::AlbumGroup::elementsInGroup(int row)
115 //DEBUG_BLOCK
116 foreach( Group group, m_groups ) {
117 if ( group.rows.contains( row ) ) {
118 return group.rows.count();
122 return 0;
126 int Playlist::AlbumGroup::firstInGroup(int row)
129 foreach( Group group, m_groups ) {
130 if ( group.rows.contains( row ) ) {
131 return group.rows.first();
135 return -1;
139 int Playlist::AlbumGroup::lastInGroup(int row)
142 foreach( Group group, m_groups ) {
143 if ( group.rows.contains( row ) ) {
144 return group.rows.last();
148 return -1;
151 void Playlist::AlbumGroup::removeGroup(int row)
154 for (int i = 0; i < m_groups.count(); i++ ) {
155 if ( m_groups[ i ].rows.contains( row ) ) {
156 m_groups.removeAt( i );
157 return;
164 int Playlist::AlbumGroup::subgroupCount()
166 return m_groups.count();
169 void Playlist::AlbumGroup::printGroupRows()
171 foreach( Group group, m_groups ) {
172 debug() << "Subgroup: " << group.rows;
177 void Playlist::AlbumGroup::removeBetween(int first, int last)
179 DEBUG_BLOCK
180 debug() << "first: " << first << ", last: " << last;
181 for ( int i = first; i <= last; i++ ) {
182 for (int j = 0; j < m_groups.count(); j++ ) {
183 if ( m_groups[ j ].rows.contains( i ) ) {
184 m_groups.removeAt( j );
192 //when something is inserted or removed, all following indexes must be moved to match their actual new position.
193 void Playlist::AlbumGroup::offsetBetween(int first, int last, int offset)
196 DEBUG_BLOCK
197 debug() << "first: " << first << ", last: " << last;
198 for (int j = 0; j < m_groups.count(); j++ ) {
199 for ( int i = first; i <= last; i++ ) {
200 if ( m_groups[ j ].rows.contains( i ) ) {
201 //offset all in this group (so we dont break any groups)
202 for ( int k = 0; k < m_groups[ j ].rows.count(); k++ ) {
203 m_groups[ j ].rows.append( m_groups[ j ].rows.takeFirst() + offset );
205 break;