21 #include "ldDocument.h" |
21 #include "ldDocument.h" |
22 #include "ldObject.h" |
22 #include "ldObject.h" |
23 #include "mainwindow.h" |
23 #include "mainwindow.h" |
24 #include "dialogs/openprogressdialog.h" |
24 #include "dialogs/openprogressdialog.h" |
25 |
25 |
26 DocumentLoader::DocumentLoader (bool onForeground, QObject *parent) : |
26 DocumentLoader::DocumentLoader(bool onForeground, QObject *parent) : |
27 QObject (parent), |
27 QObject(parent), |
28 m_warningCount (0), |
28 m_warningCount(0), |
29 m_isDone (false), |
29 m_isDone(false), |
30 m_hasAborted (false), |
30 m_hasAborted(false), |
31 m_isOnForeground (onForeground) {} |
31 m_isOnForeground(onForeground) {} |
32 |
32 |
33 bool DocumentLoader::hasAborted() |
33 bool DocumentLoader::hasAborted() |
34 { |
34 { |
35 return m_hasAborted; |
35 return m_hasAborted; |
36 } |
36 } |
58 const LDObjectList& DocumentLoader::objects() const |
58 const LDObjectList& DocumentLoader::objects() const |
59 { |
59 { |
60 return m_objects; |
60 return m_objects; |
61 } |
61 } |
62 |
62 |
63 void DocumentLoader::read (QIODevice* fp) |
63 void DocumentLoader::read(QIODevice* fp) |
64 { |
64 { |
65 if (fp and fp->isOpen()) |
65 if (fp and fp->isOpen()) |
66 { |
66 { |
67 while (not fp->atEnd()) |
67 while (not fp->atEnd()) |
68 m_lines << QString::fromUtf8 (fp->readLine()); |
68 m_lines << QString::fromUtf8(fp->readLine()); |
69 } |
69 } |
70 } |
70 } |
71 |
71 |
72 void DocumentLoader::start() |
72 void DocumentLoader::start() |
73 { |
73 { |
77 |
77 |
78 if (isOnForeground()) |
78 if (isOnForeground()) |
79 { |
79 { |
80 // Show a progress dialog if we're loading the main ldDocument.here so we can show progress updates and keep the |
80 // Show a progress dialog if we're loading the main ldDocument.here so we can show progress updates and keep the |
81 // WM posted that we're still here. |
81 // WM posted that we're still here. |
82 m_progressDialog = new OpenProgressDialog (g_win); |
82 m_progressDialog = new OpenProgressDialog(g_win); |
83 m_progressDialog->setNumLines (m_lines.size()); |
83 m_progressDialog->setNumLines(m_lines.size()); |
84 m_progressDialog->setModal (true); |
84 m_progressDialog->setModal(true); |
85 m_progressDialog->show(); |
85 m_progressDialog->show(); |
86 connect (this, SIGNAL (workDone()), m_progressDialog, SLOT (accept())); |
86 connect(this, SIGNAL(workDone()), m_progressDialog, SLOT(accept())); |
87 connect (m_progressDialog, SIGNAL (rejected()), this, SLOT (abort())); |
87 connect(m_progressDialog, SIGNAL(rejected()), this, SLOT(abort())); |
88 } |
88 } |
89 else |
89 else |
90 m_progressDialog = nullptr; |
90 m_progressDialog = nullptr; |
91 |
91 |
92 // Begin working |
92 // Begin working |
93 work (0); |
93 work(0); |
94 } |
94 } |
95 |
95 |
96 void DocumentLoader::work (int i) |
96 void DocumentLoader::work(int i) |
97 { |
97 { |
98 // User wishes to abort, so stop here now. |
98 // User wishes to abort, so stop here now. |
99 if (hasAborted()) |
99 if (hasAborted()) |
100 { |
100 { |
101 for (LDObject* obj : m_objects) |
101 for (LDObject* obj : m_objects) |
107 } |
107 } |
108 |
108 |
109 // Parse up to 200 lines per iteration |
109 // Parse up to 200 lines per iteration |
110 int max = i + 200; |
110 int max = i + 200; |
111 |
111 |
112 for (; i < max and i < (int) m_lines.size(); ++i) |
112 for (; i < max and i <(int) m_lines.size(); ++i) |
113 { |
113 { |
114 QString line = m_lines[i]; |
114 QString line = m_lines[i]; |
115 |
115 |
116 // Trim the trailing newline |
116 // Trim the trailing newline |
117 while (line.endsWith ("\n") or line.endsWith ("\r")) |
117 while (line.endsWith("\n") or line.endsWith("\r")) |
118 line.chop (1); |
118 line.chop(1); |
119 |
119 |
120 LDObject* obj = ParseLine (line); |
120 LDObject* obj = ParseLine(line); |
121 |
121 |
122 // Check for parse errors and warn about them |
122 // Check for parse errors and warn about them |
123 if (obj->type() == OBJ_Error) |
123 if (obj->type() == OBJ_Error) |
124 { |
124 { |
125 print ("Couldn't parse line #%1: %2", progress() + 1, static_cast<LDError*> (obj)->reason()); |
125 print("Couldn't parse line #%1: %2", progress() + 1, static_cast<LDError*>(obj)->reason()); |
126 ++m_warningCount; |
126 ++m_warningCount; |
127 } |
127 } |
128 |
128 |
129 m_objects << obj; |
129 m_objects << obj; |
130 } |
130 } |
131 |
131 |
132 m_progress = i; |
132 m_progress = i; |
133 |
133 |
134 if (m_progressDialog) |
134 if (m_progressDialog) |
135 m_progressDialog->setProgress (i); |
135 m_progressDialog->setProgress(i); |
136 |
136 |
137 if (i >= m_lines.size() - 1) |
137 if (i >= m_lines.size() - 1) |
138 { |
138 { |
139 emit workDone(); |
139 emit workDone(); |
140 m_isDone = true; |
140 m_isDone = true; |
143 { |
143 { |
144 // If we have a dialog to show progress output to, we cannot just call work() again immediately as the dialog |
144 // If we have a dialog to show progress output to, we cannot just call work() again immediately as the dialog |
145 // needs to be updated as well. Thus, we take a detour through the event loop by using the meta-object system. |
145 // needs to be updated as well. Thus, we take a detour through the event loop by using the meta-object system. |
146 // |
146 // |
147 // This terminates the loop here and control goes back to the function which called the file loader. It will |
147 // This terminates the loop here and control goes back to the function which called the file loader. It will |
148 // keep processing the event loop until we're ready (see loadFileContents), thus the event loop will eventually |
148 // keep processing the event loop until we're ready(see loadFileContents), thus the event loop will eventually |
149 // catch the invokation we throw here and send us back. |
149 // catch the invokation we throw here and send us back. |
150 if (isOnForeground()) |
150 if (isOnForeground()) |
151 QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection, Q_ARG (int, i)); |
151 QMetaObject::invokeMethod(this, "work", Qt::QueuedConnection, Q_ARG(int, i)); |
152 else |
152 else |
153 work (i); |
153 work(i); |
154 } |
154 } |
155 } |
155 } |
156 |
156 |
157 void DocumentLoader::abort() |
157 void DocumentLoader::abort() |
158 { |
158 { |