void SizeDialogBase::setupConnections()
{
    connect(&dialogWidget().partResizerWidget(), SIGNAL(firstSectorChanged(qint64)), SLOT(onResizerWidgetFirstSectorChanged(qint64)));
    connect(&dialogWidget().partResizerWidget(), SIGNAL(lastSectorChanged(qint64)), SLOT(onResizerWidgetLastSectorChanged(qint64)));

    connect(&dialogWidget().spinFreeBefore(), SIGNAL(valueChanged(double)), SLOT(onSpinFreeBeforeChanged(double)));
    connect(&dialogWidget().spinFreeAfter(), SIGNAL(valueChanged(double)), SLOT(onSpinFreeAfterChanged(double)));
    connect(&dialogWidget().spinCapacity(), SIGNAL(valueChanged(double)), SLOT(onSpinCapacityChanged(double)));

    connect(&detailsWidget().spinFirstSector(), SIGNAL(valueChanged(double)), SLOT(onSpinFirstSectorChanged(double)));
    connect(&detailsWidget().spinLastSector(), SIGNAL(valueChanged(double)), SLOT(onSpinLastSectorChanged(double)));
    connect(&detailsWidget().checkAlign(), SIGNAL(toggled(bool)), SLOT(onAlignToggled(bool)));
}
void
PartitionSizeController::connectWidgets()
{
    if ( !m_spinBox || !m_partResizerWidget )
        return;

    connect( m_spinBox, SIGNAL( editingFinished() ), SLOT( updatePartResizerWidget() ) );
    connect( m_partResizerWidget, SIGNAL( firstSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) );
    connect( m_partResizerWidget, SIGNAL( lastSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) );

    // Init m_spinBox from m_partResizerWidget
    updateSpinBox();
}
bool PartResizerWidget::updateLastSector(qint64 newLastSector)
{
    if (minimumLastSector(align()) > -1 && newLastSector < minimumLastSector(align()))
        newLastSector = minimumLastSector(align());

    if (maximumLastSector(align()) > 0 && newLastSector > maximumLastSector(align()))
        newLastSector = maximumLastSector(align());

    const qint64 newLength = newLastSector - partition().firstSector() + 1;

    if (newLength < minimumLength())
        newLastSector += minimumLength() - newLength;

    if (newLength > maximumLength())
        newLastSector -= newLength - maximumLength();

    if (align())
        newLastSector = PartitionAlignment::alignedLastSector(device(), partition(), newLastSector, minimumLastSector(align()), maximumLastSector(align()), minimumLength(), maximumLength());

    if (newLastSector != partition().lastSector() && (partition().children().size() == 0 || checkAlignment(*partition().children().last(), partition().lastSector() - newLastSector)))
    {
        const qint64 deltaLast = newLastSector - partition().lastSector();

        partition().setLastSector(newLastSector);
        partition().fileSystem().setLastSector(newLastSector);

        resizeLogicals(0, deltaLast);
        updatePositions();

        emit lastSectorChanged(partition().lastSector());

        return true;
    }

    return false;
}
bool PartResizerWidget::movePartition(qint64 newFirstSector)
{
    const qint64 originalLength = partition().length();
    const bool isLengthAligned = PartitionAlignment::isLengthAligned(device(), partition());

    if (maximumFirstSector(align()) > -1 && newFirstSector > maximumFirstSector(align()))
        newFirstSector = maximumFirstSector(align());

    if (minimumFirstSector(align()) > 0 && newFirstSector < minimumFirstSector(align()))
        newFirstSector = minimumFirstSector(align());

    if (align())
        newFirstSector = PartitionAlignment::alignedFirstSector(device(), partition(), newFirstSector, minimumFirstSector(align()), maximumFirstSector(align()), -1, -1);

    qint64 delta = newFirstSector - partition().firstSector();

    if (delta == 0)
        return false;

    qint64 newLastSector = partition().lastSector() + delta;

    if (minimumLastSector(align()) > -1 && newLastSector < minimumLastSector(align()))
    {
        const qint64 deltaLast = minimumLastSector(align()) - newLastSector;
        newFirstSector += deltaLast;
        newLastSector += deltaLast;
    }

    if (maximumLastSector(align()) > 0 && newLastSector > maximumLastSector(align()))
    {
        const qint64 deltaLast = newLastSector - maximumLastSector(align());
        newFirstSector -= deltaLast;
        newLastSector -= deltaLast;
    }

    if (align())
        newLastSector = PartitionAlignment::alignedLastSector(device(), partition(), newLastSector, minimumLastSector(align()), maximumLastSector(align()), -1, -1, originalLength, isLengthAligned);

    if (newLastSector == partition().lastSector())
        return false;

    if (isLengthAligned && newLastSector - newFirstSector + 1 != partition().length())
    {
        qDebug() << "length changes while trying to move partition " << partition().deviceNode() << ". new first: " << newFirstSector << ", new last: " << newLastSector << ", old length: " << partition().length() << ", new length: " << newLastSector - newFirstSector + 1;
        return false;
    }

    if (!checkConstraints(newFirstSector, newLastSector))
    {
        qDebug() << "constraints not satisfied while trying to move partition " << partition().deviceNode() << ". new first: " << newFirstSector << ", new last: " << newLastSector;
        return false;
    }

    if (align() && !PartitionAlignment::isAligned(device(), partition(), newFirstSector, newLastSector, true))
    {
        qDebug() << "partition " << partition().deviceNode() << " not aligned but supposed to be. new first: " << newFirstSector << " delta: " << PartitionAlignment::firstDelta(device(), partition(), newFirstSector) << ", new last: " << newLastSector << ", delta: " << PartitionAlignment::lastDelta(device(), partition(), newLastSector);
        return false;
    }

    if (partition().children().size() > 0 &&
            (!checkAlignment(*partition().children().first(), partition().firstSector() - newFirstSector) ||
             !checkAlignment(*partition().children().last(), partition().lastSector() - newLastSector)))
    {
        qDebug() << "cannot align children while trying to move partition " << partition().deviceNode();
        return false;
    }

    partition().setFirstSector(newFirstSector);
    partition().fileSystem().setFirstSector(newFirstSector);

    partition().setLastSector(newLastSector);
    partition().fileSystem().setLastSector(newLastSector);

    updatePositions();

    emit firstSectorChanged(partition().firstSector());
    emit lastSectorChanged(partition().lastSector());

    return true;
}