PrefsWindow.H

Go to the documentation of this file.
00001 /*!@file GUI/PrefsWindow.H */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GUI/PrefsWindow.H $
00035 // $Id: PrefsWindow.H 8759 2007-09-06 19:24:13Z rjpeters $
00036 //
00037 
00038 #ifndef GUI_PREFSWINDOW_H_DEFINED
00039 #define GUI_PREFSWINDOW_H_DEFINED
00040 
00041 #include "Image/SimpleFont.H"
00042 #include "Util/StringConversions.H"
00043 
00044 #include <deque>
00045 #include <vector>
00046 #include <X11/Xlib.h>
00047 
00048 class ModelComponent;
00049 class ModelParamBase;
00050 class XWinManaged;
00051 
00052 class PrefItem;
00053 
00054 /// A simple X window to allow online manipulation of preferences values
00055 /** The window will show a list of your preference items. One of the
00056     items will always be the current item, highlighted by a different
00057     color.
00058 
00059     In "scrolling" mode, the default mode, you can do the following:
00060     - press up/down arrows to scroll to the previous/next item
00061     - press left/right arrows to change the current item's value by -1/+1
00062     - press < or > to change the current item's value by -10/+10
00063     - press return or enter to enter "editing" mode
00064 
00065     In "editing" mode, you can do the following:
00066     - enter a new value for the current item
00067     - press return or enter to accept the new value (or leave the
00068       current item's value unchanged if no new value was entered) and
00069       return to "scrolling" mode
00070 
00071     See PrefItem, PrefItemNum and PrefItemStr for useful preference
00072     item types that can be used with PrefsWindow.
00073  */
00074 class PrefsWindow
00075 {
00076 public:
00077 
00078   /// Construct with a list of prefence descriptors
00079   PrefsWindow(const std::string& wintitle = "preferences",
00080               const SimpleFont& font = SimpleFont::FIXED(10));
00081 
00082   ~PrefsWindow();
00083 
00084   /// Specify the number of characters in the value editing area
00085   void setValueNumChars(int n);
00086 
00087   /// Change the window's font
00088   void setFont(const SimpleFont& font);
00089 
00090   /// Call this periodically to retrieve new values of the preferences
00091   void update();
00092 
00093   /// Add preference items corresponding to the editable params of comp
00094   void addPrefForParam(ModelParamBase* mp, ModelComponent* comp);
00095 
00096   /// Add preference items corresponding to the editable params of comp
00097   void addPrefsForComponent(ModelComponent* comp, bool recurse = false);
00098 
00099 private:
00100   PrefsWindow(const PrefsWindow&);
00101   PrefsWindow& operator=(const PrefsWindow&);
00102 
00103   friend class PrefItem;
00104 
00105   void addItem(PrefItem* item, bool takeOwnership); // called by PrefItem's constructor
00106 
00107   bool handleKeysym(KeySym ks, const std::string& s);
00108   bool handleButtonPress(XButtonEvent* ev);
00109 
00110   void redraw();
00111 
00112   enum State { EDITING, SCROLLING };
00113 
00114   std::string itsWinTitle;
00115   XWinManaged* itsWin;
00116   SimpleFont itsFont;
00117   std::vector<PrefItem*> itsItems;
00118   std::vector<PrefItem*> itsOwnedItems;
00119   std::string itsEditBuffer;
00120   size_t itsCurrentItem;
00121   State itsState;
00122   int itsNameWidth;
00123   int itsNumWidth;
00124   bool itsDirty;
00125 };
00126 
00127 /// Base class for preference items to be used in a PrefsWindow
00128 class PrefItem
00129 {
00130 public:
00131   PrefItem(PrefsWindow* pwin, const std::string& nm,
00132            bool pwinTakesOwnership = false);
00133 
00134   virtual ~PrefItem();
00135 
00136   virtual std::string getName() const;
00137 
00138   virtual void fromString(const std::string& s) = 0;
00139 
00140   virtual std::string toString() const = 0;
00141 
00142   virtual void adjust(int val) = 0;
00143 
00144   bool isValueChanged();
00145 
00146   virtual bool isDisabled() const = 0;
00147 
00148 protected:
00149   std::string name;
00150   bool isChanged;
00151   bool wasDisabled;
00152 };
00153 
00154 /// Template class for numeric preference items
00155 template <class T>
00156 class PrefItemNum : public PrefItem
00157 {
00158 private:
00159   T val;
00160   bool disabled;
00161 
00162   template <class U>
00163   static void adjustHelper(U* val, int v)
00164   { *val += v; }
00165 
00166   static void adjustHelper(bool* val, int v)
00167   { *val = (int(*val) + v) % 2; }
00168 
00169 public:
00170   PrefItemNum(PrefsWindow* pwin, const std::string& nm, T v,
00171               bool pwinTakesOwnership = false)
00172     : PrefItem(pwin, nm, pwinTakesOwnership), val(v), disabled(false) {}
00173 
00174   void set(T v) { if (this->val != v) { this->val = v; this->isChanged = true; } }
00175   T get() const { return this->val; }
00176 
00177   virtual void fromString(const std::string& s)
00178   {
00179     if (s.size() == 0) return; // ignore empty strings
00180     try { this->set(fromStr<T>(s)); }
00181     catch (conversion_error& e) {} // ignore error; leave value unchanged
00182   }
00183 
00184   virtual std::string toString() const
00185   { return toStr(this->val); }
00186 
00187   virtual void adjust(int v)
00188   { adjustHelper(&this->val, v); this->isChanged = true; }
00189 
00190   virtual bool isDisabled() const { return disabled; }
00191 
00192   void setDisabled(bool v) { if (v != disabled) { disabled = v; isChanged = true; } }
00193 };
00194 
00195 typedef PrefItemNum<double> PrefItemDbl;
00196 typedef PrefItemNum<int>    PrefItemInt;
00197 typedef PrefItemNum<uint>   PrefItemUin;
00198 typedef PrefItemNum<byte>   PrefItemByt;
00199 typedef PrefItemNum<bool>   PrefItemBln;
00200 
00201 /// String preference item; includes value history
00202 class PrefItemStr : public PrefItem
00203 {
00204 private:
00205   std::deque<std::string> itsHist;
00206   const size_t itsMaxSize;
00207   size_t itsPos;
00208   bool disabled;
00209 
00210 public:
00211   PrefItemStr(PrefsWindow* pwin, const std::string& nm, const std::string& v,
00212               bool pwinTakesOwnership = false);
00213 
00214   void set(const std::string& v);
00215 
00216   std::string get() const;
00217 
00218   virtual std::string getName() const;
00219 
00220   virtual void fromString(const std::string& s);
00221 
00222   virtual std::string toString() const;
00223 
00224   /// Step through our value history by the specified amount
00225   virtual void adjust(int val);
00226 
00227   virtual bool isDisabled() const { return disabled; }
00228 
00229   void setDisabled(bool v) { if (v != disabled) { disabled = v; isChanged = true; } }
00230 };
00231 
00232 // ######################################################################
00233 /* So things look consistent in everyone's emacs... */
00234 /* Local Variables: */
00235 /* mode: c++ */
00236 /* indent-tabs-mode: nil */
00237 /* End: */
00238 
00239 #endif // GUI_PREFSWINDOW_H_DEFINED
Generated on Sun May 8 08:04:48 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3