void WaveformRendererPreroll::draw(QPainter* painter, QPaintEvent* event) { Q_UNUSED(event); const TrackPointer track = m_waveformRenderer->getTrackInfo(); if (!track) { return; } double samplesPerPixel = m_waveformRenderer->getVisualSamplePerPixel(); double numberOfSamples = m_waveformRenderer->getLength() * samplesPerPixel; int currentPosition = m_waveformRenderer->getPlayPosVSample(); //qDebug() << "currentPosition" << currentPosition // << "samplesPerPixel" << samplesPerPixel // << "numberOfSamples" << numberOfSamples; // Some of the pre-roll is on screen. Draw little triangles to indicate // where the pre-roll is located. if (currentPosition < numberOfSamples / 2.0) { int index = static_cast<int>(numberOfSamples / 2.0 - currentPosition); const int polyLength = static_cast<int>(40.0 / samplesPerPixel); const float halfBreadth = m_waveformRenderer->getBreadth()/2.0; const float halfPolyBreadth = m_waveformRenderer->getBreadth()/5.0; painter->save(); painter->setRenderHint(QPainter::Antialiasing); //painter->setRenderHint(QPainter::HighQualityAntialiasing); //painter->setBackgroundMode(Qt::TransparentMode); painter->setWorldMatrixEnabled(false); painter->setPen(QPen(QBrush(m_color), 1)); // Rotate if drawing vertical waveforms if (m_waveformRenderer->getOrientation() == Qt::Vertical) { painter->setTransform(QTransform(0, 1, 1, 0, 0, 0)); } QPolygonF polygon; polygon << QPointF(0, halfBreadth) << QPointF(-polyLength, halfBreadth - halfPolyBreadth) << QPointF(-polyLength, halfBreadth + halfPolyBreadth); // Draw at most one not or halve visible polygon at the widget borders if (index > (numberOfSamples + ((polyLength + 1) * samplesPerPixel))) { int rest = index - numberOfSamples; rest %= (int)((polyLength + 1) * samplesPerPixel); index = numberOfSamples + rest; } polygon.translate(((qreal)index) / samplesPerPixel, 0); while (index > 0) { painter->drawPolygon(polygon); polygon.translate(-(polyLength + 1), 0); index -= (polyLength + 1) * samplesPerPixel; } painter->restore(); } }
/** * Creates and removes handle instances as necessary to adapt to a new object * selection. */ void EditPolygonTool::updateHandles() { const QSet<MapObjectItem*> &selection = mapScene()->selectedObjectItems(); // First destroy the handles for objects that are no longer selected QMutableMapIterator<MapObjectItem*, QList<PointHandle*> > i(mHandles); while (i.hasNext()) { i.next(); if (!selection.contains(i.key())) { const auto &handles = i.value(); for (PointHandle *handle : handles) { if (handle->isSelected()) mSelectedHandles.remove(handle); delete handle; } i.remove(); } } MapRenderer *renderer = mapDocument()->renderer(); for (MapObjectItem *item : selection) { const MapObject *object = item->mapObject(); if (!object->cell().isEmpty()) continue; QPolygonF polygon = object->polygon(); polygon.translate(object->position()); QList<PointHandle*> pointHandles = mHandles.value(item); // Create missing handles while (pointHandles.size() < polygon.size()) { PointHandle *handle = new PointHandle(item, pointHandles.size()); pointHandles.append(handle); mapScene()->addItem(handle); } // Remove superfluous handles while (pointHandles.size() > polygon.size()) { PointHandle *handle = pointHandles.takeLast(); if (handle->isSelected()) mSelectedHandles.remove(handle); delete handle; } // Update the position of all handles for (int i = 0; i < pointHandles.size(); ++i) { const QPointF &point = polygon.at(i); const QPointF handlePos = renderer->pixelToScreenCoords(point); const QPointF internalHandlePos = handlePos - item->pos(); pointHandles.at(i)->setPos(item->mapToScene(internalHandlePos)); } mHandles.insert(item, pointHandles); } }
void IsometricRenderer::drawMapObject(QPainter *painter, const MapObject *object, const QColor &color) const { painter->save(); QPen pen(Qt::black); if (object->tile()) { const QPixmap &img = object->tile()->image(); QPointF paintOrigin(-img.width() / 2, -img.height()); paintOrigin += tileToPixelCoords(object->position()).toPoint(); painter->drawPixmap(paintOrigin, img); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); } else { QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidth(2); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Draw the object name // TODO: Do something sensible to make null-sized objects usable QPolygonF polygon = tileRectToPolygon(object->bounds()); // Make sure the line aligns nicely on the pixels if (pen.width() % 2) painter->translate(0.5, 0.5); painter->drawPolygon(polygon); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); polygon.translate(0, -1); painter->drawPolygon(polygon); } painter->restore(); }
static PyObject *meth_QPolygonF_translate(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { const QPointF* a0; int a0State = 0; QPolygonF *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QPolygonF, &sipCpp, sipType_QPointF, &a0, &a0State)) { sipCpp->translate(*a0); sipReleaseType(const_cast<QPointF *>(a0),sipType_QPointF,a0State); Py_INCREF(Py_None); return Py_None; } } { qreal a0; qreal a1; QPolygonF *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "Bdd", &sipSelf, sipType_QPolygonF, &sipCpp, &a0, &a1)) { sipCpp->translate(a0,a1); Py_INCREF(Py_None); return Py_None; } } /* Raise an exception if the arguments couldn't be parsed. */ sipNoMethod(sipParseErr, sipName_QPolygonF, sipName_translate, doc_QPolygonF_translate); return NULL; }
YigModField::YigModField(YigSynthGraphic *synthGraphic, float param, qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent) : QGraphicsEllipseItem( x, y, width, height, parent ), modArc(this), yigAttractor(this), modTriangle(this) { //createModCircle(); parentGraphic = synthGraphic; setTransformOriginPoint(boundingRect().width()/2, boundingRect().height()/2); rotationAngle = param * 360; setRotation(fmod(rotationAngle+180, 360)); transformOriginPoint(); updateParameter(fmod((rotationAngle+180), 360)/360); modTriangle.setZValue(99); QPolygonF modPolygon; modPolygon.append(QPointF(0, 0)); modPolygon.append(QPointF(5, 15)); modPolygon.append(QPointF(-5, 15)); modPolygon.translate(width/2, 0); modTriangle.setPolygon(modPolygon); QPen modPen(Qt::NoPen); modTriangle.setPen(modPen); QColor modColor(YigColorKit::background2); modColor.setAlpha(200); QBrush modBrush(modColor); modTriangle.setBrush(modBrush); // Mod amount /* modElectron = new QGraphicsEllipseItem(0, 0, 25, 25, this); modElectron->moveBy(0, (boundingRect().width()/2) - 3); modElectron->setFlag( QGraphicsItem::ItemIsSelectable, false ); modElectron->setFlag( QGraphicsItem::ItemIsMovable, false ); modElectron->setCacheMode( QGraphicsItem::ItemCoordinateCache ); QColor modColor(YigColorKit::background2); modColor.setAlpha(200); modElectron->setPen(QPen(modColor)); modColor.setAlpha(20); modElectron->setBrush(modColor);*/ /* QColor modFill(YigColorKit::background2); modFill.setAlpha(100); QColor modOutline(YigColorKit::background2); modOutline.setAlpha(220); modArc.setBrush(modFill); modArc.setPen(Qt::NoPen);*/ //redrawArc(); }
QPainterPath BallItem::path() const { QPainterPath path; QPolygonF collidingPlgn = collidingPolygon(); QMatrix m; m.rotate(rotation()); QPointF firstP = collidingPlgn.at(0); collidingPlgn.translate(-firstP.x(), -firstP.y()); path.addEllipse(collidingPlgn.boundingRect()); path = m.map(path); path.translate(firstP.x(), firstP.y()); return path; }
void CylindricalProjectionPrivate::translatePolygons( const QVector<QPolygonF *> &polygons, QVector<QPolygonF *> &translatedPolygons, qreal xOffset ) { // mDebug() << "Translation: " << xOffset; QVector<QPolygonF *>::const_iterator itPolygon = polygons.constBegin(); QVector<QPolygonF *>::const_iterator itEnd = polygons.constEnd(); for( ; itPolygon != itEnd; ++itPolygon ) { QPolygonF * polygon = new QPolygonF; *polygon = **itPolygon; polygon->translate( xOffset, 0 ); translatedPolygons.append( polygon ); } }
void HorizontalPaintingStrategy::drawIndents(const KoRulerPrivate *d, QPainter &painter) { QPolygonF polygon; painter.setBrush(d->ruler->palette().brush(QPalette::Base)); painter.setRenderHint( QPainter::Antialiasing ); qreal x; // Draw first line start indent if (d->rightToLeft) x = d->effectiveActiveRangeEnd() - d->firstLineIndent - d->paragraphIndent; else x = d->effectiveActiveRangeStart() + d->firstLineIndent + d->paragraphIndent; // convert and use the +0.5 to go to nearest integer so that the 0.5 added below ensures sharp lines x = int(d->viewConverter->documentToViewX(x) + d->offset + 0.5); polygon << QPointF(x+6.5, 0.5) << QPointF(x+0.5, 8.5) << QPointF(x-5.5, 0.5) << QPointF(x+5.5, 0.5); painter.drawPolygon(polygon); // draw the hanging indent. if (d->rightToLeft) x = d->effectiveActiveRangeStart() + d->endIndent; else x = d->effectiveActiveRangeStart() + d->paragraphIndent; // convert and use the +0.5 to go to nearest integer so that the 0.5 added below ensures sharp lines x = int(d->viewConverter->documentToViewX(x) + d->offset + 0.5); const int bottom = d->ruler->height(); polygon.clear(); polygon << QPointF(x+6.5, bottom - 0.5) << QPointF(x+0.5, bottom - 8.5) << QPointF(x-5.5, bottom - 0.5) << QPointF(x+5.5, bottom - 0.5); painter.drawPolygon(polygon); // Draw end-indent or paragraph indent if mode is rightToLeft qreal diff; if (d->rightToLeft) diff = d->viewConverter->documentToViewX(d->effectiveActiveRangeEnd() - d->paragraphIndent) + d->offset - x; else diff = d->viewConverter->documentToViewX(d->effectiveActiveRangeEnd() - d->endIndent) + d->offset - x; polygon.translate(diff, 0); painter.drawPolygon(polygon); }
void OnMonitorCornersItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) { /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) { * event->accept(); * return; }*/ if (event->buttons() & Qt::LeftButton) { QPointF mousePos = mapFromScene(event->scenePos()); QPolygonF p = polygon(); switch (m_mode) { case Corner: p.replace(m_selectedCorner, mousePos); m_modified = true; break; case Move: p.translate(mousePos - m_lastPoint); m_modified = true; break; case MoveSide: p[m_selectedCorner] += mousePos - m_lastPoint; p[(m_selectedCorner + 1) % 4] += mousePos - m_lastPoint; m_modified = true; break; default: break; } m_lastPoint = mousePos; setPolygon(p); } if (m_modified) { event->accept(); if (KdenliveSettings::monitorscene_directupdate()) { emit changed(); m_modified = false; } } else { event->ignore(); } }
void MainWindow::paintEvent(QPaintEvent *) { QPainter p(this); QPolygonF frame; frame.append(QPointF(0.0, 0.0)); frame.append(QPointF(0.0, 50.0)); frame.append(QPointF(50.0, 50.0)); frame.append(QPointF(50.0, 0.0)); frame.append(QPointF(30.0, 0.0)); frame.append(QPointF(30.0, 10.0)); frame.append(QPointF(20.0, 10.0)); frame.append(QPointF(20.0, 0.0)); frame.append(QPointF(10.0, 0.0)); qDebug("contains 30.0 30.0 = %d", frame.containsPoint(QPointF(30.0, 30.0), Qt::WindingFill)); qDebug("contains 25.0 0.0 = %d", frame.containsPoint(QPointF(25.0, 0.0), Qt::WindingFill)); frame.translate(QPointF(100, 100.0)); p.setPen(QPen(Qt::red, 1.0)); p.setBrush(Qt::NoBrush); p.drawPolygon(frame, Qt::WindingFill); p.setPen(QPen(Qt::blue, 2.0)); p.setBrush(QBrush(Qt::blue)); p.drawPoint(QPointF(130.0, 130.0)); p.setPen(QPen(Qt::green, 2.0)); p.setBrush(QBrush(Qt::green)); p.drawPoint(QPointF(125.0, 100.0)); }
void IsometricRenderer::drawTileSelection(QPainter *painter, const QRegion ®ion, const QColor &color, const QRectF &exposed) const { painter->setBrush(color); painter->setPen(Qt::NoPen); #if QT_VERSION < 0x050800 const auto rects = region.rects(); for (const QRect &r : rects) { #else for (const QRect &r : region) { #endif QPolygonF polygon = tileRectToScreenPolygon(r); if (QRectF(polygon.boundingRect()).intersects(exposed)) painter->drawConvexPolygon(polygon); } } void IsometricRenderer::drawMapObject(QPainter *painter, const MapObject *object, const QColor &color) const { painter->save(); QPen pen(Qt::black); pen.setCosmetic(true); const Cell &cell = object->cell(); if (!cell.isEmpty()) { const QSizeF size = object->size(); const QPointF pos = pixelToScreenCoords(object->position()); CellRenderer(painter).render(cell, pos, size, CellRenderer::BottomCenter); if (testFlag(ShowTileObjectOutlines)) { QPointF tileOffset; if (const Tile *tile = cell.tile()) tileOffset = tile->offset(); QRectF rect(QPointF(pos.x() - size.width() / 2 + tileOffset.x(), pos.y() - size.height() + tileOffset.y()), size); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(rect); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(rect); } } else if (object->shape() == MapObject::Text) { const QPointF pos = pixelToScreenCoords(object->position()); const auto& textData = object->textData(); painter->setFont(textData.font); painter->setPen(textData.color); painter->drawText(QRectF(pos, object->size()), textData.text, textData.textOption()); } else { const qreal lineWidth = objectLineWidth(); const qreal scale = painterScale(); const qreal shadowOffset = (lineWidth == 0 ? 1 : lineWidth) / scale; QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidthF(lineWidth); QPen colorPen(pen); colorPen.setColor(color); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Do something sensible to make null-sized objects usable switch (object->shape()) { case MapObject::Ellipse: { QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); qreal tw = map()->tileWidth(); qreal th = map()->tileHeight(); QPointF transformScale(1, 1); if (tw > th) transformScale = QPointF(1, th/tw); else transformScale = QPointF(tw/th, 1); QPointF l1 = polygon.at(1) - polygon.at(0); QPointF l2 = polygon.at(3) - polygon.at(0); QTransform trans; trans.scale(transformScale.x(), transformScale.y()); trans.rotate(45); QTransform iTrans = trans.inverted(); QPointF l1x = iTrans.map(l1); QPointF l2x = iTrans.map(l2); QSizeF ellipseSize(l1x.manhattanLength(), l2x.manhattanLength()); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->setPen(pen); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } painter->setBrush(Qt::NoBrush); painter->drawPolygon(polygon); painter->setPen(colorPen); painter->setBrush(Qt::NoBrush); painter->translate(QPointF(0, -shadowOffset)); painter->drawPolygon(polygon); painter->setBrush(brush); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } break; } case MapObject::Point: painter->translate(pixelToScreenCoords(object->position())); drawPointObject(painter, color); break; case MapObject::Rectangle: { QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); painter->drawPolygon(polygon); painter->setPen(colorPen); painter->setBrush(brush); polygon.translate(0, -shadowOffset); painter->drawPolygon(polygon); break; } case MapObject::Polygon: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); QPen thickPen(pen); QPen thickColorPen(colorPen); thickPen.setWidthF(thickPen.widthF() * 4); thickColorPen.setWidthF(thickColorPen.widthF() * 4); painter->drawPolygon(screenPolygon); painter->setPen(thickPen); painter->drawPoint(screenPolygon.first()); painter->setPen(colorPen); painter->setBrush(brush); screenPolygon.translate(0, -shadowOffset); painter->drawPolygon(screenPolygon); painter->setPen(thickColorPen); painter->drawPoint(screenPolygon.first()); break; } case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); QPen thickPen(pen); QPen thickColorPen(colorPen); thickPen.setWidthF(thickPen.widthF() * 4); thickColorPen.setWidthF(thickColorPen.widthF() * 4); painter->drawPolyline(screenPolygon); painter->setPen(thickPen); painter->drawPoint(screenPolygon.first()); pen.setColor(color); painter->setPen(pen); screenPolygon.translate(0, -shadowOffset); painter->drawPolyline(screenPolygon); painter->setPen(thickColorPen); painter->drawPoint(screenPolygon.first()); break; } case MapObject::Text: break; // already handled above } } painter->restore(); }
void TimelineWidget::paintEvent(QPaintEvent* event) { if (similar(mStart,mStop)) return; QWidget::paintEvent(event); QPainter painter(this); // QPen pointPen, pointLinePen; painter.setRenderHint(QPainter::Antialiasing); painter.setPen(Qt::NoPen); QColor gray0(240, 240, 240); QColor gray01(220, 220, 220); QColor gray1(200, 200, 200); QColor gray2(170, 170, 170); // darker QColor gray3(150, 150, 150); // even darker QColor gray4(100, 100, 100); // even darker QColor highlight(110, 214, 255); // color around highlighted circle in qslider on KDE. // Fill with white background color and grey plot area background color QBrush brush(Qt::SolidPattern); // = painter.brush(); brush.setColor(gray2); painter.setBrush(brush); painter.drawRoundedRect(this->mFullArea, 4, 4); // brush.setColor(gray01); brush.setColor(gray1); painter.setBrush(brush); painter.drawRoundedRect(this->mPlotArea, 4, 4); int margin = 1; // draw noncompacted interval for (unsigned i = 0; i < mNoncompactedIntervals.size(); ++i) { int start_p = this->mapTime2PlotX(mNoncompactedIntervals[i].mStartTime); int stop_p = this->mapTime2PlotX(mNoncompactedIntervals[i].mEndTime); QColor color = gray01; painter.fillRect(QRect(start_p, mPlotArea.top(), stop_p - start_p, mPlotArea.height()), color); } // draw all continous events for (unsigned i = 0; i < mEvents.size(); ++i) { if (!mContinousEvents.contains(mEvents[i].mGroup)) continue; int start_p = this->mapTime2PlotX(mEvents[i].mStartTime); int stop_p = this->mapTime2PlotX(mEvents[i].mEndTime); int level = std::distance(mContinousEvents.begin(), std::find(mContinousEvents.begin(), mContinousEvents.end(), mEvents[i].mGroup)); int level_max = mContinousEvents.size(); int thisHeight = (mPlotArea.height()) / level_max - margin * (level_max - 1) / level_max; int thisTop = mPlotArea.top() + level * thisHeight + level * margin; // QColor color = mEventColors[level % mEventColors.size()]; QColor color = mEvents[i].mColor; painter.fillRect(QRect(start_p, thisTop, stop_p - start_p, thisHeight), color); } // draw all singular events for (unsigned i = 0; i < mEvents.size(); ++i) { if (mContinousEvents.contains(mEvents[i].mGroup)) continue; int start_p = this->mapTime2PlotX(mEvents[i].mStartTime); // int stop_p = this->mapTime2PlotX(mEvents[i].mEndTime); int glyphWidth = 3; QRect rect(start_p - glyphWidth / 2, mPlotArea.top(), glyphWidth, mPlotArea.height()); brush.setColor(QColor(50, 50, 50)); painter.setBrush(brush); painter.drawRoundedRect(rect, 2, 2); // painter.fillRect(rect, gray4); if (rect.width() > 2 && rect.height() > 2) { rect.adjust(1, 1, -1, -1); painter.fillRect(rect, gray2); } } int offset_p = this->mapTime2PlotX(mPos); QPolygonF glyph; int z = 5; int h = mPlotArea.height(); glyph.push_back(QPointF(-z, 0)); glyph.push_back(QPointF(z, 0)); glyph.push_back(QPointF(z, 0.7 * h)); glyph.push_back(QPointF(0, h)); glyph.push_back(QPointF(-z, 0.7 * h)); glyph.translate(offset_p, 0); if (this->hasFocus() || mCloseToGlyph) painter.setPen(highlight); else painter.setPen(gray4); // QBrush brush(Qt::SolidPattern);// = painter.brush(); QRadialGradient radialGrad(QPointF(offset_p, h / 3), 2 * h / 3); radialGrad.setColorAt(0, gray0); // radialGrad.setColorAt(0.5, Qt::blue); radialGrad.setColorAt(1, gray2); brush = QBrush(radialGrad); // brush.setColor(gray0); painter.setBrush(brush); painter.drawPolygon(glyph); }
void IsometricRenderer::drawMapObjectDecorate(QPainter *painter, const MapObject *object, const QColor &color, bool decorate) const { painter->save(); QPen pen(Qt::black); if (object->tile()) { const QImage img = object->toImage(); QPointF paintOrigin(-img.width() / 2, -img.height()); paintOrigin += tileToPixelCoords(object->position()).toPoint(); painter->drawImage(paintOrigin, img); if (decorate) { pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); } } else { QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidth(2); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Draw the object name // TODO: Do something sensible to make null-sized objects usable switch (object->shape()) { case MapObject::Rectangle: { QPolygonF polygon = tileRectToPolygon(object->bounds()); painter->drawPolygon(polygon); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); polygon.translate(0, -1); painter->drawPolygon(polygon); break; } case MapObject::Polygon: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = tileToPixelCoords(polygon); painter->drawPolygon(screenPolygon); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); screenPolygon.translate(0, -1); painter->drawPolygon(screenPolygon); break; } case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = tileToPixelCoords(polygon); painter->drawPolyline(screenPolygon); pen.setColor(color); painter->setPen(pen); screenPolygon.translate(0, -1); painter->drawPolyline(screenPolygon); break; } } } painter->restore(); }
void IsometricRenderer::drawMapObject(QPainter *painter, const MapObject *object, const QColor &color) const { painter->save(); QPen pen(Qt::black); pen.setCosmetic(true); const Cell &cell = object->cell(); if (!cell.isEmpty()) { const Tile *tile = cell.tile; const QSize imgSize = tile->size(); const QPointF pos = pixelToScreenCoords(object->position()); const QPointF tileOffset = tile->tileset()->tileOffset(); // Draw the name before the transform is applied const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, imgSize.width() + 2); if (!name.isEmpty()) { const QPointF textPos = pos + tileOffset - QPointF(imgSize.width() / 2, 5 + imgSize.height()); painter->drawText(textPos + QPointF(1, 1), name); painter->setPen(color); painter->drawText(textPos, name); } CellRenderer(painter).render(cell, pos, CellRenderer::BottomCenter); if (testFlag(ShowTileObjectOutlines)) { QRectF rect(QPointF(pos.x() - imgSize.width() / 2 + tileOffset.x(), pos.y() - imgSize.height() + tileOffset.y()), imgSize); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(rect); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(rect); } } else { const qreal lineWidth = objectLineWidth(); const qreal scale = painterScale(); const qreal shadowOffset = (lineWidth == 0 ? 1 : lineWidth) / scale; QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidth(lineWidth); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Draw the object name // TODO: Do something sensible to make null-sized objects usable switch (object->shape()) { case MapObject::Ellipse: { QPointF topLeft(pixelToScreenCoords(object->bounds().topLeft())); QPointF bottomLeft(pixelToScreenCoords(object->bounds().bottomLeft())); QPointF topRight(pixelToScreenCoords(object->bounds().topRight())); const qreal headerX = bottomLeft.x(); const qreal headerY = topLeft.y(); QRectF rect(bottomLeft, topRight); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, rect.width() + 2); QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); float tw = map()->tileWidth(); float th = map()->tileHeight(); QPointF transformScale(1, 1); if (tw > th) transformScale = QPointF(1, th/tw); else transformScale = QPointF(tw/th, 1); QPointF l1 = polygon.at(1) - polygon.at(0); QPointF l2 = polygon.at(3) - polygon.at(0); QTransform trans; trans.scale(transformScale.x(), transformScale.y()); trans.rotate(45); QTransform iTrans = trans.inverted(); QPointF l1x = iTrans.map(l1); QPointF l2x = iTrans.map(l2); QSizeF ellipseSize(l1x.manhattanLength(), l2x.manhattanLength()); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->setPen(pen); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } painter->setBrush(Qt::NoBrush); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); pen.setColor(color); painter->setPen(pen); painter->setBrush(Qt::NoBrush); painter->translate(QPointF(0, -shadowOffset)); painter->drawPolygon(polygon); painter->setBrush(brush); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); break; } case MapObject::Rectangle: { QPointF topLeft(pixelToScreenCoords(object->bounds().topLeft())); QPointF bottomLeft(pixelToScreenCoords(object->bounds().bottomLeft())); QPointF topRight(pixelToScreenCoords(object->bounds().topRight())); const qreal headerX = bottomLeft.x(); const qreal headerY = topLeft.y(); QRectF rect(bottomLeft, topRight); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, rect.width() + 2); QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPointF(headerX, headerY - 5 + shadowOffset), name); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); polygon.translate(0, -shadowOffset); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPointF(headerX, headerY - 5), name); break; } case MapObject::Polygon: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); const QRectF polygonBoundingRect = screenPolygon.boundingRect(); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, polygonBoundingRect.width() + 2); if (!name.isEmpty()) painter->drawText(QPointF(polygonBoundingRect.left(), polygonBoundingRect.top() - 5 + shadowOffset), name); painter->drawPolygon(screenPolygon); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); screenPolygon.translate(0, -shadowOffset); painter->drawPolygon(screenPolygon); if (!name.isEmpty()) painter->drawText(QPointF(polygonBoundingRect.left(), polygonBoundingRect.top() - 5), name); break; } case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); painter->drawPolyline(screenPolygon); pen.setColor(color); painter->setPen(pen); screenPolygon.translate(0, -shadowOffset); painter->drawPolyline(screenPolygon); break; } } } painter->restore(); }
int Polygon::translate(lua_State * L) // ( int dx, int dy ) { QPolygonF* obj = ValueInstaller2<QPolygonF>::check( L, 1 ); obj->translate( Util::toDbl( L, 2 ), Util::toDbl( L, 3 ) ); return 0; }
/** * Update position the edge */ void GuiEdge::updatePosition (bool original_run) { GuiNode* pre = addGui (pred()); GuiNode* suc = addGui (succ()); edge_start_point_priv = mapFromItem( pre, pre->width()/2, pre->height()/2); edge_end_point_priv = mapFromItem( suc, suc->width()/2, suc->height()/2);//!!!rarely it not work if (pre == suc)//mesh edge { QPointF heigh (0, 2*pre->height()); QPointF middle (pre->pos().x() - 10, pre->pos().y() + pre->height()/2); QPointF middleDirUp = middle + heigh; QPointF middleDirDown = middle - heigh; edge_start_dir_priv = edge_start_point_priv + heigh; edge_end_dir_priv = edge_end_point_priv - heigh; QPolygonF polygon = suc->polygon(); polygon.translate (suc->pos()); getIntersection (QLineF (edge_start_point_priv, edge_start_dir_priv), polygon, &edge_start_point_priv); getIntersection (QLineF (edge_end_point_priv, edge_end_dir_priv), polygon, &edge_end_point_priv); QPainterPath path; path.moveTo (edge_start_point_priv); path.cubicTo (edge_start_dir_priv, middleDirUp, middle); path.cubicTo (middleDirDown, edge_end_dir_priv, edge_end_point_priv); edge_curve_priv = path; } else { edge_valid_priv = true; QPolygonF headPolygon = suc->polygon(); headPolygon.translate (suc->pos()); QPolygonF tailPolygon = pre->polygon(); tailPolygon.translate (pre->pos()); if (suc->real()) edge_valid_priv = edge_valid_priv && getIntersection (QLineF (edge_start_point_priv, edge_end_point_priv), headPolygon, &edge_end_point_priv) == QLineF::BoundedIntersection; if (pre->real()) edge_valid_priv = edge_valid_priv && getIntersection (QLineF (edge_start_point_priv, edge_end_point_priv), tailPolygon, &edge_start_point_priv) == QLineF::BoundedIntersection; QPointF delta = edge_start_point_priv - edge_end_point_priv; delta.setX(0); if (pre->real()) edge_start_dir_priv = (edge_start_point_priv + edge_end_point_priv)/2; else { if (pre->firstPred() != 0) edge_start_dir_priv = (suc->coor() - pre->firstPred()->pred()->coor())/8 + pre->coor(); else edge_start_dir_priv = edge_start_point_priv - delta/2; } if (suc->real()) edge_end_dir_priv = (edge_start_point_priv + edge_end_point_priv)/2; else if (suc->firstSucc() != 0) edge_end_dir_priv = (pre->coor() - suc->firstSucc()->succ()->coor())/8 + suc->coor(); else edge_end_dir_priv = edge_end_point_priv + delta/2; QPainterPath path; path.moveTo (edge_start_point_priv); path.cubicTo (edge_start_dir_priv, edge_end_dir_priv, edge_end_point_priv); if (edge_valid_priv) edge_curve_priv = path; } edge_top_left_priv.setX( min< qreal>( edge_start_point_priv.x(), edge_end_point_priv.x())); edge_top_left_priv.setY( min< qreal>( edge_start_point_priv.y(), edge_end_point_priv.y())); edge_bottom_right_priv.setX( max< qreal>( edge_start_point_priv.x(), edge_end_point_priv.x())); edge_bottom_right_priv.setY( max< qreal>( edge_start_point_priv.y(), edge_end_point_priv.y())); if (original_run) { if (Edge::succ() && !addAux (Edge::succ())->real() && Edge::succ()->firstSucc()) addGui (Edge::succ()->firstSucc())->updatePosition (false);//avoidance sharp corners if (Edge::pred() && !addAux (Edge::pred())->real() && Edge::pred()->firstPred()) addGui (Edge::pred()->firstPred())->updatePosition (false);//avoidance sharp corners GuiEdge* i = this; for (i; !i->startEdge(); i->pred()->firstPred() && (i = addGui (i->pred()->firstPred()))); i->updateLabels(); } prepareGeometryChange(); }
void IsometricRenderer::drawMapObject(QPainter *painter, const MapObject *object, const QColor &color) const { painter->save(); QPen pen(Qt::black); if (object->tile()) { const QPixmap &img = object->tile()->image(); QPointF paintOrigin(-img.width() / 2, -img.height()); paintOrigin += tileToPixelCoords(object->position()).toPoint(); painter->drawPixmap(paintOrigin, img); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, img.width() + 2); if (!name.isEmpty()) painter->drawText(QPoint(paintOrigin.x(), paintOrigin.y() - 5 + 1), name); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); if (!name.isEmpty()) painter->drawText(QPoint(paintOrigin.x(), paintOrigin.y() - 5), name); } else { QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidth(2); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Draw the object name // TODO: Do something sensible to make null-sized objects usable switch (object->shape()) { case MapObject::Ellipse: { QPointF topLeft(tileToPixelCoords(object->bounds().topLeft())); QPointF bottomLeft(tileToPixelCoords(object->bounds().bottomLeft())); QPointF topRight(tileToPixelCoords(object->bounds().topRight())); const qreal headerX = bottomLeft.x(); const qreal headerY = topLeft.y(); QRectF rect(bottomLeft, topRight); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, rect.width() + 2); QPolygonF polygon = tileRectToPolygon(object->bounds()); float tw = map()->tileWidth(); float th = map()->tileHeight(); QPointF transformScale(1, 1); if (tw > th) transformScale = QPointF(1, th/tw); else transformScale = QPointF(tw/th, 1); QPointF l1 = polygon.at(1) - polygon.at(0); QPointF l2 = polygon.at(3) - polygon.at(0); QTransform trans; trans.scale(transformScale.x(), transformScale.y()); trans.rotate(45); QTransform iTrans = trans.inverted(); QPointF l1x = iTrans.map(l1); QPointF l2x = iTrans.map(l2); QSizeF ellipseSize(l1x.manhattanLength(), l2x.manhattanLength()); painter->save(); painter->setPen(pen); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); painter->setBrush(Qt::NoBrush); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); pen.setColor(color); painter->setPen(pen); painter->setBrush(Qt::NoBrush); painter->translate(QPointF(0, -1)); painter->drawPolygon(polygon); painter->setBrush(brush); painter->save(); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); break; } case MapObject::Rectangle: { QPointF topLeft(tileToPixelCoords(object->bounds().topLeft())); QPointF bottomLeft(tileToPixelCoords(object->bounds().bottomLeft())); QPointF topRight(tileToPixelCoords(object->bounds().topRight())); const qreal headerX = bottomLeft.x(); const qreal headerY = topLeft.y(); QRectF rect(bottomLeft, topRight); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, rect.width() + 2); QPolygonF polygon = tileRectToPolygon(object->bounds()); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5 + 1), name); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); polygon.translate(0, -1); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); break; } case MapObject::Polygon: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = tileToPixelCoords(polygon); const QRectF polygonBoundingRect = screenPolygon.boundingRect(); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, polygonBoundingRect.width() + 2); if (!name.isEmpty()) painter->drawText(QPoint(polygonBoundingRect.left(), polygonBoundingRect.top() - 5 + 1), name); painter->drawPolygon(screenPolygon); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); screenPolygon.translate(0, -1); painter->drawPolygon(screenPolygon); if (!name.isEmpty()) painter->drawText(QPoint(polygonBoundingRect.left(), polygonBoundingRect.top() - 5), name); break; } case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = tileToPixelCoords(polygon); painter->drawPolyline(screenPolygon); pen.setColor(color); painter->setPen(pen); screenPolygon.translate(0, -1); painter->drawPolyline(screenPolygon); break; } } } painter->restore(); }
void Arrow::readGraphicAttributes(const QXmlStreamAttributes &attributes) { // Check for legacy arrow type auto legacyArrowType = attributes.value("type").toString(); // if not legacy type, read arrow tip type and return if ("ReactionArrow" != legacyArrowType // TODO make these names constants (see MolScene::produceChild()) && "MechanismArrow" != legacyArrowType) { d->arrowType = (ArrowType) (attributes.value("arrowType").toString().toInt()) ; d->spline = ! (attributes.value("splineDisabled").toString().toInt()); return; } // Code for legacy version if ("ReactionArrow" == legacyArrowType) { enum LegacyReactionArrowType { SingleArrow = 0, DoubleArrow, Equilibrium, EqRightShifted, EqLeftShifted }; // Arrow tip auto legacyReactionArrowType = (LegacyReactionArrowType) (attributes.value("arrowType").toString().toInt()); switch(legacyReactionArrowType) { case SingleArrow: setArrowType(UpperBackward | LowerBackward); break; case DoubleArrow: setArrowType(LowerForward | UpperForward | LowerBackward | UpperBackward); break; case Equilibrium: case EqRightShifted: case EqLeftShifted: setArrowType(UpperBackward); break; default: setArrowType(NoArrow); } // Coordinates QPointF origin(attributes.value("posx").toString().toDouble(), attributes.value("posy").toString().toDouble()); QLineF arrowLine(origin, origin + QPointF(attributes.value("endx").toString().toDouble(), attributes.value("endy").toString().toDouble())); setCoordinates(QPolygonF() << arrowLine.p1() << arrowLine.p2()); if (!scene()) return; // Fix equilibrium arrows: if (Equilibrium == legacyReactionArrowType || EqLeftShifted == legacyReactionArrowType || EqRightShifted == legacyReactionArrowType) { // shift both arrows in equilibrium QLineF normalVector = arrowLine.normalVector().unitVector(); QLineF unitVector = arrowLine.unitVector(); QPointF normalTranslation = 2*(normalVector.p2() - normalVector.p1()); QPointF unitTranslation = 15*(unitVector.p2() - unitVector.p1()); QLineF reverseArrowLine = arrowLine; arrowLine.translate(normalTranslation); reverseArrowLine.translate(-normalTranslation); if (EqRightShifted == legacyReactionArrowType) { reverseArrowLine.setP1(reverseArrowLine.p1() + unitTranslation); reverseArrowLine.setP2(reverseArrowLine.p2() - unitTranslation); } if (EqLeftShifted == legacyReactionArrowType) { arrowLine.setP1(arrowLine.p1() + unitTranslation); arrowLine.setP2(arrowLine.p2() - unitTranslation); } auto reverseArrow = new Arrow; reverseArrow->setParentItem(parentItem()); scene()->addItem(reverseArrow); reverseArrow->setCoordinates(QPolygonF() << reverseArrowLine.p1() << reverseArrowLine.p2()); reverseArrow->setArrowType(LowerForward); setCoordinates(QPolygonF() << arrowLine.p1() << arrowLine.p2()); } } if ("MechanismArrow" == legacyArrowType) { enum LegacyMechanismArrowType { SingleArrowRight = 0, SingleArrowLeft, DoubleMechanismArrow, SingleHookRight, SingleHookLeft, DoubleHook }; // Arrow tip auto legacyMechanismArrowType = (LegacyMechanismArrowType) (attributes.value("arrowType").toString().toInt()); switch(legacyMechanismArrowType) { case SingleArrowRight: setArrowType(UpperBackward | LowerBackward); break; case SingleArrowLeft: setArrowType(UpperForward | LowerForward); break; case DoubleMechanismArrow: setArrowType(LowerForward | UpperForward | LowerBackward | UpperBackward); break; case SingleHookRight: setArrowType(UpperBackward); break; case SingleHookLeft: setArrowType(UpperForward); break; case DoubleHook: setArrowType(UpperForward | UpperBackward); break; default: setArrowType(NoArrow); } // Setting coordinates QPolygonF points; for (int i = 0 ; i < 4 ; ++i) points << QPointF(attributes.value("p" + QString::number(i+1) + "x").toString().toDouble(), attributes.value("p" + QString::number(i+1) + "y").toString().toDouble()); points.translate(attributes.value("posx").toString().toDouble(), attributes.value("posy").toString().toDouble()); setCoordinates(points); } }
int main( int argc, char **argv ) { QApplication a( argc, argv ); QwtPlot plot; plot.setTitle( "Plot Demo" ); plot.setCanvasBackground( Qt::white ); plot.setAxisScale( QwtPlot::xBottom, -1.0, 6.0 ); QwtLegend *legend = new QwtLegend(); legend->setDefaultItemMode( QwtLegendData::Checkable ); plot.insertLegend( legend ); for ( int i = 0; i < 4; i++ ) { QwtPlotCurve *curve = new QwtPlotCurve(); curve->setRenderHint( QwtPlotItem::RenderAntialiased, true ); curve->setPen( Qt::blue ); QBrush brush; QwtSymbol::Style style = QwtSymbol::NoSymbol; QString title; if ( i == 0 ) { brush = Qt::magenta; style = QwtSymbol::Path; title = "Path"; } else if ( i == 2 ) { brush = Qt::red; style = QwtSymbol::Graphic; title = "Graphic"; } else if ( i == 1 ) { brush = Qt::yellow; style = QwtSymbol::SvgDocument; title = "Svg"; } else if ( i == 3 ) { brush = Qt::cyan; style = QwtSymbol::Pixmap; title = "Pixmap"; } MySymbol *symbol = new MySymbol( style, brush ); curve->setSymbol( symbol ); curve->setTitle( title ); curve->setLegendAttribute( QwtPlotCurve::LegendShowSymbol, true ); curve->setLegendIconSize( QSize( 15, 18 ) ); QPolygonF points; points << QPointF( 0.0, 4.4 ) << QPointF( 1.0, 3.0 ) << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 ) << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 ); points.translate( 0.0, i * 2.0 ); curve->setSamples( points ); curve->attach( &plot ); } plot.resize( 600, 400 ); plot.show(); return a.exec(); }
void DataCurve::loadData() { Graph *g = (Graph *)plot(); if (!g) return; int xcol = d_x_table->colIndex(d_x_column); int ycol = d_table->colIndex(title().text()); if (xcol < 0 || ycol < 0) { remove(); return; } int rows = d_table->numRows(); if (d_end_row < 0 || d_end_row >= rows) d_end_row = rows - 1; int xColType = d_x_table->columnType(xcol); int yColType = d_table->columnType(ycol); int r = abs(d_end_row - d_start_row) + 1; QPolygonF data; data.reserve(r); QStringList xLabels, yLabels;// store text labels int xAxis = QwtPlot::xBottom; if (d_type == Graph::HorizontalBars) xAxis = QwtPlot::yLeft; QString date_time_fmt = d_table->columnFormat(xcol); int size = 0, from = 0; d_data_ranges.clear(); for (int i = d_start_row; i <= d_end_row; i++ ) { QString xval = d_x_table->text(i, xcol); QString yval = d_table->text(i, ycol); if (!xval.isEmpty() && !yval.isEmpty()) { bool valid_data = true; QPointF p; if (xColType == Table::Text) { xLabels << xval; p.setX((double)(size + 1)); } else if (xColType == Table::Time) p.setX(Table::fromTime(QTime::fromString(xval.trimmed(), date_time_fmt))); else if (xColType == Table::Date) p.setX(Table::fromDateTime(QDateTime::fromString(xval.trimmed(), date_time_fmt))); else p.setX(g->locale().toDouble(xval, &valid_data)); if (yColType == Table::Text) { yLabels << yval; p.setY((double)(size + 1)); } else p.setY(g->locale().toDouble(yval, &valid_data)); if (valid_data) { data << p; size++; } } else if (from < size) { DataRange range; range.from = from; range.to = size - 1; d_data_ranges.push_back(range); from = size; } } if (d_data_ranges.size() && from < size) { DataRange range; range.from = from; range.to = size - 1; d_data_ranges.push_back(range); } if (!size) { remove(); return; } data.resize(size); if (g->isWaterfallPlot()) { int index = g->curveIndex(this); int curves = g->curveCount(); DataCurve *c = g->dataCurve(0); if (index > 0 && c) { double xmin = c->minXValue(); double dx = index*g->waterfallXOffset()*0.01*g->canvas()->width()/(double)(curves - 1); d_x_offset = g->invTransform(xAxis, g->transform(xAxis, xmin) + dx) - xmin; double ymin = c->minYValue(); double dy = index*g->waterfallYOffset()*0.01*g->canvas()->height()/(double)(curves - 1); d_y_offset = ymin - g->invTransform(yAxis(), g->transform(yAxis(), ymin) + dy); setZ(-index); setBaseline(d_y_offset); data.translate(d_x_offset, d_y_offset); } else { setZ(0); setBaseline(0.0); } if (g->grid()) g->grid()->setZ(-g->curveCount() - 1); } double speedTol = g->getDouglasPeukerTolerance(); if (speedTol != 0.0 && size >= g->speedModeMaxPoints()) { QwtWeedingCurveFitter *fitter = new QwtWeedingCurveFitter(speedTol); data = fitter->fitCurve(data); delete fitter; } if (d_type == Graph::HorizontalBars) { size = data.size(); for (int i = 0; i < size; i++) { QPointF p = data.at(i); data[i] = QPointF(p.y(), p.x()); } } setData(data); foreach(ErrorBarsCurve *c, d_error_bars) c->setData(data); if (xColType == Table::Text) g->setLabelsTextFormat(xAxis, ScaleDraw::Text, d_x_column, xLabels); if (yColType == Table::Text) g->setLabelsTextFormat(QwtPlot::yLeft, ScaleDraw::Text, title().text(), yLabels); setRenderHint(QwtPlotItem::RenderAntialiased, g->isCurveAntialiasingEnabled(this)); if (!d_labels_list.isEmpty()) { ((Graph*)plot())->updatePlot(); loadLabels(); } }
void QgsComposerMouseHandles::drawSelectedItemBounds( QPainter* painter ) { //draw dotted border around selected items to give visual feedback which items are selected QList<QgsComposerItem*> selectedItems = mComposition->selectedComposerItems(); if ( selectedItems.size() == 0 ) { return; } //use difference mode so that they are visible regardless of item colors painter->save(); painter->setCompositionMode( QPainter::CompositionMode_Difference ); // use a grey dashed pen - in difference mode this should always be visible QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) ); selectedItemPen.setStyle( Qt::DashLine ); selectedItemPen.setWidth( 0 ); painter->setPen( selectedItemPen ); painter->setBrush( Qt::NoBrush ); QList<QgsComposerItem*>::iterator itemIter = selectedItems.begin(); for ( ; itemIter != selectedItems.end(); ++itemIter ) { //get bounds of selected item QPolygonF itemBounds; if ( mIsDragging && !( *itemIter )->positionLock() ) { //if currently dragging, draw selected item bounds relative to current mouse position //first, get bounds of current item in scene coordinates QPolygonF itemSceneBounds = ( *itemIter )->mapToScene(( *itemIter )->rectWithFrame() ); //now, translate it by the current movement amount //IMPORTANT - this is done in scene coordinates, since we don't want any rotation/non-translation transforms to affect the movement itemSceneBounds.translate( transform().dx(), transform().dy() ); //finally, remap it to the mouse handle item's coordinate system so it's ready for drawing itemBounds = mapFromScene( itemSceneBounds ); } else if ( mIsResizing && !( *itemIter )->positionLock() ) { //if currently resizing, calculate relative resize of this item if ( selectedItems.size() > 1 ) { //get item bounds in mouse handle item's coordinate system QRectF itemRect = mapRectFromItem(( *itemIter ), ( *itemIter )->rectWithFrame() ); //now, resize it relative to the current resized dimensions of the mouse handles QgsComposition::relativeResizeRect( itemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect ); itemBounds = QPolygonF( itemRect ); } else { //single item selected itemBounds = rect(); } } else { //not resizing or moving, so just map from scene bounds itemBounds = mapRectFromItem(( *itemIter ), ( *itemIter )->rectWithFrame() ); } painter->drawPolygon( itemBounds ); } painter->restore(); }
void IsometricRenderer::drawMapObject(QPainter *painter, const MapObject *object, const QColor &color) const { painter->save(); QPen pen(Qt::black); if (object->tile()) { const QPixmap &img = object->tile()->image(); QPointF paintOrigin(-img.width() / 2, -img.height()); paintOrigin += tileToPixelCoords(object->position()).toPoint(); painter->drawPixmap(paintOrigin, img); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, img.width() + 2); if (!name.isEmpty()) painter->drawText(QPoint(paintOrigin.x(), paintOrigin.y() - 5 + 1), name); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(QRectF(paintOrigin, img.size())); if (!name.isEmpty()) painter->drawText(QPoint(paintOrigin.x(), paintOrigin.y() - 5), name); } else { QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidth(2); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Draw the object name // TODO: Do something sensible to make null-sized objects usable switch (object->shape()) { case MapObject::Rectangle: { QPointF topLeft(tileToPixelCoords(object->bounds().topLeft())); QPointF bottomLeft(tileToPixelCoords(object->bounds().bottomLeft())); QPointF topRight(tileToPixelCoords(object->bounds().topRight())); const qreal headerX = bottomLeft.x(); const qreal headerY = topLeft.y(); QRectF rect(bottomLeft, topRight); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, rect.width() + 2); QPolygonF polygon = tileRectToPolygon(object->bounds()); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5 + 1), name); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); polygon.translate(0, -1); painter->drawPolygon(polygon); if (!name.isEmpty()) painter->drawText(QPoint(headerX, headerY - 5), name); break; } case MapObject::Polygon: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = tileToPixelCoords(polygon); const QRectF polygonBoundingRect = screenPolygon.boundingRect(); const QFontMetrics fm = painter->fontMetrics(); QString name = fm.elidedText(object->name(), Qt::ElideRight, polygonBoundingRect.width() + 2); if (!name.isEmpty()) painter->drawText(QPoint(polygonBoundingRect.left(), polygonBoundingRect.top() - 5 + 1), name); painter->drawPolygon(screenPolygon); pen.setColor(color); painter->setPen(pen); painter->setBrush(brush); screenPolygon.translate(0, -1); painter->drawPolygon(screenPolygon); if (!name.isEmpty()) painter->drawText(QPoint(polygonBoundingRect.left(), polygonBoundingRect.top() - 5), name); break; } case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = tileToPixelCoords(polygon); painter->drawPolyline(screenPolygon); pen.setColor(color); painter->setPen(pen); screenPolygon.translate(0, -1); painter->drawPolyline(screenPolygon); break; } } } painter->restore(); }
void IsometricRenderer::drawMapObject(QPainter *painter, const MapObject *object, const QColor &color) const { painter->save(); QPen pen(Qt::black); pen.setCosmetic(true); const Cell &cell = object->cell(); if (!cell.isEmpty()) { const Tile *tile = cell.tile; const QSize imgSize = tile->size(); const QPointF pos = pixelToScreenCoords(object->position()); const QPointF tileOffset = tile->offset(); CellRenderer(painter).render(cell, pos, object->size(), CellRenderer::BottomCenter); if (testFlag(ShowTileObjectOutlines)) { QRectF rect(QPointF(pos.x() - imgSize.width() / 2 + tileOffset.x(), pos.y() - imgSize.height() + tileOffset.y()), imgSize); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawRect(rect); pen.setStyle(Qt::DotLine); pen.setColor(color); painter->setPen(pen); painter->drawRect(rect); } } else { const qreal lineWidth = objectLineWidth(); const qreal scale = painterScale(); const qreal shadowOffset = (lineWidth == 0 ? 1 : lineWidth) / scale; QColor brushColor = color; brushColor.setAlpha(50); QBrush brush(brushColor); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); pen.setWidth(lineWidth); QPen colorPen(pen); colorPen.setColor(color); painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); // TODO: Do something sensible to make null-sized objects usable switch (object->shape()) { case MapObject::Ellipse: { QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); float tw = map()->tileWidth(); float th = map()->tileHeight(); QPointF transformScale(1, 1); if (tw > th) transformScale = QPointF(1, th/tw); else transformScale = QPointF(tw/th, 1); QPointF l1 = polygon.at(1) - polygon.at(0); QPointF l2 = polygon.at(3) - polygon.at(0); QTransform trans; trans.scale(transformScale.x(), transformScale.y()); trans.rotate(45); QTransform iTrans = trans.inverted(); QPointF l1x = iTrans.map(l1); QPointF l2x = iTrans.map(l2); QSizeF ellipseSize(l1x.manhattanLength(), l2x.manhattanLength()); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->setPen(pen); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } painter->setBrush(Qt::NoBrush); painter->drawPolygon(polygon); painter->setPen(colorPen); painter->setBrush(Qt::NoBrush); painter->translate(QPointF(0, -shadowOffset)); painter->drawPolygon(polygon); painter->setBrush(brush); if (ellipseSize.width() > 0 && ellipseSize.height() > 0) { painter->save(); painter->translate(polygon.at(0)); painter->scale(transformScale.x(), transformScale.y()); painter->rotate(45); painter->drawEllipse(QRectF(QPointF(0, 0), ellipseSize)); painter->restore(); } break; } case MapObject::Rectangle: { QPolygonF polygon = pixelRectToScreenPolygon(object->bounds()); painter->drawPolygon(polygon); painter->setPen(colorPen); painter->setBrush(brush); polygon.translate(0, -shadowOffset); painter->drawPolygon(polygon); break; } case MapObject::Polygon: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); QPen thickPen(pen); QPen thickColorPen(colorPen); thickPen.setWidthF(thickPen.widthF() * 4); thickColorPen.setWidthF(thickColorPen.widthF() * 4); painter->drawPolygon(screenPolygon); painter->setPen(thickPen); painter->drawPoint(screenPolygon.first()); painter->setPen(colorPen); painter->setBrush(brush); screenPolygon.translate(0, -shadowOffset); painter->drawPolygon(screenPolygon); painter->setPen(thickColorPen); painter->drawPoint(screenPolygon.first()); break; } case MapObject::Polyline: { const QPointF &pos = object->position(); const QPolygonF polygon = object->polygon().translated(pos); QPolygonF screenPolygon = pixelToScreenCoords(polygon); QPen thickPen(pen); QPen thickColorPen(colorPen); thickPen.setWidthF(thickPen.widthF() * 4); thickColorPen.setWidthF(thickColorPen.widthF() * 4); painter->drawPolyline(screenPolygon); painter->setPen(thickPen); painter->drawPoint(screenPolygon.first()); pen.setColor(color); painter->setPen(pen); screenPolygon.translate(0, -shadowOffset); painter->drawPolyline(screenPolygon); painter->setPen(thickColorPen); painter->drawPoint(screenPolygon.first()); break; } } } painter->restore(); }
void SourceWidget::mouseReleaseEvent(QMouseEvent *event) { // Once the user releases the mouse, the selection process is finished. We need to // extract the selection and update the glWidget if (event->button() == Qt::LeftButton) { // Draw the last line path.lineTo(event->pos()); path.connectPath(path); update(); // TODO: safety due to CGAL fail // if (path.length()<20) // return; QPolygonF selectionPoly = path.toFillPolygon(); QRectF boundingRect = selectionPoly.boundingRect(); // Don't pass bad selections on if (boundingRect.width()>20 && boundingRect.height()>20) ; else return; // adjusted - boundary value will be cut without it QImage sourcePatch = image.copy(boundingRect.toRect().adjusted(-1,-1,1,1)); // Pass source patch pixels to glWidet glWidget->setSourcePatch(sourcePatch); qreal x0,x1,y0,y1; boundingRect.getCoords(&x0,&y0,&x1,&y1); // Find a point on the boundary of the selection QPoint cPoint(-1,-1); for (int x=x0; x<=x1; x++) { for (int y=y0; y<=y1; y++) { if (selectionPoly.containsPoint(QPointF(x,y),Qt::WindingFill)) cPoint = QPoint(x,y); } } //assert(cPoint != QPoint(-1,-1)); //TODO: No violence please... if (cPoint == QPoint(-1,-1)){ qDebug() << "assert(cPoint != QPoint(-1,-1)) fails"; return; } // Track the boundary of the selection std::vector<Point> boundaryVector; // TODO: Why does it fail with dir=0 ??? int dir = 1; int x,y; int c=0; bool cont = true; do { x = cPoint.x(); y = cPoint.y(); cPoint = findStartingFromDir(selectionPoly ,x, y, dir); boundaryVector.push_back(Point((cPoint.x()-x0),((y1-y0) - (cPoint.y()-y0)))); if (boundaryVector.size()>3){ if(boundaryVector[0]==boundaryVector[c-1] && boundaryVector[1]==boundaryVector[c]){ boundaryVector.pop_back(); boundaryVector.pop_back(); cont = false; } } c++; } while (cont); if (boundaryVector.size()>8192) { path = QPainterPath(); update(); QMessageBox::warning(this, tr("Boundary Size"), tr("Largest supported boundary size is 8192")); return; } // // Subsample // std::vector<Point> boundaryVectorNew; // bool subs = false; // for(std::vector<Point>::const_iterator it = boundaryVector.begin(); it != boundaryVector.end(); ++it){ // if(subs) // boundaryVectorNew.push_back(Point((*it).x(),(*it).y())); // subs = 1-subs; // } // boundaryVector = boundaryVectorNew; // // Subsample // QPolygon selectionPolyNew; // int i = 0; // for(std::vector<Point>::const_iterator it = boundaryVector.begin(); it != boundaryVector.end(); ++it){ // selectionPolyNew.setPoint(i, (*it).x(), (*it).y()); // i++; // } // TODO: Since we didn't figure out the way yet to get from CGAL the information // about the whenever triangle inside the boundary, glWidget will do it with // the help of selectionPoly // QPolygon temp(boundaryVector); // selectionPoly = QPolygonF(temp); selectionPoly.translate(-x0,-y0); // boundingRect = selectionPoly.boundingRect(); // boundingRect.getCoords(&x0,&y0,&x1,&y1); glWidget->updateSelection(boundaryVector, selectionPoly); glWidget->update(); } }