Commit a8995abf authored by Andreas Cord-Landwehr's avatar Andreas Cord-Landwehr
Browse files

Adapt molecule view UI for better Avogadro integration

Editor controls are now provided directly by the Avogadro
library.
parent 8fbc2d7c
......@@ -19,15 +19,15 @@
#include <avogadro/qtgui/periodictableview.h>
#include <avogadro/qtgui/molecule.h>
#include <avogadro/qtgui/scenepluginmodel.h>
#include <avogadro/qtgui/toolplugin.h>
// #include <avogadro/qtplugins/openbabel/openbabel.h>FIXME:Avogadro2
#include <QFileInfo>
#include <QGLFormat>
#include <QSettings>
#include <QDebug>
#include <kfiledialog.h>
#include <kjob.h>
#include <kmessagebox.h>
#include <KFileDialog>
#include <KJob>
#include <KMessageBox>
#include <KLocale>
#include <QUrl>
#include <knewstuff3/downloaddialog.h>
......@@ -51,7 +51,6 @@ MoleculeDialog::MoleculeDialog(QWidget * parent)
: KDialog(parent)
, m_path(QString())
, m_periodicTable(nullptr)
, m_addHydrogens(false)
{
// use multi-sample (anti-aliased) OpenGL if available
QGLFormat defFormat = QGLFormat::defaultFormat();
......@@ -77,34 +76,15 @@ MoleculeDialog::MoleculeDialog(QWidget * parent)
ui.optimizeButton->setEnabled(false);
}
// Set up the elements combo and bond order combo
elementCombo();
ui.bondCombo->addItem(i18n("Single"));
ui.bondCombo->addItem(i18n("Double"));
ui.bondCombo->addItem(i18n("Triple"));
// Initialise the draw settings
m_drawSettings = new QSettings;
m_drawSettings->setValue("currentElement", 6);
m_drawSettings->setValue("bondOrder", 1);
m_drawSettings->setValue("addHydrogens", 0);
ui.styleCombo->addItems({"Wireframe", "Ball and Stick", "Van der Waals"});
slotUpdateScenePlugin();
ui.styleCombo->addItems({"Ball and Stick", "Van der Waals", "Wireframe"});
connect(ui.styleCombo, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
this, &MoleculeDialog::slotUpdateScenePlugin);
slotUpdateScenePlugin();
connect(ui.tabWidget, &QTabWidget::currentChanged,
this, &MoleculeDialog::setViewEdit);
// Editing parameters
connect(ui.elementCombo, static_cast<void (QComboBox::*)(int)>(&KComboBox::currentIndexChanged),
this, &MoleculeDialog::slotElementChanged);
connect(ui.bondCombo, static_cast<void (QComboBox::*)(int)>(&KComboBox::currentIndexChanged),
this, &MoleculeDialog::slotBondOrderChanged);
connect(ui.hydrogenBox, &QCheckBox::stateChanged,
this, &MoleculeDialog::slotAddHydrogensChanged);
connect(ui.hydrogensButton, &QPushButton::clicked,
this, &MoleculeDialog::slotAdjustHydrogens);
connect(ui.optimizeButton, &QPushButton::clicked,
this, &MoleculeDialog::slotGeometryOptimize);
connect(ui.clearDrawingButton, &QPushButton::clicked,
......@@ -126,6 +106,14 @@ MoleculeDialog::MoleculeDialog(QWidget * parent)
QString error = i18n("No tools loaded - it is likely that the Avogadro plugins could not be located.");
KMessageBox::error(this, error, i18n("Kalzium"));
}
// objectName is also used in Avogadro2 for identifying tools
foreach (auto *tool, ui.glWidget->tools()) {
if (tool->objectName() == "Editor") {
ui.editTabLayout->insertWidget(0, tool->toolWidget());
break;
}
}
}
void MoleculeDialog::slotLoadMolecule()
......@@ -203,7 +191,7 @@ void MoleculeDialog::clearAllElementsInEditor()
{
ui.glWidget->molecule()->clearBonds();
ui.glWidget->molecule()->clearAtoms();
ui.glWidget->update();
ui.glWidget->updateScene();
}
void MoleculeDialog::slotSaveMolecule()
......@@ -233,9 +221,6 @@ void MoleculeDialog::setViewEdit(int mode)
ui.glWidget->setActiveTool("Navigator");
} else if (mode == 1) {
ui.glWidget->setActiveTool("Editor");
if (ui.glWidget->activeTool()) {
// ui.glWidget->activeTool()->readSettings(*m_drawSettings);//FIXME Avogadro2: settings
}
} else if (mode == 2) {
ui.glWidget->setActiveTool("MeasureTool");
}
......@@ -243,7 +228,6 @@ void MoleculeDialog::setViewEdit(int mode)
MoleculeDialog::~MoleculeDialog()
{
delete m_drawSettings;
}
void MoleculeDialog::slotUpdateStatistics()
......@@ -309,119 +293,6 @@ void MoleculeDialog::slotDownloadNewStuff()
}
}
void MoleculeDialog::elementCombo()
{
ui.elementCombo->addItem(ElementTranslator::name(1) + " (1)");
m_elementsIndex.append(1);
ui.elementCombo->addItem(ElementTranslator::name(5) + " (5)");
m_elementsIndex.append(5);
ui.elementCombo->addItem(ElementTranslator::name(6) + " (6)");
m_elementsIndex.append(6);
ui.elementCombo->addItem(ElementTranslator::name(7) + " (7)");
m_elementsIndex.append(7);
ui.elementCombo->addItem(ElementTranslator::name(8) + " (8)");
m_elementsIndex.append(8);
ui.elementCombo->addItem(ElementTranslator::name(9) + " (9)");
m_elementsIndex.append(9);
ui.elementCombo->addItem(ElementTranslator::name(15) + " (15)");
m_elementsIndex.append(15);
ui.elementCombo->addItem(ElementTranslator::name(16) + " (16)");
m_elementsIndex.append(16);
ui.elementCombo->addItem(ElementTranslator::name(17) + " (17)");
m_elementsIndex.append(17);
ui.elementCombo->addItem(ElementTranslator::name(35) + " (35)");
m_elementsIndex.append(35);
ui.elementCombo->addItem(i18nc("Other element", "Other..."));
m_elementsIndex.append(0);
ui.elementCombo->setCurrentIndex(2);
}
void MoleculeDialog::slotElementChanged(int element)
{
// Check if other was chosen
if (m_elementsIndex[element] == 0) {
if (!m_periodicTable) {
m_periodicTable = new PeriodicTableView(this);
connect(m_periodicTable, SIGNAL(elementChanged(int)),
this, SLOT(slotCustomElementChanged(int)));
}
m_periodicTable->show();
return;
}
m_drawSettings->setValue("currentElement", m_elementsIndex[element]);
// ui.glWidget->toolGroup()->activeTool()->readSettings(*m_drawSettings);//FIXME:Avogadro2
}
void MoleculeDialog::slotCustomElementChanged(int element)
{
// Set the element so we can draw with it
m_drawSettings->setValue("currentElement", element);
if (ui.glWidget->activeTool()) {
// ui.glWidget->toolGroup()->activeTool()->readSettings(*m_drawSettings);//FIXME:Avogadro2
}
// Check to see if we already have this in the comboBox list
// If not, we get back -1 and need to create a new item
int comboItem = m_elementsIndex.indexOf(element);
if (comboItem != -1) {
ui.elementCombo->setCurrentIndex(comboItem);
return; // we found it in the list, so we're done
}
// Find where we should put the new entry
// (i.e., in order by atomic number)
int position = 0;
foreach (int entry, m_elementsIndex) {
// Two cases: entry > index -- insert the new element before this one
// Or... we hit the "Other" menu choice -- also insert here
if (entry > element || entry == 0) {
break;
}
++position;
}
// And now we set up a new entry into the combo list
QString entryName(ElementTranslator::name(element)); // (e.g., "Hydrogen")
entryName += " (" + QString::number(element) + ')';
m_elementsIndex.insert(position, element);
ui.elementCombo->insertItem(position, entryName);
ui.elementCombo->setCurrentIndex(position);
}
void MoleculeDialog::slotBondOrderChanged(int bond)
{
m_drawSettings->setValue("bondOrder", bond+1);
if (ui.glWidget->activeTool()) {
// ui.glWidget->toolGroup()->activeTool()->readSettings(*m_drawSettings);//FIXME:Avogadro2
}
}
void MoleculeDialog::slotAddHydrogensChanged(int hydrogens)
{
m_drawSettings->setValue("addHydrogens", hydrogens);
if (ui.glWidget->activeTool()) {
// ui.glWidget->toolGroup()->activeTool()->readSettings(*m_drawSettings);//FIXME:Avogadro2
}
}
void MoleculeDialog::slotAdjustHydrogens()
{
// Add/remove hydrogens from the molecule
if (!m_addHydrogens) {
ui.hydrogensButton->setText(i18n("Remove hydrogens"));
// ui.glWidget->molecule()->addHydrogens();//FIXME:Avogadro2
m_addHydrogens = true;
} else {
ui.hydrogensButton->setText(i18n("Add hydrogens"));
// ui.glWidget->molecule()->removeHydrogens();//FIXME:Avogadro2
m_addHydrogens = false;
}
// ui.glWidget->molecule()->update();//FIXME:Avogadro2
}
void MoleculeDialog::slotGeometryOptimize()
{
// Perform a geometry optimization
......
......@@ -51,15 +51,9 @@ private:
Avogadro::QtGui::PeriodicTableView *m_periodicTable;
OpenBabel::OBForceField* m_forceField;
QSettings *m_drawSettings;
bool m_addHydrogens;
Ui::moleculeViewerForm ui;
/**
* Set up the element combo box
*/
void elementCombo();
private slots:
/**
* Load a molecule
......@@ -86,36 +80,11 @@ private slots:
*/
void setViewEdit(int mode);
/**
* Current element has changed
*/
void slotElementChanged(int element);
/**
* Custom element has been selected
*/
void slotCustomElementChanged(int);
/**
* Bond order has been changed
*/
void slotBondOrderChanged(int);
/**
* Automatic hydrogen addition on drawing
*/
void slotAddHydrogensChanged(int);
/**
* Update the statistical information about the current molecule
*/
void slotUpdateStatistics();
/**
* Add/remove hydrogens
*/
void slotAdjustHydrogens();
/**
* Geometry optimization
*/
......
......@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>520</height>
<width>760</width>
<height>523</height>
</rect>
</property>
<property name="sizePolicy">
......@@ -17,333 +17,268 @@
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QWidget" name="widget_2" native="true">
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="labelStyle">
<property name="text">
<string>Style:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>styleCombo</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QComboBox" name="styleCombo">
<property name="minimumSize">
<size>
<width>160</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="KalziumGLWidget" name="glWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>10</horstretch>
<verstretch>10</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>240</width>
<height>240</height>
</size>
</property>
<property name="sizeIncrement">
<size>
<width>10</width>
<height>10</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<widget class="QWidget" name="widget" native="true">
<property name="maximumSize">
<size>
<width>300</width>
<height>16777215</height>
</size>
</property>
<widget class="QWidget" name="display">
<attribute name="title">
<string>Display</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Style:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>styleCombo</cstring>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="display">
<attribute name="title">
<string>Display</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="enabled">
<bool>true</bool>
</property>
<property name="maximumSize">
<size>
<width>266</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Load a molecule and use the mouse pointer and scroll wheel to change the view point.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="spacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>148</width>
<height>43</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QComboBox" name="styleCombo">
<property name="minimumSize">
<size>
<width>160</width>
<height>0</height>
</size>
<widget class="QWidget" name="edit">
<attribute name="title">
<string>Edit</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="editTabLayout">
<item>
<widget class="QPushButton" name="optimizeButton">
<property name="text">
<string>Optimize</string>
</property>
</widget>
</item>
<item>
<spacer name="spacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>88</width>
<height>43</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="clearDrawingButton">
<property name="text">
<string>Clear drawing</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="measure">
<attribute name="title">
<string>Measure</string>
</attribute>
<widget class="QLabel" name="label_10">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>221</width>
<height>141</height>
</rect>
</property>
<property name="text">
<string>Click on 2 atoms to measure a distance, on 3 atoms to measure an angle, on 4 atoms to measure a dihedral angle.</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</widget>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>240</width>
<height>110</height>
</size>
</property>
<property name="title">
<string>Statistics</string>
</property>
<layout class="QGridLayout">
<item row="0" column="0">
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Name:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_4"/>
</item>
</layout>
</item>
<item>
<spacer name="spacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>148</width>
<height>43</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="edit">
<attribute name="title">
<string>Edit</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Element:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="KComboBox" name="elementCombo"/>
<item row="0" column="1">
<widget class="QLabel" name="nameLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<item row="1" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Formula:</string>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</spacer>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Bond Order:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="KComboBox" name="bondCombo"/>
<item row="1" column="1">
<widget class="QLabel" name="formulaLabel">