Vespucci  1.0.0
macrodialog.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
20 #include "macrodialog.h"
21 #include "ui_macrodialog.h"
23 
24 MacroDialog::MacroDialog(MainWindow *parent, QSharedPointer<VespucciWorkspace> ws) :
25  QDialog(parent),
26  ui(new Ui::MacroDialog)
27 {
28  ui->setupUi(this);
29  workspace_ = ws;
30  dataset_valid_ = false;
31 }
32 
34 {
35  delete ui;
36 }
37 
38 void MacroDialog::SetActiveDataset(QSharedPointer<VespucciDataset> dataset)
39 {
40  dataset_ = dataset;
41  dataset_valid_ = true;
42 }
43 
44 void MacroDialog::MacroRequested(const QStringList &macro)
45 {
46  QString operations = ui->plainTextEdit->toPlainText() + "\n";
47  for (auto line: macro){
48  operations = operations + line + "\n";
49  }
50  ui->plainTextEdit->setPlainText(operations);
51 }
52 
53 void MacroDialog::DatasetSelectionChanged(QString dataset_key)
54 {
55  dataset_ = workspace_->GetDataset(dataset_key);
56  ui->datasetLabel->setText("Dataset: " + dataset_key);
57 }
58 
59 void MacroDialog::DatasetToBeRemoved(const QString &dataset_key)
60 {
61  if (dataset_->name() == dataset_key){
62  dataset_valid_ = false;
63  dataset_ = QSharedPointer<VespucciDataset>(NULL);
64  ui->datasetLabel->setText("Dataset: N/A");
65  }
66 }
67 
68 void MacroDialog::closeEvent(QCloseEvent *ev)
69 {
70  QDialog::closeEvent(ev);
71  emit SetActionChecked(false);
72 }
73 
74 void MacroDialog::on_openToolButton_clicked()
75 {
76  QString filename = QFileDialog::getOpenFileName(this, "Load Macro",
77  workspace_->directory(),
78  "Text files (*.txt)");
79  QFileInfo file_info(filename);
80  if (file_info.exists() && file_info.isFile()){
81  QFile file(filename);
82  file.open(QFile::ReadOnly);
83  QTextStream stream(&file);
84  QString text = stream.readAll();
85  file.close();
86  ui->plainTextEdit->clear();
87  ui->plainTextEdit->setPlainText(text);
88  }
89 }
90 
91 void MacroDialog::on_saveToolButton_clicked()
92 {
93  QString filename = QFileDialog::getSaveFileName(this, "Save Macro",
94  workspace_->directory(),
95  "Text files (*.txt)");
96  QFile file(filename);
97  file.open(QFile::WriteOnly);
98  QTextStream stream(&file);
99  QString text = ui->plainTextEdit->toPlainText();
100  stream << text;
101  file.close();
102 }
103 
104 void MacroDialog::on_runToolButton_clicked()
105 {
106  if (!dataset_.isNull()){
107  MacroParser parser(dataset_);
108  QString macro = ui->plainTextEdit->toPlainText();
109  parser.LoadMacro(macro);
110  bool ok = false;
111  try{
112  ok = parser.ExecuteMacro();
113  }catch(exception e){
114  workspace_->main_window()->DisplayExceptionWarning(e);
115  return;
116  }
117 
118  if (!ok){
119  int error_line, error_param;
120  parser.Error(error_line, error_param);
121  QString description = "Error occured at parameter "
122  + QString::number(error_param)
123  + " in line " + QString::number(error_line);
124  QMessageBox::warning(this, "Syntax Error", description);
125  }
126  }
127 }
128 
129 void MacroDialog::on_helpToolButton_clicked()
130 {
131  QDesktopServices::openUrl(QUrl("http://vespucciproject.org/Vespucci-docs/adv/macros.html"));
132 }
133 
134 void MacroDialog::on_runAllToolButton_clicked()
135 {
136  QString macro = ui->plainTextEdit->toPlainText();
137  for (auto dataset_key: workspace_->dataset_names()){
138  QSharedPointer<VespucciDataset> dataset = workspace_->GetDataset(dataset_key);
139  MacroParser parser(dataset);
140  parser.LoadMacro(macro);
141  bool ok = false;
142  try{
143  ok = parser.ExecuteMacro();
144  }catch(exception e){
145  workspace_->main_window()->DisplayExceptionWarning(e);
146  return;
147  }
148 
149  if (!ok){
150  int error_line, error_param;
151  parser.Error(error_line, error_param);
152  QString description = "Error occured at parameter "
153  + QString::number(error_param)
154  + " in line " + QString::number(error_line);
155  QMessageBox::warning(this, "Syntax Error", description);
156  return;
157  }
158  }
159 
160 }
Definition: ahcadialog.h:26
MacroDialog(MainWindow *parent, QSharedPointer< VespucciWorkspace > ws)
Definition: macrodialog.cpp:24
void SetActionChecked(bool checked)
void closeEvent(QCloseEvent *ev)
Definition: macrodialog.cpp:68
bool LoadMacro(QString macro)
MacroParser::LoadMacro.
Definition: macroparser.cpp:68
void DatasetSelectionChanged(QString dataset_key)
Definition: macrodialog.cpp:53
void MacroRequested(const QStringList &macro)
Definition: macrodialog.cpp:44
void SetActiveDataset(QSharedPointer< VespucciDataset > dataset)
Definition: macrodialog.cpp:38
void Error(int &error_line, int &error_param)
void DatasetToBeRemoved(const QString &dataset_key)
Definition: macrodialog.cpp:59
bool ExecuteMacro()
MacroParser::ExecuteMacro Iteratively executes the commands in commands_. Each command is first valid...
The MainWindow class The main window of the program, this is where the user performs most operations...
Definition: mainwindow.h:58