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/partresizerwidget.h"
21
#include "gui/partwidget.h"
23
#include "core/partition.h"
24
#include "core/device.h"
26
#include "fs/filesystem.h"
29
#include <QMouseEvent>
30
#include <QPaintEvent>
31
#include <QResizeEvent>
34
#include <kcolorscheme.h>
36
const qint32 PartResizerWidget::m_HandleWidth = 16;
37
const qint32 PartResizerWidget::m_HandleHeight = 59;
39
/** Creates a new PartResizerWidget
41
Initializing is mostly done in init().
43
@param parent pointer to the parent widget
45
PartResizerWidget::PartResizerWidget(QWidget* parent) :
59
m_DraggedWidget(NULL),
66
/** Intializes the PartResizerWidget
67
@param d the Device the Partition is on
68
@param p the Partition to show and/or resize
69
@param freeBefore number of sectors free before the Partition
70
@param freeAfter number of sectors free after the Partition
72
void PartResizerWidget::init(Device& d, Partition& p, qint64 freeBefore, qint64 freeAfter)
77
setSectorsBefore(freeBefore);
78
setSectorsAfter(freeAfter);
80
setTotalSectors(sectorsBefore() + partition().length() + sectorsAfter());
82
setMinimumSectors(qMax(partition().sectorsUsed(), partition().minimumSectors()));
83
setMaximumSectors(qMin(totalSectors(), partition().maximumSectors()));
85
/** @todo get real pixmaps for the handles */
86
QPixmap pixmap(handleWidth(), handleHeight());
87
pixmap.fill(QColor(0x44, 0x44, 0x44));
89
leftHandle().setPixmap(pixmap);
90
rightHandle().setPixmap(pixmap);
91
leftHandle().setFixedSize(handleWidth(), handleHeight());
92
rightHandle().setFixedSize(handleWidth(), handleHeight());
95
m_PartWidget = new PartWidget(this, NULL, &partition());
99
leftHandle().setCursor(Qt::SizeHorCursor);
100
rightHandle().setCursor(Qt::SizeHorCursor);
103
partWidget().setCursor(Qt::SizeAllCursor);
105
partWidget().setToolTip(QString());
111
qint64 PartResizerWidget::sectorsPerPixel() const
113
return totalSectors() / (width() - 2 * handleWidth());
116
int PartResizerWidget::partWidgetStart() const
118
return handleWidth() + sectorsBefore() / sectorsPerPixel();
121
int PartResizerWidget::partWidgetWidth() const
123
return partition().length() / sectorsPerPixel();
126
void PartResizerWidget::updatePositions()
128
partWidget().move(partWidgetStart(), 0);
129
partWidget().resize(partWidgetWidth(), height() - 1);
130
leftHandle().move(partWidgetStart() - leftHandle().width(), 0);
131
rightHandle().move(partWidgetStart() + partWidgetWidth(), 0);
133
partWidget().update();
136
void PartResizerWidget::resizeEvent(QResizeEvent* event)
139
QWidget::resizeEvent(event);
142
void PartResizerWidget::paintEvent(QPaintEvent*)
144
QPainter painter(this);
145
painter.setPen(Qt::NoPen);
146
painter.setBrush(QColor(0x99, 0x99, 0x99));
147
painter.drawRect(QRect(handleWidth(), 0, width() - (2 * handleWidth()) - 1, height() - 1));
150
void PartResizerWidget::mousePressEvent(QMouseEvent* event)
155
if (event->button() == Qt::LeftButton)
157
m_DraggedWidget = static_cast<QWidget*>(childAt(event->pos()));
159
if (m_DraggedWidget != NULL)
161
if (partWidget().isAncestorOf(m_DraggedWidget))
162
m_DraggedWidget = &partWidget();
164
m_Hotspot = m_DraggedWidget->mapFromParent(event->pos()).x();
169
void PartResizerWidget::mouseMoveEvent(QMouseEvent* event)
171
int x = event->pos().x() - m_Hotspot;
173
if (draggedWidget() == &leftHandle())
175
const qint64 newSectorsBefore = qMax(x * sectorsPerPixel(), 0LL);
176
updateSectorsBefore(newSectorsBefore);
178
else if (draggedWidget() == &rightHandle())
180
const qint64 newSectorsAfter = qMax((width() - rightHandle().width() - x) * sectorsPerPixel(), 0LL);
181
updateSectorsAfter(newSectorsAfter);
183
else if (draggedWidget() == &partWidget())
186
qint64 newSectorsBefore = qMax(x * sectorsPerPixel(), 0LL);
187
qint64 newSectorsAfter = sectorsAfter() + sectorsBefore() - newSectorsBefore;
189
if (newSectorsAfter < 0)
192
newSectorsBefore = sectorsBefore() + sectorsAfter();
195
if (newSectorsBefore != sectorsBefore() && newSectorsAfter != sectorsAfter())
196
updateSectors(newSectorsBefore, newSectorsAfter);
200
/** Updates the start and end sector of the Partition.
201
@param newSectorsBefore new value for free sectors before the Partition
202
@param newSectorsAfter new value for free sectors after the Partition
203
@return true on success
205
bool PartResizerWidget::updateSectors(qint64 newSectorsBefore, qint64 newSectorsAfter)
207
Q_ASSERT(newSectorsBefore >= 0);
208
Q_ASSERT(newSectorsAfter >= 0);
209
Q_ASSERT(newSectorsBefore + newSectorsAfter + partition().length() == totalSectors());
211
if (newSectorsBefore < 0 || newSectorsAfter < 0)
213
kWarning() << "new sectors before partition: " << newSectorsBefore;
214
kWarning() << "new sectors after partition: " << newSectorsBefore;
218
if (newSectorsBefore + newSectorsAfter + partition().length() != totalSectors())
220
kWarning() << "total sectors: " << totalSectors();
221
kWarning() << "new sectors before partition: " << newSectorsBefore;
222
kWarning() << "new sectors after partition: " << newSectorsBefore;
223
kWarning() << "partition length: " << partition().length();
230
const qint64 oldBefore = sectorsBefore();
231
const qint64 oldAfter = sectorsAfter();
233
// Two hacky things about updating free sectors before and after a partition in one go:
234
// 1) We have to call updateSectorsBefore and updateSectorsAfter with length-checking disabled,
235
// because the partition might in between those calls get smaller or bigger than
236
// allowed. The second call will, of course, restore the original length.
237
// 2) If the user moves the mouse fast enough, it's possible to move the beginning past the
238
// end or the end in front of the beginning of the partition in between the calls. Both
239
// methods won't allow that and return false in that case. We try moving the beginning first and
240
// just move the end first if that fails.
241
if (!updateSectorsBefore(newSectorsBefore, false))
243
updateSectorsAfter(newSectorsAfter, false);
244
updateSectorsBefore(newSectorsBefore, false);
247
updateSectorsAfter(newSectorsAfter, false);
251
if (oldBefore != sectorsBefore())
254
emit sectorsBeforeChanged(sectorsBefore());
257
if (oldAfter != sectorsAfter())
260
emit sectorsAfterChanged(sectorsAfter());
269
void PartResizerWidget::mouseReleaseEvent(QMouseEvent* event)
271
if (event->button() == Qt::LeftButton)
272
m_DraggedWidget = NULL;
275
/** Updates the free sectors before the Partition
276
@param newSectorsBefore new value for number of sectors free before Partition
277
@param enableLengthCheck true if the method is supposed to do some sanity checking on the Partition length
278
@return true on success
280
bool PartResizerWidget::updateSectorsBefore(qint64 newSectorsBefore, bool enableLengthCheck)
282
Q_ASSERT(newSectorsBefore >= 0);
284
if (newSectorsBefore < 0)
286
kWarning() << "new sectors before partition: " << newSectorsBefore;
290
const qint64 oldSectorsBefore = sectorsBefore();
291
const qint64 newLength = partition().length() + oldSectorsBefore - newSectorsBefore;
293
if (enableLengthCheck)
295
if (newLength < minimumSectors())
296
newSectorsBefore -= minimumSectors() - newLength;
298
if (newLength > maximumSectors())
299
newSectorsBefore += newLength - maximumSectors();
301
else if (newLength < 0)
304
qint64 newFirstSector = partition().firstSector() + newSectorsBefore - oldSectorsBefore;
306
if (maxFirstSector() > -1 && newFirstSector > maxFirstSector())
308
newSectorsBefore -= newFirstSector - maxFirstSector();
309
newFirstSector = maxFirstSector();
312
if (newSectorsBefore >= 0 && newSectorsBefore != oldSectorsBefore && (partition().children().size() == 0 || checkSnap(*partition().children().first(), oldSectorsBefore - newSectorsBefore)))
314
setSectorsBefore(newSectorsBefore);
316
partition().setFirstSector(newFirstSector);
317
partition().fileSystem().setFirstSector(newFirstSector);
321
emit sectorsBeforeChanged(sectorsBefore());
322
emit lengthChanged(partition().length());
332
bool PartResizerWidget::checkSnap(const Partition& child, qint64 delta) const
334
if (!partition().roles().has(PartitionRole::Extended))
337
if (child.roles().has(PartitionRole::Unallocated))
340
return qAbs(delta) >= device().cylinderSize();
343
void PartResizerWidget::resizeLogicals()
345
if (!partition().roles().has(PartitionRole::Extended) || partition().children().size() == 0)
348
Q_ASSERT(device().partitionTable());
350
device().partitionTable()->removeUnallocated(&partition());
351
device().partitionTable()->insertUnallocated(device(), &partition(), partition().firstSector());
353
partWidget().updateChildren();
356
/** Updates the number of free sectors after the Partition.
357
@param newSectorsAfter new value for number of sectors free after Partition
358
@param enableLengthCheck true if the method is supposed to do some sanity checking on the Partition length
359
@return true on success
361
bool PartResizerWidget::updateSectorsAfter(qint64 newSectorsAfter, bool enableLengthCheck)
363
Q_ASSERT(newSectorsAfter >= 0);
365
if (newSectorsAfter < 0)
367
kWarning() << "new sectors after partition: " << newSectorsAfter;
371
const qint64 oldSectorsAfter = sectorsAfter();
372
const qint64 newLength = partition().length() + oldSectorsAfter - newSectorsAfter;
374
if (enableLengthCheck)
376
if (newLength < minimumSectors())
377
newSectorsAfter -= minimumSectors() - newLength;
379
if (newLength > maximumSectors())
380
newSectorsAfter += newLength - maximumSectors();
382
else if (newLength < 0)
385
qint64 newLastSector = partition().lastSector() + oldSectorsAfter - newSectorsAfter;
387
if (minLastSector() > -1 && newLastSector < minLastSector())
389
newSectorsAfter += newLastSector - minLastSector();
390
newLastSector = minLastSector();
393
if (newSectorsAfter >= 0 && newSectorsAfter != oldSectorsAfter && (partition().children().size() == 0 || checkSnap(*partition().children().last(), oldSectorsAfter - newSectorsAfter)))
395
setSectorsAfter(newSectorsAfter);
397
partition().setLastSector(newLastSector);
398
partition().fileSystem().setLastSector(newLastSector);
402
emit sectorsAfterChanged(sectorsAfter());
403
emit lengthChanged(partition().length());
413
/** Updates the Partition's length
414
@param newLength the new length
415
@return true on success
417
bool PartResizerWidget::updateLength(qint64 newLength)
419
newLength = qBound(minimumSectors(), newLength, qMin(totalSectors(), maximumSectors()));
421
if (newLength == partition().length())
424
const qint64 oldLength = partition().length();
425
qint64 delta = newLength - oldLength;
427
qint64 tmp = qMin(delta, sectorsAfter());
432
setSectorsAfter(sectorsAfter() - tmp);
433
partition().setLastSector(partition().lastSector() + tmp);
434
partition().fileSystem().setLastSector(partition().lastSector() + tmp);
435
emit sectorsAfterChanged(sectorsAfter());
438
tmp = qMin(delta, sectorsBefore());;
443
setSectorsBefore(sectorsBefore() - tmp);
444
partition().setFirstSector(partition().firstSector() - tmp);
445
partition().fileSystem().setFirstSector(partition().firstSector() - tmp);
446
emit sectorsBeforeChanged(sectorsBefore());
449
if (partition().length() != oldLength)
451
emit lengthChanged(partition().length());
460
/** Sets the minimum sectors the Partition can be long.
461
@note This value can never be less than 0 and never be higher than totalSectors()
462
@param s the new minimum length
464
void PartResizerWidget::setMinimumSectors(qint64 s)
466
m_MinimumSectors = qBound(0LL, s, totalSectors());
469
/** Sets the maximum sectors the Partition can be long.
470
@note This value can never be less than 0 and never by higher than totalSectors()
471
@param s the new maximum length
473
void PartResizerWidget::setMaximumSectors(qint64 s)
475
m_MaximumSectors = qBound(0LL, s, totalSectors());
478
/** Sets if moving the Partition is allowed.
479
@param b true if moving is allowed
481
void PartResizerWidget::setMoveAllowed(bool b)
486
partWidget().setCursor(Qt::ArrowCursor);