summaryrefslogtreecommitdiff
path: root/src/canvastoolselections.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/canvastoolselections.cc')
-rw-r--r--src/canvastoolselections.cc487
1 files changed, 260 insertions, 227 deletions
diff --git a/src/canvastoolselections.cc b/src/canvastoolselections.cc
index ad5e193..ff1056e 100644
--- a/src/canvastoolselections.cc
+++ b/src/canvastoolselections.cc
@@ -34,286 +34,319 @@
#define unmapX(x) canvas->unmapX(x)
#define unmapY(x) canvas->unmapY(x)
-CanvasToolSelections::CanvasToolSelections(Canvas *c, Selections &s,
- Selections &p)
- : selections(s), selections_preview(p)
+CanvasToolSelections::CanvasToolSelections(Canvas* c, Selections& s,
+ Selections& p)
+ : selections(s)
+ , selections_preview(p)
{
- threshold = 0.5; // Default from CanvasToolThreshold
- hold = 100;
+ threshold = 0.5; // Default from CanvasToolThreshold
+ hold = 100;
- canvas = c;
+ canvas = c;
- selection_is_moving_left = false;
- selection_is_moving_right = false;
+ selection_is_moving_left = false;
+ selection_is_moving_right = false;
- 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);
+ 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 CanvasToolSelections::mouseMoveEvent(QMouseEvent *event)
+bool CanvasToolSelections::mouseMoveEvent(QMouseEvent* event)
{
- sel_id_t active_selection = selections.active();
- Selection act_sel = selections.get(active_selection);
-
- if(selection_is_moving_left) {
- float val = unmapX(event->x());
- if(val > act_sel.to) val = act_sel.to - 1;
- act_sel.from = val;
- selections.update(active_selection, act_sel);
- canvas->update();
- return true;
- }
-
- if(selection_is_moving_right) {
- float val = unmapX(event->x());
- if(val < act_sel.from) val = act_sel.from + 1;
- act_sel.to = val;
- selections.update(active_selection, act_sel);
- canvas->update();
- return true;
- }
-
- if(event->button() != Qt::LeftButton) {
- // Check if a selection is being dragged.
- QVector<sel_id_t> ids = selections.ids();
- QVector<sel_id_t>::iterator i = ids.begin();
- while(i != ids.end()) {
- Selection sel = selections.get(*i);
- if(abs(event->x() - mapX(sel.from)) < 2
- || abs(event->x() - mapX(sel.to)) < 2) {
- canvas->setCursor(Qt::SplitHCursor);
- return true;
- }
- i++;
- }
- }
-
- return false;
+ sel_id_t active_selection = selections.active();
+ Selection act_sel = selections.get(active_selection);
+
+ if(selection_is_moving_left)
+ {
+ float val = unmapX(event->x());
+ if(val > act_sel.to) val = act_sel.to - 1;
+ act_sel.from = val;
+ selections.update(active_selection, act_sel);
+ canvas->update();
+ return true;
+ }
+
+ if(selection_is_moving_right)
+ {
+ float val = unmapX(event->x());
+ if(val < act_sel.from) val = act_sel.from + 1;
+ act_sel.to = val;
+ selections.update(active_selection, act_sel);
+ canvas->update();
+ return true;
+ }
+
+ if(event->button() != Qt::LeftButton)
+ {
+ // Check if a selection is being dragged.
+ QVector<sel_id_t> ids = selections.ids();
+ QVector<sel_id_t>::iterator i = ids.begin();
+ while(i != ids.end())
+ {
+ Selection sel = selections.get(*i);
+ if(abs(event->x() - mapX(sel.from)) < 2
+ || abs(event->x() - mapX(sel.to)) < 2)
+ {
+ canvas->setCursor(Qt::SplitHCursor);
+ return true;
+ }
+ i++;
+ }
+ }
+
+ return false;
}
-bool CanvasToolSelections::mousePressEvent(QMouseEvent *event)
+bool CanvasToolSelections::mousePressEvent(QMouseEvent* event)
{
- if(event->button() == Qt::LeftButton) {
- // Check if a selection is being dragged.
- QVector<sel_id_t> ids = selections.ids();
- QVector<sel_id_t>::iterator i = ids.begin();
- while(i != ids.end()) {
- Selection sel = selections.get(*i);
- if(abs(event->x() - mapX(sel.from)) < 2) {
- selections.setActive(*i);
- selection_is_moving_left = true;
- return true;
- }
-
- if(abs(event->x() - mapX(sel.to)) < 2) {
- selections.setActive(*i);
- selection_is_moving_right = true;
- return true;
- }
-
- i++;
- }
-
- // Check if a selection is being selected.
- i = ids.begin();
- while(i != ids.end()) {
- Selection sel = selections.get(*i);
- if(event->x() > mapX(sel.from) &&
- event->x() < mapX(sel.to)) {
- selections.setActive(*i);
- canvas->update();
- return true;
- }
-
- i++;
- }
-
- // Make new selection
- int from = unmapX(event->x());
- Selection new_selection(from, from);
- sel_id_t id = selections.add(new_selection);
- selections.setActive(id);
- selection_is_moving_right = true;
- canvas->update();
- return true;
- }
-
- return false;
+ if(event->button() == Qt::LeftButton)
+ {
+ // Check if a selection is being dragged.
+ QVector<sel_id_t> ids = selections.ids();
+ QVector<sel_id_t>::iterator i = ids.begin();
+ while(i != ids.end())
+ {
+ Selection sel = selections.get(*i);
+ if(abs(event->x() - mapX(sel.from)) < 2)
+ {
+ selections.setActive(*i);
+ selection_is_moving_left = true;
+ return true;
+ }
+
+ if(abs(event->x() - mapX(sel.to)) < 2)
+ {
+ selections.setActive(*i);
+ selection_is_moving_right = true;
+ return true;
+ }
+
+ i++;
+ }
+
+ // Check if a selection is being selected.
+ i = ids.begin();
+ while(i != ids.end())
+ {
+ Selection sel = selections.get(*i);
+ if(event->x() > mapX(sel.from) &&
+ event->x() < mapX(sel.to))
+ {
+ selections.setActive(*i);
+ canvas->update();
+ return true;
+ }
+
+ i++;
+ }
+
+ // Make new selection
+ int from = unmapX(event->x());
+ Selection new_selection(from, from);
+ sel_id_t id = selections.add(new_selection);
+ selections.setActive(id);
+ selection_is_moving_right = true;
+ canvas->update();
+ return true;
+ }
+
+ return false;
}
-bool CanvasToolSelections::mouseReleaseEvent(QMouseEvent *event)
+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;
+ 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)
+void CanvasToolSelections::paintEvent(QPaintEvent* event, QPainter& painter)
{
- int pos = unmapX(event->rect().x());
- int width = unmapX(event->rect().width());
-
- {
- QVector<sel_id_t> ids = selections.ids();
- QVector<sel_id_t>::iterator i = ids.begin();
- while(i != ids.end()) {
- Selection sel = selections.get(*i);
- int from = sel.from;
- int to = sel.to;
- int fadein = sel.fadein;
- int fadeout = sel.fadeout;
- if(from > pos + width || to + width < pos) { i++; continue; }
- if(selections.active() == *i) {
- 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++;
- }
- }
-
- if(show_preview) {
- QVector<sel_id_t> ids = selections_preview.ids();
- QVector<sel_id_t>::iterator i = ids.begin();
- while(i != ids.end()) {
- Selection sel = selections_preview.get(*i);
- int from = sel.from;
- int to = sel.to;
- int fadein = sel.fadein;
- int fadeout = sel.fadeout;
- if(from > pos + width || to + width < pos) { i++; continue; }
- painter.setBrush(colPreviewSelBg);
- painter.setPen(colPreviewSel);
-
- 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++;
- }
- }
+ int pos = unmapX(event->rect().x());
+ int width = unmapX(event->rect().width());
+
+ {
+ QVector<sel_id_t> ids = selections.ids();
+ QVector<sel_id_t>::iterator i = ids.begin();
+ while(i != ids.end())
+ {
+ Selection sel = selections.get(*i);
+ int from = sel.from;
+ int to = sel.to;
+ int fadein = sel.fadein;
+ int fadeout = sel.fadeout;
+ if(from > pos + width || to + width < pos)
+ { i++; continue; }
+ if(selections.active() ==* i)
+ {
+ 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++;
+ }
+ }
+
+ if(show_preview)
+ {
+ QVector<sel_id_t> ids = selections_preview.ids();
+ QVector<sel_id_t>::iterator i = ids.begin();
+ while(i != ids.end())
+ {
+ Selection sel = selections_preview.get(*i);
+ int from = sel.from;
+ int to = sel.to;
+ int fadein = sel.fadein;
+ int fadeout = sel.fadeout;
+ if(from > pos + width || to + width < pos)
+ { i++; continue; }
+ painter.setBrush(colPreviewSelBg);
+ painter.setPen(colPreviewSel);
+
+ 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)
+void CanvasToolSelections::keyReleaseEvent(QKeyEvent* event)
{
- if(selections.active() != SEL_NONE && event->key() == Qt::Key_Delete) {
- selections.remove(selections.active());
- canvas->update();
- }
+ if(selections.active() != SEL_NONE && event->key() == Qt::Key_Delete)
+ {
+ selections.remove(selections.active());
+ canvas->update();
+ }
}
void CanvasToolSelections::thresholdChanged(double t)
{
- threshold = t;
+ threshold = t;
}
void CanvasToolSelections::holdChanged(int h)
{
- hold = h;
+ hold = h;
}
void CanvasToolSelections::noiseFloorChanged(int t)
{
- double div = 666.0 / 0.00003;
- noise_floor = (double)t/div;
+ double div = 666.0 / 0.00003;
+ noise_floor = (double)t/div;
}
void CanvasToolSelections::fadeoutChanged(int t)
{
- fadeout = (double)t/1000.0;
+ fadeout = (double)t/1000.0;
}
void CanvasToolSelections::autoCreateSelections()
{
- doAutoCreateSelections(false);
+ doAutoCreateSelections(false);
}
void CanvasToolSelections::autoCreateSelectionsPreview()
{
- doAutoCreateSelections(true);
+ doAutoCreateSelections(true);
}
void CanvasToolSelections::doAutoCreateSelections(bool preview)
{
- float *data = canvas->data;
- size_t size = canvas->size;
-
- if(preview) selections_preview.clear();
-
- 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 to = i;
- float runavg = fabs(data[from]);
- while((runavg > noise_floor ||
- to < from + hold) &&
- to < (int)size) {
- double p = 0.9;
- runavg = runavg * p + fabs(data[to]) * (1 - p);
- to++;
- }
-
- Selection s(from, to, 2, ((to - from) / 3) * fadeout);
- if(preview) {
- selections_preview.add(s);
- } else {
- selections.add(s);
- }
-
- i = to+1;
- }
- }
- canvas->update();
+ float* data = canvas->data;
+ size_t size = canvas->size;
+
+ if(preview) selections_preview.clear();
+
+ 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 to = i;
+ float runavg = fabs(data[from]);
+ while((runavg > noise_floor || to < from + hold) &&
+ to < (int)size)
+ {
+ double p = 0.9;
+ runavg = runavg * p + fabs(data[to]) * (1 - p);
+ to++;
+ }
+
+ Selection s(from, to, 2, ((to - from) / 3) * fadeout);
+ if(preview)
+ {
+ selections_preview.add(s);
+ }
+ else
+ {
+ selections.add(s);
+ }
+
+ i = to+1;
+ }
+ }
+ canvas->update();
}
void CanvasToolSelections::clearSelections()
{
- selections.clear();
- selection_is_moving_left = false;
- selection_is_moving_right = false;
- canvas->setCursor(Qt::ArrowCursor);
- canvas->update();
+ selections.clear();
+ selection_is_moving_left = false;
+ selection_is_moving_right = false;
+ canvas->setCursor(Qt::ArrowCursor);
+ canvas->update();
}
void CanvasToolSelections::setShowPreview(bool s)
{
- show_preview = s;
- canvas->update();
+ show_preview = s;
+ canvas->update();
}