Commit c8e732f3 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Save and restore the currently selected layer in .kra files

parent 5368ccb0
......@@ -317,6 +317,11 @@ bool KisDoc2::completeLoading(KoStore *store)
m_d->kraLoader->loadBinaryData(store, m_d->image, url().url(), isStoredExtern());
vKisNodeSP preselectedNodes = m_d->kraLoader->selectedNodes();
if (preselectedNodes.size() > 0) {
m_d->preActivatedNode = preselectedNodes.first();
}
delete m_d->kraLoader;
m_d->kraLoader = 0;
......@@ -481,7 +486,6 @@ QPixmap KisDoc2::generatePreview(const QSize& size)
newSize.scale(size, Qt::KeepAspectRatio);
QImage image = m_d->image->convertToQImage(QRect(0, 0, newSize.width(), newSize.height()), newSize, 0);
//image.save("thumb.png");
return QPixmap::fromImage(image);
}
return QPixmap(size);
......@@ -505,6 +509,9 @@ vKisNodeSP KisDoc2::activeNodes() const
if (view) {
KisNodeSP activeNode = view->activeNode();
if (!nodes.contains(activeNode)) {
if (activeNode->inherits("KisMask")) {
activeNode = activeNode->parent();
}
nodes.append(activeNode);
}
}
......
......@@ -66,6 +66,7 @@ public:
QString imageComment; // used to be stored in the image, is now in the documentInfo block
QMap<KisNode*, QString> layerFilenames; // temp storage during loading
int syntaxVersion; // version of the fileformat we are loading
vKisNodeSP selectedNodes; // the nodes that were active when saving the document.
};
......@@ -212,6 +213,11 @@ void KisKraLoader::loadBinaryData(KoStore * store, KisImageWSP image, const QStr
}
vKisNodeSP KisKraLoader::selectedNodes() const
{
return m_d->selectedNodes;
}
KisNode* KisKraLoader::loadNodes(const KoXmlElement& element, KisImageWSP image, KisNode* parent)
{
......@@ -351,7 +357,11 @@ KisNode* KisKraLoader::loadNode(const KoXmlElement& element, KisImageWSP image)
if (element.attribute(FILE_NAME).isNull())
m_d->layerFilenames[node] = name;
else
m_d->layerFilenames[node] = QString(element.attribute(FILE_NAME));
m_d->layerFilenames[node] = element.attribute(FILE_NAME);
if (element.hasAttribute("selected") && element.attribute("selected") == "true") {
m_d->selectedNodes.append(node);
}
return node;
}
......
......@@ -52,6 +52,8 @@ public:
void loadBinaryData(KoStore* store, KisImageWSP image, const QString & uri, bool external);
vKisNodeSP selectedNodes() const;
private:
KisNode* loadNodes(const KoXmlElement& element, KisImageWSP image, KisNode* parent);
......
......@@ -86,6 +86,7 @@ QDomElement KisKraSaver::saveXML(QDomDocument& doc, KisImageWSP image)
quint32 count = 1; // We don't save the root layer, but it does count
KisSaveXmlVisitor visitor(doc, imageElement, count, true);
visitor.setSelectedNodes(m_d->doc->activeNodes());
image->rootLayer()->accept(visitor);
m_d->nodeFileNames = visitor.nodeFileNames();
......
......@@ -54,6 +54,11 @@ KisSaveXmlVisitor::KisSaveXmlVisitor(QDomDocument doc, const QDomElement & eleme
m_elem = element;
}
void KisSaveXmlVisitor::setSelectedNodes(vKisNodeSP selectedNodes)
{
m_selectedNodes = selectedNodes;
}
bool KisSaveXmlVisitor::visit(KisExternalLayer * layer)
{
if (layer->inherits("KisShapeLayer")) {
......@@ -103,6 +108,7 @@ bool KisSaveXmlVisitor::visit(KisGroupLayer *layer)
Q_ASSERT(!layerElement.isNull());
layerElement.appendChild(elem);
KisSaveXmlVisitor visitor(m_doc, elem, m_count);
visitor.setSelectedNodes(m_selectedNodes);
m_count++;
bool success = visitor.visitAllInverse(layer);
......@@ -208,6 +214,13 @@ void KisSaveXmlVisitor::saveLayer(QDomElement & el, const QString & layerType, c
el.setAttribute(Y, layer->y());
el.setAttribute(UUID, layer->uuid().toString());
foreach (KisNodeSP node, m_selectedNodes) {
if (node.data() == layer) {
el.setAttribute("selected", "true");
break;
}
}
m_nodeFileNames[layer] = LAYER + QString::number(m_count);
dbgFile << "Saved layer "
......@@ -242,6 +255,7 @@ bool KisSaveXmlVisitor::saveMasks(KisNode * node, QDomElement & layerElement)
Q_ASSERT(!layerElement.isNull());
layerElement.appendChild(elem);
KisSaveXmlVisitor visitor(m_doc, elem, m_count);
visitor.setSelectedNodes(m_selectedNodes);
bool success = visitor.visitAllInverse(node);
QMapIterator<const KisNode*, QString> i(visitor.nodeFileNames());
......
......@@ -31,6 +31,8 @@ class KRITAUI_EXPORT KisSaveXmlVisitor : public KisNodeVisitor
public:
KisSaveXmlVisitor(QDomDocument doc, const QDomElement & element, quint32 &count, bool root = false);
void setSelectedNodes(vKisNodeSP selectedNodes);
using KisNodeVisitor::visit;
public:
......@@ -60,6 +62,7 @@ private:
friend class KisKraSaveXmlVisitorTest;
vKisNodeSP m_selectedNodes;
QMap<const KisNode*, QString> m_nodeFileNames;
QDomDocument m_doc;
QDomElement m_elem;
......
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