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 <tobias.hunger@qt.io>
This commit is contained in:
Jochen Becher
2016-06-21 21:27:43 +02:00
parent 9ccb089870
commit 80e5259999
11 changed files with 98 additions and 47 deletions

View File

@@ -58,7 +58,6 @@ public:
Q_UNUSED(widget); Q_UNUSED(widget);
painter->save(); painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(pen()); painter->setPen(pen());
painter->setBrush(brush()); painter->setBrush(brush());
painter->drawRoundedRect(rect(), 3, 3); painter->drawRoundedRect(rect(), 3, 3);

View File

@@ -40,23 +40,6 @@
namespace qmt { 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 class ArrowItem::GraphicsHeadItem : public QGraphicsItem
{ {
public: public:
@@ -182,7 +165,7 @@ public:
if (hasArrow) { if (hasArrow) {
if (!m_arrowItem) if (!m_arrowItem)
m_arrowItem = new ArrowItem::GraphicsPathItem(this); m_arrowItem = new QGraphicsPathItem(this);
if (m_head == ArrowItem::HeadOpen || m_head == ArrowItem::HeadTriangle) { if (m_head == ArrowItem::HeadOpen || m_head == ArrowItem::HeadTriangle) {
m_arrowItem->setPen(style->linePen()); m_arrowItem->setPen(style->linePen());
@@ -210,7 +193,7 @@ public:
if (hasDiamond) { if (hasDiamond) {
if (!m_diamondItem) if (!m_diamondItem)
m_diamondItem = new ArrowItem::GraphicsPathItem(this); m_diamondItem = new QGraphicsPathItem(this);
if (m_head == ArrowItem::HeadDiamond || m_head == ArrowItem::HeadDiamondFilledTriangle) { if (m_head == ArrowItem::HeadDiamond || m_head == ArrowItem::HeadDiamondFilledTriangle) {
m_diamondItem->setPen(style->linePen()); m_diamondItem->setPen(style->linePen());
@@ -238,15 +221,15 @@ private:
ArrowItem::Head m_head = ArrowItem::HeadNone; ArrowItem::Head m_head = ArrowItem::HeadNone;
double m_arrowSize = 10.0; double m_arrowSize = 10.0;
double m_diamondSize = 15.0; double m_diamondSize = 15.0;
ArrowItem::GraphicsPathItem *m_arrowItem = 0; QGraphicsPathItem *m_arrowItem = 0;
ArrowItem::GraphicsPathItem *m_diamondItem = 0; QGraphicsPathItem *m_diamondItem = 0;
}; };
class ArrowItem::GraphicsShaftItem : public ArrowItem::GraphicsPathItem class ArrowItem::GraphicsShaftItem : public QGraphicsPathItem
{ {
public: public:
explicit GraphicsShaftItem(QGraphicsItem *parent) explicit GraphicsShaftItem(QGraphicsItem *parent)
: GraphicsPathItem(parent) : QGraphicsPathItem(parent)
{ {
} }
}; };

View File

@@ -37,7 +37,6 @@ class Style;
class ArrowItem : public QGraphicsItem class ArrowItem : public QGraphicsItem
{ {
class GraphicsPathItem;
class GraphicsHeadItem; class GraphicsHeadItem;
class GraphicsShaftItem; class GraphicsShaftItem;

View File

@@ -63,7 +63,6 @@ void RelationStarter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
Q_UNUSED(widget); Q_UNUSED(widget);
painter->save(); painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(pen()); painter->setPen(pen());
painter->setBrush(brush()); painter->setBrush(brush());
painter->drawRoundedRect(rect(), 3, 3); painter->drawRoundedRect(rect(), 3, 3);

View File

@@ -46,6 +46,7 @@ DiagramView::DiagramView(QWidget *parent)
setAlignment(Qt::AlignLeft | Qt::AlignTop); setAlignment(Qt::AlignLeft | Qt::AlignTop);
setDragMode(QGraphicsView::RubberBandDrag); setDragMode(QGraphicsView::RubberBandDrag);
setFrameShape(QFrame::NoFrame); setFrameShape(QFrame::NoFrame);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
} }
DiagramView::~DiagramView() DiagramView::~DiagramView()

View File

@@ -43,67 +43,47 @@ void ShapePaintVisitor::visitLine(const LineShape *shapeLine)
{ {
QPointF p1 = shapeLine->pos1().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size); 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); 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->drawLine(p1, p2);
m_painter->restore();
} }
void ShapePaintVisitor::visitRect(const RectShape *shapeRect) 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), 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))); shapeRect->size().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size)));
m_painter->restore();
} }
void ShapePaintVisitor::visitRoundedRect(const RoundedRectShape *shapeRoundedRect) void ShapePaintVisitor::visitRoundedRect(const RoundedRectShape *shapeRoundedRect)
{ {
qreal radiusX = shapeRoundedRect->radius().mapScaledTo(0, m_originalSize.width(), m_baseSize.width(), m_size.width()); 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()); 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), 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)), shapeRoundedRect->size().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size)),
radiusX, radiusY); radiusX, radiusY);
m_painter->restore();
} }
void ShapePaintVisitor::visitCircle(const CircleShape *shapeCircle) 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), 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.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())); shapeCircle->radius().mapScaledTo(m_scaledOrigin.y(), m_originalSize.height(), m_baseSize.height(), m_size.height()));
m_painter->restore();
} }
void ShapePaintVisitor::visitEllipse(const EllipseShape *shapeEllipse) void ShapePaintVisitor::visitEllipse(const EllipseShape *shapeEllipse)
{ {
QSizeF radius = shapeEllipse->radius().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size); 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), m_painter->drawEllipse(shapeEllipse->center().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size),
radius.width(), radius.height()); radius.width(), radius.height());
m_painter->restore();
} }
void ShapePaintVisitor::visitArc(const ArcShape *shapeArc) void ShapePaintVisitor::visitArc(const ArcShape *shapeArc)
{ {
QSizeF radius = shapeArc->radius().mapScaledTo(m_scaledOrigin, m_originalSize, m_baseSize, m_size); 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), 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); shapeArc->startAngle() * 16, shapeArc->spanAngle() * 16);
m_painter->restore();
} }
void ShapePaintVisitor::visitPath(const PathShape *shapePath) void ShapePaintVisitor::visitPath(const PathShape *shapePath)
{ {
m_painter->save();
m_painter->setRenderHint(QPainter::Antialiasing, true);
QPainterPath path; QPainterPath path;
foreach (const PathShape::Element &element, shapePath->elements()) { foreach (const PathShape::Element &element, shapePath->elements()) {
switch (element.m_elementType) { switch (element.m_elementType) {
@@ -138,7 +118,6 @@ void ShapePaintVisitor::visitPath(const PathShape *shapePath)
} }
} }
m_painter->drawPath(path); m_painter->drawPath(path);
m_painter->restore();
} }
ShapeSizeVisitor::ShapeSizeVisitor(const QPointF &scaledOrigin, const QSizeF &originalSize, const QSizeF &baseSize, ShapeSizeVisitor::ShapeSizeVisitor(const QPointF &scaledOrigin, const QSizeF &originalSize, const QSizeF &baseSize,

View File

@@ -54,6 +54,9 @@ public:
QAction *selectAllAction = 0; QAction *selectAllAction = 0;
QAction *openParentDiagramAction = 0; QAction *openParentDiagramAction = 0;
QAction *exportDiagramAction = 0; QAction *exportDiagramAction = 0;
QAction *zoomInAction = 0;
QAction *zoomOutAction = 0;
QAction *resetZoomAction = 0;
}; };
ActionHandler::ActionHandler(const Core::Context &context, QObject *parent) ActionHandler::ActionHandler(const Core::Context &context, QObject *parent)
@@ -118,6 +121,21 @@ QAction *ActionHandler::exportDiagramAction() const
return d->exportDiagramAction; 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() void ActionHandler::createActions()
{ {
Core::ActionContainer *medit = Core::ActionManager::actionContainer(Core::Constants::M_EDIT); Core::ActionContainer *medit = Core::ActionManager::actionContainer(Core::Constants::M_EDIT);
@@ -150,6 +168,26 @@ void ActionHandler::createActions()
menuModelEditor->addAction(exportDiagramCommand); menuModelEditor->addAction(exportDiagramCommand);
d->exportDiagramAction = exportDiagramCommand->action(); 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( d->openParentDiagramAction = registerCommand(
Constants::OPEN_PARENT_DIAGRAM, [this]() { openParentDiagram(); }, Core::Context(), true, Constants::OPEN_PARENT_DIAGRAM, [this]() { openParentDiagram(); }, Core::Context(), true,
tr("Open Parent Diagram"), QKeySequence(QStringLiteral("Ctrl+Shift+P")))->action(); tr("Open Parent Diagram"), QKeySequence(QStringLiteral("Ctrl+Shift+P")))->action();
@@ -256,6 +294,27 @@ void ActionHandler::exportDiagram()
editor->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, Core::Command *ActionHandler::registerCommand(const Core::Id &id, const std::function<void()> &slot,
const Core::Context &context, bool scriptable, const QString &title, const Core::Context &context, bool scriptable, const QString &title,
const QKeySequence &keySequence) const QKeySequence &keySequence)

View File

@@ -64,6 +64,9 @@ public:
QAction *selectAllAction() const; QAction *selectAllAction() const;
QAction *openParentDiagramAction() const; QAction *openParentDiagramAction() const;
QAction *exportDiagramAction() const; QAction *exportDiagramAction() const;
QAction *zoomInAction() const;
QAction *zoomOutAction() const;
QAction *resetZoom() const;
void createActions(); void createActions();
@@ -80,6 +83,9 @@ private slots:
void onEditProperties(); void onEditProperties();
void onEditItem(); void onEditItem();
void exportDiagram(); void exportDiagram();
void zoomIn();
void zoomOut();
void resetZoom();
private: private:
Core::Command *registerCommand(const Core::Id &id, const std::function<void()> &slot, Core::Command *registerCommand(const Core::Id &id, const std::function<void()> &slot,

View File

@@ -100,6 +100,7 @@ namespace ModelEditor {
namespace Internal { namespace Internal {
static const char PROPERTYNAME_TOOLBARID[] = "ToolbarId"; static const char PROPERTYNAME_TOOLBARID[] = "ToolbarId";
static const double ZOOM_FACTOR = 1.05;
class ModelEditor::ModelEditorPrivate 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 *ModelEditor::guessSelectedPackage() const
{ {
qmt::MPackage *package = 0; qmt::MPackage *package = 0;

View File

@@ -86,6 +86,9 @@ public:
void editProperties(); void editProperties();
void editSelectedItem(); void editSelectedItem();
void exportDiagram(); void exportDiagram();
void zoomIn();
void zoomOut();
void resetZoom();
qmt::MPackage *guessSelectedPackage() const; qmt::MPackage *guessSelectedPackage() const;

View File

@@ -36,6 +36,9 @@ const char DELETE_SELECTED_ELEMENTS[] = "ModelEditor.DeleteSelectedElements";
const char OPEN_PARENT_DIAGRAM[] = "ModelEditor.OpenParentDiagram"; const char OPEN_PARENT_DIAGRAM[] = "ModelEditor.OpenParentDiagram";
const char MENU_ID[] = "ModelEditor.Menu"; const char MENU_ID[] = "ModelEditor.Menu";
const char EXPORT_DIAGRAM[] = "ModelEditor.ExportDiagram"; 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_PACKAGE[] = "ModelEditor.Action.AddPackage";
const char ACTION_ADD_COMPONENT[] = "ModelEditor.Action.AddComponent"; const char ACTION_ADD_COMPONENT[] = "ModelEditor.Action.AddComponent";
const char ACTION_ADD_CLASS[] = "ModelEditor.Action.AddClass"; const char ACTION_ADD_CLASS[] = "ModelEditor.Action.AddClass";