~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to lib/kotext/KoStyleManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
 
 
21
#include "KoStyleCollection.h"
 
22
#include "KoStyleManager.h"
 
23
#include "KoStyleManager.moc"
 
24
#include <KoFontDia.h>
 
25
#include <KoGlobal.h>
 
26
 
 
27
#include <klocale.h>
 
28
#include <kiconloader.h>
 
29
#include <kdebug.h>
 
30
 
 
31
#include <qtabwidget.h>
 
32
#include <qpushbutton.h>
 
33
#include <qlabel.h>
 
34
#include <qcombobox.h>
 
35
#include <qcheckbox.h>
 
36
#include <qlayout.h>
 
37
 
 
38
/******************************************************************/
 
39
/* Class: KoStyleManager                                          */
 
40
/******************************************************************/
 
41
 
 
42
/* keep 2 qlists with the styles.
 
43
   1 of the origs, another with the changed ones (in order of the stylesList)
 
44
   When an orig entry is empty and the other is not, a new one has to be made,
 
45
   when the orig is present and the other is not, the orig has to be deleted.
 
46
   Otherwise all changes are copied from the changed ones to the origs on OK.
 
47
   OK updates the doc if styles are deleted.
 
48
   The dtor frees all the changed ones.
 
49
*/
 
50
/* Months later the above seems SOO stupid.. Just should have created a small class
 
51
   containing the orig and the copy and an enum plus some simple methods..
 
52
   Well; just keep that for those loonly uninspiring days :) (Thomas Z)
 
53
*/
 
54
class KoStyleManagerPrivate
 
55
{
 
56
public:
 
57
    KoStylePreview* preview;
 
58
    QCheckBox* cbIncludeInTOC;
 
59
};
 
60
 
 
61
KoStyleManager::KoStyleManager( QWidget *_parent, KoUnit::Unit unit,
 
62
                                const KoStyleCollection& styles, const QString & activeStyleName,
 
63
                                int flags )
 
64
    : KDialogBase( _parent, "Stylist", true,
 
65
                   i18n("Style Manager"),
 
66
                   KDialogBase::Ok | KDialogBase::Cancel | KDialogBase::Apply )
 
67
{
 
68
    d = new KoStyleManagerPrivate;
 
69
    //setWFlags(getWFlags() || WDestructiveClose);
 
70
    m_currentStyle =0L;
 
71
    noSignals=true;
 
72
    m_origStyles.setAutoDelete(false);
 
73
    m_changedStyles.setAutoDelete(false);
 
74
    setupWidget(styles); // build the widget with the buttons and the list selector.
 
75
    addGeneralTab( flags );
 
76
    KoStyleFontTab * fontTab = new KoStyleFontTab( m_tabs );
 
77
    addTab( fontTab );
 
78
 
 
79
    KoStyleParagTab *newTab = new KoStyleParagTab( m_tabs );
 
80
    newTab->setWidget( new KoIndentSpacingWidget( unit, -1/*no limit*/,newTab ) );
 
81
    addTab( newTab );
 
82
 
 
83
    newTab = new KoStyleParagTab( m_tabs );
 
84
    newTab->setWidget( new KoParagAlignWidget( true, newTab ) );
 
85
    addTab( newTab );
 
86
 
 
87
    newTab = new KoStyleParagTab( m_tabs );
 
88
    KoParagLayoutWidget *decorations = new KoParagDecorationWidget( newTab );
 
89
    decorations->layout()->setMargin(KDialog::marginHint());
 
90
    newTab->setWidget( decorations );
 
91
    addTab( newTab );
 
92
 
 
93
    newTab = new KoStyleParagTab( m_tabs );
 
94
    newTab->setWidget( new KoParagCounterWidget( false , newTab ) );
 
95
    addTab( newTab );
 
96
 
 
97
    newTab = new KoStyleParagTab( m_tabs );
 
98
    newTab->setWidget( new KoParagTabulatorsWidget( unit, -1, newTab ) );
 
99
    addTab( newTab );
 
100
 
 
101
    QListBoxItem * item = m_stylesList->findItem( activeStyleName );
 
102
    m_stylesList->setCurrentItem( item ? m_stylesList->index(item) : 0 );
 
103
 
 
104
    noSignals=false;
 
105
    switchStyle();
 
106
    setInitialSize( QSize( 600, 570 ) );
 
107
}
 
108
 
 
109
KoStyleManager::~KoStyleManager()
 
110
{
 
111
    for (unsigned int i =0 ; m_origStyles.count() > i ; i++) {
 
112
        KoParagStyle *orig = m_origStyles.at(i);
 
113
        KoParagStyle *changed = m_changedStyles.at(i);
 
114
        if( orig && changed && orig != changed ) // modified style, we can delete the changed one now that changes have been applied
 
115
            delete changed;
 
116
    }
 
117
 
 
118
    delete d;
 
119
}
 
120
 
 
121
void KoStyleManager::addTab( KoStyleManagerTab * tab )
 
