From 016f7f2a57dbb776846a511e06e03b2e2572ab7c Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 28 Jul 2011 23:59:21 +0200 Subject: First attemt on the selections tool in the new CanvasTool structure. --- dgedit/Makefile.am | 4 + dgedit/canvas.cc | 187 +++++----------------------------- dgedit/canvas.h | 24 +---- dgedit/canvastool.h | 43 ++++---- dgedit/canvastoolselections.cc | 222 ++++++++++++++++++++++++++++++++++++++--- dgedit/canvastoolselections.h | 47 +++++++++ 6 files changed, 306 insertions(+), 221 deletions(-) diff --git a/dgedit/Makefile.am b/dgedit/Makefile.am index 1aa940a..a29a9b8 100644 --- a/dgedit/Makefile.am +++ b/dgedit/Makefile.am @@ -9,6 +9,8 @@ dgedit_SOURCES = $(shell ../tools/MocList cc ) \ dgedit.cc \ audioextractor.cc \ canvas.cc \ + canvastool.cc \ + canvastoolselections.cc \ filelist.cc \ itemeditor.cc \ mainwindow.cc \ @@ -20,6 +22,8 @@ dgedit_SOURCES = $(shell ../tools/MocList cc ) \ EXTRA_DIST = \ audioextractor.h \ canvas.h \ + canvastool.h \ + canvastoolselections.h \ filelist.h \ itemeditor.h \ mainwindow.h \ diff --git a/dgedit/canvas.cc b/dgedit/canvas.cc index 44536b3..70ac219 100644 --- a/dgedit/canvas.cc +++ b/dgedit/canvas.cc @@ -34,6 +34,8 @@ #include +#include "canvastoolselections.h" + #define DEFYSCALE 200 Canvas::Canvas(QWidget *parent) @@ -55,9 +57,6 @@ Canvas::Canvas(QWidget *parent) threshold = 0.5; threshold_is_moving = false; - selection_is_moving_left = false; - selection_is_moving_right = false; - active_selection = NULL; colBg = QColor(180, 200, 180); colSec = QColor(160, 180, 160); @@ -67,14 +66,12 @@ Canvas::Canvas(QWidget *parent) colHalf = QColor(180, 180, 255); colThreshold = QColor(255, 127, 127); colThresholdMoving = QColor(180, 0, 0); - colSelBg = QColor(255, 0, 0, 60); - colSel = QColor(255, 0, 0, 160); - colActiveSelBg = QColor(255, 255, 0, 60); - colActiveSel = QColor(255, 255, 0, 160); setCursor(Qt::ArrowCursor); wav = QImage(width(), height(), QImage::Format_RGB32); + + tools.push_back(new CanvasToolSelections(this)); } Canvas::~Canvas() @@ -157,41 +154,18 @@ void Canvas::mouseMoveEvent(QMouseEvent *event) return; } - if(selection_is_moving_left) { - float val = unmapX(event->x()); - if(val > active_selection->to) val = active_selection->to - 1; - active_selection->from = val; - update(); - emit selectionsChanged(_selections); - return; - } - - if(selection_is_moving_right) { - float val = unmapX(event->x()); - if(val < active_selection->from) val = active_selection->from + 1; - active_selection->to = val; - update(); - emit selectionsChanged(_selections); - return; - } - if(event->button() != Qt::LeftButton) { if(abs(event->y() - mapY(threshold)) < 2 || abs(event->y() - mapY(-threshold)) < 2 ) { setCursor(Qt::SplitVCursor); + return; } else { setCursor(Qt::ArrowCursor); } + } - // Check if a selection is being dragged. - QMap::iterator i = _selections.begin(); - while(i != _selections.end()) { - if(abs(event->x() - mapX(i.value().from)) < 2 - || abs(event->x() - mapX(i.value().to)) < 2) { - setCursor(Qt::SplitHCursor); - } - i++; - } + for(int i = 0; i < tools.size(); i++) { + if(tools[i]->mouseMoveEvent(event)) return; } } @@ -206,48 +180,10 @@ void Canvas::mousePressEvent(QMouseEvent *event) update(); return; } + } - // Check if a selection is being dragged. - QMap::iterator i = _selections.begin(); - while(i != _selections.end()) { - if(abs(event->x() - mapX(i.value().from)) < 2) { - active_selection = &i.value(); - selection_is_moving_left = true; - emit activeSelectionChanged(i.value()); - return; - } - - if(abs(event->x() - mapX(i.value().to)) < 2) { - active_selection = &i.value(); - selection_is_moving_right = true; - emit activeSelectionChanged(i.value()); - return; - } - - i++; - } - - // Check if a selection is being selected. - i = _selections.begin(); - while(i != _selections.end()) { - if(event->x() > mapX(i.value().from) && - event->x() < mapX(i.value().to)) { - active_selection = &i.value(); - update(); - emit activeSelectionChanged(i.value()); - return; - } - - i++; - } - - // Make new selection - int from = unmapX(event->x()); - _selections[from] = Selection(from, from); - active_selection = &_selections[from]; - selection_is_moving_right = true; - update(); - return; + for(int i = 0; i < tools.size(); i++) { + if(tools[i]->mousePressEvent(event)) return; } } @@ -260,18 +196,19 @@ void Canvas::mouseReleaseEvent(QMouseEvent *event) update(); return; } - if(selection_is_moving_left || selection_is_moving_right) { - selection_is_moving_left = false; - selection_is_moving_right = false; - setCursor(Qt::ArrowCursor); - update(); - return; - } + } + + for(int i = 0; i < tools.size(); i++) { + if(tools[i]->mouseReleaseEvent(event)) return; } } -void Canvas::resizeEvent(QResizeEvent *) +void Canvas::resizeEvent(QResizeEvent *event) { + for(int i = 0; i < tools.size(); i++) { + tools[i]->resizeEvent(event); + } + wav = QImage(width(), height(), QImage::Format_RGB32); updateWav(); update(); @@ -352,36 +289,15 @@ void Canvas::paintEvent(QPaintEvent *event) painter.drawLine(event->rect().x(), mapY(-threshold), event->rect().x() + event->rect().width(), mapY(-threshold)); - int pos = unmapX(event->rect().x()); - int width = unmapX(event->rect().width()); - QMap::iterator i = _selections.begin(); - while(i != _selections.end()) { - int from = i.value().from; - int to = i.value().to; - int fadein = i.value().fadein; - int fadeout = i.value().fadeout; - if(from > pos + width || to + width < pos) { i++; continue; } - if(active_selection == &i.value()) { - painter.setBrush(colActiveSelBg); - painter.setPen(colActiveSel); - } else { - painter.setBrush(colSelBg); - painter.setPen(colSel); - } - painter.drawRect(mapX(from), mapY(-1.0), mapX(to) - mapX(from), mapY(1.0) - mapY(-1.0)); - painter.drawLine(mapX(from), mapY(0.0), mapX(from + fadein), mapY(-1.0)); - painter.drawLine(mapX(from), mapY(0.0), mapX(from + fadein), mapY(1.0)); - painter.drawLine(mapX(to - fadeout), mapY(-1.0), mapX(to), mapY(0.0)); - painter.drawLine(mapX(to - fadeout), mapY(1.0), mapX(to), mapY(0.0)); - i++; + for(int i = 0; i < tools.size(); i++) { + tools[i]->paintEvent(event, painter); } } void Canvas::keyReleaseEvent(QKeyEvent *event) { - if(active_selection && event->key() == Qt::Key_Delete) { - _selections.remove(active_selection->from); - update(); + for(int i = 0; i < tools.size(); i++) { + tools[i]->keyReleaseEvent(event); } } @@ -422,58 +338,3 @@ void Canvas::setYOffset(float offset) updateWav(); update(); } - -void Canvas::autoCreateSelections() -{ - for(size_t i = 0; i < size; i++) { - if(fabs(data[i]) > fabs(threshold)) { - int from = i; - - if(data[from] > 0.0) { - while(data[from] > data[from-1] // Falling - && data[from-1] > 0.0 // Not crossing zero - ) { - from--; - } - } else if(data[from] < 0.0) { - while(data[from] < data[from-1] // Rising - && data[from-1] < 0.0 // Not crossing zero - ) { - from--; - } - } - - int minsize = 100; // attack. - float minval = 0.00003; // noise floor - int to = i; - float runavg = fabs(data[from]); - while((runavg > minval || - to < from + minsize) && - to < (int)size) { - double p = 0.9; - runavg = runavg * p + fabs(data[to]) * (1 - p); - to++; - } - _selections[from] = Selection(from, to, 2, (to - from) / 3); - - i = to+1; - } - } - update(); - emit selectionsChanged(_selections); -} - -void Canvas::clearSelections() -{ - _selections.clear(); - selection_is_moving_left = false; - selection_is_moving_right = false; - setCursor(Qt::ArrowCursor); - update(); - emit selectionsChanged(_selections); -} - -Selections Canvas::selections() -{ - return _selections; -} diff --git a/dgedit/canvas.h b/dgedit/canvas.h index 0fb7f4c..b0ff552 100644 --- a/dgedit/canvas.h +++ b/dgedit/canvas.h @@ -31,7 +31,6 @@ #include #include -#include "selection.h" #include "mipmap.h" #include "canvastool.h" @@ -42,22 +41,19 @@ public: ~Canvas(); void load(QString file); - - Selections selections(); void addTool(CanvasTool *tool); -signals: - void selectionsChanged(Selections selections); - void activeSelectionChanged(Selection selection); + float mapX(float x); + float unmapX(float x); + float mapY(float y); + float unmapY(float y); public slots: void setXScale(float scale); void setYScale(float scale); void setXOffset(float scroll); void setYOffset(float scroll); - void autoCreateSelections(); - void clearSelections(); protected: void mouseMoveEvent(QMouseEvent *event); @@ -73,10 +69,6 @@ private: void updateWav(); void getWavValues(int last, int lx, float *vu, float *vl, float *avgu, float *avgl); - float mapX(float x); - float unmapX(float x); - float mapY(float y); - float unmapY(float y); QImage wav; @@ -91,9 +83,6 @@ private: float threshold; bool threshold_is_moving; - bool selection_is_moving_left; - bool selection_is_moving_right; - Selection *active_selection; QColor colBg; QColor colSec; @@ -103,12 +92,7 @@ private: QColor colWavAvg; QColor colThreshold; QColor colThresholdMoving; - QColor colSelBg; - QColor colSel; - QColor colActiveSelBg; - QColor colActiveSel; - Selections _selections; QVector tools; diff --git a/dgedit/canvastool.h b/dgedit/canvastool.h index bd224d0..0e802ba 100644 --- a/dgedit/canvastool.h +++ b/dgedit/canvastool.h @@ -27,40 +27,31 @@ #ifndef __DRUMGIZMO_CANVASTOOL_H__ #define __DRUMGIZMO_CANVASTOOL_H__ +#include +#include +#include +#include +#include + class CanvasTool : public QObject { Q_OBJECT public: - virtual void mouseMoveEvent(QMouseEvent *event) = 0; - virtual void mousePressEvent(QMouseEvent *event) = 0; - virtual void mouseReleaseEvent(QMouseEvent *event) = 0; - virtual void resizeEvent(QResizeEvent *event) = 0; - virtual void paintEvent(QPaintEvent *event) = 0; - virtual void keyReleaseEvent(QKeyEvent *event) = 0; + virtual bool mouseMoveEvent(QMouseEvent *event); + virtual bool mousePressEvent(QMouseEvent *event); + virtual bool mouseReleaseEvent(QMouseEvent *event); + virtual void resizeEvent(QResizeEvent *event); + virtual void paintEvent(QPaintEvent *event, QPainter &painter); + virtual void keyReleaseEvent(QKeyEvent *event); + + bool isActive(); signals: void activateChanged(bool activestate); public slots: - void setActive(bool active) - { - _active = active; - emit activateChanged(active); - } - - void activate() - { - setActive(true); - } - - void disactivate() - { - setActive(false); - } - - bool isActive() - { - return _active; - } + void setActive(bool active); + void activate(); + void disactivate(); private: bool _active; diff --git a/dgedit/canvastoolselections.cc b/dgedit/canvastoolselections.cc index 13798a8..7492b9b 100644 --- a/dgedit/canvastoolselections.cc +++ b/dgedit/canvastoolselections.cc @@ -26,19 +26,217 @@ */ #include "canvastoolselections.h" -#ifdef TEST_CANVASTOOLSELECTIONS -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" +#include -TEST_BEGIN; +#define mapX(x) canvas->mapX(x) +#define mapY(x) canvas->mapY(x) +#define unmapX(x) canvas->unmapX(x) +#define unmapY(x) canvas->unmapY(x) -// TODO: Put some testcode here (see test.h for usable macros). +CanvasToolSelections::CanvasToolSelections(Canvas *c) +{ + canvas = c; + data = canvas->data; + size = canvas->size; -TEST_END; + selection_is_moving_left = false; + selection_is_moving_right = false; + active_selection = NULL; -#endif/*TEST_CANVASTOOLSELECTIONS*/ + colSelBg = QColor(255, 0, 0, 60); + colSel = QColor(255, 0, 0, 160); + colActiveSelBg = QColor(255, 255, 0, 60); + colActiveSel = QColor(255, 255, 0, 160); + +} + +bool CanvasToolSelections::mouseMoveEvent(QMouseEvent *event) +{ + if(selection_is_moving_left) { + float val = unmapX(event->x()); + if(val > active_selection->to) val = active_selection->to - 1; + active_selection->from = val; + canvas->update(); + emit selectionsChanged(_selections); + return true; + } + + if(selection_is_moving_right) { + float val = unmapX(event->x()); + if(val < active_selection->from) val = active_selection->from + 1; + active_selection->to = val; + canvas->update(); + emit selectionsChanged(_selections); + return true; + } + + if(event->button() != Qt::LeftButton) { + // Check if a selection is being dragged. + QMap::iterator i = _selections.begin(); + while(i != _selections.end()) { + if(abs(event->x() - mapX(i.value().from)) < 2 + || abs(event->x() - mapX(i.value().to)) < 2) { + canvas->setCursor(Qt::SplitHCursor); + } + i++; + } + } + + return false; +} + +bool CanvasToolSelections::mousePressEvent(QMouseEvent *event) +{ + if(event->button() == Qt::LeftButton) { + // Check if a selection is being dragged. + QMap::iterator i = _selections.begin(); + while(i != _selections.end()) { + if(abs(event->x() - mapX(i.value().from)) < 2) { + active_selection = &i.value(); + selection_is_moving_left = true; + emit activeSelectionChanged(i.value()); + return true; + } + + if(abs(event->x() - mapX(i.value().to)) < 2) { + active_selection = &i.value(); + selection_is_moving_right = true; + emit activeSelectionChanged(i.value()); + return true; + } + + i++; + } + + // Check if a selection is being selected. + i = _selections.begin(); + while(i != _selections.end()) { + if(event->x() > mapX(i.value().from) && + event->x() < mapX(i.value().to)) { + active_selection = &i.value(); + canvas->update(); + emit activeSelectionChanged(i.value()); + return true; + } + + i++; + } + + // Make new selection + int from = unmapX(event->x()); + _selections[from] = Selection(from, from); + active_selection = &_selections[from]; + selection_is_moving_right = true; + canvas->update(); + return true; + } + + return false; +} + +bool CanvasToolSelections::mouseReleaseEvent(QMouseEvent *event) +{ + if(event->button() == Qt::LeftButton) { + if(selection_is_moving_left || selection_is_moving_right) { + selection_is_moving_left = false; + selection_is_moving_right = false; + canvas->setCursor(Qt::ArrowCursor); + canvas->update(); + return true; + } + } + + return false; +} + +void CanvasToolSelections::paintEvent(QPaintEvent *event, QPainter &painter) +{ + int pos = unmapX(event->rect().x()); + int width = unmapX(event->rect().width()); + QMap::iterator i = _selections.begin(); + while(i != _selections.end()) { + int from = i.value().from; + int to = i.value().to; + int fadein = i.value().fadein; + int fadeout = i.value().fadeout; + if(from > pos + width || to + width < pos) { i++; continue; } + if(active_selection == &i.value()) { + painter.setBrush(colActiveSelBg); + painter.setPen(colActiveSel); + } else { + painter.setBrush(colSelBg); + painter.setPen(colSel); + } + painter.drawRect(mapX(from), mapY(-1.0), mapX(to) - mapX(from), mapY(1.0) - mapY(-1.0)); + painter.drawLine(mapX(from), mapY(0.0), mapX(from + fadein), mapY(-1.0)); + painter.drawLine(mapX(from), mapY(0.0), mapX(from + fadein), mapY(1.0)); + painter.drawLine(mapX(to - fadeout), mapY(-1.0), mapX(to), mapY(0.0)); + painter.drawLine(mapX(to - fadeout), mapY(1.0), mapX(to), mapY(0.0)); + i++; + } +} + +void CanvasToolSelections::keyReleaseEvent(QKeyEvent *event) +{ + if(active_selection && event->key() == Qt::Key_Delete) { + _selections.remove(active_selection->from); + canvas->update(); + } +} + +void CanvasToolSelections::autoCreateSelections() +{ + /* + for(size_t i = 0; i < size; i++) { + if(fabs(data[i]) > fabs(threshold)) { + int from = i; + + if(data[from] > 0.0) { + while(data[from] > data[from-1] // Falling + && data[from-1] > 0.0 // Not crossing zero + ) { + from--; + } + } else if(data[from] < 0.0) { + while(data[from] < data[from-1] // Rising + && data[from-1] < 0.0 // Not crossing zero + ) { + from--; + } + } + + int minsize = 100; // attack. + float minval = 0.00003; // noise floor + int to = i; + float runavg = fabs(data[from]); + while((runavg > minval || + to < from + minsize) && + to < (int)size) { + double p = 0.9; + runavg = runavg * p + fabs(data[to]) * (1 - p); + to++; + } + _selections[from] = Selection(from, to, 2, (to - from) / 3); + + i = to+1; + } + } + canvas->update(); + emit selectionsChanged(_selections); + */ +} + +void CanvasToolSelections::clearSelections() +{ + _selections.clear(); + selection_is_moving_left = false; + selection_is_moving_right = false; + canvas->setCursor(Qt::ArrowCursor); + canvas->update(); + emit selectionsChanged(_selections); +} + +Selections CanvasToolSelections::selections() +{ + return _selections; +} diff --git a/dgedit/canvastoolselections.h b/dgedit/canvastoolselections.h index 730f99d..a8b37a3 100644 --- a/dgedit/canvastoolselections.h +++ b/dgedit/canvastoolselections.h @@ -26,4 +26,51 @@ */ #ifndef __DRUMGIZMO_CANVASTOOLSELECTIONS_H__ #define __DRUMGIZMO_CANVASTOOLSELECTIONS_H__ + +#include "canvastool.h" + +#include + +#include "canvas.h" + +#include "selection.h" + +class CanvasToolSelections : public CanvasTool { +Q_OBJECT +public: + CanvasToolSelections(Canvas *canvas); + + bool mouseMoveEvent(QMouseEvent *event); + bool mousePressEvent(QMouseEvent *event); + bool mouseReleaseEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event, QPainter &painter); + void keyReleaseEvent(QKeyEvent *event); + + Selections selections(); + +signals: + void selectionsChanged(Selections selections); + void activeSelectionChanged(Selection selection); + +public slots: + void autoCreateSelections(); + void clearSelections(); + +private: + bool selection_is_moving_left; + bool selection_is_moving_right; + Selection *active_selection; + Selections _selections; + + Canvas *canvas; + + float *data; + size_t size; + + QColor colSelBg; + QColor colSel; + QColor colActiveSelBg; + QColor colActiveSel; +}; + #endif/*__DRUMGIZMO_CANVASTOOLSELECTIONS_H__*/ -- cgit v1.2.3