Applets
net.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "net.h"
00020 #include <Plasma/SignalPlotter>
00021 #include <Plasma/Theme>
00022 #include <KConfigDialog>
00023 #include <QTimer>
00024 #include <QGraphicsLinearLayout>
00025
00026 SM::Net::Net(QObject *parent, const QVariantList &args)
00027 : SM::Applet(parent, args)
00028 {
00029 setHasConfigurationInterface(true);
00030 resize(234 + 20 + 23, 135 + 20 + 25);
00031 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00032 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
00033 }
00034
00035 SM::Net::~Net()
00036 {
00037 }
00038
00039 void SM::Net::init()
00040 {
00041 KConfigGroup cg = config();
00042 setEngine(dataEngine("systemmonitor"));
00043 setInterval(cg.readEntry("interval", 2) * 1000);
00044 setTitle(i18n("Network"));
00045 if (engine()->sources().count() == 0) {
00046 connect(engine(), SIGNAL(sourceAdded(QString)), this, SLOT(initLater(const QString)));
00047 } else {
00048 parseSources();
00049 }
00050 }
00051
00052 void SM::Net::parseSources()
00053 {
00054 QRegExp rx("network/interfaces/(\\w+)/transmitter/data");
00055
00056 foreach (const QString& s, engine()->sources()) {
00057 if (rx.indexIn(s) != -1) {
00058
00059 if (rx.cap(1) != "lo") {
00060 m_interfaces << s;
00061 }
00062 }
00063 }
00064 KConfigGroup cg = config();
00065 setItems(cg.readEntry("interfaces", m_interfaces));
00066 connectToEngine();
00067 }
00068
00069 void SM::Net::initLater(const QString &name)
00070 {
00071
00072 if (name == "ps") {
00073 QTimer::singleShot(0, this, SLOT(parseSources()));
00074 }
00075 }
00076
00077 bool SM::Net::addMeter(const QString& source)
00078 {
00079 QStringList l = source.split('/');
00080 if (l.count() < 3) {
00081 return false;
00082 }
00083 QString interface = l[2];
00084 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
00085 Plasma::SignalPlotter *plotter = new Plasma::SignalPlotter(this);
00086 QColor color = theme->color(Plasma::Theme::TextColor);
00087 plotter->addPlot(QColor(((color.red() - 128) * 0.65) + 128,
00088 ((color.green() - 128) * 0.65) + 128, 0, color.alpha()));
00089 plotter->addPlot(QColor(((color.red() - 128) * 0.90) + 128, 0, 0, color.alpha()));
00090 plotter->setUseAutoRange(true);
00091 plotter->setThinFrame(false);
00092 plotter->setShowLabels(false);
00093 plotter->setShowTopBar(false);
00094 plotter->setShowVerticalLines(false);
00095 plotter->setShowHorizontalLines(false);
00096 plotter->setStackPlots(true);
00097 plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
00098 QFont font = theme->font(Plasma::Theme::DefaultFont);
00099 font.setPointSize(8);
00100 plotter->setFont(font);
00101 plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00102 plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00103 plotter->setHorizontalLinesCount(4);
00104
00105 plotter->setTitle(interface);
00106 plotter->setUnit("KiB/s");
00107 appendPlotter(interface, plotter);
00108 mainLayout()->addItem(plotter);
00109 connectSource("network/interfaces/" + interface + "/receiver/data");
00110 setPreferredItemHeight(80);
00111 return true;
00112 }
00113
00114 void SM::Net::themeChanged()
00115 {
00116 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
00117 QColor color = theme->color(Plasma::Theme::TextColor);
00118 foreach (Plasma::SignalPlotter *plotter, plotters().values()) {
00119 plotter->removePlot(1);
00120 plotter->removePlot(0);
00121 plotter->addPlot(QColor(((color.red() - 128) * 0.65) + 128,
00122 ((color.green() - 128) * 0.65) + 128, 0, color.alpha()));
00123 plotter->addPlot(QColor(((color.red() - 128) * 0.90) + 128, 0, 0, color.alpha()));
00124 plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
00125 plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00126 plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00127 }
00128 }
00129
00130 void SM::Net::dataUpdated(const QString& source,
00131 const Plasma::DataEngine::Data &data)
00132 {
00133 QString interface = source.split('/')[2];
00134
00135 m_data[interface] << data["value"].toDouble();
00136 if (m_data[interface].count() > 1) {
00137 Plasma::SignalPlotter *plotter = plotters()[interface];
00138 if (plotter) {
00139 plotter->addSample(m_data[interface]);
00140
00141 }
00142 m_data[interface].clear();
00143 }
00144 }
00145
00146 void SM::Net::createConfigurationInterface(KConfigDialog *parent)
00147 {
00148 QWidget *widget = new QWidget();
00149 ui.setupUi(widget);
00150 m_model.clear();
00151 m_model.setHorizontalHeaderLabels(QStringList() << i18n("Network interface"));
00152 QStandardItem *parentItem = m_model.invisibleRootItem();
00153
00154
00155 foreach (const QString& interface, m_interfaces) {
00156 QString ifname = interface.split('/')[2];
00157 QStandardItem *item1 = new QStandardItem(ifname);
00158 item1->setEditable(false);
00159 item1->setCheckable(true);
00160 item1->setData(interface);
00161 if (items().contains(interface)) {
00162 item1->setCheckState(Qt::Checked);
00163 }
00164 parentItem->appendRow(QList<QStandardItem *>() << item1);
00165 }
00166 ui.treeView->setModel(&m_model);
00167 ui.treeView->resizeColumnToContents(0);
00168 ui.intervalSpinBox->setValue(interval() / 1000);
00169
00170 parent->addPage(widget, i18n("Interfaces"), "network-workgroup");
00171 connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
00172 connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
00173 }
00174
00175 void SM::Net::configAccepted()
00176 {
00177 KConfigGroup cg = config();
00178 QStandardItem *parentItem = m_model.invisibleRootItem();
00179
00180 clearItems();
00181 for (int i = 0; i < parentItem->rowCount(); ++i) {
00182 QStandardItem *item = parentItem->child(i, 0);
00183 if (item) {
00184 if (item->checkState() == Qt::Checked) {
00185 appendItem(item->data().toString());
00186 }
00187 }
00188 }
00189 cg.writeEntry("interfaces", items());
00190
00191 uint interval = ui.intervalSpinBox->value();
00192 cg.writeEntry("interval", interval);
00193 interval *= 1000;
00194 setInterval(interval);
00195 emit configNeedsSaving();
00196 connectToEngine();
00197 }
00198
00199 void SM::Net::setDetail(Detail detail)
00200 {
00201 foreach (const QString& key, plotters().keys()) {
00202 plotters().value(key)->setShowLabels(detail == SM::Applet::High);
00203
00204
00205 plotters().value(key)->setShowHorizontalLines(detail == SM::Applet::High);
00206 }
00207 }
00208
00209 #include "net.moc"