122
{
 
123
    m_tabsList.append( tab );
 
124
    m_tabs->insertTab( tab, tab->tabName() );
 
125
    tab->layout()->activate();
 
126
}
 
127
 
 
128
void KoStyleManager::setupWidget(const KoStyleCollection& styleCollection)
 
129
{
 
130
    QFrame * frame1 = makeMainWidget();
 
131
    QGridLayout *frame1Layout = new QGridLayout( frame1, 0, 0, // auto
 
132
                                                 0, KDialog::spacingHint() );
 
133
    numStyles = styleCollection.count();
 
134
    m_stylesList = new QListBox( frame1, "stylesList" );
 
135
    m_stylesList->insertStringList( styleCollection.displayNameList() );
 
136
 
 
137
    const QValueList<KoUserStyle*> styleList = styleCollection.styleList();
 
138
    for ( QValueList<KoUserStyle *>::const_iterator it = styleList.begin(), end = styleList.end();
 
139
          it != end ; ++it )
 
140
    {
 
141
        KoParagStyle* style = static_cast<KoParagStyle *>( *it );
 
142
        m_origStyles.append( style );
 
143
        m_changedStyles.append( style );
 
144
        m_styleOrder<< style->name();
 
145
    }
 
146
 
 
147
    frame1Layout->addMultiCellWidget( m_stylesList, 0, 0, 0, 1 );
 
148
 
 
149
 
 
150
    m_moveUpButton = new QPushButton( frame1, "moveUpButton" );
 
151
    m_moveUpButton->setIconSet( SmallIconSet( "up" ) );
 
152
    connect( m_moveUpButton, SIGNAL( clicked() ), this, SLOT( moveUpStyle() ) );
 
153
    frame1Layout->addWidget( m_moveUpButton, 1, 1 );
 
154
 
 
155
    m_moveDownButton = new QPushButton( frame1, "moveDownButton" );
 
156
    m_moveDownButton->setIconSet( SmallIconSet( "down" ) );
 
157
    connect( m_moveDownButton, SIGNAL( clicked() ), this, SLOT( moveDownStyle() ) );
 
158
    frame1Layout->addWidget( m_moveDownButton, 1, 0 );
 
159
 
 
160
 
 
161
    m_deleteButton = new QPushButton( frame1, "deleteButton" );
 
162
    m_deleteButton->setText( i18n( "&Delete" ) );
 
163
    connect( m_deleteButton, SIGNAL( clicked() ), this, SLOT( deleteStyle() ) );
 
164
 
 
165
    frame1Layout->addWidget( m_deleteButton, 2, 1 );
 
166
 
 
167
    m_newButton = new QPushButton( frame1, "newButton" );
 
168
    m_newButton->setText( i18n( "New" ) );
 
169
    connect( m_newButton, SIGNAL( clicked() ), this, SLOT( addStyle() ) );
 
170
 
 
171
    frame1Layout->addWidget( m_newButton, 2, 0 );
 
172
 
 
173
    m_tabs = new QTabWidget( frame1 );
 
174
    frame1Layout->addMultiCellWidget( m_tabs, 0, 2, 2, 2 );
 
175
 
 
176
    connect( m_stylesList, SIGNAL( selectionChanged() ), this, SLOT( switchStyle() ) );
 
177
    connect( m_tabs, SIGNAL( currentChanged ( QWidget * ) ), this, SLOT( switchTabs() ) );
 
178
}
 
