SimCenterCommon
Common functionality used within different SimCenter projects
mainwindow.cpp
Go to the documentation of this file.
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include <QStandardItemModel>
4 #include <QStandardItem>
5 #include <QList>
6 #include <simfigure.h>
7 
8 #include <QDebug>
9 #include <math.h>
10 
11 #define PI 3.141592653590
12 
13 MainWindow::MainWindow(QWidget *parent) :
14  QMainWindow(parent),
15  ui(new Ui::MainWindow)
16 {
17  ui->setupUi(this);
18  QStandardItemModel *model = new QStandardItemModel();
19  QList<QStandardItem *> items;
20  items.append(new QStandardItem("Demo 1"));
21  items.append(new QStandardItem("Demo 2"));
22  items.append(new QStandardItem("Demo 3"));
23  items.append(new QStandardItem("Demo 4"));
24  items.append(new QStandardItem("Clear"));
25  model->appendColumn(items);
26  ui->selectionView->setModel(model);
27  ui->selectionView->header()->hide();
28  ui->selectionView->setColumnWidth(0,30);
29 
30  connect(ui->theFigure, SIGNAL(curve_selected(int)), this, SLOT(on_selection_changed(int)));
31 }
32 
34 {
35  delete ui;
36 }
37 
38 void MainWindow::on_selectionView_clicked(const QModelIndex &index)
39 {
40  QVector<double> x;
41  QVector<double> y;
42  QVector<double> z;
43  QVector<double> d;
44  QVector<double> u;
45 
46  ui->theFigure->clear();
47 
48  int cnt = index.row();
49  switch (cnt)
50  {
51  case 0: {
52  for (double s=5.;s<=128.9;s+=0.1)
53  {
54  x.append(s);
55  y.append( 2.+sin(0.1*s) );
56  z.append( 2.+cos(0.1*s) );
57  }
58  int idx;
59  idx = ui->theFigure->plot(x,y);
60  idx = ui->theFigure->plot(x,z, SimFigure::LineType::Solid, QColor(Qt::blue));
61 
62  ui->theFigure->setTitle("Demo #1: simple functions");
63  ui->theFigure->setXLabel("x -->");
64  ui->theFigure->setYLabel("f(x) -->");
65  ui->theFigure->showLegend();
66  ui->theFigure->setAxisType(SimFigure::AxisType::LogX);
67 
68  break; }
69  case 1: {
70  for (double s=0.;s<=2.*PI;s+=PI/20)
71  {
72  x.append( s );
73  y.append( 200. + 150*sin(s) );
74  z.append( 200. + 150*cos(s) );
75  d.append( 200. + 175*0.5*s/PI * cos(s));
76  u.append( 200. + 175*0.5*s/PI * sin(s));
77  }
78  int idx;
79  idx = ui->theFigure->plot(y,z, SimFigure::LineType::DashDotted, Qt::green);
80  idx = ui->theFigure->plot(d,u, SimFigure::LineType::Solid, Qt::blue, SimFigure::Marker::Triangle);
81 
82  ui->theFigure->setTitle("Demo #2: parametric plot");
83  ui->theFigure->setXLabel("x(s) -->");
84  ui->theFigure->setYLabel("y(s) -->");
85  ui->theFigure->showLegend();
86  ui->theFigure->setAxisType(SimFigure::AxisType::Default);
87 
88  break; }
89  case 2: {
90  for (double s=0.;s<=2.*PI;s+=PI/20)
91  {
92  x.append( s );
93  y.append( 200. + 150*sin(s) );
94  z.append( 200. + 150*cos(s) );
95  d.append( 200. - 125. + 125*s/PI );
96  u.append( 200. + 125 * sin(s));
97  }
98  int idx;
99  idx = ui->theFigure->plot(y,z, SimFigure::LineType::DashDotted, Qt::darkGreen);
100  idx = ui->theFigure->scatter(d,u, Qt::red, SimFigure::Marker::Circle);
101 
102  ui->theFigure->setTitle("Demo #3: scatter plot");
103  ui->theFigure->setXLabel("x -->");
104  ui->theFigure->setYLabel("y = f(x) -->");
105  ui->theFigure->showLegend();
106  ui->theFigure->setAxisType(SimFigure::AxisType::Default);
107 
108  break; }
109  case 3: {
110  for (double s=0.;s<=2.*PI;s+=PI/20)
111  {
112  x.append( s );
113  y.append( 200. + 150*sin(s) );
114  z.append( 200. + 150*cos(s) );
115  d.append( 200. - 125. + 125*s/PI );
116  u.append( 200. + 125 * sin(s));
117  }
118  int idx;
119  idx = ui->theFigure->plot(y,z, SimFigure::LineType::DashDotted, Qt::green);
120  idx = ui->theFigure->plot(d,u, SimFigure::LineType::Solid, Qt::blue, SimFigure::Marker::Triangle);
121 
122  ui->theFigure->setTitle("Demo #4: ");
123  ui->theFigure->setTitleFontSize(24);
124  ui->theFigure->setXLabel("index i");
125  ui->theFigure->setYLabel("element a_i");
126  ui->theFigure->setLabelFontSize(20);
127  ui->theFigure->showLegend();
128  ui->theFigure->setAxisType(SimFigure::AxisType::Default);
129 
130  break; }
131  default:
132  x.clear();
133  y.clear();
134  z.clear();
135  d.clear();
136  u.clear();
137  ui->theFigure->showLegend(false);
138  break;
139  }
140 
141  ui->showLegend->setCheckState(Qt::CheckState::Checked);
142 }
143 
144 void MainWindow::on_selection_changed(int ID)
145 {
146  if (ID >= 0)
147  {
148  ui->btn_option3->setText(tr("ID = %1\n\nclick to\nclear\nselection").arg(ID));
149  ui->btn_option3->setStyleSheet("color: #cc4444");
150 
151  switch (ID)
152  {
153  case 1:
154  ui->btn_1st->setChecked(true);
155  break;
156  case 2:
157  ui->btn_2nd->setChecked(true);
158  break;
159  default:
160  ui->btn_none->setChecked(true);
161  }
162  }
163  else {
164  ui->btn_option3->setText(tr("none\nselected"));
165  ui->btn_option3->setStyleSheet("color: #cc4444");
166 
167  ui->btn_none->setChecked(true);
168  }
169 }
170 
171 void MainWindow::on_btn_option1_clicked()
172 {
173  if (ui->theFigure->legendVisible()) {
174  ui->showLegend->setCheckState(Qt::CheckState::Unchecked);
175  //ui->theFigure->showLegend(false);
176  } else {
177  ui->showLegend->setCheckState(Qt::CheckState::Checked);
178  //ui->theFigure->showLegend(true);
179  }
180 }
181 
182 void MainWindow::on_btn_option2_clicked()
183 {
184  QVector<SimFigure::Location> locList({
193  });
194 
195  currentLocation++;
196  if (currentLocation >= locList.length()) currentLocation -= locList.length();
197  ui->theFigure->moveLegend(locList[currentLocation]);
198 }
199 
200 void MainWindow::on_btn_option3_clicked()
201 {
202  ui->theFigure->clearSelection();
203  ui->btn_none->setChecked(true);
204 }
205 
206 void MainWindow::on_btn_1st_clicked()
207 {
208  ui->theFigure->select(1);
209 }
210 
211 void MainWindow::on_btn_2nd_clicked()
212 {
213  ui->theFigure->select(2);
214 }
215 
216 void MainWindow::on_btn_none_clicked()
217 {
218  ui->theFigure->clearSelection();
219 }
220 
221 void MainWindow::on_action_Save_triggered()
222 {
223  ui->theFigure->saveToFile("SimPlotDemo1.png", SimFigure::FileType::PNG);
224 }
225 
226 void MainWindow::on_actionSave_As_triggered()
227 {
228  ui->theFigure->exportToFile("SimPlotDemo2", SimFigure::FileType::PNG);
229 }
230 
231 void MainWindow::on_actionSave_Hi_res_triggered()
232 {
233  ui->theFigure->saveToFile("SimPlotDemo3", SimFigure::FileType::PNG, QSizeF(300,200), 200);
234 }
235 
236 void MainWindow::on_actionSave_PDF_triggered()
237 {
238  ui->theFigure->saveToFile("SimPlotDemo4.pdf", SimFigure::FileType::PNG, QSizeF(300,200), 150);
239 }
240 
241 void MainWindow::on_actionShow_axis_controls_triggered()
242 {
243  ui->theFigure->showAxisControls(true);
244 }
245 
246 void MainWindow::on_actionHide_axis_controls_triggered()
247 {
248  ui->theFigure->showAxisControls(false);
249 }
250 
251 void MainWindow::on_btn_option4_clicked()
252 {
253  ui->theFigure->fit_data();
254 }
255 
256 void MainWindow::on_showLegend_stateChanged(int arg1)
257 {
258  ui->theFigure->showLegend((ui->showLegend->checkState()==Qt::CheckState::Checked));
259 }
260 
261 void MainWindow::on_zoomOut_clicked()
262 {
263  double xmax = ui->theFigure->maxX();
264  double xmin = ui->theFigure->minX();
265  double ymax = ui->theFigure->maxY();
266  double ymin = ui->theFigure->minY();
267 
268  double w=xmax-xmin;
269  double h=ymax-ymin;
270 
271  ui->theFigure->setXLim(xmin-w/10.,xmax+w/10.);
272  ui->theFigure->setYLim(ymin-h/10.,ymax+h/10.);
273 }
274 
275 void MainWindow::on_zoomIn_clicked()
276 {
277  double xmax = ui->theFigure->maxX();
278  double xmin = ui->theFigure->minX();
279  double ymax = ui->theFigure->maxY();
280  double ymin = ui->theFigure->minY();
281 
282  double w=xmax-xmin;
283  double h=ymax-ymin;
284 
285  ui->theFigure->setXLim(xmin+w/10.,xmax-w/10.);
286  ui->theFigure->setYLim(ymin+h/10.,ymax-h/10.);
287 }
centered vertically on the left
centered horizontally at the bottom
Definition: dialogabout.h:6
centered vertically on the right
portable network graphic file
a dash-dotted line is drawn
centered horizontally at the top
MainWindow(QWidget *parent=0)
linear scales for x and y
log scale for x, linear scale for y
#define PI
Definition: mainwindow.cpp:11
a solid line is drawn