void QCustomTreeWidget::dropEvent(QDropEvent *e)
{
    QPoint pos = e->pos();
    QModelIndex index;
    // we test if we point inside an item
    if (e->source() == this && viewport()->rect().contains(pos),true)
    {
        index = indexAt(pos);
        if (!index.isValid())
        {
            return;
        }
        QTreeWidgetItem *item = itemAt(pos);
        // now we have an item, we check whether we are above, below or on it
        QRect rect = visualRect(index);
        const int margin = 2;
        if (pos.y() - rect.top() < margin)
        {
            // we are above it
            pTree->move(pTree->indicesOf(dynamic_cast<QCustomTreeWidgetItem*>(pDragSource)->branch()),pTree->indicesOf(dynamic_cast<QCustomTreeWidgetItem*>(item)->branch()));
        }
        else if (rect.bottom() - pos.y() < margin)
        {
            // we are below it
            pTree->move(pTree->indicesOf(dynamic_cast<QCustomTreeWidgetItem*>(pDragSource)->branch()),pTree->indicesOfNext(dynamic_cast<QCustomTreeWidgetItem*>(item)->branch()));
        }
        else if (rect.contains(pos, true))
        {
            // we are on it : new child
            stringstream buf(stringstream::in|stringstream::out);
            Branch *branch = dynamic_cast<QCustomTreeWidgetItem*>(item)->branch();
            buf << pTree->indicesOf(branch);
            // we want to add it as the last child
            buf << "_" << branch->tree().numberOfChildren();
            pTree->move(pTree->indicesOf(dynamic_cast<QCustomTreeWidgetItem*>(pDragSource)->branch()), buf.str());
        }
    }
    QTreeWidget::dropEvent(e);
    resizeColumnToContents(0);
}
void QCustomTreeWidget::addItem(QCustomTreeWidgetItem *item)
{
    pItemDial->exec();
    if (pItemDial->result()==QDialog::Accepted)
    {
        // creation of the new item
        Item *newItem = NULL;
        try
        {
            switch (pItemDial->type())
            {
                case Item::tSound:      newItem = new SoundItem(pItemDial->text().toStdString(),pItemDial->state(),pItemDial->fileName().toStdString(),bSizeLimited);
                                        break;
                case Item::tImage:    newItem = new ImageItem(pItemDial->text().toStdString(),pItemDial->state(),pItemDial->fileName().toStdString());
                                        break;
                default:                newItem = new Item(pItemDial->text().toStdString(),pItemDial->state());
                                        break;
            }
        }
        catch (exception &e)
        {
            QMessageBox::critical(this,QApplication::translate("mainWindow","Error",0),e.what());
            if (newItem != NULL)
            {
                delete newItem;
            }
            return;
        }
        if (item == NULL)
        {
            // There is no item in the tree
            Branch *newBranch = pTree->add(newItem);
            new QCustomTreeWidgetItem(this,newBranch);
        }
        else
        {
            switch (pItemDial->selectionResult())
            {
                case ItemDialog::rBrother:  {
                                                // we want to insert it after the given item
                                                Branch *parent = item->branch()->parent()->parent();
                                                if (parent==NULL)
                                                {
                                                    Branch *newBranch = pTree->insert(pTree->indexOf(item->branch())+1,newItem);
                                                    new QCustomTreeWidgetItem(this,newBranch,item);
                                                }
                                                else
                                                {
                                                    Branch *newBranch = parent->tree().insert(parent->tree().indexOf(item->branch())+1,newItem);
                                                    new QCustomTreeWidgetItem(dynamic_cast<QCustomTreeWidgetItem*>(item->parent()),newBranch,item);
                                                }
                                                break;
                                            }
                case ItemDialog::rChild:    {
                                                // we want to insert it inside the given item
                                                Branch *newBranch = item->branch()->tree().add(newItem);
                                                QCustomTreeWidgetItem *newQItem = new QCustomTreeWidgetItem(item,newBranch);
                                                expandItem(newQItem->parent());
                                                break;
                                            }
            }
        }
        resizeColumnToContents(0);
    }
}