[libcalamaresui] Polish on LogWidget

- apply coding style
- reduce shadowed variables
- use Q_EMIT to mark signals
This commit is contained in:
Adriaan de Groot 2022-03-14 16:14:40 +01:00
parent d89955f4b3
commit 1a6fb1c3d2
3 changed files with 56 additions and 54 deletions

View File

@ -27,17 +27,17 @@
#include "utils/Retranslator.h" #include "utils/Retranslator.h"
#include "widgets/LogWidget.h" #include "widgets/LogWidget.h"
#include <QDir>
#include <QLabel>
#include <QProgressBar>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QToolBar>
#include <QAction> #include <QAction>
#include <QToolButton> #include <QDir>
#include <QTabWidget> #include <QHBoxLayout>
#include <QLabel>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QProgressBar>
#include <QTabBar> #include <QTabBar>
#include <QTabWidget>
#include <QToolBar>
#include <QToolButton>
#include <QVBoxLayout>
static Calamares::Slideshow* static Calamares::Slideshow*
makeSlideshow( QWidget* parent ) makeSlideshow( QWidget* parent )
@ -81,8 +81,8 @@ ExecutionViewStep::ExecutionViewStep( QObject* parent )
m_progressBar->setMaximum( 10000 ); m_progressBar->setMaximum( 10000 );
m_tab_widget->addTab(m_slideshow->widget(), "Slideshow"); m_tab_widget->addTab( m_slideshow->widget(), "Slideshow" );
m_tab_widget->addTab(m_log_widget, "Log"); m_tab_widget->addTab( m_log_widget, "Log" );
m_tab_widget->tabBar()->hide(); m_tab_widget->tabBar()->hide();
layout->addWidget( m_tab_widget ); layout->addWidget( m_tab_widget );
@ -94,13 +94,13 @@ ExecutionViewStep::ExecutionViewStep( QObject* parent )
bottomLayout->addWidget( m_label ); bottomLayout->addWidget( m_label );
QToolBar* toolBar = new QToolBar; QToolBar* toolBar = new QToolBar;
auto toggleLogAction = toolBar->addAction(QIcon::fromTheme("utilities-terminal"), "Toggle log"); auto toggleLogAction = toolBar->addAction( QIcon::fromTheme( "utilities-terminal" ), "Toggle log" );
auto toggleLogButton = dynamic_cast<QToolButton*>(toolBar->widgetForAction(toggleLogAction)); auto toggleLogButton = dynamic_cast< QToolButton* >( toolBar->widgetForAction( toggleLogAction ) );
connect( toggleLogButton, &QToolButton::clicked, this, &ExecutionViewStep::toggleLog ); connect( toggleLogButton, &QToolButton::clicked, this, &ExecutionViewStep::toggleLog );
barLayout->addWidget(m_progressBar); barLayout->addWidget( m_progressBar );
barLayout->addWidget(toolBar); barLayout->addWidget( toolBar );
connect( JobQueue::instance(), &JobQueue::progress, this, &ExecutionViewStep::updateFromJobQueue ); connect( JobQueue::instance(), &JobQueue::progress, this, &ExecutionViewStep::updateFromJobQueue );
@ -176,7 +176,7 @@ ExecutionViewStep::onActivate()
const auto instanceDescriptor const auto instanceDescriptor
= std::find_if( instanceDescriptors.constBegin(), = std::find_if( instanceDescriptors.constBegin(),
instanceDescriptors.constEnd(), instanceDescriptors.constEnd(),
[=]( const Calamares::InstanceDescription& d ) { return d.key() == instanceKey; } ); [ = ]( const Calamares::InstanceDescription& d ) { return d.key() == instanceKey; } );
int weight = moduleDescriptor.weight(); int weight = moduleDescriptor.weight();
if ( instanceDescriptor != instanceDescriptors.constEnd() && instanceDescriptor->explicitWeight() ) if ( instanceDescriptor != instanceDescriptors.constEnd() && instanceDescriptor->explicitWeight() )
{ {
@ -228,7 +228,7 @@ ExecutionViewStep::updateFromJobQueue( qreal percent, const QString& message )
void void
ExecutionViewStep::toggleLog() ExecutionViewStep::toggleLog()
{ {
m_tab_widget->setCurrentIndex((m_tab_widget->currentIndex() + 1) % m_tab_widget->count()); m_tab_widget->setCurrentIndex( ( m_tab_widget->currentIndex() + 1 ) % m_tab_widget->count() );
} }
void void
@ -239,4 +239,3 @@ ExecutionViewStep::onLeave()
} // namespace Calamares } // namespace Calamares

View File

@ -1,16 +1,16 @@
#include "LogWidget.h" #include "LogWidget.h"
#include <QStackedLayout>
#include "utils/Logger.h" #include "utils/Logger.h"
#include <QFile>
#include <QStackedLayout>
#include <QTextStream> #include <QTextStream>
#include <QThread> #include <QThread>
#include <QFile>
namespace Calamares namespace Calamares
{ {
LogThread::LogThread(QObject *parent) LogThread::LogThread( QObject* parent )
: QThread(parent) { : QThread( parent )
{
} }
LogThread::~LogThread() LogThread::~LogThread()
@ -20,63 +20,65 @@ LogThread::~LogThread()
wait(); wait();
} }
void LogThread::run() void
LogThread::run()
{ {
auto filePath = Logger::logFile(); const auto filePath = Logger::logFile();
qint64 lastPosition = 0; qint64 lastPosition = 0;
while (!QThread::currentThread()->isInterruptionRequested()) { while ( !QThread::currentThread()->isInterruptionRequested() )
auto filePath = Logger::logFile(); {
QFile file(filePath); QFile file( filePath );
qint64 fileSize = file.size(); qint64 fileSize = file.size();
// Check whether the file size has changed since last time // Check whether the file size has changed since last time
// we read the file. // we read the file.
if (lastPosition != fileSize && file.open(QFile::ReadOnly | QFile::Text)) { if ( lastPosition != fileSize && file.open( QFile::ReadOnly | QFile::Text ) )
{
// Start reading at the position we ended up last time we read the file. // Start reading at the position we ended up last time we read the file.
file.seek(lastPosition); file.seek( lastPosition );
QTextStream in(&file); QTextStream in( &file );
auto chunk = in.readAll(); auto chunk = in.readAll();
qint64 newPosition = in.pos(); qint64 newPosition = in.pos();
lastPosition = newPosition; lastPosition = newPosition;
onLogChunk(chunk); Q_EMIT onLogChunk( chunk );
} }
QThread::msleep(100); QThread::msleep( 100 );
} }
} }
LogWidget::LogWidget(QWidget *parent) LogWidget::LogWidget( QWidget* parent )
: QWidget(parent) : QWidget( parent )
, m_text( new QPlainTextEdit ) , m_text( new QPlainTextEdit )
, m_log_thread( this ) , m_log_thread( this )
{ {
auto layout = new QStackedLayout(this); auto layout = new QStackedLayout( this );
setLayout(layout); setLayout( layout );
m_text->setReadOnly(true); m_text->setReadOnly( true );
QFont monospaceFont("monospace"); QFont monospaceFont( "monospace" );
monospaceFont.setStyleHint(QFont::Monospace); monospaceFont.setStyleHint( QFont::Monospace );
m_text->setFont(monospaceFont); m_text->setFont( monospaceFont );
layout->addWidget(m_text); layout->addWidget( m_text );
connect(&m_log_thread, &LogThread::onLogChunk, this, &LogWidget::handleLogChunk); connect( &m_log_thread, &LogThread::onLogChunk, this, &LogWidget::handleLogChunk );
m_log_thread.start(); m_log_thread.start();
} }
void void
LogWidget::handleLogChunk(const QString &logChunk) LogWidget::handleLogChunk( const QString& logChunk )
{ {
m_text->appendPlainText(logChunk); m_text->appendPlainText( logChunk );
m_text->moveCursor(QTextCursor::End); m_text->moveCursor( QTextCursor::End );
m_text->ensureCursorVisible(); m_text->ensureCursorVisible();
} }
} } // namespace Calamares

