Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Port KoShapeQtQuickLabel to use KoShapePainter, add some extra functionality.

Wolthera van Hövell tot Westerflierfd6a4a54cfef
diff --git a/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp b/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp
index 562c4f32e9..0e2bdb9764 100644
--- a/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp
+++ b/plugins/dockers/textproperties/KoShapeQtQuickLabel.cpp
@@ -10,23 +10,79 @@
#include <KoDocumentResourceManager.h>
#include <QQuickWindow>
#include <KoShapeGroupCommand.h>
+#include <KoShapeUngroupCommand.h>
#include <KoShapePainter.h>
+#include <kis_assert.h>
struct KoShapeQtQuickLabel::Private {
+
+ Private(): shape(new KoShapeGroup()) {
+
+ }
+
+ ~Private(){}
+
QScopedPointer<KoShapeGroup> shape;
+ QScopedPointer<KoShapePainter> shapePainter;
QString svgData;
- int imagePadding = 5;
- qreal imageScale = 1;
bool fullColor = false;
+ KoShapeQtQuickLabel::ScalingType scalingType = KoShapeQtQuickLabel::Fit;
+ Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignVCenter;
+
+ QRectF documentRect;
+
+ int leftPadding = 0;
+ int rightPadding = 0;
+ int topPadding = 0;
+ int bottomPadding = 0;
+
+ QRectF adjustBBoxToScaling(const QRectF &bbox, const QRectF &widgetBounds) {
+ const double newWidth = widgetBounds.width() - (leftPadding + rightPadding);
+ const double newHeight = widgetBounds.height() - (topPadding + bottomPadding);
+
+ QRectF newBox = bbox; ///< "Fit" behaviour.
+ if (scalingType == KoShapeQtQuickLabel::FitWidth) {
+ const double docWidth = bbox.width();
+ const double docHeight = bbox.width() * (newHeight / newWidth);
+ double docY = bbox.top();
+ if (alignment.testFlag(Qt::AlignBottom)) {
+ docY = bbox.bottom() - docHeight;
+ } else if (alignment.testFlag(Qt::AlignVCenter)) {
+ docY = (bbox.bottom()*0.5) - (docHeight*0.5);
+ }
+ newBox = QRectF(bbox.left(), docY, docWidth, docHeight);
+ } else if (scalingType == KoShapeQtQuickLabel::FitHeight) {
+ const double docWidth = bbox.height() * (newWidth / newHeight);
+ const double docHeight = bbox.height();
+ double docX = bbox.left();
+ if (alignment.testFlag(Qt::AlignRight)) {
+ docX = bbox.right() - docWidth;
+ } else if (alignment.testFlag(Qt::AlignHCenter)) {
+ docX = (bbox.right()*0.5) - (docWidth*0.5);
+ }
+ newBox = QRectF(docX, bbox.top(), docWidth, docHeight);
+ }
+ const double newTop = (topPadding/newHeight) * newBox.height();
+ const double newBottom = (bottomPadding/newHeight) * newBox.height();
+ const double newLeft = (leftPadding/newWidth) * newBox.width();
+ const double newRight = (rightPadding/newWidth) * newBox.width();
+ return newBox.adjusted(-newLeft, -newTop, newRight, newBottom);
+ }
+
+ QRectF addPaddingToBounds(QRectF bounds) {
+ return bounds.adjusted(-leftPadding, -topPadding, rightPadding, bottomPadding);
+ }
- bool firstPaint = false;
};
KoShapeQtQuickLabel::KoShapeQtQuickLabel(QQuickItem *parent)
: QQuickPaintedItem(parent)
- , d(new Private)
+ , d(new Private())
{
+ setAntialiasing(true);
+ setOpaquePainting(true);
+ connect(this, SIGNAL(minimumRectChanged()), this, SLOT(callUpdateIfComplete()));
}
KoShapeQtQuickLabel::~KoShapeQtQuickLabel()
@@ -35,38 +91,20 @@ KoShapeQtQuickLabel::~KoShapeQtQuickLabel()
void KoShapeQtQuickLabel::paint(QPainter *painter)
{
+ KIS_SAFE_ASSERT_RECOVER_RETURN(d->shapePainter);
if (!painter->isActive()) return;
- if (!d->firstPaint) {
- updateShapes();
- d->firstPaint = true;
- }
+
+ const QRectF bbox = d->documentRect.isValid()? d->documentRect: d->shapePainter->contentRect();
+ const QRectF bounds = QRectF(0, 0, width(), height());
painter->save();
- painter->setRenderHint(QPainter::Antialiasing, true);
- painter->fillRect(0, 0, width(), height(), fillColor());
if (!d->shape) {
painter->restore();
return;
}
- QRectF bbox = d->shape->boundingRect();
- qreal scale = (height()- d->imagePadding*2) / bbox.height();
-
- painter->translate(boundingRect().center());
- painter->scale(scale, scale);
- painter->translate(-bbox.center());
-
- qreal pixelRatio = this->window()->effectiveDevicePixelRatio();
- QRectF scaledBounds = QRectF(0, 0, this->boundingRect().width()*pixelRatio, this->boundingRect().height()*pixelRatio);
-
- painter->setClipRect(painter->transform().inverted().mapRect(scaledBounds));
- painter->setPen(Qt::transparent);
-
- Q_FOREACH(const KoShape *shape, d->shape->shapes()) {
- painter->save();
- painter->setTransform(shape->transformation() * painter->transform());
- shape->paint(*painter);
- painter->restore();
- }
+ d->shapePainter->paint(*painter,
+ bounds.toAlignedRect(),
+ d->adjustBBoxToScaling(bbox, bounds));
painter->restore();
@@ -79,8 +117,6 @@ QString KoShapeQtQuickLabel::svgData() const
void KoShapeQtQuickLabel::setSvgData(const QString &newSvgData)
{
- qreal max = qMax(20.0*imageScale(), qMin(width()-(d->imagePadding*2), height()-(d->imagePadding*2)));
- setImplicitHeight(max);
if (d->svgData == newSvgData)
return;
d->svgData = newSvgData;
@@ -94,11 +130,15 @@ void KoShapeQtQuickLabel::setSvgData(const QString &newSvgData)
QList<KoShape*> shapes = p.parseSvg(doc.documentElement(), &sz);
if (shapes.isEmpty()) return;
- d->shape.reset(new KoShapeGroup());
- KoShapeGroupCommand cmd(d->shape.data(), shapes, false);
- cmd.redo();
+ if (!d->shape->shapes().isEmpty()) {
+ KoShapeUngroupCommand unGroup(d->shape.data(), d->shape->shapes());
+ unGroup.redo();
+ }
+ KoShapeGroupCommand group(d->shape.data(), shapes, false);
+ group.redo();
emit svgDataChanged();
+ emit minimumRectChanged();
}
QColor KoShapeQtQuickLabel::foregroundColor() const
@@ -124,34 +164,27 @@ void KoShapeQtQuickLabel::setForegroundColor(const QColor &newForegroundColor)
}
d->shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(newForegroundColor)));
- update(this->boundingRect().toAlignedRect());
+ callUpdateIfComplete();
emit foregroundColorChanged();
}
-int KoShapeQtQuickLabel::imagePadding() const
+int KoShapeQtQuickLabel::padding() const
{
- return d->imagePadding;
+ return (d->leftPadding + d->rightPadding + d->topPadding + d->bottomPadding) / 4;
}
-void KoShapeQtQuickLabel::setImagePadding(int newPadding)
+void KoShapeQtQuickLabel::setPadding(int newPadding)
{
- if (d->imagePadding == newPadding)
+ const int currentPadding = (d->leftPadding + d->rightPadding + d->topPadding + d->bottomPadding) / 4;
+ if (currentPadding == newPadding)
return;
- d->imagePadding = newPadding;
- emit imagePaddingChanged();
-}
+ d->leftPadding = newPadding;
+ d->rightPadding = newPadding;
+ d->topPadding = newPadding;
+ d->bottomPadding = newPadding;
-qreal KoShapeQtQuickLabel::imageScale() const
-{
- return d->imageScale;
-}
-
-void KoShapeQtQuickLabel::setImageScale(qreal newImageScale)
-{
- if (qFuzzyCompare(d->imageScale, newImageScale))
- return;
- d->imageScale = newImageScale;
- emit imageScaleChanged();
+ emit paddingChanged();
+ emit minimumRectChanged();
}
bool KoShapeQtQuickLabel::fullColor() const
@@ -168,6 +201,42 @@ void KoShapeQtQuickLabel::setFullColor(bool newFullColor)
emit fullColorChanged();
}
+KoShapeQtQuickLabel::ScalingType KoShapeQtQuickLabel::scalingType() const
+{
+ return d->scalingType;
+}
+
+void KoShapeQtQuickLabel::setScalingType(const ScalingType type)
+{
+ if (d->scalingType == type) return;
+ d->scalingType = type;
+ emit scalingTypeChanged();
+}
+
+Qt::Alignment KoShapeQtQuickLabel::alignment() const
+{
+ return d->alignment;
+}
+
+void KoShapeQtQuickLabel::setAlignment(const Qt::Alignment align)
+{
+ if (d->alignment == align) return;
+ d->alignment = align;
+ emit alignmentChanged();
+}
+
+void KoShapeQtQuickLabel::componentComplete()
+{
+ QQuickPaintedItem::componentComplete();
+ updateShapes();
+ if (!d->shapePainter) {
+ d->shapePainter.reset(new KoShapePainter());
+ d->shapePainter->setShapes({d->shape.data()});
+ emit minimumRectChanged();
+ }
+ setOpaquePainting(fillColor().alpha() == 255);
+}
+
void KoShapeQtQuickLabel::updateShapes()
{
if (d->shape) {
@@ -176,3 +245,29 @@ void KoShapeQtQuickLabel::updateShapes()
}
}
}
+
+void KoShapeQtQuickLabel::callUpdateIfComplete()
+{
+ if (isComponentComplete()) {
+ update(boundingRect().toAlignedRect());
+ }
+}
+
+QRectF KoShapeQtQuickLabel::documentRect() const
+{
+ return d->documentRect;
+}
+
+void KoShapeQtQuickLabel::setDocumentRect(const QRectF &rect)
+{
+ if (d->documentRect == rect) return;
+ d->documentRect = rect;
+ emit documentRectChanged();
+ emit minimumRectChanged();
+}
+
+QRectF KoShapeQtQuickLabel::minimumRect() const
+{
+ const QRectF bounds = d->documentRect.isValid()? d->documentRect: d->shapePainter? d->shapePainter->contentRect(): QRectF();
+ return d->addPaddingToBounds(bounds);
+}
diff --git a/plugins/dockers/textproperties/KoShapeQtQuickLabel.h b/plugins/dockers/textproperties/KoShapeQtQuickLabel.h
index 17446355ea..a9787a4739 100644
--- a/plugins/dockers/textproperties/KoShapeQtQuickLabel.h
+++ b/plugins/dockers/textproperties/KoShapeQtQuickLabel.h
@@ -19,13 +19,24 @@ class KoShapeQtQuickLabel: public QQuickPaintedItem
Q_OBJECT
Q_PROPERTY(QString svgData READ svgData WRITE setSvgData NOTIFY svgDataChanged)
Q_PROPERTY(QColor foregroundColor READ foregroundColor WRITE setForegroundColor NOTIFY foregroundColorChanged)
- Q_PROPERTY(int imagePadding READ imagePadding WRITE setImagePadding NOTIFY imagePaddingChanged)
- Q_PROPERTY(qreal imageScale READ imageScale WRITE setImageScale NOTIFY imageScaleChanged)
+ Q_PROPERTY(int padding READ padding WRITE setPadding NOTIFY paddingChanged)
Q_PROPERTY(bool fullColor READ fullColor WRITE setFullColor NOTIFY fullColorChanged)
+ Q_PROPERTY(ScalingType scalingType READ scalingType WRITE setScalingType NOTIFY scalingTypeChanged)
+ Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
+ Q_PROPERTY(QRectF documentRect READ documentRect WRITE setDocumentRect NOTIFY documentRectChanged)
+ Q_PROPERTY(QRectF minimumRect READ minimumRect NOTIFY minimumRectChanged)
+
public:
KoShapeQtQuickLabel(QQuickItem *parent = nullptr);
~KoShapeQtQuickLabel();
+ enum ScalingType {
+ Fit, ///< Default koshapepainter behaviour, fit whole document into widget bounds.
+ FitWidth, ///< Scale document view to document width. Use alignment to position top or bottom on the document.
+ FitHeight ///< Scale document view to document height. Use alignment to position left or right on the document.
+ };
+ Q_ENUM(ScalingType)
+
// QQuickPaintedItem interface
public:
void paint(QPainter *painter) override;
@@ -36,17 +47,45 @@ public:
QColor foregroundColor() const;
void setForegroundColor(const QColor &newForegroundColor);
- /// Padding of the image to the borders.
- int imagePadding() const;
- void setImagePadding(int newPadding);
-
- /// extra scaling applied to the image.
- qreal imageScale() const;
- void setImageScale(qreal newImageScale);
+ /**
+ * Padding of the documentView to the borders.
+ * This will add extra padding to the sides. If there's still paintable
+ * shape data in that padding, it will be drawn as well.
+ */
+ int padding() const;
+ void setPadding(int newPadding);
bool fullColor() const;
void setFullColor(bool newFullColor);
+ ScalingType scalingType() const;
+ void setScalingType(const ScalingType type);
+
+ /**
+ * @brief alignment
+ * @return how to align the document viewport when using FillWidth or FillHeight as the scaling type.
+ */
+ Qt::Alignment alignment() const;
+ void setAlignment(const Qt::Alignment align);
+
+ /**
+ * By default, this label will use the content rect as the document view.
+ * When this rectangle is valid, it will be used instead.
+ **/
+ QRectF documentRect() const;
+ void setDocumentRect(const QRectF &rect);
+
+ /**
+ * @brief minimumRect
+ * Minimum size rectangle, defined by either documentRect or loaded SVG contentRect,
+ * with padding attached. Use this when you want sensible minimum size.
+ * We cannot set this to implicitsize or the like, because implicit width and height
+ * need to be used when setting KoShapeQtQuickLabel as a content item to a QQuickControl.
+ */
+ QRectF minimumRect() const;
+
+ void componentComplete() override;
+
Q_SIGNALS:
void svgDataChanged();
@@ -54,15 +93,23 @@ Q_SIGNALS:
void foregroundColorChanged();
- void imagePaddingChanged();
-
- void imageScaleChanged();
+ void paddingChanged();
void fullColorChanged();
+ void scalingTypeChanged();
+
+ void alignmentChanged();
+
+ void documentRectChanged();
+
+ void minimumRectChanged();
+
private Q_SLOTS:
void updateShapes();
+ void callUpdateIfComplete();
+
private:
struct Private;
const QScopedPointer<Private> d;
diff --git a/plugins/dockers/textproperties/qml/FontResourceDropdown.qml b/plugins/dockers/textproperties/qml/FontResourceDropdown.qml
index 318310283f..750aa0ef73 100644
--- a/plugins/dockers/textproperties/qml/FontResourceDropdown.qml
+++ b/plugins/dockers/textproperties/qml/FontResourceDropdown.qml
@@ -44,6 +44,7 @@ Button {
/// When updating the model wrapper, the "model" doesn't always update on the delegate, so we need to manually load
/// the metadata from the modelwrapper.
width: fontResourceView.listWidth;
+ height: implicitContentHeight;
highlighted: familyCmb.highlightedIndex === model.index;
Component.onCompleted: {
@@ -64,13 +65,12 @@ Button {
property bool isVariable : typeof meta.is_variable === 'boolean'? meta.is_variable: false;
property int type : typeof meta.font_type === 'number'? meta.font_type: 0;
property string fontName: fontDelegateItem.fontName;
- width: parent.width;
- height: nameLabel.height * 5;
- imageScale: 3;
- imagePadding: nameLabel.height;
+ padding: nameLabel.height;
+ scalingType: KoShapeQtQuickLabel.FitHeight;
svgData: fontDelegateItem.sample;
foregroundColor: highlighted? palette.highlightedText: palette.text;
fullColor: colorBitmap || colorCLRV0 || colorCLRV1 || colorSVG;
+ implicitHeight: padding*5;
Label {
id: nameLabel;