41 #include <QWheelEvent>
42 #include <QMouseEvent>
43 #include <QGraphicsScene>
45 #include <QGraphicsPathItem>
47 #include <QPrintDialog>
49 #include <QFileDialog>
50 #include <QTextDocument>
51 #include <QAbstractTextDocumentLayout>
152 view->setTransformationAnchor(AnchorUnderMouse);
153 view->setResizeAnchor(AnchorViewCenter);
154 if (QGLFormat::hasOpenGL())
155 view->setViewport(
new QGLWidget(QGLFormat(QGL::SampleBuffers),
view));
156 view->setRenderHint(QPainter::Antialiasing);
157 view->setBackgroundBrush(Qt::black);
159 view->setScene(
new QGraphicsScene());
164 view->setFrameStyle(QFrame::NoFrame);
197 view->setInteractive (
false );
213 qreal factor =
transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
214 if (factor < 1e-5 || factor > 1e5)
216 scale(scaleFactor, scaleFactor);
225 fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
233 viewport()->update();
235 double ratefact = 0.8;
236 if (
event->modifiers() & Qt::ControlModifier)
238 if (
event->modifiers() & Qt::ShiftModifier)
253 viewport()->update();
256 switch (
event->key()) {
258 if (
event->modifiers() & Qt::ControlModifier)
266 viewport()->update();
293 if (
event->modifiers() & Qt::ControlModifier)
315 QGraphicsView::keyPressEvent(
event);
332 setDragMode(QGraphicsView::ScrollHandDrag);
334 setDragMode(QGraphicsView::NoDrag);
340 setCursor(Qt::ArrowCursor);
343 setCursor(Qt::OpenHandCursor);
347 setCursor(Qt::SizeVerCursor);
350 setCursor(Qt::CrossCursor);
361 ic->setTemporaryIgnoreInteractions(
false);
364 ic->setTemporaryIgnoreMovable(
false);
367 ic->setTemporaryIgnoreMovable(
true);
369 ic->setTemporaryIgnoreInteractions(
true);
389 ic->attachToView(
this);
398 ic->setTemporaryIgnoreInteractions(
false);
399 ic->setTemporaryIgnoreMovable(
false);
400 ic->real_detachFromView();
410 viewport()->update();
414 QGraphicsView::mouseDoubleClickEvent(
event);
420 if (
ic->itemBelongsAndIsPresentlyActive(
item)) {
426 QGraphicsView::mouseDoubleClickEvent(
event);
440 ViewportAnchor
save = transformationAnchor();
441 setTransformationAnchor(AnchorViewCenter);
443 setTransformationAnchor(
save);
450 if (
ic->itemBelongsAndIsPresentlyActive(
item)) {
457 QGraphicsView::mouseMoveEvent(
event);
464 setFocus(Qt::MouseFocusReason);
468 viewport()->update();
486 if (
event->buttons()==(Qt::LeftButton|Qt::MiddleButton|Qt::RightButton)) {
503 if (
event->buttons()!=Qt::LeftButton) {
504 QGraphicsView::mousePressEvent(
event);
516 QPoint pickpoint =
event->pos();
517 if (continueselection) {
522 QGraphicsView::mousePressEvent(
event);
534 ic->itemPickedPrivate(
item);
554 QGraphicsView::mousePressEvent(
event);
597 QGraphicsView::mouseReleaseEvent(
event);
609 const double tol = 2.0;
610 QRectF tolrect(
pos.x()-tol,
pos.y()-tol, 2*tol, 2*tol );
615 ptmp.addEllipse(tolrect);
617 transform->inverseTransformPath(
view->mapToScene(ptmp),ptmp2);
618 QPainterPath
p =
view->mapFromScene(ptmp2);
620 p.addEllipse(tolrect);
624 for (QGraphicsItem* item2 :
view->items(
p)) {
633 if (ic2->itemBelongsAndIsPresentlyActive(item2)) {
650 QMapIterator<QGraphicsItem*,HighLightItem*> ittest(selecteditems);
651 assert(ittest.findNext(hlitem));
655 it = selecteditems.begin(),
itE = selecteditems.end();
657 if (
it.value()==hlitem) {
658 selecteditems.erase(
it);
663 if (ic_multiselection) {
664 view->emitSelectionChanged(ic_multiselection);
667 QMapIterator<QGraphicsItem*,HighLightItem*> ittest2(selecteditems);
668 assert(!ittest2.findNext(hlitem));
683 while (
it.hasNext()) {
685 it.value()->sendsignalondelete =
false;
687 if (
m_d->
scene->items().contains(
it.value())) {
705 if (selecteditems.count()==1) {
706 view->clearSelections();
710 assert(selecteditems.contains(
item));
712 if (scene->items().contains(
it.value())) {
713 assert(scene->items().contains(
it.value()));
714 scene->removeItem(
it.value());
715 it.value()->sendsignalondelete =
false;
718 assert(selecteditems.contains(
item));
719 selecteditems.erase(
it);
720 assert(!selecteditems.contains(
item));
726 assert(!selecteditems.contains(
item));
731 outline->setBrush(QColor(250,0,250,100));
732 outline->setPen(QPen(
Qt::red,0,Qt::SolidLine,Qt::SquareCap,Qt::RoundJoin));
733 scene->addItem(outline);
735 selecteditems.insert(
item,outline);
744 if (continueselection) {
745 if (selecteditems.contains(
item))
751 if (!selecteditems.empty()) {
752 clearselectionsemitsignals=
false;
753 view->clearSelections();
772 QRectF currentview = mapToScene(viewport()->rect()).boundingRect();
773 double w1= currentview.width(),
h1= currentview.height();
775 double x2 = targetpoint.x()-0.5*w2,
y2 = targetpoint.y()-0.5*h2;
788 QRectF currentview = mapToScene(viewport()->rect()).boundingRect();
790 double w1 = currentview.width(),
h1 = currentview.height();
791 double x1 = currentview.x(),
y1 = currentview.y();
793 double w2 = goal.width(), h2 = goal.height();
794 double x2 = goal.x(),
y2 = goal.y();
797 for (
double f = eps;
f < 1.0+0.5*eps;
f+=eps ) {
807 setRenderHints(QPainter::RenderHints());
867 const bool animatedhome =
true;
881 fitInView(
m_d->
home, Qt::KeepAspectRatio );
888 m_d->
home=mapToScene(viewport()->rect()).boundingRect();
897 ic->selectionChangedPrivate(emitlist);
909 ic->setTemporaryIgnoreInteractions(
true);
913 ic->setTemporaryIgnoreInteractions(
false);
926 ic->setTemporaryIgnoreMovable(
true);
930 ic->setTemporaryIgnoreMovable(
false);
939 QPrintDialog dialog(&printer,
this);
941 QPainter painter(&printer);
961 QString
filename = QFileDialog::getSaveFileName(0,
"Select target image file", guess,
962 "Image (*.png *.bmp)",
963 0,QFileDialog::DontResolveSymlinks);
967 QPixmap pm = viewport()->grab();
968 if (!(
filename.endsWith(
".png",Qt::CaseInsensitive)||
filename.endsWith(
".bmp",Qt::CaseInsensitive)))
992 QRect imgrect = viewport()->rect();
993 imgrect.setWidth(imgrect.width()*3);
994 imgrect.setHeight(imgrect.height()*3);
997 painter->setRenderHint(QPainter::Antialiasing,
false);
998 painter->drawImage(mapToScene(viewport()->rect()).boundingRect(),*(
m_d->
helptextimage),
1005 if(helptext.isEmpty()) {
1006 helptext.setHtml (
"<html>\n"
1008 "<h2>How to navigate a VP1GraphicsView</h2>\n"
1010 " <p>\n <b>H/F1</b> Show this help."
1011 " \n <b>S</b> Seek mode.\n"
1012 " \n <b>+/-</b> Zoom.\n"
1013 " \n <b>CTRL+HOME</b> Set home.\n"
1014 " \n <b>HOME</b> View home.\n"
1015 " \n <b>1</b> View all.\n"
1016 " \n <b>ESC</b> Switch between view and pick mode.\n"
1017 " \n <b>P</b> Print view.\n"
1018 " \n <b>CTRL+P</b> Save snapshot to image file.</p>\n"
1019 " <p>\n <b>Mouse wheel</b> zoom. Use <b>CTRL</b> and <b>SHIFT</b> for finegrained control.</p>\n"
1020 " <p>\n To select items (in pick mode), click on them. Selected multiple by holding down <b>SHIFT</b>.</p>\n"
1023 " Please report any bugs to <i>Thomas.Kittelmann@cern.ch</i>.\n"
1028 QFont myfont(
"Helvetica",36);
1030 helptext.setDefaultFont ( myfont );
1034 delete helptextimage;
1035 helptextimage =
new QImage(imrect.size(), QImage::Format_ARGB32);
1038 painter.begin(helptextimage);
1040 int pageWidth = qMax(imrect.width() - 100, 100);
1041 int pageHeight = qMax(imrect.height() - 100, 100);
1042 if (pageWidth != helptext.pageSize().width()) {
1043 helptext.setPageSize(QSize(pageWidth, pageHeight));
1046 QRect textRect(imrect.width() / 2 - pageWidth / 2,
1047 imrect.height() / 2 - pageHeight / 2,
1051 QRect clearRect = textRect.adjusted(-pad, -pad, pad, pad);
1052 painter.setPen(Qt::NoPen);
1053 painter.setBrush(QColor(0, 0, 0, 63));
1055 painter.drawRect(clearRect.x() + clearRect.width() + 1,
1056 clearRect.y() + shade,
1058 clearRect.height() + 1);
1059 painter.drawRect(clearRect.x() + shade,
1060 clearRect.y() + clearRect.height() + 1,
1061 clearRect.width() - shade + 1,
1064 painter.setRenderHint(QPainter::Antialiasing,
false);
1065 painter.setBrush(QColor(255, 255, 255, 220));
1066 painter.setPen(Qt::black);
1067 painter.drawRect(clearRect);
1069 painter.setClipRegion(textRect, Qt::IntersectClip);
1070 painter.translate(textRect.topLeft());
1072 QAbstractTextDocumentLayout::PaintContext ctx;
1074 QLinearGradient
g(0, 0, 0, textRect.height());
1075 g.setColorAt(0, Qt::black);
1076 g.setColorAt(0.9, Qt::black);
1077 g.setColorAt(1, Qt::transparent);
1079 QPalette pal =
view->viewport()->palette();
1080 pal.setBrush(QPalette::Text,
g);
1083 ctx.clip = QRect(0, 0, textRect.width(), textRect.height());
1084 helptext.documentLayout()->draw(&painter, ctx);
1092 QGraphicsItem *
items[],
1093 const QStyleOptionGraphicsItem
options[])
1097 QGraphicsView::drawItems(painter,numItems,
items,
options);
1102 for (
int i = 0;
i < numItems; ++
i) {
1105 painter->setTransform(
item->sceneTransform(),
true);