diff options
author | Juergen Beisert <jbe@pengutronix.de> | 2011-07-10 13:54:18 +0200 |
---|---|---|
committer | Juergen Beisert <jbe@pengutronix.de> | 2011-07-10 13:56:32 +0200 |
commit | bae1f7e51f5e1a3a5d2735e2b727ef6f4de66b42 (patch) | |
tree | 295843e319bc35b39eedeecdd50a6ed73e6a8e0b /local_src | |
parent | c3d8f75104d2190353971e30276bf15861479f13 (diff) | |
download | OSELAS.BSP-Pengutronix-Mini6410-bae1f7e51f5e1a3a5d2735e2b727ef6f4de66b42.tar.gz OSELAS.BSP-Pengutronix-Mini6410-bae1f7e51f5e1a3a5d2735e2b727ef6f4de66b42.tar.xz |
BSP: Add a small graphics demo
Signed-off-by: Juergen Beisert <jbe@pengutronix.de>
Diffstat (limited to 'local_src')
-rw-r--r-- | local_src/qt4-demo-2011.07.0/config.ui | 154 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/configdialog.cpp | 66 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/configdialog.h | 31 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/main.cpp | 43 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/mainwindow.ui | 90 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/plotdata.cpp | 55 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/plotdata.h | 36 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/plothandler.cpp | 194 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/plothandler.h | 49 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/qt4-demo-load | 16 | ||||
-rw-r--r-- | local_src/qt4-demo-2011.07.0/qt4-demo.pro | 15 |
11 files changed, 749 insertions, 0 deletions
diff --git a/local_src/qt4-demo-2011.07.0/config.ui b/local_src/qt4-demo-2011.07.0/config.ui new file mode 100644 index 0000000..bc5c272 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/config.ui @@ -0,0 +1,154 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>ConfigDialog</class> + <widget class="QDialog" name="ConfigDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>175</width> + <height>188</height> + </rect> + </property> + <property name="windowTitle"> + <string>Dialog</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="QCheckBox" name="user_checkBox"> + <property name="text"> + <string>User</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QPushButton" name="user_colorButton"> + <property name="text"> + <string>Color...</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QCheckBox" name="nice_checkBox"> + <property name="text"> + <string>Nice</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QPushButton" name="nice_colorButton"> + <property name="text"> + <string>Color...</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QCheckBox" name="system_checkBox"> + <property name="text"> + <string>System</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QPushButton" name="system_colorButton"> + <property name="text"> + <string>Color...</string> + </property> + </widget> + </item> + <item row="3" column="0"> + <widget class="QCheckBox" name="idle_checkBox"> + <property name="text"> + <string>Idle</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QPushButton" name="idle_colorButton"> + <property name="text"> + <string>Color...</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> + </item> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>4</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Close</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>accepted()</signal> + <receiver>ConfigDialog</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>248</x> + <y>254</y> + </hint> + <hint type="destinationlabel"> + <x>157</x> + <y>274</y> + </hint> + </hints> + </connection> + <connection> + <sender>buttonBox</sender> + <signal>rejected()</signal> + <receiver>ConfigDialog</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>316</x> + <y>260</y> + </hint> + <hint type="destinationlabel"> + <x>286</x> + <y>274</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/local_src/qt4-demo-2011.07.0/configdialog.cpp b/local_src/qt4-demo-2011.07.0/configdialog.cpp new file mode 100644 index 0000000..10e265e --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/configdialog.cpp @@ -0,0 +1,66 @@ + +#include <QtCore/QDebug> +#include <QtGui/QColorDialog> + +#include "configdialog.h" + +ConfigDialog::ConfigDialog(QWidget *parent) + : QDialog(parent) +{ + m_ui.setupUi(this); + connect(m_ui.user_checkBox, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int))); + connect(m_ui.nice_checkBox, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int))); + connect(m_ui.system_checkBox, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int))); + connect(m_ui.idle_checkBox, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int))); + m_ui.user_checkBox->setChecked(true); + m_ui.nice_checkBox->setChecked(false); + m_ui.system_checkBox->setChecked(true); + m_ui.idle_checkBox->setChecked(false); + + connect(m_ui.user_colorButton, SIGNAL(clicked()), this, SLOT(clicked())); + connect(m_ui.nice_colorButton, SIGNAL(clicked()), this, SLOT(clicked())); + connect(m_ui.system_colorButton, SIGNAL(clicked()), this, SLOT(clicked())); + connect(m_ui.idle_colorButton, SIGNAL(clicked()), this, SLOT(clicked())); + +} + +int ConfigDialog::getId(QObject *s) +{ + if (s == m_ui.user_checkBox) + return 0; + if (s == m_ui.nice_checkBox) + return 1; + if (s == m_ui.system_checkBox) + return 2; + if (s == m_ui.idle_checkBox) + return 3; + if (s == m_ui.user_colorButton) + return 0; + if (s == m_ui.nice_colorButton) + return 1; + if (s == m_ui.system_colorButton) + return 2; + if (s == m_ui.idle_colorButton) + return 3; + return -1; +} + +void ConfigDialog::stateChanged(int state) +{ + bool show = state == Qt::Checked; + int id = getId(sender()); + + if (id >= 0) + emit curveShow(id, show); +} + +void ConfigDialog::clicked() +{ + int id = getId(sender()); + QColor c = QColorDialog::getColor(); + + if ((id >= 0) && c.isValid()) + emit curveColor(id, c); +} + + diff --git a/local_src/qt4-demo-2011.07.0/configdialog.h b/local_src/qt4-demo-2011.07.0/configdialog.h new file mode 100644 index 0000000..8fc9b29 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/configdialog.h @@ -0,0 +1,31 @@ + +#ifndef CONFIG_DIALOG_H +#define CONFIG_DIALOG_H + +#include <QtGui/QColor> +#include <QtGui/QDialog> + +#include "ui_config.h" + +class ConfigDialog : public QDialog +{ +Q_OBJECT +public: + ConfigDialog(QWidget *parent = 0); + +signals: + void curveShow(int id, bool show); + void curveColor(int id, const QColor &color); + +private slots: + void stateChanged(int state); + void clicked(); + +private: + int getId(QObject *s); + + Ui_ConfigDialog m_ui; +}; + +#endif // CONFIG_DIALOG_H + diff --git a/local_src/qt4-demo-2011.07.0/main.cpp b/local_src/qt4-demo-2011.07.0/main.cpp new file mode 100644 index 0000000..d7dc180 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/main.cpp @@ -0,0 +1,43 @@ + +#include <QtGui/QApplication> +#include <QtGui/QWidget> +#include <QtGui/QFont> +#include <QtGui/QVBoxLayout> + +#include "plothandler.h" +#include "plotdata.h" +#include "configdialog.h" + +#include "ui_mainwindow.h" + +int main(int argc, char **argv) +{ + + QApplication app(argc, argv); + + QFont f = app.font(); + f.setPointSize(7); + app.setFont(f); + f.setPointSize(5); + app.setFont(f, "QwtPlot"); + + QMainWindow window; + Ui_MainWindow ui; + ConfigDialog config; + + ui.setupUi(&window); + + PlotHandler handler(ui.plot); + PlotData data; + QObject::connect(&data, SIGNAL(data(int, const QVector<int>&)), &handler, SLOT(append(int, const QVector<int>&))); + + QObject::connect(ui.action_Configure, SIGNAL(triggered(bool)), &config, SLOT(open())); + QObject::connect(ui.action_Quit, SIGNAL(triggered(bool)), &window, SLOT(close())); + QObject::connect(ui.action_Load, SIGNAL(triggered(bool)), &handler, SLOT(load(bool))); + QObject::connect(&config, SIGNAL(curveShow(int, bool)), &handler, SLOT(curveShow(int, bool))); + QObject::connect(&config, SIGNAL(curveColor(int, const QColor&)), &handler, SLOT(curveColor(int, const QColor&))); + + window.showFullScreen(); + return app.exec();; +} + diff --git a/local_src/qt4-demo-2011.07.0/mainwindow.ui b/local_src/qt4-demo-2011.07.0/mainwindow.ui new file mode 100644 index 0000000..1fafb5d --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/mainwindow.ui @@ -0,0 +1,90 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>480</width> + <height>272</height> + </rect> + </property> + <property name="minimumSize"> + <size> + <width>480</width> + <height>272</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>480</width> + <height>272</height> + </size> + </property> + <property name="windowTitle"> + <string>MainWindow</string> + </property> + <widget class="QWidget" name="centralwidget"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QwtPlot" name="plot"> + </widget> + </item> + </layout> + </widget> + <widget class="QMenuBar" name="menubar"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>480</width> + <height>27</height> + </rect> + </property> + <widget class="QMenu" name="menu_File"> + <property name="title"> + <string>&File</string> + </property> + <addaction name="action_Quit"/> + </widget> + <widget class="QMenu" name="menu_Settings"> + <property name="title"> + <string>&Settings</string> + </property> + <addaction name="action_Configure"/> + <addaction name="action_Load"/> + </widget> + <addaction name="menu_File"/> + <addaction name="menu_Settings"/> + </widget> + <action name="action_Quit"> + <property name="text"> + <string>&Quit</string> + </property> + </action> + <action name="action_Configure"> + <property name="text"> + <string>&Configure ...</string> + </property> + </action> + <action name="action_Load"> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="text"> + <string>&Load</string> + </property> + </action> + </widget> + <customwidgets> + <customwidget> + <class>QwtPlot</class> + <extends>QFrame</extends> + <header>qwt_plot.h</header> + <container>1</container> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui> diff --git a/local_src/qt4-demo-2011.07.0/plotdata.cpp b/local_src/qt4-demo-2011.07.0/plotdata.cpp new file mode 100644 index 0000000..6ef1bcf --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/plotdata.cpp @@ -0,0 +1,55 @@ + +#include <QtCore/QDateTime> +#include <QtCore/QFile> +#include <QtCore/QTextStream> +#include <QtCore/QStringList> +#include <QtCore/QDebug> + +#include "plotdata.h" + +PlotData::PlotData(QObject *parent) + : QObject(parent), m_old(nValue, 0) +{ + QVector<int> values(nValue); + getValues(values); + startTimer(1000); +} + +void PlotData::getValues(QVector<int> &values) +{ + int cpuCount = -1; + QFile file("/proc/stat"); + if (!file.open(QIODevice::ReadOnly)) + return; + QTextStream stream(&file); + do { + QString line = stream.readLine(); + const QStringList list = line.trimmed().split(" ", QString::SkipEmptyParts); + if (list[0].left(3) == "cpu") + ++cpuCount; + if ((list.count() < 6) || list[0] != "cpu") + continue; + + m_now = 0; + for (int i = 0; i < list.length()-1; ++i) { + int value = list[i+1].toInt(); + m_now += value; + if (i < nValue) { + values[i] = value - m_old[i]; + m_old[i] = value; + } + } + } while(!stream.atEnd()); + for (int i = 0; i < nValue; ++i) + values[i] /= cpuCount; + m_now /= cpuCount; +} + +void PlotData::timerEvent(QTimerEvent *) +{ + QVector<int> values(nValue); + + getValues(values); + emit data(m_now, values); +} + diff --git a/local_src/qt4-demo-2011.07.0/plotdata.h b/local_src/qt4-demo-2011.07.0/plotdata.h new file mode 100644 index 0000000..f2ebc38 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/plotdata.h @@ -0,0 +1,36 @@ + +#ifndef PLOT_DATA_H +#define PLOT_DATA_H + +#include <QtCore/QObject> +#include <QtCore/QVector> + +class PlotData : public QObject +{ +Q_OBJECT +public: + PlotData(QObject *parent = 0); + + enum Value { + User, + Nice, + System, + Idle, + + nValue + }; + +signals: + void data(int timestamp, const QVector<int> &values); + +protected: + virtual void timerEvent(QTimerEvent *); + +private: + void getValues(QVector<int> &values); + QVector<int> m_old; + int m_now; +}; + +#endif + diff --git a/local_src/qt4-demo-2011.07.0/plothandler.cpp b/local_src/qt4-demo-2011.07.0/plothandler.cpp new file mode 100644 index 0000000..283fd64 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/plothandler.cpp @@ -0,0 +1,194 @@ + +#include <QtCore/QTime> +#include <QtGui/QPainter> +#include <QtCore/QDebug> + +#include <qwt_plot.h> +#include <qwt_legend.h> +#include <qwt_plot_layout.h> +#include <qwt_plot_curve.h> +#include <qwt_scale_draw.h> +#include <qwt_scale_widget.h> + +#include "plothandler.h" + +#define SIZE 100 + +class TimeScaleDraw: public QwtScaleDraw +{ +public: + virtual QwtText label(double v) const + { + QTime time(0,0); + time = time.addSecs((int)v/100); + return time.toString(); + } +}; + +class Background: public QwtPlotItem +{ +public: + Background() + { + setZ(0.0); + } + + virtual int rtti() const + { + return QwtPlotItem::Rtti_PlotUserItem; + } + + virtual void draw(QPainter *painter, + const QwtScaleMap &, const QwtScaleMap &yMap, + const QRect &rect) const + { + QColor c(Qt::white); + QRect r = rect; + + for ( int i = 100; i > 0; i -= 10 ) + { + r.setBottom(yMap.transform(i - 10)); + r.setTop(yMap.transform(i)); + painter->fillRect(r, c); + + c = c.dark(110); + } + } +}; + +class PlotCurve: public QwtPlotCurve +{ +public: + PlotCurve(const QString &title) : QwtPlotCurve(title) + { + setRenderHint(QwtPlotItem::RenderAntialiased); + } + void setColor(const QColor &color) + { + QColor c = color; + c.setAlpha(150); + setPen(c); + setBrush(c); + } +}; + +PlotHandler::PlotHandler(QwtPlot *plot) + : QObject(plot), m_plot(plot), m_pos(0) +{ + QwtText xaxis("System Uptime [h:m:s]", QwtText::PlainText); + QwtText yaxis("Cpu Usage [%]", QwtText::PlainText); + + QFont f(plot->font()); + xaxis.setFont(f); + yaxis.setFont(f); + + for (int i = 0; i < nValue; ++i) + m_data[i] = new double[SIZE]; + m_timeStamp = new double[SIZE]; + + m_plot->setAutoReplot(false); + m_plot->plotLayout()->setAlignCanvasToScales(true); + + QwtLegend *legend = new QwtLegend(); + m_plot->insertLegend(legend, QwtPlot::RightLegend); + + m_plot->setAxisTitle(QwtPlot::xBottom, xaxis); + m_plot->setAxisFont(QwtPlot::xBottom, f); + m_plot->setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw()); + m_plot->setAxisScale(QwtPlot::xBottom, 0, SIZE); + + m_plot->setAxisLabelRotation(QwtPlot::xBottom, -50.0); + m_plot->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom); + + QwtScaleWidget *scaleWidget = m_plot->axisWidget(QwtPlot::xBottom); + const int fmh = QFontMetrics(scaleWidget->font()).height(); + scaleWidget->setMinBorderDist(0, fmh / 2); + + m_plot->setAxisTitle(QwtPlot::yLeft, yaxis); + m_plot->setAxisFont(QwtPlot::yLeft, f); + m_plot->setAxisScale(QwtPlot::yLeft, 0, 100); + + Background *bg = new Background(); + bg->attach(m_plot); + + m_curve[0] = new PlotCurve("User"); + m_curve[0]->setColor(Qt::blue); + m_curve[0]->setZ(m_curve[0]->z() - 1); + m_curve[0]->attach(m_plot); + m_curve[0]->setRawData(m_timeStamp, m_data[0], SIZE); + curveShow(0, true); + + m_curve[1] = new PlotCurve("Nice"); + m_curve[1]->setColor(Qt::yellow); + m_curve[1]->setZ(m_curve[1]->z() - 2); + m_curve[1]->attach(m_plot); + m_curve[1]->setRawData(m_timeStamp, m_data[1], SIZE); + curveShow(1, false); + + m_curve[2] = new PlotCurve("System"); + m_curve[2]->setColor(Qt::red); + m_curve[2]->attach(m_plot); + m_curve[2]->setRawData(m_timeStamp, m_data[2], SIZE); + curveShow(2, true); + + m_curve[3] = new PlotCurve("Idle"); + m_curve[3]->setColor(Qt::darkCyan); + m_curve[3]->setZ(m_curve[3]->z() - 3); + m_curve[3]->attach(m_plot); + m_curve[3]->setRawData(m_timeStamp, m_data[3], SIZE); + curveShow(3, false); + +} + +PlotHandler::~PlotHandler() +{ + for (int i = 0; i < nValue; ++i) + delete m_data[i]; + delete m_timeStamp; +} + +void PlotHandler::append(int timestamp, const QVector<int> &values) +{ + if (m_pos == 0) { + for (int j = 1; j < SIZE; ++j) { + m_timeStamp[j] = timestamp - (SIZE-j)*100; + for (int i = 0; i < nValue; ++i) + m_data[i][j] = 0; + } + } + int pos = m_pos % SIZE; + m_timeStamp[pos] = (double)timestamp; + m_timeStamp[(pos+1)%SIZE] = (double)timestamp + 100; + for (int i = 0; i < nValue; ++i) { + m_data[i][pos] = (double)values[i]; + m_data[i][(pos+1)%SIZE] = 0.0; + m_data[i][(pos+2)%SIZE] = 0.0; + } + + + m_plot->setAxisScale(QwtPlot::xBottom, m_timeStamp[(pos+3)%SIZE], m_timeStamp[pos]); + m_plot->replot(); + + ++m_pos; +} + + +void PlotHandler::curveShow(int id, bool show) +{ + m_curve[id]->setVisible(show); + m_plot->replot(); +} + +void PlotHandler::curveColor(int id, const QColor &color) +{ + m_curve[id]->setColor(color); + m_plot->replot(); +} + +void PlotHandler::load(bool enable) +{ + if (enable) + loadProcess.start("qt4-demo-load"); + else + loadProcess.terminate(); +} diff --git a/local_src/qt4-demo-2011.07.0/plothandler.h b/local_src/qt4-demo-2011.07.0/plothandler.h new file mode 100644 index 0000000..d860d39 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/plothandler.h @@ -0,0 +1,49 @@ + +#ifndef PLOT_HANDLER_H +#define PLOT_HANDLER_H + +#include <QtGui/QColor> +#include <QtCore/QObject> +#include <QtCore/QProcess> + +class QwtPlot; +class QwtPlotItem; + +class PlotCurve; + +class PlotHandler : public QObject +{ +Q_OBJECT +public: + PlotHandler(QwtPlot *plot); + ~PlotHandler(); + + enum Value { + User, + Nice, + System, + Idle, + + nValue + }; + + void showCurve(QwtPlotItem *item, bool on); + +public slots: + void append(int timestamp, const QVector<int> &values); + void curveShow(int id, bool show); + void curveColor(int id, const QColor &color); + + void load(bool enable); + +private: + PlotCurve *m_curve[nValue]; + QwtPlot *m_plot; + int m_pos; + double *m_timeStamp; + double *m_data[nValue]; + QProcess loadProcess; +}; + +#endif // PLOT_HANDLER_H + diff --git a/local_src/qt4-demo-2011.07.0/qt4-demo-load b/local_src/qt4-demo-2011.07.0/qt4-demo-load new file mode 100644 index 0000000..929c288 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/qt4-demo-load @@ -0,0 +1,16 @@ +#!/bin/sh +# create some simple load + +run_some_load { + while [ 1 ]; do + find / > /dev/null + done +} + +run_some_load +sleep 2 +run_some_load +sleep 3 +run_some_load + +exit 0 diff --git a/local_src/qt4-demo-2011.07.0/qt4-demo.pro b/local_src/qt4-demo-2011.07.0/qt4-demo.pro new file mode 100644 index 0000000..0b52659 --- /dev/null +++ b/local_src/qt4-demo-2011.07.0/qt4-demo.pro @@ -0,0 +1,15 @@ + +CONFIG += qt + +TARGET = qt4-demo + +INCLUDEPATH += /usr/include/qwt-qt4 + +SOURCES = plotdata.cpp plothandler.cpp configdialog.cpp main.cpp +FORMS = config.ui mainwindow.ui +HEADERS = plotdata.h plothandler.h configdialog.h + +LIBS += -lqwt + +MOC_DIR = moc + |