NitfPropertiesQWidgetWrapper.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2011 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef PROPERTIESQWIDGETWRAPPER_H
00011 #define PROPERTIESQWIDGETWRAPPER_H
00012 
00013 #include "NitfProperties.h"
00014 #include "PlugInManagerServices.h"
00015 #include "PlugInShell.h"
00016 
00017 #include <memory>
00018 #include <string>
00019 
00020 namespace Nitf
00021 {
00022 
00023 /**
00024  *  \ingroup ShellModule
00025  *  A plug-in that both implements the Properties and Nitf::Properties interfaces and provides a
00026  *  QWidget.
00027  *
00028  *  This templated class is used by writing a QWidget subclass and
00029  *  then doing the following to register the plug-in:
00030  *
00031  * @code
00032  *    //assume SampleTreProperties is a subclass of QWidget and
00033  *    //the plug-in belongs to a module that was registered
00034  *    //using REGISTER_MODULE(SampleTreModule);
00035  *    REGISTER_PLUGIN(SampleTreModule, SampleTreProperties,
00036  *                    Nitf::PropertiesQWidgetWrapper<SampleTreProperties>());
00037  * @endcode
00038  *
00039  *  The templated argument must meet the following requirements:
00040  *  - Subclasses QWidget.
00041  *  - Provides a default constructor.
00042  *  - Provides a static getDescription() method.  @see PlugIn::getDescription()
00043  *  - Provides a static getVersion() method.  @see PlugIn::getVersion()
00044  *  - Provides a static getCreator() method.  @see PlugIn::getCreator()
00045  *  - Provides a static getCopyright() method.  @see PlugIn::getCopyright()
00046  *  - Provides a static getShortDescription() method.  @see PlugIn::getShortDescription()
00047  *  - Provices a static getTypeName() method. @see Nitf::Properties::getTypeName()
00048  *  - Provides a static isProduction() method.  @see PlugIn::isProduction()
00049  *  - Provides an applyChanges() method.  @see Properties::applyChanges()
00050  *  - Provides a canDisplayMetadata() method. @see Nitf::Properties::canDisplayMetadata()
00051  *
00052  * The plug-in name will be "Properties " + PropertiesWidget::getTypeName().
00053  */
00054 template <typename PropertiesWidget>
00055 class PropertiesQWidgetWrapper : public Nitf::Properties, public PlugInShell
00056 {
00057 public:
00058    /**
00059     *  Instantiates this properties plug-in by querying the templated class
00060     *  using its provided static methods.
00061     */
00062    PropertiesQWidgetWrapper() : mpWidget(NULL)
00063    {
00064       PlugInShell::setName(std::string("Properties ") + PropertiesWidget::getTypeName());
00065       setDescription(PropertiesWidget::getDescription());
00066       setVersion(PropertiesWidget::getVersion());
00067       setCreator(PropertiesWidget::getCreator());
00068       setCopyright(PropertiesWidget::getCopyright());
00069       setShortDescription(PropertiesWidget::getShortDescription());
00070       setProductionStatus(PropertiesWidget::isProduction());
00071       setDescriptorId(PropertiesWidget::getDescriptorId());
00072       setType(PlugInManagerServices::PropertiesType());
00073       setSubtype(Properties::SubType());
00074    }
00075 
00076    /**
00077     *  Destroys this properties plug-in.
00078     */
00079    virtual ~PropertiesQWidgetWrapper()
00080    {
00081    }
00082 
00083    /**
00084     *  Initializes the widget with a SessionItem by calling initialize() on the templated class.
00085     *
00086     *  @param   pItem
00087     *           The session item for which to initialize the widget's values.
00088     *
00089     *  @return  Returns \c true if the widget was successfully initialized from
00090     *           the given session item; otherwise returns \c false.  If this
00091     *           method returns \c false, the widget is not added to the
00092     *           properties dialog.
00093     */
00094    bool initialize(SessionItem* pItem)
00095    {
00096       PropertiesWidget* pWidget = dynamic_cast<PropertiesWidget*>(getWidget());
00097       if (pWidget != NULL)
00098       {
00099          return pWidget->initialize(pItem);
00100       }
00101 
00102       return false;
00103    }
00104 
00105    /**
00106     *  Applies the changes in the widget.
00107     *
00108     *  This method is called when the user clicks the OK or Apply buttons in
00109     *  the properties dialog.  It applies the changes in the widget by calling
00110     *  applyChanges() on the templated class.  The applyChanges() method of the
00111     *  templated class should return \c true if all changes were successfully
00112     *  applied to the object.  It should also return \c true if no updates need
00113     *  to be made or if the widget is for display purposes only.
00114     *
00115     *  @return  This method returns the value returned from applyChanges() of
00116     *           the templated class or \c NULL if the widget has not been
00117     *           created.  If \c false is returned, the widget will be activated
00118     *           in the properties dialog if necessary and the dialog will not
00119     *           be closed if the OK button was clicked.
00120     */
00121    bool applyChanges()
00122    {
00123       PropertiesWidget* pWidget = dynamic_cast<PropertiesWidget*>(getWidget());
00124       if (pWidget != NULL)
00125       {
00126          return pWidget->applyChanges();
00127       }
00128 
00129       return false;
00130    }
00131 
00132    /**
00133     * @copydoc Nitf::Properties::getTypeName()
00134     */
00135    virtual std::string getTypeName() const
00136    {
00137       return PropertiesWidget::getTypeName();
00138    }
00139 
00140    /**
00141     * @copydoc Nitf::Properties::canDisplayMetadata()
00142     */
00143    virtual bool canDisplayMetadata(const DynamicObject& metadata)
00144    {
00145       const PropertiesWidget* pWidget = dynamic_cast<const PropertiesWidget*>(getWidget());
00146       if (pWidget != NULL)
00147       {
00148          return pWidget->canDisplayMetadata(metadata);
00149       }
00150       return false;
00151    }
00152 
00153    /**
00154     * @copydoc Properties::getWidget()
00155     */
00156    QWidget* getWidget()
00157    {
00158       if (mpWidget.get() == NULL)
00159       {
00160          mpWidget.reset(new PropertiesWidget());
00161       }
00162       return mpWidget.get();
00163    }
00164 
00165    /**
00166     * @copydoc Properties::getPropertiesName()
00167     */
00168    const std::string& getPropertiesName() const
00169    {
00170       return PropertiesWidget::getTypeName();
00171    }
00172 
00173 private:
00174    std::auto_ptr<QWidget> mpWidget;
00175 };
00176 
00177 }
00178 
00179 #endif

Software Development Kit - Opticks 4.9.0 Build 16218