1
/***************************************************************************
2
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18
***************************************************************************/
20
#include "gui/sizedialogbase.h"
21
#include "gui/partresizerwidget.h"
22
#include "gui/sizedialogwidget.h"
24
#include "core/partitiontable.h"
25
#include "core/device.h"
26
#include "core/partition.h"
31
SizeDialogBase::SizeDialogBase(QWidget* parent, Capacity::Unit preferred, Device& device, Partition& part, qint64 freebefore, qint64 freeafter) :
33
m_SizeDialogWidget(new SizeDialogWidget(this)),
34
m_PreferredUnit(preferred),
37
m_FreeSectorsBefore(freebefore),
38
m_FreeSectorsAfter(freeafter)
42
qint64 SizeDialogBase::minSectors() const
45
return partition().length();
47
return qMax(partition().sectorsUsed(), partition().minimumSectors());
50
qint64 SizeDialogBase::maxSectors() const
53
return partition().length();
55
return qMin(partition().length() + freeSectorsBefore() + freeSectorsAfter(), partition().maximumSectors());
58
int SizeDialogBase::sectorsToDialogUnit(const Partition& p, Capacity::Unit u, qint64 v)
60
return Capacity(v * p.sectorSize()).toInt(u);
63
qint64 SizeDialogBase::dialogUnitToSectors(const Partition& p, Capacity::Unit u, int v)
65
return Capacity::unitFactor(Capacity::Byte, u) * v / p.sectorSize();
68
void SizeDialogBase::setupDialog()
70
dialogWidget().spinFreeBefore().setValue(Capacity(freeSectorsBefore() * partition().sectorSize()).toInt(preferredUnit()));
71
dialogWidget().spinFreeAfter().setValue(Capacity(freeSectorsAfter() * partition().sectorSize()).toInt(preferredUnit()));
72
dialogWidget().spinCapacity().setValue(Capacity(partition().capacity()).toInt(preferredUnit()));
74
dialogWidget().spinFreeBefore().setSuffix(QString(" ") + Capacity::unitName(preferredUnit()));
75
dialogWidget().spinFreeAfter().setSuffix(QString(" ") + Capacity::unitName(preferredUnit()));
76
dialogWidget().spinCapacity().setSuffix(QString(" ") + Capacity::unitName(preferredUnit()));
78
dialogWidget().partResizerWidget().init(device(), partition(), freeSectorsBefore(), freeSectorsAfter());
81
void SizeDialogBase::setupConstraints()
83
dialogWidget().partResizerWidget().setMinimumSectors(minSectors());
84
dialogWidget().partResizerWidget().setMaximumSectors(maxSectors());
86
dialogWidget().labelMinSize().setText(Capacity(minSectors() * partition().sectorSize()).toString());
87
dialogWidget().labelMaxSize().setText(Capacity(maxSectors() * partition().sectorSize()).toString());
89
if (!canShrink() && !canGrow())
90
dialogWidget().spinCapacity().setEnabled(false);
93
dialogWidget().partResizerWidget().setMoveAllowed(false);
95
dialogWidget().partResizerWidget().setMaxFirstSector(partition().maxFirstSector());
96
dialogWidget().partResizerWidget().setMinLastSector(partition().minLastSector());
98
const qint64 totalCapacity = sectorsToDialogUnit(partition(), preferredUnit(), dialogWidget().partResizerWidget().totalSectors());
100
const qint64 minCapacity = sectorsToDialogUnit(partition(), preferredUnit(), minSectors());
101
const qint64 maxCapacity = sectorsToDialogUnit(partition(), preferredUnit(), maxSectors());
102
dialogWidget().spinCapacity().setRange(minCapacity, maxCapacity);
104
const qint64 maxFree = totalCapacity - minCapacity;
106
dialogWidget().spinFreeBefore().setRange(0, maxFree);
107
dialogWidget().spinFreeAfter().setRange(0, maxFree);
110
void SizeDialogBase::setupConnections()
112
connect(&dialogWidget().partResizerWidget(), SIGNAL(sectorsBeforeChanged(qint64)), SLOT(onSectorsBeforeChanged(qint64)));
113
connect(&dialogWidget().partResizerWidget(), SIGNAL(sectorsAfterChanged(qint64)), SLOT(onSectorsAfterChanged(qint64)));
114
connect(&dialogWidget().partResizerWidget(), SIGNAL(lengthChanged(qint64)), SLOT(onLengthChanged(qint64)));
116
connect(&dialogWidget().spinFreeBefore(), SIGNAL(valueChanged(int)), SLOT(onFreeSpaceBeforeChanged(int)));
117
connect(&dialogWidget().spinFreeAfter(), SIGNAL(valueChanged(int)), SLOT(onFreeSpaceAfterChanged(int)));
118
connect(&dialogWidget().spinCapacity(), SIGNAL(valueChanged(int)), SLOT(onCapacityChanged(int)));
121
void SizeDialogBase::onSectorsBeforeChanged(qint64 newBefore)
123
dialogWidget().spinFreeBefore().disconnect(this);
124
dialogWidget().spinFreeBefore().setValue(sectorsToDialogUnit(partition(), preferredUnit(), newBefore));
125
connect(&dialogWidget().spinFreeBefore(), SIGNAL(valueChanged(int)), SLOT(onFreeSpaceBeforeChanged(int)));
129
void SizeDialogBase::onSectorsAfterChanged(qint64 newAfter)
131
dialogWidget().spinFreeAfter().disconnect(this);
132
dialogWidget().spinFreeAfter().setValue(sectorsToDialogUnit(partition(), preferredUnit(), newAfter));
133
connect(&dialogWidget().spinFreeAfter(), SIGNAL(valueChanged(int)), SLOT(onFreeSpaceAfterChanged(int)));
137
void SizeDialogBase::onLengthChanged(qint64 newLength)
139
dialogWidget().spinCapacity().disconnect(this);
140
dialogWidget().spinCapacity().setValue(sectorsToDialogUnit(partition(), preferredUnit(), newLength));
141
connect(&dialogWidget().spinCapacity(), SIGNAL(valueChanged(int)), SLOT(onCapacityChanged(int)));
144
void SizeDialogBase::onCapacityChanged(int newCapacity)
146
qint64 newLength = dialogUnitToSectors(partition(), preferredUnit(), newCapacity);
147
dialogWidget().partResizerWidget().updateLength(newLength);
150
void SizeDialogBase::onFreeSpaceBeforeChanged(int newBefore)
152
qint64 newSectorsBefore = dialogUnitToSectors(partition(), preferredUnit(), newBefore);
153
qint64 delta = dialogWidget().partResizerWidget().sectorsBefore() - newSectorsBefore;
154
qint64 newSectorsAfter = dialogWidget().partResizerWidget().sectorsAfter() + delta;
156
if (newSectorsAfter < 0)
158
dialogWidget().partResizerWidget().updateLength(partition().length() + newSectorsAfter);
162
dialogWidget().partResizerWidget().updateSectors(newSectorsBefore, newSectorsAfter);
166
void SizeDialogBase::onFreeSpaceAfterChanged(int newAfter)
168
qint64 newSectorsAfter = dialogUnitToSectors(partition(), preferredUnit(), newAfter);
169
qint64 delta = newSectorsAfter - dialogWidget().partResizerWidget().sectorsAfter();
170
qint64 newSectorsBefore = dialogWidget().partResizerWidget().sectorsBefore() - delta;
172
if (newSectorsBefore < 0)
174
dialogWidget().partResizerWidget().updateLength(partition().length() + newSectorsBefore);
175
newSectorsBefore = 0;
178
dialogWidget().partResizerWidget().updateSectors(newSectorsBefore, newSectorsAfter);
182
const PartitionTable& SizeDialogBase::partitionTable() const
184
Q_ASSERT(device().partitionTable());
186
return *device().partitionTable();