~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to compoundviewer/kalziumglwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *  Copyright (C) 2006 by Carsten Niehaus <cniehaus@kde.org>
 
3
 *  Copyright (C) 2007-2008 by Marcus D. Hanwell <marcus@cryos.org>
 
4
 ***************************************************************************/
 
5
 
 
6
/***************************************************************************
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or modify  *
 
9
 *   it under the terms of the GNU General Public License as published by  *
 
10
 *   the Free Software Foundation; either version 2 of the License, or     *
 
11
 *   (at your option) any later version.                                   *
 
12
 *                                                                         *
 
13
 ***************************************************************************/
 
14
#include "kalziumglwidget.h"
 
15
 
 
16
#include <QSettings>
 
17
 
 
18
#include <avogadro/primitive.h>
 
19
#include <avogadro/pluginmanager.h>
 
20
#include <avogadro/toolgroup.h>
 
21
#include <avogadro/engine.h>
 
22
 
 
23
#include "openbabel2wrapper.h"
 
24
 
 
25
#include <config-kalzium.h>
 
26
 
 
27
KalziumGLWidget::KalziumGLWidget(QWidget *parent) : Avogadro::GLWidget(parent),
 
28
  m_lastEngine1(0), m_lastEngine2(0)
 
29
{
 
30
    // work around a bug in OpenBabel: the chemical data files parsing
 
31
    // is dependent on the LC_NUMERIC locale.
 
32
    m_lc_numeric = QByteArray(setlocale(LC_NUMERIC, 0));
 
33
    setlocale(LC_NUMERIC, "C");
 
34
 
 
35
    // Prevent What's this from intercepting right mouse clicks
 
36
    setContextMenuPolicy(Qt::PreventContextMenu);
 
37
    // Load the tools and set navigate as the default
 
38
    // first set the Avogadro plugin directory,
 
39
    // avoiding overwriting an already set envvar
 
40
    static bool s_pluginDirSet = false;
 
41
    if (!s_pluginDirSet)
 
42
    {
 
43
        if (qgetenv("AVOGADRO_PLUGINS").isEmpty())
 
44
        {
 
45
            qputenv("AVOGADRO_PLUGINS", AVOGADRO_PLUGIN_DIR);
 
46
        }
 
47
        s_pluginDirSet = true;
 
48
    }
 
49
    Avogadro::PluginManager *manager = Avogadro::PluginManager::instance();
 
50
    manager->loadFactories();
 
51
    Avogadro::ToolGroup* tools = new Avogadro::ToolGroup(this);
 
52
    tools->append(manager->tools(this));
 
53
    tools->setActiveTool("Navigate");
 
54
    setToolGroup(tools);
 
55
    // Set the default engine to be active
 
56
    loadDefaultEngines();
 
57
 
 
58
    // Set the default quality level to high
 
59
    setQuality(2);
 
60
 
 
61
    setMolecule(new Avogadro::Molecule(this));
 
62
    update();
 
63
}
 
64
 
 
65
KalziumGLWidget::~KalziumGLWidget()
 
66
{
 
67
    //restore the LC_NUMERIC locale.
 
68
    setlocale(LC_NUMERIC, m_lc_numeric.constData());
 
69
}
 
70
 
 
71
bool KalziumGLWidget::openFile(const QString &file)
 
72
{
 
73
    Avogadro::Molecule* mol = OpenBabel2Wrapper::readMolecule( file );
 
74
    if (!mol)
 
75
        return false;
 
76
    Avogadro::Molecule* oldmol = molecule();
 
77
    if (oldmol)
 
78
        oldmol->deleteLater();
 
79
    setMolecule( mol );
 
80
    update();
 
81
    return true;
 
82
}
 
83
 
 
84
void KalziumGLWidget::setStyle( int style )
 
85
{
 
86
    foreach(Avogadro::Engine *engine, engines())
 
87
    {
 
88
        if((m_lastEngine1 == 0 && engine->identifier() == "Ball and Stick")
 
89
           || (m_lastEngine1 == 1 && engine->identifier() == "Stick")
 
90
           || (m_lastEngine1 == 2 && engine->identifier() == "Van der Waals Spheres")
 
91
           || (m_lastEngine1 == 3 && engine->identifier() == "Wireframe"))
 
92
        {
 
93
            engine->setEnabled(false);
 
94
        }
 
95
        if((style == 0 && engine->identifier() == "Ball and Stick")
 
96
           || (style == 1 && engine->identifier() == "Stick")
 
97
           || (style == 2 && engine->identifier() == "Van der Waals Spheres")
 
98
           || (style == 3 && engine->identifier() == "Wireframe"))
 
99
        {
 
100
            engine->setEnabled(true);
 
101
        }
 
102
    }
 
103
    m_lastEngine1 = style;
 
104
    update();
 
105
}
 
106
 
 
107
void KalziumGLWidget::setStyle2( int style )
 
108
{
 
109
    foreach(Avogadro::Engine *engine, engines())
 
110
    {
 
111
        if((m_lastEngine2 == 1 && engine->identifier() == "Ribbon")
 
112
           || (m_lastEngine2 == 2 && engine->identifier() == "Ring")
 
113
           || (m_lastEngine2 == 3 && engine->identifier() == "Orbitals"))
 
114
        {
 
115
            engine->setEnabled(false);
 
116
        }
 
117
        if((style == 1 && engine->identifier() == "Ribbon")
 
118
           || (style == 2 && engine->identifier() == "Ring")
 
119
           || (style == 3 && engine->identifier() == "Orbitals"))
 
120
        {
 
121
            engine->setEnabled(true);
 
122
        }
 
123
    }
 
124
    m_lastEngine2 = style;
 
125
    update();
 
126
}
 
127
 
 
128
void KalziumGLWidget::setLabels(int style)
 
129
{
 
130
  // Use the QSettings framework to configure the label engine
 
131
  foreach(Avogadro::Engine *engine, engines())
 
132
  {
 
133
    if(engine->name() == "Label")
 
134
    {
 
135
      QSettings settings;
 
136
      int atomType = 0;
 
137
      int bondType = 0;
 
138
      bool enabled = false;
 
139
      // We need to use 
 
140
      switch(style)
 
141
      {
 
142
        case 0: // Display no labels
 
143
          enabled = false;
 
144
          break;
 
145
        case 1: // Display the atom index
 
146
          enabled = true;
 
147
          atomType = 1;
 
148
          break;
 
149
        case 2: // Display the atom symbol
 
150
          enabled = true;
 
151
          atomType = 3;
 
152
          break;
 
153
        case 3: // Display the atom name
 
154
          enabled = true;
 
155
          atomType = 2;
 
156
          break;
 
157
        default:
 
158
          engine->setEnabled(false);
 
159
      }
 
160
      settings.setValue("atomLabel", atomType);
 
161
      settings.setValue("bondLabel", bondType);
 
162
      settings.setValue("enabled", enabled);
 
163
      engine->readSettings(settings);
 
164
    }
 
165
  }
 
166
}
 
167
 
 
168
void KalziumGLWidget::setQuality(int quality)
 
169
{
 
170
    // Set the global quality of the GLWidget, 0=min, 2=mid, 4=max
 
171
    int q = 0;
 
172
    if(quality == 1) q = 2;
 
173
    else if(quality == 2) q = 4;
 
174
    GLWidget::setQuality(q);
 
175
    invalidateDLs();
 
176
    GLWidget::update();
 
177
}
 
178
 
 
179
#include "kalziumglwidget.moc"