Exemple #1
0
/**
 * \brief Set the amount of children of a SphereFraction
 * \param yawres Number of columns
 * \param pitchres Number of rows
 */
void SphereFraction::setChildren(int yawres, int pitchres)
{
	// do not change children if m_children_static is activated
	if (m_children_static) return;

	for (auto &row : m_children)
	for (auto child : row)
		delete child.second;

	m_children.clear();

	if (yawres == 0 || pitchres == 0) return;

	float pitchperc1 = 0;
	float pitchperc2 = 0;

	for (int pitchn = 0; pitchn <= pitchres; pitchn++)
	{
		std::map<int, SphereFraction *> row;
		row.clear();

		pitchperc1 = pitchperc2;
		pitchperc2 = (pitchn * 1.) / pitchres;

		float yawperc = 0;

		SphericalVector3f p[4]; // contains children's positions
		p[0] = getChildPosition(m_positions, 0, pitchperc1);
		p[2] = getChildPosition(m_positions, 0, pitchperc2);

		int yawn = 0;
		for (; yawn <= yawres + 1; yawn++)
		{
			yawperc = (yawn * 1.) / yawres;

			p[1] = getChildPosition(m_positions, yawperc, pitchperc1);
			p[3] = getChildPosition(m_positions, yawperc, pitchperc2);

			row[yawn] = new SphereFraction(p);

			p[0] = p[1];
			p[2] = p[3];
		}

		m_children.push_back(row);
	}
}
void ContactListProxyModel::insertItem(const QModelIndex &source)
{
    if( !source.isValid() )
        return;
    TreeItem *source_item = static_cast<TreeItem*>(source.internalPointer());
    QString name = source_item->data(Qt::DisplayRole).toString();
    int mass = source_item->data(Qt::UserRole+1).toInt();
    int visibility = source_item->getItemVisibility();
    int attributes = source_item->getAttributes();
    const TreeModelItem &structure = source_item->getStructure();
    int source_type = structure.m_item_type;
    bool show = false;
    if( source_type == 0 )
    {
        show |= ( mass == 1000 ) && ( visibility & ShowOffline || (m_show_offline && !( visibility & NotShowOffline ) ) );
        show |= ( mass < 1000 )  && ( visibility & ShowOnline );
        show |= ( attributes & ItemHasUnreaded )      && ( visibility & ShowMessage );
        show |= ( attributes & ItemHasChangedStatus ) && ( visibility & ShowStatus );
        show |= ( attributes & ItemIsTyping )         && ( visibility & ShowTyping );
    }
    else
        show = true;
    if( !show )
        return;
    QModelIndex proxy_index = mapFromSource(source.parent());
    int position=getItem(proxy_index)->childCount();
    QVariant font;
    QVariant color;
    if(source_type==0)
    {
        font=mass==1000?m_offline_font:m_online_font;
        color=mass==1000?m_offline_color:m_online_color;
    }
    else if(source_type==1)
    {
        font=m_group_font;
        color=m_group_color;
    }
    else if(source_type==2)
    {
        font=m_account_font;
        color=m_account_color;
    }
    if(source_type==0)
    {
        if(!m_sort_status && mass<1000)
            mass=0;
        if(m_model_type==2 || m_model_type==3)
            if(source_item->parent()->data(Qt::DisplayRole).toString().isEmpty())
                mass+=2000;
        position = findPosition(proxy_index,name,mass);
    }
    else if(source_type==1)
        position = findPosition(proxy_index,name,mass);
    else if(source_type==2)
        mass=-100;
    switch(m_model_type)
    {
    case 2: // With accounts, without groups
    case 0: { // Standart view
        if(source_type==0 && m_model_type==2)
        {
            proxy_index = mapFromSource(source.parent().parent());
            position = findPosition(proxy_index,name,mass);
        }
        if(!proxy_index.isValid() && source_type!=2)
        {
            insertItem(source.parent());
            proxy_index = mapFromSource(source.parent());
            position = findPosition(proxy_index,name,mass);
        }
        switch(source_type)
        {
        case 1:
            if(m_model_type==2)
                return;
            if(!m_show_empty_group && ((source_item->m_visible+m_show_offline?source_item->childCount():0)==0))
                return;
        case 0:
        case 2: {
//			emit layoutAboutToBeChanged();
            m_tree_view->setUpdatesEnabled(false);
            if(position>getItem(proxy_index)->childCount())
                position=getItem(proxy_index)->childCount();
            else if(position<0)
                position=0;
            bool expanded = false;
            if( source_type > 0 )
                expanded = source_item->isExpanded();
            insertRows(position,1,proxy_index);
            ProxyModelItem *item = getItem(proxy_index)->child(position);
            item->setSourceIndex(source);
            item->setMass(mass);
            item->setName(name);
            item->setData(font,Qt::FontRole);
            item->setData(color,Qt::UserRole+10);
            endInsertRows();
            if(mass!=1000 && source_type==0)
                item->parent()->m_online_children++;
            m_source_list.insert(source_item,createIndex(0,0,item));
            if( source_type > 0 && expanded )
            {
                if( m_append_to_expand_list )
                    m_list_for_expand.append(item);
                else
                    m_tree_view->setExpanded( createIndex_(item), true );
            }
            m_tree_view->setUpdatesEnabled(true);
//			emit layoutChanged();
            break;
        }
        }
        break;
    }
    case 3: // without accounts, wihtout groups
        if(source_type==1 || source_type==2)
            return;
    case 1: { // View without separate to accounts
        TreeItem *source_item = static_cast<TreeItem*>(source.internalPointer());
        int source_type = source_item->data(Qt::UserRole).toInt();
        if(source_type==2)
            return;
        /*if(source_type==1)
        {
        	if(!m_show_empty_group && source_item->childCount()==0)
        		return;
        }*/
        // Try to find exists group with needed name
        QString group_name;
        if(source_type==1)
            group_name = source_item->data(Qt::DisplayRole).toString();
        else
            group_name = source_item->parent()->data(Qt::DisplayRole).toString();
        //if(m_model_type==3 && !group_name.isEmpty())
        //	group_name=tr("In list");
        ProxyModelItem *parent_item;
        QModelIndex group_index;
        if(m_model_type==1)
        {
            bool found=false;
            for(int i=0; i<m_root_item->childCount(); i++)
            {
                ProxyModelItem *group = m_root_item->child(i);
                QString name = group->getName();
                //if(m_model_type==3 && !name.isEmpty())
                //	name=tr("In list");
                if(name==group_name) // if group exists, break
                {
                    parent_item=group;
                    found=true;
                    break;
                }
            }
            if(source_type==1)
                group_index = createIndex(0,0,static_cast<TreeItem*>(source.internalPointer()));
            else
                group_index = createIndex(0,0,static_cast<TreeItem*>(source.parent().internalPointer()));
            if(!m_show_empty_group && m_model_type==1 && source_type==1 && static_cast<TreeItem*>(group_index.internalPointer())->m_visible==0)
                return;
            if(!found)// So create new group
            {
//				emit layoutAboutToBeChanged();
                bool expanded=isExpanded(group_name);
                int group_mass = getChildPosition(group_name);
                position = findPosition(QModelIndex(), group_name, group_mass);
                if(position>m_root_item->childCount())
                    position=m_root_item->childCount();
                else if(position<0)
                    position=0;
                insertRows(position,1,QModelIndex());
                parent_item = m_root_item->child(position);
                if(m_model_type==1 || group_name=="")
                {
                    parent_item->setSourceIndex(group_index);
                    m_source_list.insert(static_cast<TreeItem*>(group_index.internalPointer()),createIndex(0,0,m_root_item->child(position)));
                }
                parent_item->setMass(group_mass);
                parent_item->setName(group_name);
                parent_item->setData(font,Qt::FontRole);
                parent_item->setData(color,Qt::UserRole+10);
                endInsertRows();
                if(expanded)
                {
                    if( m_append_to_expand_list )
                        m_list_for_expand.append( parent_item );
                    else
                        m_tree_view->setExpanded( createIndex(position,0,parent_item), true );
                }
//				emit layoutChanged();
            }
            if(!m_source_list.contains(static_cast<TreeItem*>(group_index.internalPointer())))
            {
                parent_item->appendSourceIndex(group_index);
                m_source_list.insert(static_cast<TreeItem*>(group_index.internalPointer()),createIndex_(parent_item));
            }
            if(source_type==1) // This is all for creating group
                return;
        }
        else
        {
            parent_item = m_root_item;
        }
//		emit layoutAboutToBeChanged();
        //QModelIndex proxy_index = mapFromSource(source.parent());//mapFromSource(createIndex(0,0,source_item->parent()));
        int position = findPosition(createIndex_(parent_item),name,mass);
        insertRows(position,1,createIndex_(parent_item));
        ProxyModelItem *item = parent_item->child(position);
        item->setSourceIndex(source);
        item->setName(name);
        item->setMass(mass);
        item->setData(font,Qt::FontRole);
        item->setData(color,Qt::UserRole+10);
        endInsertRows();
        if(mass!=1000)
            item->parent()->m_online_children++;
        m_source_list.insert(source_item,createIndex(0,0,item));
        proxy_index = createIndex_(parent_item);
//		emit layoutChanged();
        break;
    }
    }
    position=getItem(proxy_index)->childCount();
    //return;
    if(source_type==0)
    {
        if(!m_sort_status && mass<1000)
            mass=0;
        // Add separator if need
        if(m_show_separator)
            if((mass==1000 && !getItem(proxy_index)->getSeparator(1)) || (mass>-1 && mass<999 && !getItem(proxy_index)->getSeparator(0)) || (mass>1999 && !getItem(proxy_index)->getSeparator(2) ))
            {
//				emit layoutAboutToBeChanged();
                int separator_mass;
                //int separator_position;
                QString separator_name;
                if(mass<1000)
                {
                    getItem(proxy_index)->setSeparator(0,true);
                    separator_mass=-1;
                    separator_name=tr("Online");
                }
                else if(mass==1000)
                {
                    getItem(proxy_index)->setSeparator(1,true);
                    separator_mass=999;
                    separator_name=tr("Offline");
                }
                else
                {
                    getItem(proxy_index)->setSeparator(2,true);
                    separator_mass=1500;
                    separator_name=tr("Not in list");
                }
                //getItem(proxy_index)->setSeparator(mass==1000?1:0,true);
                // Mass of offline separator is 999, of online -1
                //int separator_mass= (mass==1000)?999:-1;
                int separator_position = findPosition(proxy_index,"",separator_mass);
                insertRows(separator_position,1,proxy_index);
                ProxyModelItem *item = getItem(proxy_index)->child(separator_position);
                item->setMass(separator_mass);
                //item->setName(mass==1000?tr("Offline"):tr("Online"));
                item->setName(separator_name);
                item->setData(m_separator_font,Qt::FontRole);
                item->setData(m_separator_color,Qt::UserRole+10);
                endInsertRows();
//				emit layoutChanged();
            }
    }
}