diff options
| -rw-r--r-- | EventFilter.h | 28 | ||||
| -rw-r--r-- | Info.plist | 35 | ||||
| -rw-r--r-- | MainWindow.h | 2 | ||||
| -rw-r--r-- | mainwin.cc | 65 | ||||
| -rw-r--r-- | openscad.cc | 6 | ||||
| -rw-r--r-- | openscad.pro | 5 | 
6 files changed, 106 insertions, 35 deletions
| diff --git a/EventFilter.h b/EventFilter.h new file mode 100644 index 0000000..c3942b5 --- /dev/null +++ b/EventFilter.h @@ -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 diff --git a/Info.plist b/Info.plist new file mode 100644 index 0000000..5dadad9 --- /dev/null +++ b/Info.plist @@ -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> diff --git a/MainWindow.h b/MainWindow.h index 39609cd..f89809a 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -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); @@ -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 diff --git a/openscad.cc b/openscad.cc index 4096ed2..37875df 100644 --- a/openscad.cc +++ b/openscad.cc @@ -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; diff --git a/openscad.pro b/openscad.pro index 46866d3..23e076e 100644 --- a/openscad.pro +++ b/openscad.pro @@ -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 \ | 
