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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  
     | 
    
      /* This file is part of the KDE project
 * Copyright (C) 2007 Thomas Zander <zander@kde.org>
 * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "KoPositionSelector.h"
#include <QRadioButton>
#include <QGridLayout>
#include <QButtonGroup>
#include <QPainter>
#include <WidgetsDebug.h>
#include <QStyleOption>
#define GAP 0
class Q_DECL_HIDDEN KoPositionSelector::Private
{
public:
    Private()
        : position(KoFlake::TopLeftCorner)
    {
        topLeft = createButton(KoFlake::TopLeftCorner);
        topLeft->setChecked(true);
        topRight = createButton(KoFlake::TopRightCorner);
        center = createButton(KoFlake::CenteredPosition);
        bottomRight = createButton(KoFlake::BottomRightCorner);
        bottomLeft = createButton(KoFlake::BottomLeftCorner);
    }
    QRadioButton *createButton(int id) {
        QRadioButton *b = new QRadioButton();
        buttonGroup.addButton(b, id);
        return b;
    }
    QRadioButton *topLeft, *topRight, *center, *bottomRight, *bottomLeft;
    QButtonGroup buttonGroup;
    KoFlake::Position position;
};
class RadioLayout : public QLayout {
Q_OBJECT
public:
    RadioLayout(QWidget *parent)
        : QLayout(parent)
    {
    }
    ~RadioLayout() override
    {
        foreach( const Item & item, items )
            delete item.child;
        items.clear();
    }
    void setGeometry (const QRect &geom) override {
        QSize prefSize = calcSizes();
        qreal columnWidth, rowHeight;
        if (geom.width() <= minimum.width())
            columnWidth = geom.width() / (qreal) maxCol;
        else
            columnWidth = prefSize.width() + GAP;
        if (geom.height() <= minimum.height())
            rowHeight = geom.height() / (qreal) maxRow;
        else
            rowHeight = prefSize.height() + GAP;
        // padding inside row and column so that radio button is centered
        QPoint padding( qRound(0.5 * (columnWidth - prefSize.width())), qRound(0.5 * (rowHeight - prefSize.height())));
        // offset so that all the radio button are centered within the widget
        qreal offsetX = 0.5 * (geom.width()- static_cast<qreal>(maxCol) * columnWidth);
        qreal offsetY = 0.5 * (geom.height() - static_cast<qreal>(maxRow) * rowHeight);
        QPoint offset( qRound(offsetX), qRound(offsetY));
        foreach(const Item & item, items) {
            QPoint point( qRound(item.column * columnWidth), qRound(item.row * rowHeight) );
            QRect rect(point + offset + padding + geom.topLeft(), prefSize);
            item.child->setGeometry(rect);
        }
    }
    QSize calcSizes() {
        QSize prefSize;
        maxRow = 0;
        maxCol = 0;
        foreach(const Item & item, items) {
            if(prefSize.isEmpty()) {
                QAbstractButton *but = dynamic_cast<QAbstractButton*> (item.child->widget());
                Q_ASSERT(but);
                QStyleOptionButton opt;
                opt.initFrom(but);
                prefSize = QSize(but->style()->pixelMetric(QStyle::PM_ExclusiveIndicatorWidth, &opt, but),
                        but->style()->pixelMetric(QStyle::PM_ExclusiveIndicatorHeight, &opt, but));
            }
            maxRow = qMax(maxRow, item.row);
            maxCol = qMax(maxCol, item.column);
        }
        maxCol++; maxRow++; // due to being zero-based.
        preferred = QSize(maxCol * prefSize.width() + (maxCol-1) * GAP, maxRow * prefSize.height() + (maxRow-1) * GAP);
        minimum = QSize(maxCol * prefSize.width(), maxRow * prefSize.height());
        return prefSize;
    }
    QLayoutItem *itemAt (int index) const override {
        if( index < count() )
            return items.at(index).child;
        else
            return 0;
    }
    QLayoutItem *takeAt (int index) override {
        Q_ASSERT(index < count());
        Item item = items.takeAt(index);
        return item.child;
    }
    int count () const override {
        return items.count();
    }
    void addItem(QLayoutItem *) override {
        Q_ASSERT(0);
    }
    QSize sizeHint() const override {
        if(preferred.isEmpty())
            const_cast<RadioLayout*> (this)->calcSizes();
        return preferred;
    }
    QSize minimumSize() const override {
        if(minimum.isEmpty())
            const_cast<RadioLayout*> (this)->calcSizes();
        return minimum;
    }
    void addWidget(QRadioButton *widget, int row, int column) {
        addChildWidget(widget);
        Item newItem;
        newItem.child = new QWidgetItem(widget);
        newItem.row = row;
        newItem.column = column;
        items.append(newItem);
    }
private:
    struct Item {
        QLayoutItem *child;
        int column;
        int row;
    };
    QList<Item> items;
    QSize preferred, minimum;
    int maxCol, maxRow;
};
KoPositionSelector::KoPositionSelector(QWidget *parent)
    : QWidget(parent),
    d(new Private())
{
    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    RadioLayout *lay = new RadioLayout(this);
    lay->addWidget(d->topLeft, 0, 0);
    lay->addWidget(d->topRight, 0, 2);
    lay->addWidget(d->center, 1, 1);
    lay->addWidget(d->bottomRight, 2, 2);
    lay->addWidget(d->bottomLeft, 2, 0);
    setLayout(lay);
    connect(&d->buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(positionChanged(int)));
}
KoPositionSelector::~KoPositionSelector() {
    delete d;
}
KoFlake::Position KoPositionSelector::position() const {
    return d->position;
}
void KoPositionSelector::setPosition(KoFlake::Position position) {
    d->position = position;
    switch(d->position) {
        case KoFlake::TopLeftCorner:
            d->topLeft->setChecked(true);
            break;
        case KoFlake::TopRightCorner:
            d->topRight->setChecked(true);
            break;
        case KoFlake::CenteredPosition:
            d->center->setChecked(true);
            break;
        case KoFlake::BottomLeftCorner:
            d->bottomLeft->setChecked(true);
            break;
        case KoFlake::BottomRightCorner:
            d->bottomRight->setChecked(true);
            break;
    }
}
void KoPositionSelector::positionChanged(int position) {
    d->position = static_cast<KoFlake::Position> (position);
    emit positionSelected(d->position);
}
void KoPositionSelector::paintEvent (QPaintEvent *) {
    QPainter painter( this );
    QPen pen(Qt::black);
    int width;
    if (d->topLeft->width() %2 == 0)
        width = 2;
    else
        width = 3;
    pen.setWidth(width);
    painter.setPen(pen);
    painter.drawRect( QRect( d->topLeft->geometry().center(), d->bottomRight->geometry().center() ) );
    painter.end();
}
#include "KoPositionSelector.moc"
 
     |