summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MainWindow.h9
-rw-r--r--MainWindow.ui28
-rw-r--r--mainwin.cc71
3 files changed, 68 insertions, 40 deletions
diff --git a/MainWindow.h b/MainWindow.h
index c02162a..fd5aa7f 100644
--- a/MainWindow.h
+++ b/MainWindow.h
@@ -20,8 +20,10 @@ public:
double tval, fps, fsteps;
Context root_ctx;
- AbstractModule *root_module;
- AbstractNode *absolute_root_node;
+ AbstractModule *root_module; // Result of parsing
+ AbstractNode *absolute_root_node; // Result of tree evaluation
+ AbstractNode *root_node; // Root if the root modifier (!) is used
+
CSGTerm *root_raw_term;
CSGTerm *root_norm_term;
CSGChain *root_chain;
@@ -36,7 +38,6 @@ public:
CSGChain *highlights_chain;
QVector<CSGTerm*> background_terms;
CSGChain *background_chain;
- AbstractNode *root_node;
QString last_compiled_doc;
bool enableOpenCSG;
@@ -57,7 +58,7 @@ private slots:
private:
void openFile(const QString &filename);
void load();
- void find_root_tag(AbstractNode *n);
+ AbstractNode *find_root_tag(AbstractNode *n);
void compile(bool procevents);
bool maybeSave();
diff --git a/MainWindow.ui b/MainWindow.ui
index 0a296a4..3cb8d30 100644
--- a/MainWindow.ui
+++ b/MainWindow.ui
@@ -98,7 +98,7 @@
<x>0</x>
<y>0</y>
<width>681</width>
- <height>27</height>
+ <height>22</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
@@ -113,6 +113,8 @@
<addaction name="fileActionNew"/>
<addaction name="fileActionOpen"/>
<addaction name="menuOpenRecent"/>
+ <addaction name="separator"/>
+ <addaction name="fileActionClose"/>
<addaction name="fileActionSave"/>
<addaction name="fileActionSaveAs"/>
<addaction name="fileActionReload"/>
@@ -594,6 +596,14 @@
<string>Export as DXF...</string>
</property>
</action>
+ <action name="fileActionClose">
+ <property name="text">
+ <string>Close</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+W</string>
+ </property>
+ </action>
</widget>
<customwidgets>
<customwidget>
@@ -621,5 +631,21 @@
</hint>
</hints>
</connection>
+ <connection>
+ <sender>fileActionClose</sender>
+ <signal>triggered()</signal>
+ <receiver>MainWindow</receiver>
+ <slot>close()</slot>
+ <hints>
+ <hint type="sourcelabel">
+ <x>-1</x>
+ <y>-1</y>
+ </hint>
+ <hint type="destinationlabel">
+ <x>340</x>
+ <y>323</y>
+ </hint>
+ </hints>
+ </connection>
</connections>
</ui>
diff --git a/mainwin.cc b/mainwin.cc
index cb733a8..55a001d 100644
--- a/mainwin.cc
+++ b/mainwin.cc
@@ -35,6 +35,7 @@
#include <QVBoxLayout>
#include <QLabel>
#include <QFileInfo>
+#include <QTextStream>
#include <QStatusBar>
#include <QDropEvent>
#include <QMimeData>
@@ -341,6 +342,7 @@ MainWindow::setFileName(const QString &filename)
// Check that the canonical file path exists - only update recent files
// if it does. Should prevent empty list items on initial open etc.
if (!infoFileName.isEmpty()) {
+ this->fileName = infoFileName;
QSettings settings; // already set up properly via main.cpp
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(this->fileName);
@@ -348,7 +350,6 @@ MainWindow::setFileName(const QString &filename)
while (files.size() > maxRecentFiles)
files.removeLast();
settings.setValue("recentFileList", files);
- this->fileName = infoFileName;
} else {
this->fileName = fileinfo.fileName();
}
@@ -397,34 +398,26 @@ void MainWindow::load()
{
current_win = this;
if (!this->fileName.isEmpty()) {
- QString text;
- FILE *fp = fopen(this->fileName.toUtf8(), "rt");
- if (!fp) {
- PRINTA("Failed to open file: %1 (%2)", this->fileName, QString(strerror(errno)));
- } else {
- char buffer[513];
- int rc;
- while ((rc = fread(buffer, 1, 512, fp)) > 0) {
- buffer[rc] = 0;
- text += buffer;
- }
- fclose(fp);
+ QFile file(this->fileName);
+ if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ PRINTA("Failed to open file: %1 (%2)", this->fileName, file.errorString());
+ }
+ else {
+ QString text = QTextStream(&file).readAll();
PRINTA("Loaded design `%1'.", this->fileName);
+ editor->setPlainText(text);
}
- editor->setPlainText(text);
}
current_win = this;
}
-void MainWindow::find_root_tag(AbstractNode *n)
+AbstractNode *MainWindow::find_root_tag(AbstractNode *n)
{
foreach(AbstractNode *v, n->children) {
- if (v->modinst->tag_root)
- root_node = v;
- if (root_node)
- return;
- find_root_tag(v);
+ if (v->modinst->tag_root) return v;
+ return find_root_tag(v);
}
+ return NULL;
}
void MainWindow::compile(bool procevents)
@@ -433,6 +426,8 @@ void MainWindow::compile(bool procevents)
if (procevents)
QApplication::processEvents();
+
+ // Remove previous CSG tree
if (root_module) {
delete root_module;
root_module = NULL;
@@ -477,6 +472,7 @@ void MainWindow::compile(bool procevents)
root_node = NULL;
enableOpenCSG = false;
+ // Initialize special variables
root_ctx.set_variable("$t", Value(e_tval->text().toDouble()));
Value vpt;
@@ -493,9 +489,11 @@ void MainWindow::compile(bool procevents)
vpr.vec.append(new Value(fmodf(360 - screen->object_rot_z, 360)));
root_ctx.set_variable("$vpr", vpr);
+ // Parse
last_compiled_doc = editor->toPlainText();
root_module = parse((last_compiled_doc + "\n" + commandline_commands).toAscii().data(), false);
+ // Error highlighting
if (highlighter) {
delete highlighter;
highlighter = NULL;
@@ -513,6 +511,7 @@ void MainWindow::compile(bool procevents)
goto fail;
}
+ // Evaluate CSG tree
PRINT("Compiling design (CSG Tree generation)...");
if (procevents)
QApplication::processEvents();
@@ -526,9 +525,10 @@ void MainWindow::compile(bool procevents)
if (!absolute_root_node)
goto fail;
- find_root_tag(absolute_root_node);
- if (!root_node)
- root_node = absolute_root_node;
+ // Do we have an explicit root node (! modifier)?
+ if (!(this->root_node = find_root_tag(absolute_root_node))) {
+ this->root_node = absolute_root_node;
+ }
root_node->dump("");
PRINT("Compiling design (CSG Products generation)...");
@@ -700,12 +700,12 @@ void MainWindow::actionSave()
}
else {
current_win = this;
- FILE *fp = fopen(this->fileName.toUtf8(), "wt");
- if (!fp) {
- PRINTA("Failed to open file for writing: %1 (%2)", this->fileName, QString(strerror(errno)));
- } else {
- fprintf(fp, "%s", editor->toPlainText().toAscii().data());
- fclose(fp);
+ QFile file(this->fileName);
+ if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
+ PRINTA("Failed to open file for writing: %1 (%2)", this->fileName, file.errorString());
+ }
+ else {
+ QTextStream(&file) << this->editor->toPlainText();
PRINTA("Saved design `%1'.", this->fileName);
this->editor->document()->setModified(false);
}
@@ -868,14 +868,11 @@ void MainWindow::actionCompile()
compile(!viewActionAnimate->isChecked());
-#ifdef ENABLE_OPENCSG
- if (!(viewActionOpenCSG->isVisible() && viewActionOpenCSG->isChecked()) &&
- !viewActionThrownTogether->isChecked()) {
+ // Go to non-CGAL view mode
+ if (!viewActionOpenCSG->isChecked() && !viewActionThrownTogether->isChecked()) {
viewModeOpenCSG();
}
- else
-#endif
- {
+ else {
screen->updateGL();
}
current_win = NULL;
@@ -1245,6 +1242,10 @@ static void renderGLviaOpenCSG(void *vp)
}
}
+/*!
+ Go to the OpenCSG view mode.
+ Falls back to thrown together mode if OpenCSG is not available
+*/
void MainWindow::viewModeOpenCSG()
{
if (screen->opencsg_support) {
contact: Jan Huwald // Impressum