casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MWCPolyTool.h
Go to the documentation of this file.
00001 //# MWCPolyTool.h: Base class for MultiWorldCanvas event-based polygon tools
00002 //# Copyright (C) 1999,2000,2001,2002
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 
00028 #ifndef TRIALDISPLAY_MWCPOLYTOOL_H
00029 #define TRIALDISPLAY_MWCPOLYTOOL_H
00030 
00031 #include <casa/aips.h>
00032 #include <display/DisplayEvents/MultiWCTool.h>
00033 #include <display/DisplayEvents/DTVisible.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 // <summary>
00038 // Base class for WorldCanvas event-based polygon tools
00039 // </summary>
00040 //
00041 // <use visibility=export>
00042 //
00043 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00044 // </reviewed>
00045 //
00046 // <prerequisites>
00047 //   <li><linkto>WCTool</linkto>
00048 // </prerequisites>
00049 //
00050 // <etymology>
00051 // MWCPolyTool stands for Multi-WorldCanvas Polygon Tool
00052 // </etymology>
00053 //
00054 // <synopsis>
00055 // This class adds to its base MWCTool to provide a tool for drawing,
00056 // reshaping and moving polygons on a WorldCanvas.  While MWCPolyTool
00057 // is not abstract, it performs no useful function.  The programmer
00058 // should derive from this class and override the functions doubleInside
00059 // and doubleOutside at the very least.  These are called when the user
00060 // double-clicks a particular key or mouse button inside or outside an
00061 // existing polygon respectively.  It is up to the programmer to decide
00062 // what these events mean, but it is recommended that an internal double-
00063 // click correspond to the main action of the tool, eg. emitting the 
00064 // polygon vertices to the application, and that an external double-click
00065 // correspond to a secondary action of the tool, if indeed there are
00066 // additional actions suitable to the tool.
00067 //
00068 // The polygon is drawn by clicking at each of the vertices, and
00069 // clicking again on the last or first vertex to complete the polygon.
00070 // Once drawn, the vertices can be moved by dragging their handles,
00071 // and the entire polygon relocated by dragging inside the polygon.
00072 // The polygon is removed from the display when the Esc key is
00073 // pressed.
00074 // </synopsis>
00075 //
00076 // <example>
00077 // </example>
00078 //
00079 // <motivation>
00080 // Many activities on the WorldCanvas will be based on the user drawing
00081 // a polygon and using the polygon in some operation.
00082 // </motivation>
00083 //
00084 // <todo asof="1999/02/24">
00085 //   <li> Add time constraint to double click detection
00086 // </todo>
00087 
00088 class MWCPolyTool : public MultiWCTool, public DTVisible {
00089 
00090  public:
00091 
00092   // Constructor
00093   MWCPolyTool(Display::KeySym keysym = Display::K_Pointer_Button1,
00094              const Bool persistent = False);
00095 
00096   // Destructor
00097   virtual ~MWCPolyTool();
00098 
00099   // Switch the tool off - this calls the base class disable,
00100   // and then erases the polygon if it's around
00101   virtual void disable();
00102 
00103   // reset to non-existent, non-active polygon.
00104   // Refreshes if necessary to erase (unless skipRefresh==True).
00105   // (Does not unregister from WCs or disable future event handling).
00106   virtual void reset(Bool skipRefresh=False);
00107   
00108   // Is a polygon currently defined?
00109   virtual Bool polygonDefined() { return itsMode>=Ready;  } 
00110   
00111  protected:
00112 
00113   // Functions called by the base class event handling operators--and
00114   // normally only those.  This is the input that controls the polygon's
00115   // appearance and action.  When the polygon is ready and double-click
00116   // is received, the doubleInside/Outside routine is invoked.
00117   // <group>
00118   virtual void keyPressed(const WCPositionEvent &/*ev*/);
00119   virtual void moved(const WCMotionEvent &/*ev*/, const viewer::region::region_list_type & /*selected_regions*/);
00120   virtual void keyReleased(const WCPositionEvent &/*ev*/);
00121   virtual void otherKeyPressed(const WCPositionEvent &/*ev*/);
00122   // </group>
00123 
00124   // draw the polygon (if any) on the object's currently active WC.
00125   // Only to be called by the base class refresh event handler.  Derived
00126   // objects should use refresh() if they need to redraw, but even that
00127   // is normally handled automatically by this class.
00128   virtual void draw(const WCRefreshEvent&/*ev*/, const viewer::region::region_list_type & /*selected_regions*/);
00129 
00130   // Output callback functions--to be overridden in derived class as needed.
00131   // Called when there is a double click inside/outside the polygon
00132   // <group>
00133   virtual void doubleInside() { };
00134   virtual void doubleOutside() { };
00135   // </group>
00136 
00137   // Function called when a polygon is ready and not being 
00138   // edited.  (Unused so far on the glish level (12/01)).
00139   virtual void polygonReady() { };
00140 
00141   // Retrieve polygon vertices, or a single vertex, in screen pixels.
00142   // Valid results during the callback functions; to be used by them,
00143   // as well as internally.
00144   // <group>
00145   virtual void get(Vector<Int> &x, Vector<Int> &y) const;
00146   virtual void get(Int &x, Int &y, const Int pt) const;
00147   // </group>
00148 
00149  private:
00150 
00151   // Set the polygon vertices. itsNPoints should already be set, and
00152   // x and y must contain (at least) this many points.
00153   virtual void set(const Vector<Int> &x, const Vector<Int> &y);
00154 
00155   // replace a single vertex.
00156   virtual void set(const Int x, const Int y, const Int pt);
00157   // push/pop last vertex
00158   // <group>
00159   void pushPoint(Int x1, Int y1);
00160   void popPoint();
00161   // </group>
00162 
00163   // are we inside the polygon?
00164   Bool inPolygon(const Int &x, const Int &y) const;
00165 
00166   // are we within the specified handle?
00167   Bool inHandle(const Int &pt, const Int &x, const Int &y) const;
00168 
00169 
00170   // should the polygon remain on screen after double clicks?
00171   Bool itsPolygonPersistent;
00172 
00173   // state of the polygon tool
00174   enum AdjustMode {
00175     Off,        // Nothing exists yet
00176     Def,        // defining initial polygon
00177     Ready,      // polygon finished, no current activity
00178     Move,       // moving entire polygon
00179     Resize };   // moving single vertex whose handle was pressed
00180   MWCPolyTool::AdjustMode itsMode;
00181 
00182   // set True on double-click, if the polygon is persistent.
00183   // set False when the polygon is moved, resized or reset.
00184   // If True, a click outside the polygon will erase it and begin
00185   // definition of a new one.
00186   Bool itsEmitted;
00187 
00188   // Number of points
00189   Int itsNPoints;
00190 
00191   // Polygon points (linear).  Not to be used directly.
00192   // use get, set, push, pop instead, which take pixel coordinate arguments.
00193   // It's done this way so that zooms work on the figures.
00194   Vector<Double> itsX, itsY;
00195 
00196   // size in pixels of the handles
00197   Int itsHandleSize;
00198   
00199   // vertex being moved
00200   Int itsSelectedHandle;
00201 
00202   // position that move started from
00203   Int itsBaseMoveX, itsBaseMoveY;
00204 
00205   // times of the last two presses
00206   Double itsLastPressTime, its2ndLastPressTime;
00207 
00208 };
00209 
00210 
00211 } //# NAMESPACE CASA - END
00212 
00213 #endif
00214 
00215