Implemented support for opening files from the Mac OS X Finder, e.g. double-click, file type association

git-svn-id: http://svn.clifford.at/openscad/trunk@164 b57f626f-c46c-0410-a088-ec61d464b74c
stl_dim
kintel 2009-12-12 21:27:54 +00:00
parent 44357696ed
commit 6e32acbe65
6 changed files with 106 additions and 35 deletions

28
EventFilter.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef FILTER_H_
#define FILTER_H_
#include <QObject>
#include <QFileOpenEvent>
#include "MainWindow.h"
class EventFilter : public QObject
{
Q_OBJECT;
public:
EventFilter(QObject *parent) : QObject(parent) {}
protected:
bool eventFilter(QObject *obj, QEvent *event) {
// Handle Apple event for opening files
if (event->type() == QEvent::FileOpen) {
QFileOpenEvent *foe = static_cast<QFileOpenEvent *>(event);
MainWindow::requestOpenFile(foe->file());
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}
};
#endif

35
Info.plist Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>CFBundleIconFile</key>
<string>@ICON@</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleExecutable</key>
<string>OpenSCAD</string>
<key>CFBundleIdentifier</key>
<string>org.openscad.OpenSCAD</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>scad</string>
</array>
<key>CFBundleTypeMIMETypes</key>
<array>
<string>text/plain</string>
</array>
<key>CFBundleTypeName</key>
<string>OpenSCAD Model</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSIsAppleDefaultForType</key>
<true/>
</dict>
</array>
</dict>
</plist>

View File

@ -11,6 +11,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindow
public:
static QPointer<MainWindow> current_win;
static void requestOpenFile(const QString &filename);
QString filename;
class Highlighter *highlighter;
@ -46,6 +47,7 @@ private slots:
void updateTVal();
private:
void openFile(const QString &filename);
void load();
void maybe_change_dir();
void find_root_tag(AbstractNode *n);

View File

@ -253,6 +253,32 @@ MainWindow::~MainWindow()
#endif
}
/*!
Requests to open a file from an external event, e.g. by double-clicking a filename.
*/
void MainWindow::requestOpenFile(const QString &filename)
{
#ifdef ENABLE_MDI
new MainWindow(filename.toUtf8());
#endif
}
void
MainWindow::openFile(const QString &new_filename)
{
#ifdef ENABLE_MDI
if (!editor->toPlainText().isEmpty()) {
new MainWindow(new_filename.toUtf8());
current_win = NULL;
return;
}
#endif
filename = new_filename;
maybe_change_dir();
setWindowTitle(filename);
load();
}
void MainWindow::updatedFps()
{
bool fps_ok;
@ -287,7 +313,7 @@ void MainWindow::load()
if (!filename.isEmpty())
{
QString text;
FILE *fp = fopen(filename.toAscii().data(), "rt");
FILE *fp = fopen(filename.toUtf8(), "rt");
if (!fp) {
PRINTA("Failed to open file: %1 (%2)", filename, QString(strerror(errno)));
} else {
@ -529,43 +555,16 @@ void MainWindow::actionNew()
void MainWindow::actionOpen()
{
current_win = this;
QString new_filename = QFileDialog::getOpenFileName(this, "Open File", "", "OpenSCAD Designs (*.scad)");
if (!new_filename.isEmpty())
{
#ifdef ENABLE_MDI
if (!editor->toPlainText().isEmpty()) {
new MainWindow(new_filename.toAscii().data());
current_win = NULL;
return;
}
#endif
filename = new_filename;
maybe_change_dir();
setWindowTitle(filename);
QString text;
FILE *fp = fopen(filename.toAscii().data(), "rt");
if (!fp) {
PRINTA("Failed to open file: %1 (%2)", QString(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);
PRINTA("Loaded design `%1'.", QString(filename));
}
editor->setPlainText(text);
}
QString new_filename = QFileDialog::getOpenFileName(this, "Open File", "",
"OpenSCAD Designs (*.scad)");
if (!new_filename.isEmpty()) openFile(new_filename);
current_win = NULL;
}
void MainWindow::actionSave()
{
current_win = this;
FILE *fp = fopen(filename.toAscii().data(), "wt");
FILE *fp = fopen(filename.toUtf8(), "wt");
if (!fp) {
PRINTA("Failed to open file for writing: %1 (%2)", QString(filename), QString(strerror(errno)));
} else {
@ -1310,7 +1309,7 @@ void MainWindow::dropEvent(QDropEvent *event)
QString fn = urls[i].path();
#ifdef ENABLE_MDI
if (!editor->toPlainText().isEmpty()) {
new MainWindow(fn.toAscii().data());
new MainWindow(fn.toUtf8());
break;
}
#endif

View File

@ -28,6 +28,9 @@
#include <QDir>
#include <QSet>
#include <getopt.h>
#ifdef Q_WS_MAC
#include "EventFilter.h"
#endif
static void help(const char *progname)
{
@ -72,8 +75,9 @@ int main(int argc, char **argv)
bool useGUI = true;
#endif
QApplication app(argc, argv, useGUI);
#ifdef __APPLE__
#ifdef Q_WS_MAC
app.setLibraryPaths(QStringList(app.applicationDirPath() + "/../PlugIns"));
app.installEventFilter(new EventFilter(&app));
#endif
const char *filename = NULL;

View File

@ -2,6 +2,7 @@
macx {
TARGET = OpenSCAD
ICON = OpenSCAD.icns
QMAKE_INFO_PLIST = Info.plist
}
else {
TARGET = openscad
@ -16,7 +17,7 @@ QMAKE_CXXFLAGS_RELEASE = -O3 -march=pentium
QMAKE_CXXFLAGS_DEBUG = -O0 -ggdb
# MDI needs an OpenCSG library that is compiled with OpenCSG-Reset-Hack.patch applied
# DEFINES += ENABLE_MDI
DEFINES += ENABLE_MDI
DEFINES += ENABLE_CGAL
LIBS += -lCGAL
@ -47,6 +48,8 @@ HEADERS += openscad.h \
GLView.h \
printutils.h
macx: HEADERS += EventFilter.h
SOURCES += openscad.cc mainwin.cc glview.cc export.cc \
value.cc expr.cc func.cc module.cc context.cc \
csgterm.cc polyset.cc csgops.cc transform.cc \