/** * Tablet events are handled here * @param event event info */ bool CanvasView::viewportEvent(QEvent *event) { if(event->type() == QEvent::TabletMove) { // Stylus moved QTabletEvent *tabev = static_cast<QTabletEvent*>(event); tabev->accept(); const dpcore::Point point(mapToScene(tabev->pos()), tabev->pressure()); if(!_prevpoint.intSame(point)) { if(_isdragging) moveDrag(tabev->x(), tabev->y()); else { if(_pendown) { if(point.pressure()==0) { // Missed a release event _pendown = NOTDOWN; onPenUp(); } else { onPenMove(point); } } updateOutline(point); } _prevpoint = point; } } else if(event->type() == QEvent::TabletPress) { // Stylus touches the tablet surface QTabletEvent *tabev = static_cast<QTabletEvent*>(event); tabev->accept(); if(_dragbtndown) { startDrag(tabev->x(), tabev->y(), _dragbtndown); } else { if(_pendown == NOTDOWN) { const dpcore::Point point(mapToScene(tabev->pos()), tabev->pressure()); _pendown = TABLETDOWN; onPenDown(point, false); updateOutline(point); _prevpoint = point; } } } else if(event->type() == QEvent::TabletRelease) { // Stylus lifted QTabletEvent *tabev = static_cast<QTabletEvent*>(event); tabev->accept(); if(_isdragging) { stopDrag(); } else if(_pendown == TABLETDOWN) { dpcore::Point point(mapToScene(tabev->pos()), 0); updateOutline(point); _prevpoint = point; _pendown = NOTDOWN; onPenUp(); } } else { return QGraphicsView::viewportEvent(event); } return true; }
/** * Tablet events are handled here * @param event event info */ bool EditorView::viewportEvent(QEvent *event) { if(event->type() == QEvent::TabletMove) { // Stylus moved QTabletEvent *tabev = static_cast<QTabletEvent*>(event); tabev->accept(); const dpcore::Point point(mapToScene(tabev->pos()), tabev->pressure()); if(!prevpoint_.intSame(point)) { if(isdragging_) moveDrag(tabev->x(), tabev->y()); else { if(pendown_) { if(point.pressure()==0) { // Missed a release event pendown_ = NOTDOWN; emit penUp(); } else { emit penMove(point); } } updateOutline(point); } prevpoint_ = point; } } else if(event->type() == QEvent::TabletPress) { // Stylus touches the tablet surface QTabletEvent *tabev = static_cast<QTabletEvent*>(event); tabev->accept(); if(dragbtndown_) { startDrag(tabev->x(), tabev->y(), dragbtndown_); } else { if(pendown_ == NOTDOWN) { const dpcore::Point point(mapToScene(tabev->pos()), tabev->pressure()); pendown_ = TABLETDOWN; emit penDown(point); updateOutline(point); prevpoint_ = point; } } } else if(event->type() == QEvent::TabletRelease) { // Stylus lifted QTabletEvent *tabev = static_cast<QTabletEvent*>(event); tabev->accept(); if(isdragging_) { stopDrag(); } else if(pendown_ == TABLETDOWN) { dpcore::Point point(mapToScene(tabev->pos()), 0); updateOutline(point); prevpoint_ = point; pendown_ = NOTDOWN; emit penUp(); } } else { return QGraphicsView::viewportEvent(event); } return true; }
/** * Convert a dpcore::Point to network format. The * reverse operation for this is in statetracker.cpp * @param p * @return */ protocol::PenPoint pointToProtocol(const dpcore::Point &p) { // The two least significant bits of the coordinate // are the fractional part. // The rest is the integer part with a bias of 128 uint16_t x = (qMax(0, p.x() + 128) << 2) | (uint16_t(p.xFrac()*4) & 3); uint16_t y = (qMax(0, p.y() + 128) << 2) | (uint16_t(p.yFrac()*4) & 3); return protocol::PenPoint(x, y, p.pressure() * 255); }
/** * @param point stroke coordinates to send */ void SessionState::sendStrokePoint(const dpcore::Point& point) { host_->sendPacket(protocol::StrokePoint( host_->localuser_, point.x(), point.y(), point.xFrac(), point.yFrac(), qRound(point.pressure()*255) ) ); }
/** * @param from point from which the line begins * @param to point at which the line ends * @param brush brush to draw the line with */ void Preview::preview(const dpcore::Point& from, const dpcore::Point& to, const dpcore::Brush& brush) { brush_ = brush; from_ = from; to_ = to; const int rad = brush.radius(to.pressure()); QColor color = brush.color(to.pressure()); QPen pen; if(rad==0) { pen.setWidth(1); color.setAlphaF(brush.opacity(to.pressure())); } else { pen.setWidth(rad*2); pen.setCapStyle(Qt::RoundCap); pen.setJoinStyle(Qt::RoundJoin); // Approximate brush transparency const qreal a = brush.opacity(to.pressure()) * rad * (1-brush.spacing()/100.0); color.setAlphaF(qMin(a, 1.0)); } pen.setColor(color); initAppearance(pen); }