Beispiel #1
0
bool Matrix::canCalculate(bool useMuParser)
{
  if (formula_str.isEmpty())
    return false;

  if (useMuParser){
    muParserScript *mup = new muParserScript(scriptingEnv(),QString("<%1>").arg(objectName()), this, false);
    connect(mup, SIGNAL(error(const QString&,const QString&,int)), scriptingEnv(), SIGNAL(error(const QString&, const QString&,int)));

    double *ri = mup->defineVariable("i");
    double *rr = mup->defineVariable("row");
    double *cj = mup->defineVariable("j");
    double *cc = mup->defineVariable("col");
    double *x = mup->defineVariable("x");
    double *y = mup->defineVariable("y");

    if (!mup->compile(formula_str))
      return false;

    double r = 1.0;
    *ri = r; *rr = r; *y = r;
    double c = 1.0; *cj = c; *cc = c; *x = c;
    int codeLines = mup->codeLines();
    if (codeLines == 1 && gsl_isnan(mup->evalSingleLine()))
      return false;
    else if (codeLines > 1){
      QVariant res = mup->evaluate(formula_str);
      if (!res.canConvert(QVariant::Double))
        return false;
    }
  } else {
    Script *script = scriptingEnv()->newScript(QString("<%1>").arg(objectName()),this, Script::NonInteractive);
    connect(script, SIGNAL(error(const QString&,const QString&,int)), scriptingEnv(), SIGNAL(error(const QString&,const QString&,int)));
    connect(script, SIGNAL(print(const QString&)), scriptingEnv(), SIGNAL(print(const QString&)));

    double r = 1.0;
    script->setDouble(r, "i");
    script->setDouble(r, "row");
    double c = 1.0;
    script->setDouble(c, "j");
    script->setDouble(c, "col");
    double x = 1.0;
    script->setDouble(x, "x");
    double y = 1.0;
    script->setDouble(y, "y");

    QVariant res = script->evaluate(formula_str);
    if (!res.canConvert(QVariant::Double))
      return false;
  }
  return true;
}
/**
 * Open a file
 * @param newtab :: If true, a new tab will be created
 * @param filename :: An optional file name
 */
