Reference documentation for deal.II version 8.4.2
mainwindow.cpp
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2010 - 2013 by Martin Steigemann and Wolfgang Bangerth
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <QtGui>
18 
19 #include "mainwindow.h"
20 #include "parameter_delegate.h"
21 #include "xml_parameter_reader.h"
22 #include "xml_parameter_writer.h"
23 
24 
25 namespace dealii
26 {
27  namespace ParameterGui
28  {
29  MainWindow::MainWindow(const QString &filename)
30  {
31  QString settings_file = QDir::currentPath() + "/settings.ini"; // a file for user settings
32 
33  gui_settings = new QSettings (settings_file, QSettings::IniFormat); // load settings
34  // Up to now, we do not read any settings,
35  // but this can be used in the future for customizing the GUI.
36 
37  tree_widget = new QTreeWidget; // tree for showing XML tags
38 
39  // Setup the tree and the window first:
40  tree_widget->header()->setResizeMode(QHeaderView::ResizeToContents); // behavior of the header sections:
41  // "Interactive: User can resize sections"
42  // "Fixed: User cannot resize sections"
43  // "Stretch: Qt will automatically resize sections to fill available space"
44  // "ResizeToContents: Qt will automatically resize sections to optimal size"
45  tree_widget->setHeaderLabels(QStringList() << tr("(Sub)Sections/Parameters")
46  << tr("Value"));
47  tree_widget->setMouseTracking(true); // enables mouse events e.g. showing ToolTips
48  // and documentation in the StatusLine
49  tree_widget->setEditTriggers(QAbstractItemView::DoubleClicked|
50  QAbstractItemView::SelectedClicked|
51  QAbstractItemView::EditKeyPressed);
52  // set which actions will initiate item editing: Editing starts when:
53  // DoubleClicked: an item is double clicked
54  // SelectedClicked: clicking on an already selected item
55  // EditKeyPressed: the platform edit key has been pressed over an item
56  // AnyKeyPressed: any key is pressed over an item
57 
58  tree_widget->setItemDelegate(new ParameterDelegate(1)); // set the delegate for editing items
59  setCentralWidget(tree_widget);
60  // connect: if the tree changes, the window will know
61  connect(tree_widget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(tree_was_modified()));
62 
63  create_actions(); // create window actions as "Open",...
64  create_menus(); // and menus
65  statusBar()->showMessage(tr("Ready, start editing by double-clicking or hitting F2!"));
66  setWindowTitle(tr("[*]parameterGUI")); // set window title
67 
68  resize(800, 600); // set window height and width
69 
70  if (filename.size() > 3) // if there is a file_name, try to load the file.
71  load_file(filename); // a vliad file has the xml extension, so we require size() > 3
72  }
73 
74 
75 
77  {
78  if (maybe_save()) // check, if the content was modified
79  {
80  QString file_name = // open a file dialog
81  QFileDialog::getOpenFileName(this, tr("Open XML Parameter File"),
82  QDir::currentPath(),
83  tr("XML Files (*.xml)"));
84  if (!file_name.isEmpty()) // if a file was selected,
85  load_file(file_name); // load the content
86  };
87  }
88 
89 
90 
92  {
93  if (current_file.isEmpty()) // if there is no file
94  return save_as(); // to save changes, open a dialog
95  else
96  return save_file(current_file); // otherwise save
97  }
98 
99 
100 
102  {
103  QString file_name = // open a file dialog
104  QFileDialog::getSaveFileName(this, tr("Save XML Parameter File"),
105  QDir::currentPath(),
106  tr("XML Files (*.xml)"));
107 
108  if (file_name.isEmpty()) // if no file was selected
109  return false; // return false
110  else
111  return save_file(file_name); // otherwise save content to file
112  }
113 
114 
115 
117  {
118 #ifdef Q_WS_MAC
119  static QPointer<QMessageBox> old_msg_box;
120 
121  if (old_msg_box)
122  {
123  old_msg_box->show();
124  old_msg_box->raise();
125  old_msg_box->activateWindow();
126  return;
127  };
128 #endif
129 
130  QString title = "About parameterGUI";
131 
132  QString trAboutparameterGUIcaption;
133  trAboutparameterGUIcaption = QMessageBox::tr(
134  "<h3>parameterGUI: A GraphicalUserInterface for parameter handling in deal.II</h3>"
135  "<p>This program uses Qt version %1.</p>"
136  ).arg(QLatin1String(QT_VERSION_STR));
137 
138  QString trAboutparameterGUItext;
139  trAboutparameterGUItext = QMessageBox::tr(
140  "<p>The parameterGUI is a graphical user interface for editing XML parameter files "
141  "created by the ParameterHandler class of deal.II. Please see "
142  "<a href=\"http://www.dealii.org/7.0.0/doxygen/deal.II/classParameterHandler.html\">dealii.org/doc</a> for more information. "
143  "The parameterGUI parses XML files into a tree structure and provides "
144  " special editors for different types of parameters.</p>"
145 
146  "<p><b>Editing parameter values:</b><br>"
147  "Parameters can be edited by (double-)clicking on the value or "
148  "by pressing the platform edit key (F2 on Linux) over an parameter item.</p>"
149 
150  "<p><b>Editors for parameter values:</b>"
151  " <ul>"
152  " <li>Integer- and Double-type parameters: SpinBox</li>"
153  " <li>Booleans: ComboBox</li>"
154  " <li>Selection: ComboBox</li>"
155  " <li>File- and DirectoryName parameters: BrowseLineEditor</li>"
156  " <li>Anything|MultipleSelection|List: LineEditor</li>"
157  " </ul>"
158  "</p>"
159 
160  "<p>Please see <a href=\"http://www.dealii.org\">dealii.org</a> for more information</p>"
161  "<p><b>Authors:</b><br> "
162  "Martin Steigemann, <a href=\"mailto:martin.steigemann@mathematik.uni-kassel.de\">martin.steigemann@mathematik.uni-kassel.de</a><br>"
163  "Wolfgang Bangerth, <a href=\"mailto:bangerth@math.tamu.edu\">bangerth@math.tamu.edu</a></p>"
164  );
165 
166  QMessageBox *msg_box = new QMessageBox;
167  msg_box->setAttribute(Qt::WA_DeleteOnClose);
168  msg_box->setWindowTitle(title);
169  msg_box->setText(trAboutparameterGUIcaption);
170  msg_box->setInformativeText(trAboutparameterGUItext);
171 
172  QPixmap pm(QLatin1String(":/images/logo_dealii_gui_128.png"));
173 
174  if (!pm.isNull())
175  msg_box->setIconPixmap(pm);
176 
177 #ifdef Q_WS_MAC
178  old_msg_box = msg_box;
179  msg_box->show();
180 #else
181  msg_box->exec();
182 #endif
183  }
184 
185 
186 
188  {
189  setWindowModified(true); // store, that the window was modified
190  // this is a function from the QMainWindow class
191  // and we use the windowModified mechanism to show a "*"
192  // in the window title, if content was modified
193  }
194 
195 
196 
198  {
199  QString title = "parameterGUI";
200 
201  info_message = new InfoMessage(this);
202 
203  info_message->setWindowTitle(title);
204  info_message->setInfoMessage(tr("Start Editing by double-clicking on the parameter value or"
205  " by hitting the platform edit key. For example, on Linux this is the F2-key!"));
207  }
208 
209 
210 
211  void MainWindow::closeEvent(QCloseEvent *event)
212  {
213  if (maybe_save()) // reimplement the closeEvent from the QMainWindow class
214  event->accept(); // check, if we have to save modified content,
215  else // if content was saved, accept the event,
216  event->ignore(); // otherwise ignore it
217  }
218 
219 
220 
222  {
223  QStyle * style = tree_widget->style();
224 
225  open_act = new QAction(tr("&Open..."), this); // create actions
226  open_act->setIcon(style->standardPixmap(QStyle::SP_DialogOpenButton)); // and set icons
227  open_act->setShortcut(Qt::CTRL + Qt::Key_O); // set a short cut
228  open_act->setStatusTip(tr("Open a XML file")); // set a status tip
229  connect(open_act, SIGNAL(triggered()), this, SLOT(open())); // and connect
230 
231  save_act = new QAction(tr("&Save ..."), this);
232  save_act->setIcon(style->standardPixmap(QStyle::SP_DialogSaveButton));
233  save_act->setShortcut(Qt::CTRL + Qt::Key_S);
234  save_act->setStatusTip(tr("Save the current XML file"));
235  connect(save_act, SIGNAL(triggered()), this, SLOT(save()));
236 
237  save_as_act = new QAction(tr("&Save As..."), this);
238  save_as_act->setIcon(style->standardPixmap(QStyle::SP_DialogSaveButton));
239  save_as_act->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_Q);
240  save_as_act->setStatusTip(tr("Save the current XML file as"));
241  connect(save_as_act, SIGNAL(triggered()), this, SLOT(save_as()));
242 
243  exit_act = new QAction(tr("E&xit"), this);
244  exit_act->setIcon(style->standardPixmap(QStyle::SP_DialogCloseButton));
245  exit_act->setShortcut(Qt::CTRL + Qt::Key_Q);
246  exit_act->setStatusTip(tr("Exit the parameterGUI application"));
247  connect(exit_act, SIGNAL(triggered()), this, SLOT(close()));
248 
249  about_act = new QAction(tr("&About"), this);
250  about_act->setIcon(style->standardPixmap(QStyle::SP_FileDialogInfoView));
251  about_act->setStatusTip(tr("Show the parameterGUI About box"));
252  connect(about_act, SIGNAL(triggered()), this, SLOT(about()));
253 
254  about_qt_act = new QAction(tr("About &Qt"), this);
255  about_qt_act->setStatusTip(tr("Show the Qt library's About box"));
256  connect(about_qt_act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
257  }
258 
259 
260 
262  {
263  file_menu = menuBar()->addMenu(tr("&File")); // create a file menu
264  file_menu->addAction(open_act); // and add actions
265  file_menu->addAction(save_act);
266  file_menu->addAction(save_as_act);
267  file_menu->addAction(exit_act);
268 
269  menuBar()->addSeparator();
270 
271  help_menu = menuBar()->addMenu(tr("&Help")); // create a help menu
272  help_menu->addAction(about_act);
273  help_menu->addAction(about_qt_act);
274  }
275 
276 
277 
279  {
280  if (isWindowModified()) // if content was modified
281  {
282  QMessageBox::StandardButton ret; // ask, if content should be saved
283  ret = QMessageBox::warning(this, tr("parameterGUI"),
284  tr("The content has been modified.\n"
285  "Do you want to save your changes?"),
286  QMessageBox::Save | QMessageBox::Discard |QMessageBox::Cancel);
287 
288  if (ret == QMessageBox::Save)
289  return save();
290  else if (ret == QMessageBox::Cancel)
291  return false;
292  };
293 
294  return true;
295  }
296 
297 
298 
299  bool MainWindow::save_file(const QString &filename)
300  {
301  QFile file(filename);
302 
303  if (!file.open(QFile::WriteOnly | QFile::Text)) // open a file dialog
304  {
305  QMessageBox::warning(this, tr("parameterGUI"),
306  tr("Cannot write file %1:\n%2.")
307  .arg(filename)
308  .arg(file.errorString()));
309  return false;
310  };
311 
312  XMLParameterWriter xml_writer(tree_widget); // create a xml_writer
313 
314  if (!xml_writer.write_xml_file(&file)) // and read the xml file
315  return false;
316 
317  statusBar()->showMessage(tr("File saved"), 2000); // if we succeed, show a message
318  set_current_file(filename); // and reset the window
319 
320  return true;
321  }
322 
323 
324 
325  void MainWindow::load_file(const QString &filename)
326  {
327  QFile file(filename);
328 
329  if (!file.open(QFile::ReadOnly | QFile::Text)) // open the file
330  {
331  QMessageBox::warning(this, tr("parameterGUI"),
332  tr("Cannot read file %1:\n%2.")
333  .arg(filename)
334  .arg(file.errorString()));
335  return;
336  };
337 
338  tree_widget->clear(); // clear the tree
339 
340  XMLParameterReader xml_reader(tree_widget); // and read the xml file
341 
342  if (!xml_reader.read_xml_file(&file))
343  {
344  QMessageBox::warning(this, tr("parameterGUI"),
345  tr("Parse error in file %1:\n\n%2")
346  .arg(filename)
347  .arg(xml_reader.error_string()));
348  }
349  else
350  {
351  statusBar()->showMessage(tr("File loaded - Start editing by double-clicking or hitting F2"), 25000);
352  set_current_file(filename); // show a message and set current file
353 
354  show_message (); // show some informations how values can be edited
355  };
356  }
357 
358 
359 
360  void MainWindow::set_current_file(const QString &filename)
361  {
362  // We use the windowModified mechanism from the
363  // QMainWindow class to indicate in the window title,
364  // if the content was modified.
365  // If there is "[*]" in the window title, a * will
366  // added automatically at this position, if the
367  // window was modified.
368  // We set the window title to
369  // file_name[*] - XMLParameterHandler
370 
371  current_file = filename; // set the (global) current file to file_name
372 
373  std::string win_title = (filename.toStdString()); // and create the window title,
374 
375  if (current_file.isEmpty()) // if file_name is empty
376  win_title = "[*]parameterGUI"; // set the title to our application name,
377  else
378  win_title += "[*] - parameterGUI"; // if there is a file_name, add the
379  // the file_name and a minus to the title
380 
381  setWindowTitle(tr(win_title.c_str())); // set the window title
382  setWindowModified(false); // and reset window modified
383  }
384  }
385 }
void load_file(const QString &filename)
Definition: mainwindow.cpp:325
MainWindow(const QString &filename="")
Definition: mainwindow.cpp:29
void set_current_file(const QString &filename)
Definition: mainwindow.cpp:360
void setInfoMessage(const QString &message)
void closeEvent(QCloseEvent *event)
Definition: mainwindow.cpp:211
bool save_file(const QString &filename)
Definition: mainwindow.cpp:299