2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2015-12-03 19:38:56 +01:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2015-2016 Teo Mrnjavac <teo@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2015-12-03 19:38:56 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PartitionLabelsView.h"
|
|
|
|
|
2019-05-09 13:58:20 +02:00
|
|
|
#include "core/ColorUtils.h"
|
2020-08-22 01:19:58 +02:00
|
|
|
#include "core/PartitionModel.h"
|
2015-12-03 19:38:56 +01:00
|
|
|
|
2019-05-09 13:58:20 +02:00
|
|
|
#include "utils/CalamaresUtilsGui.h"
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
#include "utils/Units.h"
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
#include <kpmcore/core/device.h>
|
2015-12-16 15:48:25 +01:00
|
|
|
#include <kpmcore/fs/filesystem.h>
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
#include <KFormat>
|
|
|
|
|
|
|
|
// Qt
|
2016-02-16 14:45:50 +01:00
|
|
|
#include <QGuiApplication>
|
2015-12-16 18:22:36 +01:00
|
|
|
#include <QMouseEvent>
|
2015-12-03 19:38:56 +01:00
|
|
|
#include <QPainter>
|
|
|
|
|
2019-05-09 13:58:20 +02:00
|
|
|
using CalamaresUtils::operator""_MiB;
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
static const int LAYOUT_MARGIN = 4;
|
2020-08-22 01:19:58 +02:00
|
|
|
static const int LABEL_PARTITION_SQUARE_MARGIN = qMax( QFontMetrics( CalamaresUtils::defaultFont() ).ascent() - 2, 18 );
|
2015-12-15 17:35:48 +01:00
|
|
|
static const int LABELS_MARGIN = LABEL_PARTITION_SQUARE_MARGIN;
|
2015-12-16 18:22:36 +01:00
|
|
|
static const int CORNER_RADIUS = 2;
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
|
2017-09-10 21:17:33 +02:00
|
|
|
static QStringList
|
2015-12-03 19:38:56 +01:00
|
|
|
buildUnknownDisklabelTexts( Device* dev )
|
|
|
|
{
|
|
|
|
QStringList texts = { QObject::tr( "Unpartitioned space or unknown partition table" ),
|
2016-08-31 05:30:45 +02:00
|
|
|
KFormat().formatByteSize( dev->totalLogical() * dev->logicalSize() ) };
|
2015-12-03 19:38:56 +01:00
|
|
|
return texts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PartitionLabelsView::PartitionLabelsView( QWidget* parent )
|
|
|
|
: QAbstractItemView( parent )
|
2017-09-10 21:17:33 +02:00
|
|
|
, m_canBeSelected( []( const QModelIndex& ) { return true; } )
|
2016-02-26 12:44:08 +01:00
|
|
|
, m_extendedPartitionHidden( false )
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
|
|
|
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
|
|
|
|
setFrameStyle( QFrame::NoFrame );
|
2015-12-11 18:44:41 +01:00
|
|
|
setSelectionBehavior( QAbstractItemView::SelectRows );
|
|
|
|
setSelectionMode( QAbstractItemView::SingleSelection );
|
2020-08-22 01:19:58 +02:00
|
|
|
this->setObjectName( "partitionLabel" );
|
2015-12-11 18:44:41 +01:00
|
|
|
setMouseTracking( true );
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
PartitionLabelsView::~PartitionLabelsView() {}
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
QSize
|
|
|
|
PartitionLabelsView::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return sizeHint();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSize
|
|
|
|
PartitionLabelsView::sizeHint() const
|
|
|
|
{
|
|
|
|
QAbstractItemModel* modl = model();
|
|
|
|
if ( modl )
|
|
|
|
{
|
|
|
|
return QSize( -1, LAYOUT_MARGIN + sizeForAllLabels( rect().width() ).height() );
|
|
|
|
}
|
|
|
|
return QSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionLabelsView::paintEvent( QPaintEvent* event )
|
|
|
|
{
|
2019-04-17 11:57:46 +02:00
|
|
|
Q_UNUSED( event )
|
2017-09-10 21:17:33 +02:00
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
QPainter painter( viewport() );
|
|
|
|
painter.fillRect( rect(), palette().window() );
|
|
|
|
painter.setRenderHint( QPainter::Antialiasing );
|
|
|
|
|
2015-12-16 18:22:36 +01:00
|
|
|
QRect lRect = labelsRect();
|
2015-12-03 19:38:56 +01:00
|
|
|
|
2015-12-16 18:22:36 +01:00
|
|
|
drawLabels( &painter, lRect, QModelIndex() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QRect
|
|
|
|
PartitionLabelsView::labelsRect() const
|
|
|
|
{
|
|
|
|
return rect().adjusted( 0, LAYOUT_MARGIN, 0, 0 );
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
drawPartitionSquare( QPainter* painter, const QRect& rect, const QBrush& brush )
|
|
|
|
{
|
|
|
|
painter->fillRect( rect.adjusted( 1, 1, -1, -1 ), brush );
|
|
|
|
painter->setRenderHint( QPainter::Antialiasing, true );
|
|
|
|
painter->setPen( QPalette().shadow().color() );
|
|
|
|
painter->translate( .5, .5 );
|
2015-12-16 18:22:36 +01:00
|
|
|
painter->drawRoundedRect( rect.adjusted( 0, 0, -1, -1 ), CORNER_RADIUS, CORNER_RADIUS );
|
|
|
|
painter->translate( -.5, -.5 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
drawSelectionSquare( QPainter* painter, const QRect& rect, const QBrush& brush )
|
|
|
|
{
|
2015-12-17 14:00:26 +01:00
|
|
|
painter->save();
|
2015-12-16 18:22:36 +01:00
|
|
|
painter->setPen( QPen( brush.color().darker(), 1 ) );
|
|
|
|
QColor highlightColor = QPalette().highlight().color();
|
|
|
|
highlightColor = highlightColor.lighter( 500 );
|
|
|
|
highlightColor.setAlpha( 120 );
|
|
|
|
painter->setBrush( highlightColor );
|
|
|
|
painter->translate( .5, .5 );
|
|
|
|
painter->drawRoundedRect( rect.adjusted( 0, 0, -1, -1 ), CORNER_RADIUS, CORNER_RADIUS );
|
2015-12-03 19:38:56 +01:00
|
|
|
painter->translate( -.5, -.5 );
|
2015-12-17 14:00:26 +01:00
|
|
|
painter->restore();
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QModelIndexList
|
|
|
|
PartitionLabelsView::getIndexesToDraw( const QModelIndex& parent ) const
|
|
|
|
{
|
|
|
|
QModelIndexList list;
|
|
|
|
|
|
|
|
QAbstractItemModel* modl = model();
|
|
|
|
if ( !modl )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-03 19:38:56 +01:00
|
|
|
return list;
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
for ( int row = 0; row < modl->rowCount( parent ); ++row )
|
|
|
|
{
|
|
|
|
QModelIndex index = modl->index( row, 0, parent );
|
2015-12-22 13:08:54 +01:00
|
|
|
|
|
|
|
//HACK: horrible special casing follows.
|
|
|
|
// To save vertical space, we choose to hide short instances of free space.
|
2019-05-09 13:58:20 +02:00
|
|
|
// Arbitrary limit: 10MiB.
|
|
|
|
const qint64 maxHiddenB = 10_MiB;
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( index.data( PartitionModel::IsFreeSpaceRole ).toBool()
|
|
|
|
&& index.data( PartitionModel::SizeRole ).toLongLong() < maxHiddenB )
|
|
|
|
{
|
2015-12-22 13:08:54 +01:00
|
|
|
continue;
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-22 13:08:54 +01:00
|
|
|
|
2016-02-11 16:00:07 +01:00
|
|
|
if ( !modl->hasChildren( index ) || !m_extendedPartitionHidden )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2016-02-11 16:00:07 +01:00
|
|
|
list.append( index );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2016-02-11 16:00:07 +01:00
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
if ( modl->hasChildren( index ) )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-03 19:38:56 +01:00
|
|
|
list.append( getIndexesToDraw( index ) );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-15 16:56:10 +01:00
|
|
|
QStringList
|
|
|
|
PartitionLabelsView::buildTexts( const QModelIndex& index ) const
|
|
|
|
{
|
|
|
|
QString firstLine, secondLine;
|
|
|
|
|
|
|
|
if ( index.data( PartitionModel::IsPartitionNewRole ).toBool() )
|
|
|
|
{
|
2019-01-07 17:26:53 +01:00
|
|
|
QString label = index.data( PartitionModel::FileSystemLabelRole ).toString();
|
|
|
|
|
|
|
|
if ( !label.isEmpty() )
|
|
|
|
{
|
|
|
|
firstLine = label;
|
|
|
|
}
|
2016-07-22 15:03:37 +02:00
|
|
|
else
|
2019-01-07 17:26:53 +01:00
|
|
|
{
|
2020-08-22 01:19:58 +02:00
|
|
|
QString mountPoint = index.sibling( index.row(), PartitionModel::MountPointColumn ).data().toString();
|
2019-01-07 17:26:53 +01:00
|
|
|
if ( mountPoint == "/" )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
|
|
|
firstLine = m_customNewRootLabel.isEmpty() ? tr( "Root" ) : m_customNewRootLabel;
|
|
|
|
}
|
2019-01-07 17:26:53 +01:00
|
|
|
else if ( mountPoint == "/home" )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2019-01-07 17:26:53 +01:00
|
|
|
firstLine = tr( "Home" );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2019-01-07 17:26:53 +01:00
|
|
|
else if ( mountPoint == "/boot" )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2019-01-07 17:26:53 +01:00
|
|
|
firstLine = tr( "Boot" );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
|
|
|
else if ( mountPoint.contains( "/efi" )
|
|
|
|
&& index.data( PartitionModel::FileSystemTypeRole ).toInt() == FileSystem::Fat32 )
|
|
|
|
{
|
2019-01-07 17:26:53 +01:00
|
|
|
firstLine = tr( "EFI system" );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2019-01-07 17:26:53 +01:00
|
|
|
else if ( index.data( PartitionModel::FileSystemTypeRole ).toInt() == FileSystem::LinuxSwap )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2019-01-07 17:26:53 +01:00
|
|
|
firstLine = tr( "Swap" );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2019-01-07 17:26:53 +01:00
|
|
|
else if ( !mountPoint.isEmpty() )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2019-01-07 17:26:53 +01:00
|
|
|
firstLine = tr( "New partition for %1" ).arg( mountPoint );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2019-01-07 17:26:53 +01:00
|
|
|
else
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2019-01-07 17:26:53 +01:00
|
|
|
firstLine = tr( "New partition" );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2019-01-07 17:26:53 +01:00
|
|
|
}
|
2015-12-15 16:56:10 +01:00
|
|
|
}
|
|
|
|
else if ( index.data( PartitionModel::OsproberNameRole ).toString().isEmpty() )
|
2015-12-22 12:49:36 +01:00
|
|
|
{
|
2015-12-15 16:56:10 +01:00
|
|
|
firstLine = index.data().toString();
|
2017-02-02 16:58:53 +01:00
|
|
|
if ( firstLine.startsWith( "/dev/" ) )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
|
|
|
firstLine.remove( 0, 5 ); // "/dev/"
|
|
|
|
}
|
2015-12-22 12:49:36 +01:00
|
|
|
}
|
2015-12-15 16:56:10 +01:00
|
|
|
else
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-15 16:56:10 +01:00
|
|
|
firstLine = index.data( PartitionModel::OsproberNameRole ).toString();
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-15 16:56:10 +01:00
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( index.data( PartitionModel::IsFreeSpaceRole ).toBool()
|
|
|
|
|| index.data( PartitionModel::FileSystemTypeRole ).toInt() == FileSystem::Extended )
|
|
|
|
{
|
|
|
|
secondLine = index.sibling( index.row(), PartitionModel::SizeColumn ).data().toString();
|
|
|
|
}
|
2015-12-16 15:48:25 +01:00
|
|
|
else
|
2019-04-11 16:28:35 +02:00
|
|
|
//: size[number] filesystem[name]
|
2015-12-16 15:48:25 +01:00
|
|
|
secondLine = tr( "%1 %2" )
|
2020-08-22 01:19:58 +02:00
|
|
|
.arg( index.sibling( index.row(), PartitionModel::SizeColumn ).data().toString() )
|
|
|
|
.arg( index.sibling( index.row(), PartitionModel::FileSystemColumn ).data().toString() );
|
2015-12-15 16:56:10 +01:00
|
|
|
|
|
|
|
return { firstLine, secondLine };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
void
|
2020-08-22 01:19:58 +02:00
|
|
|
PartitionLabelsView::drawLabels( QPainter* painter, const QRect& rect, const QModelIndex& parent )
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
|
|
|
PartitionModel* modl = qobject_cast< PartitionModel* >( model() );
|
|
|
|
if ( !modl )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-03 19:38:56 +01:00
|
|
|
return;
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-03 19:38:56 +01:00
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QModelIndexList indexesToDraw = getIndexesToDraw( parent );
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
int label_x = rect.x();
|
|
|
|
int label_y = rect.y();
|
2016-09-01 14:21:05 +02:00
|
|
|
for ( const QModelIndex& index : indexesToDraw )
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
2015-12-15 16:56:10 +01:00
|
|
|
QStringList texts = buildTexts( index );
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
QSize labelSize = sizeForLabel( texts );
|
|
|
|
|
|
|
|
QColor labelColor = index.data( Qt::DecorationRole ).value< QColor >();
|
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( label_x + labelSize.width() > rect.width() ) //wrap to new line if overflow
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
|
|
|
label_x = rect.x();
|
2015-12-15 17:06:26 +01:00
|
|
|
label_y += labelSize.height() + labelSize.height() / 4;
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
|
|
|
|
// Draw hover
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( selectionMode() != QAbstractItemView::NoSelection && // no hover without selection
|
|
|
|
m_hoveredIndex.isValid() && index == m_hoveredIndex )
|
2015-12-16 18:22:36 +01:00
|
|
|
{
|
|
|
|
painter->save();
|
|
|
|
QRect labelRect( QPoint( label_x, label_y ), labelSize );
|
2020-08-22 01:19:58 +02:00
|
|
|
labelRect.adjust( 0, -LAYOUT_MARGIN, 0, -2 * LAYOUT_MARGIN );
|
2015-12-16 18:22:36 +01:00
|
|
|
painter->translate( 0.5, 0.5 );
|
|
|
|
QRect hoverRect = labelRect.adjusted( 0, 0, -1, -1 );
|
2019-11-26 16:56:45 +01:00
|
|
|
painter->setBrush( QPalette().window().color().lighter( 102 ) );
|
2015-12-16 18:22:36 +01:00
|
|
|
painter->setPen( Qt::NoPen );
|
|
|
|
painter->drawRoundedRect( hoverRect, CORNER_RADIUS, CORNER_RADIUS );
|
|
|
|
|
|
|
|
painter->translate( -0.5, -0.5 );
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this element the selected one?
|
2020-08-22 01:19:58 +02:00
|
|
|
bool sel = selectionMode() != QAbstractItemView::NoSelection && index.isValid() && selectionModel()
|
|
|
|
&& !selectionModel()->selectedIndexes().isEmpty() && selectionModel()->selectedIndexes().first() == index;
|
2015-12-16 18:22:36 +01:00
|
|
|
|
|
|
|
drawLabel( painter, texts, labelColor, QPoint( label_x, label_y ), sel );
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
label_x += labelSize.width() + LABELS_MARGIN;
|
|
|
|
}
|
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( !modl->rowCount() && !modl->device()->partitionTable() ) // No disklabel or unknown
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
|
|
|
QStringList texts = buildUnknownDisklabelTexts( modl->device() );
|
|
|
|
QColor labelColor = ColorUtils::unknownDisklabelColor();
|
2015-12-16 18:22:36 +01:00
|
|
|
drawLabel( painter, texts, labelColor, QPoint( rect.x(), rect.y() ), false /*can't be selected*/ );
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSize
|
|
|
|
PartitionLabelsView::sizeForAllLabels( int maxLineWidth ) const
|
|
|
|
{
|
|
|
|
PartitionModel* modl = qobject_cast< PartitionModel* >( model() );
|
|
|
|
if ( !modl )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-03 19:38:56 +01:00
|
|
|
return QSize();
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-03 19:38:56 +01:00
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
int lineLength = 0;
|
|
|
|
int numLines = 1;
|
|
|
|
int singleLabelHeight = 0;
|
2016-09-01 14:21:05 +02:00
|
|
|
for ( const QModelIndex& index : indexesToDraw )
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
2015-12-15 16:56:10 +01:00
|
|
|
QStringList texts = buildTexts( index );
|
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
QSize labelSize = sizeForLabel( texts );
|
|
|
|
|
|
|
|
if ( lineLength + labelSize.width() > maxLineWidth )
|
|
|
|
{
|
|
|
|
numLines++;
|
|
|
|
lineLength = labelSize.width();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lineLength += LABELS_MARGIN + labelSize.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
singleLabelHeight = qMax( singleLabelHeight, labelSize.height() );
|
|
|
|
}
|
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( !modl->rowCount() && !modl->device()->partitionTable() ) // Unknown or no disklabel
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
2020-08-22 01:19:58 +02:00
|
|
|
singleLabelHeight = sizeForLabel( buildUnknownDisklabelTexts( modl->device() ) ).height();
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
int totalHeight = numLines * singleLabelHeight + ( numLines - 1 ) * singleLabelHeight / 4; //spacings
|
2015-12-03 19:38:56 +01:00
|
|
|
|
|
|
|
return QSize( maxLineWidth, totalHeight );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSize
|
|
|
|
PartitionLabelsView::sizeForLabel( const QStringList& text ) const
|
|
|
|
{
|
|
|
|
int vertOffset = 0;
|
|
|
|
int width = 0;
|
2016-09-01 14:21:05 +02:00
|
|
|
for ( const QString& textLine : text )
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
|
|
|
QSize textSize = fontMetrics().size( Qt::TextSingleLine, textLine );
|
|
|
|
|
|
|
|
vertOffset += textSize.height();
|
|
|
|
width = qMax( width, textSize.width() );
|
|
|
|
}
|
2020-08-22 01:19:58 +02:00
|
|
|
width += LABEL_PARTITION_SQUARE_MARGIN; //for the color square
|
2015-12-03 19:38:56 +01:00
|
|
|
return QSize( width, vertOffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionLabelsView::drawLabel( QPainter* painter,
|
|
|
|
const QStringList& text,
|
|
|
|
const QColor& color,
|
2015-12-16 18:22:36 +01:00
|
|
|
const QPoint& pos,
|
|
|
|
bool selected )
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
|
|
|
painter->setPen( Qt::black );
|
|
|
|
int vertOffset = 0;
|
|
|
|
int width = 0;
|
2016-09-01 14:21:05 +02:00
|
|
|
for ( const QString& textLine : text )
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
|
|
|
QSize textSize = painter->fontMetrics().size( Qt::TextSingleLine, textLine );
|
2020-08-22 01:19:58 +02:00
|
|
|
painter->drawText(
|
|
|
|
pos.x() + LABEL_PARTITION_SQUARE_MARGIN, pos.y() + vertOffset + textSize.height() / 2, textLine );
|
2015-12-03 19:38:56 +01:00
|
|
|
vertOffset += textSize.height();
|
|
|
|
painter->setPen( Qt::gray );
|
|
|
|
width = qMax( width, textSize.width() );
|
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
QRect partitionSquareRect(
|
|
|
|
pos.x(), pos.y() - 3, LABEL_PARTITION_SQUARE_MARGIN - 5, LABEL_PARTITION_SQUARE_MARGIN - 5 );
|
2015-12-16 18:22:36 +01:00
|
|
|
drawPartitionSquare( painter, partitionSquareRect, color );
|
|
|
|
|
|
|
|
if ( selected )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
drawSelectionSquare( painter, partitionSquareRect.adjusted( 2, 2, -2, -2 ), color );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
painter->setPen( Qt::black );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
PartitionLabelsView::indexAt( const QPoint& point ) const
|
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
PartitionModel* modl = qobject_cast< PartitionModel* >( model() );
|
|
|
|
if ( !modl )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
return QModelIndex();
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
|
2015-12-16 18:22:36 +01:00
|
|
|
|
|
|
|
QRect rect = this->rect();
|
|
|
|
int label_x = rect.x();
|
|
|
|
int label_y = rect.y();
|
2016-09-01 14:21:05 +02:00
|
|
|
for ( const QModelIndex& index : indexesToDraw )
|
2015-12-16 18:22:36 +01:00
|
|
|
{
|
|
|
|
QStringList texts = buildTexts( index );
|
|
|
|
|
|
|
|
QSize labelSize = sizeForLabel( texts );
|
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( label_x + labelSize.width() > rect.width() ) //wrap to new line if overflow
|
2015-12-16 18:22:36 +01:00
|
|
|
{
|
|
|
|
label_x = rect.x();
|
|
|
|
label_y += labelSize.height() + labelSize.height() / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect labelRect( QPoint( label_x, label_y ), labelSize );
|
|
|
|
if ( labelRect.contains( point ) )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
return index;
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
|
|
|
|
label_x += labelSize.width() + LABELS_MARGIN;
|
|
|
|
}
|
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QRect
|
2015-12-16 18:22:36 +01:00
|
|
|
PartitionLabelsView::visualRect( const QModelIndex& idx ) const
|
2015-12-03 19:38:56 +01:00
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
PartitionModel* modl = qobject_cast< PartitionModel* >( model() );
|
|
|
|
if ( !modl )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
return QRect();
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
|
2015-12-16 18:22:36 +01:00
|
|
|
|
|
|
|
QRect rect = this->rect();
|
|
|
|
int label_x = rect.x();
|
|
|
|
int label_y = rect.y();
|
2016-09-01 14:21:05 +02:00
|
|
|
for ( const QModelIndex& index : indexesToDraw )
|
2015-12-16 18:22:36 +01:00
|
|
|
{
|
|
|
|
QStringList texts = buildTexts( index );
|
|
|
|
|
|
|
|
QSize labelSize = sizeForLabel( texts );
|
|
|
|
|
2020-08-22 01:19:58 +02:00
|
|
|
if ( label_x + labelSize.width() > rect.width() ) //wrap to new line if overflow
|
2015-12-16 18:22:36 +01:00
|
|
|
{
|
|
|
|
label_x = rect.x();
|
|
|
|
label_y += labelSize.height() + labelSize.height() / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( idx.isValid() && idx == index )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
return QRect( QPoint( label_x, label_y ), labelSize );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
|
|
|
|
label_x += labelSize.width() + LABELS_MARGIN;
|
|
|
|
}
|
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
return QRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QRegion
|
|
|
|
PartitionLabelsView::visualRegionForSelection( const QItemSelection& selection ) const
|
|
|
|
{
|
2019-04-17 11:57:46 +02:00
|
|
|
Q_UNUSED( selection )
|
2017-09-10 21:17:33 +02:00
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
return QRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PartitionLabelsView::horizontalOffset() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PartitionLabelsView::verticalOffset() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionLabelsView::scrollTo( const QModelIndex& index, ScrollHint hint )
|
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
Q_UNUSED( index )
|
|
|
|
Q_UNUSED( hint )
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-16 11:13:03 +01:00
|
|
|
void
|
|
|
|
PartitionLabelsView::setCustomNewRootLabel( const QString& text )
|
|
|
|
{
|
|
|
|
m_customNewRootLabel = text;
|
|
|
|
viewport()->repaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-13 14:50:08 +01:00
|
|
|
void
|
|
|
|
PartitionLabelsView::setSelectionModel( QItemSelectionModel* selectionModel )
|
|
|
|
{
|
2016-01-13 15:07:11 +01:00
|
|
|
QAbstractItemView::setSelectionModel( selectionModel );
|
2020-08-22 01:19:58 +02:00
|
|
|
connect( selectionModel, &QItemSelectionModel::selectionChanged, this, [=] { viewport()->repaint(); } );
|
2016-01-13 14:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-15 18:03:48 +01:00
|
|
|
void
|
|
|
|
PartitionLabelsView::setSelectionFilter( SelectionFilter canBeSelected )
|
|
|
|
{
|
2017-09-10 21:17:33 +02:00
|
|
|
m_canBeSelected = canBeSelected;
|
2016-01-15 18:03:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-11 16:00:07 +01:00
|
|
|
void
|
|
|
|
PartitionLabelsView::setExtendedPartitionHidden( bool hidden )
|
|
|
|
{
|
|
|
|
m_extendedPartitionHidden = hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
QModelIndex
|
|
|
|
PartitionLabelsView::moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers )
|
|
|
|
{
|
2019-04-17 11:57:46 +02:00
|
|
|
Q_UNUSED( cursorAction )
|
|
|
|
Q_UNUSED( modifiers )
|
2017-09-10 21:17:33 +02:00
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
PartitionLabelsView::isIndexHidden( const QModelIndex& index ) const
|
|
|
|
{
|
2019-04-17 11:57:46 +02:00
|
|
|
Q_UNUSED( index )
|
2017-09-10 21:17:33 +02:00
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionLabelsView::setSelection( const QRect& rect, QItemSelectionModel::SelectionFlags flags )
|
|
|
|
{
|
2016-01-15 16:55:23 +01:00
|
|
|
QModelIndex eventIndex = indexAt( rect.topLeft() );
|
2017-09-10 21:17:33 +02:00
|
|
|
if ( m_canBeSelected( eventIndex ) )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2016-01-15 16:55:23 +01:00
|
|
|
selectionModel()->select( eventIndex, flags );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionLabelsView::mouseMoveEvent( QMouseEvent* event )
|
|
|
|
{
|
|
|
|
QModelIndex candidateIndex = indexAt( event->pos() );
|
|
|
|
QPersistentModelIndex oldHoveredIndex = m_hoveredIndex;
|
|
|
|
if ( candidateIndex.isValid() )
|
|
|
|
{
|
|
|
|
m_hoveredIndex = candidateIndex;
|
|
|
|
}
|
|
|
|
else
|
2016-02-16 14:50:01 +01:00
|
|
|
{
|
2015-12-16 18:22:36 +01:00
|
|
|
m_hoveredIndex = QModelIndex();
|
2016-02-16 14:45:50 +01:00
|
|
|
QGuiApplication::restoreOverrideCursor();
|
2016-02-16 14:50:01 +01:00
|
|
|
}
|
2016-02-16 14:45:50 +01:00
|
|
|
|
2016-02-16 14:50:01 +01:00
|
|
|
if ( oldHoveredIndex != m_hoveredIndex )
|
|
|
|
{
|
2017-09-10 21:17:33 +02:00
|
|
|
if ( m_hoveredIndex.isValid() && !m_canBeSelected( m_hoveredIndex ) )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2016-02-16 14:50:01 +01:00
|
|
|
QGuiApplication::setOverrideCursor( Qt::ForbiddenCursor );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2016-02-16 14:50:01 +01:00
|
|
|
else
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2016-02-16 14:50:01 +01:00
|
|
|
QGuiApplication::restoreOverrideCursor();
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2016-02-16 14:50:01 +01:00
|
|
|
|
|
|
|
viewport()->repaint();
|
|
|
|
}
|
2015-12-16 18:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PartitionLabelsView::leaveEvent( QEvent* event )
|
|
|
|
{
|
2019-04-17 11:57:46 +02:00
|
|
|
Q_UNUSED( event )
|
2017-09-10 21:17:33 +02:00
|
|
|
|
2016-02-16 14:45:50 +01:00
|
|
|
QGuiApplication::restoreOverrideCursor();
|
2015-12-16 18:22:36 +01:00
|
|
|
if ( m_hoveredIndex.isValid() )
|
|
|
|
{
|
|
|
|
m_hoveredIndex = QModelIndex();
|
|
|
|
viewport()->repaint();
|
|
|
|
}
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-15 18:31:57 +01:00
|
|
|
void
|
|
|
|
PartitionLabelsView::mousePressEvent( QMouseEvent* event )
|
|
|
|
{
|
|
|
|
QModelIndex candidateIndex = indexAt( event->pos() );
|
2017-09-10 21:17:33 +02:00
|
|
|
if ( m_canBeSelected( candidateIndex ) )
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2016-01-15 18:31:57 +01:00
|
|
|
QAbstractItemView::mousePressEvent( event );
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2016-01-15 18:31:57 +01:00
|
|
|
else
|
2020-08-22 01:19:58 +02:00
|
|
|
{
|
2016-01-15 18:31:57 +01:00
|
|
|
event->accept();
|
2020-08-22 01:19:58 +02:00
|
|
|
}
|
2016-01-15 18:31:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-03 19:38:56 +01:00
|
|
|
void
|
|
|
|
PartitionLabelsView::updateGeometries()
|
|
|
|
{
|
2020-08-22 01:19:58 +02:00
|
|
|
updateGeometry(); //get a new rect() for redrawing all the labels
|
2015-12-03 19:38:56 +01:00
|
|
|
}
|