Commit 5bee9154 authored by Jouni Pentikäinen's avatar Jouni Pentikäinen

Add saving and loading sets of reference images

BUG: 393586
parent c977d72d
......@@ -262,6 +262,11 @@ void KisMimeDatabase::fillMimeData()
mimeType.suffixes = QStringList() << "kts";
s_mimeDatabase << mimeType;
mimeType.mimeType = "application/x-krita-reference-images";
mimeType.description = i18nc("description of a file type", "Krita Reference Image Collection");
mimeType.suffixes = QStringList() << "krf";
s_mimeDatabase << mimeType;
mimeType.mimeType = "image/x-krita-raw";
mimeType.description = i18nc("description of a file type", "Camera Raw Files");
mimeType.suffixes = QStringList() << "rw2" << "nef" << "cr2" << "sr2" << "crw" << "pef" << "x3f" << "kdc" << "mrw" << "arw" << "k25" << "dcr" << "orf" << "raw" << "raw" << "raf" << "srf" << "dng";
......
......@@ -88,6 +88,19 @@ KUndo2Command * KisReferenceImagesLayer::addReferenceImage(KisReferenceImage *re
return new AddReferenceImageCommand(this, referenceImage);
}
QVector<KisReferenceImage*> KisReferenceImagesLayer::referenceImages() const
{
QVector<KisReferenceImage*> references;
Q_FOREACH(auto shape, shapes()) {
KisReferenceImage *referenceImage = dynamic_cast<KisReferenceImage*>(shape);
if (referenceImage) {
references.append(referenceImage);
}
}
return references;
}
void KisReferenceImagesLayer::paintReferences(QPainter &painter) {
shapeManager()->paint(painter, *converter(), false);
}
......
......@@ -33,6 +33,7 @@ public:
KisReferenceImagesLayer(const KisReferenceImagesLayer &rhs);
KUndo2Command * addReferenceImage(KisReferenceImage *referenceImage);
QVector<KisReferenceImage*> referenceImages() const;
QColor getPixel(QPointF position) const;
......
......@@ -25,6 +25,7 @@ set ( defaulttools_SRCS
referenceimagestool/ToolReferenceImages.cpp
referenceimagestool/ToolReferenceImagesWidget.cpp
referenceimagestool/KisReferenceImageCollection.cpp
)
ki18n_wrap_ui(defaulttools_SRCS
......
/*
* Copyright (c) 2018 Jouni Pentikäinen <joupent@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 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 "KisReferenceImageCollection.h"
#include <QIODevice>
#include <libs/store/KoStore.h>
#include <KisReferenceImage.h>
#include <libs/store/KoStoreDevice.h>
const QString METADATA_FILE = "reference_images.xml";
KisReferenceImageCollection::KisReferenceImageCollection(const QVector<KisReferenceImage *> &references)
: references(references)
{}
const QVector<KisReferenceImage*> &KisReferenceImageCollection::referenceImages() const
{
return references;
}
bool KisReferenceImageCollection::save(QIODevice *io)
{
QScopedPointer<KoStore> store(KoStore::createStore(io, KoStore::Write, "application/x-krita-reference-images]", KoStore::Zip));
if (store.isNull()) return false;
QDomDocument doc;
QDomElement root = doc.createElement("referenceimages");
doc.insertBefore(root, QDomNode());
int nextId = 0;
Q_FOREACH(KisReferenceImage *reference, references) {
reference->saveXml(doc, root, nextId++);
if (reference->embed()) {
bool ok = reference->saveImage(store.data());
if (!ok) return false;
}
}
if (!store->open(METADATA_FILE)) {
return false;
}
KoStoreDevice xmlDev(store.data());
xmlDev.write(doc.toByteArray());
xmlDev.close();
store->close();
return true;
}
bool KisReferenceImageCollection::load(QIODevice *io)
{
QScopedPointer<KoStore> store(KoStore::createStore(io, KoStore::Read, "application/x-krita-reference-images", KoStore::Zip));
if (!store || store->bad()) {
return false;
}
if (!store->hasFile(METADATA_FILE) || !store->open(METADATA_FILE)) {
return false;
}
QByteArray xml = store->device()->readAll();
store->close();
QDomDocument doc;
doc.setContent(xml);
QDomElement root = doc.documentElement();
QDomElement element = root.firstChildElement("referenceimage");
while (!element.isNull()) {
KisReferenceImage *reference = KisReferenceImage::fromXml(element);
reference->loadImage(store.data());
references.append(reference);
element = element.nextSiblingElement("referenceimage");
}
return true;
}
/*
* Copyright (c) 2018 Jouni Pentikäinen <joupent@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 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 KISREFERENCEIMAGECOLLECTION_H
#define KISREFERENCEIMAGECOLLECTION_H
#include <QVector>
class QIODevice;
class KisReferenceImage;
class KisReferenceImageCollection
{
public:
explicit KisReferenceImageCollection() = default;
explicit KisReferenceImageCollection(const QVector<KisReferenceImage*> &references);
const QVector<KisReferenceImage*> &referenceImages() const;
bool save(QIODevice *io);
bool load(QIODevice *io);
private:
QVector<KisReferenceImage*> references;
};
#endif
......@@ -18,8 +18,11 @@
#include "ToolReferenceImages.h"
#include <QDesktopServices>
#include <QFile>
#include <QLayout>
#include <QMenu>
#include <QMessageBox>
#include <QVector>
#include <KoShapeRegistry.h>
#include <KoShapeManager.h>
......@@ -33,6 +36,7 @@
#include <KisReferenceImagesLayer.h>
#include "ToolReferenceImagesWidget.h"
#include "KisReferenceImageCollection.h"
ToolReferenceImages::ToolReferenceImages(KoCanvasBase * canvas)
: DefaultTool(canvas)
......@@ -88,10 +92,71 @@ void ToolReferenceImages::removeAllReferenceImages()
void ToolReferenceImages::loadReferenceImages()
{
auto kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
KIS_ASSERT_RECOVER_RETURN(kisCanvas)
KoFileDialog dialog(kisCanvas->viewManager()->mainWindow(), KoFileDialog::OpenFile, "OpenReferenceImageCollection");
dialog.setMimeTypeFilters(QStringList() << "application/x-krita-reference-images");
dialog.setCaption(i18n("Load Reference Images"));
QStringList locations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
if (!locations.isEmpty()) {
dialog.setDefaultDir(locations.first());
}
QString filename = dialog.filename();
if (filename.isEmpty()) return;
if (!QFileInfo(filename).exists()) return;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(nullptr, i18nc("@title:window", "Krita"), i18n("Could open '%1'.", filename));
return;
}
KisReferenceImageCollection collection;
if (collection.load(&file)) {
KisReferenceImagesLayer *layer = referenceImagesLayer();
Q_FOREACH(KisReferenceImage *reference, collection.referenceImages()) {
layer->addShape(reference);
}
} else {
QMessageBox::critical(nullptr, i18nc("@title:window", "Krita"), i18n("Could not load reference images from '%1'.", filename));
}
file.close();
}
void ToolReferenceImages::saveReferenceImages()
{
auto kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
KIS_ASSERT_RECOVER_RETURN(kisCanvas)
KoFileDialog dialog(kisCanvas->viewManager()->mainWindow(), KoFileDialog::SaveFile, "SaveReferenceImageCollection");
dialog.setMimeTypeFilters(QStringList() << "application/x-krita-reference-images");
dialog.setCaption(i18n("Save Reference Images"));
QStringList locations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
if (!locations.isEmpty()) {
dialog.setDefaultDir(locations.first());
}
QString filename = dialog.filename();
if (filename.isEmpty()) return;
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::critical(nullptr, i18nc("@title:window", "Krita"), i18n("Could open '%1' for saving.", filename));
return;
}
KisReferenceImageCollection collection(referenceImagesLayer()->referenceImages());
bool ok = collection.save(&file);
file.close();
if (!ok) {
QMessageBox::critical(nullptr, i18nc("@title:window", "Krita"), i18n("Failed to save reference images.", filename));
}
}
void ToolReferenceImages::slotSelectionChanged()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment