mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-11 07:18:08 +00:00
Fixup speedwidget code.
This commit is contained in:
parent
8dd7014af6
commit
0b20794672
@ -68,9 +68,9 @@ SpeedPlotView::SpeedPlotView(QWidget *parent)
|
|||||||
m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen);
|
m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedPlotView::setGraphEnable(int id, bool enable)
|
void SpeedPlotView::setGraphEnable(GraphID id, bool enable)
|
||||||
{
|
{
|
||||||
m_properties[id].enable = enable;
|
m_properties[id].m_enable = enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedPlotView::pushXPoint(double x)
|
void SpeedPlotView::pushXPoint(double x)
|
||||||
@ -81,7 +81,7 @@ void SpeedPlotView::pushXPoint(double x)
|
|||||||
m_xData.append(x);
|
m_xData.append(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedPlotView::pushYPoint(int id, double y)
|
void SpeedPlotView::pushYPoint(GraphID id, double y)
|
||||||
{
|
{
|
||||||
while (m_yData[id].size() >= m_maxCapacity)
|
while (m_yData[id].size() >= m_maxCapacity)
|
||||||
m_yData[id].pop_front();
|
m_yData[id].pop_front();
|
||||||
@ -89,9 +89,24 @@ void SpeedPlotView::pushYPoint(int id, double y)
|
|||||||
m_yData[id].append(y);
|
m_yData[id].append(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedPlotView::setViewableLastPoints(int period)
|
void SpeedPlotView::setViewableLastPoints(TimePeriod period)
|
||||||
{
|
{
|
||||||
m_viewablePointsCount = period;
|
switch (period) {
|
||||||
|
case SpeedPlotView::MIN1:
|
||||||
|
m_viewablePointsCount = SpeedPlotView::MIN1_SEC;
|
||||||
|
break;
|
||||||
|
case SpeedPlotView::MIN5:
|
||||||
|
m_viewablePointsCount = SpeedPlotView::MIN5_SEC;
|
||||||
|
break;
|
||||||
|
case SpeedPlotView::MIN30:
|
||||||
|
m_viewablePointsCount = SpeedPlotView::MIN30_SEC;
|
||||||
|
break;
|
||||||
|
case SpeedPlotView::HOUR6:
|
||||||
|
m_viewablePointsCount = SpeedPlotView::HOUR6_SEC;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedPlotView::replot()
|
void SpeedPlotView::replot()
|
||||||
@ -102,9 +117,9 @@ void SpeedPlotView::replot()
|
|||||||
double SpeedPlotView::maxYValue()
|
double SpeedPlotView::maxYValue()
|
||||||
{
|
{
|
||||||
double maxYValue = 0;
|
double maxYValue = 0;
|
||||||
for (QMap<int, QQueue<double> >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) {
|
for (QMap<GraphID, QQueue<double> >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) {
|
||||||
|
|
||||||
if (!m_properties[it.key()].enable)
|
if (!m_properties[it.key()].m_enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QQueue<double> &queue = m_yData[it.key()];
|
QQueue<double> &queue = m_yData[it.key()];
|
||||||
@ -182,9 +197,9 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
double y_multiplier = rect.height() / max_y;
|
double y_multiplier = rect.height() / max_y;
|
||||||
double x_tick_size = double(rect.width()) / m_viewablePointsCount;
|
double x_tick_size = double(rect.width()) / m_viewablePointsCount;
|
||||||
|
|
||||||
for (QMap<int, QQueue<double> >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) {
|
for (QMap<GraphID, QQueue<double> >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) {
|
||||||
|
|
||||||
if (!m_properties[it.key()].enable)
|
if (!m_properties[it.key()].m_enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QQueue<double> &queue = m_yData[it.key()];
|
QQueue<double> &queue = m_yData[it.key()];
|
||||||
@ -195,7 +210,7 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
rect.bottom() - queue.at(i) * y_multiplier));
|
rect.bottom() - queue.at(i) * y_multiplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
painter.setPen(m_properties[it.key()].pen);
|
painter.setPen(m_properties[it.key()].m_pen);
|
||||||
painter.drawPolyline(points.data(), points.size());
|
painter.drawPolyline(points.data(), points.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,15 +219,13 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
|
|
||||||
double legend_height = 0;
|
double legend_height = 0;
|
||||||
int legend_width = 0;
|
int legend_width = 0;
|
||||||
for (QMap<int, GraphProperties>::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) {
|
for (QMap<GraphID, GraphProperties>::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) {
|
||||||
|
|
||||||
if (!it.value().enable)
|
if (!it.value().m_enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (font_metrics.width(it.value().name) > legend_width)
|
if (font_metrics.width(it.value().m_name) > legend_width)
|
||||||
{
|
legend_width = font_metrics.width(it.value().m_name);
|
||||||
legend_width = font_metrics.width(it.value().name);
|
|
||||||
}
|
|
||||||
legend_height += 1.5 * font_metrics.height();
|
legend_height += 1.5 * font_metrics.height();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,34 +233,32 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
painter.fillRect(legend_background_rect, QColor(255, 255, 255, 128)); // 50% transparent
|
painter.fillRect(legend_background_rect, QColor(255, 255, 255, 128)); // 50% transparent
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (QMap<int, GraphProperties>::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) {
|
for (QMap<GraphID, GraphProperties>::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) {
|
||||||
|
|
||||||
if (!it.value().enable)
|
if (!it.value().m_enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int name_size = font_metrics.width(it.value().name);
|
int name_size = font_metrics.width(it.value().m_name);
|
||||||
double indent = 1.5 * i * font_metrics.height();
|
double indent = 1.5 * i * font_metrics.height();
|
||||||
|
|
||||||
painter.setPen(it.value().pen);
|
painter.setPen(it.value().m_pen);
|
||||||
painter.drawLine(legend_top_left + QPointF(0, indent + font_metrics.height()),
|
painter.drawLine(legend_top_left + QPointF(0, indent + font_metrics.height()),
|
||||||
legend_top_left + QPointF(name_size, indent + font_metrics.height()));
|
legend_top_left + QPointF(name_size, indent + font_metrics.height()));
|
||||||
painter.drawText(QRectF(legend_top_left + QPointF(0, indent), QSizeF(2 * name_size, font_metrics.height())),
|
painter.drawText(QRectF(legend_top_left + QPointF(0, indent), QSizeF(2 * name_size, font_metrics.height())),
|
||||||
it.value().name, QTextOption(Qt::AlignVCenter));
|
it.value().m_name, QTextOption(Qt::AlignVCenter));
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SpeedPlotView::GraphProperties::GraphProperties()
|
SpeedPlotView::GraphProperties::GraphProperties()
|
||||||
: name()
|
: m_enable(false)
|
||||||
, pen()
|
|
||||||
, enable(false)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SpeedPlotView::GraphProperties::GraphProperties(const QString &_name, const QPen &_pen, bool _enable)
|
SpeedPlotView::GraphProperties::GraphProperties(const QString &name, const QPen &pen, bool enable)
|
||||||
: name(_name)
|
: m_name(name)
|
||||||
, pen(_pen)
|
, m_pen(pen)
|
||||||
, enable(_enable)
|
, m_enable(enable)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,17 +38,6 @@ class SpeedPlotView : public QGraphicsView
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit SpeedPlotView(QWidget *parent = 0);
|
|
||||||
|
|
||||||
void setGraphEnable(int id, bool enable);
|
|
||||||
|
|
||||||
void pushXPoint(double x);
|
|
||||||
void pushYPoint(int id, double y);
|
|
||||||
|
|
||||||
void setViewableLastPoints(int period);
|
|
||||||
|
|
||||||
void replot();
|
|
||||||
|
|
||||||
enum GraphID
|
enum GraphID
|
||||||
{
|
{
|
||||||
UP = 0,
|
UP = 0,
|
||||||
@ -72,34 +61,45 @@ public:
|
|||||||
HOUR6
|
HOUR6
|
||||||
};
|
};
|
||||||
|
|
||||||
enum PeriodInSeconds
|
explicit SpeedPlotView(QWidget *parent = 0);
|
||||||
{
|
|
||||||
MIN1_SEC = 60,
|
void setGraphEnable(GraphID id, bool enable);
|
||||||
MIN5_SEC = 300,
|
|
||||||
MIN30_SEC = 1800,
|
void pushXPoint(double x);
|
||||||
HOUR6_SEC = 21600
|
void pushYPoint(GraphID id, double y);
|
||||||
};
|
|
||||||
|
void setViewableLastPoints(TimePeriod period);
|
||||||
|
|
||||||
|
void replot();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void paintEvent(QPaintEvent *event);
|
virtual void paintEvent(QPaintEvent *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
enum PeriodInSeconds
|
||||||
|
{
|
||||||
|
MIN1_SEC = 60,
|
||||||
|
MIN5_SEC = 5 * 60,
|
||||||
|
MIN30_SEC = 30 * 60,
|
||||||
|
HOUR6_SEC = 6 * 60 * 60
|
||||||
|
};
|
||||||
|
|
||||||
struct GraphProperties
|
struct GraphProperties
|
||||||
{
|
{
|
||||||
GraphProperties();
|
GraphProperties();
|
||||||
GraphProperties(const QString &_name, const QPen &_pen, bool _enable = false);
|
GraphProperties(const QString &name, const QPen &pen, bool enable = false);
|
||||||
|
|
||||||
QString name;
|
QString m_name;
|
||||||
QPen pen;
|
QPen m_pen;
|
||||||
bool enable;
|
bool m_enable;
|
||||||
};
|
};
|
||||||
|
|
||||||
QQueue<double> m_xData;
|
QQueue<double> m_xData;
|
||||||
QMap<int, QQueue<double> > m_yData;
|
QMap<GraphID, QQueue<double> > m_yData;
|
||||||
QMap<int, GraphProperties> m_properties;
|
QMap<GraphID, GraphProperties> m_properties;
|
||||||
|
|
||||||
int m_viewablePointsCount;
|
PeriodInSeconds m_viewablePointsCount;
|
||||||
int m_maxCapacity;
|
PeriodInSeconds m_maxCapacity;
|
||||||
|
|
||||||
double maxYValue();
|
double maxYValue();
|
||||||
};
|
};
|
||||||
|
@ -46,7 +46,6 @@
|
|||||||
|
|
||||||
SpeedWidget::SpeedWidget(PropertiesWidget *parent)
|
SpeedWidget::SpeedWidget(PropertiesWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
, m_properties(parent)
|
|
||||||
{
|
{
|
||||||
m_layout = new QVBoxLayout(this);
|
m_layout = new QVBoxLayout(this);
|
||||||
m_layout->setContentsMargins(0, 0, 0, 0);
|
m_layout->setContentsMargins(0, 0, 0, 0);
|
||||||
@ -143,7 +142,7 @@ void SpeedWidget::update()
|
|||||||
m_plot->pushYPoint(SpeedPlotView::TRACKER_UP, btStatus.trackerUploadRate());
|
m_plot->pushYPoint(SpeedPlotView::TRACKER_UP, btStatus.trackerUploadRate());
|
||||||
m_plot->pushYPoint(SpeedPlotView::TRACKER_DOWN, btStatus.trackerDownloadRate());
|
m_plot->pushYPoint(SpeedPlotView::TRACKER_DOWN, btStatus.trackerDownloadRate());
|
||||||
|
|
||||||
QMetaObject::invokeMethod(this, "graphUpdate");
|
QMetaObject::invokeMethod(this, "graphUpdate", Qt::QueuedConnection);
|
||||||
Utils::Misc::msleep(1000);
|
Utils::Misc::msleep(1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,30 +154,14 @@ void SpeedWidget::graphUpdate()
|
|||||||
|
|
||||||
void SpeedWidget::onPeriodChange(int period)
|
void SpeedWidget::onPeriodChange(int period)
|
||||||
{
|
{
|
||||||
switch (period) {
|
m_plot->setViewableLastPoints(static_cast<SpeedPlotView::TimePeriod>(period));
|
||||||
case SpeedPlotView::MIN1:
|
|
||||||
m_plot->setViewableLastPoints(SpeedPlotView::MIN1_SEC);
|
|
||||||
break;
|
|
||||||
case SpeedPlotView::MIN5:
|
|
||||||
m_plot->setViewableLastPoints(SpeedPlotView::MIN5_SEC);
|
|
||||||
break;
|
|
||||||
case SpeedPlotView::MIN30:
|
|
||||||
m_plot->setViewableLastPoints(SpeedPlotView::MIN30_SEC);
|
|
||||||
break;
|
|
||||||
case SpeedPlotView::HOUR6:
|
|
||||||
m_plot->setViewableLastPoints(SpeedPlotView::HOUR6_SEC);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
graphUpdate();
|
graphUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedWidget::onGraphChange(int id)
|
void SpeedWidget::onGraphChange(int id)
|
||||||
{
|
{
|
||||||
QAction *action = m_graphsMenuActions.at(id);
|
QAction *action = m_graphsMenuActions.at(id);
|
||||||
m_plot->setGraphEnable(id, action->isChecked());
|
m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), action->isChecked());
|
||||||
|
|
||||||
graphUpdate();
|
graphUpdate();
|
||||||
}
|
}
|
||||||
@ -189,18 +172,14 @@ void SpeedWidget::loadSettings()
|
|||||||
|
|
||||||
int periodIndex = preferences->getSpeedWidgetPeriod();
|
int periodIndex = preferences->getSpeedWidgetPeriod();
|
||||||
m_periodCombobox->setCurrentIndex(periodIndex);
|
m_periodCombobox->setCurrentIndex(periodIndex);
|
||||||
onPeriodChange(periodIndex);
|
onPeriodChange(static_cast<SpeedPlotView::TimePeriod>(periodIndex));
|
||||||
|
|
||||||
for (int id = SpeedPlotView::UP; id < SpeedPlotView::NB_GRAPHS; ++id) {
|
for (int id = SpeedPlotView::UP; id < SpeedPlotView::NB_GRAPHS; ++id) {
|
||||||
QAction *action = m_graphsMenuActions.at(id);
|
QAction *action = m_graphsMenuActions.at(id);
|
||||||
|
bool enable = preferences->getSpeedWidgetGraphEnable(id);
|
||||||
|
|
||||||
if (preferences->getSpeedWidgetGraphEnable(id)) {
|
action->setChecked(enable);
|
||||||
action->setChecked(true);
|
m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), enable);
|
||||||
m_plot->setGraphEnable(id, true);
|
|
||||||
} else {
|
|
||||||
action->setChecked(false);
|
|
||||||
m_plot->setGraphEnable(id, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,6 @@ private:
|
|||||||
QLabel *m_periodLabel;
|
QLabel *m_periodLabel;
|
||||||
QComboBox *m_periodCombobox;
|
QComboBox *m_periodCombobox;
|
||||||
SpeedPlotView *m_plot;
|
SpeedPlotView *m_plot;
|
||||||
PropertiesWidget *m_properties;
|
|
||||||
|
|
||||||
QToolButton *m_graphsButton;
|
QToolButton *m_graphsButton;
|
||||||
QMenu *m_graphsMenu;
|
QMenu *m_graphsMenu;
|
||||||
|
Loading…
Reference in New Issue
Block a user