Commit fbbefbb4 authored by Laurent Montel's avatar Laurent Montel 😁

Add printpreviewpagewidget

parent 0901b8a1
/*
Copyright (c) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "printpreviewpagewidgettest.h"
#include "../printpreviewpagewidget.h"
#include "../printpreviewpageviewer.h"
#include <QDialogButtonBox>
#include <QTest>
#include <QLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
PrintPreviewPageWidgetTest::PrintPreviewPageWidgetTest(QObject *parent)
: QObject(parent)
{
}
PrintPreviewPageWidgetTest::~PrintPreviewPageWidgetTest()
{
}
void PrintPreviewPageWidgetTest::shouldHaveNotMargin()
{
WebEngineViewer::PrintPreviewPageWidget w;
QCOMPARE(w.layout()->margin(), 0);
}
void PrintPreviewPageWidgetTest::shouldHaveDefaultValue()
{
WebEngineViewer::PrintPreviewPageWidget w;
WebEngineViewer::PrintPreviewPageViewer *page = w.findChild<WebEngineViewer::PrintPreviewPageViewer *>(QStringLiteral("printpreviewpage"));
QVERIFY(page);
QHBoxLayout *hbox = w.findChild<QHBoxLayout *>(QStringLiteral("layoutcombobox"));
QVERIFY(hbox);
QCOMPARE(hbox->margin(), 0);
QLabel *lab = w.findChild<QLabel *>(QStringLiteral("labelcombobox"));
QVERIFY(lab);
QComboBox *mPageComboBox = w.findChild<QComboBox *>(QStringLiteral("pagecombobox"));
QVERIFY(mPageComboBox);
QCOMPARE(mPageComboBox->count(), 0);
}
QTEST_MAIN(PrintPreviewPageWidgetTest)
/*
Copyright (c) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PRINTPREVIEWPAGEWIDGETTEST_H
#define PRINTPREVIEWPAGEWIDGETTEST_H
#include <QObject>
class PrintPreviewPageWidgetTest : public QObject
{
Q_OBJECT
public:
explicit PrintPreviewPageWidgetTest(QObject *parent = Q_NULLPTR);
~PrintPreviewPageWidgetTest();
private Q_SLOTS:
void shouldHaveNotMargin();
void shouldHaveDefaultValue();
};
#endif // PRINTPREVIEWPAGEWIDGETTEST_H
/*
Copyright (c) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "printpreviewpagewidget.h"
#include "printpreviewpageviewer.h"
#include <poppler-qt5.h>
#include <QLabel>
#include <QComboBox>
#include <QVBoxLayout>
#include <KMessageBox>
#include <KLocalizedString>
using namespace WebEngineViewer;
PrintPreviewPageWidget::PrintPreviewPageWidget(QWidget *parent)
: QWidget(parent),
mDoc(Q_NULLPTR)
{
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(0);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->setObjectName(QStringLiteral("layoutcombobox"));
hbox->setMargin(0);
layout->addLayout(hbox);
QLabel *lab = new QLabel(i18n("Page:"), this);
lab->setObjectName(QStringLiteral("labelcombobox"));
hbox->addWidget(lab);
mPageComboBox = new QComboBox(this);
mPageComboBox->setObjectName(QStringLiteral("pagecombobox"));
hbox->addWidget(mPageComboBox);
hbox->addStretch(1);
connect(mPageComboBox, SIGNAL(activated(int)), this, SLOT(showPage(int)));
mPrintPreviewPage = new PrintPreviewPageViewer(this);
mPrintPreviewPage->setObjectName(QStringLiteral("printpreviewpage"));
layout->addWidget(mPrintPreviewPage);
setLayout(layout);
}
PrintPreviewPageWidget::~PrintPreviewPageWidget()
{
delete mDoc;
mDoc = Q_NULLPTR;
}
void PrintPreviewPageWidget::loadFile(const QString &path)
{
if (path.isEmpty()) {
return;
}
mDoc = Poppler::Document::load(path);
if (!mDoc) {
KMessageBox::error(this, i18n("Unable to open file \"%1\"", path), i18n("Open file error"));
return;
}
mDoc->setRenderHint(Poppler::Document::TextAntialiasing, true);
mDoc->setRenderHint(Poppler::Document::Antialiasing, true);
fillComboBox();
showPage(0);
}
void PrintPreviewPageWidget::fillComboBox()
{
mPageComboBox->clear();
const int pageCount = mDoc->numPages();
for (int i = 0; i < pageCount; ++i) {
mPageComboBox->addItem(QString::number(i + 1));
}
}
void PrintPreviewPageWidget::showPage(int index)
{
Poppler::Page *popplerPage = mDoc->page(index);
mPrintPreviewPage->showPage(popplerPage);
delete popplerPage;
}
/*
Copyright (c) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PRINTPREVIEWPAGEWIDGET_H
#define PRINTPREVIEWPAGEWIDGET_H
#include <QWidget>
class QComboBox;
namespace Poppler
{
class Document;
}
namespace WebEngineViewer
{
class PrintPreviewPageViewer;
class PrintPreviewPageWidget : public QWidget
{
Q_OBJECT
public:
explicit PrintPreviewPageWidget(QWidget *parent = Q_NULLPTR);
~PrintPreviewPageWidget();
void loadFile(const QString &path);
private Q_SLOTS:
void showPage(int index);
private:
void fillComboBox();
PrintPreviewPageViewer *mPrintPreviewPage;
Poppler::Document *mDoc;
QComboBox *mPageComboBox;
};
}
#endif // PRINTPREVIEWPAGEWIDGET_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