Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit cb86caf6 authored by Dmitry Kazakov's avatar Dmitry Kazakov

Removed code duplications for filtering editable shapes

parent 69376a1f
......@@ -28,6 +28,7 @@
#include "KoPointerEvent.h"
#include "KoShapePaintingContext.h"
#include "kis_algebra_2d.h"
#include "krita_container_utils.h"
#include <QPainter>
......@@ -180,6 +181,19 @@ const QList<KoShape*> KoSelection::selectedShapes() const
return d->selectedShapes;
}
const QList<KoShape *> KoSelection::selectedEditableShapes() const
{
Q_D(const KoSelection);
QList<KoShape*> shapes = selectedShapes();
KritaUtils::filterContainer (shapes, [](KoShape *shape) {
return shape->isEditable();
});
return shapes;
}
bool KoSelection::isSelected(const KoShape *shape) const
{
Q_D(const KoSelection);
......
......@@ -106,6 +106,12 @@ public:
*/
const QList<KoShape*> selectedShapes() const;
/**
* Same as selectedShapes() but only for editable shapes. Used by
* the algorithms that modify the image
*/
const QList<KoShape*> selectedEditableShapes() const;
/**
* Return the first selected shape, or 0 if there is nothing selected.
* @param strip if StrippedSelection, the returned list will not include any children
......
/*
* Copyright (c) 2016 Dmitry Kazakov <dimula73@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 KRITA_CONTAINER_UTILS_H
#define KRITA_CONTAINER_UTILS_H
#include <functional>
namespace KritaUtils
{
template <class T>
bool compareListsUnordered(const QList<T> &a, const QList<T> &b) {
if (a.size() != b.size()) return false;
Q_FOREACH(const T &t, a) {
if (!b.contains(t)) return false;
}
return true;
}
template <class C>
void makeContainerUnique(C &container) {
std::sort(container.begin(), container.end());
auto newEnd = std::unique(container.begin(), container.end());
while (newEnd != container.end()) {
newEnd = container.erase(newEnd);
}
}
template <class C>
void filterContainer(C &container, std::function<bool(typename C::reference)> keepIf) {
auto newEnd = std::remove_if(container.begin(), container.end(), std::unary_negate<decltype(keepIf)>(keepIf));
while (newEnd != container.end()) {
newEnd = container.erase(newEnd);
}
}
}
#endif // KRITA_CONTAINER_UTILS_H
......@@ -31,6 +31,7 @@ class QPainter;
#include <QVector>
#include "kritaimage_export.h"
#include "kis_types.h"
#include "krita_container_utils.h"
#include <functional>
......@@ -74,38 +75,6 @@ namespace KritaUtils
KisNodeSP KRITAIMAGE_EXPORT nearestNodeAfterRemoval(KisNodeSP node);
template <class T>
bool compareListsUnordered(const QList<T> &a, const QList<T> &b) {
if (a.size() != b.size()) return false;
Q_FOREACH(const T &t, a) {
if (!b.contains(t)) return false;
}
return true;
}
template <class C>
void makeContainerUnique(C &container) {
std::sort(container.begin(), container.end());
auto newEnd = std::unique(container.begin(), container.end());
while (newEnd != container.end()) {
newEnd = container.erase(newEnd);
}
}
template <class C>
void filterContainer(C &container, std::function<bool(typename C::reference)> keepIf) {
auto newEnd = std::remove_if(container.begin(), container.end(), std::unary_negate<decltype(keepIf)>(keepIf));
while (newEnd != container.end()) {
newEnd = container.erase(newEnd);
}
}
/**
* When drawing a rect Qt uses quite a weird algorithm. It
* draws 4 lines:
......
......@@ -53,6 +53,7 @@
#include "kis_debug.h"
#include "kis_acyclic_signal_connector.h"
DefaultToolWidget::DefaultToolWidget(KoInteractionTool *tool, QWidget *parent)
: QWidget(parent)
, m_tool(tool)
......@@ -108,17 +109,7 @@ DefaultToolWidget::~DefaultToolWidget()
{
}
#include <functional>
// FIXME: remove code duplication with KritaUtils!
template <class C>
void filterContainer(C &container, std::function<bool(typename C::reference)> keepIf) {
auto newEnd = std::remove_if(container.begin(), container.end(), std::unary_negate<decltype(keepIf)>(keepIf));
while (newEnd != container.end()) {
newEnd = container.erase(newEnd);
}
}
namespace {
void tryAnchorPosition(KoFlake::AnchorPosition anchor,
const QRectF &rect,
......@@ -132,23 +123,12 @@ void tryAnchorPosition(KoFlake::AnchorPosition anchor,
}
}
QList<KoShape*> fetchEditableShapes(KoSelection *selection)
{
QList<KoShape*> shapes = selection->selectedShapes();
filterContainer (shapes, [](KoShape *shape) {
return shape->isEditable();
});
return shapes;
}
QRectF calculateSelectionBounds(KoSelection *selection,
KoFlake::AnchorPosition anchor,
bool useGlobalSize,
QList<KoShape*> *outShapes = 0)
{
QList<KoShape*> shapes = fetchEditableShapes(selection);
QList<KoShape*> shapes = selection->selectedEditableShapes();
KoShape *shape = shapes.size() == 1 ? shapes.first() : selection;
......@@ -179,6 +159,8 @@ QRectF calculateSelectionBounds(KoSelection *selection,
return QRectF(resultPoint, resultRect.size());
}
}
void DefaultToolWidget::slotAnchorPointChanged()
{
QVariant newValue(positionSelector->value());
......@@ -189,7 +171,7 @@ void DefaultToolWidget::slotAnchorPointChanged()
void DefaultToolWidget::slotUpdateCheckboxes()
{
KoSelection *selection = m_tool->canvas()->shapeManager()->selection();
QList<KoShape*> shapes = fetchEditableShapes(selection);
QList<KoShape*> shapes = selection->selectedEditableShapes();
KoShapeGroup *onlyGroupShape = 0;
......@@ -215,7 +197,7 @@ void DefaultToolWidget::slotUpdateCheckboxes()
void DefaultToolWidget::slotAspectButtonToggled()
{
KoSelection *selection = m_tool->canvas()->shapeManager()->selection();
QList<KoShape*> shapes = fetchEditableShapes(selection);
QList<KoShape*> shapes = selection->selectedEditableShapes();
QList<bool> oldKeepAspectRatio;
QList<bool> newKeepAspectRatio;
......@@ -234,7 +216,7 @@ void DefaultToolWidget::slotAspectButtonToggled()
void DefaultToolWidget::slotUpdateAspectButton()
{
KoSelection *selection = m_tool->canvas()->shapeManager()->selection();
QList<KoShape*> shapes = fetchEditableShapes(selection);
QList<KoShape*> shapes = selection->selectedEditableShapes();
bool hasKeepAspectRatio = false;
bool hasNotKeepAspectRatio = false;
......
......@@ -42,13 +42,10 @@ ShapeMoveStrategy::ShapeMoveStrategy(KoToolBase *tool, const QPointF &clicked)
, m_start(clicked)
, m_canvas(tool->canvas())
{
QList<KoShape *> selectedShapes = m_canvas->shapeManager()->selection()->selectedShapes();
QList<KoShape *> selectedShapes = m_canvas->shapeManager()->selection()->selectedEditableShapes();
QRectF boundingRect;
Q_FOREACH (KoShape *shape, selectedShapes) {
if (!shape->isEditable()) {
continue;
}
m_selectedShapes << shape;
m_previousPositions << shape->absolutePosition(KoFlake::Center);
m_newPositions << shape->absolutePosition(KoFlake::Center);
......
......@@ -40,16 +40,10 @@ ShapeResizeStrategy::ShapeResizeStrategy(KoToolBase *tool, const QPointF &clicke
: KoInteractionStrategy(tool)
{
Q_ASSERT(tool->canvas()->shapeManager()->selection()->count() > 0);
QList<KoShape *> selectedShapes = tool->canvas()->shapeManager()->selection()->selectedShapes();
Q_FOREACH (KoShape *shape, selectedShapes) {
if (!shape->isEditable()) {
continue;
}
m_selectedShapes << shape;
}
m_selectedShapes = tool->canvas()->shapeManager()->selection()->selectedEditableShapes();
m_start = clicked;
KoShape *shape = 0;
KoShape *shape = 0;
if (m_selectedShapes.size() > 1) {
shape = tool->canvas()->shapeManager()->selection();
} else if (m_selectedShapes.size() == 1) {
......
......@@ -37,17 +37,11 @@ ShapeRotateStrategy::ShapeRotateStrategy(KoToolBase *tool, const QPointF &clicke
: KoInteractionStrategy(tool)
, m_start(clicked)
{
QList<KoShape *> selectedShapes = tool->canvas()->shapeManager()->selection()->selectedShapes();
Q_FOREACH (KoShape *shape, selectedShapes) {
if (!shape->isEditable()) {
continue;
}
m_selectedShapes << shape;
m_selectedShapes = tool->canvas()->shapeManager()->selection()->selectedEditableShapes();
Q_FOREACH (KoShape *shape, m_selectedShapes) {
m_oldTransforms << shape->transformation();
}
KoFlake::AnchorPosition anchor = !(buttons & Qt::RightButton) ?
KoFlake::Center :
KoFlake::AnchorPosition(tool->canvas()->resourceManager()->resource(KoFlake::HotPosition).toInt());
......
......@@ -42,12 +42,8 @@ ShapeShearStrategy::ShapeShearStrategy(KoToolBase *tool, const QPointF &clicked,
, m_start(clicked)
{
KoSelection *sel = tool->canvas()->shapeManager()->selection();
QList<KoShape *> selectedShapes = sel->selectedShapes();
Q_FOREACH (KoShape *shape, selectedShapes) {
if (!shape->isEditable()) {
continue;
}
m_selectedShapes << shape;
m_selectedShapes = sel->selectedEditableShapes();
Q_FOREACH (KoShape *shape, m_selectedShapes) {
m_oldTransforms << shape->transformation();
}
......
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