2016-12-03 14:14:06 +00:00
|
|
|
#include "fgcanvasgroup.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2017-01-09 16:55:03 +00:00
|
|
|
#include "canvasitem.h"
|
2016-12-03 14:14:06 +00:00
|
|
|
#include "localprop.h"
|
|
|
|
#include "fgcanvaspaintcontext.h"
|
|
|
|
#include "fgcanvaspath.h"
|
|
|
|
#include "fgcanvastext.h"
|
|
|
|
#include "fgqcanvasmap.h"
|
|
|
|
#include "fgqcanvasimage.h"
|
|
|
|
|
2017-01-26 23:14:48 +00:00
|
|
|
class ChildOrderingFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(const FGCanvasElement* a, FGCanvasElement* b)
|
|
|
|
{
|
|
|
|
if (a->zIndex() == b->zIndex()) {
|
|
|
|
// use prop node positions in the parent
|
|
|
|
return a->property()->position() < b->property()->position();
|
|
|
|
}
|
|
|
|
|
|
|
|
return a->zIndex() < b->zIndex();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-03 14:14:06 +00:00
|
|
|
FGCanvasGroup::FGCanvasGroup(FGCanvasGroup* pr, LocalProp* prop) :
|
|
|
|
FGCanvasElement(pr, prop)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const FGCanvasElementVec &FGCanvasGroup::children() const
|
|
|
|
{
|
|
|
|
return _children;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGCanvasGroup::markChildZIndicesDirty() const
|
|
|
|
{
|
|
|
|
_zIndicesDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FGCanvasGroup::hasChilden() const
|
|
|
|
{
|
|
|
|
return !_children.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int FGCanvasGroup::childCount() const
|
|
|
|
{
|
|
|
|
return _children.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
FGCanvasElement *FGCanvasGroup::childAt(unsigned int index) const
|
|
|
|
{
|
|
|
|
return _children.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int FGCanvasGroup::indexOfChild(const FGCanvasElement *e) const
|
|
|
|
{
|
|
|
|
auto it = std::find(_children.begin(), _children.end(), e);
|
|
|
|
if (it == _children.end()) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "not found";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::distance(_children.begin(), it);
|
|
|
|
}
|
|
|
|
|
2017-01-09 16:55:03 +00:00
|
|
|
CanvasItem *FGCanvasGroup::createQuickItem(QQuickItem *parent)
|
|
|
|
{
|
|
|
|
_quick = new CanvasItem(parent);
|
|
|
|
|
|
|
|
for (auto e : _children) {
|
|
|
|
e->createQuickItem(_quick);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _quick;
|
|
|
|
}
|
|
|
|
|
2016-12-03 14:14:06 +00:00
|
|
|
void FGCanvasGroup::doPaint(FGCanvasPaintContext *context) const
|
|
|
|
{
|
2017-11-02 17:20:42 +00:00
|
|
|
for (FGCanvasElement* element : _children) {
|
|
|
|
element->paint(context);
|
2016-12-03 14:14:06 +00:00
|
|
|
}
|
2017-11-02 17:20:42 +00:00
|
|
|
}
|
2016-12-03 14:14:06 +00:00
|
|
|
|
2017-11-02 17:20:42 +00:00
|
|
|
void FGCanvasGroup::doPolish()
|
|
|
|
{
|
2016-12-03 14:14:06 +00:00
|
|
|
if (_cachedSymbolDirty) {
|
|
|
|
qDebug() << _propertyRoot->path() << "should use symbol cache:" << _propertyRoot->value("symbol-type", QVariant()).toByteArray();
|
|
|
|
_cachedSymbolDirty = false;
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:20:42 +00:00
|
|
|
if (_zIndicesDirty) {
|
|
|
|
std::sort(_children.begin(), _children.end(), ChildOrderingFunction());
|
|
|
|
_zIndicesDirty = false;
|
|
|
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "adjust z order of quick items";
|
|
|
|
}
|
|
|
|
|
2016-12-03 14:14:06 +00:00
|
|
|
for (FGCanvasElement* element : _children) {
|
2017-11-02 17:20:42 +00:00
|
|
|
element->polish();
|
2016-12-03 14:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FGCanvasGroup::onChildAdded(LocalProp *prop)
|
|
|
|
{
|
|
|
|
const bool isRootGroup = (parent() == nullptr);
|
|
|
|
int newChildCount = 0;
|
|
|
|
|
|
|
|
if (FGCanvasElement::onChildAdded(prop)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray nm = prop->name();
|
|
|
|
if (nm == "group") {
|
|
|
|
_children.push_back(new FGCanvasGroup(this, prop));
|
|
|
|
newChildCount++;
|
|
|
|
} else if (nm == "path") {
|
|
|
|
_children.push_back(new FGCanvasPath(this, prop));
|
|
|
|
newChildCount++;
|
|
|
|
} else if (nm == "text") {
|
|
|
|
_children.push_back(new FGCanvasText(this, prop));
|
|
|
|
newChildCount++;
|
|
|
|
} else if (nm == "image") {
|
|
|
|
_children.push_back(new FGQCanvasImage(this, prop));
|
|
|
|
newChildCount++;
|
|
|
|
} else if (nm == "map") {
|
|
|
|
_children.push_back(new FGQCanvasMap(this, prop));
|
|
|
|
newChildCount++;
|
|
|
|
} else if (nm == "symbol-type") {
|
|
|
|
connect(prop, &LocalProp::valueChanged, this, &FGCanvasGroup::markCachedSymbolDirty);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isRootGroup) {
|
|
|
|
// ignore all of these, handled by the enclosing canvas view
|
2017-11-03 13:56:43 +00:00
|
|
|
if (nm == "size") {
|
|
|
|
connect(prop, &LocalProp::valueChanged, this, &FGCanvasGroup::canvasSizeChanged);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nm == "view") || nm.startsWith("status") || (nm == "name") || (nm == "mipmapping") || (nm == "placement")) {
|
2016-12-03 14:14:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newChildCount > 0) {
|
2017-01-26 23:14:48 +00:00
|
|
|
markChildZIndicesDirty();
|
2017-01-09 16:55:03 +00:00
|
|
|
|
|
|
|
if (_quick) {
|
|
|
|
_children.back()->createQuickItem(_quick);
|
|
|
|
}
|
|
|
|
|
2016-12-03 14:14:06 +00:00
|
|
|
emit childAdded();
|
2017-01-26 23:14:48 +00:00
|
|
|
return true;
|
2016-12-03 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "saw unknown group child" << prop->name();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FGCanvasGroup::onChildRemoved(LocalProp *prop)
|
|
|
|
{
|
|
|
|
if (FGCanvasElement::onChildRemoved(prop)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray nm = prop->name();
|
|
|
|
if ((nm == "group") || (nm == "image") || (nm == "path") || (nm == "text") || (nm == "map")) {
|
|
|
|
int removedChildIndex = indexOfChildWithProp(prop);
|
|
|
|
if (removedChildIndex >= 0) {
|
|
|
|
auto it = _children.begin() + removedChildIndex;
|
|
|
|
delete *it;
|
|
|
|
_children.erase(it);
|
|
|
|
emit childRemoved(removedChildIndex);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FGCanvasGroup::indexOfChildWithProp(LocalProp* prop) const
|
|
|
|
{
|
|
|
|
auto it = std::find_if(_children.begin(), _children.end(), [prop](FGCanvasElement* child)
|
|
|
|
{
|
|
|
|
return (child->property() == prop);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (it == _children.end()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::distance(_children.begin(), it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGCanvasGroup::markStyleDirty()
|
|
|
|
{
|
|
|
|
for (FGCanvasElement* element : _children) {
|
|
|
|
element->markStyleDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGCanvasGroup::markCachedSymbolDirty()
|
|
|
|
{
|
|
|
|
_cachedSymbolDirty = true;
|
|
|
|
}
|