1 /***************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
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. *
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. *
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"
26 AlbumGroup::AlbumGroup()
32 AlbumGroup::~AlbumGroup()
39 void Playlist::AlbumGroup::addRow(int row
)
43 //Does this row fit in any of our existing groups?
45 for ( int i
= 0; i
< m_groups
.count(); i
++ ) {
47 if ( m_groups
[i
].rows
.contains( row
) ) {
51 else if ( m_groups
[i
].rows
.last() == row
- 1 ) {
52 m_groups
[i
].rows
.append( row
);
59 //no group found, create new one:
62 newGroup
.collapsed
= false;
63 newGroup
.rows
.append( row
);
64 m_groups
.append( newGroup
);
68 int Playlist::AlbumGroup::groupMode( int row
)
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 )
79 else if ( group
.rows
.first() == row
) {
80 if ( !group
.collapsed
)
83 return Head_Collapsed
;
84 } else if ( group
.rows
.last() == row
) {
85 if ( !group
.collapsed
)
90 if ( !group
.collapsed
)
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
);
109 return ( index
% 2 ) == 1;
116 void Playlist::AlbumGroup::setCollapsed(int row
, bool collapsed
)
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
)
130 foreach( const Group
&group
, m_groups
) {
131 if ( group
.rows
.contains( row
) ) {
132 return group
.rows
.count();
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();
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();
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
);
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
)
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
)
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
);