179
 
 
180
void KoStyleManager::addGeneralTab( int flags ) {
 
181
    QWidget *tab = new QWidget( m_tabs );
 
182
 
 
183
    QGridLayout *tabLayout = new QGridLayout( tab );
 
184
    tabLayout->setSpacing( KDialog::spacingHint() );
 
185
    tabLayout->setMargin( KDialog::marginHint() );
 
186
 
 
187
    m_nameString = new QLineEdit( tab );
 
188
    m_nameString->resize(m_nameString->sizeHint() );
 
189
    connect( m_nameString, SIGNAL( textChanged( const QString &) ), this, SLOT( renameStyle(const QString &) ) );
 
190
 
 
191
    tabLayout->addWidget( m_nameString, 0, 1 );
 
192
 
 
193
    QLabel *nameLabel = new QLabel( tab );
 
194
    nameLabel->setText( i18n( "Name:" ) );
 
195
    nameLabel->resize(nameLabel->sizeHint());
 
196
    nameLabel->setAlignment( AlignRight | AlignVCenter );
 
197
 
 
198
    tabLayout->addWidget( nameLabel, 0, 0 );
 
199
 
 
200
    m_styleCombo = new QComboBox( FALSE, tab, "styleCombo" );
 
201
 
 
202
    tabLayout->addWidget( m_styleCombo, 1, 1 );
 
203
 
 
204
    QLabel *nextStyleLabel = new QLabel( tab );
 
205
    nextStyleLabel->setText( i18n( "Next style:" ) );
 
206
    nextStyleLabel->setAlignment( AlignRight | AlignVCenter );
 
207
 
 
208
    tabLayout->addWidget( nextStyleLabel, 1, 0 );
 
209
 
 
210
    m_inheritCombo = new QComboBox( FALSE, tab, "inheritCombo" );
 
211
    tabLayout->addWidget( m_inheritCombo, 2, 1 );
 
212
 
 
213
    QLabel *inheritStyleLabel = new QLabel( tab );
 
214
    inheritStyleLabel->setText( i18n( "Inherit style:" ) );
 
215
    inheritStyleLabel->setAlignment( AlignRight | AlignVCenter );
 
216
 
 
217
    tabLayout->addWidget( inheritStyleLabel, 2, 0 );
 
218
 
 
219
    int row = 3;
 
220
 
 
221
    if ( flags & ShowIncludeInToc ) {
 
222
        d->cbIncludeInTOC = new QCheckBox( i18n("Include in table of contents"), tab );
 
223
        tabLayout->addMultiCellWidget( d->cbIncludeInTOC, row, row, 0, 1 );
 
224
        ++row;
 
225
    } else {
 
226
        d->cbIncludeInTOC = 0;
 
227
    }
 
228
 
 
229
    d->preview = new KoStylePreview( i18n( "Preview" ), i18n( "The quick brown fox jumps over the lazy dog. And, what about the cat, one may ask? Well, the cat is playing cards with the mouse, the bird and the fish. It is, to say the least a hell of a party!" ), tab, "stylepreview" );
 
230
 
 
231
    tabLayout->addMultiCellWidget( d->preview, row, row, 0, 1 );
 
232
 
 
233
    m_tabs->insertTab( tab, i18n( "General" ) );
 
234
 
 
235
    m_inheritCombo->insertItem( i18n("<None>"));
 
236
 
 
237
    for ( unsigned int i = 0; i < m_stylesList->count(); i++ ) {
 
238
        m_styleCombo->insertItem( m_stylesList->text(i));
 
239
        m_inheritCombo->insertItem( m_stylesList->text(i));
 
240
    }
 
241
 
 
242
}
 
243
 
 
244
void KoStyleManager::switchStyle() {
 
245
    kdDebug(32500) << "KoStyleManager::switchStyle noSignals=" << noSignals << endl;
 
246
    if(noSignals) return;
 
247
    noSignals=true;
 
248
 
 
249
    if(m_currentStyle !=0L)
 
250
        save();
 
251
 
 
252
    m_currentStyle = 0L;
 
253
    int num = styleIndex( m_stylesList->currentItem() );
 
254
    kdDebug(32500) << "KoStyleManager::switchStyle switching to " << num << endl;
 
255
    if(m_origStyles.at(num) == m_changedStyles.at(num)) {
 
256
        m_currentStyle = new KoParagStyle( *m_origStyles.at(num) );
 
257
        m_changedStyles.take(num);
 
258
        m_changedStyles.insert(num, m_currentStyle);
 
259
    } else {
 
260
        m_currentStyle = m_changedStyles.at(num);
 
261
    }
 
262
    updateGUI();
 
263
 
 
264
    noSignals=false;
 
265
}
 
266
 
 
267
void KoStyleManager::switchTabs()
 
268
{
 
269
    // Called when the user switches tabs
 
270
    // We call save() to update our style, for the preview on the 1st tab
 
271
    save();
 
272
    updatePreview();
 
273
}
 
274
 
 
275
// Return the index of the a style from its position in the GUI
 
276
// (e.g. in m_stylesList or m_styleCombo). This index is used in
 
277
// the m_origStyles and m_changedStyles lists.
 
278
// The reason for the difference is that a deleted style is removed
 
279
// from the GUI but not from the internal lists.
 
280
int KoStyleManager::styleIndex( int pos ) {
 
281
    int p = 0;
 
282
    for(unsigned int i=0; i < m_changedStyles.count(); i++) {
 
283
        // Skip deleted styles, they're no in m_stylesList anymore
 
284
        KoParagStyle * style = m_changedStyles.at(i);
 
285
        if ( !style ) continue;
 
286
        if ( p == pos )
 
287
            return i;
 
288
        ++p;
 
289
    }
 
290
    kdWarning() << "KoStyleManager::styleIndex no style found at pos " << pos << endl;
 
291
 
 
292
#ifdef __GNUC_
 
293
#warning implement undo/redo
 
294
#endif
 
295
 
 
296
    return 0;
 
297
}
 
298
 
 
299
// Update the GUI so that it shows m_currentStyle
 
