Guest User

Doubt

a guest
Jan 29th, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. class Widget {
  2. private:
  3.   unsigned int h,w;
  4.   bool visible;
  5. public:
  6.   virtual void setStandardSize() =0;
  7.  
  8.   Widget(unsigned int height=0, unsigned int width=0, bool vis =true):
  9.     h(height), w(width), visible(vis) {}
  10.  
  11.   virtual ~Widget() =default;
  12.  
  13.   void setHeight(unsigned int height) {h=height;}
  14.   void setWidth(unsigned int width) {w=width;}
  15. };
  16.  
  17. #include<string>
  18.  
  19. class AbstractButton: public Widget {
  20. private:
  21.   std::string label;
  22. public:
  23.   AbstractButton(unsigned int height=0, unsigned int width=0, bool vis =true,
  24.          const std::string& l=""): Widget(height,width,vis), label(l) {}
  25.  
  26.   std::string getLabel() const {return label;}
  27. };
  28.  
  29. class PushButton: public AbstractButton {
  30. public:
  31.   static const unsigned int standardHeight = 80;
  32.   static const unsigned int standardWidth = 20;
  33.  
  34.   void setStandardSize() override {
  35.     setHeight(standardHeight); setWidth(standardWidth);
  36.   }
  37.  
  38.   PushButton(unsigned int height=standardHeight, unsigned int width= standardWidth,
  39.          bool vis =true, const std::string& l=""): AbstractButton(height,width,vis,l) {}
  40. };
  41.  
  42. class CheckBox: public AbstractButton {
  43. private:
  44.   bool checked;
  45. public:
  46.   static const unsigned int standardHeight = 5;
  47.   static const unsigned int standardWidth = 5;
  48.  
  49.   void setStandardSize() override {
  50.     setHeight(standardHeight); setWidth(standardWidth);
  51.   }
  52.  
  53.   CheckBox(unsigned int height=standardHeight, unsigned int width= standardWidth,
  54.        const std::string& l="", bool check=false):
  55.     AbstractButton(height,width,true,l), checked(check) {}
  56.  
  57.   bool isChecked() const {return checked;}  
  58. };
  59.  
  60. #include<vector>
  61. #include<list>
  62.  
  63. class Gui {
  64. private:
  65.   std::vector<const Widget*> NoButtons;
  66.   std::list<const AbstractButton*> Buttons;
  67. public:
  68.   void insert(Widget* pw) {
  69.     if(pw == nullptr) throw std::string("NoInsert");
  70.     if(dynamic_cast<AbstractButton*>(pw) != nullptr)
  71.       Buttons.push_back(static_cast<AbstractButton*>(pw));
  72.     else NoButtons.push_back(pw);
  73.   }
  74.  
  75.   void insert(unsigned int pos, PushButton& pb) {
  76.     if(pos > Buttons.size()) throw std::string("NoInsert");
  77.     auto it = Buttons.begin();
  78.     for( ; pos>0; pos--, it++);
  79.     Buttons.insert(it, &pb);
  80.   }
  81.  
  82.   std::vector<AbstractButton*> removeUnchecked() {
  83.     std::vector<AbstractButton*> v;
  84.     CheckBox* p;
  85.     for(auto it=Buttons.begin(); it!=Buttons.end(); ++it) {
  86.       p=const_cast<CheckBox*>(dynamic_cast<const CheckBox*>(*it));
  87.       if(p && !(p->isChecked())) {
  88.     v.push_back(p);
  89.     it=Buttons.erase(it); --it;
  90.       }
  91.     }
  92.     return v;
  93.   }
  94.  
  95.   void setStandardPushButton() {
  96.     for(auto it = Buttons.begin(); it != Buttons.end(); ++it)
  97.       if(dynamic_cast<const PushButton*>(*it) != nullptr && !((*it)->getLabel().empty()))
  98.      (const_cast<AbstractButton*>(*it))->setStandardSize();
  99.   }
  100. };
Advertisement
Add Comment
Please, Sign In to add comment