代码拉取完成,页面将自动刷新
#include <QtWidgets>
#include "mainwindow.h"
#include "simpletable.h"
#include "linefit.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
simpletable = new SimpleTable;
setCentralWidget(simpletable);
createActions();
createMenus();
createContextMenu();
createToolBars();
createStatusBar();
readSettings();
setWindowIcon(QIcon(":icon.png"));
setCurrentFile("");
}
MainWindow::~MainWindow()
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (okToContinue()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
void MainWindow::newFile()
{
if (okToContinue()) {
simpletable->clear();
setCurrentFile("");
}
}
bool MainWindow::okToContinue()
{
if (isWindowModified()) {
int r = QMessageBox::warning(this, tr("Spreadsheet"),
tr("文件已被修改.\n"
"是否保存?"),
QMessageBox::Yes | QMessageBox::No
| QMessageBox::Cancel);
if (r == QMessageBox::Yes) {
return save();
} else if (r == QMessageBox::Cancel) {
return false;
}
}
return true;
}
void MainWindow::open()
{
if (okToContinue()) {
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open csv"), ".",
tr("csv files (*.csv)"));
if (!fileName.isEmpty())
loadFile(fileName);
}
}
bool MainWindow::loadFile(const QString &fileName)
{
if (!simpletable->readFile(fileName)) {
statusBar()->showMessage(tr("Loading canceled"), 2000);
return false;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
return true;
}
bool MainWindow::saveFile(const QString &fileName)
{
if (!simpletable->writeFile(fileName)) {
statusBar()->showMessage(tr("Saving canceled"), 2000);
return false;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
bool MainWindow::save()
{
if (curFile.isEmpty()) {
return saveAs();
} else {
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save spreadsheet file"), ".",
tr("Spreadsheet files (*.csv)"));
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About linefit"),
tr("<h2>linefit 0.1</h2>"
"<p>Copyright © 2016 Software Inc."
"<p>Linefit is a small application program"
"<p>y=a0+a1x+a2x^2+a3x^3"
"<p>欢迎使用,如有问题请反馈,谢谢!张兴武"));
}
void MainWindow::createActions()
{
newAction = new QAction(tr("&N新建"), this);
newAction->setIcon(QIcon(":new.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("创建新的电子表格文件"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
openAction = new QAction(tr("&O打开..."), this);
openAction->setIcon(QIcon(":open.png"));
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("打开已存在的电子表格文件"));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
saveAction = new QAction(tr("&S保存"), this);
saveAction->setIcon(QIcon(":save.png"));
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("电子表格文件存盘"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
saveAsAction = new QAction(tr("&A另存为..."), this);
saveAsAction->setStatusTip(tr("文件另存为...."));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
delRowAction=new QAction(tr("del row!"),this);
delRowAction->setIcon(QIcon(":delete_row.png"));
delRowAction->setStatusTip(tr("删除当前行"));
connect(delRowAction,SIGNAL(triggered()),this,SLOT(delRow()));
delColumnAction=new QAction(tr("del column!"),this);
delColumnAction->setIcon(QIcon(":column_delete.png"));
delColumnAction->setStatusTip(tr("删除当前列"));
connect(delColumnAction,SIGNAL(triggered()),this,SLOT(delColumn()));
ThermocoupleFitAction=new QAction(tr("热电偶数据拟合"),this);
ThermocoupleFitAction->setIcon(QIcon(":Temp.png"));
ThermocoupleFitAction->setStatusTip(tr("热电偶数据拟合"));
connect(ThermocoupleFitAction,SIGNAL(triggered()),this,SLOT(ThermocoupleFit()));
VenturitubeFitAction=new QAction(tr("文丘里数据拟合"),this);
VenturitubeFitAction->setIcon(QIcon(":ven1.png"));
VenturitubeFitAction->setStatusTip(tr("文丘里数据拟合"));
connect(VenturitubeFitAction,SIGNAL(triggered()),this,SLOT(VenturitubeFit()));
for (int i = 0; i < MaxRecentFiles; ++i) {
recentFileActions[i] = new QAction(this);
recentFileActions[i]->setVisible(false);
connect(recentFileActions[i], SIGNAL(triggered()),
this, SLOT(openRecentFile()));
}
exitAction = new QAction(tr("&X退出"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("退出应用程序"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
aboutAction = new QAction(tr("关于"), this);
aboutAction->setStatusTip(tr("显示信息"));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
// aboutQtAction = new QAction(tr("About &Qt"), this);
// aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
// connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::createMenus()
{
fileMenu=menuBar()->addMenu("文件(&N)");
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addAction(exitAction);
separatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i)
fileMenu->addAction(recentFileActions[i]);
fileMenu->addSeparator();
fileMenu=menuBar()->addMenu("运行(&R)");
fileMenu->addAction(ThermocoupleFitAction);
fileMenu->addAction(VenturitubeFitAction);
fileMenu=menuBar()->addMenu("关于");
fileMenu->addAction(aboutAction);
// fileMenu->addAction(aboutQtAction);
}
void MainWindow::createContextMenu()
{
}
void MainWindow::createToolBars()
{
fileToolBar=addToolBar("&File");
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction);
fileToolBar->addSeparator();
fileToolBar->addAction(delRowAction);
fileToolBar->addAction(delColumnAction);
fileToolBar->addSeparator();
fileToolBar->addAction(ThermocoupleFitAction);
fileToolBar->addAction(VenturitubeFitAction);
}
void MainWindow::createStatusBar()
{
locationLabel=new QLabel("W600");
statusBar()->addWidget(locationLabel);
locationLabel->setAlignment(Qt::AlignHCenter);
locationLabel->setMinimumSize(locationLabel->sizeHint());
formulaLabel = new QLabel;
formulaLabel->setIndent(3);
statusBar()->addWidget(formulaLabel, 1);
connect(simpletable, SIGNAL(currentCellChanged(int, int, int, int)),
this, SLOT(updateStatusBar()));
connect(simpletable,SIGNAL(itemChanged(QTableWidgetItem *)),
this,SLOT(simpletableModified()));
}
void MainWindow::simpletableModified()
{
setWindowModified(true);
updateStatusBar();
}
void MainWindow::updateStatusBar()
{
locationLabel->setText(simpletable->currentLocation());
// formulaLabel->setText(simpletable->currentFormula());
}
void MainWindow::writeSettings()
{
QSettings settings("ZhangXingWu", "lineFit");
settings.setValue("geometry", saveGeometry());
settings.setValue("recentFiles", recentFiles);
}
void MainWindow::readSettings()
{
QSettings settings("ZhangXingWu", "lineFit");
restoreGeometry(settings.value("geometry").toByteArray());
recentFiles = settings.value("recentFiles").toStringList();
updateRecentFileActions();
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void MainWindow::openRecentFile()
{
if (okToContinue()) {
QAction *action = qobject_cast<QAction *>(sender());
if (action)
loadFile(action->data().toString());
}
}
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = fileName;
setWindowModified(false);
QString shownName = tr("Untitled");
if (!curFile.isEmpty()) {
shownName = strippedName(curFile);
recentFiles.removeAll(curFile);
recentFiles.prepend(curFile);
updateRecentFileActions();
}
setWindowTitle(tr("%1[*] - %2").arg(shownName)
.arg(tr("Linefit")));
}
void MainWindow::updateRecentFileActions()
{
QMutableStringListIterator i(recentFiles);
while (i.hasNext()) {
if (!QFile::exists(i.next()))
i.remove();
}
if (recentFiles.count()<1)
{
return;
}
for (int j = 0; j < MaxRecentFiles; ++j) {
if (j < recentFiles.count()) {
QString text = tr("&%1 %2")
.arg(j + 1)
.arg(strippedName(recentFiles[j]));
recentFileActions[j]->setText(text);
recentFileActions[j]->setData(recentFiles[j]);
recentFileActions[j]->setVisible(true);
} else {
recentFileActions[j]->setVisible(false);
}
}
separatorAction->setVisible(!recentFiles.isEmpty());
}
void MainWindow::delRow()
{
//如何完美删除?确认在行选择的情况下?
int ix;
ix=simpletable->currentRow();
if (ix>0)
{
simpletable->removeRow(ix);
setWindowModified(true);
}
}
void MainWindow::delColumn()
{
int iy;
iy=simpletable->currentColumn();
if (iy>0)
{
simpletable->removeColumn(iy);
setWindowModified(true);
}
}
void MainWindow::ThermocoupleFit()
{
//get y array data
int row=0;
int arraynum=0;
int degree=3;
int columnCount;
bool numisok;
QString str;
// rowCount=simpletable->getRowCount();
columnCount=simpletable->getColumnCount();
//判断阵列长度,从表格第一行B列开始的字符串,判断是否为整数,不为整数则认为结束退出。
for (int ic=1;ic<columnCount;ic++)
{
str=simpletable->GetCellText(row,ic);
arraynum=ic;
bool ok;
str.toInt(&ok);
if (!ok)
break;
}
if (arraynum<2)
return;
arraynum--;
//建立y阵列,并读取温度值,y阵列
vector< double> yArray(arraynum);
for (int ic=1;ic<arraynum+1;ic++)
{
str=simpletable->GetCellText(row,ic);
if (!str.isEmpty())
yArray[ic-1]=str.toInt();
}
//qDebug()<<yArray[0]<<","<<yArray[arraynum-1]<<endl;
//write csv header
for (int ic=0;ic<=degree;ic++)
{
QString strnum;
strnum.setNum(ic);
simpletable->SetCellText(0,ic+arraynum+1,"a"+strnum);
}
for (int ic=0;ic<=degree;ic++)
{
QString strnum;
strnum.setNum(ic);
simpletable->SetCellText(0,ic+arraynum+1+degree+1,"b"+strnum);
}
simpletable->SetCellText(0,arraynum+(degree+1)*2+1,"Ra^2");
simpletable->SetCellText(0,arraynum+(degree+1)*2+2,"Rb^2");
//
row++;
do
{
//建立x阵列,并读取mv值
vector <double> xArray(arraynum);
for (int ic=1;ic<arraynum+1;ic++)
{
str=simpletable->GetCellText(row,ic);
if (!str.isEmpty())
xArray[ic-1]=str.toDouble(&numisok);
if (!numisok)
{
QMessageBox::warning(this,tr("错误"),QChar('A' + ic)+ QString::number(row + 1) +" is not a number");
return;
}
}
//[3] polynomial fit x array ,输入x,y阵列,进行拟合
//next
vector <double> aarray(degree+1);
polynomialFit(xArray,yArray,degree,aarray);
//[3]
//[4] get y
vector <double> barray(degree+1);
polynomialFit(yArray,xArray,degree,barray);
// qDebug()<<"\nHence the fitted Polynomial is given by:\ny=";
// for (int i=0; i<=degree; i++)
// qDebug()<<" + ("<<barray[i]<<")"<<"x^"<<i;
//[4]
for (int ic=0;ic<=degree;ic++)
{
QString strnum;
strnum.setNum(aarray[ic]);
simpletable->SetCellText(row,ic+arraynum+1,strnum);
}
for (int ic=0;ic<=degree;ic++)
{
QString strnum;
strnum.setNum(barray[ic]);
simpletable->SetCellText(row,ic+arraynum+1+degree+1,strnum);
}
//[5]计算方差
double ra2;
ra2=polynomialFitR2(xArray,yArray,aarray,degree);
QString strRa;
strRa.setNum(ra2);
double rb2;
rb2=polynomialFitR2(yArray,xArray,barray,degree);
QString strRb;
strRb.setNum(rb2);
simpletable->SetCellText(row,arraynum+(degree+1)*2+1,strRa);
simpletable->SetCellText(row,arraynum+(degree+1)*2+2,strRb);
//[5]
//[6]设置背景色
double cois=abs(abs(ra2)-1);
if (cois>0.01)
{
quint16 colmax=arraynum+(degree+1)*2+2;
for (int icol=0;icol<=colmax;icol++)
{
QTableWidgetItem *titem= simpletable->item(row,icol);
//下一行不能设为titem==NULL,否则可以运行,但有警告。
if (!titem==0)
titem->setBackgroundColor(QColor("red"));
}
}
//[6]
row++;
str=simpletable->GetCellText(row,0);
}while(!str.isEmpty());
//根据内容调整列宽
simpletable->resizeColumnsToContents();
}
void MainWindow::VenturitubeFit()
{
// *dc:节流孔或喉部内径
//# 获取文丘里管喉径
double dc;
double Ro=1000;
QString dcstr;
dcstr=simpletable->GetCellText(2,1);
bool dcisok;
dc=dcstr.toDouble(&dcisok);
if (!dcisok)
return;
bool feUnitIsM3,dhUnitIsKPa;
QString strfeUnit=simpletable->GetCellText(4,0);
QString strdhUnit=simpletable->GetCellText(4,1);
feUnitIsM3=strfeUnit.contains("m3", Qt::CaseInsensitive);
dhUnitIsKPa=strdhUnit.contains("KPa", Qt::CaseInsensitive);
quint16 rowdataBegin=0;
quint16 rowdataend=0;
int arrsize;
bool beginisok=false;
bool endisok=false;
for (int ic=0;ic<simpletable->rowCount();ic++)
{
QString rowfirstchar=simpletable->GetCellText(ic,0);
if (rowfirstchar.contains('{'))
{
beginisok=true;
rowdataBegin=ic;}
if (rowfirstchar.contains('}'))
{
rowdataend=ic;
endisok=true;
break;
}
}
arrsize=rowdataend-rowdataBegin-1;
if (arrsize<2)
return;
if (!beginisok||!endisok)
return;
vector <double> fe_kg(arrsize),dh_mmH2o(arrsize),logdh(arrsize);
vector <double> alpha(arrsize),reno(arrsize),logreno(arrsize);
for (int ic=0;ic<arrsize;ic++)
{
QString cellstr1=simpletable->GetCellText(ic+rowdataBegin+1,0);
QString cellstr2=simpletable->GetCellText(ic+rowdataBegin+1,1);
double fekg,dhmmH2o;
fekg=cellstr1.toDouble();
dhmmH2o=cellstr2.toDouble();
if (feUnitIsM3)
fekg=fekg*Ro;
if (dhUnitIsKPa)
dhmmH2o=dhmmH2o*1000/9.80665;
fe_kg[ic]=fekg;
dh_mmH2o[ic]=dhmmH2o;
}
for (int ic=0;ic<arrsize;ic++)
{
logdh[ic]=log10(dh_mmH2o[ic]);
alpha[ic]=fe_kg[ic]/(0.01251*dc*dc*sqrt(dh_mmH2o[ic]*Ro));
reno[ic]=0.354*fe_kg[ic]/0.00093216/dc;
logreno[ic]=log10(reno[ic]);
}
quint16 degree=3;
vector <double> aarray(degree+1),barray(degree+1);
polynomialFit(logdh,alpha,degree,aarray);
polynomialFit(logreno,alpha,degree,barray);
//[5]计算方差
double ra2;
ra2=polynomialFitR2(logdh,alpha,aarray,degree);
QString strRa;
strRa.setNum(ra2);
double rb2;
rb2=polynomialFitR2(logreno,alpha,barray,degree);
QString strRb;
strRb.setNum(rb2);
//write
for (int ic=0;ic<arrsize;ic++)
{
QString strdh,stralpha,strreno,strfe;
strdh.setNum(dh_mmH2o[ic]);
stralpha.setNum(alpha[ic]);
strreno.setNum(reno[ic]);
strfe.setNum(fe_kg[ic]);
simpletable->SetCellText(ic+rowdataBegin+1,2,strfe);
simpletable->SetCellText(ic+rowdataBegin+1,3,strdh);
simpletable->SetCellText(ic+rowdataBegin+1,4,stralpha);
simpletable->SetCellText(ic+rowdataBegin+1,5,strreno);
}
for (int ic=0;ic<degree+1;ic++)
{
QString xa,xb;
xa.setNum(aarray[ic]);
xb.setNum(barray[ic]);
simpletable->SetCellText(rowdataend+1,ic+1,xa);
simpletable->SetCellText(rowdataend+2,ic+1,xb);
}
simpletable->SetCellText(rowdataend+1,0,"dh_alpha");
simpletable->SetCellText(rowdataend+2,0,"reno_alpha");
simpletable->SetCellText(rowdataend+1,5,strRa);
simpletable->SetCellText(rowdataend+2,5,strRb);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。