300
void KoStyleManager::updateGUI() {
 
301
    kdDebug(32500) << "KoStyleManager::updateGUI m_currentStyle=" << m_currentStyle << " " << m_currentStyle->name() << endl;
 
302
    QPtrListIterator<KoStyleManagerTab> it( m_tabsList );
 
303
    for ( ; it.current() ; ++it )
 
304
    {
 
305
        it.current()->setStyle( m_currentStyle );
 
306
        it.current()->update();
 
307
    }
 
308
 
 
309
    m_nameString->setText(m_currentStyle->displayName());
 
310
 
 
311
    QString followingName = m_currentStyle->followingStyle() ? m_currentStyle->followingStyle()->displayName() : QString::null;
 
312
    kdDebug(32500) << "KoStyleManager::updateGUI updating combo to " << followingName << endl;
 
313
    for ( int i = 0; i < m_styleCombo->count(); i++ ) {
 
314
        if ( m_styleCombo->text( i ) == followingName ) {
 
315
            m_styleCombo->setCurrentItem( i );
 
316
            kdDebug(32500) << "found at " << i << endl;
 
317
            break;
 
318
        }
 
319
    }
 
320
 
 
321
    QString inheritName = m_currentStyle->parentStyle() ? m_currentStyle->parentStyle()->displayName() : QString::null;
 
322
    kdDebug(32500) << "KoStyleManager::updateGUI updating combo to " << inheritName << endl;
 
323
    for ( int i = 0; i < m_inheritCombo->count(); i++ ) {
 
324
        if ( m_inheritCombo->text( i ) == inheritName ) {
 
325
            m_inheritCombo->setCurrentItem( i );
 
326
            kdDebug(32500) << "found at " << i << endl;
 
327
            break;
 
328
        }
 
329
        else
 
330
            m_inheritCombo->setCurrentItem( 0 );//none !!!
 
331
    }
 
332
 
 
333
    if ( d->cbIncludeInTOC )
 
334
        d->cbIncludeInTOC->setChecked( m_currentStyle->isOutline() );
 
335
 
 
336
    // update delete button (can't delete first style);
 
337
    m_deleteButton->setEnabled(m_stylesList->currentItem() != 0);
 
338
 
 
339
    m_moveUpButton->setEnabled(m_stylesList->currentItem() != 0);
 
340
    m_moveDownButton->setEnabled(m_stylesList->currentItem()!=(int)m_stylesList->count()-1);
 
341
 
 
342
    updatePreview();
 
343
}
 
344
 
 
345
void KoStyleManager::updatePreview()
 
346
{
 
347
    d->preview->setStyle(m_currentStyle);
 
348
    d->preview->repaint(true);
 
349
}
 
350
 
 
351
void KoStyleManager::save() {
 
352
    if(m_currentStyle) {
 
353
        // save changes from UI to object.
 
354
        QPtrListIterator<KoStyleManagerTab> it( m_tabsList );
 
355
        for ( ; it.current() ; ++it )
 
356
            it.current()->save();
 
357
 
 
358
        // Rename the style - only if it's actually been renamed.
 
359
        if ( m_currentStyle->name() != m_nameString->text() &&
 
360
            m_currentStyle->displayName() != m_nameString->text() )
 
361
        {
 
362
            m_currentStyle->setDisplayName( m_nameString->text() );
 
363
        }
 
364
 
 
365
        int indexNextStyle = styleIndex( m_styleCombo->currentItem() );
 
366
        m_currentStyle->setFollowingStyle( m_origStyles.at( indexNextStyle ) ); // point to orig, not changed! (#47377)
 
367
        m_currentStyle->setParentStyle( style( m_inheritCombo->currentText() ) );
 
368
        if ( d->cbIncludeInTOC )
 
369
            m_currentStyle->setOutline( d->cbIncludeInTOC->isChecked() );
 
370
    }
 
371
}
 
372
 
 
373
KoParagStyle * KoStyleManager::style( const QString & _name )
 
374
{
 
375
    for(unsigned int i=0; i < m_changedStyles.count(); i++) {
 
376
        // Skip deleted styles, they're no in m_stylesList anymore
 
377
        KoParagStyle * style = m_changedStyles.at(i);
 
378
        if ( !style ) continue;
 
379
        if ( style->name() == _name)
 
380
            return style;
 
381
    }
 
382
    return 0;
 
383
}
 
384
 
 
385
QString KoStyleManager::generateUniqueName()
 
386
{
 
387
    int count = 1;
 
388
    QString name;
 
389
    do {
 
390
        name = "new" + QString::number( count++ );
 
391
    } while ( style( name ) );
 
392
    return name;
 
393
}
 
394
 
 
395
 
 
396
void KoStyleManager::addStyle() {
 
397
    save();
 
398
 
 
399
    QString str = i18n( "New Style Template (%1)" ).arg(numStyles++);
 
400
    if ( m_currentStyle )
 
401
    {
 
402
        m_currentStyle = new KoParagStyle( *m_currentStyle ); // Create a new style, initializing from the current one
 
403
        m_currentStyle->setDisplayName( str );
 
404
        m_currentStyle->setName( generateUniqueName() );
 
405
    }
 
406
    else
 
407
        m_currentStyle = new KoParagStyle( str );
 
408
    m_currentStyle->setFollowingStyle( m_currentStyle ); // #45868
 
409
 
 
410
    noSignals=true;
 
411
    m_origStyles.append(0L);
 
412
    m_changedStyles.append(m_currentStyle);
 
413
    m_stylesList->insertItem( str );
 
414
    m_styleCombo->insertItem( str );
 
415
    m_inheritCombo->insertItem( str );
 
416
    m_stylesList->setCurrentItem( m_stylesList->count() - 1 );
 
417
    noSignals=false;
 
418
    m_styleOrder << m_currentStyle->name();
 
419
 
 
420
    updateGUI();
 
421
}
 
422
 
 
423
void KoStyleManager::updateFollowingStyle( KoParagStyle *s )
 
424
{
 
425
    for ( KoParagStyle* p = m_changedStyles.first(); p != 0L; p = m_changedStyles.next() )
 
426
    {
 
427
        if ( p->followingStyle() == s)
 
428
            p->setFollowingStyle(p);
 
429
    }
 
430
 
 
431
}
 
432
 
 
433
void KoStyleManager::updateInheritStyle( KoParagStyle *s )
 
434
{
 
435
    for ( KoParagStyle* p = m_changedStyles.first(); p != 0L; p = m_changedStyles.next() )
 
436
    {
 
437
        //when we remove style, we must replace inherite style to 0L
 
438
        //when parent style was removed.
 
439
        //##########Laurent change inherited style attribute
 
440
        if ( p->parentStyle() == s)
 
441
            p->setParentStyle(0L);
 
442
    }
 
443
 
 
444
}
 
445
 
 
446
void KoStyleManager::deleteStyle() {
 
447
 
 
448
    unsigned int cur = styleIndex( m_stylesList->currentItem() );
 
449
    unsigned int curItem = m_stylesList->currentItem();
 
450
    QString name = m_stylesList->currentText();
 
451
    KoParagStyle *s = m_changedStyles.at(cur);
 
452
    m_styleOrder.remove( s->name());
 
453
    updateFollowingStyle( s );
 
454
    updateInheritStyle( s );
 
455
    Q_ASSERT( s == m_currentStyle );
 
456
    delete s;
 
457
    m_currentStyle = 0L;
 
458
    m_changedStyles.remove(cur);
 
459
    m_changedStyles.insert(cur,0L);
 
460
 
 
461
    // Done with noSignals still false, so that when m_stylesList changes the current item
 
462
    // we display it automatically
 
463
    m_stylesList->removeItem(curItem);
 
464
    m_styleCombo->removeItem(curItem);
 
465
 
 
466
    m_inheritCombo->listBox()->removeItem( m_inheritCombo->listBox()->index(m_inheritCombo->listBox()->findItem(name )));
 
467
 
 
468
    numStyles--;
 
469
    m_stylesList->setSelected( m_stylesList->currentItem(), true );
 
470
}
 
471
 
 
472
void KoStyleManager::moveUpStyle()
 
473
{
 
474
    Q_ASSERT( m_currentStyle );
 
475
    if ( m_currentStyle )
 
476
        save();
 
477
    const QString currentStyleName = m_currentStyle->name();
 
478
    const QString currentStyleDisplayName = m_stylesList->currentText();
 
479
    int pos2 = m_styleOrder.findIndex( currentStyleName );
 
480
    if ( pos2 != -1 )
 
481
    {
 
482
        m_styleOrder.remove( m_styleOrder.at(pos2));
 
483
        m_styleOrder.insert( m_styleOrder.at(pos2-1), currentStyleName);
 
484
    }
 
485
 
 
486
    int pos = m_stylesList->currentItem();
 
487
    noSignals=true;
 
488
    m_stylesList->changeItem( m_stylesList->text( pos-1 ), pos );
 
489
    m_styleCombo->changeItem( m_stylesList->text( pos-1 ), pos );
 
490
 
 
491
    m_stylesList->changeItem( currentStyleDisplayName, pos-1 );
 
492
    m_styleCombo->changeItem( currentStyleDisplayName, pos-1 );
 
493
 
 
494
    m_stylesList->setCurrentItem( m_stylesList->currentItem() );
 
495
    noSignals=false;
 
496
 
 
497
    updateGUI();
 
498
}
 
499
 
 
500
void KoStyleManager::moveDownStyle()
 
501
{
 
502
    Q_ASSERT( m_currentStyle );
 
503
    if ( m_currentStyle )
 
504
        save();
 
505
    const QString currentStyleName = m_currentStyle->name();
 
506
    const QString currentStyleDisplayName = m_stylesList->currentText();
 
507
    int pos2 = m_styleOrder.findIndex( currentStyleName );
 
508
    if ( pos2 != -1 )
 
509
    {
 
510
        m_styleOrder.remove( m_styleOrder.at(pos2));
 
511
        m_styleOrder.insert( m_styleOrder.at(pos2+1), currentStyleName);
 
512
    }
 
513
 
 
514
    int pos = m_stylesList->currentItem();
 
515
    noSignals=true;
 
516
    m_stylesList->changeItem( m_stylesList->text( pos+1 ), pos );
 
517
    m_styleCombo->changeItem( m_stylesList->text( pos+1 ), pos );
 
518
    m_stylesList->changeItem( currentStyleDisplayName, pos+1 );
 
519
    m_styleCombo->changeItem( currentStyleDisplayName, pos+1 );
 
520
    m_stylesList->setCurrentItem( m_stylesList->currentItem() );
 
521
    noSignals=false;
 
522
 
 
523
    updateGUI();
 
524
}
 
