~ubuntu-branches/ubuntu/oneiric/partitionmanager/oneiric

« back to all changes in this revision

Viewing changes to src/gui/sizedialogbase.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-01-23 17:57:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090123175736-2ltrhgg3m55dokbm
Tags: upstream-1.0.0~beta1a
ImportĀ upstreamĀ versionĀ 1.0.0~beta1a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Volker Lanz <vl@fidra.de>                       *
 
3
 *                                                                         *
 
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.                                   *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "gui/sizedialogbase.h"
 
21
#include "gui/partresizerwidget.h"
 
22
#include "gui/sizedialogwidget.h"
 
23
 
 
24
#include "core/partitiontable.h"
 
25
#include "core/device.h"
 
26
#include "core/partition.h"
 
27
 
 
28
 
 
29
#include <kdebug.h>
 
30
 
 
31
SizeDialogBase::SizeDialogBase(QWidget* parent, Capacity::Unit preferred, Device& device, Partition& part, qint64 freebefore, qint64 freeafter) :
 
32
        KDialog(parent),
 
33
        m_SizeDialogWidget(new SizeDialogWidget(this)),
 
34
        m_PreferredUnit(preferred),
 
35
        m_Device(device),
 
36
        m_Partition(part),
 
37
        m_FreeSectorsBefore(freebefore),
 
38
        m_FreeSectorsAfter(freeafter)
 
39
{
 
40
}
 
41
 
 
42
qint64 SizeDialogBase::minSectors() const
 
43
{
 
44
        if (!canShrink())
 
45
                return partition().length();
 
46
 
 
47
        return qMax(partition().sectorsUsed(), partition().minimumSectors());
 
48
}
 
49
 
 
50
qint64 SizeDialogBase::maxSectors() const
 
51
{
 
52
        if (!canGrow())
 
53
                return partition().length();
 
54
 
 
55
        return qMin(partition().length() + freeSectorsBefore() + freeSectorsAfter(), partition().maximumSectors());
 
56
}
 
57
 
 
58
int SizeDialogBase::sectorsToDialogUnit(const Partition& p, Capacity::Unit u, qint64 v)
 
59
{
 
60
        return Capacity(v * p.sectorSize()).toInt(u);
 
61
}
 
62
 
 
63
qint64 SizeDialogBase::dialogUnitToSectors(const Partition& p, Capacity::Unit u, int v)
 
64
{
 
65
        return Capacity::unitFactor(Capacity::Byte, u) * v / p.sectorSize();
 
66
}
 
67
 
 
68
void SizeDialogBase::setupDialog()
 
69
{
 
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()));
 
73
 
 
74
        dialogWidget().spinFreeBefore().setSuffix(QString(" ") + Capacity::unitName(preferredUnit()));
 
75
        dialogWidget().spinFreeAfter().setSuffix(QString(" ") + Capacity::unitName(preferredUnit()));
 
76
        dialogWidget().spinCapacity().setSuffix(QString(" ") + Capacity::unitName(preferredUnit()));
 
77
 
 
78
        dialogWidget().partResizerWidget().init(device(), partition(), freeSectorsBefore(), freeSectorsAfter());
 
79
}
 
80
 
 
81
void SizeDialogBase::setupConstraints()
 
82
{
 
83
        dialogWidget().partResizerWidget().setMinimumSectors(minSectors());
 
84
        dialogWidget().partResizerWidget().setMaximumSectors(maxSectors());
 
85
 
 
86
        dialogWidget().labelMinSize().setText(Capacity(minSectors() * partition().sectorSize()).toString());
 
87
        dialogWidget().labelMaxSize().setText(Capacity(maxSectors() * partition().sectorSize()).toString());
 
88
 
 
89
        if (!canShrink() && !canGrow())
 
90
                dialogWidget().spinCapacity().setEnabled(false);
 
91
 
 
92
        if (!canMove())
 
93
                dialogWidget().partResizerWidget().setMoveAllowed(false);
 
94
 
 
95
        dialogWidget().partResizerWidget().setMaxFirstSector(partition().maxFirstSector());
 
96
        dialogWidget().partResizerWidget().setMinLastSector(partition().minLastSector());
 
97
 
 
98
        const qint64 totalCapacity = sectorsToDialogUnit(partition(), preferredUnit(), dialogWidget().partResizerWidget().totalSectors());
 
99
 
 
100
        const qint64 minCapacity = sectorsToDialogUnit(partition(), preferredUnit(), minSectors());
 
101
        const qint64 maxCapacity = sectorsToDialogUnit(partition(), preferredUnit(), maxSectors());
 
102
        dialogWidget().spinCapacity().setRange(minCapacity, maxCapacity);
 
103
 
 
104
        const qint64 maxFree = totalCapacity - minCapacity;
 
105
 
 
106
        dialogWidget().spinFreeBefore().setRange(0, maxFree);
 
107
        dialogWidget().spinFreeAfter().setRange(0, maxFree);
 
108
}
 
