| 1 |
#ifndef PQRREQUEST_H |
| 2 |
#define PQRREQUEST_H |
| 3 |
|
| 4 |
#include <QtNetwork/QNetworkAccessManager> |
| 5 |
#include <QtNetwork/QNetworkReply> |
| 6 |
#include <QtNetwork/QNetworkRequest> |
| 7 |
|
| 8 |
#include <QtCore/QDateTime> |
| 9 |
#include <QtCore/QDir> |
| 10 |
#include <QtCore/QFile> |
| 11 |
#include <QtCore/QSize> |
| 12 |
#include <QtCore/QUrl> |
| 13 |
#include <QtCore/QVariantMap> |
| 14 |
|
| 15 |
#include <QtWidgets/QLabel> |
| 16 |
#include <QtWidgets/QLineEdit> |
| 17 |
#include <QtWidgets/QTableWidget> |
| 18 |
#include <QtWidgets/QTableWidgetItem> |
| 19 |
|
| 20 |
#include <cctype> |
| 21 |
|
| 22 |
/** |
| 23 |
* @brief The PQRRequest class sends and receives network requests to PQR and |
| 24 |
* updates ui elements from the widget. |
| 25 |
*/ |
| 26 |
namespace Avogadro { |
| 27 |
namespace QtPlugins { |
| 28 |
class PQRWidget; |
| 29 |
|
| 30 |
class PQRRequest : public QObject |
| 31 |
{ |
| 32 |
Q_OBJECT |
| 33 |
|
| 34 |
public: |
| 35 |
/** |
| 36 |
* @brief Constructor to initialize the NetworkAccessManager and set pointers |
| 37 |
* to the widget's ui elements. |
| 38 |
* @param tw Pointer to ui's table widget |
| 39 |
* @param gv Pointer to ui's graphics view for SVG preview |
| 40 |
* @param nd Pointer to the name display |
| 41 |
* @param fd Pointer to the formula display |
| 42 |
*/ |
| 43 |
PQRRequest(QTableWidget*, QLabel*, QLineEdit*, QLabel*, PQRWidget*); |
| 44 |
|
| 45 |
/** |
| 46 |
* @brief Free the ui pointers |
| 47 |
*/ |
| 48 |
~PQRRequest() override; |
| 49 |
|
| 50 |
/** |
| 51 |
* @brief Sends a network request to search for molecules from PQR; |
| 52 |
* @param url The url to query |
| 53 |
*/ |
| 54 |
void sendRequest(QString); |
| 55 |
|
| 56 |
/** |
| 57 |
* @brief Sends a network request to download a file from PQR |
| 58 |
* @param url The url to send the request to |
| 59 |
* @param mol2 The mol2 representation of the molecule to download |
| 60 |
*/ |
| 61 |
void sendRequest(QString, QString); |
| 62 |
|
| 63 |
/** |
| 64 |
* @brief Sends a network request to download a png form PQR |
| 65 |
* @param url The url to send the request to |
| 66 |
*/ |
| 67 |
void sendPNGRequest(QString url); |
| 68 |
|
| 69 |
/** |
| 70 |
* @brief Called when a molecule is selected to display information about the |
| 71 |
* molecule and start grabbing the SVG preview. |
| 72 |
* @param num The row number of the table result selected |
| 73 |
* @returns The mol2 of the result for the widget to reference |
| 74 |
*/ |
| 75 |
QString molSelected(int); |
| 76 |
|
| 77 |
private slots: |
| 78 |
/** |
| 79 |
* @brief Parses the JSON response from querying PQR |
| 80 |
*/ |
| 81 |
void parseJson(); |
| 82 |
|
| 83 |
/** |
| 84 |
* @brief Creates a file after requesting a file from PQR |
| 85 |
*/ |
| 86 |
void getFile(); |
| 87 |
|
| 88 |
/** |
| 89 |
* @brief Loads PNG data after sending a request |
| 90 |
*/ |
| 91 |
void SetPNG(); |
| 92 |
|
| 93 |
private: |
| 94 |
/** |
| 95 |
* @brief The result struct holds all data received in each result from |
| 96 |
* querying PQR |
| 97 |
*/ |
| 98 |
struct result |
| 99 |
{ |
| 100 |
QString inchikey; |
| 101 |
QString name; |
| 102 |
QString mol2url; |
| 103 |
QString formula; |
| 104 |
float mass; |
| 105 |
|
| 106 |
// Default constructor |
| 107 |
result() |
| 108 |
: inchikey("Error"), name("Error"), mol2url("Error"), formula("Error"), |
| 109 |
mass(-1.0) |
| 110 |
{ |
| 111 |
} |
| 112 |
}; |
| 113 |
/** An array to hold all results from a query */ |
| 114 |
std::vector<result> results; |
| 115 |
|
| 116 |
/** Holds a reply from a network request */ |
| 117 |
QNetworkReply* reply; |
| 118 |
/** Used to send/receive network request */ |
| 119 |
QNetworkAccessManager* oNetworkAccessManager; |
| 120 |
/** Used to parse JSON results */ |
| 121 |
QVariantMap m_jsonResult; |
| 122 |
|
| 123 |
/** Pointer to dialog */ |
| 124 |
PQRWidget* widget; |
| 125 |
|
| 126 |
/** Pointers to a widget's ui elements */ |
| 127 |
QTableWidget* table; |
| 128 |
QLineEdit* nameDisplay; |
| 129 |
QLabel* formulaDisplay; |
| 130 |
QLabel* pngPreview; |
| 131 |
|
| 132 |
/** Variables to fold file download information for getFile() */ |
| 133 |
QString currentMolName; |
| 134 |
|
| 135 |
/** |
| 136 |
* @brief Takes a formula string and returns a QString with subscript tags |
| 137 |
* @param formula The formula string |
| 138 |
*/ |
| 139 |
QString parseSubscripts(QString); |
| 140 |
|
| 141 |
/** |
| 142 |
* @brief Takes a formula string and returns the molecular mass of the |
| 143 |
* molecule |
| 144 |
* @param formula The formula string |
| 145 |
*/ |
| 146 |
float getMolMass(QString); |
| 147 |
}; |
| 148 |
} // namespace QtPlugins |
| 149 |
} // namespace Avogadro |
| 150 |
#endif // PQRRequest_H |