kword

KWTableStyleManager.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Nash Hoogwater <nrhoogwater@wanadoo.nl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; using
00007    version 2 of the License.
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 "KWTableStyleManager.h"
00021 #include "KWTableStyleManager.moc"
00022 #include "KWImportStyleDia.h"
00023 
00024 #include "KWDocument.h"
00025 
00026 #include <KoParagCounter.h>
00027 #include <KoTextDocument.h>
00028 
00029 #include "KoParagStyle.h"
00030 #include "KWFrameStyleManager.h"
00031 #include "KWStyleManager.h"
00032 
00033 #include <klocale.h>
00034 #include <kiconloader.h>
00035 #include <kdebug.h>
00036 
00037 #include <qpushbutton.h>
00038 #include <qlabel.h>
00039 #include <qgroupbox.h>
00040 #include <qpainter.h>
00041 #include <qlineedit.h>
00042 #include <qlayout.h>
00043 #include <qcombobox.h>
00044 #include <qframe.h>
00045 
00046 
00047 /******************************************************************/
00048 /* Class: KWTableStylePreview                                     */
00049 /******************************************************************/
00050 
00051 KWTableStylePreview::KWTableStylePreview( const QString& title, const QString& text, QWidget* parent, const char* name )
00052     : QGroupBox( title, parent, name )
00053 {
00054     m_zoomHandler = new KoTextZoomHandler;
00055     QFont font = KoGlobal::defaultFont();
00056     m_textdoc = new KoTextDocument( m_zoomHandler, new KoTextFormatCollection( font, QColor(),KGlobal::locale()->language(), false ) );
00057     KoTextParag * parag = m_textdoc->firstParag();
00058     parag->insert( 0, text );
00059 }
00060 
00061 KWTableStylePreview::~KWTableStylePreview()
00062 {
00063     delete m_textdoc;
00064     delete m_zoomHandler;
00065 }
00066 
00067 #undef ptToPx
00068 #define ptToPx qRound
00069 
00070 void KWTableStylePreview::drawContents( QPainter *p )
00071 {
00072     p->save();
00073     QRect r = contentsRect();
00074 
00075     int wid = ( r.width() - 40 );
00076     int hei = ( r.height() - 40 );
00077 
00078 
00079     // 1: create document background = white
00080 
00081     p->fillRect( QRect( 10, 20, r.width()-20, r.height() - 20 ), QColor("white") );
00082 
00083     // 2: create borders (KWFrameStyle)
00084 
00085     if (tableStyle->frameStyle()->topBorder().width()>0) {
00086         p->setPen( KoBorder::borderPen(tableStyle->frameStyle()->topBorder(), ptToPx( tableStyle->frameStyle()->topBorder().width() ), black) ); // Top border
00087         p->drawLine( 20 - int(tableStyle->frameStyle()->leftBorder().width()/2), 30,
00088                      20 + wid + int(tableStyle->frameStyle()->rightBorder().width()/2), 30 );
00089     }
00090     if (tableStyle->frameStyle()->leftBorder().width()>0) {
00091         p->setPen( KoBorder::borderPen(tableStyle->frameStyle()->leftBorder(), ptToPx( tableStyle->frameStyle()->leftBorder().width() ), black) ); // Left border
00092         p->drawLine( 20, 30 - int(tableStyle->frameStyle()->topBorder().width()/2),
00093                      20 , 30 + hei + int(tableStyle->frameStyle()->bottomBorder().width()/2) );
00094     }
00095     if (tableStyle->frameStyle()->bottomBorder().width()>0) {
00096         p->setPen( KoBorder::borderPen(tableStyle->frameStyle()->bottomBorder(), ptToPx( tableStyle->frameStyle()->bottomBorder().width() ), black) ); // Bottom border
00097         p->drawLine( 20 + wid + int(ceil(tableStyle->frameStyle()->rightBorder().width()/2)), 30 + hei,
00098                      20 - int(tableStyle->frameStyle()->leftBorder().width()/2), 30 + hei );
00099     }
00100     if (tableStyle->frameStyle()->rightBorder().width()>0) {
00101         p->setPen( KoBorder::borderPen(tableStyle->frameStyle()->rightBorder(), ptToPx( tableStyle->frameStyle()->rightBorder().width() ), black) ); // Right border
00102         p->drawLine( 20 + wid, 30 - int(tableStyle->frameStyle()->topBorder().width()/2) ,
00103                      20 + wid, 30 + hei + int(tableStyle->frameStyle()->bottomBorder().width()/2) );
00104     }
00105 
00106    // 3: create background whithin "frame"
00107 
00108     QRect fr( QPoint(20 + int(ceil(tableStyle->frameStyle()->leftBorder().width()/2)), 30 + int(ceil(tableStyle->frameStyle()->topBorder().width()/2))),
00109               QPoint(20 + wid - int(floor(tableStyle->frameStyle()->rightBorder().width()/2)+1), 30 + hei - int(floor(tableStyle->frameStyle()->bottomBorder().width()/2)+1)) );
00110 
00111     p->fillRect( fr, tableStyle->frameStyle()->backgroundColor() );
00112 
00113    // 4: create text (KoParagStyle)
00114 
00115     KoTextParag * parag = m_textdoc->firstParag();
00116     int widthLU = m_zoomHandler->pixelToLayoutUnitX( fr.width() - 2 ); // keep one pixel border horizontally
00117     if ( m_textdoc->width() != widthLU )
00118     {
00119         // For centering to work, and to even get word wrapping when the thing is too big :)
00120         m_textdoc->setWidth( widthLU );
00121         parag->invalidate(0);
00122     }
00123 
00124     parag->format();
00125     QRect textRect = parag->pixelRect( m_zoomHandler );
00126 
00127     // Center vertically, but not horizontally, to keep the parag alignment working,
00128     textRect.moveTopLeft( QPoint( fr.x(), fr.y() + ( fr.height() - textRect.height() ) / 2 ) );
00129 
00130     p->setClipRect( textRect.intersect( fr ) );
00131     p->translate( textRect.x(), textRect.y() );
00132 
00133     QColorGroup cg = QApplication::palette().active();
00134     cg.setBrush( QColorGroup::Base, tableStyle->frameStyle()->backgroundColor() );
00135 
00136     m_textdoc->drawWYSIWYG( p, 1, 0, textRect.width() - 1, textRect.height(), cg, m_zoomHandler );
00137 
00138     p->restore();
00139 }
00140 
00141 void KWTableStylePreview::setTableStyle( KWTableStyle *_tableStyle )
00142 {
00143     tableStyle = _tableStyle;
00144 
00145     KoTextParag * parag = m_textdoc->firstParag();
00146     parag->applyStyle( tableStyle->paragraphStyle() );
00147 
00148     repaint(true);
00149 }
00150 
00151 /******************************************************************/
00152 /* Class: KWTableStyleListItem                                    */
00153 /******************************************************************/
00154 
00155 KWTableStyleListItem::~KWTableStyleListItem()
00156 {
00157 }
00158 
00159 void KWTableStyleListItem::switchStyle()
00160 {
00161     delete m_changedTableStyle;
00162 
00163     if ( m_origTableStyle )
00164         m_changedTableStyle = new KWTableStyle( *m_origTableStyle );
00165 }
00166 
00167 void KWTableStyleListItem::deleteStyle( KWTableStyle *current )
00168 {
00169     Q_ASSERT( m_changedTableStyle == current );
00170     delete m_changedTableStyle;
00171     m_changedTableStyle = 0L;
00172 }
00173 
00174 void KWTableStyleListItem::apply()
00175 {
00176     *m_origTableStyle = *m_changedTableStyle;
00177 }
00178 
00179 /******************************************************************/
00180 /* Class: KWTableStyleManager                                     */
00181 /******************************************************************/
00182 
00183 // Proof reader comment: stylist sounds like a hair dresser
00184 
00185 KWTableStyleManager::KWTableStyleManager( QWidget *_parent, KWDocument *_doc )
00186     : KDialogBase( _parent, "Tablestylist", true,
00187                    i18n("Table Style Manager"),
00188                    KDialogBase::Ok | KDialogBase::Cancel | KDialogBase::Apply | KDialogBase::User1 )
00189 {
00190     m_doc = _doc;
00191 
00192     m_currentTableStyle = 0L;
00193     noSignals=true;
00194 
00195     m_tableStyles.setAutoDelete(false);
00196 
00197     setupWidget(); // build the widget with the buttons and the list selector.
00198 
00199     m_stylesList->setCurrentItem( 0 );
00200     noSignals=false;
00201     switchStyle();
00202     setInitialSize( QSize( 450, 450 ) );
00203     setButtonText( KDialogBase::User1, i18n("Import From File...") );
00204     connect(this, SIGNAL(user1Clicked()), this, SLOT(importFromFile()));
00205 
00206 }
00207 
00208 KWTableStyleManager::~KWTableStyleManager()
00209 {
00210     m_tableStyles.setAutoDelete( true );
00211     m_tableStyles.clear();
00212 }
00213 
00214 void KWTableStyleManager::setupWidget()
00215 {
00216     QFrame * frame1 = makeMainWidget();
00217     QGridLayout *frame1Layout = new QGridLayout( frame1, 0, 0, // auto
00218                                                  0, KDialog::spacingHint() );
00219 
00220 
00221     KWTableStyleCollection* collection = m_doc->tableStyleCollection();
00222     numTableStyles = collection->count();
00223     m_stylesList = new QListBox( frame1, "stylesList" );
00224     m_stylesList->insertStringList( collection->displayNameList() );
00225     const QValueList<KoUserStyle*> styleList = collection->styleList();
00226     Q_ASSERT( !styleList.isEmpty() );
00227     for ( QValueList<KoUserStyle *>::const_iterator it = styleList.begin(), end = styleList.end();
00228           it != end ; ++it )
00229     {
00230         KWTableStyle* style = static_cast<KWTableStyle *>( *it );
00231         m_tableStyles.append( new KWTableStyleListItem(style,new KWTableStyle(*style) ) );
00232         m_styleOrder << style->name();
00233     }
00234     Q_ASSERT( m_stylesList->count() == m_styleOrder.count() );
00235     Q_ASSERT( m_styleOrder.count() == m_tableStyles.count() );
00236 
00237     frame1Layout->addMultiCellWidget( m_stylesList, 0, 0, 0, 1 );
00238 
00239 
00240     m_moveUpButton = new QPushButton( frame1, "moveUpButton" );
00241     m_moveUpButton->setIconSet( SmallIconSet( "up" ) );
00242     connect( m_moveUpButton, SIGNAL( clicked() ), this, SLOT( moveUpStyle() ) );
00243     frame1Layout->addWidget( m_moveUpButton, 1, 1 );
00244 
00245     m_moveDownButton = new QPushButton( frame1, "moveDownButton" );
00246     m_moveDownButton->setIconSet( SmallIconSet( "down" ) );
00247     connect( m_moveDownButton, SIGNAL( clicked() ), this, SLOT( moveDownStyle() ) );
00248     frame1Layout->addWidget( m_moveDownButton, 1, 0 );
00249 
00250 
00251     m_deleteButton = new QPushButton( frame1, "deleteButton" );
00252     m_deleteButton->setText( i18n( "&Delete" ) );
00253     connect( m_deleteButton, SIGNAL( clicked() ), this, SLOT( deleteStyle() ) );
00254 
00255     frame1Layout->addWidget( m_deleteButton, 2, 1 );
00256 
00257     m_newButton = new QPushButton( frame1, "newButton" );
00258     m_newButton->setText( i18n( "New" ) );
00259     connect( m_newButton, SIGNAL( clicked() ), this, SLOT( addStyle() ) );
00260 
00261     frame1Layout->addWidget( m_newButton, 2, 0 );
00262 
00263     main = new QWidget( frame1 );
00264 
00265     setupMain();
00266 
00267     frame1Layout->addMultiCellWidget( main, 0, 2, 2, 2 );
00268 
00269     connect( m_stylesList, SIGNAL( selectionChanged() ), this, SLOT( switchStyle() ) );
00270 }
00271 
00272 void KWTableStyleManager::setupMain()
00273 {
00274     QGridLayout *mainLayout = new QGridLayout( main );
00275     mainLayout->setSpacing( KDialog::spacingHint() );
00276 
00277     preview = new KWTableStylePreview( i18n("Preview"), i18n("Tablestyles preview"), main );
00278     preview->resize(preview->sizeHint());
00279 
00280     mainLayout->addMultiCellWidget( preview, 1, 1, 0, 1 );
00281 
00282     m_nameString = new QLineEdit( main );
00283     m_nameString->resize(m_nameString->sizeHint() );
00284     connect( m_nameString, SIGNAL( textChanged( const QString &) ), this, SLOT( renameStyle(const QString &) ) );
00285 
00286     mainLayout->addWidget( m_nameString, 0, 1 );
00287 
00288     QLabel *nameLabel = new QLabel( main );
00289     nameLabel->setText( i18n( "Name:" ) );
00290     nameLabel->resize(nameLabel->sizeHint());
00291     nameLabel->setAlignment( AlignRight | AlignVCenter );
00292 
00293     mainLayout->addWidget( nameLabel, 0, 0 );
00294 
00295     QGroupBox *adjustBox = new QGroupBox( 0, Qt::Vertical, i18n("Adjust"), main);
00296     adjustBox->layout()->setSpacing(KDialog::spacingHint());
00297     adjustBox->layout()->setMargin(KDialog::marginHint());
00298     QGridLayout *adjustLayout = new QGridLayout( adjustBox->layout() );
00299 
00300     QLabel *frameStyleLabel = new QLabel( adjustBox );
00301     frameStyleLabel->setText( i18n( "Framestyle:" ) );
00302     frameStyleLabel->setAlignment( AlignRight | AlignVCenter );
00303 
00304     QLabel *styleLabel = new QLabel( adjustBox );
00305     styleLabel->setText( i18n( "Textstyle:" ) );
00306     styleLabel->setAlignment( AlignRight | AlignVCenter );
00307 
00308     m_frameStyle = new QComboBox( adjustBox );
00309     m_style = new QComboBox( adjustBox );
00310     updateAllStyleCombos();
00311 
00312     connect( m_frameStyle, SIGNAL( activated(int) ), this, SLOT( selectFrameStyle(int) ) );
00313     connect( m_style, SIGNAL( activated(int) ), this, SLOT( selectStyle(int) ) );
00314 
00315     m_changeFrameStyleButton = new QPushButton( adjustBox );
00316     m_changeFrameStyleButton->setText( i18n( "Change..." ) );
00317     connect( m_changeFrameStyleButton, SIGNAL( clicked() ), this, SLOT( changeFrameStyle() ) );
00318 
00319     m_changeStyleButton = new QPushButton( adjustBox );
00320     m_changeStyleButton->setText( i18n( "Change..." ) );
00321     connect( m_changeStyleButton, SIGNAL( clicked() ), this, SLOT( changeStyle() ) );
00322 
00323     adjustLayout->addWidget( frameStyleLabel, 0, 0 );
00324     adjustLayout->addWidget( styleLabel, 1, 0 );
00325 
00326     adjustLayout->addWidget( m_frameStyle, 0, 1 );
00327     adjustLayout->addWidget( m_style, 1, 1 );
00328 
00329     adjustLayout->addWidget( m_changeFrameStyleButton, 0, 2 );
00330     adjustLayout->addWidget( m_changeStyleButton, 1, 2 );
00331 
00332     adjustBox->setMaximumHeight(120);
00333     mainLayout->addMultiCellWidget( adjustBox, 2, 2, 0, 1);
00334 }
00335 
00336 void KWTableStyleManager::switchStyle()
00337 {
00338     kdDebug() << "KWTableStyleManager::switchStyle noSignals=" << noSignals << endl;
00339     if(noSignals) return;
00340     noSignals=true;
00341 
00342     if(m_currentTableStyle !=0L)
00343         save();
00344 
00345     m_currentTableStyle = 0L;
00346     int num = tableStyleIndex( m_stylesList->currentItem() );
00347 
00348     kdDebug() << "KWTableStyleManager::switchStyle switching to " << num << endl;
00349     if( m_tableStyles.at(num)->origTableStyle() == m_tableStyles.at(num)->changedTableStyle() )
00350         m_tableStyles.at(num)->switchStyle();
00351     else
00352         m_currentTableStyle = m_tableStyles.at(num)->changedTableStyle();
00353 
00354     updateGUI();
00355 
00356     noSignals=false;
00357 }
00358 
00359 int KWTableStyleManager::tableStyleIndex( int pos )
00360 {
00361     int p = 0;
00362     for(unsigned int i=0; i < m_tableStyles.count(); i++) {
00363         // Skip deleted styles, they're no in m_stylesList anymore
00364         KWTableStyle * style = m_tableStyles.at(i)->changedTableStyle();
00365         if ( !style ) continue;
00366         if ( p == pos )
00367             return i;
00368         ++p;
00369     }
00370     kdWarning() << "KWTableStyleManager::tableStyleIndex no style found at pos " << pos << endl;
00371 
00372 #ifdef __GNUC_
00373 #warning implement undo/redo
00374 #endif
00375 
00376     return 0;
00377 }
00378 
00379 void KWTableStyleManager::updateGUI()
00380 {
00381     kdDebug() << "KWTableStyleManager::updateGUI m_currentTableStyle=" << m_currentTableStyle << " " << m_currentTableStyle->name() << endl;
00382 
00383     // Update name
00384     m_nameString->setText(m_currentTableStyle->displayName());
00385     // Update style and framestyle
00386     if ( m_doc->styleCollection()->findStyle( m_currentTableStyle->paragraphStyle()->name() ) )
00387         m_style->setCurrentText(m_currentTableStyle->paragraphStyle()->displayName());
00388     if ( m_doc->frameStyleCollection()->findStyle( m_currentTableStyle->frameStyle()->name() ) )
00389         m_frameStyle->setCurrentText(m_currentTableStyle->frameStyle()->displayName());
00390 
00391     // update delete button (can't delete first style);
00392     m_deleteButton->setEnabled(m_stylesList->currentItem() != 0);
00393 
00394     m_moveUpButton->setEnabled(m_stylesList->currentItem() != 0);
00395     m_moveDownButton->setEnabled(m_stylesList->currentItem()!=(int)m_stylesList->count()-1);
00396 
00397     updatePreview();
00398 }
00399 
00400 void KWTableStyleManager::updatePreview()
00401 {
00402     preview->setTableStyle(m_currentTableStyle);
00403 }
00404 
00405 void KWTableStyleManager::save()
00406 {
00407     m_currentTableStyle->setDisplayName( m_nameString->text() );
00408 }
00409 
00410 void KWTableStyleManager::addStyle()
00411 {
00412     save();
00413     KWTableStyleCollection* collection = m_doc->tableStyleCollection();
00414 
00415     QString str = i18n( "New Tablestyle Template (%1)" ).arg(numTableStyles++);
00416     if ( m_currentTableStyle )
00417     {
00418         m_currentTableStyle = new KWTableStyle( *m_currentTableStyle );
00419         m_currentTableStyle->setDisplayName( str );
00420     }
00421     else
00422     {
00423         KWTableStyle* defaultTableStyle = collection->findStyle( collection->defaultStyleName() );
00424         Q_ASSERT( defaultTableStyle ); // can't be 0 except if there are no styles at all
00425 
00426         KWFrameStyle *defaultFrameStyle = defaultTableStyle->frameStyle();
00427         KoParagStyle *defaultParagraphStyle = defaultTableStyle->paragraphStyle();
00428 
00429         m_currentTableStyle = new KWTableStyle( str, defaultParagraphStyle, defaultFrameStyle );
00430     }
00431     m_currentTableStyle->setName( collection->generateUniqueName() );
00432 
00433     noSignals=true;
00434     m_tableStyles.append(new KWTableStyleListItem(0L,m_currentTableStyle));
00435     m_stylesList->insertItem( str );
00436     m_styleOrder << m_currentTableStyle->name();
00437     m_stylesList->setCurrentItem( m_stylesList->count() - 1 );
00438     noSignals=false;
00439 
00440     updateGUI();
00441 }
00442 
00443 void KWTableStyleManager::importFromFile()
00444 {
00445     QStringList lst;
00446     for (unsigned int i = 0; i<m_stylesList->count();i++)
00447     {
00448         lst << m_stylesList->text(i );
00449     }
00450 
00451     KWImportFrameTableStyleDia dia( m_doc, lst, KWImportFrameTableStyleDia::TableStyle, this, 0 );
00452     if ( dia.listOfTableStyleImported().count() > 0 && dia.exec() ) {
00453         QPtrList<KWTableStyle> list = dia.listOfTableStyleImported();
00454         addStyles( list);
00455     }
00456 }
00457 
00458 void KWTableStyleManager::addStyles(const QPtrList<KWTableStyle> &listStyle )
00459 {
00460     save();
00461 
00462     QPtrListIterator<KWTableStyle> style( listStyle );
00463     for ( ; style.current() ; ++style )
00464     {
00465         noSignals=true;
00466         m_stylesList->insertItem( style.current()->displayName() );
00467         m_styleOrder << style.current()->name();
00468         m_tableStyles.append( new KWTableStyleListItem( 0L,new KWTableStyle(*style.current())) );
00469         noSignals=false;
00470 
00471     }
00472 
00473     updateGUI();
00474 }
00475 
00476 
00477 void KWTableStyleManager::deleteStyle()
00478 {
00479     Q_ASSERT( m_currentTableStyle );
00480 
00481     unsigned int cur = tableStyleIndex( m_stylesList->currentItem() );
00482     m_styleOrder.remove( m_currentTableStyle->name() );
00483     if ( !m_tableStyles.at(cur)->origTableStyle() )
00484         m_tableStyles.take( cur );
00485     else {
00486         m_tableStyles.at(cur)->deleteStyle( m_currentTableStyle );
00487         m_currentTableStyle = 0L;
00488     }
00489 
00490     // Update GUI
00491     m_stylesList->removeItem( m_stylesList->currentItem() );
00492     numTableStyles--;
00493     m_stylesList->setSelected( m_stylesList->currentItem(), true );
00494 }
00495 
00496 void KWTableStyleManager::moveUpStyle()
00497 {
00498     Q_ASSERT( m_currentTableStyle );
00499     if ( m_currentTableStyle )
00500         save();
00501 
00502     const QString currentStyleName = m_currentTableStyle->name();
00503     const QString currentStyleDisplayName = m_stylesList->currentText();
00504     int pos2 = m_styleOrder.findIndex( currentStyleName );
00505     if ( pos2 != -1 )
00506     {
00507         m_styleOrder.remove( m_styleOrder.at(pos2) );
00508         m_styleOrder.insert( m_styleOrder.at(pos2-1), currentStyleName );
00509     }
00510 
00511 
00512     int pos = m_stylesList->currentItem();
00513     noSignals=true;
00514     m_stylesList->changeItem( m_stylesList->text( pos-1 ), pos );
00515 
00516     m_stylesList->changeItem( currentStyleDisplayName, pos-1 );
00517 
00518     m_stylesList->setCurrentItem( m_stylesList->currentItem() );
00519     noSignals=false;
00520 
00521     updateGUI();
00522 }
00523 
00524 void KWTableStyleManager::moveDownStyle()
00525 {
00526     Q_ASSERT( m_currentTableStyle );
00527     if ( m_currentTableStyle )
00528         save();
00529 
00530     const QString currentStyleName = m_currentTableStyle->name();
00531     const QString currentStyleDisplayName = m_stylesList->currentText();
00532     int pos2 = m_styleOrder.findIndex( currentStyleName );
00533     if ( pos2 != -1 )
00534     {
00535         m_styleOrder.remove( m_styleOrder.at(pos2) );
00536         m_styleOrder.insert( m_styleOrder.at(pos2+1), currentStyleName );
00537     }
00538 
00539     int pos = m_stylesList->currentItem();
00540     noSignals=true;
00541     m_stylesList->changeItem( m_stylesList->text( pos+1 ), pos );
00542     m_stylesList->changeItem( currentStyleDisplayName, pos+1 );
00543     m_stylesList->setCurrentItem( m_stylesList->currentItem() );
00544     noSignals=false;
00545 
00546     updateGUI();
00547 }
00548 
00549 void KWTableStyleManager::slotOk() {
00550     save();
00551     apply();
00552     KDialogBase::slotOk();
00553 }
00554 
00555 void KWTableStyleManager::slotApply() {
00556     save();
00557     apply();
00558     KDialogBase::slotApply();
00559 }
00560 
00561 void KWTableStyleManager::apply() {
00562     noSignals=true;
00563     for (unsigned int i =0 ; i < m_tableStyles.count() ; i++) {
00564         if(m_tableStyles.at(i)->origTableStyle() == 0) {           // newly added style
00565             kdDebug() << "adding new tablestyle" << m_tableStyles.at(i)->changedTableStyle()->name() << " (" << i << ")" << endl;
00566             KWTableStyle *tmp = m_doc->tableStyleCollection()->addStyle( m_tableStyles.take(i)->changedTableStyle() );
00567             m_tableStyles.insert(i, new KWTableStyleListItem(0, tmp) );
00568         } else if(m_tableStyles.at(i)->changedTableStyle() == 0) { // deleted style
00569             kdDebug() << "deleting orig tablestyle " << m_tableStyles.at(i)->origTableStyle()->name() << " (" << i << ")" << endl;
00570 
00571             KWTableStyle *orig = m_tableStyles.at(i)->origTableStyle();
00572             m_doc->tableStyleCollection()->removeStyle( orig );
00573         } else {
00574             kdDebug() << "update tablestyle " << m_tableStyles.at(i)->changedTableStyle()->name() << " (" << i << ")" << endl;
00575 
00576             m_tableStyles.at(i)->apply();
00577         }
00578     }
00579     m_doc->tableStyleCollection()->updateStyleListOrder( m_styleOrder );
00580     m_doc->updateAllTableStyleLists();
00581     m_doc->setModified( true );
00582     noSignals=false;
00583 }
00584 
00585 void KWTableStyleManager::renameStyle(const QString &theText) {
00586     if(noSignals) return;
00587     noSignals=true;
00588 
00589     int index = m_stylesList->currentItem();
00590     kdDebug() << "KWTableStyleManager::renameStyle " << index << " to " << theText << endl;
00591 
00592     // rename only in the GUI, not even in the underlying objects (save() does it).
00593     m_stylesList->changeItem( theText, index );
00594     //m_styleOrder[index]=theText; // not needed anymore, we use internal names
00595     // Check how many styles with that name we have now
00596     int synonyms = 0;
00597     for ( unsigned int i = 0; i < m_stylesList->count(); i++ ) {
00598         if ( m_stylesList->text( i ) == m_stylesList->currentText() )
00599             ++synonyms;
00600     }
00601     Q_ASSERT( synonyms > 0 ); // should have found 'index' at least !
00602     noSignals=false;
00603     // Can't close the dialog if two styles have the same name
00604     bool state=!theText.isEmpty() && (synonyms == 1);
00605     enableButtonOK(state );
00606     enableButtonApply(state);
00607     enableButton( KDialogBase::User1, state );
00608     m_deleteButton->setEnabled(state&&(m_stylesList->currentItem() != 0));
00609     m_newButton->setEnabled(state);
00610     m_stylesList->setEnabled( state );
00611     if ( state )
00612     {
00613         m_moveUpButton->setEnabled(m_stylesList->currentItem() != 0);
00614         m_moveDownButton->setEnabled(m_stylesList->currentItem()!=(int)m_stylesList->count()-1);
00615     }
00616     else
00617     {
00618         m_moveUpButton->setEnabled(false);
00619         m_moveDownButton->setEnabled(false);
00620     }
00621 
00622 }
00623 
00626 void KWTableStyleManager::changeFrameStyle()
00627 {
00628 // 0. Save name, otherwise it will be gone when you return
00629     save();
00630 
00631 // 1. Execute frame style manager
00632     KWFrameStyleManager frameStylist( this, m_doc, m_currentTableStyle->frameStyle()->name() );
00633     frameStylist.exec();
00634 
00635 // 2. Apply changes
00636     updateAllStyleCombos();
00637     updateGUI();
00638 }
00639 
00642 void KWTableStyleManager::changeStyle()
00643 {
00644 // 0. Save name, otherwise it will be gone when you return
00645     save();
00646 
00647 // 1. Execute stylist
00648     KWStyleManager styleManager( this, m_doc->unit(), m_doc, *m_doc->styleCollection());
00649     styleManager.exec();
00650 
00651 // 2. Apply changes
00652     updateAllStyleCombos();
00653     updateGUI();
00654 }
00655 
00656 void KWTableStyleManager::updateAllStyleCombos()
00657 {
00658     unsigned int oldSindex = 0;
00659     unsigned int oldFSindex = 0;
00660     QString oldS = "";
00661     QString oldFS = "";
00662 
00663     if (m_style->currentItem()>=0) {
00664         oldSindex = m_style->currentItem();
00665         oldS = m_style->currentText();
00666     }
00667     if (m_frameStyle->currentItem()>=0) {
00668         oldFSindex = m_frameStyle->currentItem();
00669         oldFS = m_frameStyle->currentText();
00670     }
00671 
00672     // Let's check if there were deleted framestyles or styles.
00673     // If that is the case than we search for the old name
00674     // If it exists then we use that index, else we use
00675     // the old index.
00676     // Problems:
00677     // - 1. Count is the same, but the user has delete the same number as he added
00678     // - 2. Old name is not in new list, old index is wrong index in new list
00679     if ( ( static_cast<unsigned int>(m_style->count())!=m_doc->styleCollection()->styleList().count() ) &&
00680             ( m_style->listBox()->findItem( oldS ) ) ) {
00681         oldSindex = m_style->listBox()->index( m_style->listBox()->findItem( oldS ) );
00682     }
00683     if ( ( m_frameStyle->count() != m_doc->frameStyleCollection()->count() ) &&
00684             ( m_frameStyle->listBox()->findItem( oldFS ) ) ) {
00685         oldFSindex = m_frameStyle->listBox()->index( m_frameStyle->listBox()->findItem( oldFS ) );
00686     }
00687 
00688     // Update the comboboxes
00689 
00690     m_frameStyle->clear();
00691     m_frameStyle->insertStringList( m_doc->frameStyleCollection()->displayNameList() );
00692     m_frameStyle->setCurrentItem( oldFSindex );
00693 
00694     m_style->clear();
00695     m_style->insertStringList( m_doc->styleCollection()->displayNameList() );
00696     m_style->setCurrentItem( oldSindex );
00697 }
00698 
00699 void KWTableStyleManager::selectFrameStyle(int index)
00700 {
00701     kdDebug() << "KWTableStyleManager::selectFrameStyle index " << index << endl;
00702 
00703     if ( (index>=0) && ( index < (int)m_doc->frameStyleCollection()->count() ) )
00704         m_currentTableStyle->setFrameStyle( m_doc->frameStyleCollection()->frameStyleAt(index) );
00705     save();
00706     updateGUI();
00707 }
00708 
00709 void KWTableStyleManager::selectStyle(int index)
00710 {
00711     kdDebug() << "KWTableStyleManager::selectStyle index " << index << endl;
00712     if ( (index>=0) && ( index < (int)m_doc->styleCollection()->styleList().count() ) )
00713         m_currentTableStyle->setParagraphStyle( m_doc->styleCollection()->styleAt(index) );
00714     save();
00715     updateGUI();
00716 }
KDE Home | KDE Accessibility Home | Description of Access Keys