Esempio n. 1
0
void FileListView::SelectAll( QItemSelectionModel::SelectionFlag selectAction, bool excludeCurrent /*= false*/ )
{
    QItemSelection toSelect;
    if (excludeCurrent)
    {
        toSelect << QItemSelectionRange(model()->index(0, 0), model()->index(currentIndex().row() - 1, 1));
        if (currentIndex().row() < model()->rowCount() - 1)
            toSelect << QItemSelectionRange(model()->index(currentIndex().row() + 1, 0), model()->index(model()->rowCount() - 1, 1));
    }
    else
        toSelect << QItemSelectionRange(model()->index(0, 0), model()->index(model()->rowCount(), 1));

    selectionModel()->select(toSelect, selectAction);
}
Esempio n. 2
0
const QItemSelection AMActionHistoryModel3::indicesBetween(const QModelIndex &brother, const QModelIndex &sister) const{
	QItemSelection selected;
	// Doesn't make sense if they don't share the same parent
	if(brother.parent() != sister.parent())
		return selected;
	QItemSelectionRange selectedRange;
	// Organize in the correct order
	if(brother.row() >= sister.row())
		selectedRange = QItemSelectionRange(sister, brother);
	else
		selectedRange = QItemSelectionRange(brother, sister);
	selected.append(selectedRange);
	return selected;
}
void GroupedIconView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command) {
  QVector<QModelIndex> indexes(IntersectingItems(rect.translated(horizontalOffset(), verticalOffset())));
  QItemSelection selection;

  foreach (const QModelIndex& index, indexes) {
    selection << QItemSelectionRange(index);
  }
RKTextMatrix RKVarEditMetaModel::getTextMatrix (const QItemSelectionRange& range) const {
	RK_TRACE (EDITOR);

	if ((!range.isValid ()) || data_model->objects.isEmpty ()) return RKTextMatrix ();

// NOTE: of course, when the range is small, this is terribly inefficient. On the other hand, it doesn't really matter, then, either.
	QItemSelectionRange erange = range.intersected (QItemSelectionRange (index (0, 0), index (trueRows () - 1, trueCols () - 1)));
	int top = erange.top ();
	int bottom = erange.bottom ();
	int left = erange.left ();
	int right = erange.right ();

	RKTextMatrix ret;
	int tcol = 0;
	for (int col = left; col <= right; ++col) {
		int trow = 0;
		for (int row = top; row <= bottom; ++row) {
			QVariant celldata = data (index (row, col), Qt::EditRole);
			if (!celldata.isValid ()) {
				RK_ASSERT (false);
				break;
			}
			ret.setText (trow, tcol, celldata.toString ());
			++trow;
		}
		++tcol;
	}

	return ret;
}
Esempio n. 5
0
void MusicListView::startDrag(Qt::DropActions supportedActions)
{
    Q_D(MusicListView);

    MetaPtrList list;
    for (auto index : selectionModel()->selectedIndexes()) {
        list << d->model->meta(index);
    }

    setAutoScroll(false);
    ListView::startDrag(supportedActions);
    setAutoScroll(true);

    QMap<QString, int> hashIndexs;
    for (int i = 0; i < d->model->rowCount(); ++i) {
        auto index = d->model->index(i, 0);
        auto hash = d->model->data(index).toString();
        Q_ASSERT(!hash.isEmpty());
        hashIndexs.insert(hash, i);
    }
    d->model->playlist()->saveSort(hashIndexs);
    Q_EMIT customSort();

    QItemSelection selection;
    for (auto meta : list) {
        if (!meta.isNull()) {
            auto index = this->findIndex(meta);
            selection.append(QItemSelectionRange(index));
        }
    }
    if (!selection.isEmpty()) {
        selectionModel()->select(selection, QItemSelectionModel::Select);
    }
}
void QgsAttributeTableDialog::updateSelection()
{
  QModelIndex index;
  QItemSelection selection;

  QgsFeatureIds::Iterator it = mSelectedFeatures.begin();
  for ( ; it != mSelectedFeatures.end(); ++it )
  {
    QModelIndex leftUpIndex = mFilterModel->mapFromSource( mModel->index( mModel->idToRow( *it ), 0 ) );
    QModelIndex rightBottomIndex = mFilterModel->mapFromSource( mModel->index( mModel->idToRow( *it ), mModel->columnCount() - 1 ) );
    selection.append( QItemSelectionRange( leftUpIndex, rightBottomIndex ) );
    //selection.append(QItemSelectionRange(leftUpIndex, leftUpIndex));
  }

  mSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );// | QItemSelectionModel::Columns);
  mView->setSelectionModel( mSelectionModel );
  updateTitle();

#if 0
  for ( int i = 0; i < mModel->rowCount(); ++i )
  {
    int id = mModel->rowToId( i );
    QgsDebugMsg( id );
  }
  QgsDebugMsg( "--------------" );
#endif
}
Esempio n. 7
0
void FileListView::mouseMoveEvent( QMouseEvent *event )
{
    if (event->buttons() & Qt::RightButton)
    {
        QModelIndex index = indexAt(QPoint(0, event->pos().y()));
        if (!index.isValid())
            index = model()->index(event->pos().y() < 0 ? 0 : model()->rowCount() - 1, 0);

        if (index != mouseMoveActionPrevIndex)
        {
            mouseMoveActionPrevIndex = index;
            setCurrentIndex(index);

            if (index.row() != mouseMovePrevIndex.row())
            {
                int start = qMin(index.row(), mouseMovePrevIndex.row()),
                    end = qMax(index.row(), mouseMovePrevIndex.row());

                QItemSelection toSelect;
                toSelect << QItemSelectionRange(model()->index(start, 0), model()->index(end, 1));
                selectionModel()->select(toSelect, currentSelectionAction);
            }
            else
                selectionModel()->select(index, currentSelectionAction);
        }
    }
    else
        QAbstractItemView::mouseMoveEvent(event);
}
Esempio n. 8
0
void QgsFeatureListView::selectAll()
{
  QItemSelection selection;
  selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->rowCount() - 1, 0 ) ) );

  mFeatureSelectionModel->selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
}
void QgsFeatureListView::setEditSelection( const QgsFeatureIds &fids )
{
  QItemSelection selection;

  foreach ( QgsFeatureId fid, fids )
  {
    selection.append( QItemSelectionRange( mModel->mapToMaster( mModel->fidToIdx( fid ) ) ) );
  }
void QgsFeatureListView::selectAll()
{
  disconnect( selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), this, SLOT( onSelectionChanged( QItemSelection, QItemSelection ) ) );
  QItemSelection selection;
  selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->rowCount() - 1, 0 ) ) );
  selectionModel()->select( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
  connect( selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), this, SLOT( onSelectionChanged( QItemSelection, QItemSelection ) ) );
}
Esempio n. 11
0
/*!
  Returns a proxy selection mapped from the specified \a sourceSelection.

  Reimplement this method to map source selections to proxy selections.
*/
QItemSelection QAbstractProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const
{
    QModelIndexList sourceIndexes = sourceSelection.indexes();
    QItemSelection proxySelection;
    for (int i = 0; i < sourceIndexes.size(); ++i)
        proxySelection << QItemSelectionRange(mapFromSource(sourceIndexes.at(i)));
    return proxySelection;
}
Esempio n. 12
0
/*!
  Returns a source selection mapped from the specified \a proxySelection.

  Reimplement this method to map proxy selections to source selections.
 */
