viteqtreewidget.cpp 5.82 KB
Newer Older
1 2 3 4 5 6
#include <QDropEvent>
#include <QDragMoveEvent>
#include <QDragEnterEvent>
#include <QMimeData>
#include<QList>
#include "viteqtreewidget.hpp"
Augustin Degomme's avatar
Augustin Degomme committed
7 8 9 10 11 12 13 14
#include "stdio.h"
 
viteQTreeWidget::viteQTreeWidget(QWidget *parent)
    : QTreeWidget(parent)
{
 
}

15
    viteQTreeWidget::~viteQTreeWidget() = default;
Augustin Degomme's avatar
Augustin Degomme committed
16 17 18 19 20 21 22 23 24 25

void viteQTreeWidget::dropEvent(QDropEvent *e){

       
    //we want to avoid to destroy the hierarchy of the trace, so let's only move containers near a container with the same parent 

    
    //QTreeWidgetItem* item= itemAt(event->pos());//get the item where we want to move
    //printf("drop %s to %s!\n", );

26
         //   const QPoint pos = e->pos();
Augustin Degomme's avatar
Augustin Degomme committed
27 28
            QTreeWidgetItem* target = itemAt(e->pos());
            
29
            if(target==nullptr){
Augustin Degomme's avatar
Augustin Degomme committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                e->ignore();
                return;
            }

            bool do_move = true;

            // this is used to determine if the target is itself a child
          
                foreach(QTreeWidgetItem* item, selected_items)
                {

                    // if target and item don't share the same parent...
                    if(target->parent() != item->parent())
                    {
                        // ...then don't allow the move
                        do_move = false;
                        break;
                    }
                    
                    if(target==item){
                        // ...do nothing
                        do_move = false;
                        break;
                    }
                    
                }
          

            if(!do_move)
                e->setDropAction(Qt::IgnoreAction);
            else
            {
                //QTreeWidget::dropEvent(e);
63 64 65 66 67 68
                int index=0;
                if(target->parent())
                index=target->parent()->indexOfChild(target);
                else
                index = indexOfTopLevelItem(target);
                
Augustin Degomme's avatar
Augustin Degomme committed
69 70
                int modifier=0;
                
71 72 73 74 75 76
                int initial_count=0;
                if(target->parent())
                initial_count=target->parent()->childCount();
                else
                initial_count=topLevelItemCount();
                
Augustin Degomme's avatar
Augustin Degomme committed
77 78 79 80
                //we want to insert below the pointed event
                foreach(QTreeWidgetItem* item, selected_items)
                {
                    //remove all items from their parent
81 82 83 84 85 86 87 88
                    
                   if(item->parent()) {
                        if(item->parent()->indexOfChild(item)<index)modifier++;
                        item->parent()->removeChild(item);
                    }else{
                        if(indexOfTopLevelItem(item)<index)modifier++; 
                        takeTopLevelItem(indexOfTopLevelItem(item));
                    }
Augustin Degomme's avatar
Augustin Degomme committed
89 90 91 92

                }
                //insert them again in the right position and order

93
                if(target->parent())
Augustin Degomme's avatar
Augustin Degomme committed
94
                index=target->parent()->indexOfChild(target);
95 96
                else
                index = indexOfTopLevelItem(target);
Augustin Degomme's avatar
Augustin Degomme committed
97 98
               
                int max=0;
99 100 101 102 103 104 105 106 107 108 109
                if(target->parent()){
                    if(index+modifier > target->parent()->childCount()){
                        max=target->parent()->childCount();
                        foreach(QTreeWidgetItem* item, selected_items)
                        {
                            target->parent()->insertChild(max, item);
                            max++;
                        } 
                    }else{
                        max=index+modifier;
                        target->parent()->insertChildren(max, selected_items); 
Augustin Degomme's avatar
Augustin Degomme committed
110
                    }
111 112 113 114 115 116 117 118 119 120 121
                }else{
                    if(index+modifier > topLevelItemCount()){
                        max=topLevelItemCount();
                        foreach(QTreeWidgetItem* item, selected_items)
                        {
                            insertTopLevelItem(max, item);
                            max++;
                        }
                    } else{
                        max=index+modifier;
                        	insertTopLevelItems(max,selected_items); 
Augustin Degomme's avatar
Augustin Degomme committed
122 123
                }
                    
124 125 126 127 128 129 130
                    }
                    
               int count=0;
                if(target->parent())
                    count=target->parent()->childCount();
                else
                    count=topLevelItemCount();
Augustin Degomme's avatar
Augustin Degomme committed
131
                                
132
                if (count!=initial_count){
Augustin Degomme's avatar
Augustin Degomme committed
133 134 135 136
                   //prevent loss of elements if insertions failed (can happen sometimes)
                   //try to reinsert all elements at the end
                    foreach(QTreeWidgetItem* item, selected_items)
                    {
137 138 139 140
                        if(target->parent())
                            target->parent()->insertChild(target->parent()->childCount(), item);
                        else
                            insertTopLevelItem(topLevelItemCount(), item);
Augustin Degomme's avatar
Augustin Degomme committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
                    }
                    
                }
                e->setDropAction(Qt::TargetMoveAction);
            }

            e->accept();
            selected_items.clear();
    
    }
    


void viteQTreeWidget::dragEnterEvent(QDragEnterEvent *event)
{
            selected_items = selectedItems();
            if (event->source() == this)
            {
                event->setDropAction(Qt::MoveAction);
                event->accept();
            }
            else
                event->acceptProposedAction();

}

void viteQTreeWidget::dragMoveEvent(QDragMoveEvent *e)
{
e->accept();	
}

QMimeData *viteQTreeWidget::mimeData(const QList<QTreeWidgetItem *>) const
{
return new QMimeData;
}

bool viteQTreeWidget::dropMimeData ( QTreeWidgetItem * , int , const QMimeData * , Qt::DropAction  ) 
{
return true;
}

Qt::DropActions viteQTreeWidget::supportedDropActions () const
{
return Qt::CopyAction|Qt::MoveAction;
}