diff options
Diffstat (limited to 'src/canvastoolposition.cc')
-rw-r--r-- | src/canvastoolposition.cc | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/canvastoolposition.cc b/src/canvastoolposition.cc new file mode 100644 index 0000000..7784720 --- /dev/null +++ b/src/canvastoolposition.cc @@ -0,0 +1,161 @@ +/* -*- 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 <iostream> + +#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) +{ + 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; + painter.drawLine(x1, mapY(-1.0), x1, mapY(1.0)); // left bar + + painter.drawLine(x1, mapY( 0.0), x1 + len, mapY(0.0) + len); // left arrow + painter.drawLine(x1, mapY( 0.0), x1 + len, mapY(0.0) - len); // left arrow + + painter.drawLine(x1, mapY( 0.0), x2, mapY(0.0)); // horizontal line + + painter.drawLine(x2, mapY( 0.0), x2 - len, mapY(0.0) + len); // right arrow + painter.drawLine(x2, mapY( 0.0), x2 - len, mapY(0.0) - len); // right arrow + + painter.drawLine(x2, mapY(-1.0), x2, mapY(1.0)); // right bar +} +} + +void CanvasToolPosition::paintEvent(QPaintEvent* event, QPainter& painter) +{ + int pos = unmapX(event->rect().x()); + int width = unmapX(event->rect().width()); + + { + QVector<sel_id_t> ids = ranges.ids(); + QVector<sel_id_t>::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<sel_id_t> ids = ranges_preview.ids(); + QVector<sel_id_t>::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(); +} |