summaryrefslogtreecommitdiff
path: root/src/statusbar.cpp
blob: 68c0642ae86d8f922a390b797cfe63bbbdc5067f (plain)
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
#include "statusbar.h"
#include "nexusinterface.h"
#include "settings.h"
#include "ui_mainwindow.h"

StatusBar::StatusBar(QStatusBar* bar, Ui::MainWindow* ui) :
  m_bar(bar), m_progress(new QProgressBar),
  m_notifications(new StatusBarAction(ui->actionNotifications)),
  m_update(new StatusBarAction(ui->actionUpdate)),
  m_api(new QLabel)
{
  m_bar->addPermanentWidget(m_progress);
  m_bar->addPermanentWidget(m_notifications);
  m_bar->addPermanentWidget(m_update);
  m_bar->addPermanentWidget(m_api);

  m_progress->setTextVisible(true);
  m_progress->setRange(0, 100);
  m_progress->setMaximumWidth(150);
  m_progress->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);

  m_update->set(false);
  m_notifications->set(false);

  m_api->setObjectName("apistats");
  m_api->setStyleSheet("QLabel{ padding: 0.1em 0 0.1em 0; }");

  m_bar->clearMessage();
  setProgress(-1);
  setAPI({}, {});
}

void StatusBar::setProgress(int percent)
{
  if (percent < 0 || percent >= 100) {
    m_bar->clearMessage();
    m_progress->setVisible(false);
  } else {
    m_bar->showMessage(QObject::tr("Loading..."));
    m_progress->setVisible(true);
    m_progress->setValue(percent);
  }
}

void StatusBar::setNotifications(bool hasNotifications)
{
  m_notifications->set(hasNotifications);
}

void StatusBar::setAPI(const APIStats& stats, const APIUserAccount& user)
{
  m_api->setText(
    QString("API: Q: %1 | D: %2 | H: %3")
    .arg(stats.requestsQueued)
    .arg(user.limits().remainingDailyRequests)
    .arg(user.limits().remainingHourlyRequests));

  QColor textColor;
  QColor backgroundColor;

  if (user.type() == APIUserAccountTypes::None) {
    backgroundColor = Qt::transparent;
  } else if (user.remainingRequests() > 300) {
    textColor = "white";
    backgroundColor = Qt::darkGreen;
  } else if (user.remainingRequests() < 150) {
    textColor = "white";
    backgroundColor = Qt::darkRed;
  } else {
    textColor = "black";
    backgroundColor = Qt::darkYellow;
  }

  QPalette palette = m_api->palette();

  palette.setColor(QPalette::WindowText, textColor);
  palette.setColor(QPalette::Background, backgroundColor);

  m_api->setPalette(palette);
  m_api->setAutoFillBackground(true);
}

void StatusBar::setUpdateAvailable(bool b)
{
  m_update->set(b);
}

void StatusBar::checkSettings(const Settings& settings)
{
  m_api->setVisible(!settings.hideAPICounter());
}


StatusBarAction::StatusBarAction(QAction* action)
  : m_action(action), m_icon(new QLabel), m_text(new QLabel)
{
  setLayout(new QHBoxLayout);
  layout()->setContentsMargins(0, 0, 0, 0);
  layout()->addWidget(m_icon);
  layout()->addWidget(m_text);
}

void StatusBarAction::set(bool visible)
{
  if (visible) {
    m_icon->setPixmap(m_action->icon().pixmap(16, 16));
    m_text->setText(cleanupActionText(m_action->text()));
  }

  setVisible(visible);
}

void StatusBarAction::mouseDoubleClickEvent(QMouseEvent* e)
{
  if (m_action->isEnabled()) {
    m_action->trigger();
  }
}

QString StatusBarAction::cleanupActionText(const QString& original) const
{
  QString s = original;

  s.replace(QRegExp("\\&([^&])"), "\\1");  // &Item -> Item
  s.replace("&&", "&"); // &&Item -> &Item

  if (s.endsWith("...")) {
    s = s.left(s.size() - 3);
  }

  return s;
}