Ejemplo n.º 1
0
void QgsComposerLegendWidget::on_mMoveUpToolButton_clicked()
{
  if ( !mLegend )
  {
    return;
  }

  QStandardItemModel* itemModel = qobject_cast<QStandardItemModel *>( mItemTreeView->model() );
  if ( !itemModel )
  {
    return;
  }

  QModelIndex currentIndex = mItemTreeView->currentIndex();
  if ( !currentIndex.isValid() )
  {
    return;
  }

  mLegend->beginCommand( "Moved legend item up" );
  //is there an older sibling?
  int row = currentIndex.row();
  QModelIndex olderSibling = currentIndex.sibling( row - 1, 0 );

  if ( !olderSibling.isValid() )
  {
    return;
  }

  QModelIndex parentIndex = currentIndex.parent();
  QList<QStandardItem*> itemToMove;
  QList<QStandardItem*> olderSiblingItem;

  if ( !parentIndex.isValid() ) //move toplevel item
  {
    itemToMove = itemModel->takeRow( row );
    olderSiblingItem = itemModel->takeRow( row - 1 );
    itemModel->insertRow( row - 1, itemToMove );
    itemModel->insertRow( row, olderSiblingItem );

  }
  else //move classification items
  {
    QStandardItem* parentItem = itemModel->itemFromIndex( parentIndex );
    itemToMove = parentItem->takeRow( row );
    olderSiblingItem = parentItem->takeRow( row - 1 );
    parentItem->insertRow( row - 1, itemToMove );
    parentItem->insertRow( row, olderSiblingItem );
  }

  mItemTreeView->setCurrentIndex( itemModel->indexFromItem( itemToMove.at( 0 ) ) );
  mLegend->update();
  mLegend->endCommand();
}
void QgsComposerLegendWidget::on_mMoveDownPushButton_clicked()
{
  QStandardItemModel* itemModel = dynamic_cast<QStandardItemModel*>( mItemTreeView->model() );
  if ( !itemModel )
  {
    return;
  }

  QModelIndex currentIndex = mItemTreeView->currentIndex();
  if ( !currentIndex.isValid() )
  {
    return;
  }

  //is there an older sibling?
  int row = currentIndex.row();
  QModelIndex youngerSibling = currentIndex.sibling( row + 1, 0 );

  if ( !youngerSibling.isValid() )
  {
    return;
  }

  QModelIndex parentIndex = currentIndex.parent();
  QList<QStandardItem*> itemToMove;
  QList<QStandardItem*> youngerSiblingItem;

  if ( !parentIndex.isValid() ) //move toplevel (layer) item
  {
    youngerSiblingItem = itemModel->takeRow( row + 1 );
    itemToMove = itemModel->takeRow( row );
    itemModel->insertRow( row, youngerSiblingItem );
    itemModel->insertRow( row + 1, itemToMove );
  }
  else //move child (classification) item
  {
    QStandardItem* parentItem = itemModel->itemFromIndex( parentIndex );
    youngerSiblingItem = parentItem->takeRow( row + 1 );
    itemToMove = parentItem->takeRow( row );
    parentItem->insertRow( row, youngerSiblingItem );
    parentItem->insertRow( row + 1, itemToMove );
  }

  mItemTreeView->setCurrentIndex( itemModel->indexFromItem( itemToMove.at( 0 ) ) );
  if ( mLegend )
  {
    mLegend->update();
  }
}
Ejemplo n.º 3
0
bool QgsLegendModel::removeRows( int row, int count, const QModelIndex & parent )
{
  if ( count < 1 )
  {
    return false;
  }

  if ( parent.isValid() )
  {
    for ( int i = row + count - 1; i >= row; --i )
    {
      QStandardItem* item = itemFromIndex( parent );
      if ( item )
      {
        item->takeRow( i );
      }
    }
  }
  else
  {
    for ( int i = row + count - 1; i >= row; --i )
    {
      takeRow( i );
    }
  }
  return true;
}
Ejemplo n.º 4
0
void
mail_item_model::remove_msg(mail_msg* msg)
{
  DBG_PRINTF(8, "remove_msg(mail_id=" MAIL_ID_FMT_STRING ")", msg->get_id());
  QMap<mail_id_t, QStandardItem*>::iterator it = items_map.find(msg->get_id());
  if (it!=items_map.end()) {
    QStandardItem* item = it.value();
    QStandardItem* parent_item = item->parent();
    while (item->child(0,0)!=NULL) {
      // reparent childs of the item to remove
      QList<QStandardItem*> ql = item->takeRow(0);
      if (parent_item) {
	DBG_PRINTF(9, "reparenting child to immediate parent row=%d", item->index().row());
	parent_item->insertRow(item->index().row(), ql);
      }
      else {
	DBG_PRINTF(9, "reparenting child to root");
	insertRow(item->index().row(), ql);
      }
    }
    // 
    DBG_PRINTF(9, "removing mail_id=" MAIL_ID_FMT_STRING
	       " from model at index.row=%d index.column=%d",
	       msg->get_id(), item->index().row(),
	       item->index().column());
    QModelIndex index = item->index();
    removeRow(index.row(), index.parent());
    items_map.erase(it);
  }
  else {
    DBG_PRINTF(1, "ERR: mail_id=" MAIL_ID_FMT_STRING " not found in items_map", msg->get_id());
  }
}
void list_handling::compare_downloads(QModelIndex &index, std::vector<package>::iterator &new_it, std::vector<package>::iterator &old_it, vector<view_info> &info){
	int dl_line = 0;
	vector<download>::iterator old_dit = old_it->dls.begin();
	vector<download>::iterator new_dit = new_it->dls.begin();
	vector<view_info>::iterator vit;
	QStandardItem *pkg;
	QStandardItem *dl;

	pkg = list_model->itemFromIndex(index);
	if(pkg == NULL)
		return;

	// compare every single download of the package
	while((old_dit != old_it->dls.end()) && (new_dit != new_it->dls.end())){
		compare_one_download(*new_dit, *old_dit, pkg, dl_line);

		// recreate selection if existed
		for(vit = info.begin(); vit != info.end(); ++vit){
			if((vit->id == new_dit->id) && !(vit->package)){
				if(vit->selected){ // download is selected
					for(int i=0; i<5; ++i)
						selection_model->select(index.child(dl_line, i), QItemSelectionModel::Select);

					selected_downloads_size += (double)new_dit->size / 1048576;
					++selected_downloads_count;
					break;
				}
			}
		}

		++old_dit;
		++new_dit;
		++dl_line;
	}

	if(old_dit != old_it->dls.end()){ // there are more old lines than new ones
		while(old_dit != old_it->dls.end()){

			// delete packages out of model
			for(int i=0; i<5; ++i){
				dl = pkg->takeChild(dl_line, i);
				delete dl;
			}

			pkg->takeRow(dl_line);
			++old_dit;
		}

	}else if(new_dit != new_it->dls.end()){ // there are more new lines than old ones
		while(new_dit != new_it->dls.end()){
			// insert new download linkes
			create_new_download(*new_dit, pkg, dl_line);

			++dl_line;
			++new_dit;
		}
		list->collapse(index);
		list->expand(index);
	}
}
void AbstractCameraManager::removeGroup(QModelIndex index) {
    QStandardItem * item = getModel()->itemFromIndex( index );
    if( !item->isEditable() ) return;

    QStandardItem * parent = item->parent();
    if( parent == NULL ) parent = item->model()->invisibleRootItem();

    for(int i=item->rowCount(); i>0; i--) {
        newCameraList.insertRow(0, item->takeRow(0));
    }

    parent->removeRow( item->row() );
    detectNewCamerasAndExpand();
}
/**
  Updates the sibling position of the item, depending on the position in the model.
  */
