kpresenter

KPrPropertyEditor.cpp

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2005 Thorsten Zachmann <zachmann@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 #include "KPrPropertyEditor.h"
00021 
00022 #include "KPrObject.h"
00023 #include "KPrPage.h"
00024 #include "KPrDocument.h"
00025 #include "KPrUtils.h"
00026 #include "KPrCommand.h"
00027 #include "KPrPenStyleWidget.h"
00028 #include "KPrBrushProperty.h"
00029 #include "KPrRectProperty.h"
00030 #include "KPrPolygonProperty.h"
00031 #include "KPrPieProperty.h"
00032 #include "KPrPictureProperty.h"
00033 #include "KPrTextProperty.h"
00034 #include "KPrObjectProperties.h"
00035 
00036 #include <klocale.h>
00037 #include <kstdguiitem.h>
00038 
00039 KPrPropertyEditor::KPrPropertyEditor( QWidget *parent, const char *name, KPrPage *page, KPrDocument *doc )
00040     : QTabDialog( parent, name, true )
00041     , m_page( page )
00042     , m_doc( doc )
00043     , m_objects( page->getSelectedObjects() )
00044     , m_penProperty( 0 )
00045     , m_brushProperty( 0 )
00046     , m_rectProperty( 0 )
00047     , m_polygonProperty( 0 )
00048     , m_pieProperty( 0 )
00049     , m_pictureProperty( 0 )
00050     , m_textProperty( 0 )
00051     , m_generalProperty( 0 )
00052     , m_objectProperties( 0 )
00053 {
00054     setCancelButton( KStdGuiItem::cancel().text() );
00055     setOkButton( KStdGuiItem::ok().text() );
00056     setApplyButton( KStdGuiItem::apply().text() );
00057 
00058     connect( this, SIGNAL( applyButtonPressed() ), this, SLOT( slotDone() ) );
00059 
00060     m_objectProperties = new KPrObjectProperties( m_objects );
00061 
00062     setupTabs();
00063 }
00064 
00065 
00066 KPrPropertyEditor::~KPrPropertyEditor()
00067 {
00068     delete m_objectProperties;
00069 }
00070 
00071 
00072 KCommand * KPrPropertyEditor::getCommand()
00073 {
00074     KMacroCommand *macro = 0;
00075 
00076     if ( m_penProperty )
00077     {
00078         int change = m_penProperty->getPenConfigChange();
00079         if ( change )
00080         {
00081             KoPenCmd::Pen pen( m_penProperty->getPen() );
00082 
00083             KoPenCmd *cmd = new KoPenCmd( i18n( "Apply Styles" ), m_objects, pen, m_doc, m_page, change );
00084 
00085             if ( !macro )
00086             {
00087                 macro = new KMacroCommand( i18n( "Apply Properties" ) );
00088             }
00089 
00090             macro->addCommand( cmd );
00091         }
00092     }
00093 
00094     if ( m_brushProperty )
00095     {
00096         int change = m_brushProperty->getBrushPropertyChange();
00097         if ( change )
00098         {
00099             KPrBrushCmd::Brush brush( m_brushProperty->getBrush() );
00100 
00101             KPrBrushCmd *cmd = new KPrBrushCmd( i18n( "Apply Styles" ), m_objects, brush, m_doc, m_page, change );
00102 
00103             if ( !macro )
00104             {
00105                 macro = new KMacroCommand( i18n( "Apply Properties" ) );
00106             }
00107 
00108             macro->addCommand( cmd );
00109         }
00110     }
00111 
00112     if ( m_rectProperty )
00113     {
00114         int change = m_rectProperty->getRectPropertyChange();
00115 
00116         if ( change )
00117         {
00118             KPrRectValueCmd::RectValues rectValue( m_rectProperty->getRectValues() );
00119 
00120             KPrRectValueCmd *cmd = new KPrRectValueCmd( i18n( "Apply Styles" ), m_objects, rectValue, m_doc, m_page, change );
00121 
00122             if ( !macro )
00123             {
00124                 macro = new KMacroCommand( i18n( "Apply Properties" ) );
00125             }
00126 
00127             macro->addCommand( cmd );
00128         }
00129     }
00130 
00131     if ( m_polygonProperty )
00132     {
00133         int change = m_polygonProperty->getPolygonPropertyChange();
00134 
00135         if ( change )
00136         {
00137             KPrPolygonSettingCmd::PolygonSettings polygonSettings( m_polygonProperty->getPolygonSettings() );
00138 
00139             KPrPolygonSettingCmd *cmd = new KPrPolygonSettingCmd( i18n("Apply Styles"), polygonSettings,
00140                                                             m_objects, m_doc, m_page, change );
00141 
00142             if ( !macro )
00143             {
00144                 macro = new KMacroCommand( i18n( "Apply Properties" ) );
00145             }
00146 
00147             macro->addCommand( cmd );
00148         }
00149     }
00150 
00151     if ( m_pieProperty )
00152     {
00153         int change = m_pieProperty->getPiePropertyChange();
00154 
00155         if ( change )
00156         {
00157             KPrPieValueCmd *cmd = new KPrPieValueCmd( i18n("Apply Styles"), m_pieProperty->getPieValues(),
00158                                                 m_objects, m_doc, m_page, change );
00159             if ( !macro )
00160             {
00161                 macro = new KMacroCommand( i18n( "Apply Properties" ) );
00162             }
00163 
00164             macro->addCommand( cmd );
00165         }
00166     }
00167 
00168     if ( m_pictureProperty )
00169     {
00170         int change = m_pictureProperty->getPicturePropertyChange();
00171 
00172         if ( change )
00173         {
00174             KPrPictureSettingCmd *cmd = new KPrPictureSettingCmd( i18n("Apply Styles"), m_pictureProperty->getPictureSettings(),
00175                                                             m_objects, m_doc, m_page, change );
00176             if ( !macro )
00177             {
00178                 macro = new KMacroCommand( i18n( "Apply Properties" ) );
00179             }
00180 
00181             macro->addCommand( cmd );
00182         }
00183     }
00184 
00185     if ( m_textProperty )
00186     {
00187         int change = m_textProperty->getTextPropertyChange();
00188 
00189         if ( change )
00190         {
00191             if ( change & KPrTextProperty::ProtectContent )
00192             {
00193                 KPrProtectContentCommand * cmd = new KPrProtectContentCommand( i18n( "Apply Styles" ), m_objects,
00194                                                                                m_textProperty->getProtectContent(),
00195                                                                                m_doc );
00196                 if ( !macro )
00197                 {
00198                     macro = new KMacroCommand( i18n( "Apply Properties" ) );
00199                 }
00200 
00201                 macro->addCommand( cmd );
00202             }
00203 
00204             if ( change & KPrTextProperty::Margins )
00205             {
00206                 KPrChangeMarginCommand *cmd = new KPrChangeMarginCommand( i18n( "Apply Styles" ), m_objects,
00207                                                                           m_textProperty->getMarginsStruct(),
00208                                                                           m_doc, m_page );
00209 
00210                 if ( !macro )
00211                 {
00212                     macro = new KMacroCommand( i18n( "Apply Properties" ) );
00213                 }
00214 
00215                 macro->addCommand( cmd );
00216             }
00217         }
00218     }
00219 
00220     if ( m_generalProperty )
00221     {
00222         int change = m_generalProperty->getGeneralPropertyChange();
00223 
00224         if ( change )
00225         {
00226             KPrGeneralProperty::GeneralValue generalValue( m_generalProperty->getGeneralValue() );
00227 
00228             if ( change & KPrGeneralProperty::Name )
00229             {
00230                 KCommand *cmd = new KPrNameObjectCommand( i18n( "Name Object" ), generalValue.m_name,
00231                                                           m_objects.at( 0 ), m_doc );
00232 
00233                 if ( !macro )
00234                 {
00235                     macro = new KMacroCommand( i18n( "Apply Properties" ) );
00236                 }
00237 
00238                 macro->addCommand( cmd );
00239             }
00240 
00241             if ( change & KPrGeneralProperty::Protect )
00242             {
00243                 KCommand *cmd= new KPrGeometryPropertiesCommand( i18n( "Protect Object" ), m_objects,
00244                                                                  generalValue.m_protect == STATE_ON,
00245                                                                  KPrGeometryPropertiesCommand::ProtectSize,m_doc );
00246 
00247                 if ( !macro )
00248                 {
00249                     macro = new KMacroCommand( i18n( "Apply Properties" ) );
00250                 }
00251 
00252                 macro->addCommand( cmd );
00253             }
00254 
00255             if ( change & KPrGeneralProperty::KeepRatio )
00256             {
00257                 KCommand *cmd= new KPrGeometryPropertiesCommand( i18n( "Keep Ratio" ), m_objects,
00258                                                                  generalValue.m_keepRatio == STATE_ON,
00259                                                                  KPrGeometryPropertiesCommand::KeepRatio,m_doc );
00260                 if ( !macro )
00261                 {
00262                     macro = new KMacroCommand( i18n( "Apply Properties" ) );
00263                 }
00264 
00265                 macro->addCommand( cmd );
00266             }
00267 
00268             if ( change & KPrGeneralProperty::Left
00269                  || change & KPrGeneralProperty::Top
00270                  || change & KPrGeneralProperty::Width
00271                  || change & KPrGeneralProperty::Height )
00272             {
00273                 if ( !macro )
00274                 {
00275                     macro = new KMacroCommand( i18n( "Apply Properties" ) );
00276                 }
00277 
00278                 QPtrListIterator<KPrObject> it( m_objects );
00279                 for ( ; it.current(); ++it )
00280                 {
00281                     KoRect oldRect = it.current()->getRect();
00282                     KoRect newRect = oldRect;
00283                     if ( change & KPrGeneralProperty::Left )
00284                         newRect.moveTopLeft( KoPoint( generalValue.m_rect.left(), newRect.top() ) );
00285 
00286                     if ( change & KPrGeneralProperty::Top )
00287                         newRect.moveTopLeft( KoPoint( newRect.left(), generalValue.m_rect.top() ) );
00288 
00289                     if ( change & KPrGeneralProperty::Width )
00290                         newRect.setWidth( generalValue.m_rect.width() );
00291 
00292                     if ( change & KPrGeneralProperty::Height )
00293                         newRect.setHeight( generalValue.m_rect.height() );
00294 
00295                     KCommand *cmd = new KPrResizeCmd( i18n( "Change Size" ),
00296                                                    newRect.topLeft() - oldRect.topLeft(),
00297                                                    newRect.size() - oldRect.size(),
00298                                                    it.current(), m_doc );
00299 
00300                     macro->addCommand( cmd );
00301                 }
00302             }
00303         }
00304     }
00305 
00306     return macro;
00307 }
00308 
00309 
00310 void KPrPropertyEditor::setupTabs()
00311 {
00312     setupTabGeneral();
00313 
00314     int flags = m_objectProperties->getPropertyFlags();
00315 
00316     if ( flags & KPrObjectProperties::PtPen )
00317         setupTabPen( flags & KPrObjectProperties::PtLineEnds );
00318 
00319     if ( flags & KPrObjectProperties::PtBrush )
00320         setupTabBrush();
00321 
00322     if ( flags & KPrObjectProperties::PtRectangle )
00323         setupTabRect();
00324 
00325     if ( flags & KPrObjectProperties::PtPolygon )
00326         setupTabPolygon();
00327 
00328     if ( flags & KPrObjectProperties::PtPie )
00329         setupTabPie();
00330 
00331     if ( flags & KPrObjectProperties::PtPicture )
00332         setupTabPicture();
00333 
00334     if ( flags & KPrObjectProperties::PtText )
00335         setupTabText();
00336 }
00337 
00338 
00339 void KPrPropertyEditor::setupTabPen( bool configureLineEnds )
00340 {
00341     if ( m_penProperty == 0 )
00342     {
00343         KoPenCmd::Pen pen( m_objectProperties->getPen() );
00344 
00345         m_penProperty = new KPrPenStyleWidget( this, 0, pen, configureLineEnds );
00346         addTab( m_penProperty, i18n( "Outl&ine" ) );
00347     }
00348 }
00349 
00350 
00351 void KPrPropertyEditor::setupTabBrush()
00352 {
00353     if ( m_brushProperty == 0 )
00354     {
00355         KPrBrushCmd::Brush brush( m_objectProperties->getBrush() );
00356 
00357         m_brushProperty = new KPrBrushProperty( this, 0, brush );
00358         addTab( m_brushProperty, i18n( "&Fill" ) );
00359     }
00360 }
00361 
00362 
00363 void KPrPropertyEditor::setupTabRect()
00364 {
00365     if ( m_rectProperty == 0 )
00366     {
00367         KPrRectValueCmd::RectValues rectValues = m_objectProperties->getRectValues();
00368         m_rectProperty = new KPrRectProperty( this, 0, rectValues );
00369         addTab( m_rectProperty, i18n( "&Rectangle" ) );
00370     }
00371 }
00372 
00373 
00374 void KPrPropertyEditor::setupTabPolygon()
00375 {
00376     if ( m_polygonProperty == 0 )
00377     {
00378         KPrPolygonSettingCmd::PolygonSettings polygonSettings = m_objectProperties->getPolygonSettings();
00379         m_polygonProperty = new KPrPolygonProperty( this, 0, polygonSettings );
00380         addTab( m_polygonProperty, i18n("Polygo&n" ) );
00381     }
00382 }
00383 
00384 
00385 void KPrPropertyEditor::setupTabPie()
00386 {
00387     if ( m_pieProperty == 0 )
00388     {
00389         m_pieProperty = new KPrPieProperty( this, 0, m_objectProperties->getPieValues() );
00390         addTab( m_pieProperty, i18n("&Pie" ) );
00391     }
00392 }
00393 
00394 
00395 void KPrPropertyEditor::setupTabPicture()
00396 {
00397     if ( m_pictureProperty == 0 )
00398     {
00399         m_pictureProperty = new KPrPictureProperty( this, 0, m_objectProperties->getPixmap(), m_objectProperties->getPictureSettings() );
00400         addTab( m_pictureProperty, i18n("Pict&ure" ) );
00401     }
00402 }
00403 
00404 
00405 void KPrPropertyEditor::setupTabText()
00406 {
00407     if ( m_textProperty == 0 )
00408     {
00409         m_textProperty = new KPrTextProperty( this, 0, m_objectProperties->getMarginsStruct(),
00410                                            m_doc->unit(), m_objectProperties->getProtectContent() );
00411         addTab( m_textProperty, i18n("Te&xt" ) );
00412     }
00413 }
00414 
00415 
00416 void KPrPropertyEditor::setupTabGeneral()
00417 {
00418     if ( m_generalProperty == 0 )
00419     {
00420         KPrGeneralProperty::GeneralValue generalValue = getGeneralValue();
00421         m_generalProperty = new KPrGeneralProperty( this, 0, generalValue, m_doc->unit() );
00422         addTab( m_generalProperty, i18n( "&General" ) );
00423     }
00424 }
00425 
00426 
00427 KPrGeneralProperty::GeneralValue KPrPropertyEditor::getGeneralValue()
00428 {
00429     KPrGeneralProperty::GeneralValue generalValue;
00430 
00431     if ( m_objects.count() == 1 )
00432     {
00433         generalValue.m_name = m_objects.at( 0 )->getObjectName();
00434     }
00435 
00436     bool protect = false;
00437     generalValue.m_protect = STATE_OFF;
00438     bool keepRatio = false;
00439     generalValue.m_keepRatio = STATE_OFF;
00440 
00441     QPtrListIterator<KPrObject> it( m_objects );
00442     if ( it.current() )
00443     {
00444         protect = it.current()->isProtect();
00445         generalValue.m_protect = protect ? STATE_ON : STATE_OFF;
00446         keepRatio = it.current()->isKeepRatio();
00447         generalValue.m_keepRatio = keepRatio ? STATE_ON : STATE_OFF;
00448         generalValue.m_rect = it.current()->getRect();
00449         ++it;
00450     }
00451 
00452     for ( ; it.current(); ++it )
00453     {
00454         if ( protect != it.current()->isProtect() )
00455         {
00456             generalValue.m_protect = STATE_UNDEF;
00457             if ( generalValue.m_keepRatio == STATE_UNDEF )
00458             {
00459                 break;
00460             }
00461         }
00462 
00463         if ( keepRatio != it.current()->isKeepRatio() )
00464         {
00465             generalValue.m_keepRatio = STATE_UNDEF;
00466             if ( generalValue.m_protect == STATE_UNDEF )
00467             {
00468                 break;
00469             }
00470         }
00471     }
00472 
00473     return generalValue;
00474 }
00475 
00476 
00477 void KPrPropertyEditor::slotDone()
00478 {
00479     emit propertiesOk();
00480 
00481     if ( m_penProperty )
00482         m_penProperty->apply();
00483     if ( m_brushProperty )
00484         m_brushProperty->apply();
00485     if ( m_rectProperty )
00486         m_rectProperty->apply();
00487     if ( m_polygonProperty )
00488         m_polygonProperty->apply();
00489     if ( m_pieProperty )
00490         m_pieProperty->apply();
00491     if ( m_pictureProperty )
00492         m_pictureProperty->apply();
00493     if ( m_textProperty )
00494         m_textProperty->apply();
00495     if ( m_generalProperty )
00496         m_generalProperty->apply();
00497 }
00498 
00499 #include "KPrPropertyEditor.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys