view widgets/ItemContainer.cpp @ 59:57c85a9d9b4a

(none)
author benoitrigolleau
date Mon, 28 May 2007 08:14:40 +0000
parents 81921835ddf1
children afcf540ae3a2
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*   
Sound Access
EASAIER client application.
Silogic 2007. Benoit Rigolleau.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.  See the file
COPYING included with this distribution for more information.
*/

#include "ItemContainer.h"

#include <QtAlgorithms>
#include <iostream>
#include <QList>
#include <QPalette>

ItemContainer::ItemContainer(QWidget *parent) : QWidget(parent){

	m_linkedList = new QLinkedList<int>();
	m_map = new QMap<int,GenericItemList*>();
	
	m_cpt=0;

	QVBoxLayout *mainlayout = new QVBoxLayout();
	m_itemLayout = new QVBoxLayout;
	mainlayout->addLayout(m_itemLayout);
	mainlayout->addStretch();

	this->setLayout(mainlayout);

}

void ItemContainer::addItem(GenericItemList *item){
	m_map->insert(m_cpt,item);
	m_linkedList->prepend(m_cpt);
	item->setIndex(m_cpt);
	if(m_cpt==0){
		item->setAcceptDrag(false);
	}
	
	std::cerr << "add Item" << std::endl;
	connect(item, SIGNAL(itemDropped(int, int)),
	    this, SLOT(moveItem(int, int)));
	connect(item, SIGNAL(selected(int)),
	    this, SLOT(newItemSelected(int)));
	
	m_itemLayout->insertWidget(0,item);
	m_cpt++;
}

void ItemContainer::setSelectedItem(QVariant &data){

}

void ItemContainer::removeSelectedItem(){
	//QLindList<int>::iterator iter = m_linkedList->find(x);
	
}

void ItemContainer::reset()
{
	QLayoutItem *child;
	while ((child = m_itemLayout->takeAt(0)) != 0) {
		delete child->widget();
	}
}

void ItemContainer::removeAllItems(){
	m_linkedList->clear();
	reset();
	m_map->clear();
	m_cpt=0;

}

void ItemContainer::setCurrentIndex(int i){


}

QLinkedList<int>::iterator ItemContainer::find(int value)
{
	QLinkedList<int>::iterator i = m_linkedList->begin();
	while (i != m_linkedList->end() && *i != value){
		++i;
	}
	return i;
}

void ItemContainer::reorganize(){
	for (int i = 0; i < m_itemLayout->count(); i++){
		m_itemLayout->removeItem(m_itemLayout->itemAt(i));
    }
	QLinkedList<int>::iterator iter;
	for(iter = m_linkedList->begin(); iter != m_linkedList->end(); iter++){
		m_itemLayout->addWidget(m_map->value(*iter));
	}
}

/********* SLOTS **************/
void ItemContainer::moveItem(int idItem1, int idItem2){
	m_linkedList->removeAll(idItem1);
	QLinkedList<int>::iterator iter = find( idItem2);
	m_linkedList->insert(iter,idItem1);
	reorganize();
}

void ItemContainer::newItemSelected(int idItem){

	GenericItemList* item = m_map->value(m_currentItem);
	if(item!=0){
		item->setBackgroundRole(QPalette::Window);
		item->setForegroundRole(QPalette::WindowText);
	}

	m_currentItem=idItem;

	item = m_map->value(m_currentItem);
	if(item!=0){
		item->setAutoFillBackground(true);
		item->setBackgroundRole(QPalette::Highlight);
		item->setForegroundRole(QPalette::HighlightedText);
	}

	// il va faloir changer l'indice je pense. 
	// si ça plante lorsque l'on supprime un item, ça peut etre ça
	emit currentChanged(m_currentItem);
}

void ItemContainer::upCurrentItem(){
	//return if item count <=1 
	if (m_linkedList->count()<= 1){
		return;
	}
	
	QLinkedList<int>::iterator iter;
	// return if current item is the first
	iter  = m_linkedList->end()-1;
	if(*iter==m_currentItem){
		return;
	}
	// return if current item is on top
	iter  = m_linkedList->begin();
	if(*iter==m_currentItem){
		return;
	}
	
	
	// move item
	for(iter = m_linkedList->begin()+1; iter != m_linkedList->end(); iter++){
		if(*iter == m_currentItem){
			int itemOnTop = *(iter-1);
			*(iter-1)=m_currentItem;
			*iter = itemOnTop;
		}
	}
	reorganize();

}

void ItemContainer::downCurrentItem(){
	//return if item count <=1 
	if (m_linkedList->count()<= 1){
		return;
	}
	// return if current item is on bottom
	QLinkedList<int>::iterator iter;
	iter  = m_linkedList->end()-1;
	if(*iter==m_currentItem || *(iter-1)==m_currentItem){
		return;
	}
	
	//move item
	for(iter = m_linkedList->end()-2; iter != m_linkedList->begin()-1; iter--){
		if(*iter == m_currentItem){
			int itemOnBottom = *(iter+1);
			*(iter+1)=m_currentItem;
			*iter = itemOnBottom;
		}
	}
	reorganize();
}

void ItemContainer::openConfigBoxForCurrentItem(){
	GenericItemList* item = m_map->value(m_currentItem);
	if(item!=0){
		item->configAction();
	}
}