Files
qt5-legatus-legacy/Lts/Plugins/OcSituationClient/Gui/OcTerminal/osc_edit_folder_content.cpp
T
2026-08-13 18:33:10 +02:00

363 lines
10 KiB
C++
Executable File

/**
* @RDE_HEADER@
* HMI Plugin for Observer Controller - PDF Folder Content
* $Id$
* $HeadURL$
* $Author$
* Created by Steve Kemayou, SLSE, 2022-07-07
*/
#include "osc_edit_folder_content.h"
#include "ui_osc_edit_folder_content.h"
#include <QDir>
#include <QTableView>
#include <LtsUtilsSettings/settings.h>
#include <LtsDataView/lts_data_view.h>
#include <LtsSession/lts_session_data.h>
namespace OcSituationClient
{
static Poppler::Page::Rotation getRotationFromValue(int rotation);
/*!
* \brief FolderContentEditDialog::FolderContentEditDialog
* \param parent
*/
FolderContentEditDialog::FolderContentEditDialog(QWidget *parent)
:LtsWidgets::LtsDialog(parent, LtsWidgets::ButtonCombination::CloseOnly)
, m_ui(new Ui::FolderContentEditDialog)
, m_session(nullptr)
, m_obdFtc(nullptr)
, m_requestHelper(new LtsUtils::RequestHelper(this))
, m_logger(getLogger("OscFolderContentEditDialog"))
{
m_ui->setupUi(this);
setOnlineHelpFilename(QStringLiteral("OSC-Edit_Folder_Content"));
m_ui->filesView->horizontalHeader()->setStretchLastSection(true);
m_ui->filesView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); // resize view table to have the same column width
m_ui->filesView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_ui->filesView->setEditTriggers(QAbstractItemView::NoEditTriggers); // can't be edited from user.
m_ui->filesView->verticalHeader()->hide();
m_buttonBox->connectButton(LtsWidgets::DialogButton::Remove, this, &FolderContentEditDialog::onDeleteFile);
m_buttonBox->connectButton(LtsWidgets::DialogButton::Show, this, &FolderContentEditDialog::onPreviewFile);
enaDisa();
const QString opName = LtsSessionData::LtsSessionData::getInstance()->getOperatorName();
const QString dir = QStringLiteral("%1/OcPdf/%2/").arg(QDir::tempPath()).arg(opName);
QDir directory(dir);
if(!directory.exists())
directory.mkdir(dir);
setDefaultFileSystemPath(dir);
}
FolderContentEditDialog::~FolderContentEditDialog()
{
if(m_pdfWidget != nullptr)
delete m_pdfWidget;
m_pdfWidget = nullptr;
delete m_ui;
}
/*!
* \brief FolderContentEditDialog::init
* \param session
* \return
*/
bool FolderContentEditDialog::init(LtsSession::LtsSessionDataDds *session)
{
m_session = session;
if (!m_session->getFtc<OcBaseDownload::DataInterface>(m_obdFtc))
{
ERROR_LOG(m_logger) << "Cant get Obd FTC can't download";
return false;
}
delete m_fileSystemModel;
m_fileSystemModel = new QFileSystemModel();
m_fileSystemModel->setFilter(QDir::Files|QDir::NoDotAndDotDot);
m_fileSystemModel->setReadOnly(true);
QStringList filters;
filters << QStringLiteral("*.pdf");
m_fileSystemModel->setNameFilters(filters);
m_fileSystemModel->setNameFilterDisables(false);
m_fileSystemModel->setReadOnly(true);
m_ui->filesView->setModel(m_fileSystemModel);
m_ui->filesView->setRootIndex(m_fileSystemModel->setRootPath(getFileSysPath()));
m_ui->filesView->hideColumn(1);
m_fileSystemModel->sort(3, Qt::DescendingOrder);
m_ui->filesView->horizontalHeader()->setStyleSheet(QStringLiteral("font-weight: bold;"));
connect(m_ui->filesView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &FolderContentEditDialog::onSelectionChanged);
QPushButton *btn = m_buttonBox->getButton(LtsWidgets::DialogButton::Save);
btn->setText(tr("Pdf Download"));
m_buttonBox->connectButton(LtsWidgets::DialogButton::Save, this, &FolderContentEditDialog::onOcPdfDownloadClicked);
initPdfWidow();
return true;
}
/*!
* \brief FolderContentEditDialog::retranslate
*/
void FolderContentEditDialog::retranslate()
{
translate(m_ui, tr("Folder Content"));
}
/*!
* \brief FolderContentEditDialog::enaDisa
*/
void FolderContentEditDialog::enaDisa()
{
m_buttonBox->setButtonEnabled(LtsWidgets::DialogButton::Remove, false);
m_buttonBox->setButtonEnabled(LtsWidgets::DialogButton::Show, false);
QItemSelectionModel *select = m_ui->filesView->selectionModel();
if(select == nullptr)
return;
QModelIndexList indexList =select->selectedRows();
if(indexList.size() == 0)
return;
m_buttonBox->setButtonEnabled(LtsWidgets::DialogButton::Remove, indexList.size() > 0);
m_buttonBox->setButtonEnabled(LtsWidgets::DialogButton::Show, indexList.size()==1);
}
/*!
* \brief FolderContentEditDialog::onSelectionChanged
*/
void FolderContentEditDialog::onSelectionChanged()
{
enaDisa();
}
void FolderContentEditDialog::onOcPdfDownloadClicked()
{
LtsUtils::ProjectSettings *ps = LtsUtils::ProjectSettings::getInstance();
const int folderId = ps->value(QStringLiteral("obd"), QStringLiteral("ocPdfDownloadFolderId")).toInt();
if(folderId <= 0)
{
LtsWidgets::LtsMessageDialog::showMessage(this, tr("Couldn't download pdf files: Folder id invalid!"));
return;
}
const QString matrixOperator = LtsSessionData::LtsSessionData::getInstance()->getOperatorName();
DEBUG_LOG(m_logger)<<LOGFUNCTION<<" folderId:" <<folderId<<" operatorName: "<<matrixOperator;
OcBaseDownload::OcPdfDownloadRequest req(folderId,matrixOperator.toStdString());
m_requestHelper->startSingleRequest([req, this]()
{
return m_obdFtc->ocPdfDownloadRequest(req, this);
});
}
/*!
* \brief FolderContentEditDialog::onPreviewFile
*/
void FolderContentEditDialog::onPreviewFile()
{
QItemSelectionModel *select = m_ui->filesView->selectionModel();
if(select == nullptr)
return;
QModelIndexList indexList =select->selectedRows();
if(indexList.size() == 0)
return;
QModelIndex index = indexList.at(0);
m_currViewPath = m_fileSystemModel->filePath(index);
//const QUrl url = QUrl(QStringLiteral("file://") + m_currViewPath);
if(m_pdfWidget == nullptr)
{
WARNING_LOG(m_logger)<<" pdf widget not initialized. trying to init...";
initPdfWidow();
}
QFile f(m_currViewPath);
if(f.open(QIODevice::ReadOnly))
{
DEBUG_LOG(m_logger) << "Current File: " << m_currViewPath;
setPage(1);
setZoomFactor(ZOOM_DEFAULT);
setRotation(Poppler::Page::Rotate0);
m_pdfWidget->setWindowTitle(m_fileSystemModel->fileName(index));
m_pdfWidget->showPDF(f.readAll());
m_pdfWidget->setVisible(true);
m_pdfWidget->showMaximized();
}
}
/*!
* \brief FolderContentEditDialog::onDeleteFile
*/
void FolderContentEditDialog::onDeleteFile()
{
TRACE_LOG(m_logger)<<" delete file START ";
QItemSelectionModel *select = m_ui->filesView->selectionModel();
if(select == nullptr)
return;
QModelIndexList indexList =select->selectedRows();
m_pdfWidget->hide();
if(indexList.size() > 0)
{
QFileInfoList fileInfoList;
for(const auto index : qAsConst(indexList))
{
fileInfoList << m_fileSystemModel->fileInfo(index);
TRACE_LOG(m_logger)<< "file path:"<<fileInfoList.last().absoluteFilePath();
}
QDir dir;
dir.setPath(getFileSysPath());
for (auto fileInfo : fileInfoList)
{
bool success = dir.remove(fileInfo.fileName());
TRACE_LOG(m_logger)<< "remove file:"<<fileInfo.absoluteFilePath() << " | succeed? "<< success;
}
}
TRACE_LOG(m_logger) <<" delete file End ";
}
/*!
* \brief FolderContentEditDialog::setDefaultFileSystemPath
* \param path
*/
void FolderContentEditDialog::setDefaultFileSystemPath(QString path)
{
m_filesysPath = path.isEmpty() ? QDir::homePath() : path;
TRACE_LOG(m_logger)<<" set default filesystem path: "<< path;
}
/*!
* \brief FolderContentEditDialog::setPage
* \param pageNo
*/
void FolderContentEditDialog::setPage(int pageNo)
{
if (m_pageNo == pageNo)
return;
DEBUG_LOG(m_logger) << "setPage = " << pageNo;
m_pageNo = pageNo;
m_pdfWidget->setPage(pageNo);
emit currentPageChanged(m_pageNo);
}
/*!
* \brief FolderContentEditDialog::getCurPage
* \return
*/
int FolderContentEditDialog::getCurPage() const
{
return m_pdfWidget->curPage();
}
/*!
* \brief FolderContentEditDialog::getNumPages
* \return
*/
int FolderContentEditDialog::getNumPages() const
{
return m_pdfWidget->numPages();
}
/*!
* \brief FolderContentEditDialog::setZoomFactor
* \param zoomFactor
*/
void FolderContentEditDialog::setZoomFactor(double zoomFactor)
{
if (qFuzzyCompare(m_zoomFactor, zoomFactor))
return;
DEBUG_LOG(m_logger) << "setZoomFactor = " << zoomFactor;
m_zoomFactor = zoomFactor;
m_pdfWidget->setZoomFactor(zoomFactor);
}
/*!
* \brief FolderContentEditDialog::setRotation
* \param rotation
*/
void FolderContentEditDialog::setRotation(int rotation)
{
if (m_rotation == rotation)
return;
DEBUG_LOG(m_logger) << "setRotation = " << rotation;
m_rotation = rotation;
m_pdfWidget->setRotation(getRotationFromValue(m_rotation));
emit rotationChanged(rotation);
}
/*!
* \brief FolderContentEditDialog::initPdfWidow
*/
void FolderContentEditDialog::initPdfWidow()
{
if(m_pdfWidget!= nullptr)
delete m_pdfWidget;
m_pdfWidget = new NGL::PDFWidget();
m_pdfWidget->setObjectName(QStringLiteral("osc_pdfWidget"));
m_pdfWidget->setSizePolicy(QSizePolicy::Policy::MinimumExpanding, QSizePolicy::Policy::MinimumExpanding);
m_pdfWidget->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
connect(m_pdfWidget, &NGL::PDFWidget::currentPageChanged, this, &FolderContentEditDialog::setPage);
connect(m_pdfWidget, &NGL::PDFWidget::rotationChanged, this, [this](Poppler::Page::Rotation rot)
{
switch (rot)
{
case Poppler::Page::Rotate90:
setRotation(90);
break;
case Poppler::Page::Rotate180:
setRotation(180);
break;
case Poppler::Page::Rotate270:
setRotation(270);
break;
case Poppler::Page::Rotate0:
setRotation(0);
break;
}
});
}
void FolderContentEditDialog::onOcPdfDownloadReply(const Core::FunctionReplyObject & replyObject)
{
m_requestHelper->requestReply(replyObject);
m_fileSystemModel->setRootPath(getFileSysPath());
}
/*!
* \brief getRotationFromValue
* \param rotation
* \return
*/
Poppler::Page::Rotation getRotationFromValue(int rotation)
{
Poppler::Page::Rotation rot;
switch (rotation)
{
case 90:
rot = Poppler::Page::Rotate90;
break;
case 180:
rot = Poppler::Page::Rotate180;
break;
case 270:
rot = Poppler::Page::Rotate270;
break;
case 0:
default:
rot = Poppler::Page::Rotate0;
break;
}
return rot;
}
} // end namespace