+namespace views {
+namespace TraceView {
+
+const QColor AnalogSignal::SignalColours[4] = {
+ QColor(0xC4, 0xA0, 0x00), // Yellow
+ QColor(0x87, 0x20, 0x7A), // Magenta
+ QColor(0x20, 0x4A, 0x87), // Blue
+ QColor(0x4E, 0x9A, 0x06) // Green
+};
+
+const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
+const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
+
+const QColor AnalogSignal::SamplingPointColour(0x77, 0x77, 0x77);
+
+const float AnalogSignal::EnvelopeThreshold = 256.0f;
+
+const int AnalogSignal::MaximumVDivs = 10;
+const int AnalogSignal::MinScaleIndex = -6;
+const int AnalogSignal::MaxScaleIndex = 7;
+
+const int AnalogSignal::InfoTextMarginRight = 20;
+const int AnalogSignal::InfoTextMarginBottom = 5;
+
+AnalogSignal::AnalogSignal(
+ pv::Session &session,
+ shared_ptr<data::SignalBase> base) :
+ Signal(session, base),
+ scale_index_(4), // 20 per div
+ scale_index_drag_offset_(0),
+ div_height_(3 * QFontMetrics(QApplication::font()).height()),
+ pos_vdivs_(1),
+ neg_vdivs_(1),
+ resolution_(0),
+ autoranging_(true)
+{
+ pv::data::Analog* analog_data =
+ dynamic_cast<pv::data::Analog*>(data().get());
+
+ connect(analog_data, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
+ this, SLOT(on_samples_added()));
+
+ base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
+ update_scale();
+}
+
+shared_ptr<pv::data::SignalData> AnalogSignal::data() const
+{
+ return base_->analog_data();
+}
+
+void AnalogSignal::save_settings(QSettings &settings) const
+{
+ settings.setValue("pos_vdivs", pos_vdivs_);
+ settings.setValue("neg_vdivs", neg_vdivs_);
+ settings.setValue("scale_index", scale_index_);
+ settings.setValue("autoranging", autoranging_);
+}
+
+void AnalogSignal::restore_settings(QSettings &settings)
+{
+ if (settings.contains("pos_vdivs"))
+ pos_vdivs_ = settings.value("pos_vdivs").toInt();
+
+ if (settings.contains("neg_vdivs"))
+ neg_vdivs_ = settings.value("neg_vdivs").toInt();
+
+ if (settings.contains("scale_index")) {
+ scale_index_ = settings.value("scale_index").toInt();
+ update_scale();
+ }
+
+ if (settings.contains("autoranging"))
+ autoranging_ = settings.value("autoranging").toBool();
+}
+
+pair<int, int> AnalogSignal::v_extents() const
+{
+ const int ph = pos_vdivs_ * div_height_;
+ const int nh = neg_vdivs_ * div_height_;
+ return make_pair(-ph, nh);
+}
+
+int AnalogSignal::scale_handle_offset() const
+{
+ const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
+
+ return ((scale_index_drag_offset_ - scale_index_) *
+ h / 4) - h / 2;
+}
+
+void AnalogSignal::scale_handle_dragged(int offset)
+{
+ const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
+
+ scale_index_ = scale_index_drag_offset_ -
+ (offset + h / 2) / (h / 4);
+
+ update_scale();
+}
+
+void AnalogSignal::scale_handle_drag_release()
+{
+ scale_index_drag_offset_ = scale_index_;
+ update_scale();
+}
+
+void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp)
+{
+ if (base_->enabled()) {
+ Trace::paint_back(p, pp);
+ paint_axis(p, pp, get_visual_y());
+ }
+}
+
+void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
+{
+ assert(base_->analog_data());
+ assert(owner_);
+
+ const int y = get_visual_y();
+
+ if (!base_->enabled())
+ return;
+
+ paint_grid(p, y, pp.left(), pp.right());
+
+ const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
+ base_->analog_data()->analog_segments();
+ if (segments.empty())
+ return;
+
+ const shared_ptr<pv::data::AnalogSegment> &segment =
+ segments.front();
+
+ const double pixels_offset = pp.pixels_offset();
+ const double samplerate = max(1.0, segment->samplerate());
+ const pv::util::Timestamp& start_time = segment->start_time();
+ const int64_t last_sample = segment->get_sample_count() - 1;
+ const double samples_per_pixel = samplerate * pp.scale();
+ const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
+ const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
+
+ const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
+ (int64_t)0), last_sample);
+ const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
+ (int64_t)0), last_sample);
+
+ if (samples_per_pixel < EnvelopeThreshold)
+ paint_trace(p, segment, y, pp.left(),
+ start_sample, end_sample,
+ pixels_offset, samples_per_pixel);
+ else
+ paint_envelope(p, segment, y, pp.left(),
+ start_sample, end_sample,
+ pixels_offset, samples_per_pixel);
+}
+
+void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
+{
+ if (!enabled())
+ return;
+
+ const int y = get_visual_y();
+
+ // Show the info section on the right side of the trace
+ const QString infotext = QString("%1 V/div").arg(resolution_);
+
+ p.setPen(base_->colour());
+ p.setFont(QApplication::font());
+
+ const QRectF bounding_rect = QRectF(pp.left(),
+ y + v_extents().first,
+ pp.width() - InfoTextMarginRight,
+ v_extents().second - v_extents().first - InfoTextMarginBottom);
+
+ p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
+}
+
+void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
+{
+ p.setRenderHint(QPainter::Antialiasing, false);
+
+ if (pos_vdivs_ > 0) {
+ p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
+ for (int i = 1; i <= pos_vdivs_; i++) {
+ const float dy = i * div_height_;
+ p.drawLine(QLineF(left, y - dy, right, y - dy));
+ }
+
+ p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
+ for (int i = 0; i < pos_vdivs_; i++) {
+ const float dy = i * div_height_;
+ const float dy25 = dy + (0.25 * div_height_);
+ const float dy50 = dy + (0.50 * div_height_);
+ const float dy75 = dy + (0.75 * div_height_);
+ p.drawLine(QLineF(left, y - dy25, right, y - dy25));
+ p.drawLine(QLineF(left, y - dy50, right, y - dy50));
+ p.drawLine(QLineF(left, y - dy75, right, y - dy75));
+ }
+ }
+
+ if (neg_vdivs_ > 0) {
+ p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
+ for (int i = 1; i <= neg_vdivs_; i++) {
+ const float dy = i * div_height_;
+ p.drawLine(QLineF(left, y + dy, right, y + dy));
+ }
+
+ p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
+ for (int i = 0; i < neg_vdivs_; i++) {
+ const float dy = i * div_height_;
+ const float dy25 = dy + (0.25 * div_height_);
+ const float dy50 = dy + (0.50 * div_height_);
+ const float dy75 = dy + (0.75 * div_height_);
+ p.drawLine(QLineF(left, y + dy25, right, y + dy25));
+ p.drawLine(QLineF(left, y + dy50, right, y + dy50));
+ p.drawLine(QLineF(left, y + dy75, right, y + dy75));
+ }
+ }
+
+ p.setRenderHint(QPainter::Antialiasing, true);
+}
+
+void AnalogSignal::paint_trace(QPainter &p,
+ const shared_ptr<pv::data::AnalogSegment> &segment,
+ int y, int left, const int64_t start, const int64_t end,
+ const double pixels_offset, const double samples_per_pixel)
+{
+ p.setPen(base_->colour());
+
+ const int64_t points_count = end - start;
+
+ QPointF *points = new QPointF[points_count];
+ QPointF *point = points;