Engauge Digitizer  2
FileCmdScript.cpp
Go to the documentation of this file.
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "FileCmdAbstract.h"
8 #include "FileCmdFactory.h"
9 #include "FileCmdScript.h"
10 #include "FileCmdSerialize.h"
11 #include "Logger.h"
12 #include "MainWindow.h"
13 #include <QDir>
14 #include <QFile>
15 #include <QMessageBox>
16 #include <QXmlStreamReader>
17 #include "Xml.h"
18 
19 FileCmdScript::FileCmdScript(const QString &fileCmdScriptFile)
20 {
21  LOG4CPP_INFO_S ((*mainCat)) << "FileCmdScript::FileCmdScript"
22  << " curDir=" << QDir::currentPath().toLatin1().data();
23 
24  // A non-existent script file is allowed in which case nothing gets done, as a way
25  // of tracking MainWindow being in a regression test that has no command script
26  if (!fileCmdScriptFile.isEmpty ()) {
27 
28  // Read commands into stack. The file is known to exist since it was checked in parseCmdLine
29  QFile file (fileCmdScriptFile);
30 
31  QXmlStreamReader reader (&file);
32  if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
33 
34  QString msg = QString ("%1 %2 %3 %4")
35  .arg (QObject::tr ("Cannot read script file"))
36  .arg (fileCmdScriptFile)
37  .arg (QObject::tr ("from directory"))
38  .arg (QDir::currentPath());
39  QMessageBox::critical (nullptr,
40  "Script File",
41  msg);
42  exit (-1);
43  }
44 
45  // Load commands
46  FileCmdFactory factory;
47  while (!reader.atEnd() && !reader.hasError()) {
48 
49  if ((loadNextFromReader (reader) == QXmlStreamReader::StartElement) &&
50  (reader.name() == FILE_CMD_SERIALIZE_CMD)) {
51 
52  // Extract and append new command to command stack
53  m_fileCmdStack.push_back (factory.createFileCmd (reader));
54  }
55  }
56  file.close();
57  }
58 }
59 
61 {
62 }
63 
65 {
66  LOG4CPP_INFO_S ((*mainCat)) << "FileCmdScript::canRedo";
67 
68  return (m_fileCmdStack.count () > 0);
69 }
70 
72 {
73  LOG4CPP_INFO_S ((*mainCat)) << "FileCmdScript::redo";
74 
75  m_fileCmdStack.first()->redo(mainWindow);
76  m_fileCmdStack.pop_front();
77 }
bool canRedo() const
Returns true if there is at least one command on the stack.
QXmlStreamReader::TokenType loadNextFromReader(QXmlStreamReader &reader)
Load next token from xml reader.
Definition: Xml.cpp:14
FileCmdScript(const QString &fileCmdScriptFile)
Single constructor.
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
const QString FILE_CMD_SERIALIZE_CMD
void redo(MainWindow &mainWindow)
Apply the next command. Requires non-empty stack.
log4cpp::Category * mainCat
Definition: Logger.cpp:14
FileCmdAbstract * createFileCmd(QXmlStreamReader &reader) const
Create one FileCmdAbstract from the specified xml subtree.
Factory that creates FileCmds from a file cmd script file, in xml format.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:91