ginebra2/WebChromeContainerSnippet.cpp
changeset 6 1c3b8676e58c
parent 5 0f2326c2a325
equal deleted inserted replaced
5:0f2326c2a325 6:1c3b8676e58c
    28 
    28 
    29 #include <QDebug>
    29 #include <QDebug>
    30 
    30 
    31 namespace GVA {
    31 namespace GVA {
    32 
    32 
    33   WebChromeContainerSnippet::WebChromeContainerSnippet(const QString & elementId, ChromeWidget * chrome, const QRectF& ownerArea, const QWebElement & element, QGraphicsWidget* gwidget)
    33   WebChromeContainerSnippet::WebChromeContainerSnippet(const QString & elementId, ChromeWidget * chrome, const QWebElement & element)
    34     : ChromeSnippet(elementId, chrome, gwidget, element),
    34     : ChromeSnippet(elementId, chrome, 0, element)
    35       m_ownerArea(ownerArea),
    35       ,m_layoutHeight(0)
    36       m_layoutHeight(0)
       
    37   {
    36   {
    38 
    37 
    39     m_layoutWidth = chrome->width();
    38     m_layoutWidth = chrome->layout()->size().width();
    40 
    39 
    41     QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget());
    40     //QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget());
    42     //NB: maybe size should be fixed only in one direction?
    41     //NB: maybe size should be fixed only in one direction?
    43     item->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    42     //item->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    44     //NB: add a method for setting owner area
    43     //NB: add a method for setting owner area
    45     //item->setPreferredSize(m_ownerArea.width(), m_ownerArea.height());
    44     //item->setPreferredSize(m_ownerArea.width(), m_ownerArea.height());
    46     //Also resize in case item is not part of anchor layout
    45     //Also resize in case item is not part of anchor layout
    47     //item->resize(item->preferredSize());
    46     //item->resize(item->preferredSize());
    48 
    47 
    51     m_layout->setContentsMargins(0,0,0,0);
    50     m_layout->setContentsMargins(0,0,0,0);
    52     m_layout->setSpacing(0);
    51     m_layout->setSpacing(0);
    53 
    52 
    54     //Add a stretch element at the beginning.
    53     //Add a stretch element at the beginning.
    55     m_layout->addStretch();
    54     m_layout->addStretch();
    56     item->setLayout(m_layout);
    55     //item->setLayout(m_layout);
    57     //When chrome is resized owner areas for snippets may change
    56     //When chrome is resized sizes for snippets may change
    58     QObject::connect(m_chrome->renderer(), SIGNAL(chromeResized()), this, SLOT(updateOwnerArea()));
    57     QObject::connect(m_chrome->renderer(), SIGNAL(chromeResized()), this, SLOT(updateOwnerArea()));
    59     QObject::connect(m_chrome, SIGNAL(prepareForSizeChange(QSize)), this, SLOT(updateSize(QSize)));
    58     QObject::connect(m_chrome, SIGNAL(prepareForSizeChange(QSize)), this, SLOT(updateSize(QSize)));
    60 
    59 
    61   }
    60   }
    62 
    61 
    67 
    66 
    68   void WebChromeContainerSnippet::positionChildren()
    67   void WebChromeContainerSnippet::positionChildren()
    69   {
    68   {
    70     ; //Do nothing since the layout positions children automatically.
    69     ; //Do nothing since the layout positions children automatically.
    71   }
    70   }
    72 
    71   
       
    72   void WebChromeContainerSnippet::setChromeWidget(QGraphicsWidget * widget){
       
    73       widget->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
       
    74       widget->setLayout(m_layout);
       
    75       ChromeSnippet::setChromeWidget(widget);
       
    76   }
       
    77   
    73   void WebChromeContainerSnippet::addChild(ChromeSnippet * child)
    78   void WebChromeContainerSnippet::addChild(ChromeSnippet * child)
    74   {
    79   {
    75     //Prevent layout from stretching the child widgets. NB: Revisit this to make configurable from chrome?
    80     //Prevent layout from stretching the child widgets. NB: Revisit this to make configurable from chrome?
    76     child->widget()->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    81     child->widget()->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    77     //child->widget()->setParentItem(this->widget()); // Shouldn't be needed, right?
    82     //child->widget()->setParentItem(this->widget()); // Shouldn't be needed, right?
    81 
    86 
    82     qreal childHeight = child->widget()->size().height();
    87     qreal childHeight = child->widget()->size().height();
    83     if (childHeight > m_layoutHeight){
    88     if (childHeight > m_layoutHeight){
    84       m_layoutHeight = childHeight;
    89       m_layoutHeight = childHeight;
    85 
    90 
    86       setOwnerArea();
    91       updateSizes();
    87     }
    92     }
    88     emit childAdded(child);
    93     emit childAdded(child);
    89 
    94 
    90     m_layout->activate();
    95     m_layout->activate();
    91   }
    96   }
    92 
    97 
    93   void WebChromeContainerSnippet:: updateOwnerArea()
    98   void WebChromeContainerSnippet:: updateOwnerArea()
    94   {
    99   {
    95 
   100     updateSizes();
    96     setOwnerArea();
       
    97     QObject::disconnect(m_chrome->renderer(), SIGNAL(chromeResized()), this, SLOT(updateOwnerArea()));
   101     QObject::disconnect(m_chrome->renderer(), SIGNAL(chromeResized()), this, SLOT(updateOwnerArea()));
    98 
   102 
    99   }
   103   }
   100 
   104 
   101   void WebChromeContainerSnippet::setLayoutHeight(int height){
   105   void WebChromeContainerSnippet::setLayoutHeight(int height){
   102     if (m_layoutHeight != height){
   106     if (m_layoutHeight != height){
   103       m_layoutHeight = height;
   107       m_layoutHeight = height;
   104       setOwnerArea();
   108       updateSizes();
   105     }
   109     }
   106   }
   110   }
   107 
   111 
   108   void WebChromeContainerSnippet::setLayoutWidth(int width, bool update){
   112   void WebChromeContainerSnippet::setLayoutWidth(qreal width, bool update){
   109     if (m_layoutWidth != width){
   113     if (m_layoutWidth != width){
   110       m_layoutWidth = width;
   114       m_layoutWidth = width;
   111       if (update ) {
   115       if (update ) {
   112         setOwnerArea();
   116         updateSizes();
   113       }
   117       }
   114     }
   118     }
   115   }
   119   }
   116 
   120 
   117   void WebChromeContainerSnippet::updateSize(QSize size ) {
   121   void WebChromeContainerSnippet::updateSize(QSize size ) {
   118 
   122 
   119     Q_UNUSED(size);
   123     Q_UNUSED(size);
   120     // m_layoutWidth should have been set by now through derived classes. We don't want to set it
   124     // m_layoutWidth should have been set by now through derived classes. We don't want to set it
   121     // here as that would overwrite any width set before. For example, width of the middle snippet
   125     // here as that would overwrite any width set before. For example, width of the middle snippet
   122     // in toolbar is set by the main toolbar.
   126     // in toolbar is set by the main toolbar.
   123     setOwnerArea();
   127     updateSizes();
   124 
       
   125   }
   128   }
   126 
   129 
   127   void WebChromeContainerSnippet::setOwnerArea() {
   130   void WebChromeContainerSnippet::updateSizes() {
   128 
   131       
   129     QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget());
   132     QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget());
   130     m_ownerArea =  m_chrome->getSnippetRect(m_elementId);
       
   131 
       
   132     //Resize the item
   133     //Resize the item
   133     item->setPreferredSize(m_layoutWidth, m_layoutHeight);
   134     item->setPreferredSize(m_layoutWidth, m_layoutHeight);
   134     //Also resize in case item is not part of anchor layout
   135     //Also resize in case item is not part of anchor layout
   135     item->resize(item->preferredSize());
   136     item->resize(item->preferredSize());
   136 
   137