Browse Source

Fixup speedwidget code.

adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
0b20794672
  1. 67
      src/gui/properties/speedplotview.cpp
  2. 52
      src/gui/properties/speedplotview.h
  3. 35
      src/gui/properties/speedwidget.cpp
  4. 1
      src/gui/properties/speedwidget.h

67
src/gui/properties/speedplotview.cpp

@ -68,9 +68,9 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) @@ -68,9 +68,9 @@ SpeedPlotView::SpeedPlotView(QWidget *parent)
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)
@ -81,7 +81,7 @@ void SpeedPlotView::pushXPoint(double x) @@ -81,7 +81,7 @@ void SpeedPlotView::pushXPoint(double 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)
m_yData[id].pop_front();
@ -89,9 +89,24 @@ void SpeedPlotView::pushYPoint(int id, double y) @@ -89,9 +89,24 @@ void SpeedPlotView::pushYPoint(int id, double 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()
@ -102,9 +117,9 @@ void SpeedPlotView::replot() @@ -102,9 +117,9 @@ void SpeedPlotView::replot()
double SpeedPlotView::maxYValue()
{
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;
QQueue<double> &queue = m_yData[it.key()];
@ -182,9 +197,9 @@ void SpeedPlotView::paintEvent(QPaintEvent *) @@ -182,9 +197,9 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
double y_multiplier = rect.height() / max_y;
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;
QQueue<double> &queue = m_yData[it.key()];
@ -195,7 +210,7 @@ void SpeedPlotView::paintEvent(QPaintEvent *) @@ -195,7 +210,7 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
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());
}
@ -204,15 +219,13 @@ void SpeedPlotView::paintEvent(QPaintEvent *) @@ -204,15 +219,13 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
double legend_height = 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;
if (font_metrics.width(it.value().name) > legend_width)
{
legend_width = font_metrics.width(it.value().name);
}
if (font_metrics.width(it.value().m_name) > legend_width)
legend_width = font_metrics.width(it.value().m_name);
legend_height += 1.5 * font_metrics.height();
}
@ -220,34 +233,32 @@ void SpeedPlotView::paintEvent(QPaintEvent *) @@ -220,34 +233,32 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
painter.fillRect(legend_background_rect, QColor(255, 255, 255, 128)); // 50% transparent
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;
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();
painter.setPen(it.value().pen);
painter.setPen(it.value().m_pen);
painter.drawLine(legend_top_left + QPointF(0, 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())),
it.value().name, QTextOption(Qt::AlignVCenter));
it.value().m_name, QTextOption(Qt::AlignVCenter));
++i;
}
}
SpeedPlotView::GraphProperties::GraphProperties()
: name()
, pen()
, enable(false)
: m_enable(false)
{
}
SpeedPlotView::GraphProperties::GraphProperties(const QString &_name, const QPen &_pen, bool _enable)
: name(_name)
, pen(_pen)
, enable(_enable)
SpeedPlotView::GraphProperties::GraphProperties(const QString &name, const QPen &pen, bool enable)
: m_name(name)
, m_pen(pen)
, m_enable(enable)
{
}

52
src/gui/properties/speedplotview.h

@ -38,17 +38,6 @@ class SpeedPlotView : public QGraphicsView @@ -38,17 +38,6 @@ class SpeedPlotView : public QGraphicsView
{
Q_OBJECT
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
{
UP = 0,
@ -72,34 +61,45 @@ public: @@ -72,34 +61,45 @@ public:
HOUR6
};
enum PeriodInSeconds
{
MIN1_SEC = 60,
MIN5_SEC = 300,
MIN30_SEC = 1800,
HOUR6_SEC = 21600
};
explicit SpeedPlotView(QWidget *parent = 0);
void setGraphEnable(GraphID id, bool enable);
void pushXPoint(double x);
void pushYPoint(GraphID id, double y);
void setViewableLastPoints(TimePeriod period);
void replot();
protected:
virtual void paintEvent(QPaintEvent *event);
private:
enum PeriodInSeconds
{
MIN1_SEC = 60,
MIN5_SEC = 5 * 60,
MIN30_SEC = 30 * 60,
HOUR6_SEC = 6 * 60 * 60
};
struct GraphProperties
{
GraphProperties();
GraphProperties(const QString &_name, const QPen &_pen, bool _enable = false);
GraphProperties(const QString &name, const QPen &pen, bool enable = false);
QString name;
QPen pen;
bool enable;
QString m_name;
QPen m_pen;
bool m_enable;
};
QQueue<double> m_xData;
QMap<int, QQueue<double> > m_yData;
QMap<int, GraphProperties> m_properties;
QMap<GraphID, QQueue<double> > m_yData;
QMap<GraphID, GraphProperties> m_properties;
int m_viewablePointsCount;
int m_maxCapacity;
PeriodInSeconds m_viewablePointsCount;
PeriodInSeconds m_maxCapacity;
double maxYValue();
};

35
src/gui/properties/speedwidget.cpp

@ -46,7 +46,6 @@ @@ -46,7 +46,6 @@
SpeedWidget::SpeedWidget(PropertiesWidget *parent)
: QWidget(parent)
, m_properties(parent)
{
m_layout = new QVBoxLayout(this);
m_layout->setContentsMargins(0, 0, 0, 0);
@ -143,7 +142,7 @@ void SpeedWidget::update() @@ -143,7 +142,7 @@ void SpeedWidget::update()
m_plot->pushYPoint(SpeedPlotView::TRACKER_UP, btStatus.trackerUploadRate());
m_plot->pushYPoint(SpeedPlotView::TRACKER_DOWN, btStatus.trackerDownloadRate());
QMetaObject::invokeMethod(this, "graphUpdate");
QMetaObject::invokeMethod(this, "graphUpdate", Qt::QueuedConnection);
Utils::Misc::msleep(1000);
}
}
@ -155,30 +154,14 @@ void SpeedWidget::graphUpdate() @@ -155,30 +154,14 @@ void SpeedWidget::graphUpdate()
void SpeedWidget::onPeriodChange(int period)
{
switch (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;
}
m_plot->setViewableLastPoints(static_cast<SpeedPlotView::TimePeriod>(period));
graphUpdate();
}
void SpeedWidget::onGraphChange(int id)
{
QAction *action = m_graphsMenuActions.at(id);
m_plot->setGraphEnable(id, action->isChecked());
m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), action->isChecked());
graphUpdate();
}
@ -189,18 +172,14 @@ void SpeedWidget::loadSettings() @@ -189,18 +172,14 @@ void SpeedWidget::loadSettings()
int periodIndex = preferences->getSpeedWidgetPeriod();
m_periodCombobox->setCurrentIndex(periodIndex);
onPeriodChange(periodIndex);
onPeriodChange(static_cast<SpeedPlotView::TimePeriod>(periodIndex));
for (int id = SpeedPlotView::UP; id < SpeedPlotView::NB_GRAPHS; ++id) {
QAction *action = m_graphsMenuActions.at(id);
bool enable = preferences->getSpeedWidgetGraphEnable(id);
if (preferences->getSpeedWidgetGraphEnable(id)) {
action->setChecked(true);
m_plot->setGraphEnable(id, true);
} else {
action->setChecked(false);
m_plot->setGraphEnable(id, false);
}
action->setChecked(enable);
m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), enable);
}
}

1
src/gui/properties/speedwidget.h

@ -65,7 +65,6 @@ private: @@ -65,7 +65,6 @@ private:
QLabel *m_periodLabel;
QComboBox *m_periodCombobox;
SpeedPlotView *m_plot;
PropertiesWidget *m_properties;
QToolButton *m_graphsButton;
QMenu *m_graphsMenu;

Loading…
Cancel
Save