void NavigatorTreeModel::updateItemRowOrder(const ModelNode &node)
{
    if (!containsNode(node))
        return;

    ItemRow itemRow = itemRowForNode(node);
    int currentRow = itemRow.idItem->row();
    int newRow = currentRow;
    if (node.parentProperty().parentModelNode().isValid())
        newRow = modelNodeChildren(node.parentProperty().parentModelNode()).indexOf(node);
    Q_ASSERT(newRow >= 0);

    if (currentRow != newRow) {
        QStandardItem *parentIdItem = itemRow.idItem->parent();
        QList<QStandardItem*> items = parentIdItem->takeRow(currentRow);
        parentIdItem->insertRow(newRow, items);
    }
}
Ejemplo n.º 8
0
void EventViewer::deleteAllChildren(QStandardItem * parent)
{
    //delete all children of eventsItem;
    QStandardItem * loopItem = parent; //main loop item
    QList<QStandardItem *> carryItems; //Last In First Out stack of items
    QList<QStandardItem *> itemsToBeDeleted; //List of items to be deleted
    while (loopItem->rowCount())
    {
        itemsToBeDeleted << loopItem->takeRow(0);
        //if the row removed has children:
        if (itemsToBeDeleted.at(0)->hasChildren())
        {
            carryItems << loopItem;                 //put on the stack the current loopItem
            loopItem = itemsToBeDeleted.at(0);      //set the row with children as the loopItem
        }
        //if current loopItem has no more rows but carryItems list is not empty:
        if (!loopItem->rowCount() && !carryItems.isEmpty()) loopItem = carryItems.takeFirst();

    }
    qDeleteAll(itemsToBeDeleted);

}
void QgsEffectStackPropertiesWidget::moveEffectByOffset( int offset )
{
  EffectItem *item = currentEffectItem();
  if ( !item )
    return;

  int row = item->row();

  QStandardItem* root = mModel->invisibleRootItem();

  int layerIdx = root->rowCount() - row - 1;
  // switch effects
  QgsPaintEffect* tmpEffect = mStack->takeEffect( layerIdx );
  mStack->insertEffect( layerIdx - offset, tmpEffect );

  QList<QStandardItem *> toMove = root->takeRow( row );
  root->insertRows( row + offset, toMove );

  QModelIndex newIdx = toMove[ 0 ]->index();
  mEffectsList->setCurrentIndex( newIdx );

  updatePreview();
  updateUi();
}
Ejemplo n.º 10
0
void FileOrganiserWidget::moveItem(QStandardItem *pItem,
                                   QStandardItem *pDropItem,
                                   const QAbstractItemView::DropIndicatorPosition &pDropPosition)
{
    if (!pDropItem)
        // pDropItem is not valid, so...

        return;

    // Move pItem above/on/below pDropItem, depending on the drop position, but
    // first, determine the item that will own pItem

    QStandardItem *crtParentItem = pItem->parent()?
                                       pItem->parent():
                                       mModel->invisibleRootItem();
    QStandardItem *newParentItem = parentItem(pDropItem, pDropPosition);

    // Second, check whether the (file) item points to a file which is already
    // owned by newParentItem

    bool fileAlreadyOwned = false;

    if (!pItem->data(Item::Folder).toBool())
        // The current item is a file item, so retrieve its file name and check
        // whether it's already owned by newParentItem

        fileAlreadyOwned = ownedBy(pItem->data(Item::Path).toString(), newParentItem);

    // Third, move pItem to newParentItem and this to the right place, depending
    // on the value of pDropPosition and only if the destination doesn't already
    // have that item (should it be a file item, since folder items are always
    // moved)

    if (!fileAlreadyOwned || (crtParentItem == newParentItem)) {
        // Either newParentItem doesn't already own an item which points to the
        // same file as pItem or pItem's current parent is the same as
        // newParentItem in which case it means that we want to move the item
        // within its current location

        // First, check whether the item is a folder and, if so, whether it's
        // expanded (and the same with any (in)direct child folder it may
        // contain)

        backupExpandedInformation(pItem);

        // Second, move the item (and any of its children)

        dropItems(pDropItem, pDropPosition, newParentItem,
                  crtParentItem->takeRow(pItem->row()));

        // Third, re-expand folders, if necessary

        restoreExpandedInformation(pItem);

        // Fourth, resize the widget, just in case the new location of the
        // item(s) requires more space than is visible

        resizeToContents();
    } else {
        // A (file) item pointing to the same file is already owned by
        // newParentItem, so just remove the item rather than move it

        crtParentItem->removeRow(pItem->row());
    }
}
Ejemplo n.º 11
0
void Model::apply(std::shared_ptr<Core::MutationInfo> mutation) noexcept
{
	emit documentMutated(&mutation->prev, &mutation->cur);

	// Update the pointers stored in the model items
	using NodeOrProperty = eggs::variant<NodePtr, PropertyPtr>;
	std::unordered_map<NodeOrProperty, NodeOrProperty> mutated;
	auto updatePointers = [&](auto& changes)
	{
		for (auto&& mut: changes)
		{
			if (mut.type != ChangeType::Mutated) continue;

			findItem(mut.prev)->update(mut.cur);
			mutated.insert({ mut.prev, mut.cur });
		}
	};
	updatePointers(mutation->nodes);
	updatePointers(mutation->properties);

	auto resolve = [&](auto item) -> decltype(item)
	{
		if (!item) return item;

		auto it = mutated.find(item);
#ifdef _MSC_VER
		if (it != cend(mutated)) return *it->second.target<decltype(item)>();
#else
		if (it != cend(mutated)) return *it->second.template target<decltype(item)>();
#endif
		return item;
	};

	enum class RowType { Node, Property };

	auto setRow = [&](QStandardItem* parent, size_t row, QList<QStandardItem*> items, RowType rowType)
	{
		if (rowType == RowType::Property)
		{
			// Properties are indexed from 0, but they should always go below the node children of the parent
			// So increase the row offset by the number of children the parent has
			row += mutation->cur.childCount(*ModelItem::node(parent));
		}

		// When inserting a new item, make sure we above any items that have a higher index in the actual document
		// If we are inserting a node, also make sure we stay above any properties
		auto maxRow = row;
		for (row = 0; row < maxRow; row++)
		{
			if (row == static_cast<size_t>(parent->rowCount())) break;

			auto child = parent->child(row);

			// Encountered a property, so stop
			if (rowType == RowType::Node && ModelItem::prop(child)) break;

			size_t index;

			// Encountered a property that has a higher index than this item, so stop
			auto prop = ModelItem::prop(child);
			if (prop)
			{
				auto propNode = mutation->cur.parent(*prop);
				// add node childCount because the properties start BELOW the child nodes
				index = mutation->cur.childIndex(*prop) + mutation->cur.childCount(*propNode);
				if (index > maxRow) break;
			}

			// Encountered a node that has a higher index than this item, so stop
			auto node = ModelItem::node(child);
			if (node)
			{
				index = mutation->cur.childIndex(*node);
				if (index > maxRow) break;
			}
		}

		//LOG->debug("row: {}, rowCount: {}", row, parent->rowCount());
		if (row <= static_cast<size_t>(parent->rowCount())) parent->insertRow(row, items);
		else parent->appendRow(items);
	};

	auto applyMutations = [&](auto& changes, RowType rowType, auto createItems, bool onlyRemove)
	{
		for (auto&& mut : changes)
		{
			QStandardItem* prevParentNode = findItem(resolve(mut.prevParent));
			if (!prevParentNode && rowType == RowType::Node) prevParentNode = invisibleRootItem();
			QStandardItem* curParentNode = findItem(resolve(mut.curParent));
			if (!curParentNode && rowType == RowType::Node) curParentNode = invisibleRootItem();

			switch (mut.type)
			{
			case ChangeType::Added:
			{
				if (onlyRemove) continue;
				LOG->debug("Adding at position {}: {}", mut.curIndex, *mut.cur);
				setRow(curParentNode, mut.curIndex, createItems(mut.cur), rowType);
				break;
			}
			case ChangeType::Removed:
			{
				if (!onlyRemove) continue;
				auto item = findItem(mut.prev);
				if (!item) continue; // maybe was already deleted when parent was removed
				auto childIndex = findChildIndex(prevParentNode, item);
				if (childIndex != -1) prevParentNode->removeRow(childIndex);
				LOG->debug("Removed at position {}: {}", childIndex, *mut.prev);
				break;
			}
			case ChangeType::Mutated:
			{
				if (onlyRemove) continue;
				assert(prevParentNode && curParentNode);

				LOG->debug("Mutating from position {} to position {}: {}", mut.prevIndex, mut.curIndex, *mut.cur);
				auto nodeOrProperty = resolve(mut.prev);
				auto item = findItem(nodeOrProperty);
				assert(item);
				auto prevIndex = findChildIndex(prevParentNode, item);
				assert(prevIndex != -1);

				if (prevIndex != mut.curIndex || prevParentNode != curParentNode)
				{
					// Move the row
					auto itemRowItems = prevParentNode->takeRow(prevIndex);
					setRow(curParentNode, mut.curIndex, itemRowItems, rowType);
				}

				break;
			}
			}
		}
	};

	auto createNodeItems = [&](NodePtr node)
	{
		QList<QStandardItem*> items;
		items << new ModelItem(this, node) << nullptr;
		return items;
	};
	auto createPropertyItems = [&](PropertyPtr prop)
	{
		QList<QStandardItem*> items;
		auto modelItem = new ModelItem(this, prop);
		items << modelItem << modelItem->propertyValueItem();
		return items;
	};

	applyMutations(mutation->nodes, RowType::Node, createNodeItems, false);
	applyMutations(mutation->properties, RowType::Property, createPropertyItems, false);
	applyMutations(mutation->nodes, RowType::Node, createNodeItems, true);
	applyMutations(mutation->properties, RowType::Property, createPropertyItems, true);
}