525
 
 
526
void KoStyleManager::slotOk() {
 
527
    save();
 
528
    apply();
 
529
    KDialogBase::slotOk();
 
530
}
 
531
 
 
532
void KoStyleManager::slotApply() {
 
533
    save();
 
534
    apply();
 
535
    KDialogBase::slotApply();
 
536
}
 
537
 
 
538
void KoStyleManager::apply() {
 
539
    noSignals=true;
 
540
    KoStyleChangeDefMap styleChanged;
 
541
    QPtrList<KoParagStyle> removeStyle;
 
542
    for (unsigned int i =0 ; m_origStyles.count() > i ; i++) {
 
543
        if(m_origStyles.at(i) == 0L && m_changedStyles.at(i)!=0L) {           // newly added style
 
544
            kdDebug(32500) << "adding new " << m_changedStyles.at(i)->name() << " (" << i << ")" << endl;
 
545
            KoParagStyle *tmp = addStyleTemplate(m_changedStyles.take(i));
 
546
            m_changedStyles.insert(i, tmp);
 
547
        } else if(m_changedStyles.at(i) == 0L && m_origStyles.at(i) != 0L) { // deleted style
 
548
            kdDebug(32500) << "deleting orig " << m_origStyles.at(i)->name() << " (" << i << ")" << endl;
 
549
 
 
550
            KoParagStyle *orig = m_origStyles.at(i);
 
551
            //applyStyleChange( orig, -1, -1 );
 
552
            KoStyleChangeDef tmp( -1,-1);
 
553
            styleChanged.insert( orig, tmp);
 
554
 
 
555
            removeStyle.append( orig );
 
556
            // Note that the style is never deleted (we'll need it for undo/redo purposes)
 
557
 
 
558
        } else if(m_changedStyles.at(i) != 0L && m_origStyles.at(i)!=0L) { // simply updated style
 
559
            kdDebug(32500) << "update style " << m_changedStyles.at(i)->name() << " (" << i << ")" << endl;
 
560
            KoParagStyle *orig = m_origStyles.at(i);
 
561
            KoParagStyle *changed = m_changedStyles.at(i);
 
562
            if ( orig != changed )
 
563
            {
 
564
                int paragLayoutChanged = orig->paragLayout().compare( changed->paragLayout() );
 
565
                int formatChanged = orig->format().compare( changed->format() );
 
566
                //kdDebug(32500) << "old format " << orig->format().key() << " pointsize " << orig->format().pointSizeFloat() << endl;
 
567
                //kdDebug(32500) << "new format " << changed->format().key() << " pointsize " << changed->format().pointSizeFloat() << endl;
 
568
 
 
569
                // Copy everything from changed to orig
 
570
                *orig = *changed;
 
571
 
 
572
                // Apply the change selectively - i.e. only what changed
 
573
                //applyStyleChange( orig, paragLayoutChanged, formatChanged );
 
574
                if ( formatChanged != 0 || paragLayoutChanged != 0 ) {
 
575
                    KoStyleChangeDef tmp(paragLayoutChanged, formatChanged);
 
576
                    styleChanged.insert( orig, tmp );
 
577
                }
 
578
 
 
579
            }
 
580
 
 
581
        }// else
 
582
         //     kdDebug(32500) << "has not changed " <<  m_changedStyles.at(i)->name() << " (" << i << ")" <<  endl;
 
583
    }
 
584
 
 
585
    applyStyleChange( styleChanged );
 
586
 
 
587
    KoParagStyle *tmp = 0L;
 
588
    for ( tmp = removeStyle.first(); tmp ;tmp = removeStyle.next() )
 
589
        removeStyleTemplate( tmp );
 
590
 
 
591
    updateStyleListOrder( m_styleOrder );
 
592
    updateAllStyleLists();
 
593
    noSignals=false;
 
594
}
 