QItemSelection QAbstractProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const
{
    QModelIndexList proxyIndexes = proxySelection.indexes();
    QItemSelection sourceSelection;
    for (int i = 0; i < proxyIndexes.size(); ++i)
        sourceSelection << QItemSelectionRange(mapToSource(proxyIndexes.at(i)));
    return sourceSelection;
}
Esempio n. 13
0
QItemSelection FlatProxyModel::mapSelectionToSource(const QItemSelection& proxySelection) const
{
    QItemSelection sourceSelection;

    for (int i = 0; i < proxySelection.count(); i++) {
        const QItemSelectionRange& range = proxySelection[i];

        SourceItem* topLeftItem = nullptr;
        SourceItem* bottomRightItem = nullptr;
        auto* currentItem = static_cast<SourceItem*>(range.topLeft().internalPointer());
        int row = range.topLeft().row();
        int left = range.topLeft().column();
        int right = range.bottomRight().column();
        while (currentItem && row <= range.bottomRight().row()) {
            Q_ASSERT(currentItem->pos() == row);
            if (!topLeftItem)
                topLeftItem = currentItem;

            if (currentItem->parent() == topLeftItem->parent()) {
                bottomRightItem = currentItem;
            }
            else {
                Q_ASSERT(topLeftItem && bottomRightItem);
                sourceSelection << QItemSelectionRange(mapToSource(createIndex(topLeftItem->pos(), left, topLeftItem)),
                                                       mapToSource(createIndex(bottomRightItem->pos(), right, bottomRightItem)));
                topLeftItem = nullptr;
                bottomRightItem = nullptr;
            }

            // update loop vars
            currentItem = currentItem->next();
            row++;
        }

        if (topLeftItem && bottomRightItem) {  // there should be one range left.
            sourceSelection << QItemSelectionRange(mapToSource(createIndex(topLeftItem->pos(), left, topLeftItem)),
                                                   mapToSource(createIndex(bottomRightItem->pos(), right, bottomRightItem)));
        }
    }

    return sourceSelection;
}
void QgsAttributeTableFilterModel::selectionChanged()
{
  disconnect( mMasterSelection, SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), this, SLOT( masterSelectionChanged( QItemSelection, QItemSelection ) ) );
  mMasterSelection->clear();

  QItemSelection selection;

  foreach ( QgsFeatureId fid, layer()->selectedFeaturesIds() )
  {
    selection.append( QItemSelectionRange( mTableModel->idToIndex( fid ) ) );
  }
