bulletindicator.cpp 3.34 KB
#include "bulletindicator.h"

BulletIndicator::BulletIndicator(QWidget *parent) : QWidget(parent)
{
    max = 1;
    cur = 0;
    bulletPixmap.load(":/images/symbol/step_bullet.png");
    currentBulletPixmap.load(":/images/symbol/selected_step_bullet.png");
    padding = qMin(bulletPixmap.width(), currentBulletPixmap.width());

    QRect defaultGeometry;
    defaultGeometry.setSize(bulletPixmap.size());

    QLabel *bullet;

    bullet = new QLabel(this);
    bullet->setPixmap(currentBulletPixmap);
    bullet->setGeometry(defaultGeometry);
    bullets.append(bullet);

    bullet = new QLabel(this);
    bullet->setPixmap(bulletPixmap);
    bullet->setGeometry(defaultGeometry);
    bullets.append(bullet);

    updatePosition();
}

void BulletIndicator::setBulletPixmap(QPixmap &pixmap)
{
    bulletPixmap = pixmap;

    for (int i = 0; i <= max; i++)
        if (i != cur)
            bullets.at(i)->setPixmap(pixmap);

    padding = qMin(pixmap.width(), padding);

    updatePosition();
}

void BulletIndicator::setCurrentBulletPixmap(QPixmap &pixmap)
{
    currentBulletPixmap = pixmap;

    bullets.at(cur)->setPixmap(pixmap);

    padding = qMin(pixmap.width(), padding);

    updatePosition();
}

void BulletIndicator::setCurrentIndex(int index)
{
    if (index == cur || index < 0 || index > max)
        return;

    bullets.at(cur)->setPixmap(bulletPixmap);
    bullets.at(index)->setPixmap(currentBulletPixmap);

    cur = index;

    updatePosition();
}

void BulletIndicator::setMaximum(int maximum)
{
    if (maximum == max || maximum < 1)
        return;

    if (bullets.size() <= maximum)
    {
        QRect defaultGeometry;
        defaultGeometry.setSize(bulletPixmap.size());
        for (int i = bullets.size(); i <= maximum; i++)
        {
            QLabel *l = new QLabel(this);
            l->setPixmap(bulletPixmap);
            l->setGeometry(defaultGeometry);
            bullets.append(l);
        }
    }

    if (maximum > max)
    {
        for (int i = max + 1; i <= maximum; i++)
        {
            QLabel *l = bullets.at(i);
            if (l->isHidden())
                l->show();
        }
    }
    else if (maximum < max)
    {
        for (int i = maximum + 1; i <= max; i++)
        {
            QLabel *l = bullets.at(i);
            if (l->isVisible())
                l->hide();
        }
    }

    max = maximum;

    if (maximum < cur)
        setCurrentIndex(maximum);
    else
        updatePosition();
}

void BulletIndicator::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);

    updatePosition();
}

void BulletIndicator::updatePosition()
{
    int bulletWidth = bulletPixmap.width();
    int bulletHeight = bulletPixmap.height();
    int currentBulletWidth = currentBulletPixmap.width();
    int currentBulletHeight = currentBulletPixmap.height();

    int x = (width() - ((bulletWidth + padding) * max + currentBulletWidth)) / 2;
    int bulletY = (height() - bulletHeight) / 2;
    int currentBulletY = (height() - currentBulletHeight) / 2;

    for (int i = 0; i <= max; i++)
    {
        if (i == cur)
        {
            bullets.at(i)->setGeometry(x, currentBulletY, currentBulletWidth, currentBulletHeight);
            x += currentBulletWidth + padding;
        }
        else
        {
            bullets.at(i)->setGeometry(x, bulletY, bulletWidth, bulletHeight);
            x += bulletWidth + padding;
        }
    }
}