View.h

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _VIEW_H_
00017 #define _VIEW_H_
00018 
00019 #include <QFrame>
00020 #include <QProgressBar>
00021 
00022 #include "base/ZoomConstraint.h"
00023 #include "base/PropertyContainer.h"
00024 #include "ViewManager.h"
00025 #include "base/XmlExportable.h"
00026 
00027 // #define DEBUG_VIEW_WIDGET_PAINT 1
00028 
00029 class Layer;
00030 class ViewPropertyContainer;
00031 
00032 #include <map>
00033 #include <set>
00034 
00048 class View : public QFrame,
00049              public XmlExportable
00050 {
00051     Q_OBJECT
00052 
00053 public:
00058     virtual ~View();
00059 
00065     long getStartFrame() const;
00066 
00071     void setStartFrame(long);
00072 
00079     size_t getCentreFrame() const { return m_centreFrame; }
00080 
00084     void setCentreFrame(size_t f) { setCentreFrame(f, true); }
00085 
00091     size_t getEndFrame() const;
00092 
00097     int getXForFrame(long frame) const;
00098 
00102     long getFrameForX(int x) const;
00103 
00112     float getYForFrequency(float frequency, float minFreq, float maxFreq, 
00113                            bool logarithmic) const;
00114 
00121     float getFrequencyForY(int y, float minFreq, float maxFreq,
00122                            bool logarithmic) const;
00123 
00127     int getZoomLevel() const;
00128 
00134     virtual void setZoomLevel(size_t z);
00135 
00139     virtual void zoom(bool in);
00140 
00144     virtual void scroll(bool right, bool lots);
00145 
00146     virtual void addLayer(Layer *v);
00147     virtual void removeLayer(Layer *v); // does not delete the layer
00148     virtual int getLayerCount() const { return m_layers.size(); }
00149 
00154     virtual Layer *getLayer(int n) {
00155         if (n < int(m_layers.size())) return m_layers[n]; else return 0;
00156     }
00157 
00163     virtual Layer *getTopLayer() {
00164         return m_layers.empty() ? 0 : m_layers[m_layers.size()-1];
00165     }
00166 
00174     virtual Layer *getSelectedLayer();
00175     virtual const Layer *getSelectedLayer() const;
00176 
00177     virtual void setViewManager(ViewManager *m);
00178     virtual ViewManager *getViewManager() const { return m_manager; }
00179 
00180     virtual void setFollowGlobalPan(bool f);
00181     virtual bool getFollowGlobalPan() const { return m_followPan; }
00182 
00183     virtual void setFollowGlobalZoom(bool f);
00184     virtual bool getFollowGlobalZoom() const { return m_followZoom; }
00185 
00186     virtual bool hasLightBackground() const;
00187     virtual QColor getForeground() const;
00188     virtual QColor getBackground() const;
00189 
00190     enum TextStyle {
00191         BoxedText,
00192         OutlinedText
00193     };
00194 
00195     virtual void drawVisibleText(QPainter &p, int x, int y,
00196                                  QString text, TextStyle style) const;
00197 
00198     virtual void drawMeasurementRect(QPainter &p, const Layer *,
00199                                      QRect rect, bool focus) const;
00200 
00201     virtual bool shouldIlluminateLocalFeatures(const Layer *, QPoint &) const {
00202         return false;
00203     }
00204     virtual bool shouldIlluminateLocalSelection(QPoint &, bool &, bool &) const {
00205         return false;
00206     }
00207 
00208     virtual void setPlaybackFollow(PlaybackFollowMode m);
00209     virtual PlaybackFollowMode getPlaybackFollow() const { return m_followPlay; }
00210 
00211     typedef PropertyContainer::PropertyName PropertyName;
00212 
00213     // We implement the PropertyContainer API, although we don't
00214     // actually subclass PropertyContainer.  We have our own
00215     // PropertyContainer that we can return on request that just
00216     // delegates back to us.
00217     virtual PropertyContainer::PropertyList getProperties() const;
00218     virtual QString getPropertyLabel(const PropertyName &) const;
00219     virtual PropertyContainer::PropertyType getPropertyType(const PropertyName &) const;
00220     virtual int getPropertyRangeAndValue(const PropertyName &,
00221                                          int *min, int *max, int *deflt) const;
00222     virtual QString getPropertyValueLabel(const PropertyName &,
00223                                           int value) const;
00224     virtual void setProperty(const PropertyName &, int value);
00225     virtual QString getPropertyContainerName() const {
00226         return objectName();
00227     }
00228     virtual QString getPropertyContainerIconName() const = 0;
00229 
00230     virtual size_t getPropertyContainerCount() const;
00231 
00232     virtual const PropertyContainer *getPropertyContainer(size_t i) const;
00233     virtual PropertyContainer *getPropertyContainer(size_t i);
00234 
00235     // Render the contents on a wide canvas
00236     virtual QImage *toNewImage(size_t f0, size_t f1);
00237     virtual QImage *toNewImage();
00238     virtual QSize getImageSize(size_t f0, size_t f1);
00239     virtual QSize getImageSize();
00240 
00241     virtual int getTextLabelHeight(const Layer *layer, QPainter &) const;
00242 
00243     virtual bool getValueExtents(QString unit, float &min, float &max,
00244                                  bool &log) const;
00245 
00246     virtual void toXml(QTextStream &stream, QString indent = "",
00247                        QString extraAttributes = "") const;
00248 
00249     // First frame actually in model, to right of scale, if present
00250     virtual size_t getFirstVisibleFrame() const;
00251     virtual size_t getLastVisibleFrame() const;
00252 
00253     size_t getModelsStartFrame() const;
00254     size_t getModelsEndFrame() const;
00255 
00256     typedef std::set<Model *> ModelSet;
00257     ModelSet getModels();
00258 
00260     Model *getAligningModel() const;
00261     size_t alignFromReference(size_t) const;
00262     size_t alignToReference(size_t) const;
00263     int getAlignedPlaybackFrame() const;
00264 
00265 signals:
00266     void propertyContainerAdded(PropertyContainer *pc);
00267     void propertyContainerRemoved(PropertyContainer *pc);
00268     void propertyContainerPropertyChanged(PropertyContainer *pc);
00269     void propertyContainerPropertyRangeChanged(PropertyContainer *pc);
00270     void propertyContainerNameChanged(PropertyContainer *pc);
00271     void propertyContainerSelected(PropertyContainer *pc);
00272     void propertyChanged(PropertyContainer::PropertyName);
00273 
00274     void layerModelChanged();
00275 
00276     void centreFrameChanged(unsigned long frame,
00277                             bool globalScroll,
00278                             PlaybackFollowMode followMode);
00279 
00280     void zoomLevelChanged(unsigned long, bool);
00281 
00282     void contextHelpChanged(const QString &);
00283 
00284 public slots:
00285     virtual void modelChanged();
00286     virtual void modelChanged(size_t startFrame, size_t endFrame);
00287     virtual void modelCompletionChanged();
00288     virtual void modelAlignmentCompletionChanged();
00289     virtual void modelReplaced();
00290     virtual void layerParametersChanged();
00291     virtual void layerParameterRangesChanged();
00292     virtual void layerMeasurementRectsChanged();
00293     virtual void layerNameChanged();
00294 
00295     virtual void globalCentreFrameChanged(unsigned long);
00296     virtual void viewCentreFrameChanged(View *, unsigned long);
00297     virtual void viewManagerPlaybackFrameChanged(unsigned long);
00298     virtual void viewZoomLevelChanged(View *, unsigned long, bool);
00299 
00300     virtual void propertyContainerSelected(View *, PropertyContainer *pc);
00301 
00302     virtual void selectionChanged();
00303     virtual void toolModeChanged();
00304     virtual void overlayModeChanged();
00305     virtual void zoomWheelsEnabledChanged();
00306 
00307 protected:
00308     View(QWidget *, bool showProgress);
00309     virtual void paintEvent(QPaintEvent *e);
00310     virtual void drawSelections(QPainter &);
00311     virtual bool shouldLabelSelections() const { return true; }
00312     virtual bool render(QPainter &paint, int x0, size_t f0, size_t f1);
00313     virtual void setPaintFont(QPainter &paint);
00314     
00315     typedef std::vector<Layer *> LayerList;
00316 
00317     int getModelsSampleRate() const;
00318     bool areLayersScrollable() const;
00319     LayerList getScrollableBackLayers(bool testChanged, bool &changed) const;
00320     LayerList getNonScrollableFrontLayers(bool testChanged, bool &changed) const;
00321     size_t getZoomConstraintBlockSize(size_t blockSize,
00322                                       ZoomConstraint::RoundingDirection dir =
00323                                       ZoomConstraint::RoundNearest) const;
00324 
00325     // True if the top layer(s) use colours for meaningful things.  If
00326     // this is the case, selections will be shown using unfilled boxes
00327     // rather than with a translucent fill.
00328     bool areLayerColoursSignificant() const;
00329 
00330     // True if the top layer has a time axis on the x coordinate (this
00331     // is generally the case except for spectrum/slice layers).  It
00332     // will not be possible to make or display selections if this is
00333     // false.
00334     bool hasTopLayerTimeXAxis() const;
00335 
00336     bool setCentreFrame(size_t f, bool doEmit);
00337 
00338     void checkProgress(void *object);
00339 
00340     size_t              m_centreFrame;
00341     int                 m_zoomLevel;
00342     bool                m_followPan;
00343     bool                m_followZoom;
00344     PlaybackFollowMode  m_followPlay;
00345     size_t              m_playPointerFrame;
00346     bool                m_lightBackground;
00347     bool                m_showProgress;
00348 
00349     QPixmap            *m_cache;
00350     size_t              m_cacheCentreFrame;
00351     int                 m_cacheZoomLevel;
00352     bool                m_selectionCached;
00353 
00354     bool                m_deleting;
00355 
00356     LayerList           m_layers; // I don't own these, but see dtor note above
00357     bool                m_haveSelectedLayer;
00358 
00359     // caches for use in getScrollableBackLayers, getNonScrollableFrontLayers
00360     mutable LayerList m_lastScrollableBackLayers;
00361     mutable LayerList m_lastNonScrollableBackLayers;
00362 
00363     class LayerProgressBar : public QProgressBar {
00364     public:
00365         LayerProgressBar(QWidget *parent);
00366         virtual QString text() const { return m_text; }
00367         virtual void setText(QString text) { m_text = text; }
00368     protected:
00369         QString m_text;
00370     };
00371 
00372     typedef std::map<Layer *, LayerProgressBar *> ProgressMap;
00373     ProgressMap m_progressBars; // I own the ProgressBars
00374 
00375     ViewManager *m_manager; // I don't own this
00376     ViewPropertyContainer *m_propertyContainer; // I own this
00377 };
00378 
00379 
00380 // Use this for delegation, because we can't subclass from
00381 // PropertyContainer (which is a QObject) ourselves because of
00382 // ambiguity with QFrame parent
00383 
00384 class ViewPropertyContainer : public PropertyContainer
00385 {
00386     Q_OBJECT
00387 
00388 public:
00389     ViewPropertyContainer(View *v);
00390     PropertyList getProperties() const { return m_v->getProperties(); }
00391     QString getPropertyLabel(const PropertyName &n) const {
00392         return m_v->getPropertyLabel(n);
00393     }
00394     PropertyType getPropertyType(const PropertyName &n) const {
00395         return m_v->getPropertyType(n);
00396     }
00397     int getPropertyRangeAndValue(const PropertyName &n, int *min, int *max,
00398                                  int *deflt) const {
00399         return m_v->getPropertyRangeAndValue(n, min, max, deflt);
00400     }
00401     QString getPropertyValueLabel(const PropertyName &n, int value) const {
00402         return m_v->getPropertyValueLabel(n, value);
00403     }
00404     QString getPropertyContainerName() const {
00405         return m_v->getPropertyContainerName();
00406     }
00407     QString getPropertyContainerIconName() const {
00408         return m_v->getPropertyContainerIconName();
00409     }
00410 
00411 public slots:
00412     virtual void setProperty(const PropertyName &n, int value) {
00413         m_v->setProperty(n, value);
00414     }
00415 
00416 protected:
00417     View *m_v;
00418 };
00419 
00420 #endif
00421 

Generated on Wed Feb 20 15:45:29 2008 for SonicVisualiser by  doxygen 1.5.1