示例#1
0
bool ImageHandler::insertImage(GFile * file, double x, double y) {
	XOJ_CHECK_TYPE(ImageHandler);

	GError * err = NULL;
	GFileInputStream * in = g_file_read(file, NULL, &err);

	g_object_unref(file);

	GdkPixbuf * pixbuf = NULL;

	if (!err) {
		pixbuf = gdk_pixbuf_new_from_stream(G_INPUT_STREAM(in), NULL, &err);
		g_input_stream_close(G_INPUT_STREAM(in), NULL, NULL);
	} else {
		GtkWidget * dialog = gtk_message_dialog_new((GtkWindow*) *control->getWindow(), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
				_("This image could not be loaded. Error message: %s"), err->message);
		gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(this->control->getWindow()->getWindow()));
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
		g_error_free(err);
		return false;
	}

	Image * img = new Image();
	img->setX(x);
	img->setY(y);
	img->setImage(pixbuf);

	int width = gdk_pixbuf_get_width(pixbuf);
	int height = gdk_pixbuf_get_height(pixbuf);
	gdk_pixbuf_unref(pixbuf);

	double zoom = 1;

	PageRef page = view->getPage();

	if (x + width > page.getWidth() || y + height > page.getHeight()) {
		double maxZoomX = (page.getWidth() - x) / width;
		double maxZoomY = (page.getHeight() - y) / height;

		if (maxZoomX < maxZoomY) {
			zoom = maxZoomX;
		} else {
			zoom = maxZoomY;
		}
	}

	img->setWidth(width * zoom);
	img->setHeight(height * zoom);

	page.getSelectedLayer()->addElement(img);

	InsertUndoAction * insertUndo = new InsertUndoAction(page, page.getSelectedLayer(), img, view);
	control->getUndoRedoHandler()->addUndoAction(insertUndo);

	view->rerenderElement(img);

	return true;
}
示例#2
0
/**
 * Finishes all pending changes, move the elements, scale the elements and add
 * them to new layer if any or to the old if no new layer
 */
void EditSelection::finalizeSelection() {
	XOJ_CHECK_TYPE(EditSelection);

	PageView * v = getBestMatchingPageView();
	if (v == NULL) {
		this->view->getXournal()->deleteSelection(this);
	} else {
		PageRef page = this->view->getPage();
		Layer * layer = page.getSelectedLayer();
		this->contents->finalizeSelection(this->x, this->y, this->width, this->height, this->aspectRatio, layer, page, this->view, this->undo);

		this->view->rerenderRect(this->x, this->y, this->width, this->height);

		// This is needed if the selection not was 100% on a page
		this->view->getXournal()->repaintSelection(true);
	}
}
示例#3
0
void InputHandler::onButtonReleaseEvent(GdkEventButton * event, PageRef page) {
	XOJ_CHECK_TYPE(InputHandler);

	if (!this->tmpStroke) {
		return;
	}

	// Backward compatibility and also easier to handle for me;-)
	// I cannot draw a line with one point, to draw a visible line I need two points,
	// twice the same Point is also OK
	if (this->tmpStroke->getPointCount() == 1) {
		ArrayIterator<Point> it = this->tmpStroke->pointIterator();
		if (it.hasNext()) {
			this->tmpStroke->addPoint(it.next());
		}
		// No pressure sensitivity
		this->tmpStroke->clearPressure();
	}

	this->tmpStroke->freeUnusedPointItems();

	if (page.getSelectedLayerId() < 1) {
		// This creates a layer if none exists
		page.getSelectedLayer();
		page.setSelectedLayerId(1);
		xournal->getControl()->getWindow()->updateLayerCombobox();
	}

	Layer * layer = page.getSelectedLayer();

	UndoRedoHandler * undo = xournal->getControl()->getUndoRedoHandler();

	undo->addUndoAction(new InsertUndoAction(page, layer, this->tmpStroke, this->redrawable));

	ToolHandler * h = xournal->getControl()->getToolHandler();
	if (h->isShapeRecognizer()) {
		if (this->reco == NULL) {
			this->reco = new ShapeRecognizer();
		}
		ShapeRecognizerResult * result = this->reco->recognizePatterns(this->tmpStroke);

		if (result != NULL) {
			UndoRedoHandler * undo = xournal->getControl()->getUndoRedoHandler();

			Stroke * recognized = result->getRecognized();

			RecognizerUndoAction * recognizerUndo = new RecognizerUndoAction(page, this->redrawable, layer, this->tmpStroke, recognized);
			undo->addUndoAction(recognizerUndo);
			layer->addElement(result->getRecognized());

			Range range(recognized->getX(), recognized->getY());
			range.addPoint(recognized->getX() + recognized->getElementWidth(), recognized->getY() + recognized->getElementHeight());

			range.addPoint(this->tmpStroke->getX(), this->tmpStroke->getY());
			range.addPoint(this->tmpStroke->getX() + this->tmpStroke->getElementWidth(), this->tmpStroke->getY() + this->tmpStroke->getElementHeight());

			ListIterator<Stroke *> l = result->getSources();
			while (l.hasNext()) {
				Stroke * s = l.next();

				layer->removeElement(s, false);

				recognizerUndo->addSourceElement(s);

				range.addPoint(s->getX(), s->getY());
				range.addPoint(s->getX() + s->getElementWidth(), s->getY() + s->getElementHeight());
			}

			this->redrawable->rerenderRange(range);

			// delete the result object, this is not needed anymore, the stroke are not deleted with this
			delete result;
		} else {
			layer->addElement(this->tmpStroke);
			this->redrawable->rerenderElement(this->tmpStroke);
		}

	} else {
		layer->addElement(this->tmpStroke);
		this->redrawable->rerenderElement(this->tmpStroke);
	}

	this->tmpStroke = NULL;

	if (currentInputDevice == event->device) {
		currentInputDevice = NULL;
		INPUTDBG("currentInputDevice = NULL\n", 0);
	}

	this->tmpStrokeDrawElem = 0;
}