Skip to content

Commit

Permalink
Feature/open external (#126)
Browse files Browse the repository at this point in the history
* Added functionality to open the current model in an external program such as a slicer

* Added a check for absolute path

* Added ~ expand for filename provided via argv

* Fixed indents

Fixed indents

Fixed indents

Fixed indents

* Added a file dialog if the default option externalCmd is not set
  • Loading branch information
7aske authored Jan 22, 2025
1 parent 2fd910d commit c61b4d1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/app.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
#include <QDebug>
#include <QFileOpenEvent>
#include <QDir>

#include "app.h"
#include "window.h"

App::App(int& argc, char *argv[]) :
QApplication(argc, argv), window(new Window())
{
if (argc > 1)
window->load_stl(argv[1]);
if (argc > 1) {
QString filename = argv[1];
if (filename.startsWith("~")) {
filename.replace(0, 1, QDir::homePath());
}
window->load_stl(filename);
}
else
{
window->load_stl(":gl/sphere.stl");
}
window->show();
}

Expand Down
40 changes: 40 additions & 0 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "loader.h"
#include "shaderlightprefs.h"

const QString Window::OPEN_EXTERNAL_KEY = "externalCmd";
const QString Window::RECENT_FILE_KEY = "recentFiles";
const QString Window::INVERT_ZOOM_KEY = "invertZoom";
const QString Window::AUTORELOAD_KEY = "autoreload";
Expand All @@ -17,6 +18,7 @@ const QString Window::RESET_TRANSFORM_ON_LOAD_KEY = "resetTransformOnLoad";
Window::Window(QWidget *parent) :
QMainWindow(parent),
open_action(new QAction("Open", this)),
open_external_action(new QAction("Open with", this)),
about_action(new QAction("About", this)),
quit_action(new QAction("Quit", this)),
perspective_action(new QAction("Perspective", this)),
Expand Down Expand Up @@ -75,6 +77,11 @@ Window::Window(QWidget *parent) :
this, &Window::on_open);
this->addAction(open_action);

open_external_action->setShortcut(QKeySequence::Open);
QObject::connect(open_external_action, &QAction::triggered, this, &Window::on_open_external);
this->addAction(open_external_action);
open_external_action->setShortcut(QKeySequence(Qt::ALT + Qt::Key_S));

QList<QKeySequence> quitShortcuts = { QKeySequence::Quit, QKeySequence::Close };
quit_action->setShortcuts(quitShortcuts);
QObject::connect(quit_action, &QAction::triggered,
Expand Down Expand Up @@ -106,6 +113,7 @@ Window::Window(QWidget *parent) :

const auto file_menu = menuBar()->addMenu("File");
file_menu->addAction(open_action);
file_menu->addAction(open_external_action);
file_menu->addMenu(recent_files);
file_menu->addSeparator();
file_menu->addAction(reload_action);
Expand Down Expand Up @@ -234,6 +242,15 @@ void Window::load_persist_settings(){
orthographic_action->setChecked(true);
}

QString path = settings.value(OPEN_EXTERNAL_KEY, "").toString();
if (!QDir::isAbsolutePath(path) && !path.isEmpty())
{
path = QStandardPaths::findExecutable(path);
}
QString displayName = path.mid(path.lastIndexOf(QDir::separator()) + 1);
open_external_action->setText("Open with " + displayName);
open_external_action->setData(path);

DrawMode draw_mode = (DrawMode)settings.value(DRAW_MODE_KEY, DRAWMODECOUNT).toInt();

if(draw_mode >= DRAWMODECOUNT)
Expand Down Expand Up @@ -269,6 +286,29 @@ void Window::on_open()
}
}

void Window::on_open_external() const
{
if (current_file.isEmpty())
{
return;
}


QString program = open_external_action->data().toString();
if (program.isEmpty()) {
program = QFileDialog::getOpenFileName((QWidget*) this, "Select program to open with", QDir::rootPath());
if (!program.isEmpty()) {
QSettings settings;
settings.setValue(OPEN_EXTERNAL_KEY, program);
QString displayName = program.mid(program.lastIndexOf(QDir::separator()) + 1);
open_external_action->setText("Open with " + displayName);
open_external_action->setData(program);
}
}

QProcess::startDetached(program, QStringList(current_file));
}

void Window::on_about()
{
QMessageBox::about(this, "",
Expand Down
3 changes: 3 additions & 0 deletions src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Window : public QMainWindow

public slots:
void on_open();
void on_open_external() const;
void on_about();
void on_bad_stl();
void on_empty_mesh();
Expand Down Expand Up @@ -63,6 +64,7 @@ private slots:
QPair<QString, QString> get_file_neighbors();

QAction* const open_action;
QAction* const open_external_action;
QAction* const about_action;
QAction* const quit_action;
QAction* const perspective_action;
Expand Down Expand Up @@ -93,6 +95,7 @@ private slots:
QActionGroup* const recent_files_group;
QAction* const recent_files_clear_action;
const static int MAX_RECENT_FILES=8;
static const QString OPEN_EXTERNAL_KEY;
const static QString RECENT_FILE_KEY;
const static QString INVERT_ZOOM_KEY;
const static QString AUTORELOAD_KEY;
Expand Down

0 comments on commit c61b4d1

Please sign in to comment.