1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/*
* This file is part of the PulseView project.
*
* Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
*
* This program 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.
*
* This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <cassert>
#include <QColor>
#include <QMenu>
#include <QToolTip>
#include "cursorpair.hpp"
#include "pv/globalsettings.hpp"
#include "pv/util.hpp"
#include "ruler.hpp"
#include "view.hpp"
using std::max;
using std::make_pair;
using std::min;
using std::shared_ptr;
using std::pair;
namespace pv {
namespace views {
namespace trace {
const int CursorPair::DeltaPadding = 8;
CursorPair::CursorPair(View &view) :
TimeItem(view),
first_(new Cursor(view, 0.0)),
second_(new Cursor(view, 1.0)),
label_incomplete_(true)
{
GlobalSettings::add_change_handler(this);
GlobalSettings settings;
fill_color_ = QColor::fromRgba(settings.value(
GlobalSettings::Key_View_CursorFillColor).value<uint32_t>());
show_frequency_ = settings.value(
GlobalSettings::Key_View_CursorShowFrequency).value<bool>();
show_interval_ = settings.value(
GlobalSettings::Key_View_CursorShowInterval).value<bool>();
show_samples_ = settings.value(
GlobalSettings::Key_View_CursorShowSamples).value<bool>();
connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
}
CursorPair::~CursorPair()
{
GlobalSettings::remove_change_handler(this);
}
bool CursorPair::enabled() const
{
return view_.cursors_shown();
}
shared_ptr<Cursor> CursorPair::first() const
{
return first_;
}
shared_ptr<Cursor> CursorPair::second() const
{
return second_;
}
void CursorPair::set_time(const pv::util::Timestamp& time)
{
const pv::util::Timestamp delta = second_->time() - first_->time();
first_->set_time(time);
second_->set_time(time + delta);
}
const pv::util::Timestamp CursorPair::time() const
{
return 0;
}
float CursorPair::get_x() const
{
return (first_->get_x() + second_->get_x()) / 2.0f;
}
const pv::util::Timestamp CursorPair::delta(const pv::util::Timestamp& other) const
{
if (other < second_->time())
return other - first_->time();
else
return other - second_->time();
}
QPoint CursorPair::drag_point(const QRect &rect) const
{
return first_->drag_point(rect);
}
pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
{
(void)parent;
return nullptr;
}
QMenu *CursorPair::create_header_context_menu(QWidget *parent)
{
QMenu *menu = new QMenu(parent);
QAction *displayIntervalAction = new QAction(tr("Display interval"), this);
displayIntervalAction->setCheckable(true);
displayIntervalAction->setChecked(show_interval_);
menu->addAction(displayIntervalAction);
connect(displayIntervalAction, &QAction::toggled, displayIntervalAction,
[=]{
GlobalSettings settings;
settings.setValue(GlobalSettings::Key_View_CursorShowInterval,
!settings.value(GlobalSettings::Key_View_CursorShowInterval).value<bool>());
});
QAction *displayFrequencyAction = new QAction(tr("Display frequency"), this);
displayFrequencyAction->setCheckable(true);
displayFrequencyAction->setChecked(show_frequency_);
menu->addAction(displayFrequencyAction);
connect(displayFrequencyAction, &QAction::toggled, displayFrequencyAction,
[=]{
GlobalSettings settings;
settings.setValue(GlobalSettings::Key_View_CursorShowFrequency,
!settings.value(GlobalSettings::Key_View_CursorShowFrequency).value<bool>());
});
QAction *displaySamplesAction = new QAction(tr("Display samples"), this);
displaySamplesAction->setCheckable(true);
displaySamplesAction->setChecked(show_samples_);
menu->addAction(displaySamplesAction);
connect(displaySamplesAction, &QAction::toggled, displaySamplesAction,
[=]{
GlobalSettings settings;
settings.setValue(GlobalSettings::Key_View_CursorShowSamples,
!settings.value(GlobalSettings::Key_View_CursorShowSamples).value<bool>());
});
return menu;
}
QRectF CursorPair::label_rect(const QRectF &rect) const
{
const QSizeF label_size(text_size_ + LabelPadding * 2);
const pair<float, float> offsets(get_cursor_offsets());
const pair<float, float> normal_offsets(
(offsets.first < offsets.second) ? offsets :
make_pair(offsets.second, offsets.first));
const float height = label_size.height();
const float left = max(normal_offsets.first + DeltaPadding, -height);
const float right = min(normal_offsets.second - DeltaPadding,
(float)rect.width() + height);
return QRectF(left, rect.height() - label_size.height() -
TimeMarker::ArrowSize - 0.5f,
right - left, height);
}
void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
{
assert(first_);
assert(second_);
if (!enabled())
return;
const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
p.setPen(text_color);
QRectF delta_rect(label_rect(rect));
const int radius = delta_rect.height() / 2;
QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
QString text = format_string(text_rect.width(),
[&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); });
text_size_ = p.boundingRect(QRectF(), 0, text).size();
if (selected()) {
p.setBrush(Qt::transparent);
p.setPen(highlight_pen());
p.drawRoundedRect(delta_rect, radius, radius);
}
p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor);
p.setPen(Cursor::FillColor.darker());
p.drawRoundedRect(delta_rect, radius, radius);
delta_rect.adjust(1, 1, -1, -1);
p.setPen(Cursor::FillColor.lighter());
const int highlight_radius = delta_rect.height() / 2 - 2;
p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius);
label_area_ = delta_rect;
p.setPen(text_color);
p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
}
void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
{
if (!enabled())
return;
p.setPen(Qt::NoPen);
p.setBrush(fill_color_);
const pair<float, float> offsets(get_cursor_offsets());
const int l = (int)max(min(offsets.first, offsets.second), 0.0f);
const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width());
p.drawRect(l, pp.top(), r - l, pp.height());
}
QString CursorPair::format_string(int max_width, std::function<double(const QString&)> query_size)
{
int time_precision = 12;
int freq_precision = 12;
QString s = format_string_sub(time_precision, freq_precision);
// Try full "{time} s / {freq} Hz" format
if ((max_width <= 0) || (query_size(s) <= max_width)) {
label_incomplete_ = false;
return s;
}
label_incomplete_ = true;
// Gradually reduce time precision to match frequency precision
while (time_precision > freq_precision) {
time_precision--;
s = format_string_sub(time_precision, freq_precision);
if (query_size(s) <= max_width)
return s;
}
// Gradually reduce both precisions down to zero
while (time_precision > 0) {
time_precision--;
freq_precision--;
s = format_string_sub(time_precision, freq_precision);
if (query_size(s) <= max_width)
return s;
}
// Try no trailing digits and drop the unit to at least display something
s = format_string_sub(0, 0, false);
if (query_size(s) <= max_width)
return s;
// Give up
return "...";
}
pair<float, float> CursorPair::get_cursor_offsets() const
{
assert(first_);
assert(second_);
return pair<float, float>(first_->get_x(), second_->get_x());
}
void CursorPair::on_setting_changed(const QString &key, const QVariant &value)
{
if (key == GlobalSettings::Key_View_CursorFillColor)
fill_color_ = QColor::fromRgba(value.value<uint32_t>());
if (key == GlobalSettings::Key_View_CursorShowFrequency)
show_frequency_ = value.value<bool>();
if (key == GlobalSettings::Key_View_CursorShowInterval)
show_interval_ = value.value<bool>();
if (key == GlobalSettings::Key_View_CursorShowSamples)
show_samples_ = value.value<bool>();
}
void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
{
if (widget != view_.ruler())
return;
if (!label_incomplete_)
return;
if (label_area_.contains(hp))
QToolTip::showText(view_.mapToGlobal(hp), format_string());
else
QToolTip::hideText(); // TODO Will break other tooltips when there can be others
}
QString CursorPair::format_string_sub(int time_precision, int freq_precision, bool show_unit)
{
QString s = " ";
const pv::util::SIPrefix prefix = view_.tick_prefix();
const pv::util::Timestamp diff = abs(second_->time() - first_->time());
const QString time = Ruler::format_time_with_distance(
diff, diff, prefix, (show_unit ? view_.time_unit() : pv::util::TimeUnit::None),
time_precision, false);
// We can only show a frequency when there's a time base
if (view_.time_unit() == pv::util::TimeUnit::Time) {
int items = 0;
if (show_frequency_) {
const QString freq = util::format_value_si(
1 / diff.convert_to<double>(), pv::util::SIPrefix::unspecified,
freq_precision, (show_unit ? "Hz" : nullptr), false);
s = QString("%1").arg(freq);
items++;
}
if (show_interval_) {
if (items > 0)
s = QString("%1 / %2").arg(s, time);
else
s = QString("%1").arg(time);
items++;
}
if (show_samples_) {
const QString samples = QString::number(
(diff * view_.session().get_samplerate()).convert_to<uint64_t>());
if (items > 0)
s = QString("%1 / %2").arg(s, samples);
else
s = QString("%1").arg(samples);
}
} else
// In this case, we return the number of samples, really
s = time;
return s;
}
} // namespace trace
} // namespace views
} // namespace pv
|