lib

KoPageLayoutSize.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; version 2.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include <KoPageLayoutDia.h>
00021 #include <KoPageLayoutSize.h>
00022 #include <KoUnit.h>
00023 #include <KoUnitWidgets.h>
00024 
00025 #include <klocale.h>
00026 #include <kiconloader.h>
00027 #include <kmessagebox.h>
00028 #include <kdebug.h>
00029 
00030 #include <qlabel.h>
00031 #include <qlayout.h>
00032 #include <qradiobutton.h>
00033 #include <qhbox.h>
00034 #include <qvgroupbox.h>
00035 #include <qhbuttongroup.h>
00036 
00037 KoPageLayoutSize::KoPageLayoutSize(QWidget *parent, const KoPageLayout& layout, KoUnit::Unit unit,const KoColumns& columns,  bool unitChooser, bool enableBorders)
00038     : QWidget(parent), m_blockSignals(false) {
00039     m_layout = layout;
00040     m_unit = unit;
00041 
00042     QGridLayout *grid1 = new QGridLayout( this, 5, 2, 0, KDialog::spacingHint() );
00043     if ( unitChooser ) {
00044         // ------------- unit _______________
00045         QWidget* unitFrame = new QWidget( this );
00046         grid1->addWidget( unitFrame, 0, 0, Qt::AlignLeft );
00047         QBoxLayout* unitLayout = new QHBoxLayout( unitFrame, 0, KDialog::spacingHint() );
00048 
00049         // label unit
00050         QLabel *lpgUnit = new QLabel( i18n( "Unit:" ), unitFrame );
00051         unitLayout->addWidget( lpgUnit, 0, Qt::AlignRight | Qt::AlignVCenter );
00052 
00053         // combo unit
00054         QComboBox *cpgUnit = new QComboBox( false, unitFrame, "cpgUnit" );
00055         lpgUnit->setBuddy( cpgUnit );
00056         cpgUnit->insertStringList( KoUnit::listOfUnitName() );
00057         cpgUnit->setCurrentItem( unit );
00058         unitLayout->addWidget( cpgUnit, 0, Qt::AlignLeft | Qt::AlignVCenter );
00059         connect( cpgUnit, SIGNAL( activated( int ) ), this, SLOT( setUnitInt( int ) ) );
00060     }
00061     else {
00062         QString str=KoUnit::unitDescription(unit);
00063 
00064         QLabel *lpgUnit = new QLabel( i18n("All values are given in %1.").arg(str), this );
00065         grid1->addWidget( lpgUnit, 0, 0, Qt::AlignLeft );
00066     }
00067 
00068     // -------------- page size -----------------
00069     QVGroupBox *formatFrame = new QVGroupBox( i18n( "Page Size" ), this );
00070     grid1->addWidget( formatFrame, 1, 0 );
00071 
00072     QHBox *formatPageSize = new QHBox( formatFrame );
00073     formatPageSize->setSpacing( KDialog::spacingHint() );
00074 
00075     // label page size
00076     QLabel *lpgFormat = new QLabel( i18n( "&Size:" ), formatPageSize );
00077 
00078     // combo size
00079     cpgFormat = new QComboBox( false, formatPageSize, "cpgFormat" );
00080     cpgFormat->insertStringList( KoPageFormat::allFormats() );
00081     lpgFormat->setBuddy( cpgFormat );
00082     connect( cpgFormat, SIGNAL( activated( int ) ), this, SLOT( formatChanged( int ) ) );
00083 
00084     // spacer
00085     formatPageSize->setStretchFactor( new QWidget( formatPageSize ), 10 );
00086 
00087     QHBox *formatCustomSize = new QHBox( formatFrame );
00088     formatCustomSize->setSpacing( KDialog::spacingHint() );
00089 
00090     // label width
00091     QLabel *lpgWidth = new QLabel( i18n( "&Width:" ), formatCustomSize );
00092 
00093     // linedit width
00094     epgWidth = new KoUnitDoubleSpinBox( formatCustomSize, "Width" );
00095     lpgWidth->setBuddy( epgWidth );
00096     if ( m_layout.format != PG_CUSTOM )
00097         epgWidth->setEnabled( false );
00098     connect( epgWidth, SIGNAL( valueChangedPt(double) ), this, SLOT( widthChanged(double) ) );
00099 
00100     // label height
00101     QLabel *lpgHeight = new QLabel( i18n( "&Height:" ), formatCustomSize );
00102 
00103     // linedit height
00104     epgHeight = new KoUnitDoubleSpinBox( formatCustomSize, "Height" );
00105     lpgHeight->setBuddy( epgHeight );
00106     if ( m_layout.format != PG_CUSTOM )
00107         epgHeight->setEnabled( false );
00108     connect( epgHeight, SIGNAL( valueChangedPt(double ) ), this, SLOT( heightChanged(double) ) );
00109 
00110     // --------------- orientation ---------------
00111     m_orientGroup = new QHButtonGroup( i18n( "Orientation" ), this );
00112     m_orientGroup->setInsideSpacing( KDialog::spacingHint() );
00113     grid1->addWidget( m_orientGroup, 2, 0 );
00114 
00115     QLabel* lbPortrait = new QLabel( m_orientGroup );
00116     lbPortrait->setPixmap( QPixmap( UserIcon( "koPortrait" ) ) );
00117     lbPortrait->setMaximumWidth( lbPortrait->pixmap()->width() );
00118     new QRadioButton( i18n("&Portrait"), m_orientGroup );
00119 
00120     QLabel* lbLandscape = new QLabel( m_orientGroup );
00121     lbLandscape->setPixmap( QPixmap( UserIcon( "koLandscape" ) ) );
00122     lbLandscape->setMaximumWidth( lbLandscape->pixmap()->width() );
00123     new QRadioButton( i18n("La&ndscape"), m_orientGroup );
00124 
00125     connect( m_orientGroup, SIGNAL (clicked (int)), this, SLOT( orientationChanged(int) ));
00126 
00127     // --------------- page margins ---------------
00128     QVGroupBox *marginsFrame = new QVGroupBox( i18n( "Margins" ), this );
00129     marginsFrame->setColumnLayout( 0, Qt::Vertical );
00130     marginsFrame->setMargin( KDialog::marginHint() );
00131     grid1->addWidget( marginsFrame, 3, 0 );
00132 
00133     QGridLayout *marginsLayout = new QGridLayout( marginsFrame->layout(), 3, 3,
00134        KDialog::spacingHint() );
00135 
00136     // left margin
00137     ebrLeft = new KoUnitDoubleSpinBox( marginsFrame, "Left" );
00138     marginsLayout->addWidget( ebrLeft, 1, 0 );
00139     connect( ebrLeft, SIGNAL( valueChangedPt( double ) ), this, SLOT( leftChanged( double ) ) );
00140 
00141     // right margin
00142     ebrRight = new KoUnitDoubleSpinBox( marginsFrame, "Right" );
00143     marginsLayout->addWidget( ebrRight, 1, 2 );
00144     connect( ebrRight, SIGNAL( valueChangedPt( double ) ), this, SLOT( rightChanged( double ) ) );
00145 
00146     // top margin
00147     ebrTop = new KoUnitDoubleSpinBox( marginsFrame, "Top" );
00148     marginsLayout->addWidget( ebrTop, 0, 1 , Qt::AlignCenter );
00149     connect( ebrTop, SIGNAL( valueChangedPt( double ) ), this, SLOT( topChanged( double ) ) );
00150 
00151     // bottom margin
00152     ebrBottom = new KoUnitDoubleSpinBox( marginsFrame, "Bottom" );
00153     marginsLayout->addWidget( ebrBottom, 2, 1, Qt::AlignCenter );
00154     connect( ebrBottom, SIGNAL( valueChangedPt( double ) ), this, SLOT( bottomChanged( double ) ) );
00155 
00156     // ------------- preview -----------
00157     pgPreview = new KoPagePreview( this, "Preview", m_layout );
00158     grid1->addMultiCellWidget( pgPreview, 1, 3, 1, 1 );
00159 
00160     // ------------- spacers -----------
00161     QWidget* spacer1 = new QWidget( this );
00162     QWidget* spacer2 = new QWidget( this );
00163     spacer1->setSizePolicy( QSizePolicy( QSizePolicy::Expanding,
00164        QSizePolicy::Expanding ) );
00165     spacer2->setSizePolicy( QSizePolicy( QSizePolicy::Expanding,
00166        QSizePolicy::Expanding ) );
00167     grid1->addWidget( spacer1, 4, 0 );
00168     grid1->addWidget( spacer2, 4, 1 );
00169 
00170     setValues();
00171     updatePreview();
00172     pgPreview->setPageColumns( columns );
00173     setEnableBorders(enableBorders);
00174 }
00175 
00176 void KoPageLayoutSize::setEnableBorders(bool on) {
00177     m_haveBorders = on;
00178     ebrLeft->setEnabled( on );
00179     ebrRight->setEnabled( on );
00180     ebrTop->setEnabled( on );
00181     ebrBottom->setEnabled( on );
00182 
00183     // update m_layout
00184     m_layout.ptLeft = on?ebrLeft->value():0;
00185     m_layout.ptRight = on?ebrRight->value():0;
00186     m_layout.ptTop = on?ebrTop->value():0;
00187     m_layout.ptBottom = on?ebrBottom->value():0;
00188 
00189     // use updated m_layout
00190     updatePreview();
00191     emit propertyChange(m_layout);
00192 }
00193 
00194 void KoPageLayoutSize::updatePreview() {
00195     pgPreview->setPageLayout( m_layout );
00196 }
00197 
00198 void KoPageLayoutSize::setValues() {
00199     // page format
00200     cpgFormat->setCurrentItem( m_layout.format );
00201     // orientation
00202     m_orientGroup->setButton( m_layout.orientation == PG_PORTRAIT ? 0: 1 );
00203 
00204     setUnit( m_unit );
00205     updatePreview();
00206 }
00207 
00208 void KoPageLayoutSize::setUnit( KoUnit::Unit unit ) {
00209     m_unit = unit;
00210     m_blockSignals = true; // due to non-atomic changes the propertyChange emits should be blocked
00211 
00212     epgWidth->setUnit( m_unit );
00213     epgWidth->setMinMaxStep( 0, KoUnit::fromUserValue( 9999, m_unit ), KoUnit::fromUserValue( 0.01, m_unit ) );
00214     epgWidth->changeValue( m_layout.ptWidth );
00215 
00216     epgHeight->setUnit( m_unit );
00217     epgHeight->setMinMaxStep( 0, KoUnit::fromUserValue( 9999, m_unit ), KoUnit::fromUserValue( 0.01, m_unit ) );
00218     epgHeight->changeValue( m_layout.ptHeight );
00219 
00220     double dStep = KoUnit::fromUserValue( 0.2, m_unit );
00221 
00222     ebrLeft->setUnit( m_unit );
00223     ebrLeft->changeValue( m_layout.ptLeft );
00224     ebrLeft->setMinMaxStep( 0, m_layout.ptWidth, dStep );
00225 
00226     ebrRight->setUnit( m_unit );
00227     ebrRight->changeValue( m_layout.ptRight );
00228     ebrRight->setMinMaxStep( 0, m_layout.ptWidth, dStep );
00229 
00230     ebrTop->setUnit( m_unit );
00231     ebrTop->changeValue( m_layout.ptTop );
00232     ebrTop->setMinMaxStep( 0, m_layout.ptHeight, dStep );
00233 
00234     ebrBottom->setUnit( m_unit );
00235     ebrBottom->changeValue( m_layout.ptBottom );
00236     ebrBottom->setMinMaxStep( 0, m_layout.ptHeight, dStep );
00237 
00238     m_blockSignals = false;
00239 }
00240 
00241 void KoPageLayoutSize::setUnitInt( int unit ) {
00242     setUnit((KoUnit::Unit)unit);
00243 }
00244 
00245 void KoPageLayoutSize::formatChanged( int format ) {
00246     if ( ( KoFormat )format == m_layout.format )
00247         return;
00248     m_layout.format = ( KoFormat )format;
00249     bool enable =  (KoFormat) format == PG_CUSTOM;
00250     epgWidth->setEnabled( enable );
00251     epgHeight->setEnabled( enable );
00252 
00253     if ( m_layout.format != PG_CUSTOM ) {
00254         m_layout.ptWidth = MM_TO_POINT( KoPageFormat::width(
00255                     m_layout.format, m_layout.orientation ) );
00256         m_layout.ptHeight = MM_TO_POINT( KoPageFormat::height(
00257                     m_layout.format, m_layout.orientation ) );
00258     }
00259 
00260     epgWidth->changeValue( m_layout.ptWidth );
00261     epgHeight->changeValue( m_layout.ptHeight );
00262 
00263     updatePreview( );
00264     emit propertyChange(m_layout);
00265 }
00266 
00267 void KoPageLayoutSize::orientationChanged(int which) {
00268     m_layout.orientation = which == 0 ? PG_PORTRAIT : PG_LANDSCAPE;
00269 
00270     // swap dimension
00271     double val = epgWidth->value();
00272     epgWidth->changeValue(epgHeight->value());
00273     epgHeight->changeValue(val);
00274     // and adjust margins
00275     m_blockSignals = true;
00276     val = ebrTop->value();
00277     if(m_layout.orientation == PG_PORTRAIT) { // clockwise
00278         ebrTop->changeValue(ebrRight->value());
00279         ebrRight->changeValue(ebrBottom->value());
00280         ebrBottom->changeValue(ebrLeft->value());
00281         ebrLeft->changeValue(val);
00282     } else { // counter clockwise
00283         ebrTop->changeValue(ebrLeft->value());
00284         ebrLeft->changeValue(ebrBottom->value());
00285         ebrBottom->changeValue(ebrRight->value());
00286         ebrRight->changeValue(val);
00287     }
00288     m_blockSignals = false;
00289 
00290     setEnableBorders(m_haveBorders); // will update preview+emit
00291 }
00292 
00293 void KoPageLayoutSize::widthChanged(double width) {
00294     if(m_blockSignals) return;
00295     m_layout.ptWidth = width;
00296     updatePreview();
00297     emit propertyChange(m_layout);
00298 }
00299 void KoPageLayoutSize::heightChanged(double height) {
00300     if(m_blockSignals) return;
00301     m_layout.ptHeight = height;
00302     updatePreview( );
00303     emit propertyChange(m_layout);
00304 }
00305 void KoPageLayoutSize::leftChanged( double left ) {
00306     if(m_blockSignals) return;
00307     m_layout.ptLeft = left;
00308     updatePreview();
00309     emit propertyChange(m_layout);
00310 }
00311 void KoPageLayoutSize::rightChanged(double right) {
00312     if(m_blockSignals) return;
00313     m_layout.ptRight = right;
00314     updatePreview();
00315     emit propertyChange(m_layout);
00316 }
00317 void KoPageLayoutSize::topChanged(double top) {
00318     if(m_blockSignals) return;
00319     m_layout.ptTop = top;
00320     updatePreview();
00321     emit propertyChange(m_layout);
00322 }
00323 void KoPageLayoutSize::bottomChanged(double bottom) {
00324     if(m_blockSignals) return;
00325     m_layout.ptBottom = bottom;
00326     updatePreview();
00327     emit propertyChange(m_layout);
00328 }
00329 
00330 bool KoPageLayoutSize::queryClose() {
00331     if ( m_layout.ptLeft + m_layout.ptRight > m_layout.ptWidth ) {
00332         KMessageBox::error( this,
00333             i18n("The page width is smaller than the left and right margins."),
00334                             i18n("Page Layout Problem") );
00335         return false;
00336     }
00337     if ( m_layout.ptTop + m_layout.ptBottom > m_layout.ptHeight ) {
00338         KMessageBox::error( this,
00339             i18n("The page height is smaller than the top and bottom margins."),
00340                             i18n("Page Layout Problem") );
00341         return false;
00342     }
00343     return true;
00344 }
00345 
00346 void KoPageLayoutSize::setColumns(KoColumns &columns) {
00347     pgPreview->setPageColumns(columns);
00348 }
00349 
00350 #include <KoPageLayoutSize.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys