Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Drag-and-Drop Export Functionality in Avogadro #491

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ jobs:
# This will handover control about PR rejection to the GitHub side
max-allowed-issues: 2147483647

- name: Filter out MISRA
# Filter out MISRA and potentially reduce the number of runs in the SARIF output
- name: Filter and Reduce SARIF
run: |
pip install globber
python3 scripts/filter_sarif.py --input results.sarif --output filtered.sarif --split-lines -- "-**/*.*:cppcheck_misra*"
python3 scripts/filter_and_reduce_sarif.py --input results.sarif --output filtered_and_reduced.sarif
env:
PYTHONUNBUFFERED: 1

# Upload the SARIF file generated in the previous step

# Upload the SARIF file generated in the previous step
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: filtered.sarif
sarif_file: filtered_and_reduced.sarif
113 changes: 102 additions & 11 deletions avogadro/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <avogadro/rendering/glrenderer.h>
#include <avogadro/rendering/scene.h>

#include <QMouseEvent>
#include <QOpenGLFramebufferObject>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
Expand All @@ -50,8 +52,6 @@
#include <QtCore/QString>
#include <QtCore/QThread>
#include <QtCore/QTimer>

#include <QOpenGLFramebufferObject>
#include <QtGui/QClipboard>
#include <QtGui/QCloseEvent>
#include <QtGui/QDesktopServices>
Expand Down Expand Up @@ -228,6 +228,8 @@ using VTK::vtkGLWidget;
#endif

MainWindow::MainWindow(const QStringList& fileNames, bool disableSettings)
: QMainWindow(/* parent */)
, dragStartPosition()
: m_molecule(nullptr)
, m_rwMolecule(nullptr)
, m_moleculeModel(nullptr)
Expand Down Expand Up @@ -357,6 +359,50 @@ MainWindow::~MainWindow()
delete m_viewFactory;
}

void MainWindow::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
dragStartPosition = event->pos();
}

void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - dragStartPosition).manhattanLength() <
QApplication::startDragDistance())
return;

performDrag();
}

void MainWindow::performDrag()
{
// Assuming you have a method to get the currently selected molecule as a
// string in a specific format
QString moleculeData = /* method to get molecule data */;
if (moleculeData.isEmpty())
return;

auto* mimeData = new QMimeData;
mimeData->setText(moleculeData); // For demonstration, using plain text.
// Adjust based on actual data format.

// Create a drag object
auto* drag = new QDrag(this);
drag->setMimeData(mimeData);

// You can set an appropriate pixmap for the drag object if you like
// QPixmap pixmap(iconSize);
// QPainter painter(&pixmap);
// painter.drawPixmap(QPoint(), /* your pixmap here */);
// painter.end();
// drag->setPixmap(pixmap);

// Execute the drag operation
drag->exec(Qt::CopyAction | Qt::MoveAction);
}

void MainWindow::setupInterface()
{
// We take care of setting up the main interface here, along with any custom
Expand Down Expand Up @@ -522,26 +568,28 @@ void MainWindow::dragEnterEvent(QDragEnterEvent* event)
else
event->ignore();
}

void MainWindow::dropEvent(QDropEvent* event)
{
if (event->mimeData()->hasUrls()) {
// TODO: check for ZIP, TAR, PY scripts (plugins)
foreach (const QUrl& url, event->mimeData()->urls()) {
QList<QUrl> urls = event->mimeData()->urls();
for (const QUrl& url : urls) {
if (url.isLocalFile()) {
QString fileName = url.toLocalFile();
QFileInfo info(fileName);
QString extension = info.completeSuffix(); // e.g. .tar.gz or .pdb.gz

if (extension == "py")
// Distinguish Python scripts for special handling
if (info.suffix().compare("py", Qt::CaseInsensitive) == 0) {
addScript(fileName);
else
} else {

openFile(fileName);
}
}
}
event->acceptProposedAction();
} else
} else {
event->ignore();
}
}

void MainWindow::moleculeReady(int)
Expand Down Expand Up @@ -577,7 +625,7 @@ void setDefaultViews(MultiViewWidget* viewWidget)
bool anyPluginTrue = false;
// load plugins normally, if all non-ignore are false.
// restore the default behavior
for (auto plugin : sceneModel->scenePlugins()) {
for (ScenePlugin* plugin : sceneModel->scenePlugins()) {
QString settingsKey("MainWindow/" + plugin->objectName());
bool enabled = settings.value(settingsKey, plugin->isEnabled()).toBool();
if (plugin->defaultBehavior() != ScenePlugin::DefaultBehavior::Ignore &&
Expand Down Expand Up @@ -1497,6 +1545,9 @@ bool MainWindow::saveFileAs(bool async)
QFileDialog saveDialog(this, tr("Save chemical file"), dir, filter);
saveDialog.setAcceptMode(QFileDialog::AcceptSave);
saveDialog.exec();
if (saveDialog.selectedFiles().isEmpty()) // user cancel
return false;

QString fileName = saveDialog.selectedFiles().first();

if (fileName.isEmpty()) // user cancel
Expand Down Expand Up @@ -1976,6 +2027,46 @@ void MainWindow::buildMenu()
m_menuBuilder->addAction(path, action, 960);
m_fileToolBar->addAction(action);
connect(action, SIGNAL(triggered()), SLOT(saveFileAs()));
// Initialize autosave feature
m_autosaveInterval = 5; // Autosave interval in minutes
m_autosaveTimer = new QTimer(this);
connect(m_autosaveTimer, &QTimer::timeout, this,
&MainWindow::autosaveDocument);
m_autosaveTimer->start(m_autosaveInterval *
60000); // Convert minutes to milliseconds

void MainWindow::autosaveDocument()
{
if (!m_molecule || !m_moleculeDirty) {
return; // No molecule loaded or no changes made since the last save.
}

QString autosaveDirPath =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
"/autosave";
QDir autosaveDir(autosaveDirPath);
if (!autosaveDir.exists()) {
autosaveDir.mkpath(".");
}

// Construct autosave file name
QString autosaveFilename;
if (m_molecule->hasData("fileName")) {
QFileInfo fileInfo(m_molecule->data("fileName").toString().c_str());
autosaveFilename = fileInfo.baseName() + "_autosave.cjson";
} else {
autosaveFilename = "unsaved_autosave.cjson";
}
QString autosaveFilePath = autosaveDirPath + "/" + autosaveFilename;

// Use CJSON format for autosaving
Io::CjsonFormat writer;
if (!writer.writeFile(autosaveFilePath, *m_molecule)) {
qWarning() << "Failed to autosave the document to" << autosaveFilePath;
} else {
qDebug() << "Document autosaved to" << autosaveFilePath;
}
}

// Export action for menu
QStringList exportPath = path;
Expand Down Expand Up @@ -2632,4 +2723,4 @@ bool MainWindow::handleCommand(const QString& command,
return false;
}

} // End of Avogadro namespace
} // End of Avogadro namespace
15 changes: 11 additions & 4 deletions avogadro/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef AVOGADRO_MAINWINDOW_H
#define AVOGADRO_MAINWINDOW_H

#include <QDrag>
#include <QMimeData>
#include <QtCore/QStringList>
#include <QtCore/QVariantMap>
#include <QtWidgets/QMainWindow>
Expand Down Expand Up @@ -69,7 +71,7 @@ class MainWindow : public QMainWindow

public slots:
void setMolecule(Avogadro::QtGui::Molecule* molecule);

void autosaveDocument(); // line to declare the autosave slot
/**
* Update internal state to reflect that the molecule has been modified.
*/
Expand Down Expand Up @@ -161,6 +163,9 @@ public slots:
void moleculeChanged(QtGui::Molecule* molecue);

protected:
void mousePressEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;

void closeEvent(QCloseEvent* event);

// Handle drag and drop -- accept files dragged on the window
Expand Down Expand Up @@ -391,14 +396,16 @@ private slots:
void setProjectionPerspective();

private:
QPoint dragStartPosition; // To store the start position of a drag operation
QtGui::Molecule* m_molecule;
QtGui::RWMolecule* m_rwMolecule;
QtGui::MoleculeModel* m_moleculeModel;
QtGui::LayerModel* m_layerModel;
QtGui::ScenePlugin* m_activeScenePlugin;
bool m_queuedFilesStarted;
QStringList m_queuedFiles;

QTimer* m_autosaveTimer; // for the autosave timer
int m_autosaveInterval; // for autosave interval in minutes
QStringList m_recentFiles;
QList<QAction*> m_actionRecentFiles;

Expand Down Expand Up @@ -478,7 +485,7 @@ private slots:
* Initialize the tool plugins.
*/
void buildTools();

void performDrag();
/**
* Convenience function that converts a file extension to a wildcard
* expression, e.g. "out" to "*.out". This method also checks for "extensions"
Expand All @@ -502,4 +509,4 @@ private slots:

} // End Avogadro namespace

#endif
#endif
7 changes: 4 additions & 3 deletions i18n/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ msgstr ""
"Project-Id-Version: _avogadro-de\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2023-12-11 21:00+0000\n"
"PO-Revision-Date: 2024-01-23 16:01+0000\n"
"PO-Revision-Date: 2024-02-19 19:02+0000\n"
"Last-Translator: Norwid Behrnd <[email protected]>\n"
"Language-Team: German <https://hosted.weblate.org/projects/avogadro/"
"avogadroapp/de/>\n"
Expand All @@ -23,7 +23,7 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.4-dev\n"
"X-Generator: Weblate 5.5-dev\n"
"X-Launchpad-Export-Date: 2018-04-13 16:02+0000\n"

#. i18n: file: aboutdialog.ui:62
Expand Down Expand Up @@ -591,8 +591,9 @@ msgstr "Dateien können nicht geöffnet werden"
#. i18n: file: mainwindow.ui:49
#. i18n: ectx: property (title), widget (QMenu, menuBuild)
#: menubuilder.cpp:79:45
#, fuzzy
msgid "&Build"
msgstr "&Erstellen"
msgstr "S&truktur"

#. i18n: file: mainwindow.ui:71
#. i18n: ectx: property (title), widget (QMenu, menuSelect)
Expand Down