void MultiTabScriptInterpreter::open(bool newtab, const QString & filename)
{
  QString fileToOpen = filename;
  if( fileToOpen.isEmpty() )
  {
    QString filter = scriptingEnv()->fileFilter();
    filter += tr("Text") + " (*.txt *.TXT);;";
    filter += tr("All Files")+" (*)";
    fileToOpen = QFileDialog::getOpenFileName(this, tr("MantidPlot - Open a script from a file"),
        m_last_dir, filter);
    if( fileToOpen.isEmpty() )
    {
      return;
    }
  }
  else
  {
    QFileInfo details(fileToOpen);
    fileToOpen = details.absoluteFilePath();
  }

  //Save last directory
  m_last_dir = QFileInfo(fileToOpen).absolutePath();

  int index(-1);
  if( !newtab ) index = closeCurrentTab();
  newTab(index, fileToOpen);

  //update the recent scripts menu 
  updateRecentScriptList(fileToOpen);

}
Beispiel #3
0
void ScriptingLangDialog::updateLangList()
{
  langList->clear();
  langList->insertItems(0, ScriptingLangManager::languages());
  QListWidgetItem *current = langList->findItems(scriptingEnv()->name(), Qt::MatchExactly).first();
  if (current)
    langList->setCurrentItem(current);
}
MatrixValuesDialog::MatrixValuesDialog( ScriptingEnv *env, QWidget* parent, Qt::WFlags fl )
: QDialog( parent, fl ), Scripted(env)
{
    setName( "MatrixValuesDialog" );
	setWindowTitle( tr( "MantidPlot - Set Matrix Values" ) );
	setSizeGripEnabled(true);

	QGridLayout *gl1 = new QGridLayout();
    gl1->addWidget(new QLabel(tr("For row (i)")), 0, 0);
	startRow = new QSpinBox();
	startRow->setRange(1, 1000000);
    gl1->addWidget(startRow, 0, 1);
	gl1->addWidget(new QLabel(tr("to")), 0, 2);
	endRow =  new QSpinBox();
	endRow->setRange(1, 1000000);
	gl1->addWidget(endRow, 0, 3);
	gl1->addWidget(new QLabel(tr("For col (j)")), 1, 0);
	startCol = new QSpinBox();
	startCol->setRange(1, 1000000);
	gl1->addWidget(startCol, 1, 1);
	gl1->addWidget(new QLabel(tr("to")), 1, 2);
	endCol = new QSpinBox();
	endCol->setRange(1, 1000000);
	gl1->addWidget(endCol, 1, 3);

	QVBoxLayout *vbox1 = new QVBoxLayout();
    vbox1->addLayout(gl1);
	QGroupBox *gb = new QGroupBox();
    gb->setLayout(vbox1);
    gb->setSizePolicy(QSizePolicy (QSizePolicy::Maximum, QSizePolicy::Preferred));

	QHBoxLayout *hbox3 = new QHBoxLayout();
	commands = new ScriptEditor(this, scriptingEnv()->createCodeLexer());
	commands->setFocus();
	hbox3->addWidget(commands);

	QVBoxLayout *vbox2 = new QVBoxLayout();
	btnApply = new QPushButton(tr( "&Apply" ));
    vbox2->addWidget(btnApply);
	btnCancel = new QPushButton(tr( "&Close" ));
    vbox2->addWidget(btnCancel);
    vbox2->addStretch();

	QHBoxLayout *hbox2 = new QHBoxLayout();
	hbox2->addWidget(gb);
	hbox2->addLayout(vbox2);

	QVBoxLayout* vbox3 = new QVBoxLayout(this);
	vbox3->addLayout(hbox2);
	vbox3->addWidget(new QLabel(tr( "Cell(i,j)=" )));
	vbox3->addLayout(hbox3);

	connect(btnApply, SIGNAL(clicked()), this, SLOT(apply()));
	connect(btnCancel, SIGNAL(clicked()), this, SLOT(close()));
}
Beispiel #5
0
bool Matrix::calculate(int startRow, int endRow, int startCol, int endCol, bool forceMuParser)
{
  if (QString(scriptingEnv()->name()) == "muParser" || forceMuParser)
    return muParserCalculate(startRow, endRow, startCol, endCol);

  double *buffer = d_matrix_model->dataCopy(startRow, endRow, startCol, endCol);
  if (buffer){
    d_undo_stack->push(new MatrixUndoCommand(d_matrix_model, Calculate, startRow, endRow,
        startCol, endCol, buffer, tr("Calculate Values")));
    emit modifiedWindow(this);
    return true;
  } else if(ignoreUndo()){
    d_matrix_model->calculate(startRow, endRow, startCol, endCol);
    emit modifiedWindow(this);
    return true;
  }
  return false;
}
/**
 * Create a new tab such that it is the specified index within the tab range.
 * @param index :: The index to give the new tab. If this is invalid the tab is simply appended
 * @param filename :: An optional filename
 */
