lib

kformulaconfigpage.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qvariant.h>   // first for gcc 2.7.2
00022 #include <qbuttongroup.h>
00023 #include <qcheckbox.h>
00024 #include <qgroupbox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qmap.h>
00028 #include <qradiobutton.h>
00029 #include <qspinbox.h>
00030 #include <qstringlist.h>
00031 #include <qvbox.h>
00032 #include <qwidget.h>
00033 
00034 //#include <algorithm>
00035 
00036 #include <kcolorbutton.h>
00037 #include <kconfig.h>
00038 #include <kdebug.h>
00039 #include <kdialog.h>
00040 #include <kfontdialog.h>
00041 #include <klistview.h>
00042 #include <klocale.h>
00043 #include <kmessagebox.h>
00044 #include <knuminput.h>
00045 #include <kpushbutton.h>
00046 
00047 #include "contextstyle.h"
00048 #include "kformulaconfigpage.h"
00049 #include "kformuladocument.h"
00050 #include "symboltable.h"
00051 
00052 
00053 KFORMULA_NAMESPACE_BEGIN
00054 
00055 
00056 ConfigurePage::ConfigurePage( Document* document, QWidget* view, KConfig* config, QVBox* box, char* name )
00057     : QObject( box->parent(), name ), m_document( document ), m_view( view ), m_config( config ), m_changed( false )
00058 {
00059     const ContextStyle& contextStyle = document->getContextStyle( true );
00060 
00061     // fonts
00062 
00063     QGroupBox *gbox = new QGroupBox( i18n( "Fonts" ), box );
00064     gbox->setColumnLayout( 0, Qt::Horizontal );
00065 
00066     QGridLayout* grid = new QGridLayout( gbox->layout(), 5, 3 );
00067     grid->setSpacing( KDialog::spacingHint() );
00068 
00069     grid->setColStretch(1, 1);
00070 
00071     defaultFont = contextStyle.getDefaultFont();
00072     nameFont = contextStyle.getNameFont();
00073     numberFont = contextStyle.getNumberFont();
00074     operatorFont = contextStyle.getOperatorFont();
00075 
00076     connect( buildFontLine( gbox, grid, 0, defaultFont,
00077             i18n( "Default font:" ), defaultFontName ),
00078             SIGNAL( clicked() ), SLOT( selectNewDefaultFont() ) );
00079 
00080     connect( buildFontLine( gbox, grid, 1, nameFont,
00081             i18n( "Name font:" ), nameFontName ),
00082             SIGNAL( clicked() ), SLOT( selectNewNameFont() ) );
00083 
00084     connect( buildFontLine( gbox, grid, 2, numberFont,
00085             i18n( "Number font:" ), numberFontName ),
00086             SIGNAL( clicked() ), SLOT( selectNewNumberFont() ) );
00087 
00088     connect( buildFontLine( gbox, grid, 3, operatorFont,
00089             i18n( "Operator font:" ), operatorFontName ),
00090             SIGNAL( clicked() ), SLOT( selectNewOperatorFont() ) );
00091 
00092     QLabel* sizeTitle = new QLabel( i18n( "Default base size:" ), gbox );
00093     grid->addWidget( sizeTitle, 4, 0 );
00094 
00095     sizeSpin = new KIntNumInput( contextStyle.baseSize(), gbox );
00096     sizeSpin->setRange( 8, 72, 1, true );
00097 
00098     grid->addMultiCellWidget( sizeSpin, 4, 4, 1, 2 );
00099 
00100     connect( sizeSpin, SIGNAL( valueChanged( int ) ),
00101             SLOT( baseSizeChanged( int ) ) );
00102 
00103     // syntax highlighting
00104 
00105     syntaxHighlighting = new QCheckBox( i18n( "Use syntax highlighting" ),box );
00106     syntaxHighlighting->setChecked( contextStyle.syntaxHighlighting() );
00107 
00108 //     hlBox = new QGroupBox( i18n( "Highlight Colors" ), box );
00109 //     hlBox->setColumnLayout( 0, Qt::Horizontal );
00110 
00111 //     grid = new QGridLayout( hlBox->layout(), 5, 2 );
00112 //     grid->setSpacing( KDialog::spacingHint() );
00113 
00114 //     QLabel* defaultLabel = new QLabel( hlBox, "defaultLabel" );
00115 //     defaultLabel->setText( i18n( "Default color:" ) );
00116 //     grid->addWidget( defaultLabel, 0, 0 );
00117 
00118 //     defaultColorBtn = new KColorButton( hlBox, "defaultColor" );
00119 //     defaultColorBtn->setColor( contextStyle.getDefaultColor() );
00120 //     grid->addWidget( defaultColorBtn, 0, 1 );
00121 
00122 
00123 //     QLabel* numberLabel = new QLabel( hlBox, "numberLabel" );
00124 //     numberLabel->setText( i18n( "Number color:" ) );
00125 //     grid->addWidget( numberLabel, 1, 0 );
00126 
00127 //     numberColorBtn = new KColorButton( hlBox, "numberColor" );
00128 //     numberColorBtn->setColor( contextStyle.getNumberColorPlain() );
00129 //     grid->addWidget( numberColorBtn, 1, 1 );
00130 
00131 
00132 //     QLabel* operatorLabel = new QLabel( hlBox, "operatorLabel" );
00133 //     operatorLabel->setText( i18n( "Operator color:" ) );
00134 //     grid->addWidget( operatorLabel, 2, 0 );
00135 
00136 //     operatorColorBtn = new KColorButton( hlBox, "operatorColor" );
00137 //     operatorColorBtn->setColor( contextStyle.getOperatorColorPlain() );
00138 //     grid->addWidget( operatorColorBtn, 2, 1 );
00139 
00140 
00141 //     QLabel* emptyLabel = new QLabel( hlBox, "emptyLabel" );
00142 //     emptyLabel->setText( i18n( "Empty color:" ) );
00143 //     grid->addWidget( emptyLabel, 3, 0 );
00144 
00145 //     emptyColorBtn = new KColorButton( hlBox, "emptyColor" );
00146 //     emptyColorBtn->setColor( contextStyle.getEmptyColorPlain() );
00147 //     grid->addWidget( emptyColorBtn, 3, 1 );
00148 
00149 
00150 //     QLabel* errorLabel = new QLabel( hlBox, "errorLabel" );
00151 //     errorLabel->setText( i18n( "Error color:" ) );
00152 //     grid->addWidget( errorLabel, 4, 0 );
00153 
00154 //     errorColorBtn = new KColorButton( hlBox, "errorColor" );
00155 //     errorColorBtn->setColor( contextStyle.getErrorColorPlain() );
00156 //     grid->addWidget( errorColorBtn, 4, 1 );
00157 
00158     connect( syntaxHighlighting, SIGNAL( clicked() ),
00159             SLOT( syntaxHighlightingClicked() ) );
00160 
00161     syntaxHighlightingClicked();
00162 
00163     connect( syntaxHighlighting, SIGNAL( clicked() ), this, SLOT( slotChanged() ) );
00164     connect( sizeSpin, SIGNAL( valueChanged( int ) ), this, SLOT( slotChanged() ) );
00165 
00166     Q_ASSERT( !m_changed );
00167 }
00168 
00169 
00170 QPushButton* ConfigurePage::buildFontLine( QWidget* parent,
00171             QGridLayout* layout, int number, QFont font, QString name,
00172             QLabel*& fontName )
00173 {
00174     QLabel* fontTitle = new QLabel( name, parent );
00175 
00176     QString labelName = font.family() + ' ' + QString::number( font.pointSize() );
00177     fontName = new QLabel( labelName, parent );
00178     fontName->setFont( font );
00179     fontName->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
00180 
00181     QPushButton* chooseButton = new QPushButton( i18n( "Choose..." ), parent );
00182 
00183     layout->addWidget( fontTitle, number, 0 );
00184     layout->addWidget( fontName, number, 1 );
00185     layout->addWidget( chooseButton, number, 2 );
00186 
00187     return chooseButton;
00188 }
00189 
00190 
00191 void ConfigurePage::apply()
00192 {
00193     if ( !m_changed )
00194         return;
00195 
00196     ContextStyle& contextStyle = m_document->getContextStyle( true );
00197 
00198     contextStyle.setDefaultFont( defaultFont );
00199     contextStyle.setNameFont( nameFont );
00200     contextStyle.setNumberFont( numberFont );
00201     contextStyle.setOperatorFont( operatorFont );
00202     contextStyle.setBaseSize( sizeSpin->value() );
00203 
00204     contextStyle.setSyntaxHighlighting( syntaxHighlighting->isChecked() );
00205 //     contextStyle.setDefaultColor( defaultColorBtn->color() );
00206 //     contextStyle.setNumberColor( numberColorBtn->color() );
00207 //     contextStyle.setOperatorColor( operatorColorBtn->color() );
00208 //     contextStyle.setEmptyColor( emptyColorBtn->color() );
00209 //     contextStyle.setErrorColor( errorColorBtn->color() );
00210 
00211     m_config->setGroup( "kformula Font" );
00212     m_config->writeEntry( "defaultFont", defaultFont.toString() );
00213     m_config->writeEntry( "nameFont", nameFont.toString() );
00214     m_config->writeEntry( "numberFont", numberFont.toString() );
00215     m_config->writeEntry( "operatorFont", operatorFont.toString() );
00216     m_config->writeEntry( "baseSize", QString::number( sizeSpin->value() ) );
00217 
00218 //     m_config->setGroup( "kformula Color" );
00219 //     m_config->writeEntry( "syntaxHighlighting", syntaxHighlighting->isChecked() );
00220 //     m_config->writeEntry( "defaultColor", defaultColorBtn->color() );
00221 //     m_config->writeEntry( "numberColor",  numberColorBtn->color() );
00222 //     m_config->writeEntry( "operatorColor", operatorColorBtn->color() );
00223 //     m_config->writeEntry( "emptyColor", emptyColorBtn->color() );
00224 //     m_config->writeEntry( "errorColor", errorColorBtn->color() );
00225 
00226     // notify!!!
00227     m_document->updateConfig();
00228     m_changed = false;
00229 }
00230 
00231 void ConfigurePage::slotDefault()
00232 {
00233     defaultFont = QFont( "Times", 12, QFont::Normal, true );
00234     nameFont = QFont( "Times" );
00235     numberFont = nameFont;
00236     operatorFont = nameFont;
00237 
00238     sizeSpin->setValue( 20 );
00239 
00240     updateFontLabel( defaultFont, defaultFontName );
00241     updateFontLabel( nameFont, nameFontName );
00242     updateFontLabel( numberFont, numberFontName );
00243     updateFontLabel( operatorFont, operatorFontName );
00244 
00245     syntaxHighlighting->setChecked( true );
00246     syntaxHighlightingClicked();
00247 
00248 //     defaultColorBtn->setColor( Qt::black );
00249 //     numberColorBtn->setColor( Qt::blue );
00250 //     operatorColorBtn->setColor( Qt::darkGreen );
00251 //     emptyColorBtn->setColor( Qt::blue );
00252 //     errorColorBtn->setColor( Qt::darkRed );
00253     slotChanged();
00254 }
00255 
00256 void ConfigurePage::syntaxHighlightingClicked()
00257 {
00258 //     bool checked = syntaxHighlighting->isChecked();
00259 //     hlBox->setEnabled( checked );
00260 }
00261 
00262 void ConfigurePage::selectNewDefaultFont()
00263 {
00264     if ( selectFont( defaultFont ) )
00265         updateFontLabel( defaultFont, defaultFontName );
00266 }
00267 
00268 void ConfigurePage::selectNewNameFont()
00269 {
00270     if ( selectFont( nameFont ) )
00271         updateFontLabel( nameFont, nameFontName );
00272 }
00273 
00274 void ConfigurePage::selectNewNumberFont()
00275 {
00276     if ( selectFont( numberFont ) )
00277         updateFontLabel( numberFont, numberFontName );
00278 }
00279 
00280 void ConfigurePage::selectNewOperatorFont()
00281 {
00282     if ( selectFont( operatorFont ) )
00283         updateFontLabel( operatorFont, operatorFontName );
00284 }
00285 
00286 bool ConfigurePage::selectFont( QFont & font )
00287 {
00288     QStringList list;
00289 
00290     KFontChooser::getFontList( list, KFontChooser::SmoothScalableFonts );
00291 
00292     KFontDialog dlg( m_view, 0, false, true, list );
00293     dlg.setFont( font );
00294 
00295     int result = dlg.exec();
00296     if (  KDialog::Accepted == result ) {
00297         font = dlg.font();
00298         slotChanged();
00299         return true;
00300     }
00301 
00302     return false;
00303 }
00304 
00305 void ConfigurePage::baseSizeChanged( int /*value*/ )
00306 {
00307 }
00308 
00309 void ConfigurePage::updateFontLabel( QFont font, QLabel* label )
00310 {
00311     label->setText( font.family() + ' ' + QString::number( font.pointSize() ) );
00312     label->setFont( font );
00313 }
00314 
00315 void ConfigurePage::slotChanged()
00316 {
00317     m_changed = true;
00318 }
00319 
00320 // class UsedFontItem : public KListViewItem {
00321 // public:
00322 //     UsedFontItem( MathFontsConfigurePage* page, QListView* parent, QString font )
00323 //         : KListViewItem( parent, font ), m_page( page ) {}
00324 
00325 //     int compare( QListViewItem* i, int col, bool ascending ) const;
00326 
00327 // private:
00328 //     MathFontsConfigurePage* m_page;
00329 // };
00330 
00331 // int UsedFontItem::compare( QListViewItem* i, int, bool ) const
00332 // {
00333 //     QValueVector<QString>::iterator lhsIt = m_page->findUsedFont( text( 0 ) );
00334 //     QValueVector<QString>::iterator rhsIt = m_page->findUsedFont( i->text( 0 ) );
00335 //     if ( lhsIt < rhsIt ) {
00336 //         return -1;
00337 //     }
00338 //     else if ( lhsIt > rhsIt ) {
00339 //         return 1;
00340 //     }
00341 //     return 0;
00342 // }
00343 
00344 // MathFontsConfigurePage::MathFontsConfigurePage( Document* document, QWidget* view,
00345 //                                                 KConfig* config, QVBox* box, char* name )
00346 //     : QObject( box->parent(), name ), m_document( document ), m_view( view ), m_config( config )
00347 // {
00348 //     QWidget* fontWidget = new QWidget( box );
00349 //     QGridLayout* fontLayout = new QGridLayout( fontWidget, 1, 1, KDialog::marginHint(), KDialog::spacingHint() );
00350 
00351 //     QHBoxLayout* hLayout = new QHBoxLayout( 0, 0, 6 );
00352 
00353 //     availableFonts = new KListView( fontWidget );
00354 //     availableFonts->addColumn( i18n( "Available Fonts" ) );
00355 //     hLayout->addWidget( availableFonts );
00356 
00357 //     QVBoxLayout* vLayout = new QVBoxLayout( 0, 0, 6 );
00358 //     QSpacerItem* spacer1 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
00359 //     vLayout->addItem( spacer1 );
00360 
00361 //     addFont = new KPushButton( fontWidget );
00362 //     addFont->setText( "->" );
00363 //     vLayout->addWidget( addFont );
00364 
00365 //     removeFont = new KPushButton( fontWidget );
00366 //     removeFont->setText( "<-" );
00367 //     vLayout->addWidget( removeFont );
00368 
00369 //     QSpacerItem* spacer2 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
00370 //     vLayout->addItem( spacer2 );
00371 
00372 //     hLayout->addLayout( vLayout );
00373 
00374 //     vLayout = new QVBoxLayout( 0, 0, 6 );
00375 
00376 //     moveUp = new KPushButton( fontWidget );
00377 //     moveUp->setText( i18n( "Up" ) );
00378 //     vLayout->addWidget( moveUp );
00379 
00380 //     requestedFonts = new KListView( fontWidget );
00381 //     requestedFonts->addColumn( i18n( "Used Fonts" ) );
00382 //     vLayout->addWidget( requestedFonts );
00383 
00384 //     moveDown = new KPushButton( fontWidget );
00385 //     moveDown->setText( i18n( "Down" ) );
00386 //     vLayout->addWidget( moveDown );
00387 
00388 //     hLayout->addLayout( vLayout );
00389 
00390 //     fontLayout->addLayout( hLayout, 0, 0 );
00391 
00392 // //     connect( availableFonts, SIGNAL( executed( QListViewItem* ) ),
00393 // //              this, SLOT( slotAddFont() ) );
00394 // //     connect( requestedFonts, SIGNAL( executed( QListViewItem* ) ),
00395 // //              this, SLOT( slotRemoveFont() ) );
00396 //     connect( addFont, SIGNAL( clicked() ), this, SLOT( slotAddFont() ) );
00397 //     connect( removeFont, SIGNAL( clicked() ), this, SLOT( slotRemoveFont() ) );
00398 //     connect( moveUp, SIGNAL( clicked() ), this, SLOT( slotMoveUp() ) );
00399 //     connect( moveDown, SIGNAL( clicked() ), this, SLOT( slotMoveDown() ) );
00400 
00401 //     const ContextStyle& contextStyle = document->getContextStyle( true );
00402 //     const SymbolTable& symbolTable = contextStyle.symbolTable();
00403 //     const QStringList& usedFonts = contextStyle.requestedFonts();
00404 
00405 //     QMap<QString, QString> fontMap;
00406 // //    symbolTable.findAvailableFonts( &fontMap );
00407 
00408 //     setupLists( usedFonts );
00409 // }
00410 
00411 // void MathFontsConfigurePage::apply()
00412 // {
00413 //     QStringList strings;
00414 //     std::copy( usedFontList.begin(), usedFontList.end(), std::back_inserter( strings ) );
00415 
00416 //     m_config->setGroup( "kformula Font" );
00417 //     m_config->writeEntry( "usedMathFonts", strings );
00418 
00419 //     ContextStyle& contextStyle = m_document->getContextStyle( true );
00420 //     contextStyle.setRequestedFonts( strings );
00421 // }
00422 
00423 // void MathFontsConfigurePage::slotDefault()
00424 // {
00425 //     QStringList usedFonts;
00426 
00427 //     usedFonts.push_back( "esstixone" );
00428 //     usedFonts.push_back( "esstixtwo" );
00429 //     usedFonts.push_back( "esstixthree" );
00430 //     usedFonts.push_back( "esstixfour" );
00431 //     usedFonts.push_back( "esstixfive" );
00432 //     usedFonts.push_back( "esstixsix" );
00433 //     usedFonts.push_back( "esstixseven" );
00434 //     usedFonts.push_back( "esstixeight" );
00435 //     usedFonts.push_back( "esstixnine" );
00436 //     usedFonts.push_back( "esstixten" );
00437 //     usedFonts.push_back( "esstixeleven" );
00438 //     usedFonts.push_back( "esstixtwelve" );
00439 //     usedFonts.push_back( "esstixthirteen" );
00440 //     usedFonts.push_back( "esstixfourteen" );
00441 //     usedFonts.push_back( "esstixfifteen" );
00442 //     usedFonts.push_back( "esstixsixteen" );
00443 //     usedFonts.push_back( "esstixseventeen" );
00444 
00445 //     usedFontList.clear();
00446 //     requestedFonts->clear();
00447 //     availableFonts->clear();
00448 
00449 //     setupLists( usedFonts );
00450 // }
00451 
00452 // QValueVector<QString>::iterator MathFontsConfigurePage::findUsedFont( QString name )
00453 // {
00454 //     return std::find( usedFontList.begin(), usedFontList.end(), name );
00455 // }
00456 
00457 // void MathFontsConfigurePage::setupLists( const QStringList& usedFonts )
00458 // {
00459 //     const ContextStyle& contextStyle = m_document->getContextStyle( true );
00460 //     const SymbolTable& symbolTable = contextStyle.symbolTable();
00461 
00462 //     QMap<QString, QString> fontMap;
00463 // //    symbolTable.findAvailableFonts( &fontMap );
00464 
00465 //     for ( QStringList::const_iterator it = usedFonts.begin(); it != usedFonts.end(); ++it ) {
00466 //         QMap<QString, QString>::iterator font = fontMap.find( *it );
00467 //         if ( font != fontMap.end() ) {
00468 //             fontMap.erase( font );
00469 //             new UsedFontItem( this, requestedFonts, *it );
00470 //             usedFontList.push_back( *it );
00471 //         }
00472 //     }
00473 //     for ( QMap<QString, QString>::iterator it = fontMap.begin(); it != fontMap.end(); ++it ) {
00474 //         new KListViewItem( availableFonts, it.key() );
00475 //     }
00476 // }
00477 
00478 // void MathFontsConfigurePage::slotAddFont()
00479 // {
00480 //     QListViewItem* fontItem = availableFonts->selectedItem();
00481 //     if ( fontItem ) {
00482 //         QString fontName = fontItem->text( 0 );
00483 //         //availableFonts->takeItem( fontItem );
00484 //         delete fontItem;
00485 
00486 //         new UsedFontItem( this, requestedFonts, fontName );
00487 //         usedFontList.push_back( fontName );
00488 //     }
00489 // }
00490 
00491 // void MathFontsConfigurePage::slotRemoveFont()
00492 // {
00493 //     QListViewItem* fontItem = requestedFonts->selectedItem();
00494 //     if ( fontItem ) {
00495 //         QString fontName = fontItem->text( 0 );
00496 //         QValueVector<QString>::iterator it = std::find( usedFontList.begin(), usedFontList.end(), fontName );
00497 //         if ( it != usedFontList.end() ) {
00498 //             usedFontList.erase( it );
00499 //         }
00500 //         //requestedFonts->takeItem( fontItem );
00501 //         delete fontItem;
00502 //         new KListViewItem( availableFonts, fontName );
00503 //     }
00504 // }
00505 
00506 // void MathFontsConfigurePage::slotMoveUp()
00507 // {
00508 //     QListViewItem* fontItem = requestedFonts->selectedItem();
00509 //     if ( fontItem ) {
00510 //         QString fontName = fontItem->text( 0 );
00511 //         QValueVector<QString>::iterator it = std::find( usedFontList.begin(), usedFontList.end(), fontName );
00512 //         if ( it != usedFontList.end() ) {
00513 //             uint pos = it - usedFontList.begin();
00514 //             if ( pos > 0 ) {
00515 //                 QValueVector<QString>::iterator before = it-1;
00516 //                 std::swap( *it, *before );
00517 //                 requestedFonts->sort();
00518 //             }
00519 //         }
00520 //     }
00521 // }
00522 
00523 // void MathFontsConfigurePage::slotMoveDown()
00524 // {
00525 //     QListViewItem* fontItem = requestedFonts->selectedItem();
00526 //     if ( fontItem ) {
00527 //         QString fontName = fontItem->text( 0 );
00528 //         QValueVector<QString>::iterator it = std::find( usedFontList.begin(), usedFontList.end(), fontName );
00529 //         if ( it != usedFontList.end() ) {
00530 //             uint pos = it - usedFontList.begin();
00531 //             if ( pos < usedFontList.size()-1 ) {
00532 //                 QValueVector<QString>::iterator after = it+1;
00533 //                 std::swap( *it, *after );
00534 //                 requestedFonts->sort();
00535 //             }
00536 //         }
00537 //     }
00538 // }
00539 
00540 KFORMULA_NAMESPACE_END
00541 
00542 using namespace KFormula;
00543 #include "kformulaconfigpage.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys