2014-07-29 13:37:32 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
2015-04-17 17:24:28 +02:00
|
|
|
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
|
2014-07-29 13:37:32 +02:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-12-03 19:38:56 +01:00
|
|
|
#include "gui/PartitionBarsView.h"
|
2014-07-29 13:37:32 +02:00
|
|
|
|
2014-08-08 13:25:56 +02:00
|
|
|
#include <core/PartitionModel.h>
|
2015-10-22 17:48:02 +02:00
|
|
|
#include <core/ColorUtils.h>
|
|
|
|
|
|
|
|
#include <kpmcore/core/device.h>
|
2014-07-29 13:37:32 +02:00
|
|
|
|
2015-04-17 17:24:28 +02:00
|
|
|
#include "utils/CalamaresUtilsGui.h"
|
|
|
|
|
2015-10-22 17:48:02 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
// Qt
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
|
2015-11-27 17:26:30 +01:00
|
|
|
static const int VIEW_HEIGHT = CalamaresUtils::defaultFontHeight() + 8;
|
2014-07-29 15:56:25 +02:00
|
|
|
static const int CORNER_RADIUS = 3;
|
2014-07-29 13:37:32 +02:00
|
|
|
static const int EXTENDED_PARTITION_MARGIN = 4;
|
|
|
|
|
|
|
|
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::PartitionBarsView( QWidget* parent )
|
2015-12-03 19:38:56 +01:00
|
|
|
: QAbstractItemView( parent )
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
|
|
|
|
setFrameStyle( QFrame::NoFrame );
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::~PartitionBarsView()
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
|
|
|
QSize
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::minimumSizeHint() const
|
2015-04-17 12:40:56 +02:00
|
|
|
{
|
|
|
|
return sizeHint();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
QSize
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::sizeHint() const
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
2015-04-22 11:17:22 +02:00
|
|
|
return QSize( -1, VIEW_HEIGHT );
|
2014-07-29 13:37:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
void
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::paintEvent( QPaintEvent* event )
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
QPainter painter( viewport() );
|
2014-07-29 15:56:25 +02:00
|
|
|
painter.fillRect( rect(), palette().window() );
|
|
|
|
painter.setRenderHint( QPainter::Antialiasing );
|
2015-04-17 17:24:28 +02:00
|
|
|
|
|
|
|
QRect partitionsRect = rect();
|
|
|
|
partitionsRect.setHeight( VIEW_HEIGHT );
|
|
|
|
|
|
|
|
painter.save();
|
|
|
|
drawPartitions( &painter, partitionsRect, QModelIndex() );
|
|
|
|
painter.restore();
|
2014-07-29 13:37:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 15:56:25 +02:00
|
|
|
static void
|
|
|
|
drawSection( QPainter* painter, const QRect& rect_, int x, int width, const QModelIndex& index )
|
|
|
|
{
|
2015-10-22 17:48:02 +02:00
|
|
|
QColor color = index.isValid() ?
|
|
|
|
index.data( Qt::DecorationRole ).value< QColor >() :
|
|
|
|
ColorUtils::unknownDisklabelColor();
|
|
|
|
bool isFreeSpace = index.isValid() ?
|
|
|
|
index.data( PartitionModel::IsFreeSpaceRole ).toBool() :
|
|
|
|
true;
|
2014-07-29 15:56:25 +02:00
|
|
|
|
|
|
|
QRect rect = rect_;
|
|
|
|
const int y = rect.y();
|
|
|
|
const int height = rect.height();
|
|
|
|
const int radius = qMax( 1, CORNER_RADIUS - ( VIEW_HEIGHT - height ) / 2 );
|
|
|
|
painter->setClipRect( x, y, width, height );
|
|
|
|
painter->translate( 0.5, 0.5 );
|
|
|
|
|
|
|
|
rect.adjust( 0, 0, -1, -1 );
|
|
|
|
const QColor borderColor = color.darker();
|
|
|
|
painter->setPen( borderColor );
|
|
|
|
painter->setBrush( color );
|
|
|
|
painter->drawRoundedRect( rect, radius, radius );
|
|
|
|
|
|
|
|
// Draw shade
|
|
|
|
if ( !isFreeSpace )
|
|
|
|
rect.adjust( 2, 2, -2, -2 );
|
|
|
|
|
|
|
|
QLinearGradient gradient( 0, 0, 0, height / 2 );
|
|
|
|
|
|
|
|
qreal c = isFreeSpace ? 0 : 1;
|
|
|
|
gradient.setColorAt( 0, QColor::fromRgbF( c, c, c, 0.3 ) );
|
|
|
|
gradient.setColorAt( 1, QColor::fromRgbF( c, c, c, 0 ) );
|
|
|
|
|
|
|
|
painter->setPen( Qt::NoPen );
|
|
|
|
painter->setBrush( gradient );
|
|
|
|
painter->drawRoundedRect( rect, radius, radius );
|
|
|
|
|
|
|
|
painter->translate( -0.5, -0.5 );
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
void
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::drawPartitions( QPainter* painter, const QRect& rect, const QModelIndex& parent )
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
2015-10-22 17:48:02 +02:00
|
|
|
PartitionModel* modl = qobject_cast< PartitionModel* >( model() );
|
2014-07-29 13:37:32 +02:00
|
|
|
if ( !modl )
|
|
|
|
return;
|
|
|
|
const int count = modl->rowCount( parent );
|
|
|
|
const int totalWidth = rect.width();
|
2015-10-22 17:48:02 +02:00
|
|
|
qDebug() << "count:" << count << "totalWidth:" << totalWidth;
|
2014-08-05 09:54:30 +02:00
|
|
|
struct Item
|
|
|
|
{
|
2014-07-29 13:37:32 +02:00
|
|
|
qreal size;
|
|
|
|
QModelIndex index;
|
|
|
|
};
|
|
|
|
QVector< Item > items( count );
|
|
|
|
qreal total = 0;
|
|
|
|
for ( int row = 0; row < count; ++row )
|
|
|
|
{
|
|
|
|
QModelIndex index = modl->index( row, 0, parent );
|
|
|
|
qreal size = index.data( PartitionModel::SizeRole ).toLongLong();
|
|
|
|
total += size;
|
2014-07-29 15:56:25 +02:00
|
|
|
items[ row ] = { size, index };
|
2014-07-29 13:37:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int x = rect.x();
|
|
|
|
for ( int row = 0; row < count; ++row )
|
|
|
|
{
|
|
|
|
const auto& item = items[ row ];
|
|
|
|
int width;
|
|
|
|
if ( row < count - 1 )
|
|
|
|
width = totalWidth * ( item.size / total );
|
|
|
|
else
|
|
|
|
// Make sure we fill the last pixel column
|
|
|
|
width = rect.right() - x + 1;
|
|
|
|
|
2014-07-29 15:56:25 +02:00
|
|
|
drawSection( painter, rect, x, width, item.index );
|
2014-07-29 13:37:32 +02:00
|
|
|
if ( modl->hasChildren( item.index ) )
|
|
|
|
{
|
|
|
|
QRect subRect(
|
|
|
|
x + EXTENDED_PARTITION_MARGIN,
|
|
|
|
rect.y() + EXTENDED_PARTITION_MARGIN,
|
|
|
|
width - 2 * EXTENDED_PARTITION_MARGIN,
|
|
|
|
rect.height() - 2 * EXTENDED_PARTITION_MARGIN
|
|
|
|
);
|
|
|
|
drawPartitions( painter, subRect, item.index );
|
|
|
|
}
|
|
|
|
x += width;
|
|
|
|
}
|
2015-10-22 17:48:02 +02:00
|
|
|
|
|
|
|
if ( !count &&
|
|
|
|
!modl->device()->partitionTable() ) // No disklabel or unknown
|
|
|
|
{
|
|
|
|
int width = rect.right() - rect.x() + 1;
|
|
|
|
drawSection( painter, rect, rect.x(), width, QModelIndex() );
|
|
|
|
}
|
2014-07-29 13:37:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
QModelIndex
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::indexAt( const QPoint& point ) const
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
QRect
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::visualRect( const QModelIndex& index ) const
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
return QRect();
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
QRegion
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::visualRegionForSelection( const QItemSelection& selection ) const
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
return QRegion();
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
int
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::horizontalOffset() const
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
int
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::verticalOffset() const
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
void
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::scrollTo( const QModelIndex& index, ScrollHint hint )
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
QModelIndex
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers )
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
bool
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::isIndexHidden( const QModelIndex& index ) const
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-17 12:40:56 +02:00
|
|
|
|
2014-07-29 13:37:32 +02:00
|
|
|
void
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::setSelection( const QRect& rect, QItemSelectionModel::SelectionFlags flags )
|
2014-07-29 13:37:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-23 18:00:56 +02:00
|
|
|
|
|
|
|
void
|
2015-12-03 17:19:02 +01:00
|
|
|
PartitionBarsView::updateGeometries()
|
2015-04-23 18:00:56 +02:00
|
|
|
{
|
|
|
|
updateGeometry(); //get a new rect() for redrawing all the labels
|
|
|
|
}
|