LogDoctor/craplog/mainwindow.cpp

237 lines
7.5 KiB
C++
Raw Normal View History

2022-06-21 21:07:06 +02:00
#include "mainwindow.h"
#include "./ui_mainwindow.h"
2022-06-20 21:44:58 +02:00
#include <iostream>
2022-06-21 21:07:06 +02:00
MainWindow::MainWindow(QWidget *parent)
2022-06-20 21:44:58 +02:00
: QMainWindow(parent)
2022-06-21 21:07:06 +02:00
, ui(new Ui::MainWindow)
2022-06-20 21:44:58 +02:00
{
this->ui->setupUi(this);
2022-06-24 01:26:00 +02:00
// initialize the colors map
this->COLORS["black"] = QColor(0,0,0,255);
this->COLORS["grey"] = QColor(127,127,127,255);
this->COLORS["white"] = QColor(255,255,255,255);
2022-06-24 01:26:00 +02:00
// load the main font
2022-06-25 18:48:08 +02:00
this->main_font_family = QFontDatabase::applicationFontFamilies(
QFontDatabase::addApplicationFont(":/fonts/Metropolis")).at(0);
// load the script font
2022-06-25 18:48:08 +02:00
this->script_font_family = QFontDatabase::applicationFontFamilies(
QFontDatabase::addApplicationFont(":/fonts/3270")).at(0);
// initialize the fonts map
2022-06-25 18:48:08 +02:00
this->FONTS["main"] = QFont(
this->main_font_family,
this->font_size );
this->FONTS["main_italic"] = QFont(
this->main_font_family,
this->font_size,
-1, true );
this->FONTS["main_bold"] = QFont(
this->main_font_family,
this->font_size,
1 );
this->FONTS["main_big"] = QFont(
this->main_font_family,
this->font_size_big );
this->FONTS["main_small"] = QFont(
this->main_font_family,
this->font_size_small );
this->FONTS["script"] = QFont(
this->script_font_family,
this->font_size );
2022-06-25 21:17:40 +02:00
// parent font for every tab
this->ui->CrapTabs->setFont( this->FONTS["main_big"] );
2022-06-25 18:48:08 +02:00
// TreeView for the LogFiles
2022-06-25 21:17:40 +02:00
this->ui->checkBox_LogFiles_CheckAll->setFont( this->FONTS["main_small"] );
2022-06-25 18:48:08 +02:00
this->ui->listLogFiles->setFont( this->FONTS["main"] );
2022-06-25 21:17:40 +02:00
// TextBrowser for the LogFiles
2022-06-25 18:48:08 +02:00
this->TB.wide_lines = false;
this->TB.color_scheme = 1;
this->TB.font_size = this->font_size;
this->TB.font_family = this->main_font_family;
this->TB.font = QFont(
this->TB.font_family,
this->TB.font_size );
this->ui->textLogFiles->setFont( this->TB.font );
2022-06-25 21:17:40 +02:00
// get a fresh list of LogFiles
this->ui->listLogFiles->header()->resizeSection(0,200);
this->ui->listLogFiles->header()->resizeSection(1,100);
2022-06-21 21:07:06 +02:00
this->on_buttonRefreshList_clicked();
2022-06-25 19:50:19 +02:00
2022-06-20 21:44:58 +02:00
}
2022-06-21 21:07:06 +02:00
MainWindow::~MainWindow()
2022-06-20 21:44:58 +02:00
{
delete ui;
}
2022-06-24 01:26:00 +02:00
2022-06-21 21:07:06 +02:00
//////////////
//// LOGS ////
//////////////
// refresh the log files list
void MainWindow::on_buttonRefreshList_clicked()
{
// clear the current tree
this->ui->listLogFiles->clear();
// iterate over elements of list
2022-06-23 04:00:37 +02:00
for ( const Craplog::LogFile& log_file : this->craplog.getLogsList(true) ) {
2022-06-21 21:07:06 +02:00
// new entry for the tree widget
QTreeWidgetItem * item = new QTreeWidgetItem();
// set unchecked
item->setCheckState(0, Qt::CheckState::Unchecked );
2022-06-23 20:07:59 +02:00
// set the name of the file
2022-06-23 04:00:37 +02:00
item->setText( 0, log_file.name );
2022-06-25 18:48:08 +02:00
//item->setFont( 0, this->FONTS["main"] );
2022-06-23 20:07:59 +02:00
// prepare the size of the file
2022-06-21 21:07:06 +02:00
float size = (float)log_file.size / 1024;
std::string sfx = " KiB";
if (size > 1024) {
size /= 1024;
sfx = " MiB";
}
// cut decimals depending on how big the floor is
std::string size_str = std::to_string( size );
2022-06-23 20:07:59 +02:00
int cut_index = size_str.find('.')+1;
if ( cut_index == 0 ) {
cut_index = size_str.find(',')+1;
2022-06-21 21:07:06 +02:00
}
2022-06-23 20:07:59 +02:00
int n_decimals = 3;
2022-06-21 21:07:06 +02:00
if ( size >= 100 ) {
n_decimals = 2;
if ( size >= 1000 ) {
n_decimals = 1;
if ( size >= 10000 ) {
n_decimals = 0;
2022-06-23 20:07:59 +02:00
cut_index --;
2022-06-21 21:07:06 +02:00
}
}
}
2022-06-23 20:07:59 +02:00
if ( cut_index >= 1 ) {
cut_index += n_decimals;
if ( cut_index > size_str.size()-1 ) {
cut_index = size_str.size()-1;
2022-06-21 21:07:06 +02:00
}
}
2022-06-23 20:07:59 +02:00
// apply text and color to the size text
item->setText( 1, QString::fromStdString( size_str.substr(0,cut_index) + sfx ) );
item->setForeground( 1, this->COLORS["grey"] );
item->setFont( 1, this->FONTS["main_italic"] );
2022-06-21 21:07:06 +02:00
// append the item (on top, forced)
this->ui->listLogFiles->addTopLevelItem( item );
}
// sort the list alphabetically
this->ui->listLogFiles->sortByColumn(0, Qt::SortOrder::AscendingOrder );
}
2022-06-25 21:17:40 +02:00
void MainWindow::on_checkBox_LogFiles_CheckAll_stateChanged(int arg1)
2022-06-21 21:07:06 +02:00
{
Qt::CheckState new_state;
2022-06-25 21:17:40 +02:00
if ( this->ui->checkBox_LogFiles_CheckAll->checkState() == Qt::CheckState::Checked ) {
// check all
new_state = Qt::CheckState::Checked;
2022-06-25 21:17:40 +02:00
} else if ( this->ui->checkBox_LogFiles_CheckAll->checkState() == Qt::CheckState::Unchecked ) {
// un-check all
new_state = Qt::CheckState::Unchecked;
} else {
// do nothing
return;
}
2022-06-21 21:07:06 +02:00
QTreeWidgetItemIterator i(this->ui->listLogFiles);
while ( *i ) {
(*i)->setCheckState( 0, new_state );
++i;
2022-06-21 21:07:06 +02:00
}
}
void MainWindow::on_buttonViewFile_clicked()
{
2022-06-25 18:48:08 +02:00
if ( this->ui->listLogFiles->selectedItems().size() > 0 ) {
// display the selected item
Craplog::LogFile item = this->craplog.getLogFileItem(
this->ui->listLogFiles->selectedItems().takeFirst()->text(0) );
2022-06-26 22:31:45 +02:00
FormatOps::LogsFormat format;
2022-06-25 18:48:08 +02:00
if ( item.type == Craplog::LogType::Access ) {
2022-06-26 22:31:45 +02:00
format = this->craplog.getCurrentALF();
2022-06-25 18:48:08 +02:00
} else if ( item.type == Craplog::LogType::Error ) {
2022-06-26 22:31:45 +02:00
format = this->craplog.getCurrentELF();
2022-06-25 18:48:08 +02:00
} else {
// this shouldn't be
// !!! PUT A DIALOG ERROR MESSAGE HERE !!!
}
this->ui->textLogFiles->setText(
RichText::enrichLogs(
IOutils::readFile( item.path ),
format,
this->TB.color_scheme,
this->TB.wide_lines ));
}
2022-06-21 21:07:06 +02:00
}
void MainWindow::on_listLogFiles_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
this->on_buttonViewFile_clicked();
}
void MainWindow::on_listLogFiles_itemChanged(QTreeWidgetItem *item, int column)
{
// control checked
int n_checked = 0;
QTreeWidgetItemIterator i(this->ui->listLogFiles);
while ( *i ) {
if ( (*i)->checkState(0) == Qt::CheckState::Checked ) {
n_checked++;
}
++i;
}
if ( n_checked == 0 ) {
2022-06-25 21:17:40 +02:00
this->ui->checkBox_LogFiles_CheckAll->setCheckState(Qt::CheckState::Unchecked);
} else if ( n_checked == this->craplog.getLogsListSize() ) {
2022-06-25 21:17:40 +02:00
this->ui->checkBox_LogFiles_CheckAll->setCheckState(Qt::CheckState::Checked);
} else {
2022-06-25 21:17:40 +02:00
this->ui->checkBox_LogFiles_CheckAll->setCheckState(Qt::CheckState::PartiallyChecked);
}
}
bool MainWindow::runCraplog()
{
// feed craplog with the checked values
bool proceed = true;
QTreeWidgetItemIterator i(this->ui->listLogFiles);
while ( *i ) {
if ( (*i)->checkState(0) == Qt::CheckState::Checked ) {
// tell Craplog to set this file as selected
if ( this->craplog.setLogFileSelected( (*i)->text(0) ) != 0 ) {
// this shouldn't be, but...
int response = QMessageBox::warning(this,
QString("File selection failed"),
QString("No file in the list matching this name:\n%1\n\nContinue?")
.arg( (*i)->text(0) ),
QMessageBox::Abort | QMessageBox::Ignore );
if ( response == QMessageBox::Abort ) {
proceed = false;
break;
}
}
}
++i;
}
if ( proceed == false ) {
return proceed;
}
2022-06-24 01:26:00 +02:00
return proceed;
2022-06-21 21:07:06 +02:00
}