Esempio n. 15
0
/*!
  Returns a proxy selection mapped from the specified \a sourceSelection.

  Reimplement this method to map source selections to proxy selections.
*/
QItemSelection QAbstractProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const
{
    QModelIndexList sourceIndexes = sourceSelection.indexes();
    QItemSelection proxySelection;
    for (int i = 0; i < sourceIndexes.size(); ++i) {
        const QModelIndex srcIdx = mapFromSource(sourceIndexes.at(i));
        if (!srcIdx.isValid())
            continue;
        proxySelection << QItemSelectionRange(srcIdx);
    }
    return proxySelection;
}
Esempio n. 16
0
/*!
  Returns a source selection mapped from the specified \a proxySelection.

  Reimplement this method to map proxy selections to source selections.
 */
QItemSelection QAbstractProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const
{
    QModelIndexList proxyIndexes = proxySelection.indexes();
    QItemSelection sourceSelection;
    for (int i = 0; i < proxyIndexes.size(); ++i) {
        const QModelIndex proxyIdx = mapToSource(proxyIndexes.at(i));
        if (!proxyIdx.isValid())
            continue;
        sourceSelection << QItemSelectionRange(proxyIdx);
    }
    return sourceSelection;
}
Esempio n. 17
0
void CSVSettings::View::setSelectedValues (const QStringList &list,
                                           bool doViewUpdate, bool signalUpdate) const
{
    QItemSelection selection;

    if (stringListsMatch (list, selectedValues()))
        return;

    if (!mHasFixedValues)
    {
        QStandardItemModel *model  =
                                static_cast <QStandardItemModel *>(mDataModel);

        model->clear();
        model->appendColumn (toStandardItemList (list));

        for (int i = 0; i < model->rowCount(); i++)
        {
            QModelIndex idx = model->index(i, 0);
            selection.append (QItemSelectionRange (idx, idx));
        }
    }
    else
    {
        for (int i = 0; i < mDataModel->rowCount(); i++)
        {
            if (list.contains(value(i)))
            {
                QModelIndex idx = mDataModel->index(i, 0);
                selection.append(QItemSelectionRange (idx, idx));
            }
        }
    }
    select (selection);

    //update the view if the selection was set from the model side, not by the
    //user
    if (doViewUpdate)
         updateView (signalUpdate);
}
/**
 * @brief Converts a selection of this model to a selection in the source model.
 *
 * Reimplemented from QSortFilterProxyModel to peacefully handle items that do
 * not exist in the source model.
 *
 * @param proxy_selection A selection in this model.
 * @return The corresponding source selection.
 */
