Commit 80e52599 authored by Jochen Becher's avatar Jochen Becher

ModelEditor: Implement zoom of diagrams.

All elements of a zoomed diagram must be dran anti-aliased. This change
removes the extra handling of drawing vertical or horizontal lines
without anti-alias. The complete graphics view is drawn anti-aliased
instead.

Change-Id: I6fc041b6d70da5a7a7bcb8e97d07990517380b90
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent 9ccb0898
......@@ -58,7 +58,6 @@ public:
Q_UNUSED(widget);
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(pen());
painter->setBrush(brush());
painter->drawRoundedRect(rect(), 3, 3);
......
......@@ -40,23 +40,6 @@
namespace qmt {
class ArrowItem::GraphicsPathItem : public QGraphicsPathItem
{
public:
explicit GraphicsPathItem(QGraphicsItem *parent)
: QGraphicsPathItem(parent)
{
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
QGraphicsPathItem::paint(painter, option, widget);
painter->restore();
}
};
class ArrowItem::GraphicsHeadItem : public QGraphicsItem
{
public:
......@@ -182,7 +165,7 @@ public:
if (hasArrow) {
if (!m_arrowItem)
m_arrowItem = new ArrowItem::GraphicsPathItem(this);
m_arrowItem = new QGraphicsPathItem(this);
if (m_head == ArrowItem::HeadOpen || m_head == ArrowItem::HeadTriangle) {
m_arrowItem->setPen(style->linePen());
......@@ -210,7 +193,7 @@ public:
if (hasDiamond) {
if (!m_diamondItem)
m_diamondItem = new ArrowItem::GraphicsPathItem(this);
m_diamondItem = new QGraphicsPathItem(this);
if (m_head == ArrowItem::HeadDiamond || m_head == ArrowItem::HeadDiamondFilledTriangle) {
m_diamondItem->setPen(style->linePen());
......@@ -238,15 +221,15 @@ private:
ArrowItem::Head m_head = ArrowItem::HeadNone;
double m_arrowSize = 10.0;
double m_diamondSize = 15.0;
ArrowItem::GraphicsPathItem *m_arrowItem = 0;
ArrowItem::GraphicsPathItem *m_diamondItem = 0;
QGraphicsPathItem *m_arrowItem = 0;
QGraphicsPathItem *m_diamondItem = 0;
};
class ArrowItem::GraphicsShaftItem : public ArrowItem::GraphicsPathItem
class ArrowItem::GraphicsShaftItem : public QGraphicsPathItem
{
public:
explicit GraphicsShaftItem(QGraphicsItem *parent)
: GraphicsPathItem(parent)
: QGraphicsPathItem(parent)
{
}
};
......
......@@ -37,7 +37,6 @@ class Style;
class ArrowItem : public QGraphicsItem
{
class GraphicsPathItem;
class GraphicsHeadItem;
class GraphicsShaftItem;
......
......@@ -63,7 +63,6 @@ void RelationStarter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
Q_UNUSED(widget);
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(pen());
painter->setBrush(brush());
painter->drawRoundedRect(rect(), 3, 3);
......
......@@ -46,6 +46,7 @@ DiagramView::DiagramView(QWidget *parent)
setAlignment(Qt::AlignLeft | Qt::AlignTop);
setDragMode(QGraphicsView::RubberBandDrag);
setFrameShape(QFrame::NoFrame);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}
DiagramView::~DiagramView()
......
......@@ -43,67 +43,47 @@ void ShapePaintVisitor::visitLine(const LineShape *shapeLine)
{
QPointF p1 = shapeLine->pos1().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size);
QPointF p2 = shapeLine->pos2().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size);
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, p1.x() != p2.x() && p1.y() != p2.y());
m_painter->drawLine(p1, p2);
m_painter->restore();
}
void ShapePaintVisitor::visitRect(const RectShape *shapeRect)
{
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, false);
m_painter->drawRect(QRectF(shapeRect->pos().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size),
shapeRect->size().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size)));
m_painter->restore();
}
void ShapePaintVisitor::visitRoundedRect(const RoundedRectShape *shapeRoundedRect)
{
qreal radiusX = shapeRoundedRect->radius().mapScaledTo(0, m_originalSize.width(), m_baseSize.width(), m_size.width());
qreal radiusY = shapeRoundedRect->radius().mapScaledTo(0, m_originalSize.height(), m_baseSize.height(), m_size.height());
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, true);
m_painter->drawRoundedRect(QRectF(shapeRoundedRect->pos().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size),
shapeRoundedRect->size().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size)),
radiusX, radiusY);
m_painter->restore();
}
void ShapePaintVisitor::visitCircle(const CircleShape *shapeCircle)
{
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, true);
m_painter->drawEllipse(shapeCircle->center().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size),
shapeCircle->radius().mapScaledTo(m_scaledOrigin.x(), m_originalSize.width(), m_baseSize.width(), m_size.width()),
shapeCircle->radius().mapScaledTo(m_scaledOrigin.y(), m_originalSize.height(), m_baseSize.height(), m_size.height()));
m_painter->restore();
}
void ShapePaintVisitor::visitEllipse(const EllipseShape *shapeEllipse)
{
QSizeF radius = shapeEllipse->radius().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size);
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, true);
m_painter->drawEllipse(shapeEllipse->center().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size),
radius.width(), radius.height());
m_painter->restore();
}
void ShapePaintVisitor::visitArc(const ArcShape *shapeArc)
{
QSizeF radius = shapeArc->radius().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size);
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, true);
m_painter->drawArc(QRectF(shapeArc->center().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size) - QPointF(radius.width(), radius.height()), radius * 2.0),
shapeArc->startAngle() * 16, shapeArc->spanAngle() * 16);
m_painter->restore();
}
void ShapePaintVisitor::visitPath(const PathShape *shapePath)
{
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, true);
QPainterPath path;
foreach (const PathShape::Element &element, shapePath->elements()) {
switch (element.m_elementType) {
......@@ -138,7 +118,6 @@ void ShapePaintVisitor::visitPath(const PathShape *shapePath)
}
}
m_painter->drawPath(path);
m_painter->restore();
}
ShapeSizeVisitor::ShapeSizeVisitor(const QPointF &scaledOrigin, const QSizeF &originalSize, const QSizeF &baseSize,
......
......@@ -54,6 +54,9 @@ public:
QAction *selectAllAction = 0;
QAction *openParentDiagramAction = 0;
QAction *exportDiagramAction = 0;
QAction *zoomInAction = 0;
QAction *zoomOutAction = 0;
QAction *resetZoomAction = 0;
};
ActionHandler::ActionHandler(const Core::Context &context, QObject *parent)
......@@ -118,6 +121,21 @@ QAction *ActionHandler::exportDiagramAction() const
return d->exportDiagramAction;
}
QAction *ActionHandler::zoomInAction() const
{
return d->zoomInAction;
}
QAction *ActionHandler::zoomOutAction() const
{
return d->zoomOutAction;
}
QAction *ActionHandler::resetZoom() const
{
return d->resetZoomAction;
}
void ActionHandler::createActions()
{
Core::ActionContainer *medit = Core::ActionManager::actionContainer(Core::Constants::M_EDIT);
......@@ -150,6 +168,26 @@ void ActionHandler::createActions()
menuModelEditor->addAction(exportDiagramCommand);
d->exportDiagramAction = exportDiagramCommand->action();
menuModelEditor->addSeparator(d->context);
Core::Command *zoomInCommand = registerCommand(
Constants::ZOOM_IN, [this]() { zoomIn(); }, d->context, true,
tr("Zoom In"), QKeySequence(QStringLiteral("Ctrl++")));
menuModelEditor->addAction(zoomInCommand);
d->zoomInAction = zoomInCommand->action();
Core::Command *zoomOutCommand = registerCommand(
Constants::ZOOM_OUT, [this]() { zoomOut(); }, d->context, true,
tr("Zoom Out"), QKeySequence(QStringLiteral("Ctrl+-")));
menuModelEditor->addAction(zoomOutCommand);
d->zoomOutAction = zoomOutCommand->action();
Core::Command *resetZoomCommand = registerCommand(
Constants::RESET_ZOOM, [this]() { resetZoom(); }, d->context, true,
tr("Reset Zoom"), QKeySequence(QStringLiteral("Ctrl+0")));
menuModelEditor->addAction(resetZoomCommand);
d->zoomOutAction = resetZoomCommand->action();
d->openParentDiagramAction = registerCommand(
Constants::OPEN_PARENT_DIAGRAM, [this]() { openParentDiagram(); }, Core::Context(), true,
tr("Open Parent Diagram"), QKeySequence(QStringLiteral("Ctrl+Shift+P")))->action();
......@@ -256,6 +294,27 @@ void ActionHandler::exportDiagram()
editor->exportDiagram();
}
void ActionHandler::zoomIn()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->zoomIn();
}
void ActionHandler::zoomOut()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->zoomOut();
}
void ActionHandler::resetZoom()
{
auto editor = qobject_cast<ModelEditor *>(Core::EditorManager::currentEditor());
if (editor)
editor->resetZoom();
}
Core::Command *ActionHandler::registerCommand(const Core::Id &id, const std::function<void()> &slot,
const Core::Context &context, bool scriptable, const QString &title,
const QKeySequence &keySequence)
......
......@@ -64,6 +64,9 @@ public:
QAction *selectAllAction() const;
QAction *openParentDiagramAction() const;
QAction *exportDiagramAction() const;
QAction *zoomInAction() const;
QAction *zoomOutAction() const;
QAction *resetZoom() const;
void createActions();
......@@ -80,6 +83,9 @@ private slots:
void onEditProperties();
void onEditItem();
void exportDiagram();
void zoomIn();
void zoomOut();
void resetZoom();
private:
Core::Command *registerCommand(const Core::Id &id, const std::function<void()> &slot,
......
......@@ -100,6 +100,7 @@ namespace ModelEditor {
namespace Internal {
static const char PROPERTYNAME_TOOLBARID[] = "ToolbarId";
static const double ZOOM_FACTOR = 1.05;
class ModelEditor::ModelEditorPrivate
{
......@@ -558,6 +559,25 @@ void ModelEditor::exportDiagram()
}
}
void ModelEditor::zoomIn()
{
QTransform transform = d->diagramView->transform();
transform.scale(ZOOM_FACTOR, ZOOM_FACTOR);
d->diagramView->setTransform(transform);
}
void ModelEditor::zoomOut()
{
QTransform transform = d->diagramView->transform();
transform.scale(1.0 / ZOOM_FACTOR, 1.0 / ZOOM_FACTOR);
d->diagramView->setTransform(transform);
}
void ModelEditor::resetZoom()
{
d->diagramView->setTransform(QTransform());
}
qmt::MPackage *ModelEditor::guessSelectedPackage() const
{
qmt::MPackage *package = 0;
......
......@@ -86,6 +86,9 @@ public:
void editProperties();
void editSelectedItem();
void exportDiagram();
void zoomIn();
void zoomOut();
void resetZoom();
qmt::MPackage *guessSelectedPackage() const;
......
......@@ -36,6 +36,9 @@ const char DELETE_SELECTED_ELEMENTS[] = "ModelEditor.DeleteSelectedElements";
const char OPEN_PARENT_DIAGRAM[] = "ModelEditor.OpenParentDiagram";
const char MENU_ID[] = "ModelEditor.Menu";
const char EXPORT_DIAGRAM[] = "ModelEditor.ExportDiagram";
const char ZOOM_IN[] = "ModelEditor.ZoomIn";
const char ZOOM_OUT[] = "ModelEditor.ZoomOut";
const char RESET_ZOOM[] = "ModelEditor.ResetZoom";
const char ACTION_ADD_PACKAGE[] = "ModelEditor.Action.AddPackage";
const char ACTION_ADD_COMPONENT[] = "ModelEditor.Action.AddComponent";
const char ACTION_ADD_CLASS[] = "ModelEditor.Action.AddClass";
......
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