diff --git a/src/Editor/KSImageEditor.cpp b/src/Editor/KSImageEditor.cpp index b0855c3..894a358 100644 --- a/src/Editor/KSImageEditor.cpp +++ b/src/Editor/KSImageEditor.cpp @@ -1,124 +1,125 @@ /* * Copyright (C) 2015 Boudhayan Gupta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KSImageEditor.h" // the image provider first KSImageProvider::KSImageProvider(const QPixmap &pixmap) : QQuickImageProvider(QQuickImageProvider::Pixmap), mPixmap(pixmap) {} QPixmap KSImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) { Q_UNUSED(id); if (size) { *size = mPixmap.size(); } if (requestedSize.isEmpty()) { return mPixmap; } return mPixmap.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); } QPixmap KSImageProvider::pixmap() const { return mPixmap; } // now the editor KSImageEditor::KSImageEditor(const QPixmap &pixmap, QObject *parent) : QObject(parent), mQuickView(new QQuickView), mImageProvider(new KSImageProvider(pixmap)) { QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection); } KSImageEditor::~KSImageEditor() { if (mQuickView->visibility() != QQuickView::Hidden) { mQuickView->hide(); } if (mQuickView) { delete mQuickView; } } void KSImageEditor::init() { // set up the KDeclarative bindings for the QQuickView KDeclarative::KDeclarative decl; decl.setDeclarativeEngine(mQuickView->engine()); decl.setupBindings(); // add in the screenshot imageprovider mQuickView->engine()->addImageProvider("screenshot", mImageProvider); qmlRegisterType("KSComponents", 2, 0, "KSCropRubberBand"); // set up the window mQuickView->setResizeMode(QQuickView::SizeViewToRootObject); mQuickView->setFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool); mQuickView->setClearBeforeRendering(true); mQuickView->setSource(QUrl("qrc:///RegionGrabber.qml")); // connect connect(mQuickView->engine(), &QQmlEngine::quit, this, &KSImageEditor::grabCanceled); connect(mQuickView, &QQuickView::statusChanged, this, &KSImageEditor::waitForViewReady); } void KSImageEditor::waitForViewReady(QQuickView::Status status) { switch(status) { case QQuickView::Ready: { QQuickItem *rootItem = mQuickView->rootObject(); connect(rootItem, SIGNAL(editCanceled()), this, SIGNAL(grabCanceled())); - // fixme: connect(rootItem, SIGNAL(selectionConfirmed(int,int,int,int)), this, SLOT(selectConfirmedHandler(int,int,int,int))); + connect(rootItem, SIGNAL(editAccepted(int,int,int,int)), this, SLOT(selectConfirmedHandler(int,int,int,int))); mQuickView->showFullScreen(); return; } case QQuickView::Error: emit grabCanceled(); return; case QQuickView::Null: case QQuickView::Loading: return; } } void KSImageEditor::selectConfirmedHandler(int x, int y, int width, int height) { + QPixmap pixmap = QPixmap::fromImage(mQuickView->grabWindow()); mQuickView->hide(); QRect region(x, y, width, height); - emit grabAccepted(mImageProvider->pixmap().copy(region), region); + emit grabAccepted(pixmap.copy(region), region); } diff --git a/src/Editor/KSImageEditor.h b/src/Editor/KSImageEditor.h index 2ec41a4..a6e5d64 100644 --- a/src/Editor/KSImageEditor.h +++ b/src/Editor/KSImageEditor.h @@ -1,81 +1,80 @@ /* * Copyright (C) 2015 Boudhayan Gupta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KSIMAGEEDITOR_H #define KSIMAGEEDITOR_H #include #include #include #include #include #include #include #include #include #include #include #include #include "KSCropRubberBand.h" class KSImageProvider : public QQuickImageProvider { public: explicit KSImageProvider(const QPixmap &Pixmap); QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize); QPixmap pixmap() const; private: QPixmap mPixmap; }; class KSImageEditor : public QObject { Q_OBJECT public: explicit KSImageEditor(const QPixmap &pixmap, QObject *parent = 0); ~KSImageEditor(); signals: void grabAccepted(const QPixmap &pixmap, const QRect ®ion); void grabCanceled(); private slots: - void init(); void waitForViewReady(QQuickView::Status status); void selectConfirmedHandler(int x, int y, int width, int height); private: QQuickView *mQuickView; KSImageProvider *mImageProvider; }; #endif // KSIMAGEEDITOR_H diff --git a/src/Editor/QmlResources/RegionGrabber.qml b/src/Editor/QmlResources/RegionGrabber.qml index da27bb0..9f1267e 100644 --- a/src/Editor/QmlResources/RegionGrabber.qml +++ b/src/Editor/QmlResources/RegionGrabber.qml @@ -1,89 +1,76 @@ /* * Copyright (C) 2015 Boudhayan Gupta * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ import QtQuick 2.4 -import QtQuick.Layouts 1.1 -import QtQuick.Controls 1.3 -import QtQuick.Dialogs 1.2 - import KSComponents 2.0 Image { id: rootItem; signal editCanceled; - signal editAccepted; + signal editAccepted(int x, int y, int width, int height); property var toolOptionsToolbar: null; property var currentGraphicElement: null; + function prepareEditAccepted() { + if (workArea.tool !== null) { + workArea.tool.visible = false; + workArea.tool.destroy(); + } + + editActionsToolbar.visible = false; + editAccepted(x, y, width, height); + } focus: true; Keys.onEscapePressed: editCanceled(); source: "image://screenshot/image"; - Component { - id: toolBarComponent; - - ColorDialog { - id: colorDialog - title: "Please choose a color" - onAccepted: { - console.log("You chose: " + colorDialog.color) - Qt.quit() - } - onRejected: { - console.log("Canceled") - Qt.quit() - } - Component.onCompleted: visible = true - } - } - Item { id: workArea; anchors.fill: parent; property var tool: null; } Component { id: rectangleTool; KSRectangleTool {} } KSEditActionsToolBar { id: editActionsToolbar; anchors.bottom: parent.bottom; anchors.right: parent.right; anchors.margins: 10; - onDone: editAccepted(); + onDone: prepareEditAccepted(); onCancel: editCanceled(); onToolSelected: { if (workArea.tool !== null) { workArea.tool.destroy(); } if (toolName == "rectangle") { workArea.tool = rectangleTool.createObject(workArea, {"parentItem" : workArea}); } } } }