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() {
00110                         return itsMode>=Ready;
00111                 }
00112 
00113         protected:
00114 
00115                 // Functions called by the base class event handling operators--and
00116                 // normally only those.  This is the input that controls the polygon's
00117                 // appearance and action.  When the polygon is ready and double-click
00118                 // is received, the doubleInside/Outside routine is invoked.
00119                 // <group>
00120                 virtual void keyPressed(const WCPositionEvent &/*ev*/);
00121                 virtual void moved(const WCMotionEvent &/*ev*/, const viewer::region::region_list_type & /*selected_regions*/);
00122                 virtual void keyReleased(const WCPositionEvent &/*ev*/);
00123                 virtual void otherKeyPressed(const WCPositionEvent &/*ev*/);
00124                 // </group>
00125 
00126                 // draw the polygon (if any) on the object's currently active WC.
00127                 // Only to be called by the base class refresh event handler.  Derived
00128                 // objects should use refresh() if they need to redraw, but even that
00129                 // is normally handled automatically by this class.
00130                 virtual void draw(const WCRefreshEvent&/*ev*/, const viewer::region::region_list_type & /*selected_regions*/);
00131 
00132                 // Output callback functions--to be overridden in derived class as needed.
00133                 // Called when there is a double click inside/outside the polygon
00134                 // <group>
00135                 virtual void doubleInside() { };
00136                 virtual void doubleOutside() { };
00137                 // </group>
00138 
00139                 // Function called when a polygon is ready and not being
00140                 // edited.  (Unused so far on the glish level (12/01)).
00141                 virtual void polygonReady() { };
00142 
00143                 // Retrieve polygon vertices, or a single vertex, in screen pixels.
00144                 // Valid results during the callback functions; to be used by them,
00145                 // as well as internally.
00146                 // <group>
00147                 virtual void get(Vector<Int> &x, Vector<Int> &y) const;
00148                 virtual void get(Int &x, Int &y, const Int pt) const;
00149                 // </group>
00150 
00151         private:
00152 
00153                 // Set the polygon vertices. itsNPoints should already be set, and
00154                 // x and y must contain (at least) this many points.
00155                 virtual void set(const Vector<Int> &x, const Vector<Int> &y);
00156 
00157                 // replace a single vertex.
00158                 virtual void set(const Int x, const Int y, const Int pt);
00159                 // push/pop last vertex
00160                 // <group>
00161                 void pushPoint(Int x1, Int y1);
00162                 void popPoint();
00163                 // </group>
00164 
00165                 // are we inside the polygon?
00166                 Bool inPolygon(const Int &x, const Int &y) const;
00167 
00168                 // are we within the specified handle?
00169                 Bool inHandle(const Int &pt, const Int &x, const Int &y) const;
00170 
00171 
00172                 // should the polygon remain on screen after double clicks?
00173                 Bool itsPolygonPersistent;
00174 
00175                 // state of the polygon tool
00176                 enum AdjustMode {
00177                     Off,        // Nothing exists yet
00178                     Def,        // defining initial polygon
00179                     Ready,      // polygon finished, no current activity
00180                     Move,       // moving entire polygon
00181                     Resize
00182                 };      // moving single vertex whose handle was pressed
00183                 MWCPolyTool::AdjustMode itsMode;
00184 
00185                 // set True on double-click, if the polygon is persistent.
00186                 // set False when the polygon is moved, resized or reset.
00187                 // If True, a click outside the polygon will erase it and begin
00188                 // definition of a new one.
00189                 Bool itsEmitted;
00190 
00191                 // Number of points
00192                 Int itsNPoints;
00193 
00194                 // Polygon points (linear).  Not to be used directly.
00195                 // use get, set, push, pop instead, which take pixel coordinate arguments.
00196                 // It's done this way so that zooms work on the figures.
00197                 Vector<Double> itsX, itsY;
00198 
00199                 // size in pixels of the handles
00200                 Int itsHandleSize;
00201 
00202                 // vertex being moved
00203                 Int itsSelectedHandle;
00204 
00205                 // position that move started from
00206                 Int itsBaseMoveX, itsBaseMoveY;
00207 
00208                 // times of the last two presses
00209                 Double itsLastPressTime, its2ndLastPressTime;
00210 
00211         };
00212 
00213 
00214 } //# NAMESPACE CASA - END
00215 
00216 #endif
00217 
00218 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1