/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * canvastoolposition.cc * * Sat Mar 25 11:45:34 CET 2023 * Copyright 2023 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of DrumGizmo. * * DrumGizmo is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * DrumGizmo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "canvastoolposition.h" #include "project.h" #include "canvas.h" #include "range.h" #include constexpr int diam{8}; #define mapX(x) canvas.mapX(x) #define mapY(x) canvas.mapY(x) #define unmapX(x) canvas.unmapX(x) #define unmapY(x) canvas.unmapY(x) CanvasToolPosition::CanvasToolPosition(Instrument& instrument, Canvas& canvas, Ranges& ranges, Ranges& ranges_preview) : instrument(instrument) , canvas(canvas) , ranges(ranges) , ranges_preview(ranges_preview) { colSelBg = QColor(255, 0, 0, 60); colSel = QColor(255, 0, 0, 160); colActiveSelBg = QColor(255, 255, 0, 60); colActiveSel = QColor(255, 255, 0, 160); colPreviewSelBg = QColor(0, 0, 255, 60); colPreviewSel = QColor(0, 0, 255, 160); } bool CanvasToolPosition::mouseMoveEvent(QMouseEvent* event) { sel_id_t active_range = ranges.active(); Range act_sel = ranges.get(active_range); // // int x1 = mapX(range.pos1); // int x2 = mapX(range.pos2); // highlight1 = false; // highlight2 = false; // if(x1 == event->x) // { // highlight1 = true; // } // else if(x2 == event->x) // { // highlight2 = true; // } // canvas->update(); // if(event->button() != Qt::LeftButton && event->y() > (canvas.height() - diam)) { // Check if a range is being dragged. QVector ids = ranges.ids(); QVector::iterator i = ids.begin(); while(i != ids.end()) { Range sel = ranges.get(*i); if(abs(event->x() - mapX(sel.pos1)) < diam/2 || abs(event->x() - mapX(sel.pos2)) < diam/2) { canvas.setCursor(Qt::SplitHCursor); return true; } i++; } } return false; } bool CanvasToolPosition::mousePressEvent(QMouseEvent* event) { return false; } bool CanvasToolPosition::mouseReleaseEvent(QMouseEvent* event) { return false; } namespace { void drawRange(const Range& range, Canvas& canvas, QPainter& painter) { int x1 = mapX(range.pos1); int x2 = mapX(range.pos2); if(x1 > x2) { std::swap(x1, x2); } // x1 x2 // | | // | | // |<---->| // | | // | | constexpr auto len = 8; auto arrows_y = canvas.height() - canvas.height() / 8; auto balls_y = canvas.height() - diam; if(x1 > 0 && x1 < canvas.width()) { painter.drawLine(x1, 0, x1, canvas.height()); // left bar painter.drawEllipse(x1-diam/2, balls_y, diam, diam); } if(((x1 > 0 && x1 < canvas.width()) || (x2 > 0 && x2 < canvas.width())) && std::abs(x1 - x2) > len) { painter.drawLine(x1, arrows_y, x1 + len, arrows_y + len); // left arrow painter.drawLine(x1, arrows_y, x1 + len, arrows_y - len); // left arrow painter.drawLine(x1, arrows_y, x2, arrows_y); // horizontal line painter.drawLine(x2, arrows_y, x2 - len, arrows_y + len); // right arrow painter.drawLine(x2, arrows_y, x2 - len, arrows_y - len); // right arrow } if(x2 > 0 && x2 < canvas.width()) { painter.drawLine(x2, 0, x2, canvas.height()); // right bar painter.drawEllipse(x2-diam/2, balls_y, diam, diam); } } } void CanvasToolPosition::paintEvent(QPaintEvent* event, QPainter& painter) { int pos = unmapX(event->rect().x()); int width = unmapX(event->rect().width()); { QVector ids = ranges.ids(); QVector::iterator i = ids.begin(); while(i != ids.end()) { Range range = ranges.get(*i); if(ranges.active() == *i) { painter.setBrush(colActiveSelBg); painter.setPen(colActiveSel); } else { painter.setBrush(colSelBg); painter.setPen(colSel); } drawRange(range, canvas, painter); i++; } } if(show_preview) { QVector ids = ranges_preview.ids(); QVector::iterator i = ids.begin(); while(i != ids.end()) { Range range = ranges.get(*i); painter.setBrush(colPreviewSelBg); painter.setPen(colPreviewSel); drawRange(range, canvas, painter); i++; } } } void CanvasToolPosition::keyReleaseEvent(QKeyEvent* event) { } void CanvasToolPosition::rangesChanged() { //auto ranges = instrument.getRanges(); canvas.update(); } void CanvasToolPosition::setShowPreview(bool show_preview) { this->show_preview = show_preview; canvas.update(); }