595
 
 
596
void KoStyleManager::renameStyle(const QString &theText) {
 
597
    if(noSignals) return;
 
598
    noSignals=true;
 
599
 
 
600
    int index = m_stylesList->currentItem();
 
601
    kdDebug(32500) << "KoStyleManager::renameStyle " << index << " to " << theText << endl;
 
602
 
 
603
    // rename only in the GUI, not even in the underlying objects (save() does it).
 
604
    kdDebug(32500) << "KoStyleManager::renameStyle before " << m_styleCombo->currentText() << endl;
 
605
    m_styleCombo->changeItem( theText, index );
 
606
    m_inheritCombo->changeItem( theText, index+1 );
 
607
    //m_styleOrder[index]=theText; // not needed anymore, we use internal names
 
608
    kdDebug(32500) << "KoStyleManager::renameStyle after " << m_styleCombo->currentText() << endl;
 
609
    m_stylesList->changeItem( theText, index );
 
610
 
 
611
    // Check how many styles with that name we have now
 
612
    int synonyms = 0;
 
613
    for ( int i = 0; i < m_styleCombo->count(); i++ ) {
 
614
        if ( m_styleCombo->text( i ) == m_stylesList->currentText() )
 
615
            ++synonyms;
 
616
    }
 
617
    Q_ASSERT( synonyms > 0 ); // should have found 'index' at least !
 
618
    noSignals=false;
 
619
    // Can't close the dialog if two styles have the same name
 
620
    bool state=!theText.isEmpty() && (synonyms == 1);
 
621
    enableButtonOK(state );
 
622
    enableButtonApply(state);
 
623
    m_deleteButton->setEnabled(state&&(m_stylesList->currentItem() != 0));
 
624
    m_newButton->setEnabled(state);
 
625
    m_stylesList->setEnabled( state );
 
626
    if ( state )
 
627
    {
 
628
        m_moveUpButton->setEnabled(m_stylesList->currentItem() != 0);
 
629
        m_moveDownButton->setEnabled(m_stylesList->currentItem()!=(int)m_stylesList->count()-1);
 
630
    }
 
631
    else
 
632
    {
 
633
        m_moveUpButton->setEnabled(false);
 
634
        m_moveDownButton->setEnabled(false);
 
635
    }
 
636
}
 
637
 
 
638
/////////////
 
639
 
 
640
KoStyleParagTab::KoStyleParagTab( QWidget * parent )
 
641
    : KoStyleManagerTab( parent )
 
642
{
 
643
    ( new QVBoxLayout( this ) )->setAutoAdd( true );
 
644
    m_widget = 0L;
 
645
}
 
646
 
 
647
void KoStyleParagTab::update()
 
648
{
 
649
     m_widget->display( m_style->paragLayout() );
 
650
}
 
651
 
 
652
void KoStyleParagTab::save()
 
653
{
 
654
     m_widget->save( m_style->paragLayout() );
 
655
}
 
656
 
 
657
void KoStyleParagTab::setWidget( KoParagLayoutWidget * widget )
 
658
{
 
659
    m_widget = widget;
 
660
}
 
661
 
 
662
void KoStyleParagTab::resizeEvent( QResizeEvent *e )
 
663
{
 
664
    QWidget::resizeEvent( e );
 
665
    if ( m_widget ) m_widget->resize( size() );
 
666
}
 
667
 
 
668
KoStyleFontTab::KoStyleFontTab( QWidget * parent )
 
669
    : KoStyleManagerTab( parent )
 
670
{
 
671
        ( new QVBoxLayout( this ) )->setAutoAdd( true );
 
672
        QTabWidget *fontTabContainer = new QTabWidget( this );
 
673
 
 
674
        m_fontTab = new KoFontTab( KFontChooser::SmoothScalableFonts, this );
 
675
        m_decorationTab = new KoDecorationTab( this );
 
676
        m_highlightingTab = new KoHighlightingTab( this );
 
677
        m_layoutTab = new KoLayoutTab( true, this );
 
678
        m_languageTab = new KoLanguageTab( 0, this );
 
679
 
 
680
        fontTabContainer->addTab( m_fontTab, i18n( "Font" ) );
 
681
        fontTabContainer->addTab( m_decorationTab, i18n( "Decoration" ) );
 
682
        fontTabContainer->addTab( m_highlightingTab, i18n( "Highlighting" ) );
 
683
        fontTabContainer->addTab( m_layoutTab, i18n( "Layout" ) );
 
684
        fontTabContainer->addTab( m_languageTab, i18n( "Language" ) );
 
685
}
 
686
 
 
687
KoStyleFontTab::~KoStyleFontTab()
 
688
{
 
689
}
 
690
 
 
691
void KoStyleFontTab::update()
 
