Skip to content

Commit

Permalink
Make interaction and drawing of barlines consistent with other clicka…
Browse files Browse the repository at this point in the history
…ble items.

Bug: #23, #192, #220
  • Loading branch information
cameronwhite committed May 23, 2021
1 parent 2a271ca commit 3a34992
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
11 changes: 7 additions & 4 deletions source/app/powertabeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,14 +766,17 @@ void PowerTabEditor::removeSelectedItem()
{
switch (getCaret().getSelectedItem())
{
case ScoreItem::Barline:
case ScoreItem::Clef:
case ScoreItem::KeySignature:
case ScoreItem::ScoreInfo:
case ScoreItem::TimeSignature:
// Do nothing.
break;

case ScoreItem::Barline:
removeSelectedPositions();
break;

case ScoreItem::TempoMarker:
editTempoMarker(/* remove */ true);
break;
Expand Down Expand Up @@ -3607,6 +3610,7 @@ canDeleteItem(ScoreItem item)
{
case ScoreItem::AlterationOfPace:
case ScoreItem::AlternateEnding:
case ScoreItem::Barline:
case ScoreItem::Bend:
case ScoreItem::ChordText:
case ScoreItem::Direction:
Expand All @@ -3618,7 +3622,6 @@ canDeleteItem(ScoreItem item)
case ScoreItem::TextItem:
case ScoreItem::VolumeSwell:
return true;
case ScoreItem::Barline:
case ScoreItem::Clef:
case ScoreItem::KeySignature:
case ScoreItem::ScoreInfo:
Expand Down Expand Up @@ -3668,9 +3671,9 @@ void PowerTabEditor::updateCommands()
myRemoveSpaceCommand->setEnabled(!pos && (position == 0 || !barline) &&
!tempoMarker && !altEnding && !dynamic);
myRemoveItemCommand->setEnabled(
pos || barline || positions_selected ||
pos || positions_selected ||
canDeleteItem(getCaret().getSelectedItem()));
myRemovePositionCommand->setEnabled(pos || barline || positions_selected);
myRemovePositionCommand->setEnabled(pos || positions_selected);

myChordNameCommand->setChecked(
ScoreUtils::findByPosition(system.getChords(), position) != nullptr);
Expand Down
48 changes: 12 additions & 36 deletions source/painters/barlinepainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ BarlinePainter::BarlinePainter(const LayoutConstPtr &layout,
const ConstScoreLocation &location,
const ScoreClickEvent &click_event,
const QColor &barlineColor)
: myLayout(layout),
: ClickableItem(QObject::tr("Click to edit barline."), click_event,
location, ScoreItem::Barline),
myLayout(layout),
myBarline(barline),
myLocation(location),
myClickEvent(click_event),
myX(0),
myWidth(0),
myBarlineColor(barlineColor)
{
setAcceptHoverEvents(true);
setToolTip(QObject::tr("Click to edit barline."));

switch (barline.getBarType())
{
case Barline::SingleBar:
Expand Down Expand Up @@ -75,42 +72,21 @@ BarlinePainter::BarlinePainter(const LayoutConstPtr &layout,
layout->getStaffHeight());
}

void BarlinePainter::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// Only handle clicks that occur in the standard notation staff.
if (!isInStdNotationStaff(event->pos().y()))
event->ignore();
}

void BarlinePainter::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
// TODO - unify selection / click behaviour with other selectable items.
myClickEvent.signal(ScoreItem::Barline, myLocation,
ScoreItemAction::DoubleClicked);
}

void BarlinePainter::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
if (isInStdNotationStaff(event->pos().y()))
setCursor(Qt::PointingHandCursor);
else
unsetCursor();
}

void BarlinePainter::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
unsetCursor();
}

bool BarlinePainter::isInStdNotationStaff(double y)
bool
BarlinePainter::filterMousePosition(const QPointF &pos) const
{
// Only allow clicking within the standard notation staff.
const double y = pos.y();
return (y <= myLayout->getBottomStdNotationLine()) &&
(y >= myLayout->getTopStdNotationLine());
}

void BarlinePainter::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *)
void
BarlinePainter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
ClickableItem::paint(painter, option, widget);

painter->setPen(QPen(myBarlineColor, 0.75));
painter->setBrush(myBarlineColor);

Expand Down
15 changes: 6 additions & 9 deletions source/painters/barlinepainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef PAINTERS_BARLINEPAINTER_H
#define PAINTERS_BARLINEPAINTER_H

#include "clickableitem.h"

#include <QGraphicsItem>
#include <memory>
#include <painters/layoutinfo.h>
Expand All @@ -26,7 +28,7 @@
class Barline;
class ScoreClickEvent;

class BarlinePainter : public QGraphicsItem
class BarlinePainter : public ClickableItem
{
public:
BarlinePainter(const LayoutConstPtr &layout, const Barline &barline,
Expand All @@ -43,20 +45,15 @@ class BarlinePainter : public QGraphicsItem
return myBounds;
}

private:
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *) override;
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override;
protected:
bool filterMousePosition(const QPointF &pos) const override;

bool isInStdNotationStaff(double y);
private:
void drawVerticalLines(QPainter *painter, double myX);

LayoutConstPtr myLayout;
const Barline &myBarline;
QRectF myBounds;
ConstScoreLocation myLocation;
const ScoreClickEvent &myClickEvent;
double myX;
double myWidth;
const QColor &myBarlineColor;
Expand Down
26 changes: 23 additions & 3 deletions source/painters/clickableitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "styles.h"

#include <QCursor>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <type_traits>
Expand All @@ -42,19 +43,38 @@ ClickableItemT<GraphicsItemT>::ClickableItemT(

template <typename GraphicsItemT>
void
ClickableItemT<GraphicsItemT>::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *)
ClickableItemT<GraphicsItemT>::mouseDoubleClickEvent(
QGraphicsSceneMouseEvent *event)
{
if (!filterMousePosition(event->pos()))
{
event->ignore();
return;
}

myClickEvent.signal(myItem, myLocation, ScoreItemAction::DoubleClicked);
}

template <typename GraphicsItemT>
void
ClickableItemT<GraphicsItemT>::hoverEnterEvent(QGraphicsSceneHoverEvent *)
ClickableItemT<GraphicsItemT>::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
this->setCursor(Qt::PointingHandCursor);
if (filterMousePosition(event->pos()))
this->setCursor(Qt::PointingHandCursor);

++myPendingHoverLeaveEvents;
}

template <typename GraphicsItemT>
void
ClickableItemT<GraphicsItemT>::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
if (filterMousePosition(event->pos()))
this->setCursor(Qt::PointingHandCursor);
else
this->unsetCursor();
}

template <typename GraphicsItemT>
void
ClickableItemT<GraphicsItemT>::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
Expand Down
6 changes: 6 additions & 0 deletions source/painters/clickableitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ClickableItemT : public GraphicsItemT
virtual void mouseDoubleClickEvent(
QGraphicsSceneMouseEvent *event) override;
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;

virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change,
Expand All @@ -43,6 +44,11 @@ class ClickableItemT : public GraphicsItemT
QWidget *widget) override;

protected:
virtual bool filterMousePosition(const QPointF &) const
{
return true;
}

const ScoreClickEvent &myClickEvent;
const ConstScoreLocation myLocation;
const ScoreItem myItem;
Expand Down

0 comments on commit 3a34992

Please sign in to comment.