Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
магистерская 2013.docx
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
2.45 Mб
Скачать

Приложение а Листинг программы

main.cpp

#include "widget.h"

#include <QApplication>

#include <QFileDialog>

#include <QDebug>

#include <QTextCodec>

#include <QMessageBox>

#include <QFile>

int main(int argc, char *argv[])

{

QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

QApplication a(argc, argv);

QString filePath = QFileDialog::getOpenFileName(NULL, QObject::tr("Выберите файл с данными"), "", "*.slk");

QFile file(filePath);

if (!file.exists()) {

QMessageBox::information(NULL, QObject::tr("Предупреждение"), QObject::tr("Вы не выбрали файл с данными!\nПриложение будет закрыто."));

return 0;

}

Widget w(file);

if (!w.parse()) {

QMessageBox::information(NULL, QObject::tr("Предупреждение"), QObject::tr("Ошибка в файле с данными!\nПриложение будет закрыто."));

return 0;

}

w.plot();

w.show();

return a.exec();

}

widge.h

#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

#include <QFile>

#include <QVector>

#include <QColor>

#include <QTableWidget>

#include <qwt_plot.h>

#include <qwt_plot_curve.h>

class Widget : public QWidget

{

Q_OBJECT

public:

explicit Widget(QFile &file, QWidget *parent = 0);

~Widget();

bool parse();

bool parseString(QString &str);

bool plot();

private:

private:

QColor _color[5];

QFile &_file;

QVector <qreal> _v[6];

QwtPlot *_plot;

QVector <QPointF> _examples[5];

QwtPlotCurve _curve[5];

QTableWidget *_table;

};

#endif // WIDGET_H

widget.cpp

#include "widget.h"

#include "ui_widget.h"

#include <QTextStream>

#include <QDebug>

#include <QHBoxLayout>

#include <QStringList>

Widget::Widget(QFile &file, QWidget *parent) :

QWidget(parent),

_file(file)

{

_color[0] = QColor(Qt::red);

_color[1] = QColor(Qt::green);

_color[2] = QColor(Qt::blue);

_color[3] = QColor(255,140,0);

_color[4] = QColor(128,0,0);

setWindowTitle(tr("Файл с данными: ") + file.fileName());

QHBoxLayout *layout = new QHBoxLayout();

_plot = new QwtPlot(this);

// _plot->setAutoFillBackground(true);

// QPalette p = _plot->palette();

// p.setColor(QPalette::Window, Qt::white);

// _plot->setPalette(p);

layout->addWidget(_plot);

_table = new QTableWidget(5, 2, this);

QStringList hh;

hh << tr("Цвет") << tr("Среднее");

_table->setHorizontalHeaderLabels(hh);

QStringList vh;

vh << tr("Плечо") << tr("Грудь") << tr("Живот") << tr("Бедро") << tr("Голень");

_table->setVerticalHeaderLabels(vh);

_table->horizontalHeader()->setResizeMode(QHeaderView::Fixed);

_table->verticalHeader()->setResizeMode((QHeaderView::Fixed));

for (int i = 0; i < 5; ++i) {

QTableWidgetItem *item = new QTableWidgetItem();

item->setBackgroundColor(_color[i]);

item->setFlags(Qt::ItemIsEnabled);

_table->setItem(i, 0, item);

}

_table->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);

_table->setMinimumWidth(_table->width());

layout->addWidget(_table);

setLayout(layout);

}

Widget::~Widget()

{

}

bool Widget::parse()

{

if (!_file.open(QIODevice::ReadOnly)) {

return false;

}

QTextStream stream(&_file);

QString str;

while (!stream.atEnd()) {

str = stream.readLine();

if (!parseString(str)) {

return false;

}

}

_file.close();

for (int i = 0; i < 5; ++i) {

if (_v[i].size() != _v[i+1].size()) {

return false;

}

}

return true;

}

bool Widget::parseString(QString &str)

{

if (str == "ID;E" || str == "E") {

return true;

}

int l = str.indexOf("C;X");

int r = str.indexOf(";Y");

if (l == -1 || r == -1) {

return false;

}

QString sx = str.mid(l + 3, r - l - 3);

bool ok;

int x = sx.toInt(&ok);

if (!ok) {

return false;

}

if (x < 1 || x > 6) {

return false;

}

l = str.indexOf(";K");

r = str.length();

QString sk = str.mid(l + 2, r - l);

double k = sk.toDouble(&ok);

if (!ok) {

return false;

}

_v[x - 1].push_back(k);

return true;

}

bool Widget::plot()

{

for (int i = 1; i < 6; ++i) {

_examples[i - 1].resize(_v[0].size());

for (int j = 0; j < _examples[i - 1].size(); ++j) {

_examples[i - 1][j].setX(_v[0][j]);

_examples[i - 1][j].setY(_v[i][j]);

}

}

for (int i = 0; i < 5; ++i) {

QPen pen = QPen(_color[i]);

_curve[i].setSamples(_examples[i]);

_curve[i].setPen(pen);

_curve[i].attach(_plot);

}

for (int i = 0; i < 5; ++i) {

qreal av = 0;

for (int j = 0; j < _examples[i].size(); ++j) {

av += _examples[i][j].y();

}

av /= _examples[i].size();

QTableWidgetItem *item = new QTableWidgetItem(QString::number(av));

item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled );

_table->setItem(i, 1, item);

}

_plot->replot();

return true;

}