692
{
 
693
        m_fontTab->setSelection( m_style->format().font() );
 
694
        m_highlightingTab->setUnderline( m_style->format().underlineType() );
 
695
        m_highlightingTab->setUnderlineStyle( m_style->format().underlineStyle() );
 
696
        m_highlightingTab->setUnderlineColor( m_style->format().textUnderlineColor() );
 
697
        m_highlightingTab->setStrikethrough( m_style->format().strikeOutType() );
 
698
        m_highlightingTab->setStrikethroughStyle( m_style->format().strikeOutStyle() );
 
699
        m_highlightingTab->setWordByWord( m_style->format().wordByWord() );
 
700
        m_highlightingTab->setCapitalisation( m_style->format().attributeFont() );
 
701
        m_decorationTab->setTextColor( m_style->format().color() );
 
702
        m_decorationTab->setBackgroundColor( m_style->format().textBackgroundColor() );
 
703
        m_decorationTab->setShadow( m_style->format().shadowDistanceX(), m_style->format().shadowDistanceY(), m_style->format().shadowColor() );
 
704
        m_layoutTab->setSubSuperScript( m_style->format().vAlign(), m_style->format().offsetFromBaseLine(), m_style->format().relativeTextSize() );
 
705
        m_layoutTab->setAutoHyphenation( m_style->format().hyphenation() );
 
706
        m_languageTab->setLanguage( m_style->format().language() );
 
707
/*
 
708
#if 0
 
709
    bool subScript = m_style->format().vAlign() == KoTextFormat::AlignSubScript;
 
710
    bool superScript = m_style->format().vAlign() == KoTextFormat::AlignSuperScript;
 
711
    QFont fn = m_style->format().font();
 
712
    kdDebug()<<" fn.bold() :"<<fn.bold()<<" fn.italic():"<<fn.italic()<<endl;
 
713
    kdDebug()<<" fn.family() :"<<fn.family()<<endl;
 
714
    m_chooser->setFont( fn, subScript, superScript );
 
715
    m_chooser->setColor( m_style->format().color() );
 
716
    QColor col=m_style->format().textBackgroundColor();
 
717
    col=col.isValid() ? col : QApplication::palette().color( QPalette::Active, QColorGroup::Base );
 
718
    m_chooser->setBackGroundColor(col);
 
719
 
 
720
    m_chooser->setUnderlineColor( m_style->format().textUnderlineColor());
 
721
 
 
722
    m_chooser->setUnderlineType(m_style->format().underlineType());
 
723
    m_chooser->setUnderlineStyle(m_style->format().underlineStyle());
 
724
    m_chooser->setStrikeOutStyle(m_style->format().strikeOutStyle());
 
725
    m_chooser->setStrikeOutlineType(m_style->format().strikeOutType());
 
726
    m_chooser->setShadowText( m_style->format().shadowText());
 
727
    m_chooser->setFontAttribute( m_style->format().attributeFont());
 
728
    m_chooser->setWordByWord( m_style->format().wordByWord());
 
729
    m_chooser->setRelativeTextSize( m_style->format().relativeTextSize());
 
730
    m_chooser->setOffsetFromBaseLine( m_style->format().offsetFromBaseLine());
 
731
    m_chooser->setLanguage( m_style->format().language());
 
732
    m_chooser->setHyphenation( m_style->format().hyphenation());
 
733
#endif
 
734
*/}
 
735
 
 
736
void KoStyleFontTab::save()
 
737
{
 
738
        m_style->format() = KoTextFormat( m_fontTab->getSelection(),
 
739
                         m_layoutTab->getSubSuperScript(),
 
740
                         m_decorationTab->getTextColor(),
 
741
                         m_decorationTab->getBackgroundColor(),
 
742
                         m_highlightingTab->getUnderlineColor(),
 
743
                         m_highlightingTab->getUnderline(),
 
744
                         m_highlightingTab->getUnderlineStyle(),
 
745
                         m_highlightingTab->getStrikethrough(),
 
746
                         m_highlightingTab->getStrikethroughStyle(),
 
747
                         m_highlightingTab->getCapitalisation(),
 
748
                         m_languageTab->getLanguage(),
 
749
                         m_layoutTab->getRelativeTextSize(),
 
750
                         m_layoutTab->getOffsetFromBaseline(),
 
751
                         m_highlightingTab->getWordByWord(),
 
752
                         m_layoutTab->getAutoHyphenation(),
 
753
                         m_decorationTab->getShadowDistanceX(),
 
754
                         m_decorationTab->getShadowDistanceY(),
 
755
                         m_decorationTab->getShadowColor()
 
756
                        );
 
757
/*
 
758
        m_style->format().setFont( m_fontTab->getSelection() );
 
759
        m_style->format().setColor( m_decorationTab->getTextColor() );
 
760
        if( m_decorationTab->getBackGroundColor()!=QApplication::palette().color( QPalette::Active, QColorGroup::Base ))
 
761
        m_style->format().setTextBackgroundColor( m_decorationTab->getBackGroundColor() );
 
762
 
 
763
        m_style->format().setTextUnderlineColor(m_chooser->underlineColor());
 
764
    m_style->format().setUnderlineType (m_chooser->getUnderlineType());
 
765
    m_style->format().setUnderlineStyle (m_chooser->getUnderlineStyle());
 
766
    m_style->format().setStrikeOutStyle( m_chooser->getStrikeOutStyle() );
 
767
    m_style->format().setStrikeOutType (m_chooser->getStrikeOutType());
 
768
    m_style->format().setShadowText(m_chooser->getShadowText());
 
769
    m_style->format().setWordByWord( m_chooser->getWordByWord());
 
770
    m_style->format().setRelativeTextSize( m_chooser->getRelativeTextSize());
 
771
    m_style->format().setAttributeFont( m_chooser->getFontAttribute());
 
772
    m_style->format().setOffsetFromBaseLine( m_chooser->getOffsetFromBaseLine());
 
773
        m_style->format().setVAlign( m_layoutTab->getSubSuperScript() );
 
774
 
 
775
    m_style->format().setLanguage( m_chooser->getLanguage());
 
776
    m_style->format().setHyphenation( m_chooser->getHyphenation());
 
777
*/}
 
778
 
 
779
QString KoStyleFontTab::tabName()
 
780
{
 
781
    return i18n("Font");
 
782
}