From c254fe0b2aa9aa7ca9cd444fb91681b1d7654976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Mon, 30 Jun 2014 15:04:10 +0200 Subject: [PATCH] Enable/disable partition buttons --- src/modules/partition/PartitionPage.cpp | 30 +++++++++++++++++++++++++ src/modules/partition/PartitionPage.h | 1 + 2 files changed, 31 insertions(+) diff --git a/src/modules/partition/PartitionPage.cpp b/src/modules/partition/PartitionPage.cpp index 2f3883ae6..4dfe074eb 100644 --- a/src/modules/partition/PartitionPage.cpp +++ b/src/modules/partition/PartitionPage.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include // Qt @@ -35,6 +36,7 @@ PartitionPage::PartitionPage( QWidget* parent ) { m_ui->setupUi( this ); m_ui->deviceListView->setModel( m_core->deviceModel() ); + updateButtons(); connect( m_ui->deviceListView->selectionModel(), &QItemSelectionModel::currentChanged, [ this ]( const QModelIndex& index, const QModelIndex& oldIndex ) @@ -42,9 +44,37 @@ PartitionPage::PartitionPage( QWidget* parent ) Device* device = m_core->deviceModel()->deviceForIndex( index ); PartitionModel* model = m_core->partitionModelForDevice( device ); m_ui->partitionListView->setModel( model ); + updateButtons(); + // Establish connection here because selection model is destroyed when + // model changes + connect( m_ui->partitionListView->selectionModel(), &QItemSelectionModel::currentChanged, + [ this ]( const QModelIndex& index, const QModelIndex& oldIndex ) + { + updateButtons(); + } ); } ); } PartitionPage::~PartitionPage() { } + +void PartitionPage::updateButtons() +{ + bool create = false, edit = false, del = false; + + QModelIndex index = m_ui->partitionListView->currentIndex(); + if ( index.isValid() ) + { + const PartitionModel* model = static_cast< const PartitionModel* >( index.model() ); + Q_ASSERT( model ); + Partition* partition = model->partitionForIndex( index ); + Q_ASSERT( partition ); + bool isFree = PMUtils::isPartitionFreeSpace( partition ); + create = isFree; + edit = del = !isFree; + } + m_ui->createButton->setEnabled( create ); + m_ui->editButton->setEnabled( edit ); + m_ui->deleteButton->setEnabled( del ); +} diff --git a/src/modules/partition/PartitionPage.h b/src/modules/partition/PartitionPage.h index ce7023398..7ecbab28c 100644 --- a/src/modules/partition/PartitionPage.h +++ b/src/modules/partition/PartitionPage.h @@ -42,6 +42,7 @@ public Q_SLOTS: private: QScopedPointer< Ui_PartitionPage > m_ui; PartitionCoreModule* m_core; + void updateButtons(); }; #endif // PARTITIONPAGE_H