bool CanvasPicker::eventFilter(QObject *object, QEvent *e)
{
	if (object != (QObject *)plot()->canvas())
		return false;

	Graph *g = plot();
	QList<QwtPlotMarker *> lines = g->linesList();
	switch(e->type())
	{
		case QEvent::MouseButtonPress:
			{
                const QMouseEvent *me = (const QMouseEvent *)e;
				if (!(me->modifiers() & Qt::ShiftModifier))
                    g->deselect();

				emit selectPlot();

				bool allAxisDisabled = true;
				for (int i=0; i < QwtPlot::axisCnt; i++){
					if (g->axisEnabled(i)){
						allAxisDisabled = false;
						break;
					}
				}

                int dist, point;
                g->closestCurve(me->pos().x(), me->pos().y(), dist, point);

				if (me->button() == Qt::LeftButton && (g->drawLineActive())){
					startLinePoint = me->pos();
					return true;
				}

				if (!g->zoomOn() && selectMarker(me)){
					if (me->button() == Qt::RightButton)
						emit showMarkerPopupMenu();
					return true;
				}

				if (me->button() == Qt::LeftButton && !g->zoomOn() &&
					!g->hasPanningMagnifierEnabled() && !g->activeTool() && !g->selectedCurveLabels()){
					QDrag *drag = new QDrag(plot());
					QMimeData *mimeData = new QMimeData;
					QPoint p = plot()->canvas()->mapToParent(me->pos());
					mimeData->setText(QString::number(abs(plot()->x() - p.x())) + ";" +
									QString::number(abs(plot()->y() - p.y())));
					drag->setMimeData(mimeData);
					drag->setPixmap(plot()->multiLayer()->windowIcon().pixmap(16));
					drag->exec();
					return true;
				}

				return !(me->modifiers() & Qt::ShiftModifier);
			}
			break;

		case QEvent::MouseButtonDblClick:
			{
				if (d_editing_marker) {
					return d_editing_marker->eventFilter(g->canvas(), e);
				} else if (g->selectedMarker()) {
					if (lines.contains(g->selectedMarker())){
						emit viewLineDialog();
						return true;
					}
				} else if (g->isPiePlot()){
                	emit showPlotDialog(0);
                    return true;
				} else {
					const QMouseEvent *me = (const QMouseEvent *)e;
                    int dist, point;
                    QwtPlotItem *c = g->closestCurve(me->pos().x(), me->pos().y(), dist, point);
                    if (c && dist < 10)
                        emit showPlotDialog(g->curveIndex(c));
                    else
                        emit showPlotDialog(-1);
					return true;
				}
			}
			break;

		case QEvent::MouseMove:
			{
				const QMouseEvent *me = (const QMouseEvent *)e;
				if (me->state() != Qt::LeftButton)
  	            	return true;

				QPoint pos = me->pos();

				QwtPlotItem *c = g->selectedCurveLabels();
				if (c){
					if (c->rtti() == QwtPlotItem::Rtti_PlotSpectrogram)
						((Spectrogram *)c)->moveLabel(pos);
					else
						((DataCurve *)c)->moveLabels(pos);
					return true;
				}

				if (plot()->drawLineActive()) {
					drawLineMarker(pos, g->drawArrow());
					return true;
				}

				return false;
			}
			break;

		case QEvent::MouseButtonRelease:
			{
				const QMouseEvent *me = (const QMouseEvent *)e;
				if (g->drawLineActive()) {
					ApplicationWindow *app = g->multiLayer()->applicationWindow();
					if (!app)
						return true;

					ArrowMarker mrk;
					mrk.attach(g);
					mrk.setStartPoint(startLinePoint);
					mrk.setEndPoint(QPoint(me->x(), me->y()));
					mrk.setColor(app->defaultArrowColor);
					mrk.setWidth(app->defaultArrowLineWidth);
					mrk.setStyle(app->defaultArrowLineStyle);
					mrk.setHeadLength(app->defaultArrowHeadLength);
					mrk.setHeadAngle(app->defaultArrowHeadAngle);
					mrk.fillArrowHead(app->defaultArrowHeadFill);
					mrk.drawEndArrow(g->drawArrow());
					mrk.drawStartArrow(false);

					g->addArrow(&mrk);
					g->drawLine(false);
					mrk.detach();
					g->replot();

					return true;
				}
				return false;
			}
			break;

		case QEvent::KeyPress:
			{
				int key = ((const QKeyEvent *)e)->key();

				QwtPlotMarker *selectedMarker = g->selectedMarker();
				if (lines.contains(selectedMarker) &&
					(key == Qt::Key_Enter || key == Qt::Key_Return)){
					emit viewLineDialog();
					return true;
				}
			}
			break;

		default:
			break;
	}
	return QObject::eventFilter(object, e);
}
Example #2
0
void jsBridge::dragResource(QString hash) {

    Resource *res = Resource::fromHash(hash);
    if (res == NULL)
        return;

    QString mime = res->mimeType();
    QString fileName = res->getFileName();
    QByteArray data = res->getData();

    QPixmap pix;
    if (res->isImage()) {
        pix.loadFromData(data);
    } else if (res->isPDF()) {
        pix.load(":/img/application-pdf.png");
    } else {
        pix.load(":/img/application-octet-stream.png");
    }

    delete res;

    if (fileName.isEmpty())
        fileName = hash;

    if (mime == "application/pdf") {
        if (!fileName.endsWith(".pdf", Qt::CaseInsensitive))
            fileName += ".pdf";
    } else if (mime == "image/jpeg") {
        if (!fileName.endsWith(".jpg", Qt::CaseInsensitive) && !fileName.endsWith(".jpeg", Qt::CaseInsensitive))
            fileName += ".jpg";
    } else if (mime == "image/png") {
        if (!fileName.endsWith(".png", Qt::CaseInsensitive))
            fileName += ".png";
    } else if (mime == "image/gif") {
        if (!fileName.endsWith(".gif", Qt::CaseInsensitive))
            fileName += ".gif";
    }

    QString tmpl = QDir::tempPath() + QDir::separator() + fileName;

    QFile* f = new QFile(tmpl);

    if (!f->open(QIODevice::WriteOnly))
        return;

    f->write(data);
    f->close();
    files.enqueue(f);

    QTimer::singleShot(60000, this, SLOT(removeTmpFile()));

    QDrag *drag = new QDrag(new QWidget());
    QMimeData *mimeData = new QMimeData;

    QFileInfo fileInfo(tmpl);
    QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
    mimeData->setUrls(QList<QUrl>() << url);

    drag->setMimeData(mimeData);

    if (!pix.isNull())
        drag->setPixmap(pix.scaled(128,128, Qt::KeepAspectRatio, Qt::SmoothTransformation));

    drag->exec(Qt::CopyAction | Qt::MoveAction);
}
 //mouse has been clicked (creates a mouse event with position event->pos()
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    LEDLabel *child = static_cast<LEDLabel*>(childAt(event->pos()));
    // What type of object have you pressed?
    QString PressedClassName = child->metaObject()->className();
    qDebug() << "type: " << PressedClassName;
    QString QLabelType = "LEDLabel";

    if (!child){
        qDebug() << "Got to !Child";
        return;}

    //Exit if you didn't click a LEDLabel Type. If you press the background
    //instead of LED Label, will clear the selected LEDs
    else if (PressedClassName != QLabelType)
    {
        if (ui->actionSelect_Mode->isChecked())
        {
        clearSelectedLEDs();
        }
        return;
    }

    for (int m = 0; m < LEDs.size(); m++)
    {
        if(LEDs.at(m) == child)
        {
           setActiveLED(m);   // Which #ID LED we pressed
        }
    }
    ui->displayText->setText(QString("Selected LED %1").arg(getActiveLED()));

    QPixmap pixmap = *child->pixmap();

    //In select mode, any label which we click on will be selected or deselected
    if (ui->actionSelect_Mode->isChecked())
    {
        selectLED(child);
    }

    // In this mode, will re-order selectedLEDs in accordance to distance.
    else if (ui->actionAssign_IDs->isChecked())

    {
        //User selects 1st LED, and the algorithm
        //finds next ones until end of selected group.
        qDebug() << "Got Here";
        if (selectedLEDs.empty() == false){
            for (int i = 0; i < selectedLEDs.size(); i++)
            {
                if (child == selectedLEDs.at(i))
                {
                    getOrderedLED(child);
                }
            }
        }
    }

    //In this mode, a drag is started. Code primarily borrowed from "Draggable
    //Icons" Qt Example:
    //http://doc.qt.io/qt-5/qtwidgets-draganddrop-draggableicons-example.html
    else if (ui->actionMove_and_Add_Mode->isChecked())

    {
        QByteArray itemData;
        QDataStream dataStream(&itemData, QIODevice::WriteOnly);
        dataStream << pixmap << QPoint(event->pos() - child->pos());

        QMimeData *mimeData = new QMimeData;
        mimeData->setData("application/x-dnditemdata", itemData);

        QDrag *drag = new QDrag(this);
        drag->setMimeData(mimeData);
        drag->setPixmap(pixmap);
        drag->setHotSpot(event->pos() - child->pos());

        child->setShade(true);


        if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) !=
                Qt::MoveAction) {
            child->show();
            child->setPixmap(pixmap);
            qDebug() << "didn't work??";

        }
    }
}