From e628183e64f35f3c17b6d47bc0f08b3f55a07c18 Mon Sep 17 00:00:00 2001 From: Himanshuu23 <138672759+Himanshuu23@users.noreply.github.com> Date: Sat, 23 Mar 2024 19:17:35 +0530 Subject: [PATCH] Update pdbformat.cpp Added the feature to drag and drop pdb files Signed-off-by: Himanshuu23 <138672759+Himanshuu23@users.noreply.github.com> --- avogadro/io/pdbformat.cpp | 106 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/avogadro/io/pdbformat.cpp b/avogadro/io/pdbformat.cpp index db6ec85bcb..41173e18f1 100644 --- a/avogadro/io/pdbformat.cpp +++ b/avogadro/io/pdbformat.cpp @@ -8,6 +8,16 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -328,3 +338,99 @@ void PdbFormat::perceiveSubstitutedCations(Core::Molecule& molecule) } } // namespace Avogadro::Io + +// vector3 class representing 3D coordinates +class vector3 { +public: + double x, y, z; +}; + +// MainWin class +class MainWin : public QWidget { +public: + MainWin(QWidget *parent = nullptr) : QWidget(parent) { + setAcceptDrops(true); + + browseButton = new QPushButton("Browse", this); + browseButton->setGeometry(10, 10, 100, 30); + connect(browseButton, &QPushButton::clicked, this, &MainWin::browseFile); + } + +protected: + // Override drag enter event - to accept if they contain URLs + void dragEnterEvent(QDragEnterEvent *event) override { + if (event->mimeData()->hasUrls()) { + event->acceptProposedAction(); + } + } + + // Override drop event - to handle dropping of files + void dropEvent(QDropEvent *event) override { + const QMimeData *mimeData = event->mimeData(); + if (mimeData->hasUrls()) { + QList urlList = mimeData->urls(); + QString filename = urlList.at(0).toLocalFile(); + if (filename.endsWith(".pdb", Qt::CaseInsensitive)) { + readPDBFile(filename); + } + } + } + +private slots: + // Slot to handle browse button click + void browseFile() { + QString filename = QFileDialog::getOpenFileName(this, "Open PDB File", "", "PDB Files (*.pdb)"); + if (!filename.isEmpty()) { + readPDBFile(filename); + } + } + +private: + // Function to read PDB file + void readPDBFile(const QString &filename) { + std::ifstream file(filename.toStdString()); + if (!file.is_open()) { + qDebug() << "Failed to open file:" << filename; + return; + } + + std::string line; + std::vector frame; + while (std::getline(file, line)) { + if (line.find("ENDMDL") != std::string::npos) { + processFrame(frame); + frame.clear(); + } else if (line.find("ATOM") == 0 || line.find("HETATM") == 0) { + std::istringstream iss(line.substr(30, 24)); + vector3 coordinates; + iss >> coordinates.x >> coordinates.y >> coordinates.z; + frame.push_back(coordinates); + } + } + if (!frame.empty()) { + processFrame(frame); + } + }//storing the coordinates in frame vector + + void processFrame(const std::vector &frame) { + //printing the (x,y,z) coordinates + qDebug() << "New Frame:"; + for (const auto &coord : frame) { + qDebug() << "X : " << coord.x << "Y : " << coord.y << "Z : " << coord.z; + } + } + + QPushButton *browseButton; +}; + +int main(int argc, char *argv[]) { + //GUI window for drag and drop of pdb files + QApplication app(argc, argv); + + MainWindow mainWindow; + mainWindow.setWindowTitle("PDB File Reader"); + mainWindow.resize(400, 300); + mainWindow.show(); + + return app.exec(); +} //Qt application with a browse button