[partition] Improve EncryptionWidget

- Use normal translation framework. The EncryptWidget was the one place
  not using the "usual" translation framework, but rolled its own.
- Emphasize that the checkbox-state (checked-ness) is the parameter,
  not a state of the EncryptWidget.
- All other instances of UI classes from Designer use a pointer-to-UI,
  not multiple inheritance.
- Convenience method for setting the pixmap in response to
  changes in the passphrase
- Tighten up types: enum -> enum class
- Reduce the scope for int-confusion by using an enum-class for
  the encryption state of the widget
- Include UI implementation header only in .cpp
- Apply coding style
- Update copyright
main
Adriaan de Groot 5 years ago
parent 1299c64415
commit 220fd31226

@ -587,19 +587,19 @@ ChoicePage::doAlongsideSetupSplitter( const QModelIndex& current,
void void
ChoicePage::onEncryptWidgetStateChanged() ChoicePage::onEncryptWidgetStateChanged()
{ {
EncryptWidget::State state = m_encryptWidget->state(); EncryptWidget::Encryption state = m_encryptWidget->state();
if ( m_choice == Erase ) if ( m_choice == Erase )
{ {
if ( state == EncryptWidget::EncryptionConfirmed || if ( state == EncryptWidget::Encryption::Confirmed ||
state == EncryptWidget::EncryptionDisabled ) state == EncryptWidget::Encryption::Disabled )
applyActionChoice( m_choice ); applyActionChoice( m_choice );
} }
else if ( m_choice == Replace ) else if ( m_choice == Replace )
{ {
if ( m_beforePartitionBarsView && if ( m_beforePartitionBarsView &&
m_beforePartitionBarsView->selectionModel()->currentIndex().isValid() && m_beforePartitionBarsView->selectionModel()->currentIndex().isValid() &&
( state == EncryptWidget::EncryptionConfirmed || ( state == EncryptWidget::Encryption::Confirmed ||
state == EncryptWidget::EncryptionDisabled ) ) state == EncryptWidget::Encryption::Disabled ) )
{ {
doReplaceSelectedPartition( m_beforePartitionBarsView-> doReplaceSelectedPartition( m_beforePartitionBarsView->
selectionModel()-> selectionModel()->
@ -1474,7 +1474,7 @@ ChoicePage::updateNextEnabled()
if ( m_choice != Manual && if ( m_choice != Manual &&
m_encryptWidget->isVisible() && m_encryptWidget->isVisible() &&
m_encryptWidget->state() == EncryptWidget::EncryptionUnconfirmed ) m_encryptWidget->state() == EncryptWidget::Encryption::Unconfirmed )
enabled = false; enabled = false;
if ( enabled == m_nextEnabled ) if ( enabled == m_nextEnabled )

@ -204,7 +204,7 @@ CreatePartitionDialog::createPartition()
Partition* partition = nullptr; Partition* partition = nullptr;
QString luksPassphrase = m_ui->encryptWidget->passphrase(); QString luksPassphrase = m_ui->encryptWidget->passphrase();
if ( m_ui->encryptWidget->state() == EncryptWidget::EncryptionConfirmed && if ( m_ui->encryptWidget->state() == EncryptWidget::Encryption::Confirmed &&
!luksPassphrase.isEmpty() ) !luksPassphrase.isEmpty() )
{ {
partition = KPMHelpers::createNewEncryptedPartition( partition = KPMHelpers::createNewEncryptedPartition(

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright 2016, Teo Mrnjavac <teo@kde.org> * Copyright 2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2020, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -19,42 +20,45 @@
#include "EncryptWidget.h" #include "EncryptWidget.h"
#include "ui_EncryptWidget.h"
#include "utils/CalamaresUtilsGui.h" #include "utils/CalamaresUtilsGui.h"
#include "utils/Retranslator.h"
EncryptWidget::EncryptWidget( QWidget* parent ) EncryptWidget::EncryptWidget( QWidget* parent )
: QWidget( parent ) : QWidget( parent )
, m_state( EncryptionDisabled ) , m_ui( new Ui::EncryptWidget )
, m_state( Encryption::Disabled )
{ {
setupUi( this ); m_ui->setupUi( this );
m_iconLabel->setFixedWidth( m_iconLabel->height() ); m_ui->m_iconLabel->setFixedWidth( m_ui->m_iconLabel->height() );
m_passphraseLineEdit->hide(); m_ui->m_passphraseLineEdit->hide();
m_confirmLineEdit->hide(); m_ui->m_confirmLineEdit->hide();
m_iconLabel->hide(); m_ui->m_iconLabel->hide();
connect( m_encryptCheckBox, &QCheckBox::stateChanged, connect( m_ui->m_encryptCheckBox, &QCheckBox::stateChanged, this, &EncryptWidget::onCheckBoxStateChanged );
this, &EncryptWidget::onCheckBoxStateChanged ); connect( m_ui->m_passphraseLineEdit, &QLineEdit::textEdited, this, &EncryptWidget::onPassphraseEdited );
connect( m_passphraseLineEdit, &QLineEdit::textEdited, connect( m_ui->m_confirmLineEdit, &QLineEdit::textEdited, this, &EncryptWidget::onPassphraseEdited );
this, &EncryptWidget::onPassphraseEdited );
connect( m_confirmLineEdit, &QLineEdit::textEdited,
this, &EncryptWidget::onPassphraseEdited );
setFixedHeight( m_passphraseLineEdit->height() ); // Avoid jumping up and down setFixedHeight( m_ui->m_passphraseLineEdit->height() ); // Avoid jumping up and down
updateState(); updateState();
CALAMARES_RETRANSLATE_SLOT( &EncryptWidget::retranslate )
} }
void void
EncryptWidget::reset() EncryptWidget::reset()
{ {
m_passphraseLineEdit->clear(); m_ui->m_passphraseLineEdit->clear();
m_confirmLineEdit->clear(); m_ui->m_confirmLineEdit->clear();
m_encryptCheckBox->setChecked( false ); m_ui->m_encryptCheckBox->setChecked( false );
} }
EncryptWidget::State EncryptWidget::Encryption
EncryptWidget::state() const EncryptWidget::state() const
{ {
return m_state; return m_state;
@ -64,53 +68,48 @@ EncryptWidget::state() const
void void
EncryptWidget::setText( const QString& text ) EncryptWidget::setText( const QString& text )
{ {
m_encryptCheckBox->setText( text ); m_ui->m_encryptCheckBox->setText( text );
} }
QString QString
EncryptWidget::passphrase() const EncryptWidget::passphrase() const
{ {
if ( m_state == EncryptionConfirmed ) if ( m_state == Encryption::Confirmed )
return m_passphraseLineEdit->text(); {
return m_ui->m_passphraseLineEdit->text();
}
return QString(); return QString();
} }
void void
EncryptWidget::changeEvent( QEvent* e ) EncryptWidget::retranslate()
{ {
QWidget::changeEvent( e ); m_ui->retranslateUi( this );
switch ( e->type() ) onPassphraseEdited(); // For the tooltip
{
case QEvent::LanguageChange:
retranslateUi( this );
break;
default:
break;
}
} }
void void
EncryptWidget::updateState() EncryptWidget::updateState()
{ {
State newState; Encryption newState;
if ( m_encryptCheckBox->isChecked() ) if ( m_ui->m_encryptCheckBox->isChecked() )
{ {
if ( !m_passphraseLineEdit->text().isEmpty() && if ( !m_ui->m_passphraseLineEdit->text().isEmpty()
m_passphraseLineEdit->text() == m_confirmLineEdit->text() ) && m_ui->m_passphraseLineEdit->text() == m_ui->m_confirmLineEdit->text() )
{ {
newState = EncryptionConfirmed; newState = Encryption::Confirmed;
} }
else else
{ {
newState = EncryptionUnconfirmed; newState = Encryption::Unconfirmed;
} }
} }
else else
{ {
newState = EncryptionDisabled; newState = Encryption::Disabled;
} }
if ( newState != m_state ) if ( newState != m_state )
@ -120,35 +119,38 @@ EncryptWidget::updateState()
} }
} }
///@brief Give @p label the @p pixmap from the standard-pixmaps
static void
applyPixmap( QLabel* label, CalamaresUtils::ImageType pixmap )
{
label->setFixedWidth( label->height() );
label->setPixmap( CalamaresUtils::defaultPixmap( pixmap, CalamaresUtils::Original, label->size() ) );
}
void void
EncryptWidget::onPassphraseEdited() EncryptWidget::onPassphraseEdited()
{ {
if ( !m_iconLabel->isVisible() ) if ( !m_ui->m_iconLabel->isVisible() )
m_iconLabel->show(); {
m_ui->m_iconLabel->show();
}
QString p1 = m_passphraseLineEdit->text(); QString p1 = m_ui->m_passphraseLineEdit->text();
QString p2 = m_confirmLineEdit->text(); QString p2 = m_ui->m_confirmLineEdit->text();
m_iconLabel->setToolTip( QString() ); m_ui->m_iconLabel->setToolTip( QString() );
if ( p1.isEmpty() && p2.isEmpty() ) if ( p1.isEmpty() && p2.isEmpty() )
{ {
m_iconLabel->clear(); m_ui->m_iconLabel->clear();
} }
else if ( p1 == p2 ) else if ( p1 == p2 )
{ {
m_iconLabel->setFixedWidth( m_iconLabel->height() ); applyPixmap( m_ui->m_iconLabel, CalamaresUtils::Yes );
m_iconLabel->setPixmap( CalamaresUtils::defaultPixmap( CalamaresUtils::Yes,
CalamaresUtils::Original,
m_iconLabel->size() ) );
} }
else else
{ {
m_iconLabel->setFixedWidth( m_iconLabel->height() ); applyPixmap( m_ui->m_iconLabel, CalamaresUtils::No );
m_iconLabel->setPixmap( CalamaresUtils::defaultPixmap( CalamaresUtils::No, m_ui->m_iconLabel->setToolTip( tr( "Please enter the same passphrase in both boxes." ) );
CalamaresUtils::Original,
m_iconLabel->size() ) );
m_iconLabel->setToolTip( tr( "Please enter the same passphrase in both boxes." ) );
} }
updateState(); updateState();
@ -156,14 +158,15 @@ EncryptWidget::onPassphraseEdited()
void void
EncryptWidget::onCheckBoxStateChanged( int state ) EncryptWidget::onCheckBoxStateChanged( int checked )
{ {
m_passphraseLineEdit->setVisible( state ); // @p checked is a Qt::CheckState, 0 is "unchecked" and 2 is "checked"
m_confirmLineEdit->setVisible( state ); m_ui->m_passphraseLineEdit->setVisible( checked );
m_iconLabel->setVisible( state ); m_ui->m_confirmLineEdit->setVisible( checked );
m_passphraseLineEdit->clear(); m_ui->m_iconLabel->setVisible( checked );
m_confirmLineEdit->clear(); m_ui->m_passphraseLineEdit->clear();
m_iconLabel->clear(); m_ui->m_confirmLineEdit->clear();
m_ui->m_iconLabel->clear();
updateState(); updateState();
} }

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright 2016, Teo Mrnjavac <teo@kde.org> * Copyright 2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2020, Adriaan de Groot <groot@kde.org>
* *
* Calamares is free software: you can redistribute it and/or modify * Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -20,41 +21,46 @@
#ifndef ENCRYPTWIDGET_H #ifndef ENCRYPTWIDGET_H
#define ENCRYPTWIDGET_H #define ENCRYPTWIDGET_H
#include "ui_EncryptWidget.h" #include <QWidget>
class EncryptWidget : public QWidget, private Ui::EncryptWidget namespace Ui
{
class EncryptWidget;
}
class EncryptWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
enum State : unsigned short enum class Encryption : unsigned short
{ {
EncryptionDisabled = 0, Disabled = 0,
EncryptionUnconfirmed, Unconfirmed,
EncryptionConfirmed Confirmed
}; };
explicit EncryptWidget( QWidget* parent = nullptr ); explicit EncryptWidget( QWidget* parent = nullptr );
void reset(); void reset();
State state() const; Encryption state() const;
void setText( const QString& text ); void setText( const QString& text );
QString passphrase() const; QString passphrase() const;
signals: void retranslate();
void stateChanged( State );
protected: signals:
void changeEvent( QEvent* e ); void stateChanged( Encryption );
private: private:
void updateState(); void updateState();
void onPassphraseEdited(); void onPassphraseEdited();
void onCheckBoxStateChanged( int state ); void onCheckBoxStateChanged( int checked );
State m_state; Ui::EncryptWidget* m_ui;
Encryption m_state;
}; };
#endif // ENCRYPTWIDGET_H #endif // ENCRYPTWIDGET_H

Loading…
Cancel
Save