Commit 9400416d authored by Bat_Guano's avatar Bat_Guano

New interface for Tab-Cards.

parent 746b4c80
......@@ -30,11 +30,13 @@ HEADERS += \
src/constants.h \
src/pdftkprocess.h \
src/inputdocument.h \
src/merge.h
src/merge.h \
src/itooltab.h
SOURCES += \
src/qpdftk.cpp \
src/main.cpp \
src/actions.cpp \
src/pdftkprocess.cpp \
src/inputdocument.cpp \
src/merge.cpp
src/merge.cpp \
src/itooltab.cpp
#include "itooltab.h"
#include "constants.h"
#include <QDebug>
#include <QMessageBox>
#include <QTreeWidget>
#include <QStringList>
#include <QPixmap>
#include <QList>
#include <QMimeDatabase>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QFileDialog>
#include "file_icon.h"
IToolTab::IToolTab(QWidget *parent) : QWidget(parent)
{
IToolTab::IToolTab(QWidget *parent) : QWidget(parent) {
construct();
}
/*
* Returns the mime-type for any given file.
*/
QString IToolTab::mimeType(const QString &filePath) {
return QMimeDatabase().mimeTypeForFile( filePath ).name();
}
/*
Choose the location and name of the output file
*/
void IToolTab::chooseOutFile() {
QString oFile = QFileDialog::getSaveFileName(this, tr("choose output file"), QDir::currentPath(),PDFFILTER ) ;
QFileInfo finf(oFile);
m_pOutFile->setText(oFile);
m_pOutFile->setToolTip(finf.absoluteFilePath());
compose();
}
/*
* Remove a file from the current list of input-files.
*/
void IToolTab::remInput() {
QList<QTreeWidgetItem *> pSelected = m_pTree->selectedItems();
QList<QTreeWidgetItem *>::iterator it = pSelected.begin();
while(it != pSelected.end() ) {
QString fn = (*it)->text(0);
qDebug() << "removing " << fn;
m_pTree->removeItemWidget(*it, 0 );
QStringList::iterator fit = m_inputFiles.begin();
QStringList newList;
while(fit != m_inputFiles.end() ) {
QString infn = QFileInfo( *fit).baseName();
if(infn != fn) {
newList << *fit;
}
++fit;
}
m_inputFiles = newList;
delete (*it);
++it;
}
compose();
}
QString IToolTab::outputFile() {
return m_pOutFile->text();
}
QStringList & IToolTab::inputFiles() {
return m_inputFiles;
}
/*
* Verify if the given file is a PDF, based on
* a comparison with the mime-type.
*/
bool IToolTab::isPdf( const QString &filePath ){
QString mt = mimeType(filePath);
qDebug() << "mime-type is " << mt;
return mt == PDFMIME;
}
/*
* Add a PDF-file to the list of input-files.
*/
void IToolTab::addInput() {
QString nFile = QFileDialog::getOpenFileName(this, tr("add input file"), QDir::currentPath(),PDFFILTER ) ;
QFileInfo finf(nFile);
if(finf.size() > 0) {
if(! isPdf(nFile)) {
emit statusWarning(QString() + finf.baseName() + tr(" is not a valid PDF-file") + " (" + mimeType(nFile) + ").");
}
}
else {
emit statusWarning(QString() + finf.baseName() + tr(" is an empty file") + ".");
}
QStringList fl = QStringList() << finf.baseName();
m_inputFiles << finf.absoluteFilePath();
qDebug() << "items are " << m_inputFiles.join(", ");
QTreeWidgetItem * wItem = new QTreeWidgetItem(m_pTree, fl, 0);
wItem->setToolTip(0, nFile );
compose();
}
/*
* Fill the card with some controls, connect some
* Slots and Signals.
*/
void IToolTab::construct() {
QHBoxLayout * pTopLayout = new QHBoxLayout(this);
QVBoxLayout * pButtonLayout = new QVBoxLayout();
QVBoxLayout * pFilesLayout = new QVBoxLayout();
m_pTree = new QTreeWidget(this);
m_pTree->setColumnCount(2);
QStringList headers;
headers << tr("File") << tr("Values") ;
m_pTree->setHeaderLabels(headers);
QHBoxLayout *pOutLayout = new QHBoxLayout();
QLabel* pLblOutFile = new QLabel(tr("Output:"));
m_pOutFile = new QLineEdit(this);
QPushButton * pBtnChooseOutFile = new QPushButton(this);
pBtnChooseOutFile->setToolTip(tr("Choose an output file"));
pBtnChooseOutFile->setIcon(QPixmap(file_xpm));
pOutLayout->addWidget(pLblOutFile);
pOutLayout->addWidget(m_pOutFile);
pOutLayout->addWidget(pBtnChooseOutFile);
pFilesLayout->addWidget(m_pTree);
pFilesLayout->addLayout(pOutLayout);
QPushButton * pBtnAddInput = new QPushButton(tr("+"), this);
pBtnAddInput->setToolTip(tr("Append to list of input files"));
QPushButton * pBtnRemInput = new QPushButton(tr("-"), this);
pBtnRemInput->setToolTip(tr("Remove selected item"));
connect(pBtnAddInput, SIGNAL(clicked()), this, SLOT(addInput()));
connect(pBtnRemInput, SIGNAL(clicked()), this, SLOT(remInput()));
connect(pBtnChooseOutFile, SIGNAL(clicked()), this, SLOT(chooseOutFile()));
pButtonLayout->addWidget(pBtnAddInput);
pButtonLayout->addWidget(pBtnRemInput);
pTopLayout->addLayout(pFilesLayout);
pTopLayout->addLayout(pButtonLayout);
pTopLayout->setAlignment(pButtonLayout, Qt::AlignTop);
}
#ifndef ITOOLTAB_H
#define ITOOLTAB_H
#ifndef ATOOLTAB_H
#define ATOOLTAB_H
#include <QObject>
#include <QWidget>
#include <QStringList>
class QTreeWidget;
class QLineEdit;
class IToolTab : public QWidget
{
Q_OBJECT
private:
QLineEdit * m_pOutFile;
QTreeWidget * m_pTree;
QStringList m_inputFiles;
private:
void construct();
protected:
virtual void compose() = 0;
bool isPdf( const QString &filePath );
QString mimeType(const QString &filePath);
QStringList & inputFiles();
QString outputFile();
private slots:
void chooseOutFile();
void addInput();
void remInput();
public:
explicit IToolTab(QWidget *parent = nullptr);
signals:
void statusWarning(const QString &);
void statusError(const QString &);
void command(QStringList);
public slots:
};
#endif // ITOOLTAB_H
\ No newline at end of file
#endif // ATOOLTAB_H
#include "constants.h"
#include "merge.h"
#include <QDebug>
#include <QMessageBox>
#include <QTreeWidget>
#include <QStringList>
#include <QPixmap>
#include <QList>
#include <QMimeDatabase>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QFileDialog>
#include "file_icon.h"
/*
* This is the tab-card which permits merging PDF-files.
......@@ -23,116 +10,7 @@
*
* But do not hurry. Make it work, first.
*/
Merge::Merge(QWidget *parent) : QWidget(parent) {
construct();
}
/*
* Fill the card with some controls, connect some
* Slots and Signals.
*/
void Merge::construct() {
QHBoxLayout * pTopLayout = new QHBoxLayout(this);
QVBoxLayout * pButtonLayout = new QVBoxLayout();
QVBoxLayout * pFilesLayout = new QVBoxLayout();
m_pTree = new QTreeWidget(this);
m_pTree->setColumnCount(2);
QStringList headers;
headers << tr("File") << tr("Values") ;
m_pTree->setHeaderLabels(headers);
QHBoxLayout *pOutLayout = new QHBoxLayout();
QLabel* pLblOutFile = new QLabel(tr("Output:"));
m_pOutFile = new QLineEdit(this);
QPushButton * pBtnChooseOutFile = new QPushButton(this);
pBtnChooseOutFile->setToolTip(tr("Choose an output file"));
pBtnChooseOutFile->setIcon(QPixmap(file_xpm));
pOutLayout->addWidget(pLblOutFile);
pOutLayout->addWidget(m_pOutFile);
pOutLayout->addWidget(pBtnChooseOutFile);
pFilesLayout->addWidget(m_pTree);
pFilesLayout->addLayout(pOutLayout);
QPushButton * pBtnAddInput = new QPushButton(tr("+"), this);
pBtnAddInput->setToolTip(tr("Append to list of input files"));
QPushButton * pBtnRemInput = new QPushButton(tr("-"), this);
pBtnRemInput->setToolTip(tr("Remove selected item"));
connect(pBtnAddInput, SIGNAL(clicked()), this, SLOT(addInput()));
connect(pBtnRemInput, SIGNAL(clicked()), this, SLOT(remInput()));
connect(pBtnChooseOutFile, SIGNAL(clicked()), this, SLOT(chooseOutFile()));
pButtonLayout->addWidget(pBtnAddInput);
pButtonLayout->addWidget(pBtnRemInput);
pTopLayout->addLayout(pFilesLayout);
pTopLayout->addLayout(pButtonLayout);
pTopLayout->setAlignment(pButtonLayout, Qt::AlignTop);
}
/*
Choose the location and name of the output file
*/
void Merge::chooseOutFile() {
QString oFile = QFileDialog::getSaveFileName(this, tr("choose output file"), QDir::currentPath(),PDFFILTER ) ;
QFileInfo finf(oFile);
m_pOutFile->setText(oFile);
m_pOutFile->setToolTip(finf.absoluteFilePath());
compose();
}
/*
* Returns the mime-type for any given file.
*/
QString Merge::mimeType(const QString &filePath) {
return QMimeDatabase().mimeTypeForFile( filePath ).name();
}
/*
* Verify if the given file is a PDF, based on
* a comparison with the mime-type.
*/
bool Merge::isPdf( const QString &filePath ){
QString mt = mimeType(filePath);
qDebug() << "mime-type is " << mt;
return mt == PDFMIME;
}
/*
* Add a PDF-file to the list of input-files.
*/
void Merge::addInput() {
QString nFile = QFileDialog::getOpenFileName(this, tr("add input file"), QDir::currentPath(),PDFFILTER ) ;
QFileInfo finf(nFile);
if(finf.size() > 0) {
if(! isPdf(nFile)) {
emit statusWarning(QString() + finf.baseName() + tr(" is not a valid PDF-file") + " (" + mimeType(nFile) + ").");
}
}
else {
emit statusWarning(QString() + finf.baseName() + tr(" is an empty file") + ".");
}
QStringList fl = QStringList() << finf.baseName();
m_inputFiles << finf.absoluteFilePath();
qDebug() << "items are " << m_inputFiles.join(", ");
QTreeWidgetItem * wItem = new QTreeWidgetItem(m_pTree, fl, 0);
wItem->setToolTip(0, nFile );
compose();
}
/*
* Remove a file from the current list of input-files.
*/
void Merge::remInput() {
QList<QTreeWidgetItem *> pSelected = m_pTree->selectedItems();
QList<QTreeWidgetItem *>::iterator it = pSelected.begin();
while(it != pSelected.end() ) {
qDebug() << "removing " << (*it)->text(0);
m_pTree->removeItemWidget(*it, 0 );
delete (*it);
++it;
}
compose();
Merge::Merge(QWidget *parent) : IToolTab(parent) {
}
/*
......@@ -144,10 +22,11 @@ void Merge::compose() {
char handle = 'A';
QStringList cmd;
QStringList handles;
if(!m_inputFiles.isEmpty() ) {
QStringList::iterator it = m_inputFiles.begin();
QStringList inFiles = inputFiles();
if(!inFiles.isEmpty() ) {
QStringList::iterator it = inFiles.begin();
while(it != m_inputFiles.end() ) {
while(it != inFiles.end() ) {
QString item = QString("%1=%2").arg(handle).arg(*it);
cmd << item;
handles << QString().append(handle);
......@@ -155,9 +34,9 @@ void Merge::compose() {
++it;
}
}
QString outfile = m_pOutFile->text();
QString outfile = outputFile();
if(!outfile.isEmpty()) {
cmd << QString("cat %1 output %2").arg(handles.join(" ")).arg(m_pOutFile->text()).split(" ");
cmd << QString("cat %1 output %2").arg(handles.join(" ")).arg(outfile).split(" ");
}
qDebug() << "command will be " << cmd.join(" ");
emit command(cmd );
......
#ifndef JOINER_H
#define JOINER_H
#ifndef MERGE_H
#define MERGE_H
#include <QObject>
#include <QWidget>
#include <QStringList>
#include "itooltab.h"
class QTreeWidget;
class QLineEdit;
class Merge : public QWidget
class Merge : public IToolTab
{
Q_OBJECT
private:
QLineEdit * m_pOutFile;
QTreeWidget * m_pTree;
QStringList m_inputFiles;
private:
void construct();
bool isPdf(const QString &);
QString mimeType(const QString &);
public:
void compose();
explicit Merge(QWidget * parent = NULL);
virtual ~Merge();
private slots:
void chooseOutFile();
void compose();
void addInput();
void remInput();
signals:
void statusWarning(const QString &);
void statusError(const QString &);
void command(QStringList);
};
#endif // JOINER_H
#endif // MERGE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment