diff --git a/avogadro/mainwindow.cpp b/avogadro/mainwindow.cpp index 751e02d7..e6ee6569 100644 --- a/avogadro/mainwindow.cpp +++ b/avogadro/mainwindow.cpp @@ -50,7 +50,7 @@ #include #include #include - +#include #include #include #include @@ -228,6 +228,7 @@ using VTK::vtkGLWidget; #endif MainWindow::MainWindow(const QStringList& fileNames, bool disableSettings) + : QMainWindow(/* parent */), dragStartPosition() : m_molecule(nullptr) , m_rwMolecule(nullptr) , m_moleculeModel(nullptr) @@ -357,6 +358,47 @@ 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 @@ -524,30 +566,29 @@ void MainWindow::dragEnterEvent(QDragEnterEvent* event) } void MainWindow::dropEvent(QDropEvent* event) { - const QMimeData* mimeData = event->mimeData(); - - if (mimeData->hasUrls()) { - QList urlList = mimeData->urls(); - . - for (int i = 0; i < urlList.size() && i < 10; ++i) { - QString filePath = urlList.at(i).toLocalFile(); - openFile(filePath); + if (event->mimeData()->hasUrls()) { + QList urls = event->mimeData()->urls(); + for (const QUrl &url : urls) { + if (url.isLocalFile()) { + QString fileName = url.toLocalFile(); + QFileInfo info(fileName); + + // Distinguish Python scripts for special handling + if (info.suffix().compare("py", Qt::CaseInsensitive) == 0) { + addScript(fileName); + } else { + + openFile(fileName); + } + } } event->acceptProposedAction(); - } - - else if (mimeData->hasText()) { - - QString textData = mimeData->text(); - - event->acceptProposedAction(); - } - - else { + } else { event->ignore(); } } + void MainWindow::moleculeReady(int) { auto* extension = qobject_cast(sender()); diff --git a/avogadro/mainwindow.h b/avogadro/mainwindow.h index 3dadf613..2bcc801f 100644 --- a/avogadro/mainwindow.h +++ b/avogadro/mainwindow.h @@ -9,6 +9,8 @@ #include #include #include +#include +#include #ifdef QTTESTING class pqTestUtility; @@ -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 @@ -391,6 +396,7 @@ 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; @@ -478,7 +484,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"