bmstu-mt-mla/mainwindow.cpp

145 lines
4.5 KiB
C++
Raw Normal View History

2022-12-05 23:53:37 +03:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMenuBar *menuBar = new QMenuBar();
m_menuFile = menuBar->addMenu("&File");
m_openAction = new QAction("&Open");
m_openAction->setStatusTip(tr("Open a document"));
m_menuFile->addAction(m_openAction);
m_menuFile->addSeparator();
m_quitAction = new QAction("&Quit");
m_quitAction->setShortcut(tr("CTRL+Q"));
m_quitAction->setStatusTip(tr("Quit application"));
m_menuFile->addAction(m_quitAction);
connect(this->m_openAction, &QAction::triggered, this, &MainWindow::slotOpenAction);
connect(this->m_quitAction, &QAction::triggered, qApp, &QApplication::quit);
this->setMenuBar(menuBar);
show();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotOpenAction() {
m_filename = QFileDialog::getOpenFileName(
this, tr("Выберите файл"), "../", tr("PDF (*.pdf) ;; DOCX (*.docx) ;; XLSX (*.xlsx) ;; PPTX (*.pptx)"));
if (m_filename != "") {
qDebug() << "Close file!\n";
closeFile();
if (m_filename.endsWith(".pdf")) {
openPDF(m_filename);
} else if (m_filename.endsWith(".pptx")) {
openPPTX(m_filename);
} else if (m_filename.endsWith(".docx")) {
openDOCX(m_filename);
} else if (m_filename.endsWith(".xlsx")) {
openXLSX(m_filename);
}
}
}
void MainWindow::closeFile() {
if (axObj != nullptr) {
QProcess process;
QString strCmd;
strCmd = "taskkill /im winword.exe /f";
process.execute(strCmd);
strCmd = "taskkill /im EXCEL.exe /f";
process.execute(strCmd);
strCmd = "taskkill /im POWERPNT.exe /f";
process.execute(strCmd);
strCmd = "taskkill /im AcroRd32.exe /f";
process.execute(strCmd);
process.close();
axObj->close();
axObj->clear();
delete axObj;
axObj = nullptr;
}
}
void MainWindow::openPDF(QString &filename) {
axObj = new QAxWidget(this);
setCentralWidget(axObj);
if(!axObj->setControl("Adobe PDF Reader"))
QMessageBox::critical(this, "Error", "Make sure you have Adobe Reader (and its ActiveX) installed!");
axObj->dynamicCall("LoadFile(QString)", filename);
}
void MainWindow::openPPTX(QString &filename) {
axObj = new QAxWidget("Powerpoint.Application", this);
setCentralWidget(axObj);
if (axObj == nullptr)
{
return;
}
if (!axObj->setControl(filename))
{
return;
}
axObj->dynamicCall("LoadFile(const QString&)", filename);
axObj->dynamicCall("SetVisible (bool Visible)", "true");
axObj->setProperty("DisplayAlerts", false);
axObj->setProperty("DisplayScrollBars", true);
axObj->setGeometry(this->geometry());
axObj->show();
/* run slideshow */
// // Open parameter
// QList<QVariant> param_list;
// param_list.append(filename);
// param_list.append(1);
// param_list.append(1);
// param_list.append(0);
// // Open a template file
// QAxObject *presentations = axObj->querySubObject("Presentations");
// QAxObject *presentation = presentations->querySubObject("Open(const QString&,int,int,int)", param_list);
// // Start the slides
// QAxObject *slideshow = presentation->querySubObject("SlideShowSettings");
// slideshow->setProperty("RangeType", "ppShowAll");
// slideshow->dynamicCall("run()");
}
void MainWindow::openXLSX(QString &filename) {
axObj = new QAxWidget("Excel.Application", this);
setCentralWidget(axObj);
if (axObj == nullptr)
{
return;
}
axObj->dynamicCall("SetVisible (bool Visible)", "false");
axObj->setProperty("DisplayAlerts", false);
axObj->setProperty("DisplayScrollBars", true); // Show scroll bar
axObj->setGeometry(this->geometry());
axObj->setControl(filename);
axObj->show();
}
void MainWindow::openDOCX(QString &filename) {
axObj = new QAxWidget("Word.Application", this);
setCentralWidget(axObj);
if (axObj == nullptr)
{
return;
}
axObj->dynamicCall("SetVisible (bool Visible)", "false");
axObj->setFocusPolicy(Qt::StrongFocus);
axObj->setProperty("DisplayAlerts", false);
axObj->setProperty("DisplayHorizontalScrollBar", true); // Show scroll bar
axObj->setGeometry(this->geometry());
axObj->setControl(filename);
axObj->show();
}