109
 
 
110
void SizeDialogBase::setupConnections()
 
111
{
 
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)));
 
115
 
 
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)));
 
119
}
 
120
 
 
121
void SizeDialogBase::onSectorsBeforeChanged(qint64 newBefore)
 
122
{
 
123
        dialogWidget().spinFreeBefore().disconnect(this);
 
124
        dialogWidget().spinFreeBefore().setValue(sectorsToDialogUnit(partition(), preferredUnit(), newBefore));
 
125
        connect(&dialogWidget().spinFreeBefore(), SIGNAL(valueChanged(int)), SLOT(onFreeSpaceBeforeChanged(int)));
 
126
        setDirty();
 
127
}
 
128
 
 
129
void SizeDialogBase::onSectorsAfterChanged(qint64 newAfter)
 
130
{
 
131
        dialogWidget().spinFreeAfter().disconnect(this);
 
132
        dialogWidget().spinFreeAfter().setValue(sectorsToDialogUnit(partition(), preferredUnit(), newAfter));
 
133
        connect(&dialogWidget().spinFreeAfter(), SIGNAL(valueChanged(int)), SLOT(onFreeSpaceAfterChanged(int)));
 
134
        setDirty();
 
135
}
 
136
 
 
137
void SizeDialogBase::onLengthChanged(qint64 newLength)
 
138
{
 
139
        dialogWidget().spinCapacity().disconnect(this);
 
140
        dialogWidget().spinCapacity().setValue(sectorsToDialogUnit(partition(), preferredUnit(), newLength));
 
141
        connect(&dialogWidget().spinCapacity(), SIGNAL(valueChanged(int)), SLOT(onCapacityChanged(int)));
 
142
}
 
143
 
 
144
void SizeDialogBase::onCapacityChanged(int newCapacity)
 
145
{
 
146
        qint64 newLength = dialogUnitToSectors(partition(), preferredUnit(), newCapacity);
 
147
        dialogWidget().partResizerWidget().updateLength(newLength);
 
148
}
 
149
 
 
150
void SizeDialogBase::onFreeSpaceBeforeChanged(int newBefore)
 
151
{
 
152
        qint64 newSectorsBefore = dialogUnitToSectors(partition(), preferredUnit(), newBefore);
 
153
        qint64 delta = dialogWidget().partResizerWidget().sectorsBefore() - newSectorsBefore;
 
154
        qint64 newSectorsAfter = dialogWidget().partResizerWidget().sectorsAfter() + delta;
 
155
 
 
156
        if (newSectorsAfter < 0)
 
157
        {
 
158
                dialogWidget().partResizerWidget().updateLength(partition().length() + newSectorsAfter);
 
159
                newSectorsAfter = 0;
 
160
        }
 
161
 
 
162
        dialogWidget().partResizerWidget().updateSectors(newSectorsBefore, newSectorsAfter);
 
163
        setDirty();
 
164
}
 
165
 
 
166
void SizeDialogBase::onFreeSpaceAfterChanged(int newAfter)
 
167
{
 
168
        qint64 newSectorsAfter = dialogUnitToSectors(partition(), preferredUnit(), newAfter);
 
169
        qint64 delta = newSectorsAfter - dialogWidget().partResizerWidget().sectorsAfter();
 
170
        qint64 newSectorsBefore = dialogWidget().partResizerWidget().sectorsBefore() - delta;
 
171
 
 
172
        if (newSectorsBefore < 0)
 
173
        {
 
174
                dialogWidget().partResizerWidget().updateLength(partition().length() + newSectorsBefore);
 
175
                newSectorsBefore = 0;
 
176
        }
 
177
 
 
178
        dialogWidget().partResizerWidget().updateSectors(newSectorsBefore, newSectorsAfter);
 
179
        setDirty();
 
180
}
 
181
 
 
182
const PartitionTable& SizeDialogBase::partitionTable() const
 
183
{
 
184
        Q_ASSERT(device().partitionTable());
 
185
 
 
186
        return *device().partitionTable();
 
187
}