UIDataWidget.cpp 14.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Gnevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


9
#include "UIMainWindow.h"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include "UIDataWidget.h"
#include "UIDataWidgetItem.h"
#include <QContextMenuEvent>
#include <QAction>
#include <QMenu>
#include <QMessageBox>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QLabel>




UIDataWidget::UIDataWidget( QWidget *parent ) : QTreeWidget( parent )
{
	connect( this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(updateVisibility(QTreeWidgetItem*)) );
26
    connect( this, SIGNAL(itemSelectionChanged()), this, SLOT(updateSelection()) );
27 28

    sortItems( 0, Qt::SortOrder::AscendingOrder );
29 30 31
}


32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
void UIDataWidget::updateDataCollapseState( QTreeWidgetItem* clickedItem )
{
    GenericUIData *m = ((UIDataWidgetItem*) clickedItem)->getUIData();
    m->SetExpanded( clickedItem->isExpanded() );
}


void UIDataWidget::updateContentBegin()
{
    disconnect( this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(updateDataCollapseState(QTreeWidgetItem*)) );
    disconnect( this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(updateDataCollapseState(QTreeWidgetItem*)) );
}


void UIDataWidget::updateContentEnd()
{
    connect( this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(updateDataCollapseState(QTreeWidgetItem*)) );
    connect( this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(updateDataCollapseState(QTreeWidgetItem*)) );
}


53 54
void UIDataWidget::updateVisibility( QTreeWidgetItem* clickedItem )
{
55 56
    // Disconnect the signal that triggers this slot to avoid multiple calls while modifying independent QTreeWidgetItem check state in the following code.

57
	disconnect( this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(updateVisibility(QTreeWidgetItem*)) );
58

59 60
    // Keep track of items whose visibility state is modified. The clicked item is part of it.

61 62 63
    QList<QTreeWidgetItem*> itemsWhoseVisibilityChanged;
    itemsWhoseVisibilityChanged.push_back( clickedItem );

64 65 66
    // If the clicked item is set to visible without maintaining CTRL pressed, visibility of all other items is cleared.

    if( !(QApplication::keyboardModifiers() & Qt::ControlModifier)  &&  clickedItem->checkState(0) == Qt::Checked )
67 68 69 70 71 72 73
        forAllTreeItems( [&]( UIDataWidgetItem *item ){
            if( item != clickedItem  &&  item->checkState(0) == Qt::Checked )
            {
                item->setCheckState( 0, Qt::Unchecked );
                itemsWhoseVisibilityChanged.push_back( item );
            }
        } );
74

75 76 77
    // If the clicked item makes part of the selection, update visibility of the whole selection accordingly.

    QList<QTreeWidgetItem*> selected = selectedItems();
78
    if( clickedItem->isSelected()  &&  !(QApplication::keyboardModifiers() & Qt::ControlModifier) )
79 80 81 82 83 84 85 86
    {
        for( auto s : selected )
            if( (s->flags() & Qt::ItemIsUserCheckable)  &&  s->checkState(0) != clickedItem->checkState(0) )
            {
                s->setCheckState( 0, clickedItem->checkState(0) );
                itemsWhoseVisibilityChanged.push_back( s );
            }

87 88 89 90 91 92
        // Tag items whose visibility must not be reset due to the click on the "eye" checkbox.
        if( selected.size() != 1  ||  !clickedItem->isSelected() )
        {
            m_LockedSelection = selected;
            m_IsSelectionLocked = false;
        }
93 94 95 96 97 98 99 100 101 102 103 104 105 106
    }

    // Otherwise, if the clicked item is not selected and CTRL is not pressed, selection is reset to this single item.

    else
    {
        if( !(QApplication::keyboardModifiers() & Qt::ControlModifier) )
            for( auto s : selected )
                s->setSelected( false );
        clickedItem->setSelected( true );
    }

    // Inform the main window of the items whose visibility must be updated.

107
    emit visibilityChanged( itemsWhoseVisibilityChanged );
108

109 110 111 112 113 114 115 116 117
    // Reconnect the triggering signal.

    connect( this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(updateVisibility(QTreeWidgetItem*)) );
}


