view widgets/ItemContainer.cpp @ 79:afcf540ae3a2

add the real time filter stack to manage real time filters and their attributes
author lbajardsilogic
date Tue, 19 Jun 2007 15:15:12 +0000
parents 57c85a9d9b4a
children 5060939ca69d
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);

	m_currentItem = -1;

}

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(){
	if (m_currentItem < 0)
		return;
	
	QLinkedList<int>::iterator iter = find(m_currentItem);
	QMap<int,GenericItemList*>::iterator iterItem = m_map->find(*iter);
	
	QLinkedList<int>::iterator newCurItem = iter + 1;

	if (iter != m_linkedList->end())
	{
		m_linkedList->erase(iter);
	}
	if (iterItem != m_map->end())
	{
		delete iterItem.value();
		m_map->erase(iterItem);
	}
	
	reorganize();

	
	if (newCurItem != m_linkedList->end())
	{
		m_currentItem = *newCurItem;
		newItemSelected(m_currentItem);
		return;
	} else if (!m_linkedList->isEmpty()) {
		m_currentItem = *(m_linkedList->end() - 1);
		newItemSelected(m_currentItem);
		return;
	}
}

void ItemContainer::removeItem(QString &name){
	
	QMap<int,GenericItemList*>::iterator iterItem;

	int index = -1;

	//erase the element from the widget map
	for (iterItem = m_map->begin(); iterItem != m_map->end(); iterItem++)
	{
		QString iterName = iterItem.value()->getName();
		if (iterName == name)
		{
			index = iterItem.key();
			delete iterItem.value();
			m_map->erase(iterItem);
			break;
		}
	}

	if (index != -1)
	{
		QLinkedList<int>::iterator iter = find(index);

		QLinkedList<int>::iterator newCurItem = iter + 1;

		//erase the element from the linked list
		if (iter != m_linkedList->end())
		{
			m_linkedList->erase(iter);
		}

		//if it was the current item, select the following item if it exists
		if (index == m_currentItem)
		{
			if (newCurItem != m_linkedList->end())
			{
				m_currentItem = *newCurItem;
				newItemSelected(m_currentItem);
				return;
			} else if (!m_linkedList->isEmpty()) {
				m_currentItem = *(m_linkedList->end() - 1);
				newItemSelected(m_currentItem);
				return;
			}
		}
	}
	
	reorganize();
}
void ItemContainer::reset()
{
	QLayoutItem *child;
	while ((child = m_itemLayout->takeAt(0)) != 0) {
		delete child->widget();
	}
}

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

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(){
	int count = m_itemLayout->count();
	for (int i = count-1; i >= 0; i--){
		m_itemLayout->removeItem(m_itemLayout->itemAt(i));
    }
	QLinkedList<int>::iterator iter;
	for(iter = m_linkedList->begin(); iter != m_linkedList->end(); iter++){
		int j = *iter;
		QMap<int,GenericItemList*>::iterator iterItem = m_map->find(j);
		if (iterItem != m_map->end())
		{
			int toto = iterItem.key();
			m_itemLayout->addWidget(iterItem.value());
		}
		

		//m_itemLayout->addWidget(m_map->value(*iter));
	}
	m_itemLayout->update();
}

/********* 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();
	}
}

QString ItemContainer::getCurrentFilterName()
{
	QString name = "";

	if (m_currentItem < 0)
		return name;

	QLinkedList<int>::iterator iter = find(m_currentItem);
	
	if (iter != m_linkedList->end())
	{
		QMap<int,GenericItemList*>::iterator iterItem = m_map->find(*iter);
		if (iterItem != m_map->end())
		{
			GenericItemList* item = iterItem.value();
			name = item->getName();
		}
	}
	return name;
}