QItemSelection QuestFilesModel::mapSelectionToSource(const QItemSelection& proxy_selection) const {

  QItemSelection source_selection;

  const QModelIndexList& indexes = proxy_selection.indexes();
  Q_FOREACH (const QModelIndex& index, indexes) {
    const QModelIndex& source_index = mapToSource(index);
    if (!index.isValid()) {
      // Selected item that does not exist in the source model.
      continue;
    }
    source_selection.append(QItemSelectionRange(source_index));
  }

  return source_selection;
}
Esempio n. 19
0
void QgsFeatureListView::setEditSelection( const QgsFeatureIds &fids )
{
  QItemSelection selection;

  const auto constFids = fids;
  for ( QgsFeatureId fid : constFids )
  {
    selection.append( QItemSelectionRange( mModel->mapToMaster( mModel->fidToIdx( fid ) ) ) );
  }

  bool ok = true;
  emit aboutToChangeEditSelection( ok );

  if ( ok )
    mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
}
Esempio n. 20
0
void FileListView::mousePressEvent( QMouseEvent *event )
{
    if ((event->button() == Qt::RightButton) || (event->button() == Qt::LeftButton && event->modifiers() & Qt::ControlModifier))
    {
        QModelIndex index = indexAt(event->pos());
        if (!index.isValid())
        {
            index = model()->index(model()->rowCount() - 1, 0);
            setCurrentIndex(index);
        }

        if (selectionModel()->isSelected(index))
            currentSelectionAction = QItemSelectionModel::Deselect;
        else
            currentSelectionAction = QItemSelectionModel::Select;

        mouseMovePrevIndex = index;

        selectionModel()->select(index, currentSelectionAction);
    }
    else if (event->button() == Qt::LeftButton && event->modifiers() & Qt::ShiftModifier)
    {
        QModelIndex indexPrev = currentIndex(), index = indexAt(event->pos());
        if (!index.isValid())
        {
            index = model()->index(model()->rowCount() - 1, 0);
            setCurrentIndex(index);
        }

        if (index.row() < indexPrev.row())
            qSwap(index, indexPrev);

        QItemSelection toSelect;
        toSelect << QItemSelectionRange(model()->index(indexPrev.row(), 0), model()->index(index.row(), 1));
        selectionModel()->select(toSelect, currentSelectionAction);
    }
    else if (event->button() == Qt::MidButton)
    {
        EnterSelected();
        return;
    }

    QAbstractItemView::mousePressEvent(event);
}
void RKVarEditModel::blankRange (const QItemSelectionRange& range) {
	RK_TRACE (EDITOR);

	if ((!range.isValid ()) || objects.isEmpty ()) return;

// NOTE: of course, when the range is small, this is terribly inefficient. On the other hand, it doesn't really matter, then, either.
	QItemSelectionRange erange = range.intersected (QItemSelectionRange (index (0, 0), index (trueRows () - 1, trueCols () - 1)));
	int top = erange.top ();
	int bottom = erange.bottom ();
	int left = erange.left ();
	int right = erange.right ();

	for (int col = left; col <= right; ++col) {
		RKVariable* var = objects[col];
		for (int row = top; row <= bottom; ++row) {
			var->setText (row, QString ());
		}
	}
}
RKTextMatrix RKVarEditModel::getTextMatrix (const QItemSelectionRange& range) const {
	RK_TRACE (EDITOR);

	if ((!range.isValid ()) || objects.isEmpty ()) return RKTextMatrix ();

// NOTE: of course, when the range is small, this is terribly inefficient. On the other hand, it doesn't really matter, then, either.
	QItemSelectionRange erange = range.intersected (QItemSelectionRange (index (0, 0), index (trueRows () - 1, trueCols () - 1)));
	int top = erange.top ();
	int bottom = erange.bottom ();
	int left = erange.left ();
	int right = erange.right ();

	RKTextMatrix ret;
	int tcol = 0;
	for (int col = left; col <= right; ++col) {
		QString* data = objects[col]->getCharacter (top, bottom);
		RK_ASSERT (data);
		ret.setColumn (tcol, data, bottom - top + 1);
		delete [] data;
		++tcol;
	}

	return ret;
}
Esempio n. 23
0
void FileListView::keyPressEvent( QKeyEvent *event )
{
    int overridden_key = 0;
    QModelIndex wasCurrent = currentIndex();

    bool isControlEnter = (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) && (event->modifiers() & Qt::ControlModifier);
    switch (event->key())
    {
    case Qt::Key_Shift:
        currentSelectionAction = selectionModel()->isSelected(currentIndex()) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
        break;

    case Qt::Key_Plus:
    case Qt::Key_Minus:
    case Qt::Key_Asterisk:
        SelectAll(event->key() == Qt::Key_Plus ? QItemSelectionModel::Select :
                  event->key() == Qt::Key_Minus ? QItemSelectionModel::Deselect :
                  QItemSelectionModel::Toggle);
        break;

    case Qt::Key_Left:
        overridden_key = Qt::Key_PageUp;
        break;
    case Qt::Key_Right:
        overridden_key = Qt::Key_PageDown;
        break;

    case Qt::Key_Insert:
        overridden_key = Qt::Key_Down;
        selectionModel()->select(currentIndex(), QItemSelectionModel::Toggle);
        break;

    case Qt::Key_Enter:
    case Qt::Key_Return:
        if (!isControlEnter)
        {
            if (event->modifiers() & Qt::ShiftModifier)
                emit OpenSelected();
            else
                emit EnterSelected();
        }
        break;
    }

    if (isSearchMode && !isControlEnter)
        switch (event->key())
        {
        case Qt::Key_Return:
        case Qt::Key_Enter:
        case Qt::Key_Escape:
        case Qt::Key_Left:
        case Qt::Key_Right:
        case Qt::Key_Up:
        case Qt::Key_Down:
        case Qt::Key_PageUp:
        case Qt::Key_PageDown:
        case Qt::Key_Home:
        case Qt::Key_End:
            Action_CloseQuickFind();
            break;
        }

    if ((isControlEnter && isSearchMode) ||
            (!event->text().isEmpty() && event->text() != "\x0d" &&
             !(event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) &&
             !edit(currentIndex(), AnyKeyPressed, event)))
    {
        Action_OpenQuickFind();
        if (!isControlEnter)
            Action_QuickFindProcessKeyPressEvent(event);

        bool isFound = (event->modifiers() & Qt::ShiftModifier) ? Action_QuickFindPrev() : Action_QuickFindNext(!isControlEnter);
        if (!isControlEnter)
        {
            static bool wasFound = true;
            if (wasFound != isFound)
            {
                wasFound = isFound;

                QPalette pal = searchEdit->palette();
                pal.setColor(QPalette::Base, QColor(255, 200, 200));
                if (isFound)
                    pal = palette();
                searchEdit->setPalette(pal);
            }
        }

        setDirtyRegion(QRegion(0, 0, width(), height()));
        //keyboardSearch(event->text());
        event->accept();
    }
    else
    {
        event->ignore();

        if (overridden_key)
        {
            QKeyEvent newEvent(event->type(), overridden_key, event->modifiers(), event->text(), event->isAutoRepeat(), event->count());
            QTreeView::keyPressEvent(&newEvent);
        }
        else
            QTreeView::keyPressEvent(event);
    }

    if (event->modifiers() & Qt::ShiftModifier)
    {
        QModelIndex current = currentIndex();
        switch (event->key())
        {
        case Qt::Key_Up:
        case Qt::Key_Down:
            selectionModel()->select(wasCurrent, currentSelectionAction);
            break;

        case Qt::Key_Left:
        case Qt::Key_PageUp:
        case Qt::Key_Right:
        case Qt::Key_PageDown:
        case Qt::Key_Home:
        case Qt::Key_End:
            if (current.row() < wasCurrent.row())
                qSwap(wasCurrent, current);

            QItemSelection toSelect;
            toSelect << QItemSelectionRange(model()->index(wasCurrent.row(), 0), model()->index(current.row(), 1));
            selectionModel()->select(toSelect, currentSelectionAction);
            break;
        }
    }
}
void QgsAttributeTableView::selectAll()
{
  QItemSelection selection;
  selection.append( QItemSelectionRange( mFilterModel->index( 0, 0 ), mFilterModel->index( mFilterModel->rowCount() - 1, 0 ) ) );
  selectionModel()->select( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
}
Esempio n. 25
0
bool KCSheetModel::setData(const QModelIndex &topLeft, const QModelIndex &bottomRight,
                         const QVariant &value, int role)
{
    return setData(QItemSelectionRange(topLeft, bottomRight), value, role);
}
void Project_selection_model::select ( const QItemSelection & selection, QItemSelectionModel::SelectionFlags command ){
//	/We only care for the first selected item
	QModelIndexList index_list = selection.indexes();
	for( int i=0; i<index_list.size() ; ++i) {
		QModelIndex index = index_list.at(i);
		if(!index.isValid()) continue;

		bool is_selected = this->isSelected(index);

		QItemSelection item_selected;
		QItemSelection item_deselected;

    //GsTL_object_item* item = static_cast<GsTL_object_item*>(index.internalPointer());
		GsTL_object_item* item = static_cast<GsTL_object_item*>(proxy_model_->mapToSource(index).internalPointer());
		//Check if a grid was selected
		Geostat_grid* grid = dynamic_cast<Geostat_grid*>(item);
		if(grid) {
			if(is_selected) {
				item_deselected.append(	QItemSelectionRange(index) );
			}
			else {
				item_selected.append(QItemSelectionRange(index) );
			}
			QItemSelectionModel::select(item_deselected,QItemSelectionModel::Deselect);
			QItemSelectionModel::select(item_selected,QItemSelectionModel::Select);

			selections_ = this->selection();
			return;
		}

		Grid_continuous_property* prop = dynamic_cast<Grid_continuous_property*>(item);
		if(prop == 0) {
			GsTL_object_property_item* object_prop = dynamic_cast<GsTL_object_property_item*>(item);
			if(object_prop) prop = object_prop->property();
		}
		if(prop) {
			if(is_selected) {

				item_deselected.append(	QItemSelectionRange(index) );
				item_deselected.append(	QItemSelectionRange(index.parent()) ); // remove the group id

			}
			else {  // Need to check if another property was selected
				QModelIndexList selected_index_list = 	this->selectedIndexes();
				for(int i=0; i<selected_index_list.size(); ++i) {

					GsTL_object_item* item_in_selection = static_cast<GsTL_object_item*>(proxy_model_->mapToSource(selected_index_list.at(i)).internalPointer());
          //GsTL_object_item* item_in_selection = static_cast<GsTL_object_item*>(selected_index_list.at(i).internalPointer());
					Grid_continuous_property* prop_in_selection = dynamic_cast<Grid_continuous_property*>(item_in_selection);
					if(prop_in_selection == 0) {
						GsTL_object_property_item* object_prop_in_selection = dynamic_cast<GsTL_object_property_item*>(item_in_selection);
						if(object_prop_in_selection) prop_in_selection = object_prop_in_selection->property();
					}
					if(prop_in_selection == 0) continue;
					// Does it has the same grid parent
					if(item->parent()->parent()->item_data(0) != item_in_selection->parent()->parent()->item_data(0)) continue;

					item_deselected.append(	QItemSelectionRange(selected_index_list.at(i))  );
					item_deselected.append(	QItemSelectionRange(selected_index_list.at(i).parent())  );

				}
				item_selected.append(QItemSelectionRange(index) );
				item_selected.append(QItemSelectionRange(index.parent()) );  // group
				item_selected.append(QItemSelectionRange(index.parent().parent()) );  // grid
			}
			QItemSelectionModel::select(item_deselected,QItemSelectionModel::Deselect);
			QItemSelectionModel::select(item_selected,QItemSelectionModel::Select);

			selections_ = this->selection();
			return;
		}

		Grid_region* region = dynamic_cast<Grid_region*>(item);
		if(region) {
			if(is_selected) {

				item_deselected.append(	QItemSelectionRange(index) );
				item_deselected.append(	QItemSelectionRange(index.parent()) ); // remove the group

			}
			else {  // Need to check if another region was selected
				QModelIndexList selected_index_list = 	this->selectedIndexes();
				for(int i=0; i<selected_index_list.size(); ++i) {

					GsTL_object_item* item_in_selection = static_cast<GsTL_object_item*>(proxy_model_->mapToSource(selected_index_list.at(i)).internalPointer());
          //GsTL_object_item* item_in_selection = static_cast<GsTL_object_item*>(selected_index_list.at(i).internalPointer());
					Grid_region* region_in_selection = dynamic_cast<Grid_region*>(item_in_selection);
					if(region_in_selection == 0) continue;
					// Does it has the same grid parent
					if(item->parent()->parent()->item_data(0) != item_in_selection->parent()->parent()->item_data(0)) continue;

					item_deselected.append(	QItemSelectionRange(selected_index_list.at(i))  );
				}
				item_selected.append(QItemSelectionRange(index) );
				item_selected.append(QItemSelectionRange(index.parent()) );
			}
			QItemSelectionModel::select(item_deselected,QItemSelectionModel::Deselect);
			QItemSelectionModel::select(item_selected,QItemSelectionModel::Select);

			selections_ = this->selection();
			return;
		}


	}
}
Esempio n. 27
0
QItemSelection FlatProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const
{
    QList<_RangeRect> newRanges;
    QHash<QModelIndex, SourceItem *> itemLookup;
    // basics steps of the loop:
    // 1. convert each source ItemSelectionRange to a mapped Range (internal one for easier post processing)
    // 2. insert it into the list of previously mapped Ranges sorted by top location
    for (int i = 0; i < sourceSelection.count(); i++) {
        const QItemSelectionRange &currentRange = sourceSelection[i];
        QModelIndex currentParent = currentRange.topLeft().parent();
        Q_ASSERT(currentParent == currentRange.bottomRight().parent());

        SourceItem *parentItem = 0;
        if (!itemLookup.contains(currentParent)) {
            parentItem = sourceToInternal(currentParent);
            itemLookup[currentParent] = parentItem;
        }
        else {
            parentItem = itemLookup[currentParent];
        }

        _RangeRect newRange = { currentRange.topLeft().column(), currentRange.bottomRight().column(),
                                currentRange.topLeft().row(), currentRange.bottomRight().row(),
                                parentItem->child(currentRange.topLeft().row()), parentItem->child(currentRange.bottomRight().row()) };

        if (newRanges.isEmpty()) {
            newRanges << newRange;
            continue;
        }

        _RangeRect &first = newRanges[0];
        if (newRange < first) {
            newRanges.prepend(newRange);
            continue;
        }

        bool inserted = false;
        for (int j = 0; j < newRanges.count() - 1; j++) {
            _RangeRect &a = newRanges[j];
            _RangeRect &b = newRanges[j + 1];

            if (a < newRange && newRange < b) {
                newRanges[j + 1] = newRange;
                inserted = true;
                break;
            }
        }
        if (inserted)
            continue;

        _RangeRect &last = newRanges[newRanges.count() - 1];
        if (last < newRange) {
            newRanges.append(newRange);
            continue;
        }

        Q_ASSERT(false);
    }

    // we've got a sorted list of ranges now. so we can easily check if there is a possibility to merge
    for (int i = newRanges.count() - 1; i > 0; i--) {
        _RangeRect &a = newRanges[i - 1];
        _RangeRect &b = newRanges[i];
        if (a.left != b.left || a.right != b.right)
            continue;

        if (a.bottom < b.top - 1) {
            continue;
        }

        // all merge checks passed!
        if (b.bottom > a.bottom) {
            a.bottom = b.bottom;
            a.bottomItem = b.bottomItem;
        } // otherwise b is totally enclosed in a -> nothing to do but drop b.
        newRanges.removeAt(i);
    }

    QItemSelection proxySelection;
    for (int i = 0; i < newRanges.count(); i++) {
        _RangeRect &r = newRanges[i];
        proxySelection << QItemSelectionRange(createIndex(r.top, r.left, r.topItem), createIndex(r.bottom, r.right, r.bottomItem));
    }
    return proxySelection;
}