Debian Codegraph @

avogadro/qtplugins/propertytables/propertytables.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 "propertytables.h"
7 #include "propertymodel.h"
8 #include "propertyview.h"
9
10 #include <QAction>
11 #include <QtWidgets/QDialog>
12 #include <QtWidgets/QHeaderView>
13 #include <QtWidgets/QScrollBar>
14 #include <QtWidgets/QVBoxLayout>
15
16 #include <QtCore/QSortFilterProxyModel>
17 #include <QtCore/QStringList>
18
19 #include <avogadro/qtgui/molecule.h>
20
21 namespace Avogadro::QtPlugins {
22
23 PropertyTables::PropertyTables(QObject* parent_)
24 : Avogadro::QtGui::ExtensionPlugin(parent_), m_molecule(nullptr)
25 {
26 auto* action = new QAction(this);
27 action->setText(tr("Atom Properties…"));
28 action->setData(PropertyType::AtomType);
29 action->setProperty("menu priority", 880);
30 connect(action, SIGNAL(triggered()), SLOT(showDialog()));
31 m_actions.append(action);
32
33 action = new QAction(this);
34 action->setText(tr("Bond Properties…"));
35 action->setData(PropertyType::BondType);
36 action->setProperty("menu priority", 870);
37 connect(action, SIGNAL(triggered()), SLOT(showDialog()));
38 m_actions.append(action);
39
40 action = new QAction(this);
41 action->setText(tr("Angle Properties…"));
42 action->setData(PropertyType::AngleType);
43 action->setProperty("menu priority", 860);
44 connect(action, SIGNAL(triggered()), SLOT(showDialog()));
45 m_actions.append(action);
46
47 action = new QAction(this);
48 action->setText(tr("Torsion Properties…"));
49 action->setData(PropertyType::TorsionType);
50 action->setProperty("menu priority", 850);
51 connect(action, SIGNAL(triggered()), SLOT(showDialog()));
52 m_actions.append(action);
53
54 action = new QAction(this);
55 action->setText(tr("Residue Properties…"));
56 action->setData(PropertyType::ResidueType);
57 action->setProperty("menu priority", 840);
58 action->setEnabled(false); // changed by molecule
59 connect(action, SIGNAL(triggered()), SLOT(showDialog()));
60 m_actions.append(action);
61
62 action = new QAction(this);
63 action->setText(tr("Conformer Properties…"));
64 action->setData(PropertyType::ConformerType);
65 action->setProperty("menu priority", 830);
66 action->setEnabled(false); // changed by molecule
67 connect(action, SIGNAL(triggered()), SLOT(showDialog()));
68 m_actions.append(action);
69 }
70
71 PropertyTables::~PropertyTables() {}
72
73 QString PropertyTables::description() const
74 {
75 return tr("Tables for displaying and editing atom, bond, angle and torsion "
76 "properties.");
77 }
78
79 QList<QAction*> PropertyTables::actions() const
80 {
81 return m_actions;
82 }
83
84 QStringList PropertyTables::menuPath(QAction*) const
85 {
86 return QStringList() << tr("&Analyze") << tr("&Properties");
87 }
88
89 void PropertyTables::setMolecule(QtGui::Molecule* mol)
90 {
91 if (mol == m_molecule)
92 return;
93
94 m_molecule = mol;
95
96 updateActions();
97
98 // update if the molecule changes
99 connect(m_molecule, SIGNAL(changed(unsigned int)), SLOT(updateActions()));
100 }
101
102 void PropertyTables::updateActions()
103 {
104 if (m_molecule == nullptr)
105 return;
106
107 // check if we enable / disable the residue and conformer actions
108 bool haveResidues = (m_molecule->residueCount() > 0);
109 // technically coordinate sets
110 bool haveConformers = (m_molecule->coordinate3dCount() > 1);
111 for (const auto& action : m_actions) {
112 if (action->data().toInt() == PropertyType::ResidueType)
113 action->setEnabled(haveResidues);
114 else if (action->data().toInt() == PropertyType::ConformerType)
115 action->setEnabled(haveConformers);
116 }
117 }
118
119 void PropertyTables::showDialog()
120 {
121 auto* action = qobject_cast<QAction*>(sender());
122 if (action == nullptr || m_molecule == nullptr)
123 return;
124
125 if (action->data().toInt() == PropertyType::ResidueType &&
126 m_molecule->residueCount() == 0)
127 return;
128
129 if (action->data().toInt() == PropertyType::ConformerType &&
130 m_molecule->coordinate3dCount() < 2)
131 return;
132
133 auto* dialog = new QDialog(qobject_cast<QWidget*>(parent()));
134 auto* layout = new QVBoxLayout(dialog);
135 dialog->setLayout(layout);
136 // Don't show whitespace around the PropertiesView
137 layout->setSpacing(0);
138 layout->setContentsMargins(0, 0, 0, 0);
139
140 PropertyType i = static_cast<PropertyType>(action->data().toInt());
141 auto* model = new PropertyModel(i);
142 model->setMolecule(m_molecule);
143 // view will delete itself & model in PropertiesView::hideEvent using
144 // deleteLater().
145 auto* view = new PropertyView(i, dialog);
146
147 auto* proxyModel = new QSortFilterProxyModel(this);
148 proxyModel->setSourceModel(model);
149 proxyModel->setDynamicSortFilter(true);
150 proxyModel->setSortLocaleAware(true);
151 // this role will received direct floating-point numbers from the model
152 proxyModel->setSortRole(Qt::UserRole);
153
154 view->setMolecule(m_molecule);
155 view->setModel(proxyModel);
156 view->setSourceModel(model);
157
158 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
159 view->horizontalHeader()->setStretchLastSection(true);
160 view->resizeColumnsToContents();
161 layout->addWidget(view);
162 dialog->setWindowTitle(view->windowTitle());
163 QSize dialogSize = dialog->size();
164 double width =
165 view->horizontalHeader()->length() + view->verticalHeader()->width() + 5;
166 if (model->rowCount() < 13) { // no scrollbar
167 dialogSize.setHeight(view->horizontalHeader()->height() +
168 model->rowCount() * 30 + 5);
169 dialogSize.setWidth(width);
170 } else { // scrollbar is needed
171 dialogSize.setHeight(width / 1.618);
172 dialogSize.setWidth(width + view->verticalScrollBar()->width());
173 }
174 dialog->resize(dialogSize);
175 dialog->setWindowFlags(Qt::Window);
176 dialog->show();
177 dialog->raise();
178 dialog->activateWindow();
179 }
180
181 } // namespace Avogadro::QtPlugins