| 1 | /* |
| 2 | * Copyright (C) 2015-2016, Mike Walters <[email protected]> |
| 3 | * |
| 4 | * This file is part of inspectrum. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "plotview.h" |
| 21 | #include <iostream> |
| 22 | #include <fstream> |
| 23 | #include <QtGlobal> |
| 24 | #include <QApplication> |
| 25 | #include <QClipboard> |
| 26 | #include <QDebug> |
| 27 | #include <QFileDialog> |
| 28 | #include <QGridLayout> |
| 29 | #include <QGroupBox> |
| 30 | #include <QMenu> |
| 31 | #include <QPainter> |
| 32 | #include <QProgressDialog> |
| 33 | #include <QRadioButton> |
| 34 | #include <QScrollBar> |
| 35 | #include <QSpinBox> |
| 36 | #include <QToolTip> |
| 37 | #include <QVBoxLayout> |
| 38 | #include "plots.h" |
| 39 | |
| 40 | PlotView::PlotView(InputSource *input) : cursors(this), viewRange({0, 0}) |
| 41 | { |
| 42 | mainSampleSource = input; |
| 43 | setDragMode(QGraphicsView::ScrollHandDrag); |
| 44 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); |
| 45 | setMouseTracking(true); |
| 46 | enableCursors(false); |
| 47 | connect(&cursors, &Cursors::cursorsMoved, this, &PlotView::cursorsMoved); |
| 48 | |
| 49 | spectrogramPlot = new SpectrogramPlot(std::shared_ptr<SampleSource<std::complex<float>>>(mainSampleSource)); |
| 50 | auto tunerOutput = std::dynamic_pointer_cast<SampleSource<std::complex<float>>>(spectrogramPlot->output()); |
| 51 | |
| 52 | enableScales(true); |
| 53 | |
| 54 | enableAnnotations(true); |
| 55 | enableAnnotationCommentsTooltips(true); |
| 56 | |
| 57 | addPlot(spectrogramPlot); |
| 58 | |
| 59 | mainSampleSource->subscribe(this); |
| 60 | } |
| 61 | |
| 62 | void PlotView::addPlot(Plot *plot) |
| 63 | { |
| 64 | plots.emplace_back(plot); |
| 65 | connect(plot, &Plot::repaint, this, &PlotView::repaint); |
| 66 | } |
| 67 | |
| 68 | void PlotView::mouseMoveEvent(QMouseEvent *event) |
| 69 | { |
| 70 | updateAnnotationTooltip(event); |
| 71 | QGraphicsView::mouseMoveEvent(event); |
| 72 | } |
| 73 | |
| 74 | void PlotView::mouseReleaseEvent(QMouseEvent *event) |
| 75 | { |
| 76 | // This is used to show the tooltip again on drag release if the mouse is |
| 77 | // hovering over an annotation. |
| 78 | updateAnnotationTooltip(event); |
| 79 | QGraphicsView::mouseReleaseEvent(event); |
| 80 | } |
| 81 | |
| 82 | void PlotView::updateAnnotationTooltip(QMouseEvent *event) |
| 83 | { |
| 84 | // If there are any mouse buttons pressed, we assume |
| 85 | // that the plot is being dragged and hide the tooltip. |
| 86 | bool isDrag = event->buttons() != Qt::NoButton; |
| 87 | if (!annotationCommentsEnabled |
| 88 | || !spectrogramPlot->isAnnotationsEnabled() |
| 89 | || isDrag) { |
| 90 | QToolTip::hideText(); |
| 91 | } else { |
| 92 | QString* comment = spectrogramPlot->mouseAnnotationComment(event); |
| 93 | if (comment != nullptr) { |
| 94 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 95 | QToolTip::showText(event->globalPosition().toPoint(), *comment); |
| 96 | #else |
| 97 | QToolTip::showText(event->globalPos(), *comment); |
| 98 | #endif |
| 99 | } else { |
| 100 | QToolTip::hideText(); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void PlotView::contextMenuEvent(QContextMenuEvent * event) |
| 106 | { |
| 107 | QMenu menu; |
| 108 | |
| 109 | // Get selected plot |
| 110 | Plot *selectedPlot = nullptr; |
| 111 | auto it = plots.begin(); |
| 112 | int y = -verticalScrollBar()->value(); |
| 113 | for (; it != plots.end(); it++) { |
| 114 | auto&& plot = *it; |
| 115 | if (range_t<int>{y, y + plot->height()}.contains(event->pos().y())) { |
| 116 | selectedPlot = plot.get(); |
| 117 | break; |
| 118 | } |
| 119 | y += plot->height(); |
| 120 | } |
| 121 | if (selectedPlot == nullptr) |
| 122 | return; |
| 123 | |
| 124 | // Add actions to add derived plots |
| 125 | // that are compatible with selectedPlot's output |
| 126 | QMenu *plotsMenu = menu.addMenu("Add derived plot"); |
| 127 | auto src = selectedPlot->output(); |
| 128 | auto compatiblePlots = as_range(Plots::plots.equal_range(src->sampleType())); |
| 129 | for (auto p : compatiblePlots) { |
| 130 | auto plotInfo = p.second; |
| 131 | auto action = new QAction(QString("Add %1").arg(plotInfo.name), plotsMenu); |
| 132 | auto plotCreator = plotInfo.creator; |
| 133 | connect( |
| 134 | action, &QAction::triggered, |
| 135 | this, [=]() { |
| 136 | addPlot(plotCreator(src)); |
| 137 | } |
| 138 | ); |
| 139 | plotsMenu->addAction(action); |
| 140 | } |
| 141 | |
| 142 | // Add submenu for extracting symbols |
| 143 | QMenu *extractMenu = menu.addMenu("Extract symbols"); |
| 144 | // Add action to extract symbols from selected plot to stdout |
| 145 | auto extract = new QAction("To stdout", extractMenu); |
| 146 | connect( |
| 147 | extract, &QAction::triggered, |
| 148 | this, [=]() { |
| 149 | extractSymbols(src, false); |
| 150 | } |
| 151 | ); |
| 152 | extract->setEnabled(cursorsEnabled && (src->sampleType() == typeid(float))); |
| 153 | extractMenu->addAction(extract); |
| 154 | |
| 155 | // Add action to extract symbols from selected plot to clipboard |
| 156 | auto extractClipboard = new QAction("Copy to clipboard", extractMenu); |
| 157 | connect( |
| 158 | extractClipboard, &QAction::triggered, |
| 159 | this, [=]() { |
| 160 | extractSymbols(src, true); |
| 161 | } |
| 162 | ); |
| 163 | extractClipboard->setEnabled(cursorsEnabled && (src->sampleType() == typeid(float))); |
| 164 | extractMenu->addAction(extractClipboard); |
| 165 | |
| 166 | // Add action to export the selected samples into a file |
| 167 | auto save = new QAction("Export samples to file...", &menu); |
| 168 | connect( |
| 169 | save, &QAction::triggered, |
| 170 | this, [=]() { |
| 171 | if (selectedPlot == spectrogramPlot) { |
| 172 | exportSamples(spectrogramPlot->tunerEnabled() ? spectrogramPlot->output() : spectrogramPlot->input()); |
| 173 | } else { |
| 174 | exportSamples(src); |
| 175 | } |
| 176 | } |
| 177 | ); |
| 178 | menu.addAction(save); |
| 179 | |
| 180 | // Add action to remove the selected plot |
| 181 | auto rem = new QAction("Remove plot", &menu); |
| 182 | connect( |
| 183 | rem, &QAction::triggered, |
| 184 | this, [=]() { |
| 185 | plots.erase(it); |
| 186 | } |
| 187 | ); |
| 188 | // Don't allow remove the first plot (the spectrogram) |
| 189 | rem->setEnabled(it != plots.begin()); |
| 190 | menu.addAction(rem); |
| 191 | |
| 192 | updateViewRange(false); |
| 193 | if(menu.exec(event->globalPos())) |
| 194 | updateView(false); |
| 195 | } |
| 196 | |
| 197 | void PlotView::cursorsMoved() |
| 198 | { |
| 199 | selectedSamples = { |
| 200 | columnToSample(horizontalScrollBar()->value() + cursors.selection().minimum), |
| 201 | columnToSample(horizontalScrollBar()->value() + cursors.selection().maximum) |
| 202 | }; |
| 203 | |
| 204 | emitTimeSelection(); |
| 205 | viewport()->update(); |
| 206 | } |
| 207 | |
| 208 | void PlotView::emitTimeSelection() |
| 209 | { |
| 210 | size_t sampleCount = selectedSamples.length(); |
| 211 | float selectionTime = sampleCount / (float)mainSampleSource->rate(); |
| 212 | emit timeSelectionChanged(selectionTime); |
| 213 | } |
| 214 | |
| 215 | void PlotView::enableCursors(bool enabled) |
| 216 | { |
| 217 | cursorsEnabled = enabled; |
| 218 | if (enabled) { |
| 219 | int margin = viewport()->rect().width() / 3; |
| 220 | cursors.setSelection({viewport()->rect().left() + margin, viewport()->rect().right() - margin}); |
| 221 | cursorsMoved(); |
| 222 | } |
| 223 | viewport()->update(); |
| 224 | } |
| 225 | |
| 226 | bool PlotView::viewportEvent(QEvent *event) { |
| 227 | // Handle wheel events for zooming (before the parent's handler to stop normal scrolling) |
| 228 | if (event->type() == QEvent::Wheel) { |
| 229 | QWheelEvent *wheelEvent = (QWheelEvent*)event; |
| 230 | if (QApplication::keyboardModifiers() & Qt::ControlModifier) { |
| 231 | bool canZoomIn = zoomLevel < fftSize; |
| 232 | bool canZoomOut = zoomLevel > 1; |
| 233 | int delta = wheelEvent->angleDelta().y(); |
| 234 | if ((delta > 0 && canZoomIn) || (delta < 0 && canZoomOut)) { |
| 235 | scrollZoomStepsAccumulated += delta; |
| 236 | |
| 237 | // `updateViewRange()` keeps the center sample in the same place after zoom. Apply |
| 238 | // a scroll adjustment to keep the sample under the mouse cursor in the same place instead. |
| 239 | #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) |
| 240 | zoomPos = wheelEvent->position().x(); |
| 241 | #else |
| 242 | zoomPos = wheelEvent->pos().x(); |
| 243 | #endif |
| 244 | zoomSample = columnToSample(horizontalScrollBar()->value() + zoomPos); |
| 245 | if (scrollZoomStepsAccumulated >= 120) { |
| 246 | scrollZoomStepsAccumulated -= 120; |
| 247 | emit zoomIn(); |
| 248 | } else if (scrollZoomStepsAccumulated <= -120) { |
| 249 | scrollZoomStepsAccumulated += 120; |
| 250 | emit zoomOut(); |
| 251 | } |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Pass mouse events to individual plot objects |
| 258 | if (event->type() == QEvent::MouseButtonPress || |
| 259 | event->type() == QEvent::MouseMove || |
| 260 | event->type() == QEvent::MouseButtonRelease) { |
| 261 | |
| 262 | QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); |
| 263 | |
| 264 | int plotY = -verticalScrollBar()->value(); |
| 265 | for (auto&& plot : plots) { |
| 266 | auto mouse_event = QMouseEvent( |
| 267 | event->type(), |
| 268 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 269 | QPoint(mouseEvent->position().x(), mouseEvent->position().y() - plotY), |
| 270 | #else |
| 271 | QPoint(mouseEvent->pos().x(), mouseEvent->pos().y() - plotY), |
| 272 | #endif |
| 273 | mouseEvent->button(), |
| 274 | mouseEvent->buttons(), |
| 275 | QApplication::keyboardModifiers() |
| 276 | ); |
| 277 | bool result = plot->mouseEvent( |
| 278 | event->type(), |
| 279 | &mouse_event |
| 280 | ); |
| 281 | if (result) |
| 282 | return true; |
| 283 | plotY += plot->height(); |
| 284 | } |
| 285 | |
| 286 | if (cursorsEnabled) |
| 287 | if (cursors.mouseEvent(event->type(), mouseEvent)) |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | if (event->type() == QEvent::Leave) { |
| 292 | for (auto&& plot : plots) { |
| 293 | plot->leaveEvent(); |
| 294 | } |
| 295 | |
| 296 | if (cursorsEnabled) |
| 297 | cursors.leaveEvent(); |
| 298 | } |
| 299 | |
| 300 | // Handle parent eveents |
| 301 | return QGraphicsView::viewportEvent(event); |
| 302 | } |
| 303 | |
| 304 | void PlotView::extractSymbols(std::shared_ptr<AbstractSampleSource> src, |
| 305 | bool toClipboard) |
| 306 | { |
| 307 | if (!cursorsEnabled) |
| 308 | return; |
| 309 | auto floatSrc = std::dynamic_pointer_cast<SampleSource<float>>(src); |
| 310 | if (!floatSrc) |
| 311 | return; |
| 312 | auto samples = floatSrc->getSamples(selectedSamples.minimum, selectedSamples.length()); |
| 313 | auto step = (float)selectedSamples.length() / cursors.segments(); |
| 314 | auto symbols = std::vector<float>(); |
| 315 | for (auto i = step / 2; i < selectedSamples.length(); i += step) |
| 316 | { |
| 317 | symbols.push_back(samples[i]); |
| 318 | } |
| 319 | if (!toClipboard) { |
| 320 | for (auto f : symbols) |
| 321 | std::cout << f << ", "; |
| 322 | std::cout << std::endl << std::flush; |
| 323 | } else { |
| 324 | QClipboard *clipboard = QGuiApplication::clipboard(); |
| 325 | QString symbolText; |
| 326 | QTextStream symbolStream(&symbolText); |
| 327 | for (auto f : symbols) |
| 328 | symbolStream << f << ", "; |
| 329 | clipboard->setText(symbolText); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void PlotView::exportSamples(std::shared_ptr<AbstractSampleSource> src) |
| 334 | { |
| 335 | if (src->sampleType() == typeid(std::complex<float>)) { |
| 336 | exportSamples<std::complex<float>>(src); |
| 337 | } else { |
| 338 | exportSamples<float>(src); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | template<typename SOURCETYPE> |
| 343 | void PlotView::exportSamples(std::shared_ptr<AbstractSampleSource> src) |
| 344 | { |
| 345 | auto sampleSrc = std::dynamic_pointer_cast<SampleSource<SOURCETYPE>>(src); |
| 346 | if (!sampleSrc) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | QFileDialog dialog(this); |
| 351 | dialog.setAcceptMode(QFileDialog::AcceptSave); |
| 352 | dialog.setFileMode(QFileDialog::AnyFile); |
| 353 | dialog.setNameFilter(getFileNameFilter<SOURCETYPE>()); |
| 354 | dialog.setOption(QFileDialog::DontUseNativeDialog, true); |
| 355 | |
| 356 | QGroupBox groupBox("Selection To Export", &dialog); |
| 357 | QVBoxLayout vbox(&groupBox); |
| 358 | |
| 359 | QRadioButton cursorSelection("Cursor Selection", &groupBox); |
| 360 | QRadioButton currentView("Current View", &groupBox); |
| 361 | QRadioButton completeFile("Complete File (Experimental)", &groupBox); |
| 362 | |
| 363 | if (cursorsEnabled) { |
| 364 | cursorSelection.setChecked(true); |
| 365 | } else { |
| 366 | currentView.setChecked(true); |
| 367 | cursorSelection.setEnabled(false); |
| 368 | } |
| 369 | |
| 370 | vbox.addWidget(&cursorSelection); |
| 371 | vbox.addWidget(¤tView); |
| 372 | vbox.addWidget(&completeFile); |
| 373 | vbox.addStretch(1); |
| 374 | |
| 375 | groupBox.setLayout(&vbox); |
| 376 | |
| 377 | QGridLayout *l = dialog.findChild<QGridLayout*>(); |
| 378 | l->addWidget(&groupBox, 4, 1); |
| 379 | |
| 380 | QGroupBox groupBox2("Decimation"); |
| 381 | QSpinBox decimation(&groupBox2); |
| 382 | decimation.setMinimum(1); |
| 383 | decimation.setValue(1 / sampleSrc->relativeBandwidth()); |
| 384 | |
| 385 | QVBoxLayout vbox2; |
| 386 | vbox2.addWidget(&decimation); |
| 387 | |
| 388 | groupBox2.setLayout(&vbox2); |
| 389 | l->addWidget(&groupBox2, 4, 2); |
| 390 | |
| 391 | if (dialog.exec()) { |
| 392 | QStringList fileNames = dialog.selectedFiles(); |
| 393 | |
| 394 | size_t start, end; |
| 395 | if (cursorSelection.isChecked()) { |
| 396 | start = selectedSamples.minimum; |
| 397 | end = start + selectedSamples.length(); |
| 398 | } else if(currentView.isChecked()) { |
| 399 | start = viewRange.minimum; |
| 400 | end = start + viewRange.length(); |
| 401 | } else { |
| 402 | start = 0; |
| 403 | end = sampleSrc->count(); |
| 404 | } |
| 405 | |
| 406 | std::ofstream os (fileNames[0].toStdString(), std::ios::binary); |
| 407 | |
| 408 | size_t index; |
| 409 | // viewRange.length() is used as some less arbitrary step value |
| 410 | size_t step = viewRange.length(); |
| 411 | |
| 412 | QProgressDialog progress("Exporting samples...", "Cancel", start, end, this); |
| 413 | progress.setWindowModality(Qt::WindowModal); |
| 414 | for (index = start; index < end; index += step) { |
| 415 | progress.setValue(index); |
| 416 | if (progress.wasCanceled()) |
| 417 | break; |
| 418 | |
| 419 | size_t length = std::min(step, end - index); |
| 420 | auto samples = sampleSrc->getSamples(index, length); |
| 421 | if (samples != nullptr) { |
| 422 | for (auto i = 0; i < length; i += decimation.value()) { |
| 423 | os.write((const char*)&samples[i], sizeof(SOURCETYPE)); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void PlotView::invalidateEvent() |
| 431 | { |
| 432 | horizontalScrollBar()->setMinimum(0); |
| 433 | horizontalScrollBar()->setMaximum(sampleToColumn(mainSampleSource->count())); |
| 434 | } |
| 435 | |
| 436 | void PlotView::repaint() |
| 437 | { |
| 438 | viewport()->update(); |
| 439 | } |
| 440 | |
| 441 | void PlotView::setCursorSegments(int segments) |
| 442 | { |
| 443 | // Calculate number of samples per segment |
| 444 | float sampPerSeg = (float)selectedSamples.length() / cursors.segments(); |
| 445 | |
| 446 | // Alter selection to keep samples per segment the same |
| 447 | selectedSamples.maximum = selectedSamples.minimum + (segments * sampPerSeg + 0.5f); |
| 448 | |
| 449 | cursors.setSegments(segments); |
| 450 | updateView(); |
| 451 | emitTimeSelection(); |
| 452 | } |
| 453 | |
| 454 | void PlotView::setFFTAndZoom(int size, int zoom) |
| 455 | { |
| 456 | auto oldSamplesPerColumn = samplesPerColumn(); |
| 457 | |
| 458 | // Set new FFT size |
| 459 | fftSize = size; |
| 460 | if (spectrogramPlot != nullptr) |
| 461 | spectrogramPlot->setFFTSize(size); |
| 462 | |
| 463 | // Set new zoom level |
| 464 | zoomLevel = zoom; |
| 465 | if (spectrogramPlot != nullptr) |
| 466 | spectrogramPlot->setZoomLevel(zoom); |
| 467 | |
| 468 | // Update horizontal (time) scrollbar |
| 469 | horizontalScrollBar()->setSingleStep(10); |
| 470 | horizontalScrollBar()->setPageStep(100); |
| 471 | |
| 472 | updateView(true, samplesPerColumn() < oldSamplesPerColumn); |
| 473 | } |
| 474 | |
| 475 | void PlotView::setPowerMin(int power) |
| 476 | { |
| 477 | powerMin = power; |
| 478 | if (spectrogramPlot != nullptr) |
| 479 | spectrogramPlot->setPowerMin(power); |
| 480 | updateView(); |
| 481 | } |
| 482 | |
| 483 | void PlotView::setPowerMax(int power) |
| 484 | { |
| 485 | powerMax = power; |
| 486 | if (spectrogramPlot != nullptr) |
| 487 | spectrogramPlot->setPowerMax(power); |
| 488 | updateView(); |
| 489 | } |
| 490 | |
| 491 | void PlotView::paintEvent(QPaintEvent *event) |
| 492 | { |
| 493 | if (mainSampleSource == nullptr) return; |
| 494 | |
| 495 | QRect rect = QRect(0, 0, width(), height()); |
| 496 | QPainter painter(viewport()); |
| 497 | painter.fillRect(rect, Qt::black); |
| 498 | |
| 499 | |
| 500 | #define PLOT_LAYER(paintFunc) \ |
| 501 | { \ |
| 502 | int y = -verticalScrollBar()->value(); \ |
| 503 | for (auto&& plot : plots) { \ |
| 504 | QRect rect = QRect(0, y, width(), plot->height()); \ |
| 505 | plot->paintFunc(painter, rect, viewRange); \ |
| 506 | y += plot->height(); \ |
| 507 | } \ |
| 508 | } |
| 509 | |
| 510 | PLOT_LAYER(paintBack); |
| 511 | PLOT_LAYER(paintMid); |
| 512 | PLOT_LAYER(paintFront); |
| 513 | if (cursorsEnabled) |
| 514 | cursors.paintFront(painter, rect, viewRange); |
| 515 | |
| 516 | if (timeScaleEnabled) { |
| 517 | paintTimeScale(painter, rect, viewRange); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | #undef PLOT_LAYER |
| 522 | } |
| 523 | |
| 524 | void PlotView::paintTimeScale(QPainter &painter, QRect &rect, range_t<size_t> sampleRange) |
| 525 | { |
| 526 | float startTime = (float)sampleRange.minimum / sampleRate; |
| 527 | float stopTime = (float)sampleRange.maximum / sampleRate; |
| 528 | float duration = stopTime - startTime; |
| 529 | |
| 530 | if (duration <= 0) |
| 531 | return; |
| 532 | |
| 533 | painter.save(); |
| 534 | |
| 535 | QPen pen(Qt::white, 1, Qt::SolidLine); |
| 536 | painter.setPen(pen); |
| 537 | QFontMetrics fm(painter.font()); |
| 538 | |
| 539 | int tickWidth = 80; |
| 540 | int maxTicks = rect.width() / tickWidth; |
| 541 | |
| 542 | double durationPerTick = 10 * pow(10, floor(log(duration / maxTicks) / log(10))); |
| 543 | |
| 544 | double firstTick = int(startTime / durationPerTick) * durationPerTick; |
| 545 | |
| 546 | double tick = firstTick; |
| 547 | |
| 548 | while (tick <= stopTime) { |
| 549 | |
| 550 | size_t tickSample = tick * sampleRate; |
| 551 | int tickLine = sampleToColumn(tickSample - sampleRange.minimum); |
| 552 | |
| 553 | char buf[128]; |
| 554 | snprintf(buf, sizeof(buf), "%.06f", tick); |
| 555 | painter.drawLine(tickLine, 0, tickLine, 30); |
| 556 | painter.drawText(tickLine + 2, 25, buf); |
| 557 | |
| 558 | tick += durationPerTick; |
| 559 | } |
| 560 | |
| 561 | // Draw small ticks |
| 562 | durationPerTick /= 10; |
| 563 | firstTick = int(startTime / durationPerTick) * durationPerTick; |
| 564 | tick = firstTick; |
| 565 | while (tick <= stopTime) { |
| 566 | |
| 567 | size_t tickSample = tick * sampleRate; |
| 568 | int tickLine = sampleToColumn(tickSample - sampleRange.minimum); |
| 569 | |
| 570 | painter.drawLine(tickLine, 0, tickLine, 10); |
| 571 | tick += durationPerTick; |
| 572 | } |
| 573 | |
| 574 | painter.restore(); |
| 575 | } |
| 576 | |
| 577 | int PlotView::plotsHeight() |
| 578 | { |
| 579 | int height = 0; |
| 580 | for (auto&& plot : plots) { |
| 581 | height += plot->height(); |
| 582 | } |
| 583 | return height; |
| 584 | } |
| 585 | |
| 586 | void PlotView::resizeEvent(QResizeEvent * event) |
| 587 | { |
| 588 | updateView(); |
| 589 | } |
| 590 | |
| 591 | size_t PlotView::samplesPerColumn() |
| 592 | { |
| 593 | return fftSize / zoomLevel; |
| 594 | } |
| 595 | |
| 596 | void PlotView::scrollContentsBy(int dx, int dy) |
| 597 | { |
| 598 | updateView(); |
| 599 | } |
| 600 | |
| 601 | void PlotView::showEvent(QShowEvent *event) |
| 602 | { |
| 603 | // Intentionally left blank. See #171 |
| 604 | } |
| 605 | |
| 606 | void PlotView::updateViewRange(bool reCenter) |
| 607 | { |
| 608 | // Update current view |
| 609 | auto start = columnToSample(horizontalScrollBar()->value()); |
| 610 | viewRange = {start, std::min(start + columnToSample(width()), mainSampleSource->count())}; |
| 611 | |
| 612 | // Adjust time offset to zoom around central sample |
| 613 | if (reCenter) { |
| 614 | horizontalScrollBar()->setValue( |
| 615 | sampleToColumn(zoomSample) - zoomPos |
| 616 | ); |
| 617 | } |
| 618 | zoomSample = viewRange.minimum + viewRange.length() / 2; |
| 619 | zoomPos = width() / 2; |
| 620 | } |
| 621 | |
| 622 | void PlotView::updateView(bool reCenter, bool expanding) |
| 623 | { |
| 624 | if (!expanding) { |
| 625 | updateViewRange(reCenter); |
| 626 | } |
| 627 | horizontalScrollBar()->setMaximum(std::max(0, sampleToColumn(mainSampleSource->count()) - width())); |
| 628 | verticalScrollBar()->setMaximum(std::max(0, plotsHeight() - viewport()->height())); |
| 629 | if (expanding) { |
| 630 | updateViewRange(reCenter); |
| 631 | } |
| 632 | |
| 633 | // Update cursors |
| 634 | range_t<int> newSelection = { |
| 635 | sampleToColumn(selectedSamples.minimum) - horizontalScrollBar()->value(), |
| 636 | sampleToColumn(selectedSamples.maximum) - horizontalScrollBar()->value() |
| 637 | }; |
| 638 | cursors.setSelection(newSelection); |
| 639 | |
| 640 | // Re-paint |
| 641 | viewport()->update(); |
| 642 | } |
| 643 | |
| 644 | void PlotView::setSampleRate(double rate) |
| 645 | { |
| 646 | sampleRate = rate; |
| 647 | |
| 648 | if (spectrogramPlot != nullptr) |
| 649 | spectrogramPlot->setSampleRate(rate); |
| 650 | |
| 651 | emitTimeSelection(); |
| 652 | } |
| 653 | |
| 654 | void PlotView::enableScales(bool enabled) |
| 655 | { |
| 656 | timeScaleEnabled = enabled; |
| 657 | |
| 658 | if (spectrogramPlot != nullptr) |
| 659 | spectrogramPlot->enableScales(enabled); |
| 660 | |
| 661 | viewport()->update(); |
| 662 | } |
| 663 | |
| 664 | void PlotView::enableAnnotations(bool enabled) |
| 665 | { |
| 666 | if (spectrogramPlot != nullptr) |
| 667 | spectrogramPlot->enableAnnotations(enabled); |
| 668 | |
| 669 | viewport()->update(); |
| 670 | } |
| 671 | |
| 672 | void PlotView::enableAnnotationCommentsTooltips(bool enabled) |
| 673 | { |
| 674 | annotationCommentsEnabled = enabled; |
| 675 | |
| 676 | viewport()->update(); |
| 677 | } |
| 678 | |
| 679 | int PlotView::sampleToColumn(size_t sample) |
| 680 | { |
| 681 | return sample / samplesPerColumn(); |
| 682 | } |
| 683 | |
| 684 | size_t PlotView::columnToSample(int col) |
| 685 | { |
| 686 | return col * samplesPerColumn(); |
| 687 | } |