void MultiTabScriptInterpreter::newTab(int index, const QString & filename)
{
  ScriptFileInterpreter *scriptRunner = new ScriptFileInterpreter(this,"ScriptWindow");
  scriptRunner->setup(*scriptingEnv(), filename);
  scriptRunner->toggleProgressReporting(m_reportProgress);
  connect(scriptRunner, SIGNAL(editorModificationChanged(bool)),
          this, SLOT(currentEditorModified(bool)));
  index = insertTab(index, scriptRunner, "");
  setCurrentIndex(index);
  setTabTitle(scriptRunner, filename); // Make sure the tooltip is set
  scriptRunner->setFocus();
  scriptRunner->editor()->zoomIn(globalZoomLevel());
  connect(scriptRunner->editor(), SIGNAL(textZoomedIn()), this, SLOT(zoomInAllButCurrent()));
  connect(scriptRunner->editor(), SIGNAL(textZoomedIn()), this, SLOT(trackZoomIn()));
  connect(scriptRunner->editor(), SIGNAL(textZoomedOut()), this, SLOT(zoomOutAllButCurrent()));
  connect(scriptRunner->editor(), SIGNAL(textZoomedOut()), this, SLOT(trackZoomOut()));

  emit newTabCreated(index);
  emit tabCountChanged(count());
}
MatrixValuesDialog::MatrixValuesDialog( ScriptingEnv *env, QWidget* parent, Qt::WFlags fl )
: QDialog( parent, fl ), Scripted(env)
{
    setName( "MatrixValuesDialog" );
	setWindowTitle( tr( "MantidPlot - Set Matrix Values" ) );
	setSizeGripEnabled(true);

	QGridLayout *gl1 = new QGridLayout();
    gl1->addWidget(new QLabel(tr("For row (i)")), 0, 0);
	startRow = new QSpinBox();
	startRow->setRange(1, 1000000);
    gl1->addWidget(startRow, 0, 1);
	gl1->addWidget(new QLabel(tr("to")), 0, 2);
	endRow =  new QSpinBox();
	endRow->setRange(1, 1000000);
	gl1->addWidget(endRow, 0, 3);
	gl1->addWidget(new QLabel(tr("For col (j)")), 1, 0);
	startCol = new QSpinBox();
	startCol->setRange(1, 1000000);
	gl1->addWidget(startCol, 1, 1);
	gl1->addWidget(new QLabel(tr("to")), 1, 2);
	endCol = new QSpinBox();
	endCol->setRange(1, 1000000);
	gl1->addWidget(endCol, 1, 3);

	functions = new QComboBox();
	btnAddFunction = new QPushButton(tr( "Add &Function" ));
	btnAddCell = new QPushButton(tr( "Add Ce&ll" ));

	QHBoxLayout *hbox1 = new QHBoxLayout();
	hbox1->addWidget(functions);
	hbox1->addWidget(btnAddFunction);
	hbox1->addWidget(btnAddCell);

	QVBoxLayout *vbox1 = new QVBoxLayout();
    vbox1->addLayout(gl1);
	vbox1->addLayout(hbox1);
	QGroupBox *gb = new QGroupBox();
    gb->setLayout(vbox1);
    gb->setSizePolicy(QSizePolicy (QSizePolicy::Preferred, QSizePolicy::Preferred));

	explain = new QTextEdit();
	explain->setReadOnly(true);
	explain->setSizePolicy(QSizePolicy (QSizePolicy::Preferred, QSizePolicy::Preferred));
    QPalette palette = explain->palette();
    palette.setColor(QPalette::Active, QPalette::Base, Qt::lightGray);
    explain->setPalette(palette);

	QHBoxLayout *hbox2 = new QHBoxLayout();
	hbox2->addWidget(explain);
	hbox2->addWidget(gb);

	QHBoxLayout *hbox3 = new QHBoxLayout();

	commands = new ScriptEditor(this, scriptingEnv()->createCodeLexer());
	commands->setFocus();
	hbox3->addWidget(commands);

	QVBoxLayout *vbox2 = new QVBoxLayout();
	btnApply = new QPushButton(tr( "&Apply" ));
    vbox2->addWidget(btnApply);
	btnCancel = new QPushButton(tr( "&Close" ));
    vbox2->addWidget(btnCancel);
    vbox2->addStretch();

	hbox3->addLayout(vbox2);

	QVBoxLayout* vbox3 = new QVBoxLayout(this);
	vbox3->addLayout(hbox2);
#ifdef SCRIPTING_PYTHON	
	boxMuParser = NULL;
	if (scriptingEnv()->name() != QString("muParser")){
		boxMuParser = new QCheckBox(tr("Use built-in muParser (much faster)"));
		boxMuParser->setChecked(true);
		vbox3->addWidget(boxMuParser);
	}
#endif
	vbox3->addWidget(new QLabel(tr( "Cell(i,j)=" )));
	vbox3->addLayout(hbox3);

	functions->insertStringList(scriptingEnv()->mathFunctions(), -1);
	insertExplain(0);

	connect(btnAddCell, SIGNAL(clicked()), this, SLOT(addCell()));
	connect(btnAddFunction, SIGNAL(clicked()), this, SLOT(insertFunction()));
	connect(btnApply, SIGNAL(clicked()), this, SLOT(apply()));
	connect(btnCancel, SIGNAL(clicked()), this, SLOT(close()));
	connect(functions, SIGNAL(activated(int)), this, SLOT(insertExplain(int)));
}
void MatrixValuesDialog::insertExplain(int index)
{
	explain->setText(scriptingEnv()->mathFunctionDoc(functions->text(index)));
}
SetColValuesDialog::SetColValuesDialog(ScriptingEnv *env, Table *t,
                                       Qt::WFlags fl)
    : QDialog(t, fl), Scripted(env) {
  setObjectName("SetColValuesDialog");
  setWindowTitle(tr("MantidPlot - Set column values"));
  setSizeGripEnabled(true);

  QHBoxLayout *hbox1 = new QHBoxLayout();
  hbox1->addWidget(new QLabel(tr("For row (i)")));
  start = new QSpinBox();
  start->setMinimum(1);
  hbox1->addWidget(start);

  hbox1->addWidget(new QLabel(tr("to")));

  end = new QSpinBox();
  end->setMinimum(1);
  hbox1->addWidget(end);

  // Ideally this would be checked at compile time. Until we have 'constexpr if`
  // on all platforms, the added complexity and minimal cost isn't worthwhile.
  if (sizeof(int) == 2) { // 16 bit signed integer
    start->setMaximum(0x7fff);
    end->setMaximum(0x7fff);
  } else { // 32 bit signed integer
    start->setMaximum(0x7fffffff);
    end->setMaximum(0x7fffffff);
  }

  QGridLayout *gl1 = new QGridLayout();
  boxColumn = new QComboBox();
  gl1->addWidget(boxColumn, 1, 0);
  btnAddCol = new QPushButton(tr("Add column"));
  gl1->addWidget(btnAddCol, 1, 1);

  QHBoxLayout *hbox3 = new QHBoxLayout();
  hbox3->addStretch();
  buttonPrev = new QPushButton("&<<");
  hbox3->addWidget(buttonPrev);
  buttonNext = new QPushButton("&>>");
  hbox3->addWidget(buttonNext);
  gl1->addLayout(hbox3, 2, 0);
  addCellButton = new QPushButton(tr("Add cell"));
  gl1->addWidget(addCellButton, 2, 1);

  QGroupBox *gb = new QGroupBox();
  QVBoxLayout *vbox1 = new QVBoxLayout();
  vbox1->addLayout(hbox1);
  vbox1->addLayout(gl1);
  gb->setLayout(vbox1);
  gb->setSizePolicy(
      QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));

  explain = new QTextEdit();
  explain->setReadOnly(true);
  explain->setSizePolicy(
      QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
  QPalette palette = explain->palette();
  palette.setColor(QPalette::Active, QPalette::Base, Qt::lightGray);
  explain->setPalette(palette);

  QHBoxLayout *hbox2 = new QHBoxLayout();
  hbox2->addWidget(explain);
  hbox2->addWidget(gb);

  commands = new ScriptEditor(this, scriptingEnv()->createCodeLexer());

  QVBoxLayout *vbox2 = new QVBoxLayout();
  btnApply = new QPushButton(tr("&Apply"));
  vbox2->addWidget(btnApply);
  btnCancel = new QPushButton(tr("&Close"));
  vbox2->addWidget(btnCancel);
  vbox2->addStretch();

  QHBoxLayout *hbox4 = new QHBoxLayout();
  hbox4->addWidget(commands);
  hbox4->addLayout(vbox2);

  QVBoxLayout *vbox3 = new QVBoxLayout();
  vbox3->addLayout(hbox2);

  colNameLabel = new QLabel();
  vbox3->addWidget(colNameLabel);
  vbox3->addLayout(hbox4);

  setLayout(vbox3);
  setFocusProxy(commands);
  commands->setFocus();

  connect(btnAddCol, SIGNAL(clicked()), this, SLOT(insertCol()));
  connect(addCellButton, SIGNAL(clicked()), this, SLOT(insertCell()));
  connect(btnApply, SIGNAL(clicked()), this, SLOT(apply()));
  connect(btnCancel, SIGNAL(clicked()), this, SLOT(close()));
  connect(buttonPrev, SIGNAL(clicked()), this, SLOT(prevColumn()));
  connect(buttonNext, SIGNAL(clicked()), this, SLOT(nextColumn()));

  setTable(t);
}
Beispiel #10
0
SetColValuesDialog::SetColValuesDialog( ScriptingEnv *env, Table* t, Qt::WFlags fl )
    : QDialog( t, fl ), Scripted(env)
{
    setName( "SetColValuesDialog" );
	setWindowTitle( tr( "MantidPlot - Set column values" ) );
	setSizeGripEnabled(true);

	QHBoxLayout *hbox1 = new QHBoxLayout();
	hbox1->addWidget(new QLabel(tr("For row (i)")));
	start = new QSpinBox();
	start->setMinValue(1);
	hbox1->addWidget(start);

	hbox1->addWidget(new QLabel(tr("to")));

	end = new QSpinBox();
	end->setMinValue(1);
	hbox1->addWidget(end);

	if (sizeof(int)==2)
	{ // 16 bit signed integer
		start->setMaxValue(0x7fff);
		end->setMaxValue(0x7fff);
	}
	else
	{ // 32 bit signed integer
		start->setMaxValue(0x7fffffff);
		end->setMaxValue(0x7fffffff);
	}

	QGridLayout *gl1 = new QGridLayout();
	functions = new QComboBox();
	gl1->addWidget(functions, 0, 0);
	btnAddFunction = new QPushButton(tr( "Add function" ));
	gl1->addWidget(btnAddFunction, 0, 1);
	boxColumn = new QComboBox();
	gl1->addWidget(boxColumn, 1, 0);
	btnAddCol = new QPushButton(tr( "Add column" ));
	gl1->addWidget(btnAddCol, 1, 1);

	QHBoxLayout *hbox3 = new QHBoxLayout();
	hbox3->addStretch();
	buttonPrev = new QPushButton("&<<");
	hbox3->addWidget(buttonPrev);
	buttonNext = new QPushButton("&>>");
	hbox3->addWidget(buttonNext);
	gl1->addLayout(hbox3, 2, 0);
	addCellButton = new QPushButton(tr( "Add cell" ));
	gl1->addWidget(addCellButton, 2, 1);

	QGroupBox *gb = new QGroupBox();
	QVBoxLayout *vbox1 = new QVBoxLayout();
	vbox1->addLayout(hbox1);
	vbox1->addLayout(gl1);
	gb->setLayout(vbox1);
	gb->setSizePolicy(QSizePolicy (QSizePolicy::Preferred, QSizePolicy::Preferred));

	explain = new QTextEdit();
	explain->setReadOnly (true);
	explain->setSizePolicy(QSizePolicy (QSizePolicy::Preferred, QSizePolicy::Preferred));
	QPalette palette = explain->palette();
	palette.setColor(QPalette::Active, QPalette::Base, Qt::lightGray);
	explain->setPalette(palette);

	QHBoxLayout *hbox2 = new QHBoxLayout();
	hbox2->addWidget(explain);
	hbox2->addWidget(gb);

	commands = new ScriptEditor(this, scriptingEnv()->createCodeLexer());

	QVBoxLayout *vbox2 = new QVBoxLayout();
	btnApply = new QPushButton(tr( "&Apply" ));
	vbox2->addWidget(btnApply);
	btnCancel = new QPushButton(tr( "&Close" ));
	vbox2->addWidget(btnCancel);
	vbox2->addStretch();

	QHBoxLayout *hbox4 = new QHBoxLayout();
	hbox4->addWidget(commands);
	hbox4->addLayout(vbox2);

	QVBoxLayout* vbox3 = new QVBoxLayout();
	vbox3->addLayout(hbox2);

	colNameLabel = new QLabel();
	vbox3->addWidget(colNameLabel);
	vbox3->addLayout(hbox4);

	setLayout(vbox3);
	setFocusProxy (commands);
	commands->setFocus();

	functions->insertStringList(scriptingEnv()->mathFunctions(), -1);
	if (functions->count() > 0)
		insertExplain(0);

	connect(btnAddFunction, SIGNAL(clicked()),this, SLOT(insertFunction()));
	connect(btnAddCol, SIGNAL(clicked()),this, SLOT(insertCol()));
	connect(addCellButton, SIGNAL(clicked()),this, SLOT(insertCell()));
	connect(btnApply, SIGNAL(clicked()),this, SLOT(apply()));
	connect(btnCancel, SIGNAL(clicked()),this, SLOT(close()));
	connect(functions, SIGNAL(activated(int)),this, SLOT(insertExplain(int)));
	connect(buttonPrev, SIGNAL(clicked()), this, SLOT(prevColumn()));
	connect(buttonNext, SIGNAL(clicked()), this, SLOT(nextColumn()));

  setTable(t);
}