| 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 "orbitalwidget.h" |
| 7 |
#include "orbitaltablemodel.h" |
| 8 |
|
| 9 |
#include <avogadro/qtgui/richtextdelegate.h> |
| 10 |
|
| 11 |
#include <QDebug> |
| 12 |
#include <QSettings> |
| 13 |
|
| 14 |
using Avogadro::QtGui::RichTextDelegate; |
| 15 |
|
| 16 |
namespace Avogadro::QtPlugins { |
| 17 |
|
| 18 |
OrbitalWidget::OrbitalWidget(QWidget* parent, Qt::WindowFlags f) |
| 19 |
: QWidget(parent, f), m_settings(nullptr), m_quality(OQ_Low), |
| 20 |
m_isovalue(0.03), m_precalc_limit(true), m_precalc_range(10), |
| 21 |
m_tableModel(new OrbitalTableModel(this)), |
| 22 |
m_sortedTableModel(new OrbitalSortingProxyModel(this)) |
| 23 |
{ |
| 24 |
ui.setupUi(this); |
| 25 |
|
| 26 |
setWindowTitle(tr("Molecular Orbitals")); |
| 27 |
|
| 28 |
m_sortedTableModel->setSourceModel(m_tableModel); |
| 29 |
|
| 30 |
ui.table->setModel(m_sortedTableModel); |
| 31 |
ui.table->horizontalHeader()->setSectionResizeMode( |
| 32 |
QHeaderView::ResizeToContents); |
| 33 |
ui.table->horizontalHeader()->setStretchLastSection(true); |
| 34 |
// ui.table->setItemDelegateForColumn(OrbitalTableModel::C_Status, |
| 35 |
// new ProgressBarDelegate(this)); |
| 36 |
ui.table->setItemDelegateForColumn(OrbitalTableModel::C_Symmetry, |
| 37 |
new RichTextDelegate(this)); |
| 38 |
// TODO: Support orbital symmetry labels |
| 39 |
ui.table->hideColumn(OrbitalTableModel::C_Symmetry); |
| 40 |
// Hide the electron type column (used internally for alpha/beta tracking) |
| 41 |
ui.table->hideColumn(OrbitalTableModel::C_ElectronType); |
| 42 |
ui.table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 43 |
|
| 44 |
connect( |
| 45 |
ui.table->selectionModel(), |
| 46 |
SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), |
| 47 |
this, SLOT(tableClicked(const QItemSelection&))); |
| 48 |
connect(ui.push_render, SIGNAL(clicked()), this, SLOT(renderClicked())); |
| 49 |
|
| 50 |
// TODO: Implement configure dialog |
| 51 |
ui.push_configure->setVisible(false); |
| 52 |
connect(ui.push_configure, SIGNAL(clicked()), this, SLOT(configureClicked())); |
| 53 |
readSettings(); |
| 54 |
} |
| 55 |
|
| 56 |
OrbitalWidget::~OrbitalWidget() |
| 57 |
{ |
| 58 |
writeSettings(); |
| 59 |
} |
| 60 |
|
| 61 |
void OrbitalWidget::readSettings() |
| 62 |
{ |
| 63 |
QSettings settings; |
| 64 |
settings.beginGroup("orbitals"); |
| 65 |
m_quality = OrbitalQuality(settings.value("defaultQuality", 1).toInt()); |
| 66 |
m_isovalue = settings.value("isoValue", 0.03).toDouble(); |
| 67 |
ui.combo_quality->setCurrentIndex( |
| 68 |
settings.value("selectedQuality", 1).toInt()); |
| 69 |
m_sortedTableModel->HOMOFirst(settings.value("HOMOFirst", false).toBool()); |
| 70 |
m_precalc_limit = settings.value("precalc/limit", true).toBool(); |
| 71 |
m_precalc_range = settings.value("precalc/range", 10).toInt(); |
| 72 |
settings.endGroup(); |
| 73 |
} |
| 74 |
|
| 75 |
void OrbitalWidget::writeSettings() |
| 76 |
{ |
| 77 |
QSettings settings; |
| 78 |
settings.beginGroup("orbitals"); |
| 79 |
settings.setValue("defaultQuality", m_quality); |
| 80 |
settings.setValue("isoValue", m_isovalue); |
| 81 |
settings.setValue("selectedQuality", ui.combo_quality->currentIndex()); |
| 82 |
settings.setValue("HOMOFirst", m_sortedTableModel->isHOMOFirst()); |
| 83 |
settings.setValue("precalc/limit", m_precalc_limit); |
| 84 |
settings.setValue("precalc/range", m_precalc_range); |
| 85 |
settings.endGroup(); |
| 86 |
} |
| 87 |
|
| 88 |
void OrbitalWidget::reject() |
| 89 |
{ |
| 90 |
hide(); |
| 91 |
} |
| 92 |
|
| 93 |
void OrbitalWidget::configureClicked() |
| 94 |
{ |
| 95 |
/* |
| 96 |
if (!m_settings) { |
| 97 |
m_settings = new OrbitalSettingsDialog(this); |
| 98 |
} |
| 99 |
m_settings->setDefaultQuality(m_quality); |
| 100 |
m_settings->setIsoValue(m_isovalue); |
| 101 |
m_settings->setHOMOFirst(m_sortedTableModel->isHOMOFirst()); |
| 102 |
m_settings->setLimitPrecalc(m_precalc_limit); |
| 103 |
m_settings->setPrecalcRange(m_precalc_range); |
| 104 |
m_settings->show(); |
| 105 |
*/ |
| 106 |
} |
| 107 |
|
| 108 |
void OrbitalWidget::fillTable(Core::BasisSet* basis) |
| 109 |
{ |
| 110 |
if (basis == nullptr || m_tableModel == nullptr) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
// Populate the model |
| 115 |
m_tableModel->setOrbitals(basis); |
| 116 |
|
| 117 |
ui.table->horizontalHeader()->sectionResizeMode( |
| 118 |
QHeaderView::ResizeToContents); |
| 119 |
|
| 120 |
// Sort table |
| 121 |
m_sortedTableModel->sort(0, Qt::AscendingOrder); |
| 122 |
|
| 123 |
// // Find HOMO and scroll to it |
| 124 |
QModelIndex homo = m_tableModel->HOMO(); |
| 125 |
homo = m_sortedTableModel->mapFromSource(homo); |
| 126 |
// qDebug() << "HOMO at: " << homo.row(); |
| 127 |
ui.table->scrollTo(homo, QAbstractItemView::PositionAtCenter); |
| 128 |
} |
| 129 |
|
| 130 |
void OrbitalWidget::setQuality(OrbitalQuality q) |
| 131 |
{ |
| 132 |
ui.combo_quality->setCurrentIndex(int(q)); |
| 133 |
} |
| 134 |
|
| 135 |
void OrbitalWidget::selectOrbital(unsigned int orbital) |
| 136 |
{ |
| 137 |
QModelIndex start = m_tableModel->index(orbital - 1, 0, QModelIndex()); |
| 138 |
QModelIndex end = m_tableModel->index( |
| 139 |
orbital - 1, m_tableModel->columnCount(QModelIndex()) - 1, QModelIndex()); |
| 140 |
QItemSelection selection(start, end); |
| 141 |
|
| 142 |
selection = m_sortedTableModel->mapSelectionFromSource(selection); |
| 143 |
|
| 144 |
ui.table->selectionModel()->clear(); |
| 145 |
ui.table->selectionModel()->select(selection, |
| 146 |
QItemSelectionModel::SelectCurrent); |
| 147 |
} |
| 148 |
|
| 149 |
void OrbitalWidget::tableClicked(const QItemSelection& selected) |
| 150 |
{ |
| 151 |
QItemSelection mappedSelected = |
| 152 |
m_sortedTableModel->mapSelectionToSource(selected); |
| 153 |
|
| 154 |
QModelIndexList selection = mappedSelected.indexes(); |
| 155 |
|
| 156 |
// Only one row can be selected at a time, so just check the row |
| 157 |
// of the first entry. |
| 158 |
if (selection.size() == 0) |
| 159 |
return; |
| 160 |
|
| 161 |
int row = selection.first().row(); |
| 162 |
|
| 163 |
// Get the actual orbital index and electron type from the model |
| 164 |
int orbital = m_tableModel->orbitalIndex(row); |
| 165 |
auto electronType = m_tableModel->electronType(row); |
| 166 |
|
| 167 |
emit orbitalSelected(orbital, electronType); |
| 168 |
} |
| 169 |
|
| 170 |
void OrbitalWidget::renderClicked() |
| 171 |
{ |
| 172 |
double quality = OrbitalQualityToDouble(ui.combo_quality->currentIndex()); |
| 173 |
QModelIndexList selection = ui.table->selectionModel()->selectedIndexes(); |
| 174 |
|
| 175 |
// Only one row can be selected at a time, so just check the row |
| 176 |
// of the first entry. |
| 177 |
if (selection.size() == 0) |
| 178 |
return; |
| 179 |
|
| 180 |
QModelIndex first = selection.first(); |
| 181 |
first = m_sortedTableModel->mapToSource(first); |
| 182 |
|
| 183 |
int row = first.row(); |
| 184 |
|
| 185 |
// Get the actual orbital index and electron type from the model |
| 186 |
int orbital = m_tableModel->orbitalIndex(row); |
| 187 |
auto electronType = m_tableModel->electronType(row); |
| 188 |
|
| 189 |
emit renderRequested(orbital, quality, electronType); |
| 190 |
} |
| 191 |
|
| 192 |
double OrbitalWidget::OrbitalQualityToDouble(OrbitalQuality q) |
| 193 |
{ |
| 194 |
switch (q) { |
| 195 |
case OQ_VeryLow: |
| 196 |
return 0.5; |
| 197 |
case OQ_Low: |
| 198 |
return 0.35; |
| 199 |
case OQ_Medium: |
| 200 |
default: |
| 201 |
return 0.18; |
| 202 |
case OQ_High: |
| 203 |
return 0.10; |
| 204 |
case OQ_VeryHigh: |
| 205 |
return 0.05; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void OrbitalWidget::setDefaults(OrbitalQuality q, double i, bool HOMOFirst) |
| 210 |
{ |
| 211 |
m_quality = q; |
| 212 |
m_isovalue = i; |
| 213 |
m_sortedTableModel->HOMOFirst(HOMOFirst); |
| 214 |
m_sortedTableModel->sort(0, Qt::AscendingOrder); |
| 215 |
} |
| 216 |
|
| 217 |
void OrbitalWidget::setPrecalcSettings(bool limit, int range) |
| 218 |
{ |
| 219 |
m_precalc_limit = limit; |
| 220 |
m_precalc_range = range; |
| 221 |
} |
| 222 |
|
| 223 |
} // namespace Avogadro::QtPlugins |