Skip to content

Commit

Permalink
Merge pull request #156 from ghutchis/drag-to-install-scripts
Browse files Browse the repository at this point in the history
Add a drag-and-drop to install scripts
  • Loading branch information
ghutchis authored Jun 1, 2021
2 parents 27bd57d + ee13919 commit 2896345
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
64 changes: 63 additions & 1 deletion avogadro/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
#include <avogadro/rendering/scene.h>

#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QMimeData>
#include <QtCore/QSettings>
#include <QtCore/QStandardPaths>
#include <QtCore/QString>
#include <QtCore/QThread>
#include <QtCore/QTimer>
Expand Down Expand Up @@ -437,7 +439,16 @@ void MainWindow::closeEvent(QCloseEvent* e)
if (event->mimeData()->hasUrls()) {
// TODO: check for ZIP, TAR, PY scripts (plugins)
foreach(const QUrl& url, event->mimeData()->urls() ) {
openFile(url.toLocalFile());
if (url.isLocalFile()) {
QString fileName = url.toLocalFile();
QFileInfo info(fileName);
QString extension = info.completeSuffix(); // e.g. .tar.gz or .pdb.gz

if (extension == "py")
addScript(fileName);
else
openFile(fileName);
}
}
event->acceptProposedAction();
}
Expand Down Expand Up @@ -633,6 +644,57 @@ void MainWindow::importFile()
}
}

bool MainWindow::addScript(const QString& filePath)
{
if (filePath.isEmpty()) {
return false;
}

// Ask the user what type of script this is
// TODO: add some sort of warning?
QStringList types;
types << tr("Commands") << tr("Input Generators") << tr("File Formats");

bool ok;
QString item = QInputDialog::getItem(this, tr("Install Plugin Script"),
tr("Script Type:"), types, 0, false, &ok);

if (!ok || item.isEmpty())
return false;

QString typePath;

int index = types.indexOf(item);
// don't translate these
switch(index){
case 0: // commands
typePath = "commands";
break;
case 1:
typePath = "inputGenerators";
break;
case 2:
typePath = "formatScripts";
break;
default:
typePath = "other";
}

QStringList stdPaths =
QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation);

QFileInfo info(filePath);

QString destinationPath(stdPaths[0] + '/' + typePath + '/' + info.fileName());
qDebug() << " copying " << filePath << " to " << destinationPath;
QFile::remove(destinationPath); // silently fail if there's nothing to remove
QFile::copy(filePath, destinationPath);

// TODO: Ask that type of plugin script to reload?

return true;
}

bool MainWindow::openFile(const QString& fileName, Io::FileFormat* reader)
{
if (fileName.isEmpty()) {
Expand Down
5 changes: 5 additions & 0 deletions avogadro/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public slots:
*/
bool openFile(const QString& fileName, Io::FileFormat* reader = nullptr);

/**
* Move @a fileName as a plugin script (i.e. put it in the correct dir)
*/
bool addScript(const QString& fileName);

#ifdef QTTESTING
void playTest(const QString& fileName, bool exit = true);
#endif
Expand Down

0 comments on commit 2896345

Please sign in to comment.