Skip to content

Commit

Permalink
Change color of unit cell (feature request #...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Tokarev committed Feb 27, 2010
1 parent 33198fc commit b961b29
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
33 changes: 30 additions & 3 deletions libavogadro/src/extensions/unitcellextension.cpp
Expand Up @@ -29,8 +29,9 @@
#include <openbabel/mol.h>
#include <openbabel/generic.h>

#include <QMessageBox>
#include <QDebug>
#include <QtGui/QColorDialog>
#include <QtGui/QMessageBox>
#include <QtCore/QDebug>

using namespace std;
using namespace OpenBabel;
Expand All @@ -39,7 +40,8 @@ namespace Avogadro {

UnitCellExtension::UnitCellExtension(QObject *parent) : Extension(parent),
m_widget(NULL),
m_molecule(NULL)
m_molecule(NULL),
m_color(255,255,255)
{
QAction *action = new QAction(this);
action->setText(tr("Unit Cell Parameters..."));
Expand All @@ -50,6 +52,8 @@ namespace Avogadro {
this, SLOT(deleteUnitCell()));
connect(m_dialog, SIGNAL(fillUnitCell()),
this, SLOT(fillUnitCell()));
connect(m_dialog, SIGNAL(changeColor()),
this, SLOT(changeColor()));
}

UnitCellExtension::~UnitCellExtension()
Expand Down Expand Up @@ -94,6 +98,16 @@ namespace Avogadro {
this, SLOT(unitCellParametersChanged(double, double, double, double, double, double)));
}

void UnitCellExtension::writeSettings(QSettings &settings) const
{
settings.setValue("unitcell/color", m_color);
}

void UnitCellExtension::readSettings(QSettings &settings)
{
m_color = settings.value("unitcell/color", QColor(Qt::white)).value<QColor>();
}

QUndoCommand* UnitCellExtension::performAction(QAction *, GLWidget *widget)
{
// FIXME: this is bad mmmkay
Expand Down Expand Up @@ -125,6 +139,7 @@ namespace Avogadro {
m_molecule->setOBUnitCell(uc);

widget->setUnitCells(1, 1, 1);
widget->setUnitCellColor(m_color);
} else { // do nothing -- user picked "Cancel"
return NULL;
}
Expand Down Expand Up @@ -195,6 +210,18 @@ namespace Avogadro {
m_widget->clearUnitCell();
}

void UnitCellExtension::changeColor()
{
if (m_widget) {
QColor current(m_color);
QColor color = QColorDialog::getColor(current, m_dialog, tr("Select Unit Cell Color"));
m_widget->setUnitCellColor(color);
m_color = color;
if (m_molecule)
m_molecule->update();
}
}

vector3 transformedFractionalCoordinate(vector3 originalCoordinate)
{
// ensure the fractional coordinate is entirely within the unit cell
Expand Down
6 changes: 6 additions & 0 deletions libavogadro/src/extensions/unitcellextension.h
Expand Up @@ -26,6 +26,8 @@

#include "unitcellparamdialog.h"

#include <QtGui/QColor>

namespace Avogadro {

class UnitCellExtension : public Extension
Expand All @@ -45,6 +47,8 @@ namespace Avogadro {
QUndoCommand* performAction(QAction *action, GLWidget *widget);
QString menuPath(QAction *action) const;
void setMolecule(Molecule *molecule);
virtual void writeSettings(QSettings &settings) const;
virtual void readSettings(QSettings &settings);

public Q_SLOTS:
void unitCellDisplayChanged(int a, int b, int c);
Expand All @@ -53,12 +57,14 @@ namespace Avogadro {

void deleteUnitCell();
void fillUnitCell();
void changeColor();

private:
QList<QAction *> m_actions;
UnitCellParamDialog *m_dialog;
GLWidget *m_widget;
Molecule *m_molecule;
QColor m_color;
};

class UnitCellExtensionFactory : public QObject, public PluginFactory
Expand Down
6 changes: 6 additions & 0 deletions libavogadro/src/extensions/unitcellparamdialog.cpp
Expand Up @@ -43,6 +43,7 @@ namespace Avogadro {

connect(ui.deleteUnitCell, SIGNAL(clicked()), this, SLOT(deleteCellClicked()));
connect(ui.fillUnitCell, SIGNAL(clicked()), this, SLOT(fillCellClicked()));
connect(ui.changeColor, SIGNAL(clicked()), this, SLOT(changeColorClicked()));

connect(ui.aCellSpinBox, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
connect(ui.bCellSpinBox, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
Expand Down Expand Up @@ -229,6 +230,11 @@ namespace Avogadro {
{
emit fillUnitCell();
}

void UnitCellParamDialog::changeColorClicked()
{
emit changeColor();
}

}

2 changes: 2 additions & 0 deletions libavogadro/src/extensions/unitcellparamdialog.h
Expand Up @@ -73,6 +73,7 @@ namespace Avogadro
void buttonClicked(QAbstractButton *button);
void deleteCellClicked();
void fillCellClicked();
void changeColorClicked();

signals:
void unitCellDisplayChanged(int a, int b, int c);
Expand All @@ -81,6 +82,7 @@ namespace Avogadro

void deleteUnitCell();
void fillUnitCell();
void changeColor();

private:
Ui::UnitCellParamDialog ui;
Expand Down
28 changes: 27 additions & 1 deletion libavogadro/src/extensions/unitcellparamdialog.ui
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>325</width>
<height>374</height>
<height>365</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -94,6 +94,32 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="changeColor">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Change Color</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down
17 changes: 16 additions & 1 deletion libavogadro/src/glwidget.cpp
Expand Up @@ -140,6 +140,7 @@ namespace Avogadro {
public:
GLWidgetPrivate() : background( 0,0,0,0 ),
aCells( 1 ), bCells( 1 ), cCells( 1 ),
cellColor( 255,255,255 ),
molecule( 0 ),
camera( new Camera ),
tool( 0 ),
Expand Down Expand Up @@ -197,6 +198,8 @@ namespace Avogadro {
unsigned char bCells;
unsigned char cCells;

QColor cellColor;

Molecule *molecule;

Camera *camera;
Expand Down Expand Up @@ -879,7 +882,8 @@ namespace Avogadro {
v5 = v4 + v1;

glDisable(GL_LIGHTING);
glColor4f(1.0, 1.0, 1.0, 0.7);
glColor4f(d->cellColor.redF(), d->cellColor.greenF(), d->cellColor.blueF(), 0.7);
//glColor4f(1.0, 1.0, 1.0, 0.7);
glLineWidth(2.0);
for (int a = 0; a < d->aCells; a++) {
for (int b = 0; b < d->bCells; b++) {
Expand Down Expand Up @@ -1796,6 +1800,17 @@ namespace Avogadro {
update();
}

void GLWidget::setUnitCellColor(const QColor c)
{
#ifdef ENABLE_THREADED_GL
d->renderMutex.lock();
#endif
d->cellColor = c;
#ifdef ENABLE_THREADED_GL
d->renderMutex.unlock();
#endif
}

void GLWidget::clearUnitCell()
{
updateGeometry();
Expand Down
6 changes: 6 additions & 0 deletions libavogadro/src/glwidget.h
Expand Up @@ -433,6 +433,12 @@ namespace Avogadro {
*/
void setUnitCells(int a, int b, int c);

/**
* Set the color of unit cells
* @param c color of unit cells
*/
void setUnitCellColor(const QColor c);

/**
* Clear the unit cell data.
*/
Expand Down

0 comments on commit b961b29

Please sign in to comment.