Jamba  3.2.0
CustomViewCreator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <vstgui4/vstgui/uidescription/iviewcreator.h>
21 #include <vstgui4/vstgui/uidescription/uiviewcreator.h>
22 #include <vstgui4/vstgui/uidescription/uiviewfactory.h>
23 #include <vstgui4/vstgui/uidescription/uiattributes.h>
24 #include <vstgui4/vstgui/uidescription/detail/uiviewcreatorattributes.h>
25 #include <vstgui4/vstgui/lib/crect.h>
26 #include <vstgui4/vstgui/lib/ccolor.h>
27 #include <vstgui4/vstgui/lib/cbitmap.h>
28 #include <map>
29 #include <memory>
30 #include <pongasoft/logging/logging.h>
34 
35 namespace pongasoft {
36 namespace VST {
37 namespace GUI {
38 namespace Views {
39 
40 using namespace VSTGUI;
41 
46 {
47 public:
48  // Constructor
49  explicit ViewAttribute(std::string iName) :
50  fName(std::move(iName))
51  {}
52 
56  virtual IViewCreator::AttrType getType() = 0;
57 
62  std::string getName() const
63  {
64  return fName;
65  }
66 
73  virtual bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) = 0;
74 
81  virtual bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const = 0;
82 
83 private:
84  std::string fName;
85 };
86 
91 template<typename TView>
92 inline TView *createCustomView(CRect const &iSize,
93  const UIAttributes &iAttributes,
94  const IUIDescription *iDescription) { return new TView(iSize); }
95 
122 template<typename TView>
123 class TCustomViewCreator : public ViewCreatorAdapter
124 {
125 private:
131  template<typename T, typename TGetter, typename TSetter>
132  class TAttribute : public ViewAttribute
133  {
134  public:
135  using Getter = TGetter;
136  using Setter = TSetter;
137 
138  // Constructor
139  TAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
140  ViewAttribute(iName), fGetter{iGetter}, fSetter{iSetter} { }
141 
142  // getType
143  IViewCreator::AttrType getType() override
144  {
145  return IViewCreator::kUnknownType;
146  }
147 
152  virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
153  {
154  return false;
155  }
156 
161  virtual bool toString(IUIDescription const *iDescription, T const &iValue, std::string &oStringValue) const
162  {
163  return false;
164  }
165 
166  // apply => set a color to the view
167  bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
168  {
169  auto *tv = dynamic_cast<TView *>(iView);
170  if(tv != nullptr)
171  {
172  auto attributeValue = iAttributes.getAttributeValue(getName());
173  if(attributeValue)
174  {
175  T value;
176  if(fromString(iDescription, *attributeValue, value))
177  {
178  (tv->*fSetter)(value);
179  return true;
180  }
181  }
182  }
183  return false;
184  }
185 
186  // getAttributeValue => get a color from the view
187  bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const override
188  {
189  auto *tv = dynamic_cast<TView *>(iView);
190  if(tv != nullptr)
191  {
192  auto value = (tv->*fGetter)();
193  return toString(iDescription, value, oStringValue);
194  }
195  return false;
196  }
197 
198  private:
201  };
202 
206  template<typename T>
207  using ByValAttribute = TAttribute<T, T (TView::*)() const, void (TView::*)(T)>;
208 
212  template<typename T>
213  using ByRefAttribute = TAttribute<T, T const &(TView::*)() const, void (TView::*)(T const &)>;
214 
219  class TagAttribute : public ByValAttribute<int32_t>
220  {
221  public:
222  TagAttribute(std::string const &iName,
223  typename ByValAttribute<int32_t>::Getter iGetter,
224  typename ByValAttribute<int32_t>::Setter iSetter) :
225  ByValAttribute<int32_t>(iName, iGetter, iSetter) {}
226 
227  // getType
228  IViewCreator::AttrType getType() override
229  {
230  return IViewCreator::kTagType;
231  }
232 
233  // fromString
234  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, int32_t &oValue) const override
235  {
236  if(iAttributeValue.length() != 0)
237  {
238  auto tag = iDescription->getTagForName(iAttributeValue.c_str());
239  if(tag == -1)
240  {
241  char *endPtr = nullptr;
242  tag = (int32_t) strtol(iAttributeValue.c_str(), &endPtr, 10);
243  if(endPtr == iAttributeValue.c_str())
244  {
245  return false;
246  }
247  }
248  oValue = tag;
249  return true;
250  }
251  return false;
252  }
253 
254  // toString
255  bool toString(IUIDescription const *iDescription, const int32_t &iValue, std::string &oStringValue) const override
256  {
257  if(iValue != -1)
258  {
259  UTF8StringPtr controlTag = iDescription->lookupControlTagName(iValue);
260  if(controlTag)
261  {
262  oStringValue = controlTag;
263  return true;
264  }
265  }
266 
267  return false;
268  }
269  };
270 
275  template<typename TInt>
276  class IntegerAttribute : public ByValAttribute<TInt>
277  {
278  public:
281 
282  // Constructor
283  IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
284  ByValAttribute<TInt>(iName, iGetter, iSetter) {}
285 
286  // getType
287  IViewCreator::AttrType getType() override
288  {
289  return IViewCreator::kIntegerType;
290  }
291 
292  // fromString
293  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
294  {
295  char *endPtr = nullptr;
296  auto value = static_cast<TInt>(strtol(iAttributeValue.c_str(), &endPtr, 10));
297  if(endPtr == iAttributeValue.c_str())
298  {
299  DLOG_F(WARNING, "could not convert <%s> to an integer", iAttributeValue.c_str());
300  return false;
301  }
302  oValue = value;
303  return true;
304  }
305 
306  // toString
307  bool toString(IUIDescription const *iDescription, const TInt &iValue, std::string &oStringValue) const override
308  {
309  std::stringstream str;
310  str << iValue;
311  oStringValue = str.str();
312  return true;
313  }
314  };
315 
320  template<typename TFloat>
321  class FloatAttribute : public ByValAttribute<TFloat>
322  {
323  public:
326 
327  // Constructor
328  FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
329  ByValAttribute<TFloat>(iName, iGetter, iSetter) {}
330 
331  // getType
332  IViewCreator::AttrType getType() override
333  {
334  return IViewCreator::kFloatType;
335  }
336 
337  // fromString
338  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
339  {
340  TFloat value;
341  if(Utils::stringToFloat<TFloat>(iAttributeValue, value))
342  {
343  oValue = value;
344  return true;
345  }
346  DLOG_F(WARNING, "could not convert <%s> to a float", iAttributeValue.c_str());
347  return false;
348  }
349 
350  // toString
351  bool toString(IUIDescription const *iDescription, const TFloat &iValue, std::string &oStringValue) const override
352  {
353  std::stringstream str;
354  str << iValue;
355  oStringValue = str.str();
356  return true;
357  }
358  };
359 
364  class ColorAttribute : public ByRefAttribute<CColor>
365  {
366  public:
367 
368  // Constructor
369  ColorAttribute(std::string const &iName,
370  typename ByRefAttribute<CColor>::Getter iGetter,
371  typename ByRefAttribute<CColor>::Setter iSetter) :
372  ByRefAttribute<CColor>(iName, iGetter, iSetter) {}
373 
374  // getType
375  IViewCreator::AttrType getType() override
376  {
377  return IViewCreator::kColorType;
378  }
379 
380  // fromString
381  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
382  {
383  CColor color;
384  if(UIViewCreator::stringToColor(&iAttributeValue, color, iDescription))
385  {
386  oValue = color;
387  return true;
388  }
389  return false;
390  }
391 
392  // toString
393  bool toString(IUIDescription const *iDescription, const CColor &iValue, std::string &oStringValue) const override
394  {
395  return UIViewCreator::colorToString(iValue, oStringValue, iDescription);
396  }
397  };
398 
403  class GradientAttribute : public ByValAttribute<GradientPtr>
404  {
405  public:
406 
407  // Constructor
408  GradientAttribute(std::string const &iName,
409  typename ByValAttribute<GradientPtr>::Getter iGetter,
410  typename ByValAttribute<GradientPtr>::Setter iSetter) :
411  ByValAttribute<GradientPtr>(iName, iGetter, iSetter) {}
412 
413  // getType
414  IViewCreator::AttrType getType() override
415  {
416  return IViewCreator::kGradientType;
417  }
418 
419  // fromString
420  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
421  {
422  auto gradient = iDescription->getGradient(iAttributeValue.c_str());
423  if(gradient)
424  {
425  oValue = gradient;
426  return true;
427  }
428  return false;
429  }
430 
431  // toString
432  bool toString(IUIDescription const *iDescription, GradientPtr const &iValue, std::string &oStringValue) const override
433  {
434  if(iValue)
435  {
436  auto name = iDescription->lookupGradientName(iValue);
437  if(name)
438  {
439  oStringValue = name;
440  return true;
441  }
442 
443  }
444  return false;
445  }
446  };
447 
452  class BooleanAttribute : public ByValAttribute<bool>
453  {
454  public:
455  // Constructor
456  BooleanAttribute(std::string const &iName,
457  typename ByValAttribute<bool>::Getter iGetter,
458  typename ByValAttribute<bool>::Setter iSetter) :
459  ByValAttribute<bool>(iName, iGetter, iSetter) {}
460 
461  // getType
462  IViewCreator::AttrType getType() override
463  {
464  return IViewCreator::kBooleanType;
465  }
466 
467  // fromString
468  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
469  {
470  if(iAttributeValue == "true")
471  {
472  oValue = true;
473  return true;
474  }
475 
476  if(iAttributeValue == "false")
477  {
478  oValue = false;
479  return true;
480  }
481 
482  return false;
483  }
484 
485  // toString
486  bool toString(IUIDescription const *iDescription, const bool &iValue, std::string &oStringValue) const override
487  {
488  oStringValue = iValue ? "true" : "false";
489  return true;
490  }
491  };
492 
497  class BitmapAttribute : public ByValAttribute<BitmapPtr>
498  {
499  public:
500  // Constructor
501  BitmapAttribute(std::string const &iName,
502  typename ByValAttribute<BitmapPtr>::Getter iGetter,
503  typename ByValAttribute<BitmapPtr>::Setter iSetter) :
504  ByValAttribute<BitmapPtr>(iName, iGetter, iSetter) {}
505 
506  // getType
507  IViewCreator::AttrType getType() override
508  {
509  return IViewCreator::kBitmapType;
510  }
511 
512  // fromString
513  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
514  {
515  BitmapPtr bitmap;
516  if(UIViewCreator::stringToBitmap(&iAttributeValue, bitmap, iDescription))
517  {
518  oValue = bitmap;
519  return true;
520  }
521  return false;
522  }
523 
524  // toString
525  bool toString(IUIDescription const *iDescription, BitmapPtr const &iValue, std::string &oStringValue) const override
526  {
527  if(iValue)
528  return UIViewCreator::bitmapToString(iValue, oStringValue, iDescription);
529  else
530  return false;
531  }
532  };
533 
538  class FontAttribute : public ByValAttribute<FontPtr>
539  {
540  public:
541  // Constructor
542  FontAttribute(std::string const &iName,
543  typename ByValAttribute<FontPtr>::Getter iGetter,
544  typename ByValAttribute<FontPtr>::Setter iSetter) :
545  ByValAttribute<FontPtr>(iName, iGetter, iSetter) {}
546 
547  // getType
548  IViewCreator::AttrType getType() override
549  {
550  return IViewCreator::kFontType;
551  }
552 
553  // fromString
554  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
555  {
556  auto font = iDescription->getFont(iAttributeValue.c_str());
557  if(font)
558  {
559  oValue = font;
560  return true;
561  }
562  return false;
563  }
564 
565  // toString
566  bool toString(IUIDescription const *iDescription, FontPtr const &iValue, std::string &oStringValue) const override
567  {
568  if(iValue)
569  {
570  auto fontName = iDescription->lookupFontName(iValue);
571  if(fontName)
572  {
573  oStringValue = fontName;
574  return true;
575  }
576  }
577  return false;
578  }
579  };
580 
585  class MarginAttribute : public ByRefAttribute<Margin>
586  {
587  public:
588  MarginAttribute(std::string const &iName,
589  typename ByRefAttribute<Margin>::Getter iGetter,
590  typename ByRefAttribute<Margin>::Setter iSetter) :
591  ByRefAttribute<Margin>(iName, iGetter, iSetter) {}
592 
593  // fromString
594  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
595  {
596  auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
597 
598  if(parts.empty())
599  return false;
600 
601  // look for nan in the array
602  if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
603  return false;
604 
605  if(parts.size() == 1)
606  {
607  oValue = Margin{parts[0]};
608  }
609  else
610  {
611  if(parts.size() < 4)
612  return false;
613 
614  oValue = Margin{parts[0], parts[1], parts[2], parts[3]};
615  }
616 
617  return true;
618  }
619 
620  // toString
621  bool toString(IUIDescription const *iDescription, const Margin &iValue, std::string &oStringValue) const override
622  {
623  std::stringstream str;
624  if(iValue.fTop == iValue.fRight && iValue.fTop == iValue.fBottom && iValue.fTop == iValue.fLeft)
625  str << iValue.fTop;
626  else
627  {
628  str << iValue.fTop;
629  str << ",";
630  str << iValue.fRight;
631  str << ",";
632  str << iValue.fBottom;
633  str << ",";
634  str << iValue.fLeft;
635  }
636  oStringValue = str.str();
637  return true;
638  }
639  };
640 
645  class RangeAttribute : public ByRefAttribute<Range>
646  {
647  public:
648  RangeAttribute(std::string const &iName,
649  typename ByRefAttribute<Range>::Getter iGetter,
650  typename ByRefAttribute<Range>::Setter iSetter) :
651  ByRefAttribute<Range>(iName, iGetter, iSetter) {}
652 
653  // fromString
654  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
655  {
656  auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
657 
658  if(parts.empty())
659  return false;
660 
661  // look for nan in the array
662  if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
663  return false;
664 
665  if(parts.size() == 1)
666  oValue = Range{parts[0]};
667  else
668  oValue = Range{parts[0], parts[1]};
669 
670  return true;
671  }
672 
673  // toString
674  bool toString(IUIDescription const *iDescription, const Range &iValue, std::string &oStringValue) const override
675  {
676  std::stringstream str;
677  if(iValue.fFrom == iValue.fTo)
678  str << iValue.fFrom;
679  else
680  {
681  str << iValue.fFrom;
682  str << ",";
683  str << iValue.fTo;
684  }
685  oStringValue = str.str();
686  return true;
687  }
688  };
689 
693  class VectorStringAttribute : public ByRefAttribute<std::vector<std::string>>
694  {
696 
697  public:
698  // Constructor
699  VectorStringAttribute(std::string const &iName,
700  typename super_type::Getter iGetter,
701  typename super_type::Setter iSetter,
702  char iDelimiter = ',',
703  bool iSkipEmptyEntries = false) :
704  super_type(iName, iGetter, iSetter),
705  fDelimiter{iDelimiter},
706  fSkipEmptyEntries{iSkipEmptyEntries}
707  {}
708 
709  // fromString
710  bool fromString(IUIDescription const *iDescription,
711  std::string const &iAttributeValue,
712  std::vector<std::string> &oValue) const override
713  {
714  oValue = Utils::splitString(iAttributeValue, fDelimiter, fSkipEmptyEntries);
715  return true;
716  }
717 
718  // toString
719  bool toString(IUIDescription const *iDescription,
720  const std::vector<std::string> &iValue,
721  std::string &oStringValue) const override
722  {
723  oStringValue.clear();
724  int i = 0;
725  for(auto &entry : iValue)
726  {
727  if(i > 0)
728  oStringValue += fDelimiter;
729  if(!entry.empty() || !fSkipEmptyEntries)
730  {
731  oStringValue += entry;
732  i++;
733  }
734  }
735  return true;
736  }
737 
738  protected:
741  };
742 
743 public:
744  // Constructor
745  explicit TCustomViewCreator(char const *iViewName = nullptr,
746  char const *iDisplayName = nullptr,
747  char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
748  fViewName{iViewName},
749  fDisplayName{iDisplayName},
750  fBaseViewName{iBaseViewName},
751  fAttributes{}
752  {
753  // this allows for inheritance!
754  if(iViewName != nullptr && iDisplayName != nullptr)
755  VSTGUI::UIViewFactory::registerViewCreator(*this);
756  }
757 
758  // Destructor
760  {
761  // we simply clear the map since it holds shared pointers which will be discarded when they are no longer
762  // held by another object
763  fAttributes.clear();
764  }
765 
766  // getViewName
767  IdStringPtr getViewName() const override
768  {
769  return fViewName;
770  }
771 
772  // getDisplayName
773  UTF8StringPtr getDisplayName() const override
774  {
775  return fDisplayName;
776  }
777 
778  // getBaseViewName
779  IdStringPtr getBaseViewName() const override
780  {
781  return fBaseViewName;
782  }
783 
787  template<typename XView>
789  {
790  for(auto attribute : iOther.fAttributes)
791  {
792  registerAttribute(attribute.second);
793  }
794 
795  if(std::string(fBaseViewName) == std::string(VSTGUI::UIViewCreator::kCView))
796  fBaseViewName = iOther.fBaseViewName;
797  }
798 
802  void registerColorAttribute(std::string const &iName,
803  typename ColorAttribute::Getter iGetter,
804  typename ColorAttribute::Setter iSetter)
805  {
806  registerAttribute<ColorAttribute>(iName, iGetter, iSetter);
807  }
808 
812  void registerGradientAttribute(std::string const &iName,
813  typename GradientAttribute::Getter iGetter,
814  typename GradientAttribute::Setter iSetter)
815  {
816  registerAttribute<GradientAttribute>(iName, iGetter, iSetter);
817  }
818 
822  void registerBitmapAttribute(std::string const &iName,
823  typename BitmapAttribute::Getter iGetter,
824  typename BitmapAttribute::Setter iSetter)
825  {
826  registerAttribute<BitmapAttribute>(iName, iGetter, iSetter);
827  }
828 
832  void registerFontAttribute(std::string const &iName,
833  typename FontAttribute::Getter iGetter,
834  typename FontAttribute::Setter iSetter)
835  {
836  registerAttribute<FontAttribute>(iName, iGetter, iSetter);
837  }
838 
842  void registerMarginAttribute(std::string const &iName,
843  typename MarginAttribute::Getter iGetter,
844  typename MarginAttribute::Setter iSetter)
845  {
846  registerAttribute<MarginAttribute>(iName, iGetter, iSetter);
847  }
848 
852  void registerRangeAttribute(std::string const &iName,
853  typename RangeAttribute::Getter iGetter,
854  typename RangeAttribute::Setter iSetter)
855  {
856  registerAttribute<RangeAttribute>(iName, iGetter, iSetter);
857  }
858 
862  void registerVectorStringAttribute(std::string const &iName,
863  typename VectorStringAttribute::Getter iGetter,
864  typename VectorStringAttribute::Setter iSetter,
865  char iDelimiter = ',',
866  bool iSkipEmptyEntries = false)
867  {
868  registerAttribute<VectorStringAttribute>(iName, iGetter, iSetter, iDelimiter, iSkipEmptyEntries);
869  }
870 
874  void registerTagAttribute(std::string const &iName,
875  typename TagAttribute::Getter iGetter,
876  typename TagAttribute::Setter iSetter)
877  {
878  registerAttribute<TagAttribute>(iName, iGetter, iSetter);
879  }
880 
884  template<typename TInt>
885  void registerIntegerAttribute(std::string const &iName,
886  typename IntegerAttribute<TInt>::Getter iGetter,
887  typename IntegerAttribute<TInt>::Setter iSetter)
888  {
889  registerAttribute<IntegerAttribute<TInt>>(iName, iGetter, iSetter);
890  }
891 
895  void registerIntAttribute(std::string const &iName,
896  typename IntegerAttribute<int32_t>::Getter iGetter,
897  typename IntegerAttribute<int32_t>::Setter iSetter)
898  {
899  registerIntegerAttribute<int32_t>(iName, iGetter, iSetter);
900  }
901 
905  void registerFloatAttribute(std::string const &iName,
906  typename FloatAttribute<float>::Getter iGetter,
907  typename FloatAttribute<float>::Setter iSetter)
908  {
909  registerAttribute<FloatAttribute<float>>(iName, iGetter, iSetter);
910  }
911 
915  void registerDoubleAttribute(std::string const &iName,
916  typename FloatAttribute<double>::Getter iGetter,
917  typename FloatAttribute<double>::Setter iSetter)
918  {
919  registerAttribute<FloatAttribute<double>>(iName, iGetter, iSetter);
920  }
921 
925  void registerBooleanAttribute(std::string const &iName,
926  typename BooleanAttribute::Getter iGetter,
927  typename BooleanAttribute::Setter iSetter)
928  {
929  registerAttribute<BooleanAttribute>(iName, iGetter, iSetter);
930  }
931 
935  CView *create(const UIAttributes &attributes, const IUIDescription *description) const override
936  {
937 #ifdef JAMBA_DEBUG_LOGGING
938  DLOG_F(INFO, "CustomViewCreator<%s>::create()", getViewName());
939 #endif
940 
941  return createCustomView<TView>(CRect(0, 0, 0, 0), attributes, description);
942  }
943 
948  bool apply(CView *view, const UIAttributes &attributes, const IUIDescription *description) const override
949  {
950  auto *tv = dynamic_cast<TView *>(view);
951 
952  if(tv == nullptr)
953  return false;
954 
955  for(auto attribute : fAttributes)
956  {
957  attribute.second->apply(tv, attributes, description);
958  }
959 
960  return true;
961  }
962 
963  // getAttributeNames
964  bool getAttributeNames(std::list<std::string> &attributeNames) const override
965  {
966  for(auto attribute : fAttributes)
967  {
968  attributeNames.emplace_back(attribute.first);
969  }
970  return true;
971  }
972 
973  // getAttributeType
974  AttrType getAttributeType(const std::string &attributeName) const override
975  {
976  auto iter = fAttributes.find(attributeName);
977  if(iter != fAttributes.cend())
978  {
979  return iter->second->getType();
980  }
981  return kUnknownType;
982  }
983 
987  bool getAttributeValue(CView *iView,
988  const std::string &iAttributeName,
989  std::string &oStringValue,
990  const IUIDescription *iDescription) const override
991  {
992  auto *cdv = dynamic_cast<TView *>(iView);
993 
994  if(cdv == nullptr)
995  return false;
996 
997  auto iter = fAttributes.find(iAttributeName);
998  if(iter != fAttributes.cend())
999  {
1000  return iter->second->getAttributeValue(cdv, iDescription, oStringValue);
1001  }
1002 
1003  return false;
1004  }
1005 
1006 private:
1007 
1008  // somehow this is required...
1009  template<typename XView>
1010  friend class TCustomViewCreator;
1011 
1015  void registerAttribute(std::shared_ptr<ViewAttribute> iAttribute)
1016  {
1017  // making sure there are no duplicates (cannot use loguru here!)
1018  assert(fAttributes.find(iAttribute->getName()) == fAttributes.cend());
1019  fAttributes[iAttribute->getName()] = iAttribute;
1020  }
1021 
1025  template<typename TViewAttribute, typename... Args>
1026  void registerAttribute(std::string const &iName,
1027  typename TViewAttribute::Getter iGetter,
1028  typename TViewAttribute::Setter iSetter,
1029  Args... iArgs)
1030  {
1031  std::shared_ptr<ViewAttribute> cva;
1032  cva.reset(new TViewAttribute(iName, iGetter, iSetter, iArgs...));
1033  registerAttribute(cva);
1034  }
1035 
1036 
1037  char const *fViewName;
1038  char const *fDisplayName;
1039  char const *fBaseViewName;
1040 
1041  // use a map of shared pointers so that they can easily be copied (see registerAttributes)
1042  std::map<std::string, std::shared_ptr<ViewAttribute>> fAttributes;
1043 };
1044 
1048 template<typename TView, typename TBaseView>
1050 {
1051 public:
1052  explicit CustomViewCreator(char const *iViewName = nullptr,
1053  char const *iDisplayName = nullptr,
1054  char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
1055  TCustomViewCreator<TView>(iViewName, iDisplayName, iBaseViewName)
1056  {
1057  TCustomViewCreator<TView>::registerAttributes(typename TBaseView::Creator());
1058  }
1059 };
1060 
1061 }
1062 }
1063 }
1064 }
TSetter Setter
Definition: CustomViewCreator.h:136
Definition: CustomViewCreator.h:1049
VectorStringAttribute(std::string const &iName, typename super_type::Getter iGetter, typename super_type::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Definition: CustomViewCreator.h:699
void registerFloatAttribute(std::string const &iName, typename FloatAttribute< float >::Getter iGetter, typename FloatAttribute< float >::Setter iSetter)
Definition: CustomViewCreator.h:905
T fTo
Definition: Lerp.h:332
bool toString(IUIDescription const *iDescription, const CColor &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:393
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, std::vector< std::string > &oValue) const override
Definition: CustomViewCreator.h:710
IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition: CustomViewCreator.h:283
virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
Definition: CustomViewCreator.h:152
IdStringPtr getBaseViewName() const override
Definition: CustomViewCreator.h:779
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
Definition: CustomViewCreator.h:420
void registerIntegerAttribute(std::string const &iName, typename IntegerAttribute< TInt >::Getter iGetter, typename IntegerAttribute< TInt >::Setter iSetter)
Definition: CustomViewCreator.h:885
bool apply(CView *view, const UIAttributes &attributes, const IUIDescription *description) const override
Definition: CustomViewCreator.h:948
bool toString(IUIDescription const *iDescription, const std::vector< std::string > &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:719
void registerTagAttribute(std::string const &iName, typename TagAttribute::Getter iGetter, typename TagAttribute::Setter iSetter)
Definition: CustomViewCreator.h:874
void registerBooleanAttribute(std::string const &iName, typename BooleanAttribute::Getter iGetter, typename BooleanAttribute::Setter iSetter)
Definition: CustomViewCreator.h:925
Definition: Clock.h:22
bool getAttributeNames(std::list< std::string > &attributeNames) const override
Definition: CustomViewCreator.h:964
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:332
CBitmap * BitmapPtr
Definition: Types.h:49
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:287
bool toString(IUIDescription const *iDescription, const Range &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:674
bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const override
Definition: CustomViewCreator.h:187
void registerBitmapAttribute(std::string const &iName, typename BitmapAttribute::Getter iGetter, typename BitmapAttribute::Setter iSetter)
Definition: CustomViewCreator.h:822
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
Definition: CustomViewCreator.h:654
void registerGradientAttribute(std::string const &iName, typename GradientAttribute::Getter iGetter, typename GradientAttribute::Setter iSetter)
Definition: CustomViewCreator.h:812
CustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition: CustomViewCreator.h:1052
bool toString(IUIDescription const *iDescription, const int32_t &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:255
TView * createCustomView(CRect const &iSize, const UIAttributes &iAttributes, const IUIDescription *iDescription)
Definition: CustomViewCreator.h:92
bool toString(IUIDescription const *iDescription, const bool &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:486
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
Definition: CustomViewCreator.h:381
ViewAttribute(std::string iName)
Definition: CustomViewCreator.h:49
bool toString(IUIDescription const *iDescription, const TInt &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:307
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
Definition: CustomViewCreator.h:554
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:375
FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition: CustomViewCreator.h:328
TAttribute< T, T const &(StepButtonView ::*)() const, void(StepButtonView ::*)(T const &)> ByRefAttribute
Definition: CustomViewCreator.h:213
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, int32_t &oValue) const override
Definition: CustomViewCreator.h:234
std::vector< std::string > splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries)
Definition: StringUtils.cpp:30
virtual bool toString(IUIDescription const *iDescription, T const &iValue, std::string &oStringValue) const
Definition: CustomViewCreator.h:161
CView * create(const UIAttributes &attributes, const IUIDescription *description) const override
Definition: CustomViewCreator.h:935
MarginAttribute(std::string const &iName, typename ByRefAttribute< Margin >::Getter iGetter, typename ByRefAttribute< Margin >::Setter iSetter)
Definition: CustomViewCreator.h:588
TCustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition: CustomViewCreator.h:745
std::string getName() const
Definition: CustomViewCreator.h:62
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:548
char const * fBaseViewName
Definition: CustomViewCreator.h:1039
void registerAttribute(std::shared_ptr< ViewAttribute > iAttribute)
Definition: CustomViewCreator.h:1015
bool toString(IUIDescription const *iDescription, const Margin &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:621
IdStringPtr getViewName() const override
Definition: CustomViewCreator.h:767
Definition: LookAndFeel.h:32
Definition: Types.h:29
GradientAttribute(std::string const &iName, typename ByValAttribute< GradientPtr >::Getter iGetter, typename ByValAttribute< GradientPtr >::Setter iSetter)
Definition: CustomViewCreator.h:408
void registerAttribute(std::string const &iName, typename TViewAttribute::Getter iGetter, typename TViewAttribute::Setter iSetter, Args... iArgs)
Definition: CustomViewCreator.h:1026
TAttribute< T, T(StepButtonView ::*)() const, void(StepButtonView ::*)(T)> ByValAttribute
Definition: CustomViewCreator.h:207
std::string fName
Definition: CustomViewCreator.h:84
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
Definition: CustomViewCreator.h:338
char const * fDisplayName
Definition: CustomViewCreator.h:1038
CCoord fLeft
Definition: LookAndFeel.h:61
void registerRangeAttribute(std::string const &iName, typename RangeAttribute::Getter iGetter, typename RangeAttribute::Setter iSetter)
Definition: CustomViewCreator.h:852
ColorAttribute(std::string const &iName, typename ByRefAttribute< CColor >::Getter iGetter, typename ByRefAttribute< CColor >::Setter iSetter)
Definition: CustomViewCreator.h:369
void registerFontAttribute(std::string const &iName, typename FontAttribute::Getter iGetter, typename FontAttribute::Setter iSetter)
Definition: CustomViewCreator.h:832
void registerVectorStringAttribute(std::string const &iName, typename VectorStringAttribute::Getter iGetter, typename VectorStringAttribute::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Definition: CustomViewCreator.h:862
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:143
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
Definition: CustomViewCreator.h:513
bool toString(IUIDescription const *iDescription, const TFloat &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:351
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:228
bool toString(IUIDescription const *iDescription, GradientPtr const &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:432
BitmapAttribute(std::string const &iName, typename ByValAttribute< BitmapPtr >::Getter iGetter, typename ByValAttribute< BitmapPtr >::Setter iSetter)
Definition: CustomViewCreator.h:501
void registerMarginAttribute(std::string const &iName, typename MarginAttribute::Getter iGetter, typename MarginAttribute::Setter iSetter)
Definition: CustomViewCreator.h:842
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
Definition: CustomViewCreator.h:293
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
Definition: CustomViewCreator.h:594
std::map< std::string, std::shared_ptr< ViewAttribute > > fAttributes
Definition: CustomViewCreator.h:1042
bool getAttributeValue(CView *iView, const std::string &iAttributeName, std::string &oStringValue, const IUIDescription *iDescription) const override
Definition: CustomViewCreator.h:987
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:462
FontAttribute(std::string const &iName, typename ByValAttribute< FontPtr >::Getter iGetter, typename ByValAttribute< FontPtr >::Setter iSetter)
Definition: CustomViewCreator.h:542
T fFrom
Definition: Lerp.h:331
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
Definition: CustomViewCreator.h:468
void registerIntAttribute(std::string const &iName, typename IntegerAttribute< int32_t >::Getter iGetter, typename IntegerAttribute< int32_t >::Setter iSetter)
Definition: CustomViewCreator.h:895
RangeAttribute(std::string const &iName, typename ByRefAttribute< Range >::Getter iGetter, typename ByRefAttribute< Range >::Setter iSetter)
Definition: CustomViewCreator.h:648
bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
Definition: CustomViewCreator.h:167
void registerAttributes(TCustomViewCreator< XView > const &iOther)
Definition: CustomViewCreator.h:788
TAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition: CustomViewCreator.h:139
BooleanAttribute(std::string const &iName, typename ByValAttribute< bool >::Getter iGetter, typename ByValAttribute< bool >::Setter iSetter)
Definition: CustomViewCreator.h:456
CCoord fTop
Definition: LookAndFeel.h:58
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:414
UTF8StringPtr getDisplayName() const override
Definition: CustomViewCreator.h:773
char const * fViewName
Definition: CustomViewCreator.h:1037
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:507
~TCustomViewCreator() override
Definition: CustomViewCreator.h:759
bool toString(IUIDescription const *iDescription, BitmapPtr const &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:525
CCoord fRight
Definition: LookAndFeel.h:59
Definition: CustomViewCreator.h:123
void registerColorAttribute(std::string const &iName, typename ColorAttribute::Getter iGetter, typename ColorAttribute::Setter iSetter)
Definition: CustomViewCreator.h:802
void registerDoubleAttribute(std::string const &iName, typename FloatAttribute< double >::Getter iGetter, typename FloatAttribute< double >::Setter iSetter)
Definition: CustomViewCreator.h:915
AttrType getAttributeType(const std::string &attributeName) const override
Definition: CustomViewCreator.h:974
TGetter Getter
Definition: CustomViewCreator.h:135
bool toString(IUIDescription const *iDescription, FontPtr const &iValue, std::string &oStringValue) const override
Definition: CustomViewCreator.h:566
CFontDesc * FontPtr
Definition: Types.h:53
Getter fGetter
Definition: CustomViewCreator.h:199
Definition: CustomViewCreator.h:45
CCoord fBottom
Definition: LookAndFeel.h:60
Setter fSetter
Definition: CustomViewCreator.h:200
TagAttribute(std::string const &iName, typename ByValAttribute< int32_t >::Getter iGetter, typename ByValAttribute< int32_t >::Setter iSetter)
Definition: CustomViewCreator.h:222
CGradient * GradientPtr
Definition: Types.h:57