LimeReport/limereport/lrpreviewreportwindow.cpp

391 lines
12 KiB
C++
Raw Normal View History

2016-02-17 10:11:00 +03:00
/***************************************************************************
* This file is part of the Lime Report project *
* Copyright (C) 2015 by Alexander Arin *
* arin_a@bk.ru *
* *
** GNU General Public License Usage **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
** GNU Lesser General Public License **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
* This library 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. *
****************************************************************************/
#include "lrpreviewreportwindow.h"
#include "ui_lrpreviewreportwindow.h"
#include "serializators/lrxmlreader.h"
#include "serializators/lrxmlwriter.h"
#include "lrreportengine_p.h"
#include "lrpreviewreportwidget.h"
#include "lrpreviewreportwidget_p.h"
2016-02-17 10:11:00 +03:00
#include <QPrinter>
#include <QPrintDialog>
#include <QFileDialog>
#include <QScrollBar>
2016-02-17 10:39:17 +03:00
#include <QDesktopWidget>
2016-02-17 10:11:00 +03:00
namespace LimeReport{
2016-02-17 10:11:00 +03:00
PreviewReportWindow::PreviewReportWindow(ReportEnginePrivate *report,QWidget *parent, QSettings *settings, Qt::WindowFlags flags) :
QMainWindow(parent,flags),
ui(new Ui::PreviewReportWindow), m_settings(settings), m_ownedSettings(false)
2016-02-17 10:11:00 +03:00
{
ui->setupUi(this);
setWindowTitle("Lime Report Preview");
m_pagesNavigator = new QSpinBox(this);
m_pagesNavigator->setMaximum(10000000);
m_pagesNavigator->setPrefix(tr("Page: "));
2016-02-17 10:11:00 +03:00
m_pagesNavigator->setMinimumWidth(120);
ui->toolBar->insertWidget(ui->actionNextPage,m_pagesNavigator);
ui->actionShowMessages->setVisible(false);
2016-02-17 10:11:00 +03:00
connect(m_pagesNavigator,SIGNAL(valueChanged(int)),this,SLOT(slotPageNavigatorChanged(int)));
m_previewReportWidget = new PreviewReportWidget(report,this);
setCentralWidget(m_previewReportWidget);
layout()->setContentsMargins(1,1,1,1);
connect(m_previewReportWidget,SIGNAL(pageChanged(int)), this,SLOT(slotPageChanged(int)) );
2016-06-10 18:05:18 +03:00
connect(m_previewReportWidget->d_ptr->m_previewPage,SIGNAL(selectionChanged()),this,SLOT(slotSelectionChanged()));
connect(m_pagesNavigator,SIGNAL(valueChanged(int)),this,SLOT(slotPageNavigatorChanged(int)));
2016-06-10 18:05:18 +03:00
m_fontEditor = new FontEditorWidget(m_previewReportWidget->d_ptr->m_previewPage,tr("Font"),this);
m_fontEditor->setObjectName("fontTools");
m_fontEditor->setIconSize(ui->toolBar->iconSize());
m_textAlignmentEditor = new TextAlignmentEditorWidget(m_previewReportWidget->d_ptr->m_previewPage,tr("Text align"),this);
m_textAlignmentEditor->setObjectName("textAlignmentTools");
m_textAlignmentEditor->setIconSize(ui->toolBar->iconSize());
addToolBar(Qt::TopToolBarArea,m_fontEditor);
addToolBar(Qt::TopToolBarArea,m_textAlignmentEditor);
m_scalePercent = new QComboBox(this);
2016-04-05 23:06:11 +03:00
m_scalePercent->setEditable(true);
ui->toolBar->insertWidget(ui->actionZoomOut, m_scalePercent);
initPercentCombobox();
2016-06-10 18:05:18 +03:00
// connect(ui->graphicsView->verticalScrollBar(),SIGNAL(valueChanged(int)), this, SLOT(slotSliderMoved(int)));
connect(ui->actionShowMessages, SIGNAL(triggered()), this, SLOT(slotShowErrors()));
connect(m_previewReportWidget, SIGNAL(scalePercentChanged(int)), this, SLOT(slotScalePercentChanged(int)));
connect(m_scalePercent, SIGNAL(currentIndexChanged(QString)), this, SLOT(scaleComboboxChanged(QString)));
2016-02-17 10:11:00 +03:00
restoreSetting();
selectStateIcon();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::writeSetting()
{
settings()->beginGroup("PreviewWindow");
settings()->setValue("Geometry",saveGeometry());
settings()->setValue("State",saveState());
settings()->endGroup();
}
void PreviewReportWindow::restoreSetting()
{
settings()->beginGroup("PreviewWindow");
QVariant v = settings()->value("Geometry");
if (v.isValid()){
restoreGeometry(v.toByteArray());
2016-02-17 10:39:17 +03:00
} else {
2016-03-24 02:14:09 +03:00
QDesktopWidget *desktop = QApplication::desktop();
2016-02-17 10:39:17 +03:00
2016-03-24 02:14:09 +03:00
int screenWidth = desktop->screenGeometry().width();
int screenHeight = desktop->screenGeometry().height();
2016-02-17 10:39:17 +03:00
int x = screenWidth*0.1;
int y = screenHeight*0.1;
resize(screenWidth*0.8, screenHeight*0.8);
move(x, y);
2016-02-17 10:11:00 +03:00
}
v = settings()->value("State");
if (v.isValid()){
restoreState(v.toByteArray());
}
settings()->endGroup();
}
PreviewReportWindow::~PreviewReportWindow()
{
if (m_ownedSettings)
delete m_settings;
//delete m_previewPage;
2016-02-17 10:11:00 +03:00
delete ui;
}
void PreviewReportWindow::initPreview(int pagesCount)
{
m_pagesNavigator->setSuffix(tr(" of %1").arg(pagesCount));
m_pagesNavigator->setMinimum(1);
m_pagesNavigator->setMaximum(pagesCount);
m_pagesNavigator->setValue(1);
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::setSettings(QSettings* value)
{
if (m_ownedSettings)
delete m_settings;
m_settings=value;
m_ownedSettings=false;
restoreSetting();
}
void PreviewReportWindow::setErrorMessages(const QStringList &value){
ui->actionShowMessages->setVisible(true);
m_previewReportWidget->setErrorMessages(value);
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::setToolBarVisible(bool value)
{
ui->toolBar->setHidden(value);
selectStateIcon();
}
void PreviewReportWindow::setStatusBarVisible(bool value)
{
ui->statusbar->setVisible(value);
}
void PreviewReportWindow::setMenuVisible(bool value)
{
ui->menubar->setVisible(value);
}
2016-02-17 10:11:00 +03:00
QSettings*PreviewReportWindow::settings()
{
if (m_settings){
return m_settings;
} else {
m_settings = new QSettings("LimeReport",QApplication::applicationName());
m_ownedSettings = true;
return m_settings;
}
}
void PreviewReportWindow::setReportReader(ItemsReaderIntf::Ptr /*reader*/)
2016-02-17 10:11:00 +03:00
{
// m_reader=reader;
// if (!reader.isNull()){
// if (reader->first()) reader->readItem(m_previewPage->pageItem());
// int pagesCount = reader->firstLevelItemsCount();
// m_previewPage->pageItem()->setItemMode(PreviewMode);
// initPreview(pagesCount);
// }
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::setPages(ReportPages pages)
{
m_previewReportWidget->d_ptr->setPages(pages);
if (!pages.isEmpty()){
initPreview(pages.count());
2016-02-17 10:11:00 +03:00
}
}
void PreviewReportWindow::exec()
{
bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
setAttribute(Qt::WA_DeleteOnClose,false);
setAttribute(Qt::WA_ShowModal,true);
show();
m_eventLoop.exec();
if (deleteOnClose) delete this;
}
void PreviewReportWindow::closeEvent(QCloseEvent *)
{
#ifdef Q_OS_WIN
writeSetting();
#endif
#ifdef Q_OS_MAC
writeSetting();
#endif
m_eventLoop.exit();
}
void PreviewReportWindow::resizeEvent(QResizeEvent* e)
{
#ifdef Q_OS_UNIX
if (e->oldSize()!=e->size()){
writeSetting();
}
#else
Q_UNUSED(e)
#endif
}
void PreviewReportWindow::moveEvent(QMoveEvent* e)
{
#ifdef Q_OS_UNIX
if (e->oldPos()!=e->pos()){
writeSetting();
}
#else
Q_UNUSED(e)
#endif
}
2016-06-10 18:05:18 +03:00
void PreviewReportWindow::showEvent(QShowEvent *)
{
m_fontEditor->setVisible(ui->actionEdit_Mode->isChecked());
m_textAlignmentEditor->setVisible(ui->actionEdit_Mode->isChecked());
}
void PreviewReportWindow::selectStateIcon()
{
if (ui->toolBar->isHidden()){
ui->actionShow_Toolbar->setIcon(QIcon(":/report/images/not_checked.png"));
} else {
ui->actionShow_Toolbar->setIcon(QIcon(":/report/images/checked.png"));
}
}
2016-06-10 18:05:18 +03:00
2016-02-17 10:11:00 +03:00
void PreviewReportWindow::slotPrint()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->print();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotPriorPage()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->priorPage();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotNextPage()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->nextPage();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotZoomIn()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->zoomIn();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotZoomOut()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->zoomOut();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotPageNavigatorChanged(int value)
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->pageNavigatorChanged(value);
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotShowErrors()
{
m_previewReportWidget->setErrorsMesagesVisible(ui->actionShowMessages->isChecked());
2016-02-17 10:11:00 +03:00
}
2016-06-10 18:05:18 +03:00
void PreviewReportWindow::on_actionEdit_Mode_triggered(bool checked)
{
m_previewReportWidget->d_ptr->m_previewPage->setItemMode((checked)?ItemModes(DesignMode):PreviewMode);
m_textAlignmentEditor->setVisible(checked);
m_fontEditor->setVisible(checked);
//m_reportPages.at(m_currentPage)->setItemMode((checked)?DesignMode:PreviewMode);
}
void PreviewReportWindow::slotSelectionChanged()
{
QGraphicsScene* page=dynamic_cast<QGraphicsScene*>(sender());
if (page){
if (page->selectedItems().count()==1){
BaseDesignIntf* item = dynamic_cast<BaseDesignIntf*>(page->selectedItems().at(0));
if (item) {
m_fontEditor->setItem(item);
m_textAlignmentEditor->setItem(item);
}
}
}
}
2016-02-17 10:11:00 +03:00
ItemsReaderIntf *PreviewReportWindow::reader()
{
return m_reader.data();
}
void PreviewReportWindow::initPercentCombobox()
{
for (int i = 10; i<310; i+=10){
m_scalePercent->addItem(QString("%1%").arg(i));
}
m_scalePercent->setCurrentIndex(4);
}
2016-02-17 10:11:00 +03:00
void PreviewReportWindow::on_actionSaveToFile_triggered()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->saveToFile();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotFirstPage()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->firstPage();
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::slotLastPage()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->lastPage();
2016-02-17 10:11:00 +03:00
}
2016-02-17 10:19:50 +03:00
void PreviewReportWindow::slotPrintToPDF()
{
2016-04-02 03:38:23 +03:00
m_previewReportWidget->printToPDF();
2016-02-17 10:19:50 +03:00
}
void PreviewReportWindow::slotPageChanged(int pageIndex)
2016-02-17 10:11:00 +03:00
{
m_pagesNavigator->setValue(pageIndex);
2016-02-17 10:11:00 +03:00
}
void PreviewReportWindow::on_actionFit_page_width_triggered()
{
m_previewReportWidget->fitWidth();
}
void PreviewReportWindow::on_actionFit_page_triggered()
{
m_previewReportWidget->fitPage();
}
void PreviewReportWindow::on_actionOne_to_one_triggered()
{
m_previewReportWidget->setScalePercent(100);
}
void PreviewReportWindow::scaleComboboxChanged(QString text)
{
m_previewReportWidget->setScalePercent(text.remove(text.count()-1,1).toInt());
}
void PreviewReportWindow::slotScalePercentChanged(int percent)
{
2016-04-05 23:06:11 +03:00
m_scalePercent->setEditText(QString("%1%").arg(percent));
}
void PreviewReportWindow::on_actionShowMessages_toggled(bool value)
{
m_previewReportWidget->setErrorsMesagesVisible(value);
}
void PreviewReportWindow::on_actionShow_Toolbar_triggered()
{
setToolBarVisible(!ui->toolBar->isHidden());
writeSetting();
}
2016-06-10 18:05:18 +03:00
}// namespace LimeReport