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());
}
Ejemplo n.º 2
0
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::onResizerWidgetFirstSectorChanged(qint64 newFirst)
{
    updateSpinFreeBefore(newFirst - minimumFirstSector());
    updateSpinFirstSector(newFirst);
    updateSpinCapacity(partition().length());
    setDirty();
}
void SizeDialogBase::onSpinFirstSectorChanged(double newFirst)
{
    if (newFirst >= minimumFirstSector() && dialogWidget().partResizerWidget().updateFirstSector(newFirst))
        setDirty();
    else
        // TODO: this is not the best solution: we should prevent the user from entering
        // illegal values with a validator
        updateSpinFirstSector(partition().firstSector());
}
Ejemplo n.º 6
0
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);
    }
}
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());
}
Ejemplo n.º 8
0
bool PartResizerWidget::updateFirstSector(qint64 newFirstSector)
{
    if (maximumFirstSector(align()) > -1 && newFirstSector > maximumFirstSector(align()))
        newFirstSector = maximumFirstSector(align());

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

    const qint64 newLength = partition().lastSector() - newFirstSector + 1;

    if (newLength < minimumLength())
        newFirstSector -= minimumLength() - newLength;

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

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

    if (newFirstSector != partition().firstSector() && (partition().children().size() == 0 || checkAlignment(*partition().children().first(), partition().firstSector() - newFirstSector)))
    {
        const qint64 deltaFirst = partition().firstSector() - newFirstSector;

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

        resizeLogicals(deltaFirst, 0);

        updatePositions();

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

        return true;
    }

    return false;
}
void SizeDialogBase::onSpinFreeBeforeChanged(double newBefore)
{
    bool success = false;

    const double oldBefore = sectorsToDialogUnit(device(), partition().firstSector() - minimumFirstSector());
    const qint64 newFirstSector = minimumFirstSector() + dialogUnitToSectors(device(), newBefore);
    const qint64 deltaCorrection = newBefore > oldBefore
                                   ? PartitionAlignment::firstDelta(device(), partition(), newFirstSector)
                                   : 0;

    // We need different alignFirstSector parameters for moving the first sector (which
    // has to take into account min and max length of the partition) and for moving
    // the whole partition (which must NOT take min and max length into account since
    // the length is fixed in this case anyway)

    qint64 alignedFirstSector = align()
                                ? PartitionAlignment::alignedFirstSector(device(), partition(), newFirstSector + deltaCorrection, minimumFirstSector(), -1, -1, -1)
                                : newFirstSector;

    if (dialogWidget().partResizerWidget().movePartition(alignedFirstSector))
        success = true;
    else {
        alignedFirstSector = align()
                             ? PartitionAlignment::alignedFirstSector(device(), partition(), newFirstSector + deltaCorrection, minimumFirstSector(), -1, minimumLength(), maximumLength())
                             : newFirstSector;

        success = dialogWidget().partResizerWidget().updateFirstSector(alignedFirstSector);
    }

    if (success)
        setDirty();
    else
        // TODO: this is not the best solution: we should prevent the user from entering
        // illegal values with a validator
        updateSpinFreeBefore(dialogUnitToSectors(device(), oldBefore));
}
Ejemplo n.º 10
0
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;
}
Ejemplo n.º 11
0
int PartResizerWidget::partWidgetStart() const
{
    return handleWidth() + (partition().firstSector() - minimumFirstSector()) / sectorsPerPixel();
}