// Force the QTreeWidget to preserve the selection while clicking the "eye" icon of a single item among a multi-selection.
void UIDataWidget::updateSelection()
{
118
    if( !m_IsSelectionLocked )
119
    {
120 121 122 123 124 125 126 127
        m_IsSelectionLocked = true;

        if( !m_LockedSelection.empty() )
        {
            for( auto s : m_LockedSelection )
                s->setSelected( true );
            m_LockedSelection.clear();
        }
128
    }
129 130 131
}


132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
void UIDataWidget::updateSelection( GenericUIData *d, Qt::KeyboardModifiers modifiers )
{
    QList< GenericUIData* > currentSelection = selectedData();

    UIDataWidgetItem *dItem = NULL;

    if( modifiers & Qt::CTRL )
    {
        forAllTreeItems( [&]( UIDataWidgetItem *item ){
            if( item->getUIData() == d )
            {
                dItem = item;
                item->setSelected( !item->isSelected() );
            }
        } );
    }
    else
    {
        forAllTreeItems( [&]( UIDataWidgetItem *item ){
            item->setSelected( item->getUIData() == d );
            if( item->getUIData() == d )
                dItem = item;
        } );
    }

    scrollToItem( dItem );
}


161 162
void UIDataWidget::mouseReleaseEvent( QMouseEvent *evt )
{
163
    if( evt->button() == Qt::LeftButton  &&  (QApplication::keyboardModifiers() & Qt::AltModifier) )
164
    {
165
        GenericUIData *m = ((UIDataWidgetItem*) itemAt(evt->pos()))->getUIData();
166 167
        if( m )
        {
168
            QString clickedType = m->GetTypeString();
169 170 171

            bool allSelected = true;

172 173 174 175
            forAllTreeItems( [&]( UIDataWidgetItem* item ){
                if( item->getUIData()->GetTypeString() == clickedType )
                    allSelected &= item->isSelected();
            } );
176

177 178 179 180
            forAllTreeItems( [&]( UIDataWidgetItem* item ){
                if( item->getUIData()->GetTypeString() == clickedType )
                    item->setSelected( !allSelected );
            } );
181 182
        }
    }
183 184
    else
        QTreeWidget::mouseReleaseEvent( evt );
185 186 187
}


188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
QList<UIDataWidgetItem*> UIDataWidget::dataWidgetsFromDropEvent( QDropEvent *evt ) const
{
    QStringList pointerStrings = evt->mimeData()->text().split( ';' );

    QList< UIDataWidgetItem* > widgetList;
    for( QString &ptrStr : pointerStrings )
    {
        bool ok;
        UIDataWidgetItem *ptr = (UIDataWidgetItem*) ptrStr.toLongLong( &ok, 16 );
        if( ok  &&  ptr )
            widgetList.push_back( ptr );
    }

    return widgetList;
}


QList<GenericUIData*> UIDataWidget::dataFromDropEvent( QDropEvent *evt ) const
{
    QList< GenericUIData* > dataList;
    for( auto w : dataWidgetsFromDropEvent(evt) )
        dataList.push_back( w->getUIData() );
    return dataList;
}


214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
void UIDataWidget::dragEnterEvent( QDragEnterEvent *evt )
{
	evt->acceptProposedAction();
}


void UIDataWidget::dragMoveEvent( QDragMoveEvent *evt )
{
	evt->acceptProposedAction();
}


