void SizeDialogBase::setupConstraints()
{
    setMinimumLength(!canShrink() ? partition().length() : qMax(partition().sectorsUsed(), partition().minimumSectors()));
    setMaximumLength(!canGrow() ? partition().length() : qMin(maximumLastSector() - minimumFirstSector() + 1, partition().maximumSectors()));

    dialogWidget().partResizerWidget().setMinimumLength(minimumLength());
    dialogWidget().partResizerWidget().setMaximumLength(maximumLength());

    dialogWidget().labelMinSize().setText(Capacity::formatByteSize(minimumLength() * device().logicalSectorSize()));
    dialogWidget().labelMaxSize().setText(Capacity::formatByteSize(maximumLength() * device().logicalSectorSize()));

    dialogWidget().spinCapacity().setEnabled(canShrink() || canGrow());

    dialogWidget().partResizerWidget().setMaximumFirstSector(maximumFirstSector());
    dialogWidget().partResizerWidget().setMinimumLastSector(minimumLastSector());

    const qint64 totalCapacity = sectorsToDialogUnit(device(), maximumLastSector() - minimumFirstSector() + 1);

    const qint64 minCapacity = sectorsToDialogUnit(device(), minimumLength());
    const qint64 maxCapacity = sectorsToDialogUnit(device(), maximumLength());
    dialogWidget().spinCapacity().setRange(minCapacity, maxCapacity);

    const qint64 maxFree = totalCapacity - minCapacity;

    dialogWidget().spinFreeBefore().setRange(0, maxFree);
    dialogWidget().spinFreeAfter().setRange(0, maxFree);

    detailsWidget().spinFirstSector().setRange(minimumFirstSector(), maximumLastSector());
    detailsWidget().spinLastSector().setRange(minimumFirstSector(), maximumLastSector());

    onAlignToggled(align());
}
void SizeDialogBase::onSpinFreeAfterChanged(double newAfter)
{
    bool success = false;
    const double oldAfter = sectorsToDialogUnit(device(), maximumLastSector() - partition().lastSector());
    const qint64 newLastSector = maximumLastSector() - dialogUnitToSectors(device(), newAfter);
    const qint64 deltaCorrection = newAfter > oldAfter
                                   ? PartitionAlignment::lastDelta(device(), partition(), newLastSector)
                                   : 0;

    // see onSpinFreeBeforeChanged on why this is as complicated as it is

    qint64 alignedLastSector = align()
                               ? PartitionAlignment::alignedLastSector(device(), partition(), newLastSector - deltaCorrection, -1, maximumLastSector(), -1, -1)
                               : newLastSector;

    if (dialogWidget().partResizerWidget().movePartition(alignedLastSector - partition().length() + 1))
        success = true;
    else {
        alignedLastSector = align()
                            ? PartitionAlignment::alignedLastSector(device(), partition(), newLastSector - deltaCorrection, -1, maximumLastSector(), minimumLength(), maximumLength())
                            : newLastSector;

        success = dialogWidget().partResizerWidget().updateLastSector(alignedLastSector);
    }

    if (success)
        setDirty();
    else
        // TODO: this is not the best solution: we should prevent the user from entering
        // illegal values with a validator
        updateSpinFreeAfter(dialogUnitToSectors(device(), oldAfter));
}
bool PartResizerWidget::checkConstraints(qint64 first, qint64 last) const
{
    return (maximumFirstSector() == -1 || first <= maximumFirstSector()) &&
           (minimumFirstSector() == 0 || first >= minimumFirstSector()) &&
           (minimumLastSector() == -1 || last >= minimumLastSector()) &&
           (maximumLastSector() == 0 || last <= maximumLastSector());
}
void SizeDialogBase::onSpinCapacityChanged(double newCapacity)
{
    bool success = false;

    qint64 newLength = qBound(
                           minimumLength(),
                           dialogUnitToSectors(device(), newCapacity),
                           qMin(maximumLastSector() - minimumFirstSector() + 1, maximumLength())
                       );

    if (newLength == partition().length())
        return;

    qint64 delta = newLength - partition().length();

    qint64 tmp = qMin(delta, maximumLastSector() - partition().lastSector());
    delta -= tmp;

    const bool signalState = dialogWidget().partResizerWidget().blockSignals(true);

    if (tmp != 0) {
        qint64 newLastSector = partition().lastSector() + tmp;

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

        if (dialogWidget().partResizerWidget().updateLastSector(newLastSector)) {
            success = true;
            updateSpinFreeAfter(maximumLastSector() - newLastSector);
            updateSpinLastSector(newLastSector);
        }
    }

    tmp = qMin(delta, partition().firstSector() - minimumFirstSector());
    delta -= tmp;

    if (tmp != 0) {
        qint64 newFirstSector = partition().firstSector() - tmp;

        if (align())
            newFirstSector = PartitionAlignment::alignedFirstSector(device(), partition(), newFirstSector, minimumFirstSector(), maximumFirstSector(), minimumLength(), maximumLength());

        if (dialogWidget().partResizerWidget().updateFirstSector(newFirstSector)) {
            success = true;
            updateSpinFreeBefore(newFirstSector - minimumFirstSector());
            updateSpinFirstSector(newFirstSector);
        }
    }

    dialogWidget().partResizerWidget().blockSignals(signalState);

    if (success)
        setDirty();
}
void SizeDialogBase::onResizerWidgetLastSectorChanged(qint64 newLast)
{
    updateSpinFreeAfter(maximumLastSector() - newLast);
    updateSpinLastSector(newLast);
    updateSpinCapacity(partition().length());
    setDirty();
}
void SizeDialogBase::onSpinLastSectorChanged(double newLast)
{
    if (newLast <= maximumLastSector() && dialogWidget().partResizerWidget().updateLastSector(newLast))
        setDirty();
    else
        // TODO: this is not the best solution: we should prevent the user from entering
        // illegal values with a validator
        updateSpinLastSector(partition().lastSector());
}
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;
}
void SizeDialogBase::setupDialog()
{
    dialogWidget().spinFreeBefore().setValue(sectorsToDialogUnit(device(), partition().firstSector() - minimumFirstSector()));
    dialogWidget().spinFreeAfter().setValue(sectorsToDialogUnit(device(), maximumLastSector() - partition().lastSector()));

    dialogWidget().spinCapacity().setValue(Capacity(partition().capacity()).toDouble(preferredUnit()));

    dialogWidget().spinFreeBefore().setSuffix(QStringLiteral(" ") + Capacity::unitName(preferredUnit()));
    dialogWidget().spinFreeAfter().setSuffix(QStringLiteral(" ") + Capacity::unitName(preferredUnit()));
    dialogWidget().spinCapacity().setSuffix(QStringLiteral(" ") + Capacity::unitName(preferredUnit()));

    detailsWidget().spinFirstSector().setValue(partition().firstSector());
    detailsWidget().spinLastSector().setValue(partition().lastSector());

    detailsWidget().checkAlign().setChecked(Config::alignDefault());

    if (canGrow() || canShrink())
        dialogWidget().partResizerWidget().init(device(), partition(), minimumFirstSector(), maximumLastSector(), false, canMove());
    else
        dialogWidget().partResizerWidget().init(device(), partition(), minimumFirstSector(), maximumLastSector(), true, canMove());
    dialogWidget().partResizerWidget().setAlign(Config::alignDefault());
}
void PartResizerWidget::mouseMoveEvent(QMouseEvent* event)
{
    int x = event->pos().x() - m_Hotspot;

    if (draggedWidget() == &leftHandle())
    {
        const qint64 newFirstSector = qMax(minimumFirstSector() + x * sectorsPerPixel(), 0LL);
        updateFirstSector(newFirstSector);
    }
    else if (draggedWidget() == &rightHandle())
    {
        const qint64 newLastSector = qMin(minimumFirstSector() + (x - rightHandle().width()) * sectorsPerPixel(), maximumLastSector());
        updateLastSector(newLastSector);
    }
    else if (draggedWidget() == &partWidget() && moveAllowed())
    {
        const qint64 newFirstSector = qMax(minimumFirstSector() + (x - handleWidth()) * sectorsPerPixel(), 0LL);
        movePartition(newFirstSector);
    }
}
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;
}