Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:47

0001 /*************************************************************************
0002  * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers.               *
0003  * All rights reserved.                                                  *
0004  *                                                                       *
0005  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0006  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0007  *************************************************************************/
0008 
0009 #ifndef ROOT7_RPadBase
0010 #define ROOT7_RPadBase
0011 
0012 #include "ROOT/RDrawable.hxx"
0013 #include "ROOT/RFrame.hxx"
0014 #include "ROOT/RPadExtent.hxx"
0015 #include "ROOT/RPadPos.hxx"
0016 #include "ROOT/TypeTraits.hxx"
0017 
0018 #include <memory>
0019 #include <vector>
0020 #include <algorithm>
0021 
0022 namespace ROOT {
0023 namespace Experimental {
0024 
0025 class RPad;
0026 class RCanvas;
0027 class RPadBaseDisplayItem;
0028 
0029 /** \class ROOT::Experimental::RPadBase
0030 \ingroup GpadROOT7
0031 \brief Base class for graphic containers for `RDrawable`-s.
0032 \authors Axel Naumann <axel@cern.ch> Sergey Linev <s.linev@gsi.de>
0033 \date 2019-10-02
0034 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
0035 */
0036 
0037 class RPadBase : public RDrawable {
0038 
0039 private:
0040 
0041    using Primitive_t = Internal::RIOShared<RDrawable>;
0042 
0043    /// Content of the pad.
0044 
0045    std::vector<Primitive_t> fPrimitives;
0046 
0047    /// Disable copy construction.
0048    RPadBase(const RPadBase &) = delete;
0049 
0050    /// Disable assignment.
0051    RPadBase &operator=(const RPadBase &) = delete;
0052 
0053    void TestIfFrameRequired(const RDrawable *drawable)
0054    {
0055       if (drawable->IsFrameRequired())
0056          AddFrame();
0057    }
0058 
0059 protected:
0060    /// Allow derived classes to default construct a RPadBase.
0061    explicit RPadBase(const char *csstype) : RDrawable(csstype) {}
0062 
0063    void CollectShared(Internal::RIOSharedVector_t &) override;
0064 
0065    void DisplayPrimitives(RPadBaseDisplayItem &paditem, RDisplayContext &ctxt);
0066 
0067    void SetDrawableVersion(Version_t vers) override;
0068 
0069 public:
0070 
0071    using Primitives_t = std::vector<std::shared_ptr<RDrawable>>;
0072 
0073    ~RPadBase() override;
0074 
0075    void UseStyle(const std::shared_ptr<RStyle> &style) override;
0076 
0077    /// Add object to be painted.
0078    /// Correspondent drawable will be created via GetDrawable() function which should be defined and be accessed at calling time.
0079    /// If required, extra arguments for GetDrawable() function can be provided.
0080    template <class T, class... ARGS>
0081    auto Draw(const std::shared_ptr<T> &what, ARGS... args)
0082    {
0083       // Requires GetDrawable(what) to be known!
0084       auto drawable = GetDrawable(what, args...);
0085 
0086       TestIfFrameRequired(drawable.get());
0087 
0088       fPrimitives.emplace_back(drawable);
0089 
0090       return drawable;
0091    }
0092 
0093    /// Create drawable of specified class T
0094    template<class T, class... ARGS>
0095    std::shared_ptr<T> Draw(ARGS... args)
0096    {
0097       auto drawable = std::make_shared<T>(args...);
0098 
0099       TestIfFrameRequired(drawable.get());
0100 
0101       fPrimitives.emplace_back(drawable);
0102 
0103       return drawable;
0104    }
0105 
0106    /// Add drawable of specified class T
0107    template<class T, class... ARGS>
0108    std::shared_ptr<T> Add(ARGS... args)
0109    {
0110       auto drawable = std::make_shared<T>(args...);
0111 
0112       TestIfFrameRequired(drawable.get());
0113 
0114       fPrimitives.emplace_back(drawable);
0115 
0116       return drawable;
0117    }
0118 
0119    /// Add existing drawable instance to canvas
0120    std::shared_ptr<RDrawable> Draw(std::shared_ptr<RDrawable> &&drawable)
0121    {
0122       TestIfFrameRequired(drawable.get());
0123 
0124       auto dr = std::move(drawable);
0125 
0126       fPrimitives.emplace_back(dr);
0127 
0128       return dr;
0129    }
0130 
0131    /// returns number of primitives in the pad
0132    unsigned NumPrimitives() const { return fPrimitives.size(); }
0133 
0134    /// returns primitive of given number
0135    std::shared_ptr<RDrawable> GetPrimitive(unsigned num) const
0136    {
0137       if (num >= fPrimitives.size()) return nullptr;
0138       return fPrimitives[num].get_shared();
0139    }
0140 
0141    std::shared_ptr<RDrawable> FindPrimitive(const std::string &id) const;
0142 
0143    std::shared_ptr<RDrawable> FindPrimitiveByDisplayId(const std::string &display_id) const;
0144 
0145    const RPadBase *FindPadForPrimitiveWithDisplayId(const std::string &display_id) const;
0146 
0147    /// Get all primitives contained in the pad.
0148    auto GetPrimitives() const
0149    {
0150       Primitives_t res;
0151       for (auto &entry : fPrimitives)
0152          res.emplace_back(entry.get_shared());
0153       return res;
0154    }
0155 
0156    /// Remove an object from the list of primitives.
0157    bool Remove(const std::string &id)
0158    {
0159       auto iter = std::find_if(fPrimitives.begin(), fPrimitives.end(),
0160          [&id](const Internal::RIOShared<RDrawable>& dr) { return dr->GetId() == id; });
0161       if (iter == fPrimitives.end())
0162          return false;
0163       iter->reset();
0164       fPrimitives.erase(iter);
0165       return true;
0166    }
0167 
0168    /// Remove drawable from list of primitives
0169    bool Remove(const std::shared_ptr<RDrawable> &drawable)
0170    {
0171       auto iter = std::find_if(fPrimitives.begin(), fPrimitives.end(),
0172          [&drawable](const Internal::RIOShared<RDrawable>& dr) { return drawable.get() == dr.get(); });
0173       if (iter == fPrimitives.end())
0174          return false;
0175       iter->reset();
0176       fPrimitives.erase(iter);
0177       return true;
0178    }
0179 
0180    /// Remove drawable at specified position
0181    bool RemoveAt(unsigned indx)
0182    {
0183       if (indx >= fPrimitives.size()) return false;
0184       fPrimitives[indx].reset();
0185       fPrimitives.erase(fPrimitives.begin() + indx);
0186       return true;
0187    }
0188 
0189    /// Wipe the pad by clearing the list of primitives.
0190    void Wipe() { fPrimitives.clear(); }
0191 
0192    std::shared_ptr<RFrame> AddFrame();
0193    std::shared_ptr<RFrame> GetFrame();
0194    const std::shared_ptr<RFrame> GetFrame() const;
0195 
0196    std::shared_ptr<RPad> AddPad(const RPadPos &, const RPadExtent &);
0197 
0198    /// Divide this pad into a grid of subpads with padding in between.
0199    /// \param nHoriz Number of horizontal pads.
0200    /// \param nVert Number of vertical pads.
0201    /// \param padding Padding between pads.
0202    /// \returns vector of vector (ret[x][y]) of created pads.
0203    std::vector<std::vector<std::shared_ptr<RPad>>> Divide(int nHoriz, int nVert, const RPadExtent &padding = {});
0204 
0205    /// Access to the top-most canvas, if any (const version).
0206    virtual const RCanvas *GetCanvas() const = 0;
0207 
0208    /// Access to the top-most canvas, if any (non-const version).
0209    virtual RCanvas *GetCanvas() = 0;
0210 };
0211 
0212 } // namespace Experimental
0213 } // namespace ROOT
0214 
0215 #endif