📄 ncreportdesignerdocument.cpp
字号:
/*************************************************************************** * Copyright (C) 2006 by Szabó Norbert * * nszabo@helta.hu * * * * This program 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 2 of the License, or * * (at your option) any later version. * * * * 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., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#include "ncreportdesignerdocument.h"#include "ncreportdesignerdesignarea.h"#include "ncreportdesignerwindow.h"#include "ncreportdesignerresourcehandler.h"//#include <qmouseevent>#include <qlayout.h>#include <qlabel.h>#include <qpainter.h>//#include <qlinef>//#include <qscrollarea>#include <qscrollview.h>#include <qmessagebox.h>#include <qfile.h>#include <qcursor.h>#include <qfiledialog.h>#include <qstyle.h>#define RULERWEIGHT 28#define DEFAULT_PAGE_WIDTH 210.0///////////////////////////////////// Workplace///////////////////////////////////NCReportDesignerDocument::NCReportDesignerDocument( NCReportDesignerWindow *main, QWidget *parent, const char* name, int wflags ) : QWidget(parent, name, wflags ){ mainwindow = main; sectionNextGroupAfter = 0; modified = FALSE; QGridLayout *grid = new QGridLayout( this ); grid->setMargin(0); grid->setSpacing(0); /* rulertop = new NCReportDesignerRuler( Qt::Horizontal, this ); rulertop->setScaleStartPoint( RULERWEIGHT ); grid->addWidget( rulertop, 0,1 );*/ //QScrollArea *sa = new QScrollArea( this ); //sa->setBackgroundRole(QPalette::Dark); //-------------------------------------------- // Measurement //-------------------------------------------- msrment = new Measurement(); //rulerleft->setMeasurement( msrment ); sv = new QScrollView( this ); sv->viewport()->setBackgroundColor( QColor( 160,160,160) ); //sv->setMargins(0,0,0,0); sectioncontainer = new QWidget( sv->viewport() ); laySC = new QVBoxLayout( sectioncontainer ); laySC->setMargin(0); laySC->setSpacing(0); sectioncontainer->resize(640,480); sectioncontainer->move( 0, RULERWEIGHT ); //sectioncontainer->resize( msrment->measureToPixel( 210.0 ), 480 ); //-------------------------------------------- // Add top ruler //-------------------------------------------- rulertop = new NCReportDesignerRuler( Qt::Horizontal, sectioncontainer ); rulertop->setScaleStartPoint( RULERWEIGHT ); rulertop->setMeasurement( msrment ); laySC->addWidget( rulertop ); //-------------------------------------------- // Test sections //-------------------------------------------- //laySC->addStretch( 5 ); //sect1->setFixedSize( msrment->measureToPixel( 210.0 ), 50 ); //test //sa->setWidget( sectioncontainer ); sv->addChild(sectioncontainer,0,0); grid->addWidget( sv, 1,1 ); //grid->addWidget( sectioncontainer, 1,1 ); connect( this, SIGNAL(destroyed()), main, SLOT(refreshTools()) );}// NCReportDesignerDocument::NCReportDesignerDocument( QWidget* parent, const char* name, int wflags )// : QMainWindow( parent, name, wflags )// {// mmovie = 0;// medit = new QTextEdit( this );// setFocusProxy( medit );// setCentralWidget( medit );// }NCReportDesignerDocument::~NCReportDesignerDocument(){ delete msrment;}void NCReportDesignerDocument::closeEvent( QCloseEvent *e ){ if ( isModified() ) { switch( QMessageBox::warning( this, "Save Changes", tr("Save changes to %1?").arg( caption() ), tr("Yes"), tr("No"), tr("Cancel") ) ) { case 0: { save(); if ( !filename.isEmpty() ) e->accept(); else e->ignore(); } break; case 1: e->accept(); break; default: e->ignore(); break; } } else { e->accept(); }}bool NCReportDesignerDocument::load( const QString& fn ){ filename = fn; QFile f( filename ); if ( !f.open( IO_ReadOnly ) ) return FALSE; NCReportDesignerResourceHandler r; r.setDocument( this ); r.setMeasurement( msrment ); qApp->setOverrideCursor( QCursor( Qt::waitCursor) ); //setUpdatesEnabled( FALSE ); bool ok = r.load( &f ); //setUpdatesEnabled( TRUE ); qApp->restoreOverrideCursor(); if ( ok ) { setCaption( filename ); emit message( QString("Loaded document %1").arg(filename), 2000 ); } else { emit message( r.lastLoadError(), 2000 ); QMessageBox::warning( this, tr("Load error"), r.lastLoadError() ); } return ok;}void NCReportDesignerDocument::save(){ if ( filename.isEmpty() ) { saveAs(); return; } QString text; // = medit->text(); QFile f( filename ); if ( !f.open( IO_WriteOnly ) ) { emit message( QString("Could not write to %1").arg(filename), 2000 ); return; } qApp->setOverrideCursor( QCursor( Qt::waitCursor) ); NCReportDesignerResourceHandler r; r.setDocument( this ); r.setMeasurement( msrment ); bool ok = r.save( &f ); f.close(); qApp->restoreOverrideCursor(); if ( ok ) { setCaption( filename ); emit message( tr( "File %1 saved" ).arg( filename ), 2000 ); setModified( FALSE ); } else { QMessageBox::warning( this, tr("Save error"), tr("Could not save file %1!").arg(filename) ); } }void NCReportDesignerDocument::saveAs(){ QString fn = QFileDialog::getSaveFileName( filename, QString::null, this ); if ( !fn.isEmpty() ) { if ( !fn.contains('.') ) fn+=".xml"; filename = fn; save(); } else { emit message( "Saving aborted", 2000 ); }}void NCReportDesignerDocument::setModified( bool set ){ modified = set; emit documentModified();}bool NCReportDesignerDocument::isModified( ){ return modified;}Measurement * NCReportDesignerDocument::measurement( ){ return msrment;}double NCReportDesignerDocument::defaultPageWidth( ){ return DEFAULT_PAGE_WIDTH - po.leftMargin - po.rightMargin;}NCReportDesignerDesignArea * NCReportDesignerDocument::activeDesignArea( ){ QDictIterator<NCReportDesignerSection> it( sections ); for( ; it.current(); ++it ) { if ( it.current()->designArea()->hasFocus() ) return it.current()->designArea(); } return 0;}void NCReportDesignerDocument::setPageWidth( double pwidth ){ QDictIterator<NCReportDesignerSection> it( sections ); for( ; it.current(); ++it ) it.current()->setWidth( pwidth - po.leftMargin - po.rightMargin );}void NCReportDesignerDocument::updateSections(){ QDictIterator<NCReportDesignerSection> it( sections ); for( ; it.current(); ++it ) it.current()->update();}NCReportDesignerSection* NCReportDesignerDocument::addSection( NCReportDesignerSection::SectionType st, const QString & sname, const QString& caption, double pWidth, double pHeight, QWidget* after ){ NCReportDesignerSection *s =0; const char* n = sname.latin1(); int wIndex =-1; if ( after ) wIndex = laySC->findWidget( after )+1; s = new NCReportDesignerSection( st, mainwindow, sectioncontainer, caption, n ); s->setMeasurement( msrment ); s->designArea()->setParentDocument( this ); s->designArea()->setParentSection( s ); s->setWidth( pWidth ); s->setHeight( pHeight ); //s->designArea()->setFixedWidth( msrment->measureToPixel( pWidth ) ); // initial (fixed) width //s->designArea()->setMinimumHeight( msrment->measureToPixel(pHeight) ); // initial height connect( mainwindow, SIGNAL(currentToolChanged()), s->designArea(), SLOT(toolChanged()) ); laySC->insertWidget( wIndex, s ); sections.insert( sname, s ); return s;}void NCReportDesignerDocument::removeSection( const QString & sname ){ NCReportDesignerSection *s = sections[sname]; if ( s ) { s->hide(); sections.remove(sname); delete s; }}void NCReportDesignerDocument::addQuery( const ReportQuery & q ){ queries[q.alias]=q; }void NCReportDesignerDocument::removeQuery( const QString & alias ){ queries.remove(alias);}void NCReportDesignerDocument::addVariable( const ReportVariable & var ){ variables[var.name] = var;}void NCReportDesignerDocument::removeVariable( const QString & varname ){ variables.remove(varname);}void NCReportDesignerDocument::addGroup( ReportGroup & group ){ // Header group.header = addSection( NCReportDesignerSection::GroupHeader, group.name+"_h", tr("Group header")+": "+group.name, defaultPageWidth(), 15.0, sectionNextGroupAfter ); sectionNextGroupAfter = group.header; // save as last g.header // Footer group.footer = addSection( NCReportDesignerSection::GroupFooter, group.name+"_f", tr("Group footer")+": "+group.name, defaultPageWidth(), 15.0, sections["detail"] ); group.header->setFooterSection( group.footer ); // save link to footer groups[group.name] = group; // returns the header}void NCReportDesignerDocument::removeGroup( const QString & gname ){ ReportGroup group = groups[gname]; if ( group.name.isEmpty() ) return; delete group.header; delete group.footer; groups.remove(gname); if ( groups.empty() ) return; QMap<QString,ReportGroup>::ConstIterator it; it = groups.end(); it--; sectionNextGroupAfter = it.data().header; // last header}void NCReportDesignerDocument::updateGroup( const ReportGroup &g ){}NCReportDesignerSection* NCReportDesignerDocument::addPageHeader(){ NCReportDesignerSection *s = addSection( NCReportDesignerSection::PageHeader, "pheader", tr("Page header"), defaultPageWidth(), 15.0 ); sectionNextGroupAfter = s; return s;}NCReportDesignerSection* NCReportDesignerDocument::addPageFooter(){ NCReportDesignerSection *s = addSection( NCReportDesignerSection::PageFooter, "pfooter", tr("Page footer"), defaultPageWidth(), 15.0 ); sections["pheader"]->setFooterSection( s ); return s;}NCReportDesignerSection* NCReportDesignerDocument::addDetail(){ NCReportDesignerSection *s = addSection( NCReportDesignerSection::Detail, "detail", tr("Detail"), defaultPageWidth(), 8.0 ); return s;}NCReportDesignerSection* NCReportDesignerDocument::sectionByName( const QString& name ){ return sections[name];}void NCReportDesignerDocument::setPageSize(NCReportDesignerSection *sect ){ double w=0,h=0; if ( po.pageSize == "A0" ) { w = 841.0; h = 1189.0; } else if ( po.pageSize == "A1" ) { w = 594.0; h = 841.0; } else if ( po.pageSize == "A2" ) { w = 420.0; h = 594.0; } else if ( po.pageSize == "A3" ) { w = 297.0; h = 420.0; } else if ( po.pageSize == "A4" ) { w = 210.0; h = 297.0; } else if ( po.pageSize == "A5M" ) { // half A4 = A5 w = 210.0; h = 148.0; } else if ( po.pageSize == "A5" ) { w = 148.0; h = 210.0; } else if ( po.pageSize == "A6" ) { w = 105.0; h = 148.0; } else if ( po.pageSize == "A7" ) { w = 74.0; h = 105.0; } else if ( po.pageSize == "A8" ) { w = 52.0; h = 74.0; } else if ( po.pageSize == "A9" ) { w = 37.0; h = 52.0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -