Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / properties / speedwidget.cpp
blob417a9e6d04135255579b869370240e533a8cb9e3
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Anton Lashkov <lenton_91@mail.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "speedwidget.h"
31 #include <QDateTime>
32 #include <QHBoxLayout>
33 #include <QLabel>
34 #include <QMenu>
35 #include <QVBoxLayout>
37 #include "base/bittorrent/session.h"
38 #include "base/bittorrent/sessionstatus.h"
39 #include "base/preferences.h"
40 #include "propertieswidget.h"
41 #include "speedplotview.h"
43 ComboBoxMenuButton::ComboBoxMenuButton(QWidget *parent, QMenu *menu)
44 : QComboBox(parent)
45 , m_menu(menu)
49 void ComboBoxMenuButton::showPopup()
51 const QPoint p = mapToGlobal(QPoint(0, height()));
52 m_menu->popup(p);
54 QComboBox::hidePopup();
57 SpeedWidget::SpeedWidget(PropertiesWidget *parent)
58 : QWidget(parent)
60 m_layout = new QVBoxLayout(this);
61 m_layout->setContentsMargins(0, 0, 0, 0);
62 m_layout->setSpacing(3);
64 m_hlayout = new QHBoxLayout();
65 m_hlayout->setContentsMargins(0, 0, 0, 0);
67 m_periodLabel = new QLabel(u"<b>" + tr("Period:") + u"</b>");
69 m_periodCombobox = new QComboBox();
70 m_periodCombobox->addItem(tr("1 Minute"));
71 m_periodCombobox->addItem(tr("5 Minutes"));
72 m_periodCombobox->addItem(tr("30 Minutes"));
73 m_periodCombobox->addItem(tr("3 Hours"));
74 m_periodCombobox->addItem(tr("6 Hours"));
75 m_periodCombobox->addItem(tr("12 Hours"));
76 m_periodCombobox->addItem(tr("24 Hours"));
78 connect(m_periodCombobox, qOverload<int>(&QComboBox::currentIndexChanged)
79 , this, &SpeedWidget::onPeriodChange);
81 m_graphsMenu = new QMenu(this);
82 m_graphsMenu->addAction(tr("Total Upload"));
83 m_graphsMenu->addAction(tr("Total Download"));
84 m_graphsMenu->addAction(tr("Payload Upload"));
85 m_graphsMenu->addAction(tr("Payload Download"));
86 m_graphsMenu->addAction(tr("Overhead Upload"));
87 m_graphsMenu->addAction(tr("Overhead Download"));
88 m_graphsMenu->addAction(tr("DHT Upload"));
89 m_graphsMenu->addAction(tr("DHT Download"));
90 m_graphsMenu->addAction(tr("Tracker Upload"));
91 m_graphsMenu->addAction(tr("Tracker Download"));
93 m_graphsMenuActions = m_graphsMenu->actions();
95 for (int id = SpeedPlotView::UP; id < SpeedPlotView::NB_GRAPHS; ++id)
97 QAction *action = m_graphsMenuActions.at(id);
98 action->setCheckable(true);
99 action->setChecked(true);
100 connect(action, &QAction::changed, this, [this, id]() { onGraphChange(id); });
103 m_graphsButton = new ComboBoxMenuButton(this, m_graphsMenu);
104 m_graphsButton->addItem(tr("Select Graphs"));
106 m_hlayout->addWidget(m_periodLabel);
107 m_hlayout->addWidget(m_periodCombobox);
108 m_hlayout->addStretch();
109 m_hlayout->addWidget(m_graphsButton);
111 m_plot = new SpeedPlotView(this);
112 connect(BitTorrent::Session::instance(), &BitTorrent::Session::statsUpdated, this, &SpeedWidget::update);
114 m_layout->addLayout(m_hlayout);
115 m_layout->addWidget(m_plot);
117 loadSettings();
119 m_plot->show();
122 SpeedWidget::~SpeedWidget()
124 qDebug("SpeedWidget::~SpeedWidget() ENTER");
125 saveSettings();
126 qDebug("SpeedWidget::~SpeedWidget() EXIT");
129 void SpeedWidget::update()
131 const BitTorrent::SessionStatus &btStatus = BitTorrent::Session::instance()->status();
133 SpeedPlotView::SampleData sampleData;
134 sampleData[SpeedPlotView::UP] = btStatus.uploadRate;
135 sampleData[SpeedPlotView::DOWN] = btStatus.downloadRate;
136 sampleData[SpeedPlotView::PAYLOAD_UP] = btStatus.payloadUploadRate;
137 sampleData[SpeedPlotView::PAYLOAD_DOWN] = btStatus.payloadDownloadRate;
138 sampleData[SpeedPlotView::OVERHEAD_UP] = btStatus.ipOverheadUploadRate;
139 sampleData[SpeedPlotView::OVERHEAD_DOWN] = btStatus.ipOverheadDownloadRate;
140 sampleData[SpeedPlotView::DHT_UP] = btStatus.dhtUploadRate;
141 sampleData[SpeedPlotView::DHT_DOWN] = btStatus.dhtDownloadRate;
142 sampleData[SpeedPlotView::TRACKER_UP] = btStatus.trackerUploadRate;
143 sampleData[SpeedPlotView::TRACKER_DOWN] = btStatus.trackerDownloadRate;
145 m_plot->pushPoint(sampleData);
148 void SpeedWidget::onPeriodChange(int period)
150 m_plot->setPeriod(static_cast<SpeedPlotView::TimePeriod>(period));
153 void SpeedWidget::onGraphChange(int id)
155 QAction *action = m_graphsMenuActions.at(id);
156 m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), action->isChecked());
159 void SpeedWidget::loadSettings()
161 Preferences *preferences = Preferences::instance();
163 int periodIndex = preferences->getSpeedWidgetPeriod();
164 m_periodCombobox->setCurrentIndex(periodIndex);
165 onPeriodChange(static_cast<SpeedPlotView::TimePeriod>(periodIndex));
167 for (int id = SpeedPlotView::UP; id < SpeedPlotView::NB_GRAPHS; ++id)
169 QAction *action = m_graphsMenuActions.at(id);
170 bool enable = preferences->getSpeedWidgetGraphEnable(id);
172 action->setChecked(enable);
173 m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), enable);
177 void SpeedWidget::saveSettings() const
179 Preferences *preferences = Preferences::instance();
181 preferences->setSpeedWidgetPeriod(m_periodCombobox->currentIndex());
183 for (int id = SpeedPlotView::UP; id < SpeedPlotView::NB_GRAPHS; ++id)
185 QAction *action = m_graphsMenuActions.at(id);
186 preferences->setSpeedWidgetGraphEnable(id, action->isChecked());