Revert previous commit, was incorrect
[amarok.git] / src / playlist / PlaylistAlbumGroup.cpp
blob3b5cef37e25dd94d80ecb40e6265b2b4aee39201
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 02110-1301, 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( const 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;
103 bool Playlist::AlbumGroup::alternate( int row )
105 if( m_groups.count() > 0) {
106 foreach( const Group &group, m_groups ) {
107 int index = group.rows.indexOf( row );
108 if ( index != -1 ) {
109 return ( index % 2 ) == 1;
113 return false;
116 void Playlist::AlbumGroup::setCollapsed(int row, bool collapsed)
118 //DEBUG_BLOCK
119 for (int i = 0; i < m_groups.count(); i++ ) {
120 if ( m_groups[ i ].rows.contains( row ) ) {
121 m_groups[ i ].collapsed = collapsed;
122 //debug() << "row " << row << " collapsed = " << m_groups[ i ].collapsed;
127 int Playlist::AlbumGroup::elementsInGroup(int row)
129 //DEBUG_BLOCK
130 foreach( const Group &group, m_groups ) {
131 if ( group.rows.contains( row ) ) {
132 return group.rows.count();
136 return 0;
140 int Playlist::AlbumGroup::firstInGroup(int row)
143 foreach( const Group &group, m_groups ) {
144 if ( group.rows.contains( row ) ) {
145 return group.rows.first();
149 return -1;
153 int Playlist::AlbumGroup::lastInGroup(int row)
156 foreach( const Group &group, m_groups ) {
157 if ( group.rows.contains( row ) ) {
158 return group.rows.last();
162 return -1;
165 void Playlist::AlbumGroup::removeGroup(int row)
168 for (int i = 0; i < m_groups.count(); i++ ) {
169 if ( m_groups[ i ].rows.contains( row ) ) {
170 m_groups.removeAt( i );
171 return;
178 int Playlist::AlbumGroup::subgroupCount()
180 return m_groups.count();
183 void Playlist::AlbumGroup::printGroupRows()
185 foreach( const Group &group, m_groups ) {
186 debug() << "Subgroup: " << group.rows;
191 void Playlist::AlbumGroup::removeBetween(int first, int last)
193 DEBUG_BLOCK
194 debug() << "first: " << first << ", last: " << last;
195 for ( int i = first; i <= last; i++ ) {
196 for (int j = 0; j < m_groups.count(); j++ ) {
197 if ( m_groups[ j ].rows.contains( i ) ) {
198 m_groups.removeAt( j );
206 //when something is inserted or removed, all following indexes must be moved to match their actual new position.
207 void Playlist::AlbumGroup::offsetBetween(int first, int last, int offset)
210 DEBUG_BLOCK
211 debug() << "first: " << first << ", last: " << last;
212 for (int j = 0; j < m_groups.count(); j++ ) {
213 for ( int i = first; i <= last; i++ ) {
214 if ( m_groups[ j ].rows.contains( i ) ) {
215 //offset all in this group (so we dont break any groups)
216 for ( int k = 0; k < m_groups[ j ].rows.count(); k++ ) {
217 m_groups[ j ].rows.append( m_groups[ j ].rows.takeFirst() + offset );
219 break;