1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/***************************************************************************
* Copyright (C) 2008 by Alexey Balakin *
* mathgl.abalakin@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QLabel>
#include <QLayout>
#include <QSettings>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QFileDialog>
#include <QTextStream>
#include <QRadioButton>
#include <mgl2/mgl.h>
#include "open_dlg.h"
int numDataOpened=0;
extern mglParse parser;
QStringList dataScr;
//-----------------------------------------------------------------------------
QWidget *createDataOpenDlg(QWidget *p) { return new DataOpenDialog(p); }
QString getOpenDataFile(QWidget *w, QString filename)
{
DataOpenDialog *d = dynamic_cast<DataOpenDialog *>(w);
if(d)
{
d->setFile(filename);
if(d->exec()) return d->getCode();
}
return QString();
}
//-----------------------------------------------------------------------------
DataOpenDialog::DataOpenDialog(QWidget *parent) : QDialog(parent)
{
setWindowTitle(_("UDAV - Open data file"));
QHBoxLayout *a;
QLabel *l;
QPushButton *b;
QVBoxLayout *o=new QVBoxLayout(this);
a = new QHBoxLayout; o->addLayout(a);
l = new QLabel(_("Data name")); a->addWidget(l);
char buf[32]; snprintf(buf,32,"mgl_%d",numDataOpened); buf[31]=0;
name = new QLineEdit(buf,this); a->addWidget(name);
rA = new QRadioButton(_("Auto detect data sizes"), this);
rA->setChecked(true); o->addWidget(rA);
rM = new QRadioButton(_("Set data sizes manually"), this);
o->addWidget(rM); a = new QHBoxLayout; o->addLayout(a);
l = new QLabel(_("Nx")); a->addWidget(l);
nx = new QLineEdit("1",this); a->addWidget(nx);
l = new QLabel(_("Ny")); a->addWidget(l);
ny = new QLineEdit("1",this); a->addWidget(ny);
l = new QLabel(_("Nz")); a->addWidget(l);
nz = new QLineEdit("1",this); a->addWidget(nz);
r2 = new QRadioButton(_("Matrix with sizes from file"), this); o->addWidget(r2);
r3 = new QRadioButton(_("3D data with sizes from file"), this);o->addWidget(r3);
QSettings settings("udav","UDAV");
settings.setPath(QSettings::IniFormat, QSettings::UserScope, "UDAV");
settings.beginGroup("/UDAV");
dataScr = settings.value("/dataScr").toStringList().mid(0,10);
dataScr.removeDuplicates();
settings.endGroup();
a = new QHBoxLayout; o->addLayout(a);
l = new QLabel(_("Template")); a->addWidget(l,0);
scr = new QComboBox(this); a->addWidget(scr,1);
scr->setEditable(true); scr->lineEdit()->setText("");
scr->addItem(_("default")); scr->addItems(dataScr);
b = new QPushButton("...", this); a->addWidget(b,0);
connect(b, SIGNAL(clicked()),this, SLOT(selectScr()));
a = new QHBoxLayout; o->addLayout(a); a->addStretch(1);
b = new QPushButton(_("Cancel"),this); a->addWidget(b);
connect(b,SIGNAL(clicked()),this,SLOT(reject()));
b = new QPushButton(_("OK"), this); a->addWidget(b);
connect(b, SIGNAL(clicked()),this, SLOT(prepareResult()));
b->setDefault(true);
}
//-----------------------------------------------------------------------------
DataOpenDialog::~DataOpenDialog(){}
//-----------------------------------------------------------------------------
void DataOpenDialog::selectScr()
{
QString str = QFileDialog::getOpenFileName(this, _("UDAV - Insert filename"),
scr->lineEdit()->text(), _("MGL files (*.mgl)"));
if(!str.isEmpty())
{
scr->lineEdit()->setText(str);
scr->insertItem(1,str);
dataScr.insert(0,str);
dataScr.removeDuplicates();
}
}
//-----------------------------------------------------------------------------
void DataOpenDialog::prepareResult()
{
code = ""; numDataOpened++; data = name->text();
// prepare unique value of name for next time
char buf[32]; snprintf(buf,32,"mgl_%d",numDataOpened);
buf[31]=0; name->setText(buf);
mglData *v = dynamic_cast<mglData*>(parser.AddVar(data.toLocal8Bit().constData()));
if(!v) return;
int dd=0;
if(rA->isChecked()) // auto sizes
{
v->Read(file.toLocal8Bit().constData());
if(v->nx==1) { v->nx = v->ny; v->ny = v->nz; }
code=QString("#read %1 '%2'\n").arg(data).arg(file);
}
else if(rM->isChecked()) // manual sizes
{
int x=nx->text().toInt(), y=ny->text().toInt(), z=nz->text().toInt();
v->Read(file.toLocal8Bit().constData(),x,y,z);
code=QString("#read %1 '%2' %3 %4 %5\n").arg(data).arg(file).arg(x).arg(y).arg(z);
}
else if(r2->isChecked()) // matrix
{
v->ReadMat(file.toLocal8Bit().constData());
code=QString("#readmat %1 '%2'\n").arg(data).arg(file); dd=1;
}
else if(r3->isChecked()) // 3d-data
{
v->ReadMat(file.toLocal8Bit().constData(),3);
code=QString("#readmat %1 '%2' 3\n").arg(data).arg(file); dd=2;
}
if(scr->lineEdit()->text().isEmpty() || scr->lineEdit()->text()==_("default"))
{
if(v->nz>1 || dd==2)
code+=QString("rotate 40 60\ncrange %1:box\nsurf3 %1\n").arg(data);
else if(v->ny>1 || dd==1)
code+=QString("rotate 40 60\ncrange %1:zrange %1:box\nsurf %1\n").arg(data);
else code+=QString("yrange %1:box\nplot %1\n").arg(data);
}
else
{
QString str;
QFile fp(scr->lineEdit()->text());
if(fp.open(QFile::ReadOnly | QIODevice::Text))
{
QTextStream in(&fp);
str = in.readAll();
code += str.arg(data);
}
}
QSettings settings("udav","UDAV");
settings.setPath(QSettings::IniFormat, QSettings::UserScope, "UDAV");
settings.beginGroup("/UDAV");
settings.setValue("/dataScr", dataScr);
settings.endGroup();
accept();
}
//-----------------------------------------------------------------------------
void DataOpenDialog::setFile(const QString &fname)
{
file=fname;
mglData d(file.toLocal8Bit().constData());
rA->setText(QString(_("Auto detect data sizes (%1 x %2 x %3)")).arg(d.nx).arg(d.ny).arg(d.nz));
}
//-----------------------------------------------------------------------------
|