void UIDataWidget::dropEvent( QDropEvent *evt )
{
    if( evt->source() == this )
    {
        UIDataWidgetItem* dstItem = (UIDataWidgetItem*) itemAt( evt->pos() );
231
        QList<UIDataWidgetItem*> draggedItems = dataWidgetsFromDropEvent(evt);
232

233 234
        for( auto draggedItem : draggedItems )
            if( (!dstItem  &&  draggedItem->parent())  ||  dstItem != draggedItem->parent() )
235
            {
236 237 238 239 240 241 242
                auto p = (QTreeWidgetItem*) dstItem;
                while( p )
                {
                    if( p == draggedItem )
                        return;
                    p = p->parent();
                }
243

244 245 246
                GenericUIData *prevParent = draggedItem->parent()?  ((UIDataWidgetItem*)draggedItem->parent())->getUIData() : NULL;
                GenericUIData *newParent  = dstItem?  dstItem->getUIData() : NULL;

247 248 249 250 251 252 253 254
                if( draggedItem->parent() )
                {
                    draggedItem->parent()->removeChild( draggedItem );
                    if( !dstItem )
                        draggedItem->getUIData()->SetParent( NULL );
                }
                else
                    invisibleRootItem()->removeChild( draggedItem );
255

256 257 258 259 260 261 262
                if( dstItem )
                {
                    dstItem->addChild( draggedItem );
                    draggedItem->getUIData()->SetParent( dstItem->getUIData() );
                }
                else
                    invisibleRootItem()->addChild( draggedItem );
263 264

                emit parentDataChanged( draggedItem->getUIData(), prevParent, newParent );
265 266 267 268 269 270 271 272 273
            }
    }
    else
    	emit contentDropped( evt );
}


void UIDataWidget::mouseDoubleClickEvent( QMouseEvent *evt )
{
274 275 276 277 278
    UIDataWidgetItem* item = (UIDataWidgetItem*) itemAt( evt->pos() );
    if( item )
        emit itemDoubleClicked( item->getUIData() );
    else
        QTreeWidget::mouseDoubleClickEvent( evt );
279 280 281 282 283
}


void UIDataWidget::mouseMoveEvent( QMouseEvent *evt )
{
284
    if( evt->buttons() & Qt::RightButton )
285
    {
286 287 288 289 290 291 292 293 294 295
        QString mimeText;

        QList<QTreeWidgetItem*> selection = selectedItems();
        while( !selection.isEmpty() )
        {
            mimeText += "0x" + QString::number( (qulonglong) selection.takeFirst(), 16 );
            if( !selection.isEmpty() )
                mimeText += ";";
        }

296
        QMimeData *mimeData = new QMimeData();
297
        mimeData->setText( mimeText );
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

        QDrag *drag = new QDrag( this );
        drag->setMimeData( mimeData );

        Qt::DropAction dropAction = drag->exec( Qt::MoveAction );
    }
    else
        QTreeWidget::mouseMoveEvent( evt );
}


void UIDataWidget::contextMenuEvent( QContextMenuEvent *evt )
{
    typedef QMap< QString, QList<GenericUIData*> > MngByTypeMap;
    QMenu menu;
    bool addFactoryDispModes = true;


    // Recovers the selection and sorts it by data type.
    QList<GenericUIData*> selection = selectedData();
    MngByTypeMap selectionSortedByType;

320
    for( GenericUIData *m : selection )
321 322 323 324
        selectionSortedByType[m->GetTypeString()].push_back( m );


    // Adds menu entries for the exports available for the selected items.
325
    for( ExportInterface *exporter : m_ExportPlugins )
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    {
        QList<int> exportIds;
        QStringList exportNames;
        exporter->exportIds( exportIds );
        exporter->exportNames( exportNames );

        QList<int>::iterator id = exportIds.begin();
        QStringList::iterator name = exportNames.begin();

        while( id != exportIds.end() )
        {
            // Recovers the list of data types required by the export plug-in and checks if all of them
            // belong to the current selection.
            QVector<QString> requiredTypes;
            exporter->acceptedDataTypes( *id, requiredTypes );

            bool exportDataTypesOK = true;
343
            for( QString& type : requiredTypes )
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
                if( !selectionSortedByType.contains(type) )
                {
                    exportDataTypesOK = false;
                    break;
                }

            // If it is the case, an entry for this export is added to the menu.
            if( exportDataTypesOK )
            {
                ExportActionData exportData;
                exportData.id     = *id;
                exportData.name   = *name;
                exportData.plugin = exporter;

                QAction *actExport = new QAction( "Export "+(*name), &menu );
                actExport->setData( QVariant::fromValue(exportData) );
                connect( actExport, SIGNAL(triggered()), this, SLOT(launchExport()) );
                menu.addAction( actExport );
            }

            ++ id;
            ++ name;
        }
    }


    if( !menu.isEmpty() )
    	menu.exec( QCursor::pos() );
}


