void Test_LayerScene::test_addLayers()
{
    QList<LayerRef> layers;

    for (int i = 0; i < 4; ++i)
        layers << std::make_shared<RasterLayer>("layer" + QString::number(i));

    auto doc = new Document("temp", QSize(400, 300), { layers[0], layers[1] });

    auto dir = doc->layerScene()->rootLayer();
    doc->layerScene()->addLayers({layers[2], layers[3]}, dir, 1, "test");

    QCOMPARE(dir->count(), 4);
    QCOMPARE(dir->child(0)->name(), QString("layer0"));
    QCOMPARE(dir->child(1)->name(), QString("layer2"));
    QCOMPARE(dir->child(2)->name(), QString("layer3"));
    QCOMPARE(dir->child(3)->name(), QString("layer1"));

    doc->undoStack()->undo();

    QCOMPARE(dir->count(), 2);
    QCOMPARE(dir->child(0)->name(), QString("layer0"));
    QCOMPARE(dir->child(1)->name(), QString("layer1"));

    doc->deleteLater();
}
Exemple #2
0
void RectTool::startAdding()
{
	auto createNew = [this]()
	{
		SP<AbstractRectLayer> layer;
		
		if (d->addingType == RectTool::AddText)
		{
			auto textLayer = makeSP<TextLayer>();
			textLayer->setText(tr("Text"));
			textLayer->setName(tr("Text"));
			
			layer = textLayer;
			
			QFont font;
			font.setPointSize(36);
			
			layer->setFillBrush(Color::fromRgbValue(0, 0, 0));
			layer->setStrokeBrush(Color::fromRgbValue(0, 0, 0));
			layer->setStrokeEnabled(false);
			textLayer->setFont(font);
		}
		else
		{
			layer = makeSP<RectLayer>();
			
			if (d->addingType == RectTool::AddEllipse)
			{
				layer->setShapeType(AbstractRectLayer::ShapeTypeEllipse);
				layer->setName(tr("Ellipse"));
			}
			else
			{
				layer->setShapeType(AbstractRectLayer::ShapeTypeRect);
				layer->setName(tr("Rectangle"));
			}
			
			layer->setFillBrush(Color::fromRgbValue(0.5, 0.5, 0.5));
			layer->setStrokeBrush(Color::fromRgbValue(0, 0, 0));
		}
		
		return layer;
	};
	
	d->layerToAdd = createNew();
	
	auto current = layerScene()->current();
	if (current)
	{
		d->layerToAddParent = current->parent();
		d->layerToAddIndex = current->index();
	}
	else
	{
		d->layerToAddParent = layerScene()->rootLayer();
		d->layerToAddIndex = 0;
	}
	
	addLayerInsertion(d->layerToAddParent, d->layerToAddIndex, d->layerToAdd);
}
void Test_LayerScene::test_moveLayers_sibling()
{
    QList<LayerRef> layers;

    for (int i = 0; i < 4; ++i)
        layers << std::make_shared<RasterLayer>("layer" + QString::number(i));

    auto doc = new Document("temp", QSize(400, 300), layers);
    auto dir = doc->layerScene()->rootLayer();

    doc->layerScene()->moveLayers( { dir->child(0), dir->child(2) }, dir, 0);

    QCOMPARE(dir->count(), 4);
    QCOMPARE(dir->child(0)->name(), QString("layer0"));
    QCOMPARE(dir->child(1)->name(), QString("layer2"));
    QCOMPARE(dir->child(2)->name(), QString("layer1"));
    QCOMPARE(dir->child(3)->name(), QString("layer3"));

    doc->undoStack()->undo();

    QCOMPARE(dir->count(), 4);
    QCOMPARE(dir->child(0)->name(), QString("layer0"));
    QCOMPARE(dir->child(1)->name(), QString("layer1"));
    QCOMPARE(dir->child(2)->name(), QString("layer2"));
    QCOMPARE(dir->child(3)->name(), QString("layer3"));
}
void Test_LayerScene::test_moveLayers()
{
    auto group = std::make_shared<GroupLayer>("group");
    auto layer0 = std::make_shared<RasterLayer>("layer0");
    auto layer1 = std::make_shared<RasterLayer>("layer1");

    auto doc = new Document("temp", QSize(400, 300), { layer0, group, layer1 });
    auto dir = doc->layerScene()->rootLayer();
    doc->layerScene()->moveLayers( { dir->child(0), dir->child(2) }, dir->child(1), 0 );

    qDebug() << dir->child(0);

    QCOMPARE(dir->count(), 1);
    QCOMPARE(dir->child(0)->name(), QString("group"));
    QCOMPARE(dir->child(0)->count(), 2);
    QCOMPARE(dir->child(0)->child(0)->name(), QString("layer0"));
    QCOMPARE(dir->child(0)->child(1)->name(), QString("layer1"));

    doc->undoStack()->undo();

    QCOMPARE(dir->count(), 3);
    QCOMPARE(dir->child(0)->name(), QString("layer0"));
    QCOMPARE(dir->child(1)->name(), QString("group"));
    QCOMPARE(dir->child(2)->name(), QString("layer1"));
}
Exemple #5
0
int RectTool::cursorPressEvent(CanvasCursorEvent *event)
{
	if (d->mode != NoOperation)
		return 0;
	
	// pass event to the graphics item
	
	auto item = graphicsItem()->scene()->itemAt(event->viewPos, QTransform());
	if (item && item != d->frameItem)
	{
		event->ignore();
		return 0;
	}
	
	d->updateManager->setEnabled(true);
	
	d->clickedLayer = layerScene()->rootLayer()->descendantAt(event->data.pos.toQPoint(), handleRadius);
	d->clickedWithShift = event->modifiers() & Qt::ShiftModifier;
	
	if (d->selectingMode == SelectImmediately)
	{
		// select immediately the clicked layer
		selectLayer(d->clickedLayer, d->clickedWithShift);
	}
	else
	{
		// the clicked layer is already selected, selection is unchanged and current is set to the clicked layer
		if (layerScene()->selection().contains(d->clickedLayer))
		{
			layerScene()->setCurrent(d->clickedLayer);
		}
		// clear selection to insert a new layer
		else
		{
			layerScene()->setSelection({});
		}
	}
	
	// decide mode
	
	// if already layers are selected and a layer is clicked, mode will be Dragging
	if (d->selectedLayerInfos.size() && d->clickedLayer)
		d->mode = Dragging;
	// if no layer is selected or no layer is clicked, go to Inserting mode if addingType is not NoAdding
	else if (d->addingType != NoAdding)
		d->mode = Inserting;
	
	// there is nothing to do
	if (d->mode == NoOperation)
		return 0;
	
	// save drag origin
	d->dragDistanceEnough = false;
	d->dragStartPos = event->data.pos;

	return 0;
}
Exemple #6
0
void RectTool::finishAdding()
{
	PAINTFIELD_DEBUG;
	
	clearLayerInsertions();
	layerScene()->addLayers({d->layerToAdd}, d->layerToAddParent, d->layerToAddIndex, tr("Add Rectangle"));
	layerScene()->setCurrent(d->layerToAdd);
	layerScene()->setSelection({d->layerToAdd});
	d->layerToAdd = nullptr;
}
Exemple #7
0
void RectTool::commit()
{
	// setting property 
	for (const auto &info : d->selectedLayerInfos)
	{
		if (info.rectLayer)
			layerScene()->setLayerProperty(info.original, info.rectLayer->rect(), RoleRect, tr("Change Rect"), false);
		else if (info.original->isType<RasterLayer>())
			layerScene()->editLayer(info.original, new LayerMoveEdit(info.rasterOffset), tr("Move Layer"));
	}
}
Exemple #8
0
void ShapeSideBar::onTextAlignmentChanged(int changedAlignmentInt)
{
	if (d->textLayer)
	{
		auto alignment = d->textLayer->alignment();
		auto changedAlignment = Qt::Alignment(changedAlignmentInt);
		
		switch (changedAlignment)
		{
			case Qt::AlignLeft:
			case Qt::AlignHCenter:
			case Qt::AlignRight:
			{
				alignment &= ~(Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight);
				alignment |= changedAlignment;
				break;
			}
			case Qt::AlignTop:
			case Qt::AlignVCenter:
			case Qt::AlignBottom:
			{
				alignment &= ~(Qt::AlignTop | Qt::AlignVCenter | Qt::AlignBottom);
				alignment |= changedAlignment;
				break;
			}
			default:
				return;
		}
		
		layerScene()->setLayerProperty(d->textLayer, int(alignment), RoleTextAlignment, tr("Change Text Alignment"));
	}
}
Exemple #9
0
void ShapeSideBar::onTextChanged()
{
	if (d->textLayer)
	{
		auto text = d->textEdit->toPlainText();
		layerScene()->setLayerProperty(d->textLayer, text, RoleText, tr("Change Text"));
	}
}
void Test_LayerScene::test_setLayerProperty()
{
    auto doc = new Document("temp", QSize(400, 300), {std::make_shared<RasterLayer>("layer")});

    auto dir = doc->layerScene()->rootLayer();
    auto layer = dir->child(0);
    doc->layerScene()->setLayerProperty(layer, "newname", RoleName);
    QCOMPARE(layer->name(), QString("newname"));
    doc->undoStack()->undo();
    QCOMPARE(layer->name(), QString("layer"));

    doc->layerScene()->setLayerProperty(layer, true, RoleLocked);
    QCOMPARE(layer->isLocked(), true);

    doc->layerScene()->setLayerProperty(layer, "newname", RoleName);
    QCOMPARE(layer->name(), QString("layer"));
}
Exemple #11
0
void ShapeSideBar::onFontItalicChanged(bool italic)
{
	if (d->textLayer)
	{
		auto font = d->textLayer->font();
		font.setItalic(italic);
		layerScene()->setLayerProperty(d->textLayer, font, RoleFont, tr("Change Font Italic"));
	}
}
Exemple #12
0
void ShapeSideBar::onWidthChanged(int w)
{
	if (d->rectLayer)
	{
		QRectF rect = d->rectLayer->rect();
		rect.setWidth(w);
		layerScene()->setLayerProperty(d->rectLayer, rect, RoleRect, tr("Change Rectangle Width"));
	}
}
Exemple #13
0
void ShapeSideBar::onHeightChanged(int h)
{
	if (d->rectLayer)
	{
		QRectF rect = d->rectLayer->rect();
		rect.setHeight(h);
		layerScene()->setLayerProperty(d->rectLayer, rect, RoleRect, tr("Change Rectangle Height"));
	}
}
Exemple #14
0
void ShapeSideBar::onYChanged(int y)
{
	if (d->rectLayer)
	{
		QRectF rect = d->rectLayer->rect();
		rect.moveTop(y);
		layerScene()->setLayerProperty(d->rectLayer, rect, RoleRect, tr("Change Rectangle y"));
	}
}
Exemple #15
0
void ShapeSideBar::onXChanged(int x)
{
	if (d->rectLayer)
	{
		QRectF rect = d->rectLayer->rect();
		rect.moveLeft(x);
		layerScene()->setLayerProperty(d->rectLayer, rect, RoleRect, tr("Change Rectangle x"));
	}
}
Exemple #16
0
void RectTool::selectLayer(const LayerConstRef &layer, bool isShiftPressed)
{
	auto selection = layerScene()->selection();
	
	if (!selection.contains(layer))
	{
		if (isShiftPressed)
		{
			selection << layer;
			layerScene()->setSelection(selection);
		}
		else
		{
			layerScene()->setSelection({layer});
		}
	}
	
	layerScene()->setCurrent(layer);
}
Exemple #17
0
RectTool::RectTool(AddingType type, Canvas *canvas) :
	Tool(canvas),
	d(new Data)
{
	d->layerController = canvas->findChild<LayerUIController *>();
	d->updateManager = new CanvasUpdateManager(this);
	connect(d->updateManager, SIGNAL(updateTilesRequested(QPointSet)), this, SIGNAL(requestUpdate(QPointSet)));
	
	// set modes
	
	d->addingType = type;
	
	if (d->addingType == NoAdding)
		d->selectingMode = SelectImmediately;
	else
		d->selectingMode = SelectLater;
	
	// create graphics items
	{
		auto group = new QGraphicsItemGroup();
		group->setHandlesChildEvents(false);
		setGraphicsItem(group);
		
		{
			auto frame = new FrameGraphicsItem(group);
			d->frameItem = frame;
		}
	}
	
	addHandle(Top | Left, 1);
	addHandle(Top | Right, 1);
	addHandle(Bottom | Left, 1);
	addHandle(Bottom | Right, 1);
	addHandle(Top, 0);
	addHandle(Bottom, 0);
	addHandle(Left, 0);
	addHandle(Right, 0);
	
	connect(layerScene(), SIGNAL(selectionChanged(QList<LayerConstRef>,QList<LayerConstRef>)), this, SLOT(updateSelected()));
	connect(layerScene(), SIGNAL(layerChanged(LayerConstRef)), this, SLOT(updateLayer(LayerConstRef)));
	connect(canvas, SIGNAL(transformsChanged(SP<const CanvasTransforms>)), this, SLOT(updateGraphicsItems()));
	updateSelected();
}
Exemple #18
0
void ShapeSideBar::onFontSelectRequested()
{
	if (d->textLayer)
	{
		bool ok;
		
		auto font = QFontDialog::getFont(&ok, d->textLayer->font(), 0, tr("Select Font"), QFontDialog::DontUseNativeDialog);
		if (ok)
			layerScene()->setLayerProperty(d->textLayer, font, RoleFont, tr("Change Font"));
	}
}
Exemple #19
0
void RectTool::updateSelected()
{
	auto layers = layerScene()->selection();
	
	clearLayerDelegation();
	
	d->selectedLayerInfos.clear();
	
	for (const auto &layer : layers)
	{
		Data::LayerInfo info;
		info.setOriginalLayer(layer);
		
		if (layer->isType<AbstractRectLayer>() || layer->isType<RasterLayer>())
			addLayerDelegation(layer);
		
		d->selectedLayerInfos << info;
	}
	
	updateGraphicsItems();
}