Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

1.7 KB
Raw Blame
#pragma once

#include "services/prediction/PredictionTypes.h"

#include <QEvent>
#include <QLabel>
#include <QMutex>
#include <QPixmap>
#include <QTimer>
#include <QWidget>

namespace fincept::screens::polymarket {

/// Custom-painted order book with depth visualization.
/// Caches to QPixmap, max 20fps repaint via coalescing timer (P9).
class PolymarketOrderBook : public QWidget {
    Q_OBJECT
  public:
    explicit PolymarketOrderBook(QWidget* parent = nullptr);

    void set_data(const fincept::services::prediction::PredictionOrderBook& book);
    void clear();

  signals:
    void price_clicked(double price);

  protected:
    void paintEvent(QPaintEvent* event) override;
    void resizeEvent(QResizeEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void showEvent(QShowEvent* event) override;
    void hideEvent(QHideEvent* event) override;
    void changeEvent(QEvent* event) override;

  private:
    void rebuild_cache();
    // Number of ask/bid levels actually painted, derived from the current widget
    // height. Both the painter and the click hit-test MUST use this so a click
    // maps to the same level that was drawn. Caller must hold mutex_.
    void visible_row_counts(int& ask_rows, int& bid_rows) const;

    QVector<fincept::services::prediction::OrderLevel> bids_;
    QVector<fincept::services::prediction::OrderLevel> asks_;
    double spread_ = 0.0;
    int price_decimals_ = 2; // follows the book's tick size (0.001-tick markets need 3 dp)
    QMutex mutex_;
    QPixmap cache_;
    bool cache_dirty_ = true;
    QTimer* repaint_timer_ = nullptr;

    static constexpr int ROW_HEIGHT = 20;
    static constexpr int HEADER_HEIGHT = 28;
};

} // namespace fincept::screens::polymarket