Skip to content

Commit

Permalink
Spurious DragLeave fixed? (The worst and most annoying bug of PlotJug…
Browse files Browse the repository at this point in the history
…gler)
  • Loading branch information
facontidavide committed Nov 11, 2018
1 parent 2e59b1b commit 7959e54
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 80 deletions.
151 changes: 81 additions & 70 deletions plotter_gui/plotwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ PlotWidget::PlotWidget(PlotDataMapRef &datamap, QWidget *parent):
_current_transform( TimeseriesQwt::noTransform ),
_show_line_and_points(false),
_axisX(nullptr),
_time_offset(0.0)
_time_offset(0.0),
_dragging( { DragInfo::NONE, {} } )
{
this->setAcceptDrops( true );

Expand All @@ -83,7 +84,7 @@ PlotWidget::PlotWidget(PlotDataMapRef &datamap, QWidget *parent):
canvas->setPaintAttribute( QwtPlotCanvas::BackingStore, true );

this->setCanvas( canvas );
this->setCanvasBackground( QColor( 250, 250, 250 ) );
this->setCanvasBackground( Qt::white );
this->setAxisAutoScale(0, true);

this->axisScaleEngine(QwtPlot::xBottom)->setAttribute(QwtScaleEngine::Floating,true);
Expand Down Expand Up @@ -415,17 +416,32 @@ void PlotWidget::dragEnterEvent(QDragEnterEvent *event)
{
QByteArray encoded = mimeData->data( format );
QDataStream stream(&encoded, QIODevice::ReadOnly);
_dragging.curves.clear();
_dragging.source = event->source();

if( format.contains( "curveslist") )
while (!stream.atEnd())
{
QString curve_name;
stream >> curve_name;
_dragging.curves.push_back( curve_name );
}

if( format.contains( "curveslist/add_curve") )
{
_dragging.mode = DragInfo::CURVES;
event->acceptProposedAction();
}
if( format.contains( "curveslist/new_X_axis") && _dragging.curves.size() == 1 )
{
_dragging.mode = DragInfo::NEW_X;
event->acceptProposedAction();
}
if( format.contains( "plot_area") )
{
QString source_name;
stream >> source_name;

if(QString::compare( windowTitle(),source_name ) != 0 ){
if(_dragging.curves.size() == 1 &&
windowTitle() != _dragging.curves.front() )
{
_dragging.mode = DragInfo::SWAP_PLOTS;
event->acceptProposedAction();
}
}
Expand All @@ -435,57 +451,64 @@ void PlotWidget::dragEnterEvent(QDragEnterEvent *event)

void PlotWidget::dragLeaveEvent(QDragLeaveEvent*)
{
changeBackgroundColor( QColor( 250, 250, 250 ) );
QPoint local_pos = canvas()->mapFromGlobal(QCursor::pos()) ;
// prevent spurious exits
if( canvas()->rect().contains( local_pos ))
{
// changeBackgroundColor( QColor( 250, 150, 150 ) );
}
else{
changeBackgroundColor( Qt::white );
_dragging.mode = DragInfo::NONE;
_dragging.curves.clear();
}
}

void PlotWidget::dropEvent(QDropEvent *event)
void PlotWidget::dropEvent(QDropEvent *)
{
setCanvasBackground( QColor( 250, 250, 250 ) );

const QMimeData *mimeData = event->mimeData();
QStringList mimeFormats = mimeData->formats();
bool curves_changed = false;
bool background_changed = false;

for(const QString& format: mimeFormats)
if( _dragging.mode == DragInfo::CURVES)
{
QByteArray encoded = mimeData->data( format );
QDataStream stream(&encoded, QIODevice::ReadOnly);

if( format.contains( "curveslist/add_curve") )
for( const auto& curve_name : _dragging.curves)
{
bool curve_added = false;
while (!stream.atEnd())
{
QString curve_name;
stream >> curve_name;
bool added = addCurve( curve_name.toStdString() );
curve_added = curve_added || added;
}
if( curve_added )
{
zoomOut(false);
replot();
emit curveListChanged();
emit undoableChange();
}
event->acceptProposedAction();
bool added = addCurve( curve_name.toStdString() );
curves_changed = curves_changed || added;
}
else if( format.contains( "curveslist/new_X_axis") )
}
else if( _dragging.mode == DragInfo::NEW_X)
{
changeAxisX( _dragging.curves.front() );
curves_changed = true;
}
else if( _dragging.mode == DragInfo::SWAP_PLOTS )
{
auto plot_widget = dynamic_cast<PlotWidget*>(_dragging.source);
if( plot_widget )
{
QString curve_name;
stream >> curve_name;
changeAxisX(curve_name);
event->acceptProposedAction();
emit swapWidgetsRequested( plot_widget, this );
}
else if( format.contains( "plot_area") )
{
QString source_name;
stream >> source_name;
PlotWidget* source_plot = static_cast<PlotWidget*>( event->source() );
}
if( _dragging.mode != DragInfo::NONE &&
canvasBackground().color() != Qt::white)
{
this->setCanvasBackground( Qt::white );
background_changed = true;
}

emit swapWidgetsRequested( source_plot, this );
event->acceptProposedAction();
}
if( curves_changed )
{
zoomOut(false);
emit curveListChanged();
emit undoableChange();
}
if( curves_changed || background_changed)
{
replot();
}
_dragging.mode = DragInfo::NONE;
_dragging.curves.clear();
}

void PlotWidget::detachAllCurves()
Expand Down Expand Up @@ -1346,35 +1369,23 @@ bool PlotWidget::eventFilter(QObject *obj, QEvent *event)
}break;

case QEvent::Leave:
{
changeBackgroundColor( QColor( 250, 250, 250 ) );
}break;
//---------------------------------
case QEvent::MouseButtonRelease :
{
changeBackgroundColor( QColor( 250, 250, 250 ) );
QApplication::restoreOverrideCursor();
if( _dragging.mode == DragInfo::NONE )
{
changeBackgroundColor( Qt::white );
QApplication::restoreOverrideCursor();
}
}break;
//---------------------------------
case QEvent::KeyPress:

case QEvent::Enter:
{
// QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
// qDebug() << key_event->key();
// If you think that this code doesn't make sense, you are right.
// This is the workaround I have eventually found to avoid the problem with spurious
// QEvent::DragLeave (I have never found the origin of the bug).
dropEvent(nullptr);
}break;

case QEvent::DragEnter: {
this->dragEnterEvent( static_cast<QDragEnterEvent*>(event) );
} break; // drag moves into widget
case QEvent::DragMove: {
this->dragMoveEvent( static_cast<QDragMoveEvent*>(event ) );
} break;
case QEvent::DragLeave: {
this->dragLeaveEvent( static_cast<QDragLeaveEvent*>(event ) );
} break;
case QEvent::Drop: {
this->dropEvent( static_cast<QDropEvent*>(event ) );
} break;


} //end switch

Expand Down
9 changes: 8 additions & 1 deletion plotter_gui/plotwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ private slots:
PlotDataMapRef& _mapped_data;
TimeseriesQwt::Transform _current_transform;

struct DragInfo{
enum{ NONE, CURVES, NEW_X, SWAP_PLOTS} mode;
std::vector<QString> curves;
QObject* source;
};

DragInfo _dragging;

bool addCurve(const std::string &name);

void buildActions();
Expand All @@ -166,7 +174,6 @@ private slots:
PlotData::RangeValue _custom_Y_limits;

AxisLimitsDialog* _axis_limits_dialog;

};

#endif
9 changes: 0 additions & 9 deletions qwt/src/qwt_plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,6 @@ void QwtPlot::setCanvas( QWidget *canvas )
*/
bool QwtPlot::event( QEvent *event )
{
if( event->type() == QEvent::DragLeave )
{
QPoint local_pos = canvas()->mapFromGlobal(QCursor::pos()) ;
if( canvas()->rect().contains( local_pos ) )
{
event->ignore();
return false;
}
}
bool ok = QFrame::event( event );
switch ( event->type() )
{
Expand Down

0 comments on commit 7959e54

Please sign in to comment.