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.

3.0 KB
Raw Blame
// PendingOrdersBadge.cpp — status-bar pending-approvals indicator.
#include "screens/action_center/PendingOrdersBadge.h"

#include "screens/action_center/PendingOrdersPanel.h"
#include "trading/ActionCenter.h"
#include "ui/theme/Theme.h"

#include <QMouseEvent>
#include <QStyle>

namespace fincept::screens {

using trading::ActionCenter;
using trading::PendingOrder;

PendingOrdersBadge::PendingOrdersBadge(QWidget* parent) : QLabel(parent) {
    setObjectName("pendingOrdersBadge");
    setCursor(Qt::PointingHandCursor);
    setToolTip(tr("Orders awaiting approval — click to review"));
    // Two looks from ONE stylesheet: amber pill while orders wait for approval,
    // a muted outline when the queue is empty (the badge stays visible so the
    // Action Center — and its kill switch — is always one click away).
    setStyleSheet(QString("#pendingOrdersBadge{color:#000;background:%1;font-weight:700;"
                          "padding:1px 8px;border-radius:8px;}"
                          "#pendingOrdersBadge[idle=\"true\"]{color:%2;background:transparent;font-weight:400;"
                          "border:1px solid %3;}")
                      .arg(ui::colors::AMBER(), ui::colors::TEXT_SECONDARY(), ui::colors::BORDER()));

    auto& ac = ActionCenter::instance();
    connect(&ac, &ActionCenter::pending_order_created, this, [this](const PendingOrder&) { refresh_count(); });
    connect(&ac, &ActionCenter::order_approved, this, [this](const QString&, const QString&) { refresh_count(); });
    connect(&ac, &ActionCenter::order_rejected, this, [this](const QString&, const QString&) { refresh_count(); });
    connect(&ac, &ActionCenter::stats_updated, this, [this](const QString&) { refresh_count(); });

    refresh_count();
}

void PendingOrdersBadge::refresh_count() {
    // get_stats() loads and JSON-parses EVERY order ever queued (limit 100000) just to count
    // the pending ones; this runs on each ActionCenter signal on the UI thread. The pending
    // query is WHERE-filtered, so it only touches the handful of rows that matter.
    count_ = int(ActionCenter::instance().get_pending_orders().size());
    setText(QString::fromUtf8("\xe2\x8f\xb8 %1").arg(count_)); // ⏸ N
    const bool idle = (count_ == 0);
    if (property("idle").toBool() != idle || !property("idle").isValid()) {
        setProperty("idle", idle);
        style()->unpolish(this);
        style()->polish(this);
    }
    setToolTip(idle ? tr("Action Center — no orders awaiting approval. Click for history and the kill switch.")
                    : tr("%1 order(s) awaiting approval — click to review").arg(count_));
    setVisible(true);
}

void PendingOrdersBadge::mousePressEvent(QMouseEvent*) {
    if (!panel_)
        panel_ = new PendingOrdersPanel(window());
    if (panel_->isVisible()) {
        panel_->hide();
        return;
    }
    // Anchor the popover's bottom-right corner just above the badge.
    const QPoint anchor = mapToGlobal(QPoint(width(), 0));
    panel_->popup_at(anchor);
}

} // namespace fincept::screens