lib

KoPageLayoutDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00003    Copyright (C) 2005 Thomas Zander <zander@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // Description: Page Layout Dialog (sources)
00022 
00023 /******************************************************************/
00024 
00025 #include <KoPageLayoutDia.h>
00026 #include <KoPageLayoutColumns.h>
00027 #include <KoPageLayoutSize.h>
00028 #include <KoPageLayoutHeader.h>
00029 #include <KoUnit.h>
00030 #include <KoUnitWidgets.h>
00031 
00032 #include <klocale.h>
00033 #include <kiconloader.h>
00034 #include <kmessagebox.h>
00035 
00036 #include <qlabel.h>
00037 #include <qlayout.h>
00038 #include <qpainter.h>
00039 #include <qlineedit.h>
00040 #include <qbuttongroup.h>
00041 #include <qradiobutton.h>
00042 #include <qcheckbox.h>
00043 #include <qhbox.h>
00044 #include <qvgroupbox.h>
00045 #include <qhbuttongroup.h>
00046 
00047 /******************************************************************/
00048 /* class KoPagePreview                                            */
00049 /******************************************************************/
00050 
00051 /*===================== constrcutor ==============================*/
00052 KoPagePreview::KoPagePreview( QWidget* parent, const char *name, const KoPageLayout& layout )
00053     : QGroupBox( i18n( "Page Preview" ), parent, name )
00054 {
00055     setPageLayout( layout );
00056     columns = 1;
00057     setMinimumSize( 150, 150 );
00058 }
00059 
00060 /*====================== destructor ==============================*/
00061 KoPagePreview::~KoPagePreview()
00062 {
00063 }
00064 
00065 /*=================== set layout =================================*/
00066 void KoPagePreview::setPageLayout( const KoPageLayout &layout )
00067 {
00068     // resolution[XY] is in pixel per pt
00069     double resolutionX = POINT_TO_INCH( static_cast<double>(KoGlobal::dpiX()) );
00070     double resolutionY = POINT_TO_INCH( static_cast<double>(KoGlobal::dpiY()) );
00071 
00072     m_pageWidth = layout.ptWidth * resolutionX;
00073     m_pageHeight = layout.ptHeight * resolutionY;
00074 
00075     double zh = 110.0 / m_pageHeight;
00076     double zw = 110.0 / m_pageWidth;
00077     double z = QMIN( zw, zh );
00078 
00079     m_pageWidth *= z;
00080     m_pageHeight *= z;
00081 
00082     m_textFrameX = layout.ptLeft * resolutionX * z;
00083     m_textFrameY = layout.ptTop * resolutionY * z;
00084     m_textFrameWidth = m_pageWidth - ( layout.ptLeft + layout.ptRight ) * resolutionX * z;
00085     m_textFrameHeight = m_pageHeight - ( layout.ptTop + layout.ptBottom ) * resolutionY * z;
00086 
00087     repaint( true );
00088 }
00089 
00090 /*=================== set layout =================================*/
00091 void KoPagePreview::setPageColumns( const KoColumns &_columns )
00092 {
00093     columns = _columns.columns;
00094     repaint( true );
00095 }
00096 
00097 /*======================== draw contents =========================*/
00098 void KoPagePreview::drawContents( QPainter *painter )
00099 {
00100     double cw = m_textFrameWidth;
00101     if(columns!=1)
00102         cw/=static_cast<double>(columns);
00103 
00104     painter->setBrush( white );
00105     painter->setPen( QPen( black ) );
00106 
00107     int x=static_cast<int>( ( width() - m_pageWidth ) * 0.5 );
00108     int y=static_cast<int>( ( height() - m_pageHeight ) * 0.5 );
00109     int w=static_cast<int>(m_pageWidth);
00110     int h=static_cast<int>(m_pageHeight);
00111     //painter->drawRect( x + 1, y + 1, w, h);
00112     painter->drawRect( x, y, w, h );
00113 
00114     painter->setBrush( QBrush( black, HorPattern ) );
00115     if ( m_textFrameWidth == m_pageWidth || m_textFrameHeight == m_pageHeight )
00116         painter->setPen( NoPen );
00117     else
00118         painter->setPen( lightGray );
00119 
00120     for ( int i = 0; i < columns; ++i )
00121         painter->drawRect( x + static_cast<int>(m_textFrameX) + static_cast<int>(i * cw),
00122                            y + static_cast<int>(m_textFrameY), static_cast<int>(cw),
00123                            static_cast<int>(m_textFrameHeight) );
00124 }
00125 
00126 /******************************************************************/
00127 /* class KoPageLayoutDia                                          */
00128 /******************************************************************/
00129 
00130 /*==================== constructor ===============================*/
00131 KoPageLayoutDia::KoPageLayoutDia( QWidget* parent, const char* name,
00132                                   const KoPageLayout& layout,
00133                                   const KoHeadFoot& hf, int tabs,
00134                                   KoUnit::Unit unit, bool modal )
00135     : KDialogBase( KDialogBase::Tabbed, i18n("Page Layout"), KDialogBase::Ok | KDialogBase::Cancel,
00136                    KDialogBase::Ok, parent, name, modal)
00137 {
00138 
00139     flags = tabs;
00140     m_layout = layout;
00141     m_unit = unit;
00142     m_pageSizeTab = 0;
00143     m_columnsTab = 0;
00144     m_headerTab = 0;
00145 
00146     m_column.columns = 1;
00147 
00148     if ( tabs & FORMAT_AND_BORDERS ) setupTab1( true );
00149     if ( tabs & HEADER_AND_FOOTER ) setupTab2( hf );
00150 
00151     setFocusPolicy( QWidget::StrongFocus );
00152     setFocus();
00153 }
00154 
00155 /*==================== constructor ===============================*/
00156 KoPageLayoutDia::KoPageLayoutDia( QWidget* parent, const char* name,
00157                   const KoPageLayout& layout,
00158                   const KoHeadFoot& hf,
00159                   const KoColumns& columns,
00160                   const KoKWHeaderFooter& kwhf,
00161                   int tabs, KoUnit::Unit unit )
00162     : KDialogBase( KDialogBase::Tabbed, i18n("Page Layout"), KDialogBase::Ok | KDialogBase::Cancel,
00163                    KDialogBase::Ok, parent, name, true)
00164 {
00165     flags = tabs;
00166 
00167     m_layout = layout;
00168     m_column = columns;
00169     m_unit = unit;
00170     m_pageSizeTab = 0;
00171     m_columnsTab = 0;
00172     m_headerTab = 0;
00173 
00174     if ( tabs & FORMAT_AND_BORDERS ) setupTab1( !( tabs & DISABLE_BORDERS ) );
00175     if ( tabs & HEADER_AND_FOOTER ) setupTab2( hf );
00176     if ( tabs & COLUMNS ) setupTab3();
00177     if ( tabs & KW_HEADER_AND_FOOTER ) setupTab4(kwhf);
00178 
00179     setFocusPolicy( QWidget::StrongFocus );
00180     setFocus();
00181 }
00182 
00183 /*===================== destructor ===============================*/
00184 KoPageLayoutDia::~KoPageLayoutDia()
00185 {
00186 }
00187 
00188 /*======================= show dialog ============================*/
00189 bool KoPageLayoutDia::pageLayout( KoPageLayout& layout, KoHeadFoot& hf, int tabs, KoUnit::Unit& unit, QWidget* parent )
00190 {
00191     bool res = false;
00192     KoPageLayoutDia *dlg = new KoPageLayoutDia( parent, "PageLayout", layout, hf, tabs, unit );
00193 
00194     if ( dlg->exec() == QDialog::Accepted ) {
00195         res = true;
00196         if ( tabs & FORMAT_AND_BORDERS ) layout = dlg->layout();
00197         if ( tabs & HEADER_AND_FOOTER ) hf = dlg->headFoot();
00198         unit = dlg->unit();
00199     }
00200 
00201     delete dlg;
00202 
00203     return res;
00204 }
00205 
00206 /*======================= show dialog ============================*/
00207 bool KoPageLayoutDia::pageLayout( KoPageLayout& layout, KoHeadFoot& hf, KoColumns& columns,
00208                                   KoKWHeaderFooter &_kwhf, int tabs, KoUnit::Unit& unit, QWidget* parent )
00209 {
00210     bool res = false;
00211     KoPageLayoutDia *dlg = new KoPageLayoutDia( parent, "PageLayout", layout, hf, columns, _kwhf, tabs, unit );
00212 
00213     if ( dlg->exec() == QDialog::Accepted ) {
00214         res = true;
00215         if ( tabs & FORMAT_AND_BORDERS ) layout = dlg->layout();
00216         if ( tabs & HEADER_AND_FOOTER ) hf = dlg->headFoot();
00217         if ( tabs & COLUMNS ) columns = dlg->columns();
00218         if ( tabs & KW_HEADER_AND_FOOTER ) _kwhf = dlg->headerFooter();
00219         unit = dlg->unit();
00220     }
00221 
00222     delete dlg;
00223 
00224     return res;
00225 }
00226 
00227 /*===================== get a standard page layout ===============*/
00228 KoPageLayout KoPageLayoutDia::standardLayout()
00229 {
00230     return KoPageLayout::standardLayout();
00231 }
00232 
00233 /*====================== get header - footer =====================*/
00234 KoHeadFoot KoPageLayoutDia::headFoot() const
00235 {
00236     KoHeadFoot hf;
00237     hf.headLeft = eHeadLeft->text();
00238     hf.headMid = eHeadMid->text();
00239     hf.headRight = eHeadRight->text();
00240     hf.footLeft = eFootLeft->text();
00241     hf.footMid = eFootMid->text();
00242     hf.footRight = eFootRight->text();
00243     return hf;
00244 }
00245 
00246 /*================================================================*/
00247 const KoKWHeaderFooter& KoPageLayoutDia::headerFooter()
00248 {
00249     return m_headerTab->headerFooter();
00250 }
00251 
00252 /*================ setup page size & margins tab ==================*/
00253 void KoPageLayoutDia::setupTab1( bool enableBorders )
00254 {
00255     QWidget *tab1 = addPage(i18n( "Page Size && &Margins" ));
00256     QHBoxLayout *lay = new QHBoxLayout(tab1);
00257     m_pageSizeTab = new KoPageLayoutSize(tab1, m_layout, m_unit, m_column, !(flags & DISABLE_UNIT), enableBorders );
00258     lay->addWidget(m_pageSizeTab);
00259     m_pageSizeTab->show();
00260     connect (m_pageSizeTab, SIGNAL( propertyChange(KoPageLayout&)),
00261             this, SLOT (sizeUpdated( KoPageLayout&)));
00262 }
00263 
00264 void KoPageLayoutDia::sizeUpdated(KoPageLayout &layout) {
00265     m_layout.ptWidth = layout.ptWidth;
00266     m_layout.ptHeight = layout.ptHeight;
00267     m_layout.ptLeft = layout.ptLeft;
00268     m_layout.ptRight = layout.ptRight;
00269     m_layout.ptTop = layout.ptTop;
00270     m_layout.ptBottom = layout.ptBottom;
00271     m_layout.format = layout.format;
00272     m_layout.orientation = layout.orientation;
00273     if(m_columnsTab)
00274         m_columnsTab->setLayout(layout);
00275 }
00276 
00277 /*================ setup header and footer tab ===================*/
00278 void KoPageLayoutDia::setupTab2( const KoHeadFoot& hf )
00279 {
00280     QWidget *tab2 = addPage(i18n( "H&eader && Footer" ));
00281     QGridLayout *grid2 = new QGridLayout( tab2, 7, 2, 0, KDialog::spacingHint() );
00282 
00283     // ------------- header ---------------
00284     QGroupBox *gHead = new QGroupBox( 0, Qt::Vertical, i18n( "Head Line" ), tab2 );
00285     gHead->layout()->setSpacing(KDialog::spacingHint());
00286     gHead->layout()->setMargin(KDialog::marginHint());
00287     QGridLayout *headGrid = new QGridLayout( gHead->layout(), 2, 3 );
00288 
00289     QLabel *lHeadLeft = new QLabel( i18n( "Left:" ), gHead );
00290     headGrid->addWidget( lHeadLeft, 0, 0 );
00291 
00292     eHeadLeft = new QLineEdit( gHead );
00293     headGrid->addWidget( eHeadLeft, 1, 0 );
00294     eHeadLeft->setText( hf.headLeft );
00295 
00296     QLabel *lHeadMid = new QLabel( i18n( "Mid:" ), gHead );
00297     headGrid->addWidget( lHeadMid, 0, 1 );
00298 
00299     eHeadMid = new QLineEdit( gHead );
00300     headGrid->addWidget( eHeadMid, 1, 1 );
00301     eHeadMid->setText( hf.headMid );
00302 
00303     QLabel *lHeadRight = new QLabel( i18n( "Right:" ), gHead );
00304     headGrid->addWidget( lHeadRight, 0, 2 );
00305 
00306     eHeadRight = new QLineEdit( gHead );
00307     headGrid->addWidget( eHeadRight, 1, 2 );
00308     eHeadRight->setText( hf.headRight );
00309 
00310     grid2->addMultiCellWidget( gHead, 0, 1, 0, 1 );
00311 
00312     // ------------- footer ---------------
00313     QGroupBox *gFoot = new QGroupBox( 0, Qt::Vertical, i18n( "Foot Line" ), tab2 );
00314     gFoot->layout()->setSpacing(KDialog::spacingHint());
00315     gFoot->layout()->setMargin(KDialog::marginHint());
00316     QGridLayout *footGrid = new QGridLayout( gFoot->layout(), 2, 3 );
00317 
00318     QLabel *lFootLeft = new QLabel( i18n( "Left:" ), gFoot );
00319     footGrid->addWidget( lFootLeft, 0, 0 );
00320 
00321     eFootLeft = new QLineEdit( gFoot );
00322     footGrid->addWidget( eFootLeft, 1, 0 );
00323     eFootLeft->setText( hf.footLeft );
00324 
00325     QLabel *lFootMid = new QLabel( i18n( "Mid:" ), gFoot );
00326     footGrid->addWidget( lFootMid, 0, 1 );
00327 
00328     eFootMid = new QLineEdit( gFoot );
00329     footGrid->addWidget( eFootMid, 1, 1 );
00330     eFootMid->setText( hf.footMid );
00331 
00332     QLabel *lFootRight = new QLabel( i18n( "Right:" ), gFoot );
00333     footGrid->addWidget( lFootRight, 0, 2 );
00334 
00335     eFootRight = new QLineEdit( gFoot );
00336     footGrid->addWidget( eFootRight, 1, 2 );
00337     eFootRight->setText( hf.footRight );
00338 
00339     grid2->addMultiCellWidget( gFoot, 2, 3, 0, 1 );
00340 
00341     QLabel *lMacros2 = new QLabel( i18n( "You can insert several tags in the text:" ), tab2 );
00342     grid2->addMultiCellWidget( lMacros2, 4, 4, 0, 1 );
00343 
00344     QLabel *lMacros3 = new QLabel( i18n("<qt><ul><li>&lt;sheet&gt; The sheet name</li>"
00345                            "<li>&lt;page&gt; The current page</li>"
00346                            "<li>&lt;pages&gt; The total number of pages</li>"
00347                            "<li>&lt;name&gt; The filename or URL</li>"
00348                            "<li>&lt;file&gt; The filename with complete path or the URL</li></ul></qt>"), tab2 );
00349     grid2->addMultiCellWidget( lMacros3, 5, 6, 0, 0, Qt::AlignTop );
00350 
00351     QLabel *lMacros4 = new QLabel( i18n("<qt><ul><li>&lt;time&gt; The current time</li>"
00352                            "<li>&lt;date&gt; The current date</li>"
00353                            "<li>&lt;author&gt; Your full name</li>"
00354                            "<li>&lt;org&gt; Your organization</li>"
00355                            "<li>&lt;email&gt; Your email address</li></ul></qt>"), tab2 );
00356     grid2->addMultiCellWidget( lMacros4, 5, 6, 1, 1, Qt::AlignTop );
00357 }
00358 
00359 /*================================================================*/
00360 void KoPageLayoutDia::setupTab3()
00361 {
00362     QWidget *tab3 = addPage(i18n( "Col&umns" ));
00363     QHBoxLayout *lay = new QHBoxLayout(tab3);
00364     m_columnsTab = new KoPageLayoutColumns(tab3, m_column, m_unit, m_layout);
00365     m_columnsTab->layout()->setMargin(0);
00366     lay->addWidget(m_columnsTab);
00367     m_columnsTab->show();
00368     connect (m_columnsTab, SIGNAL( propertyChange(KoColumns&)),
00369             this, SLOT (columnsUpdated( KoColumns&)));
00370 }
00371 
00372 void KoPageLayoutDia::columnsUpdated(KoColumns &columns) {
00373     m_column.columns = columns.columns;
00374     m_column.ptColumnSpacing = columns.ptColumnSpacing;
00375     if(m_pageSizeTab)
00376         m_pageSizeTab->setColumns(columns);
00377 }
00378 
00379 /*================================================================*/
00380 void KoPageLayoutDia::setupTab4(const KoKWHeaderFooter kwhf )
00381 {
00382     QWidget *tab4 = addPage(i18n( "H&eader && Footer" ));
00383     QHBoxLayout *lay = new QHBoxLayout(tab4);
00384     m_headerTab = new KoPageLayoutHeader(tab4, m_unit, kwhf);
00385     m_headerTab->layout()->setMargin(0);
00386     lay->addWidget(m_headerTab);
00387     m_headerTab->show();
00388 
00389 }
00390 
00391 
00392 /* Validation when closing. Error messages are never liked, but
00393   better let the users enter all values in any order, and have one
00394   final validation, than preventing them from entering values. */
00395 void KoPageLayoutDia::slotOk()
00396 {
00397     if( m_pageSizeTab )
00398         m_pageSizeTab->queryClose();
00399     KDialogBase::slotOk(); // accept
00400 }
00401 
00402 #include <KoPageLayoutDia.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys