| 1 |
/* RealTierArea.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2022,2026 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "RealTierArea.h" |
| 20 |
#include "EditorM.h" |
| 21 |
|
| 22 |
Thing_implement (RealTierArea, FunctionArea, 0); |
| 23 |
|
| 24 |
#include "Prefs_define.h" |
| 25 |
#include "RealTierArea_prefs.h" |
| 26 |
#include "Prefs_install.h" |
| 27 |
#include "RealTierArea_prefs.h" |
| 28 |
#include "Prefs_copyToInstance.h" |
| 29 |
#include "RealTierArea_prefs.h" |
| 30 |
|
| 31 |
|
| 32 |
#pragma mark - RealTierArea helpers |
| 33 |
|
| 34 |
void RealTierArea_addPointAt (RealTierArea me, double time, double desiredY) { |
| 35 |
if (isdefined (my v_minimumLegalY()) && desiredY < my v_minimumLegalY()) |
| 36 |
Melder_throw (U"Cannot add a point below ", my v_minimumLegalY(), my v_rightTickUnits(), U"."); |
| 37 |
if (isdefined (my v_maximumLegalY()) && desiredY > my v_maximumLegalY()) |
| 38 |
Melder_throw (U"Cannot add a point above ", my v_maximumLegalY(), my v_rightTickUnits(), U"."); |
| 39 |
RealTier_addPoint (my realTier(), time, desiredY); |
| 40 |
} |
| 41 |
|
| 42 |
void RealTierArea_removePoints (RealTierArea me) { |
| 43 |
if (my startSelection() == my endSelection()) |
| 44 |
AnyTier_removePointNear (my realTier()->asAnyTier(), my startSelection()); |
| 45 |
else |
| 46 |
AnyTier_removePointsBetween (my realTier()->asAnyTier(), my startSelection(), my endSelection()); |
| 47 |
} |
| 48 |
|
| 49 |
void RealTierArea_addPointAtCursor (RealTierArea me) { |
| 50 |
const double cursorTime = 0.5 * (my startSelection() + my endSelection()); |
| 51 |
RealTierArea_addPointAt (me, cursorTime, my ycursor); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
#pragma mark - RealTierArea drawing |
| 56 |
|
| 57 |
static void RealTierArea_drawWhileDragging (RealTierArea me) { |
| 58 |
Graphics_xorOn (my graphics(), Melder_MAROON); |
| 59 |
/* |
| 60 |
Draw all selected points as empty circles, if inside the window. |
| 61 |
*/ |
| 62 |
for (integer ipoint = my firstSelected; ipoint <= my lastSelected; ipoint ++) { |
| 63 |
const RealPoint point = my realTier() -> points.at [ipoint]; |
| 64 |
const double t = point -> number + my dt, y = point -> value + my dy; |
| 65 |
if (t >= my startWindow() && t <= my endWindow()) |
| 66 |
Graphics_circle_mm (my graphics(), t, y, 3); |
| 67 |
} |
| 68 |
|
| 69 |
if (my lastSelected == my firstSelected) { |
| 70 |
/* |
| 71 |
Draw a crosshair with time and y. |
| 72 |
*/ |
| 73 |
const RealPoint point = my realTier() -> points.at [my firstSelected]; |
| 74 |
const double t = point -> number + my dt, y = point -> value + my dy; |
| 75 |
Graphics_line (my graphics(), t, my ymin, t, my ymax - Graphics_dyMMtoWC (my graphics(), 4.0)); |
| 76 |
Graphics_setTextAlignment (my graphics(), kGraphics_horizontalAlignment::CENTRE, Graphics_TOP); |
| 77 |
Graphics_text (my graphics(), t, my ymax, Melder_fixed (t, 6)); |
| 78 |
Graphics_line (my graphics(), my startWindow(), y, my endWindow(), y); |
| 79 |
Graphics_setTextAlignment (my graphics(), Graphics_LEFT, Graphics_BOTTOM); |
| 80 |
Graphics_text (my graphics(), my startWindow(), y, Melder_fixed (y, 6)); |
| 81 |
} |
| 82 |
Graphics_xorOff (my graphics()); |
| 83 |
} |
| 84 |
|
| 85 |
void structRealTierArea :: v_drawInside () { |
| 86 |
Graphics_setWindow (our graphics(), our startWindow(), our endWindow(), our ymin, our ymax); |
| 87 |
Graphics_setColour (our graphics(), Melder_RED); |
| 88 |
Graphics_line (our graphics(), our startWindow(), our ycursor, our endWindow(), our ycursor); |
| 89 |
Graphics_setTextAlignment (our graphics(), Graphics_RIGHT, Graphics_HALF); |
| 90 |
Graphics_text (our graphics(), our startWindow(), our ycursor, |
| 91 |
Melder_graphicalHalf (our ycursor), our v_rightTickUnits()); |
| 92 |
Graphics_setColour (our graphics(), DataGuiColour_EDITABLE); |
| 93 |
Graphics_setTextAlignment (our graphics(), Graphics_LEFT, Graphics_HALF); |
| 94 |
Graphics_text (our graphics(), our endWindow(), our ymax, |
| 95 |
Melder_graphicalHalf (our ymax), our v_rightTickUnits()); |
| 96 |
Graphics_setTextAlignment (our graphics(), Graphics_LEFT, Graphics_HALF); |
| 97 |
Graphics_text (our graphics(), our endWindow(), our ymin, |
| 98 |
Melder_graphicalHalf (our ymin), our v_rightTickUnits()); |
| 99 |
|
| 100 |
const integer ifirstSelected = AnyTier_timeToHighIndex (our realTier()->asAnyTier(), our startSelection()); |
| 101 |
const integer ilastSelected = AnyTier_timeToLowIndex (our realTier()->asAnyTier(), our endSelection()); |
| 102 |
const integer imin = AnyTier_timeToHighIndex (our realTier()->asAnyTier(), our startWindow()); |
| 103 |
const integer imax = AnyTier_timeToLowIndex (our realTier()->asAnyTier(), our endWindow()); |
| 104 |
Graphics_setLineWidth (our graphics(), 2.0); |
| 105 |
if (our realTier() -> points.size == 0) { |
| 106 |
Graphics_setTextAlignment (our graphics(), Graphics_CENTRE, Graphics_HALF); |
| 107 |
Graphics_text (our graphics(), 0.5 * (our startWindow() + our endWindow()), 0.5 * (our ymin + our ymax), |
| 108 |
U"(no points)"); |
| 109 |
} else if (imax < imin) { |
| 110 |
const double yleft = RealTier_getValueAtTime (our realTier(), our startWindow()); |
| 111 |
const double yright = RealTier_getValueAtTime (our realTier(), our endWindow()); |
| 112 |
Graphics_line (our graphics(), our startWindow(), yleft, our endWindow(), yright); |
| 113 |
} else { |
| 114 |
for (integer ipoint = imin; ipoint <= imax; ipoint ++) { |
| 115 |
RealPoint point = our realTier() -> points.at [ipoint]; |
| 116 |
const double t = point -> number, y = point -> value; |
| 117 |
if (ipoint == 1) |
| 118 |
Graphics_line (our graphics(), our startWindow(), y, t, y); |
| 119 |
else if (ipoint == imin) |
| 120 |
Graphics_line (our graphics(), t, y, our startWindow(), RealTier_getValueAtTime (our realTier(), our startWindow())); |
| 121 |
if (ipoint == our realTier() -> points.size) |
| 122 |
Graphics_line (our graphics(), t, y, our endWindow(), y); |
| 123 |
else if (ipoint == imax) |
| 124 |
Graphics_line (our graphics(), t, y, our endWindow(), RealTier_getValueAtTime (our realTier(), our endWindow())); |
| 125 |
else { |
| 126 |
RealPoint pointRight = our realTier() -> points.at [ipoint + 1]; |
| 127 |
Graphics_line (our graphics(), t, y, pointRight -> number, pointRight -> value); |
| 128 |
} |
| 129 |
} |
| 130 |
for (integer ipoint = imin; ipoint <= imax; ipoint ++) { |
| 131 |
RealPoint point = our realTier() -> points.at [ipoint]; |
| 132 |
const double t = point -> number, y = point -> value; |
| 133 |
const bool pointIsSelected = ( ipoint >= ifirstSelected && ipoint <= ilastSelected ); |
| 134 |
Graphics_setColour (our graphics(), DataGui_defaultForegroundColour (this, pointIsSelected)); |
| 135 |
Graphics_fillCircle_mm (our graphics(), t, y, 3.0); |
| 136 |
} |
| 137 |
} |
| 138 |
Graphics_setLineWidth (our graphics(), 1.0); |
| 139 |
Graphics_setColour (our graphics(), Melder_BLACK); |
| 140 |
if (isdefined (our anchorTime)) |
| 141 |
RealTierArea_drawWhileDragging (this); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
#pragma mark - RealTierArea tracking |
| 146 |
|
| 147 |
bool structRealTierArea :: v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) { |
| 148 |
if (event -> isClick()) { |
| 149 |
our anchorIsInFreePart = false; |
| 150 |
our anchorIsNearPoint = false; |
| 151 |
} |
| 152 |
const double y_world = (1.0 - localY_fraction) * our ymin + localY_fraction * our ymax; |
| 153 |
our viewRealTierAsWorldByWorld (); |
| 154 |
if (event -> isClick()) { |
| 155 |
if (isdefined (our anchorTime)) |
| 156 |
return false; |
| 157 |
RealPoint clickedPoint = nullptr; |
| 158 |
const integer inearestPoint = AnyTier_timeToNearestIndexInTimeWindow (our realTier()->asAnyTier(), x_world, |
| 159 |
our startWindow(), our endWindow()); |
| 160 |
if (inearestPoint != 0) { |
| 161 |
RealPoint nearestPoint = our realTier() -> points.at [inearestPoint]; |
| 162 |
if (Graphics_distanceWCtoMM (our graphics(), x_world, y_world, nearestPoint -> number, nearestPoint -> value) < 1.5) |
| 163 |
clickedPoint = nearestPoint; |
| 164 |
} |
| 165 |
if (! clickedPoint) { |
| 166 |
our anchorIsInFreePart = true; |
| 167 |
our ycursor = y_world; |
| 168 |
return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world) || true; |
| 169 |
} |
| 170 |
our anchorIsNearPoint = true; |
| 171 |
our draggingSelection = event -> shiftKeyPressed && |
| 172 |
clickedPoint -> number >= our startSelection() && clickedPoint -> number <= our endSelection(); |
| 173 |
if (our draggingSelection) |
| 174 |
AnyTier_getWindowPoints (our realTier()->asAnyTier(), our startSelection(), our endSelection(), |
| 175 |
& our firstSelected, & our lastSelected); |
| 176 |
else |
| 177 |
our firstSelected = our lastSelected = inearestPoint; |
| 178 |
our anchorTime = x_world; |
| 179 |
our anchorY = y_world; |
| 180 |
our dt = 0.0; |
| 181 |
our dy = 0.0; |
| 182 |
return FunctionEditor_UPDATE_NEEDED; |
| 183 |
} else if (event -> isDrag() || event -> isDrop()) { |
| 184 |
if (our anchorIsInFreePart) { |
| 185 |
our ycursor = y_world; |
| 186 |
return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world) || true; |
| 187 |
} |
| 188 |
Melder_assert (our anchorIsNearPoint); |
| 189 |
our dt = ( event -> optionKeyPressed ? 0.0 : x_world - our anchorTime ); |
| 190 |
our dy = ( event -> commandKeyPressed ? 0.0 : y_world - our anchorY ); |
| 191 |
|
| 192 |
if (event -> isDrop()) { |
| 193 |
our anchorTime = undefined; |
| 194 |
|
| 195 |
const bool somethingHasMoved = ( our dt != 0.0 || our dy != 0.0 ); |
| 196 |
if (somethingHasMoved) { |
| 197 |
if (our draggingSelection) |
| 198 |
FunctionArea_save (this, U"Drag points"); // TODO: title can be more specific |
| 199 |
else |
| 200 |
FunctionArea_save (this, U"Drag point"); // TODO: title can be more specific |
| 201 |
const double leftNewTime = our realTier() -> points.at [our firstSelected] -> number + our dt; |
| 202 |
const double rightNewTime = our realTier() -> points.at [our lastSelected] -> number + our dt; |
| 203 |
const bool offLeft = ( leftNewTime < our tmin() ); |
| 204 |
const bool offRight = ( rightNewTime > our tmax() ); |
| 205 |
const bool draggedPastLeftNeighbour = ( our firstSelected > 1 && |
| 206 |
leftNewTime <= our realTier() -> points.at [our firstSelected - 1] -> number ); |
| 207 |
const bool draggedPastRightNeighbour = ( our lastSelected < our realTier() -> points.size && |
| 208 |
rightNewTime >= our realTier() -> points.at [our lastSelected + 1] -> number ); |
| 209 |
if (offLeft || offRight || draggedPastLeftNeighbour || draggedPastRightNeighbour) { |
| 210 |
Melder_beep (); |
| 211 |
return FunctionEditor_UPDATE_NEEDED; |
| 212 |
} |
| 213 |
|
| 214 |
for (integer i = our firstSelected; i <= our lastSelected; i ++) { |
| 215 |
RealPoint point = our realTier() -> points.at [i]; |
| 216 |
point -> number += our dt; |
| 217 |
double pointY = point -> value; |
| 218 |
pointY += our dy; |
| 219 |
Melder_clip (our v_minimumLegalY(), & pointY, our v_maximumLegalY()); |
| 220 |
point -> value = pointY; |
| 221 |
} |
| 222 |
|
| 223 |
/* |
| 224 |
Make sure that the same points are still selected (a problem with Undo...). |
| 225 |
*/ |
| 226 |
if (our draggingSelection) |
| 227 |
our setSelection (our startSelection() + our dt, our endSelection() + our dt); |
| 228 |
} |
| 229 |
if (our firstSelected == our lastSelected) { |
| 230 |
/* |
| 231 |
Move crosshair to only selected point. |
| 232 |
*/ |
| 233 |
RealPoint point = our realTier() -> points.at [our firstSelected]; |
| 234 |
our setSelection (point -> number, point -> number); |
| 235 |
our ycursor = point -> value; |
| 236 |
} else { |
| 237 |
/* |
| 238 |
Move crosshair to mouse location. |
| 239 |
*/ |
| 240 |
our ycursor += our dy; |
| 241 |
Melder_clip (our v_minimumLegalY(), & our ycursor, our v_maximumLegalY()); // NaN-safe |
| 242 |
} |
| 243 |
FunctionArea_broadcastDataChanged (this); |
| 244 |
} |
| 245 |
} |
| 246 |
return FunctionEditor_UPDATE_NEEDED; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
#pragma mark - RealTierArea Modify |
| 251 |
|
| 252 |
static void menu_cb_removePoints (RealTierArea me, EDITOR_ARGS) { |
| 253 |
FunctionArea_save (me, U"Remove point(s)"); |
| 254 |
RealTierArea_removePoints (me); |
| 255 |
FunctionArea_broadcastDataChanged (me); |
| 256 |
} |
| 257 |
static void menu_cb_addPointAtCursor (RealTierArea me, EDITOR_ARGS) { |
| 258 |
FunctionArea_save (me, U"Add point"); |
| 259 |
RealTierArea_addPointAtCursor (me); |
| 260 |
FunctionArea_broadcastDataChanged (me); |
| 261 |
} |
| 262 |
static void menu_cb_addPointAt (RealTierArea me, EDITOR_ARGS) { |
| 263 |
EDITOR_FORM (U"Add point", nullptr) |
| 264 |
REAL (time, U"Time (s)", U"0.0") |
| 265 |
REAL (desiredY, my v_quantityText (), U"0.0") |
| 266 |
EDITOR_OK |
| 267 |
SET_REAL (time, 0.5 * (my startSelection() + my endSelection())) |
| 268 |
SET_REAL (desiredY, my ycursor) |
| 269 |
EDITOR_DO |
| 270 |
FunctionArea_save (me, U"Add point"); |
| 271 |
RealTierArea_addPointAt (me, time, desiredY); |
| 272 |
FunctionArea_broadcastDataChanged (me); |
| 273 |
EDITOR_END |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
#pragma mark - RealTierArea View vertical |
| 278 |
|
| 279 |
static void menu_cb_setRange (RealTierArea me, EDITOR_ARGS) { |
| 280 |
EDITOR_FORM (my v_setRangeTitle (), nullptr) |
| 281 |
REAL (ymin, my v_minimumLabelText (), my default_dataFreeMinimum()) |
| 282 |
REAL (ymax, my v_maximumLabelText (), my default_dataFreeMaximum()) |
| 283 |
EDITOR_OK |
| 284 |
SET_REAL (ymin, my instancePref_dataFreeMinimum()) |
| 285 |
SET_REAL (ymax, my instancePref_dataFreeMaximum()) |
| 286 |
EDITOR_DO |
| 287 |
my setInstancePref_dataFreeMinimum (ymin); |
| 288 |
my setInstancePref_dataFreeMaximum (ymax); |
| 289 |
my v_updateScaling (); // BUG: should be viewChanged() |
| 290 |
FunctionEditor_redraw (my functionEditor()); |
| 291 |
EDITOR_END |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
#pragma mark - RealTierArea all menus |
| 296 |
|
| 297 |
void structRealTierArea :: v_createMenus () { |
| 298 |
RealTierArea_Parent :: v_createMenus (); |
| 299 |
EditorMenu menu = Editor_addMenu (our functionEditor(), our v_menuTitle (), 0); |
| 300 |
FunctionAreaMenu_addCommand (menu, U"-- view/realtier --", 0, nullptr, this); |
| 301 |
FunctionAreaMenu_addCommand (menu, our v_setRangeTitle (), 0, |
| 302 |
menu_cb_setRange, this); |
| 303 |
FunctionAreaMenu_addCommand (menu, U"Add point at cursor", 'T', |
| 304 |
menu_cb_addPointAtCursor, this); |
| 305 |
FunctionAreaMenu_addCommand (menu, U"Add point at...", 0, |
| 306 |
menu_cb_addPointAt, this); |
| 307 |
FunctionAreaMenu_addCommand (menu, U"-- remove point --", 0, nullptr, this); |
| 308 |
FunctionAreaMenu_addCommand (menu, U"Remove point(s)", GuiMenu_COMMAND_EXTRA | 'T', |
| 309 |
menu_cb_removePoints, this); |
| 310 |
} |
| 311 |
|
| 312 |
/* End of file RealTierArea.cpp */ |