View File

@ -1,9 +1,9 @@
#ifndef LIBCALAMARESUI_LOGWIDGET_H #ifndef LIBCALAMARESUI_LOGWIDGET_H
#define LIBCALAMARESUI_LOGWIDGET_H #define LIBCALAMARESUI_LOGWIDGET_H
#include <QWidget>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QThread> #include <QThread>
#include <QWidget>
namespace Calamares namespace Calamares
{ {
@ -15,11 +15,11 @@ class LogThread : public QThread
void run() override; void run() override;
public: public:
explicit LogThread(QObject *parent = nullptr); explicit LogThread( QObject* parent = nullptr );
~LogThread() override; ~LogThread() override;
signals: signals:
void onLogChunk(const QString &logChunk); void onLogChunk( const QString& logChunk );
}; };
class LogWidget : public QWidget class LogWidget : public QWidget
@ -28,11 +28,12 @@ class LogWidget : public QWidget
QPlainTextEdit* m_text; QPlainTextEdit* m_text;
LogThread m_log_thread; LogThread m_log_thread;
public:
explicit LogWidget(QWidget *parent = nullptr);
void handleLogChunk(const QString &logChunk); public:
explicit LogWidget( QWidget* parent = nullptr );
void handleLogChunk( const QString& logChunk );
}; };
} } // namespace Calamares
#endif // LOGWIDGET_H #endif // LOGWIDGET_H