Avogadro  1.1.0
/home/kitware/dashboards/avogadro/libavogadro/src/glwidget.h
00001 /**********************************************************************
00002   GLWidget - general OpenGL display
00003 
00004   Copyright (C) 2006-2009 Geoffrey R. Hutchison
00005   Copyright (C) 2006,2007 Donald Ephraim Curtis
00006   Copyright (C) 2007-2009 Marcus D. Hanwell
00007 
00008   This file is part of the Avogadro molecular editor project.
00009   For more information, see <http://avogadro.openmolecules.net/>
00010 
00011   Avogadro is free software; you can redistribute it and/or modify
00012   it under the terms of the GNU General Public License as published by
00013   the Free Software Foundation; either version 2 of the License, or
00014   (at your option) any later version.
00015 
00016   Avogadro is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019   GNU General Public License for more details.
00020 
00021   You should have received a copy of the GNU General Public License
00022   along with this program; if not, write to the Free Software
00023   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00024   02110-1301, USA.
00025  **********************************************************************/
00026 
00027 #ifndef GLWIDGET_H
00028 #define GLWIDGET_H
00029 
00030 #include <avogadro/global.h>
00031 
00032 #include <Eigen/Core>
00033 
00034 #ifdef ENABLE_GLSL
00035   #include <GL/glew.h>
00036 #endif
00037 
00038 #include <QtOpenGL/QGLWidget>
00039 
00040 class QGLContext;
00041 class QLabel;
00042 class QMouseEvent;
00043 class QSettings;
00044 class QUndoStack;
00045 
00046 namespace Avogadro {
00047 
00048   class Primitive;
00049   class Atom;
00050   class Bond;
00051   class Molecule;
00052   class Camera;
00053   class Painter;
00054   class Tool;
00055   class ToolGroup;
00056   class Extension;
00057   class Color;
00058   class Engine;
00059   class Painter;
00060   class PrimitiveList;
00061 
00062   bool engineLessThan( const Engine* lhs, const Engine* rhs );
00063 
00082   class GLHit;
00083   class GLThread;
00084   class GLWidgetPrivate;
00085   class GLPainterDevice;
00086   class A_EXPORT GLWidget : public QGLWidget
00087   {
00088     friend class GLThread;
00089 
00090     Q_OBJECT
00091     Q_PROPERTY(QColor background READ background WRITE setBackground)
00092 //    Q_PROPERTY(float scale READ scale WRITE setScale)
00093 
00094     public:
00099       GLWidget(QWidget *parent = 0);
00100 
00107       explicit GLWidget(const QGLFormat &format, QWidget *parent = 0, const GLWidget * shareWidget = 0);
00108 
00116       GLWidget(Molecule *molecule, const QGLFormat &format, QWidget *parent = 0, const GLWidget * shareWidget = 0);
00117 
00121       ~GLWidget();
00122 
00123       void renderText(double x, double y, double z, const QString &str, const QFont &font, int listBase=2000);
00127       void setQuickRender(bool enabled);
00128 
00132       bool quickRender() const;
00133 
00137       void setRenderUnitCellAxes(bool enabled);
00138 
00142       bool renderUnitCellAxes() const;
00143 
00147       int deviceWidth() { return width(); }
00148 
00152       int deviceHeight() { return height(); }
00153 
00159       QSize sizeHint() const;
00160 
00167       QSize minimumSizeHint() const;
00168 
00172       double radius(const Primitive *p) const;
00173 
00177       Tool* tool() const;
00178 
00182       QColor background() const;
00183 
00187       void setColorMap(Color *);
00188 
00192       Color *colorMap() const;
00193 
00197       void setUndoStack(QUndoStack *undoStack);
00198 
00202       QUndoStack* undoStack() const;
00203 
00207       const Molecule* molecule() const;
00208 
00212       Molecule* molecule();
00213 
00217       Camera * camera() const;
00218 
00222       QList<Engine *> engines() const;
00223 
00227       QList<GLHit> hits(int x, int y, int w, int h);
00228 
00234       Primitive* computeClickedPrimitive(const QPoint& p);
00235 
00241       Atom* computeClickedAtom(const QPoint& p);
00242 
00248       Bond* computeClickedBond(const QPoint& p);
00249 
00253       const Eigen::Vector3d & center() const;
00257       const Eigen::Vector3d & normalVector() const;
00261       double radius() const;
00265       const Atom *farthestAtom() const;
00266 
00270       void setQuality(int quality);
00271 
00275       int quality() const;
00276 
00280       void setFogLevel(int level);
00281 
00285       int fogLevel() const;
00286 
00290       void setRenderAxes(bool renderAxes);
00291 
00295       bool renderAxes();
00296 
00300       void setRenderDebug(bool renderDebug);
00301 
00305       bool renderDebug();
00306 
00310       void setToolGroup(ToolGroup *toolGroup);
00311 
00315       ToolGroup * toolGroup() const;
00316 
00320       void setExtensions(QList<Extension*> extensions);
00321 
00329       Painter *painter() const;
00330 
00342       PrimitiveList selectedPrimitives() const;
00343 
00350       void toggleSelected(PrimitiveList primitives);
00351 
00356       void toggleSelected();
00357 
00365       void setSelected(PrimitiveList primitives, bool select = true); // do we pass by value intentionally
00366 
00370       void clearSelected();
00371 
00375       bool isSelected(const Primitive *p) const;
00376 
00384       bool addNamedSelection(const QString &name, PrimitiveList &primitives);
00390       void removeNamedSelection(const QString &name);
00396       void removeNamedSelection(int index);
00402       void renameNamedSelection(int index, const QString &name);
00408       QList<QString> namedSelections();
00415       PrimitiveList namedSelectionPrimitives(const QString &name);
00423       PrimitiveList namedSelectionPrimitives(int index);
00424       /* end selection method grouping */
00436       void setUnitCells(int a, int b, int c);
00437 
00442        void setUnitCellColor(const QColor c);
00443 
00447       void clearUnitCell();
00448 
00452       int aCells();
00453 
00457       int bCells();
00458 
00462       int cCells();
00463 
00467       enum ProjectionType
00468       {
00469         Perspective   = 1,
00470         Orthographic  = 2
00471       };
00472 
00477       void setProjection(ProjectionType type);
00478 
00483       ProjectionType projection() const;
00484 
00488       static GLWidget *m_current;
00489 
00493       static GLWidget *current();
00494 
00498       static void setCurrent(GLWidget *current);
00499 
00503       void renderNow();
00504 
00508       virtual void writeSettings(QSettings &settings) const;
00509 
00513       virtual void readSettings(QSettings &settings);
00514 
00515 
00516     protected:
00517       friend class GLGraphicsView;
00522       virtual void initializeGL();
00523 
00528       virtual void paintGL();
00529 
00534       virtual void paintGL2();
00535 
00539       virtual void resizeGL(int, int);
00540 
00544       virtual void paintEvent(QPaintEvent *event);
00545 
00549       virtual void resizeEvent(QResizeEvent *event);
00550 
00554       virtual bool event(QEvent *event);
00555 
00559       virtual void mousePressEvent(QMouseEvent * event);
00560 
00564       virtual void mouseReleaseEvent(QMouseEvent * event);
00565 
00569       virtual void mouseMoveEvent(QMouseEvent * event);
00570 
00574       virtual void wheelEvent(QWheelEvent * event);
00575 
00580       virtual void mouseDoubleClickEvent(QMouseEvent *event);
00581 
00586       virtual void keyPressEvent(QKeyEvent *event);
00587 
00592       virtual void keyReleaseEvent(QKeyEvent *event);
00593 
00600       virtual void render();
00601 
00608       virtual void renderCrystal(GLuint displayList);
00609 
00615       virtual void renderCrystalAxes();
00616 
00636       virtual bool renderClippedBox(const Eigen::Vector3d &offset,
00637                                     const Eigen::Vector3d &v1,
00638                                     const Eigen::Vector3d &v2,
00639                                     const Eigen::Vector3d &v3,
00640                                     double lineWidth );
00641 
00645       virtual void renderAxesOverlay();
00646 
00652       virtual void renderDebugOverlay();
00653 
00660       virtual void renderTextOverlay();
00661 
00668 //       QWaitCondition *paintCondition() const;
00669 
00670 
00671     private:
00672       GLWidgetPrivate * const d;
00673 
00677       void constructor(const GLWidget *shareWidget =0);
00678 
00682       inline double computeFramesPerSecond();
00683 
00685       void loadEngines(QSettings &settings);
00686 
00687       bool              m_glslEnabled;
00688       Tool*             m_navigateTool; 
00689 
00690     public Q_SLOTS:
00691 
00695       void setTool(Tool *tool);
00696 
00702       void setBackground(const QColor &background);
00703 
00708       void setMolecule(Molecule *molecule);
00709 
00713       void unselectPrimitive(Primitive *);
00714 
00718       void unselectAtom(Atom *);
00719 
00723       void unselectBond(Bond *);
00724 
00729       void addEngine(Engine *engine);
00730 
00735       void removeEngine(Engine *engine);
00736 
00740       void loadDefaultEngines();
00741 
00745       void reloadEngines();
00746 
00750       void invalidateDLs();
00751 
00755       void updateGeometry();
00756 
00760       void toolsDestroyed();
00761 
00773       void addTextOverlay(QLabel* label);
00774 
00792       void addTextOverlay(const QList<QLabel*> &labels);
00793 
00794     Q_SIGNALS:
00798       void mousePress(QMouseEvent * event);
00799 
00803       void mouseRelease( QMouseEvent * event );
00804 
00808       void mouseMove( QMouseEvent * event );
00809 
00813       void mouseDoubleClick( QMouseEvent * event );
00814 
00818       void wheel( QWheelEvent * event);
00819 
00823       void moleculeChanged(Molecule *newMolecule);
00824 
00828       void engineAdded(Engine *engine);
00829 
00833       void engineRemoved(Engine *engine);
00834 
00839       void resized();
00840 
00844       void namedSelectionsChanged();
00845 
00849       void activated(GLWidget *);
00850 
00854       void unitCellAxesRenderChanged(bool enabled);
00855 
00856 
00857   };
00858 
00859 } // end namespace Avogadro
00860 
00861 #endif