void UIDataWidget::launchExport()
{
    if( QAction *act = dynamic_cast<QAction*>(sender()) )
    {
        ExportActionData exportData = act->data().value<ExportActionData>();
        emit exportLaunched( exportData.plugin, exportData.id, exportData.name );
    }
}


385 386 387 388 389 390 391 392
void UIDataWidget::editSelectedDataName()
{
    QList<QTreeWidgetItem*> selected = selectedItems();
    if( selected.size() == 1 )
        editItem( selected.front(), 0 );
}


393 394 395 396 397
void UIDataWidget::keyPressEvent( QKeyEvent *evt )
{
	switch( evt->key() )
	{
		case Qt::Key_Delete:
398 399 400 401
        {
            emit removalAsked();
            return;
        }
402 403 404
		case Qt::Key_A:
		{
			if( evt->modifiers() == Qt::ControlModifier )
405
            {
406
				selectAll();
407 408 409
                return;
            }
            break;
410
		}
411 412 413 414 415 416 417 418 419 420
        case Qt::Key_F2:
        {
            QList<QTreeWidgetItem*> selected = selectedItems();
            if( selected.size() == 1 )
            {
                editItem( selected.front(), 0 );
                return;
            }
            break;
        }
421
	}
422 423

    QTreeWidget::keyPressEvent( evt );
424 425 426
}


427
QList<GenericUIData*> UIDataWidget::selectedData( const QStringList& typeFilters )
428 429 430 431
{
	QList<QTreeWidgetItem*> selected = selectedItems();
	QList<GenericUIData*> mng;

432 433 434 435 436 437 438
    if( typeFilters.empty() )
    	for( auto s=selected.begin(); s!=selected.end(); ++s )
    		mng.push_back( ((UIDataWidgetItem*) *s)->getUIData() );
    else
        for( auto s=selected.begin(); s!=selected.end(); ++s )
	    {
		    UIDataWidgetItem* item = (UIDataWidgetItem*) *s;
439 440 441 442 443 444 445

			for( auto t=typeFilters.begin(); t!=typeFilters.end(); ++t )
				if( item->getUIData()->GetTypeString() == *t )
				{
					mng.push_back( item->getUIData() );
					break;
				}
446
	    }
447 448 449

	return mng;
}
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490


void UIDataWidget::expandCollapseSelectedData()
{
    for( auto selected : selectedItems() )
        if( selected->isExpanded() )
            forAllItemsInSubTree( (UIDataWidgetItem*) selected, [&]( UIDataWidgetItem *item )
            {
                collapseItem( item );
            } );
        else
            forAllItemsInSubTree( (UIDataWidgetItem*) selected, [&]( UIDataWidgetItem *item )
            {
                expandItem( item );
            } );
}


void UIDataWidget::selectSubTrees()
{
    for( auto selected : selectedItems() )
        forAllItemsInSubTree( (UIDataWidgetItem*) selected, [&]( UIDataWidgetItem *item )
        {
            item->setSelected( true );
        } );
}


void UIDataWidget::selectSameTypesAsSelected()
{
    QSet< QString > selectedTypes;

    for( auto selected : selectedItems() )
        selectedTypes.insert( ((UIDataWidgetItem*) selected)->getUIData()->GetTypeString() );

    forAllTreeItems( [&]( UIDataWidgetItem *item )
    {
        if( selectedTypes.contains(item->getUIData()->GetTypeString()) )
            item->setSelected( true );
    } );
}