detailswidget.cpp 5.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/**
 ******************************************************************************
 *
 * @file       detailswidget.cpp
 * @author     The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
 *             Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
 * @brief      
 * @see        The GNU Public License (GPL) Version 3
 * @defgroup   
 * @{
 * 
 *****************************************************************************/
/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "detailswidget.h"
#include "detailsbutton.h"

#include <QtGui/QGridLayout>
#include <QtCore/QStack>
#include <QtGui/QLabel>
#include <QtGui/QGridLayout>
#include <QtGui/QPainter>

using namespace Utils;

DetailsWidget::DetailsWidget(QWidget *parent)
    : QWidget(parent),
      m_summaryLabel(new QLabel(this)),
      m_detailsButton(new DetailsButton(this)),
      m_widget(0),
      m_toolWidget(0),
      m_grid(new QGridLayout(this))

{
    m_grid->setContentsMargins(4, 3, 4, 3);

    m_summaryLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
    m_summaryLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    m_grid->addWidget(m_summaryLabel, 0, 0);
    m_grid->addWidget(m_detailsButton, 0, 2, 1, 1, Qt::AlignBottom);

    m_dummyWidget = new QWidget(this);
    m_dummyWidget->setMaximumHeight(4);
    m_dummyWidget->setMaximumHeight(4);
    m_dummyWidget->setVisible(false);
    m_grid->addWidget(m_dummyWidget, 2, 0, 1, 1);

    connect(m_detailsButton, SIGNAL(clicked()),
            this, SLOT(detailsButtonClicked()));
}

DetailsWidget::~DetailsWidget()
{

}

void DetailsWidget::paintEvent(QPaintEvent *paintEvent)
{
    //TL-->                 ___________  <-- TR
    //                     |           |
    //ML->   ______________| <--MM     | <--MR
    //       |                         |
    //BL->   |_________________________| <-- BR


    QWidget::paintEvent(paintEvent);

    if (!m_detailsButton->isToggled())
        return;

    const QRect detailsGeometry = m_detailsButton->geometry();
    const QRect widgetGeometry = m_widget ? m_widget->geometry() : QRect(x(), y() + height(), width(), 0);

    QPoint tl(detailsGeometry.topLeft());
    tl += QPoint(-3, -3);

    QPoint tr(detailsGeometry.topRight());
    tr += QPoint(3, -3);

    QPoint mm(detailsGeometry.left() - 3, widgetGeometry.top() - 3);

    QPoint ml(1, mm.y());

    QPoint mr(tr.x(), mm.y());

    int bottom = geometry().height() - 3;
    QPoint bl(1, bottom);
    QPoint br(tr.x(), bottom);

    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    p.setPen(Qt::NoPen);

    p.setBrush(palette().dark());
    p.drawRoundedRect(QRect(tl, br), 5, 5);
    p.drawRoundedRect(QRect(ml, br), 5, 5);
}

void DetailsWidget::detailsButtonClicked()
{
    bool visible = m_detailsButton->isToggled();
    if (m_widget)
        m_widget->setVisible(visible);
    m_dummyWidget->setVisible(visible);
    fixUpLayout();
}

void DetailsWidget::setSummaryText(const QString &text)
{
    m_summaryLabel->setText(text);
}

QString DetailsWidget::summaryText() const
{
    return m_summaryLabel->text();
}

bool DetailsWidget::expanded() const
{
    return m_detailsButton->isToggled();
}

void DetailsWidget::setExpanded(bool v)
{
    if (expanded() != v)
        m_detailsButton->animateClick();
}

QWidget *DetailsWidget::widget() const
{
    return m_widget;
}

void DetailsWidget::setWidget(QWidget *widget)
{
    if (m_widget == widget)
        return;
    if (m_widget) {
        m_grid->removeWidget(m_widget);
        m_widget = 0;
    }
    if (widget) {
        m_grid->addWidget(widget, 1, 0, 1, 3);
        m_widget = widget;
        bool visible = m_detailsButton->isToggled();
        m_widget->setVisible(visible);
        m_dummyWidget->setVisible(visible);
    }
}

void DetailsWidget::setToolWidget(QWidget *widget)
{
    if (m_toolWidget == widget)
        return;
    if (m_toolWidget) {
        m_grid->removeWidget(m_toolWidget);
        m_toolWidget = 0;
    }
    if (widget) {
        m_grid->addWidget(widget, 0, 1, 1, 1, Qt::AlignBottom);
        m_toolWidget = widget;
    }
}

QWidget *DetailsWidget::toolWidget() const
{
    return m_toolWidget;
}

void DetailsWidget::fixUpLayout()
{
    if (!m_widget)
        return;
    QWidget *parent = m_widget;
    QStack<QWidget *> widgets;
    while((parent = parent->parentWidget()) && parent && parent->layout()) {
        widgets.push(parent);
        parent->layout()->update();
    }

    while(!widgets.isEmpty()) {
        widgets.pop()->layout()->activate();
    }
}