casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PlotEvent.h
Go to the documentation of this file.
00001 //# PlotEvent.h: Classes for interaction events.
00002 //# Copyright (C) 2008
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef PLOTEVENT_H_
00028 #define PLOTEVENT_H_
00029 
00030 #include <graphics/GenericPlotter/PlotOptions.h>
00031 
00032 #include <casa/namespace.h>
00033 
00034 namespace casa {
00035 
00036 //# Forward Declarations
00037 class Plotter;
00038 class PlotCanvas;
00039 class PlotButton;
00040 class PlotCheckbox;
00041 
00042 // Base class for an event.
00043 class PlotEvent {
00044 public:
00045     PlotEvent() { }
00046     
00047     virtual ~PlotEvent() { }
00048     
00049     // Return the origin of the object that spawned this event.
00050     virtual void* origin() const = 0;
00051 };
00052 
00053 
00054 // Event for when the user selects a region on a plot canvas with the mouse.
00055 class PlotSelectEvent : public virtual PlotEvent {
00056 public:
00057     // Constructor that takes the originating canvas and the selected region.
00058     PlotSelectEvent(PlotCanvas* canvas, const PlotRegion& region);
00059     
00060     // Destructor.
00061     ~PlotSelectEvent();
00062     
00063     // Overrides PlotEvent::origin().
00064     void* origin() const { return canvas(); }
00065     
00066     // Returns the canvas upon which the selection was made.
00067     PlotCanvas* canvas() const;
00068     
00069     // Returns the region that was selected.
00070     PlotRegion region() const;
00071     
00072 protected:
00073     PlotCanvas* m_canvas; // Canvas
00074     PlotRegion m_region;  // Selected Region
00075 };
00076 
00077 
00078 // Generic mouse event where the canvas is the origin, and the type, button,
00079 // and location of the event are provided.
00080 class PlotMouseEvent : public virtual PlotEvent {
00081 public:
00082     // Mouse event types.
00083     enum Type {
00084         CLICK,   // press and release (can be double)
00085         PRESS,   // press
00086         RELEASE, // release
00087         DRAG,    // mouse movement while button is down
00088         MOVE     // mouse movement any time
00089     };
00090     
00091     // Mouse button types that we care about.
00092     enum Button {
00093         SINGLE,  // left button, single click
00094         DOUBLE,  // left button, double click
00095         CONTEXT, // right button
00096         MIDDLE   // middle button
00097     };
00098     
00099     // Constructor which takes the originating canvas, type, button, and
00100     // location.
00101     PlotMouseEvent(PlotCanvas* canvas, Type type, Button button,
00102                    const PlotCoordinate& coord);
00103     
00104     // Destructor.
00105     virtual ~PlotMouseEvent();
00106     
00107     // Overrides PlotEvent::origin().
00108     void* origin() const { return canvas(); }
00109     
00110     // Canvas origin of event.
00111     PlotCanvas* canvas() const;
00112     
00113     // Type of event.
00114     Type type() const;
00115     
00116     // Button of event.
00117     Button button() const;
00118     
00119     // Location of event.
00120     PlotCoordinate where() const;
00121     
00122 protected:
00123     PlotCanvas* m_canvas;   // Canvas
00124     Type m_type;            // Type
00125     Button m_button;        // Button
00126     PlotCoordinate m_coord; // Location
00127 };
00128 
00129 
00130 // Convenience class for mouse clicks.
00131 class PlotClickEvent : public PlotMouseEvent {
00132 public:    
00133     PlotClickEvent(PlotCanvas* canvas, Button button,
00134             const PlotCoordinate& coord) :
00135             PlotMouseEvent(canvas, CLICK, button, coord) { }
00136     
00137     ~PlotClickEvent() { }
00138 };
00139 
00140 // Convenience class for mouse presses.
00141 class PlotMousePressEvent : public PlotMouseEvent {
00142 public:
00143     PlotMousePressEvent(PlotCanvas* canvas, Button button,
00144             const PlotCoordinate& coord) :
00145             PlotMouseEvent(canvas, PRESS, button, coord) { }
00146     
00147     ~PlotMousePressEvent() { }
00148 };
00149 
00150 // Convenience class for mouse releases.
00151 class PlotMouseReleaseEvent : public PlotMouseEvent {
00152 public:
00153     PlotMouseReleaseEvent(PlotCanvas* canvas, Button button,
00154             const PlotCoordinate& coord) :
00155             PlotMouseEvent(canvas, RELEASE, button, coord) { }
00156     
00157     ~PlotMouseReleaseEvent() { }
00158 };
00159 
00160 // Convenience class for mouse drags.
00161 class PlotMouseDragEvent : public PlotMouseEvent {
00162 public:
00163     PlotMouseDragEvent(PlotCanvas* canvas, Button button,
00164             const PlotCoordinate& coord) :
00165             PlotMouseEvent(canvas, DRAG, button, coord) { }
00166     
00167     ~PlotMouseDragEvent() { }
00168 };
00169 
00170 // Convenience class for mouse moves.
00171 class PlotMouseMoveEvent : public PlotMouseEvent {
00172 public:
00173     PlotMouseMoveEvent(PlotCanvas* canvas, Button button,
00174             const PlotCoordinate& coord) :
00175             PlotMouseEvent(canvas, MOVE, button, coord) { }
00176     
00177     ~PlotMouseMoveEvent() { }
00178 };
00179 
00180 
00181 // Event for when the user scrolls the scroll wheel.  The scroll event contains
00182 // a "delta" which is negative when the user scrolls back and positive when
00183 // the user scrolls forward.  The number indicates how many "clicks" the user
00184 // scrolled.
00185 class PlotWheelEvent : public virtual PlotEvent {
00186 public:
00187     // Constructor which takes the originating canvas, the wheel delta, and the
00188     // location.
00189     PlotWheelEvent(PlotCanvas* canvas, int delta, const PlotCoordinate& c);
00190     
00191     // Destructor.
00192     ~PlotWheelEvent();
00193     
00194     // Overrides PlotEvent::origin().
00195     void* origin() const { return canvas(); }
00196     
00197     // Canvas that was scrolled upon.
00198     PlotCanvas* canvas() const;
00199     
00200     // Scroll delta.
00201     int delta() const;
00202     
00203     // Where the scroll happened.
00204     PlotCoordinate where() const;
00205     
00206 protected:
00207     PlotCanvas* m_canvas;   // Canvas
00208     int m_delta;            // Scroll delta
00209     PlotCoordinate m_coord; // Location
00210 };
00211 
00212 
00213 // Event for when the user makes a keyboard command when a canvas has focus.
00214 // An example of a keyboard command would be "ctrl+s" or "n" or "shift+g" or
00215 // "F3".  Note that this event is only valid for key commands that can be
00216 // represented with a char (or, in the case of the F1-F12 keys, the F modifier
00217 // plus the character representation of the number).
00218 class PlotKeyEvent : public virtual PlotEvent {
00219 public:
00220     // Modifier for a key press
00221     enum Modifier {
00222         CONTROL, SHIFT, ALT, F
00223     };
00224     
00225     // Constructor that takes the originating canvas, the key pressed, and any
00226     // modifiers.
00227     PlotKeyEvent(PlotCanvas* canvas, char key, const vector<Modifier>& mods);
00228     
00229     // Destructor.
00230     ~PlotKeyEvent();
00231     
00232     // Overrides PlotEvent::origin().
00233     void* origin() const { return canvas(); }
00234     
00235     // Returns the originating canvas.
00236     PlotCanvas* canvas() const;
00237     
00238     // Returns the pressed character key.
00239     char key() const;
00240     
00241     // Returns the key modifiers.
00242     vector<Modifier> modifiers() const;
00243     
00244     // Returns a String representation of this key event.
00245     String toString() const;
00246     
00247     
00248     // Converts between KeyModifier and its String representation.
00249     // <group>
00250     static String modifier(Modifier f) {
00251         switch(f) {
00252         case CONTROL: return "Ctrl";
00253         case ALT: return "Alt";
00254         case SHIFT: return "Shift";
00255         case F: return "F";
00256         
00257         default: return "?";
00258         }
00259     }    
00260     static Modifier modifier(String f) {
00261         f.downcase();
00262         
00263         if(f == "ctrl" || f == "control") return CONTROL;
00264         if(f == "shift") return SHIFT;
00265         if(f == "alt") return ALT;
00266         if(f == "f" || f == "function") return F;
00267         
00268         return F;
00269     }
00270     // </group>
00271     
00272 protected:
00273     PlotCanvas* m_canvas;    // Canvas
00274     char m_key;              // Character
00275     vector<Modifier> m_mods; // Modifiers
00276 };
00277 
00278 
00279 // Event for when a canvas or plotter is resized.
00280 class PlotResizeEvent : public virtual PlotEvent {
00281 public:
00282     // Canvas resize constructor, which takes the old and new sizes.
00283     PlotResizeEvent(PlotCanvas* canvas, int oldWidth, int oldHeight,
00284                     int newWidth, int newHeight);
00285     
00286     // Plotter resize constructor, which takes the old and new sizes.
00287     PlotResizeEvent(Plotter* plotter, int oldWidth, int oldHeight,
00288                     int newWidth, int newHeight);
00289     
00290     // Destructor.
00291     ~PlotResizeEvent();
00292     
00293     // Overrides PlotEvent::origin().
00294     void* origin() const {
00295         if(canvas() != NULL) return canvas();
00296         else                 return plotter();
00297     }
00298     
00299     // Canvas that was resized, or NULL if it was a plotter.
00300     PlotCanvas* canvas() const;
00301     
00302     // Plotter that was resize, or NULL if it was a canvas.
00303     Plotter* plotter() const;
00304     
00305     // Old size, in pixels.
00306     pair<int, int> oldSize() const;
00307     
00308     // New size, in pixels.
00309     pair<int, int> newSize() const;
00310     
00311 protected:
00312     Plotter* m_plotter;   // Plotter (or NULL)
00313     PlotCanvas* m_canvas; // Canvas (or NULL)
00314     pair<int, int> m_old; // Old size
00315     pair<int, int> m_new; // New size
00316 };
00317 
00318 
00319 // Event for when a PlotButton is pressed.
00320 class PlotButtonEvent : public virtual PlotEvent {
00321 public:
00322     // Constructor which takes the originating button.
00323     PlotButtonEvent(PlotButton* button);
00324     
00325     // Destructor.
00326     ~PlotButtonEvent();
00327     
00328     // Overrides PlotEvent::origin().
00329     void* origin() const { return button(); }
00330     
00331     // Returns the button that was clicked.
00332     PlotButton* button() const;
00333     
00334 protected:
00335     PlotButton* m_button; // button
00336 };
00337 
00338 // Event for when a PlotCheckbox is clicked.
00339 class PlotCheckboxEvent : public virtual PlotEvent {
00340 public:
00341     // Constructor which takes the originating checkbox.
00342     PlotCheckboxEvent(PlotCheckbox* checkbox);
00343     
00344     // Destructor.
00345     ~PlotCheckboxEvent();
00346     
00347     // Overrides PlotEvent::origin().
00348     void* origin() const { return checkbox(); }
00349     
00350     // Returns the checkbox that was clicked.
00351     PlotCheckbox* checkbox() const;
00352     
00353 protected:
00354     PlotCheckbox* m_checkbox; // checkbox
00355 };
00356 
00357 }
00358 
00359 #endif /*PLOTEVENT_H_*/