Debian Codegraph @

avogadro/qtplugins/molecularproperties/molecularview.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "molecularview.h"
7
8 #include <avogadro/qtgui/molecule.h>
9
10 #include <QAction>
11 #include <QApplication>
12 #include <QtCore/QAbstractTableModel>
13 #include <QtCore/QDir>
14 #include <QtCore/QIODevice>
15 #include <QtCore/QString>
16 #include <QtCore/QTextStream>
17 #include <QtGui/QClipboard>
18 #include <QtGui/QContextMenuEvent>
19 #include <QtGui/QKeyEvent>
20 #include <QtWidgets/QMenu>
21
22 #include <QtWidgets/QDialog>
23 #include <QtWidgets/QFileDialog>
24 #include <QtWidgets/QHeaderView>
25 #include <QtWidgets/QMessageBox>
26 #include <QtWidgets/QScrollBar>
27 #include <QtWidgets/QSizePolicy>
28 #include <QtWidgets/QVBoxLayout>
29
30 #include <QtCore/QDebug>
31
32 namespace Avogadro {
33
34 using QtGui::Molecule;
35 using QtPlugins::MolecularModel;
36
37 MolecularView::MolecularView(QWidget* parent)
38 : QTableView(parent), m_molecule(nullptr), m_model(nullptr)
39 {
40 this->setWindowTitle(tr("Molecule Properties"));
41
42 QHeaderView* horizontal = this->horizontalHeader();
43 horizontal->setSectionResizeMode(QHeaderView::Interactive);
44 QHeaderView* vertical = this->verticalHeader();
45 vertical->setSectionResizeMode(QHeaderView::Interactive);
46
47 // You can select everything (e.g., to copy, select all, etc.)
48 setCornerButtonEnabled(true);
49 setSelectionBehavior(QAbstractItemView::SelectRows);
50 setSelectionMode(QAbstractItemView::ExtendedSelection);
51 // Alternating row colors
52 setAlternatingRowColors(true);
53 // Don't allow sorting the table
54 setSortingEnabled(false);
55 }
56
57 void MolecularView::selectionChanged(
58 [[maybe_unused]] const QItemSelection& selected, const QItemSelection&)
59 {
60 }
61
62 void MolecularView::setMolecule(Molecule* molecule)
63 {
64 m_molecule = molecule;
65 }
66
67 void MolecularView::keyPressEvent(QKeyEvent* event)
68 {
69 // handle copy event
70 // thanks to https://www.walletfox.com/course/qtableviewcopypaste.php
71 if (!event->matches(QKeySequence::Copy)) {
72 QTableView::keyPressEvent(event);
73 return;
74 }
75
76 // get the selected rows (if any)
77 QModelIndexList selectedRows = selectionModel()->selectedRows();
78
79 // if nothing is selected, copy everything to the clipboard
80 QString text;
81 if (selectedRows.isEmpty()) {
82 // iterate through every row and column and copy the data
83 for (int i = 0; i < model()->rowCount(); ++i) {
84 QStringList rowContents;
85 for (int j = 0; j < model()->columnCount(); ++j)
86 rowContents << model()->index(i, j).data().toString();
87 text += rowContents.join("\t");
88 text += "\n";
89 }
90 } else {
91 // copy the selected rows to the clipboard
92 QItemSelectionRange range = selectionModel()->selection().first();
93 for (auto i = range.top(); i <= range.bottom(); ++i) {
94 QStringList rowContents;
95 for (auto j = range.left(); j <= range.right(); ++j)
96 rowContents << model()->index(i, j).data().toString();
97 text += rowContents.join("\t");
98 text += "\n";
99 }
100 }
101 QApplication::clipboard()->setText(text);
102 }
103
104 void MolecularView::copySelectedRowsToClipboard()
105 {
106 // get the selected rows (if any)
107 QModelIndexList selectedRows = selectionModel()->selectedRows();
108
109 // if nothing is selected, copy everything to the clipboard
110 QString text;
111 if (selectedRows.isEmpty()) {
112 // iterate through every row and column and copy the data
113 for (int i = 0; i < model()->rowCount(); ++i) {
114 QStringList rowContents;
115 for (int j = 0; j < model()->columnCount(); ++j)
116 rowContents << model()->index(i, j).data().toString();
117 text += rowContents.join("\t");
118 text += "\n";
119 }
120 } else {
121 // copy the selected rows to the clipboard
122 QItemSelectionRange range = selectionModel()->selection().first();
123 for (auto i = range.top(); i <= range.bottom(); ++i) {
124 QStringList rowContents;
125 for (auto j = range.left(); j <= range.right(); ++j)
126 rowContents << model()->index(i, j).data().toString();
127 text += rowContents.join("\t");
128 text += "\n";
129 }
130 }
131 QApplication::clipboard()->setText(text);
132 }
133
134 void MolecularView::openExportDialogBox()
135 {
136 // Create a file dialog for selecting the export location and file name
137 QString filePath =
138 QFileDialog::getSaveFileName(this, tr("Export CSV"), QDir::homePath(),
139 tr("CSV Files (*.csv);;All Files (*)"));
140
141 if (filePath.isEmpty()) {
142 // User canceled the dialog or didn't provide a file name
143 return;
144 }
145
146 // Open the file for writing
147 QFile file(filePath);
148 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
149 // Handle error opening the file
150 QMessageBox::critical(this, tr("Error"),
151 tr("Could not open the file for writing."));
152 return;
153 }
154
155 // Create a QTextStream to write to the file
156 QTextStream stream(&file);
157
158 // Write the header row with column names
159 for (int col = 0; col < model()->columnCount(); ++col) {
160 stream << model()->headerData(col, Qt::Horizontal).toString();
161 if (col < model()->columnCount() - 1) {
162 stream << ",";
163 }
164 }
165 stream << "\n";
166
167 // Write the data rows
168 for (int row = 0; row < model()->rowCount(); ++row) {
169 stream << model()->headerData(row, Qt::Vertical).toString() << ",";
170 for (int col = 0; col < model()->columnCount(); ++col) {
171 stream << model()->index(row, col).data().toString();
172 if (col < model()->columnCount() - 1) {
173 stream << ",";
174 }
175 }
176 stream << "\n";
177 }
178
179 // Close the file
180 file.close();
181
182 if (file.error() != QFile::NoError) {
183 // Handle error closing the file
184 QMessageBox::critical(this, tr("Error"), tr("Error writing to the file."));
185 }
186 }
187
188 void MolecularView::contextMenuEvent(QContextMenuEvent* event)
189 {
190 QMenu menu(this);
191 QAction* copyAction = menu.addAction(tr("Copy"));
192 menu.addAction(copyAction);
193 connect(copyAction, &QAction::triggered, this,
194 &MolecularView::copySelectedRowsToClipboard);
195
196 QAction* exportAction = menu.addAction(tr("Export…"));
197 menu.addAction(exportAction);
198 connect(exportAction, &QAction::triggered, this,
199 &MolecularView::openExportDialogBox);
200
201 menu.exec(event->globalPos());
202 }
203
204 } // end namespace Avogadro