00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qlistbox.h>
00021 #include <qpainter.h>
00022
00023 #include <kapplication.h>
00024 #include <kcombobox.h>
00025 #include <kglobalsettings.h>
00026 #include <ktoolbar.h>
00027 #include <kdebug.h>
00028
00029 #include "symbolaction.h"
00030
00031
00032
00033
00034
00035 KFORMULA_NAMESPACE_BEGIN
00036
00037 class SymbolComboItem : public QListBoxItem
00038 {
00039 public:
00040 SymbolComboItem( const QString&, const QFont&, QChar, QComboBox* combo );
00041 virtual ~SymbolComboItem();
00042
00043 virtual int width( const QListBox* ) const;
00044 virtual int height( const QListBox* ) const;
00045
00046 protected:
00047 virtual void paint( QPainter *p );
00048
00049 private:
00050 QComboBox *m_combo;
00051 QString m_name;
00052 QFont m_font;
00053 QChar m_symbol;
00054
00055 static int widest;
00056 };
00057
00058 int SymbolComboItem::widest = 0;
00059
00060 SymbolComboItem::SymbolComboItem( const QString &name, const QFont &font,
00061 QChar symbol, QComboBox *combo )
00062 : QListBoxItem( combo->listBox() ),
00063 m_combo( combo ),
00064 m_name( name ),
00065 m_font( font ),
00066 m_symbol( symbol )
00067 {
00068 setText( name );
00069 int charWidth = QFontMetrics( m_font ).width( QChar( m_symbol ) );
00070 widest = QMAX( widest, charWidth );
00071 }
00072
00073 SymbolComboItem::~SymbolComboItem()
00074 {
00075 }
00076
00077 int SymbolComboItem::width( const QListBox * ) const
00078 {
00079 return widest + QFontMetrics( KGlobalSettings::generalFont() ).width( text() ) + 12;
00080 }
00081
00082 int SymbolComboItem::height( const QListBox * ) const
00083 {
00084 int generalHeight = QFontMetrics( KGlobalSettings::generalFont() ).lineSpacing();
00085 int fontHeight = QFontMetrics( m_font ).lineSpacing();
00086 return QMAX( generalHeight, fontHeight ) + 2;
00087 }
00088
00089 void SymbolComboItem::paint( QPainter *p )
00090 {
00091 p->setFont( m_font );
00092 QFontMetrics fm( p->fontMetrics() );
00093 p->drawText( 3, fm.ascent() + fm.leading() / 2,
00094 QString( "%1" ).arg( QChar( m_symbol ) ) );
00095
00096 p->setFont( KGlobalSettings::generalFont() );
00097 fm = p->fontMetrics();
00098 p->drawText( widest + 6, height( m_combo->listBox() ) / 2 + fm.strikeOutPos(), m_name );
00099 }
00100
00101
00102
00103
00104 SymbolAction::SymbolAction( QObject* parent, const char* name )
00105 : KSelectAction( parent, name )
00106 {
00107 setEditable( FALSE );
00108 }
00109
00110 SymbolAction::SymbolAction( const QString& text, const KShortcut& cut,
00111 const QObject* receiver, const char* slot,
00112 QObject* parent, const char* name )
00113 : KSelectAction( text, cut, receiver, slot, parent, name )
00114 {
00115 setEditable( FALSE );
00116 }
00117
00118 int SymbolAction::plug( QWidget* w, int index )
00119 {
00120 if (kapp && !kapp->authorizeKAction(name()))
00121 return -1;
00122 if ( w->inherits( "KToolBar" ) )
00123 {
00124 KToolBar* bar = static_cast<KToolBar*>( w );
00125 int id_ = KAction::getToolButtonID();
00126 KComboBox *cb = new KComboBox( bar );
00127 connect( cb, SIGNAL( activated( const QString & ) ),
00128 SLOT( slotActivated( const QString & ) ) );
00129 cb->setEnabled( isEnabled() );
00130 bar->insertWidget( id_, comboWidth(), cb, index );
00131 cb->setMinimumWidth( cb->sizeHint().width() );
00132
00133 addContainer( bar, id_ );
00134
00135 connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00136
00137 updateItems( containerCount() - 1 );
00138
00139 return containerCount() - 1;
00140 }
00141 else return KSelectAction::plug( w, index );
00142 }
00143
00144 void SymbolAction::setSymbols( const QStringList &names, const QValueList<QFont>& fonts,
00145 const QMemArray<QChar>& chars )
00146 {
00147 m_fonts = fonts;
00148 m_chars = chars;
00149 setItems( names );
00150
00151 int len = containerCount();
00152 for ( int i = 0; i < len; ++i )
00153 updateItems( i );
00154 }
00155
00156 void SymbolAction::updateItems( int id )
00157 {
00158 QWidget *w = container( id );
00159 if ( w->inherits( "KToolBar" ) ) {
00160 QWidget *r = static_cast<KToolBar*>( w )->getWidget( itemId( id ) );
00161 if ( r->inherits( "QComboBox" ) ) {
00162 QComboBox *cb = static_cast<QComboBox*>( r );
00163 cb->clear();
00164
00165 for( uint i = 0; i < items().count(); ++i ) {
00166 new SymbolComboItem( *items().at( i ), *m_fonts.at( i ),
00167 m_chars.at( i ), cb );
00168 }
00169 cb->setMinimumWidth( cb->sizeHint().width() );
00170 }
00171 }
00172 }
00173
00174 KFORMULA_NAMESPACE_END