certmanager Library API Documentation

appearanceconfigwidget.cpp

00001 /*
00002     appearanceconfigwidget.cpp
00003 
00004     This file is part of kleopatra, the KDE key manager
00005     Copyright (c) 2002,2004 Klarälvdalens Datakonsult AB
00006     Copyright (c) 2002,2003 Marc Mutz <mutz@kde.org>
00007 
00008     Libkleopatra is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU General Public License as
00010     published by the Free Software Foundation; either version 2 of the
00011     License, or (at your option) any later version.
00012 
00013     Libkleopatra is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022     In addition, as a special exception, the copyright holders give
00023     permission to link the code of this program with any edition of
00024     the Qt library by Trolltech AS, Norway (or with modified versions
00025     of Qt that use the same license as Qt), and distribute linked
00026     combinations including the two.  You must obey the GNU General
00027     Public License in all respects for all of the code used other than
00028     Qt.  If you modify this file, you may extend this exception to
00029     your version of the file, but you are not obligated to do so.  If
00030     you do not wish to do so, delete this exception statement from
00031     your version.
00032  */
00033 
00034 #ifdef HAVE_CONFIG_H
00035 #include <config.h>
00036 #endif
00037 
00038 #include "appearanceconfigwidget.h"
00039 
00040 #include <kleo/cryptobackendfactory.h>
00041 #include <kleo/keyfiltermanager.h>
00042 
00043 #include <klistview.h>
00044 #include <kconfig.h>
00045 #include <kdialog.h>
00046 #include <klocale.h>
00047 #include <kdebug.h>
00048 #include <kmessagebox.h>
00049 #include <kfontdialog.h>
00050 #include <kcolordialog.h>
00051 
00052 #include <qpushbutton.h>
00053 #include <qlayout.h>
00054 #include <qheader.h>
00055 #include <qcolor.h>
00056 #include <qfont.h>
00057 #include <qstring.h>
00058 #include <qpainter.h>
00059 #include <qregexp.h>
00060 #include <qcheckbox.h>
00061 
00062 #include <assert.h>
00063 
00064 using namespace Kleo;
00065 
00066 class CategoryListViewItem : public QListViewItem
00067 {
00068 public:
00069   CategoryListViewItem( QListView* lv, QListViewItem* prev, const KConfigBase& config )
00070     : QListViewItem( lv, prev ) {
00071 
00072     setName( config.readEntry( "Name", i18n("<unnamed>") ) );
00073     mForegroundColor = config.readColorEntry( "foreground-color" );
00074     mBackgroundColor = config.readColorEntry( "background-color" );
00075     mHasFont = config.hasKey( "font" );
00076     if ( mHasFont ) {
00077       setFont( config.readFontEntry( "font" ) ); // sets mItalic and mBold
00078     }
00079     else {
00080       mItalic = config.readBoolEntry( "font-italic", false );
00081       mBold = config.readBoolEntry( "font-bold", false );
00082     }
00083     mStrikeOut = config.readBoolEntry( "font-strikeout", false );
00084     mIsExpired = config.readBoolEntry( "is-expired", false );
00085     mDirty = false;
00086   }
00087 
00088   void save( KConfigBase& config ) {
00089     config.writeEntry( "Name", text( 0 ) );
00090     config.writeEntry( "foreground-color", mForegroundColor );
00091     config.writeEntry( "background-color", mBackgroundColor );
00092     if ( mHasFont )
00093       config.writeEntry( "font", mFont );
00094     else {
00095       config.deleteEntry( "font" );
00096       config.writeEntry( "font-italic", mItalic );
00097       config.writeEntry( "font-bold", mBold );
00098     }
00099     config.writeEntry( "font-strikeout", mStrikeOut );
00100   }
00101 
00102   void setForegroundColor( const QColor& foreground ) { mForegroundColor = foreground; mDirty = true; }
00103   void setBackgroundColor( const QColor& background ) { mBackgroundColor = background; mDirty = true; }
00104   void setFont( const QFont& font ) {
00105     mFont = font;
00106     mHasFont = true;
00107     mItalic = font.italic();
00108     mBold = font.bold();
00109     mDirty = true;
00110   }
00111 
00112   QColor foregroundColor() const { return mForegroundColor; }
00113   QColor backgroundColor() const { return mBackgroundColor; }
00114   QFont font() const { return mFont; }
00115 
00116   void setDefaultAppearance() {
00117     mForegroundColor = mIsExpired ? Qt::red : QColor();
00118     mBackgroundColor = QColor();
00119     mHasFont = false;
00120     mFont = QFont();
00121     mBold = false;
00122     mItalic = false;
00123     mStrikeOut = false;
00124     mDirty = true;
00125   }
00126 
00127   bool isDirty() const { return mDirty; }
00128   bool isItalic() const { return mItalic; }
00129   bool isBold() const { return mBold; }
00130   bool isStrikeout() const { return mStrikeOut; }
00131   bool hasFont() const { return mHasFont; }
00132 
00133   void toggleItalic() { mItalic = !mItalic; if ( mHasFont ) mFont.setItalic( mItalic ); mDirty = true; }
00134   void toggleBold() { mBold = !mBold; if ( mHasFont ) mFont.setBold( mBold ); mDirty = true; }
00135   void toggleStrikeout() { mStrikeOut = !mStrikeOut; mDirty = true; }
00136 
00137 private:
00138   void setName( const QString& name ) {
00139     setText( 0, name );
00140   }
00141 
00142   void paintCell( QPainter * p, const QColorGroup & cg, int column, int width, int alignment );
00143 
00144 private:
00145   QColor mForegroundColor, mBackgroundColor;
00146   QFont mFont;
00147   bool mHasFont;
00148   bool mIsExpired; // used for default settings
00149   bool mItalic;
00150   bool mBold;
00151   bool mStrikeOut;
00152   bool mDirty;
00153 };
00154 
00155 void CategoryListViewItem::paintCell( QPainter * p, const QColorGroup & cg, int column, int width, int alignment ) {
00156   QColorGroup _cg = cg;
00157   QFont font = p->font();
00158   if ( mHasFont )
00159     font = mFont;
00160   else {
00161     if ( mItalic )
00162       font.setItalic( true );
00163     if ( mBold )
00164       font.setBold( true );
00165   }
00166   if ( mStrikeOut )
00167     font.setStrikeOut( true );
00168   p->setFont( font );
00169 
00170   if ( mForegroundColor.isValid() )
00171     _cg.setColor( QColorGroup::Text, mForegroundColor );
00172   if ( mBackgroundColor.isValid() )
00173     _cg.setColor( QColorGroup::Base, mBackgroundColor );
00174 
00175   QListViewItem::paintCell( p, _cg, column, width, alignment );
00176 }
00177 
00179 
00180 Kleo::AppearanceConfigWidget::AppearanceConfigWidget (
00181   QWidget* parent,  const char* name, WFlags fl )
00182   : AppearanceConfigWidgetBase( parent, name, fl )
00183 {
00184     categoriesLV->setSorting( -1 );
00185     load();
00186 }
00187 
00188 
00189 /*
00190  *  Destroys the object and frees any allocated resources
00191  */
00192 
00193 AppearanceConfigWidget::~AppearanceConfigWidget()
00194 {
00195   // no need to delete child widgets, Qt does it all for us
00196 }
00197 
00198 
00199 void AppearanceConfigWidget::slotSelectionChanged( QListViewItem* item )
00200 {
00201   bool sel = item != 0;
00202   foregroundButton->setEnabled( sel );
00203   backgroundButton->setEnabled( sel );
00204   fontButton->setEnabled( sel );
00205   italicCB->setEnabled( item );
00206   boldCB->setEnabled( item );
00207   strikeoutCB->setEnabled( item );
00208   defaultLookPB->setEnabled( sel );
00209   if ( item ) {
00210     CategoryListViewItem* clvi = static_cast<CategoryListViewItem *>( item );
00211     italicCB->setChecked( clvi->isItalic() );
00212     boldCB->setChecked( clvi->isBold() );
00213     strikeoutCB->setChecked( clvi->isStrikeout() );
00214   } else {
00215     italicCB->setChecked( false );
00216     boldCB->setChecked( false );
00217     strikeoutCB->setChecked( false );
00218   }
00219 }
00220 
00221 /*
00222  * set default appearance for selected category
00223  */
00224 void AppearanceConfigWidget::slotDefaultClicked()
00225 {
00226   CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
00227   if ( !item )
00228     return;
00229   item->setDefaultAppearance();
00230   item->repaint();
00231   slotSelectionChanged( item );
00232   emit changed();
00233 }
00234 
00235 void AppearanceConfigWidget::load()
00236 {
00237   categoriesLV->clear();
00238   KConfig * config = Kleo::CryptoBackendFactory::instance()->configObject();
00239   if ( !config )
00240     return;
00241   QStringList groups = config->groupList().grep( QRegExp( "^Key Filter #\\d+$" ) );
00242   for ( QStringList::const_iterator it = groups.begin() ; it != groups.end() ; ++it ) {
00243     KConfigGroup cfg( config, *it );
00244     (void) new CategoryListViewItem( categoriesLV, categoriesLV->lastItem(), cfg );
00245   }
00246 }
00247 
00248 void AppearanceConfigWidget::save()
00249 {
00250   KConfig * config = Kleo::CryptoBackendFactory::instance()->configObject();
00251   if ( !config )
00252     return;
00253   // We know (assume) that the groups in the config object haven't changed,
00254   // so we just iterate over them and over the listviewitems, and map one-to-one.
00255   QStringList groups = config->groupList().grep( QRegExp( "^Key Filter #\\d+$" ) );
00256   if ( groups.isEmpty() ) {
00257     // If we created the default categories ourselves just now, then we need to make up their list
00258     QListViewItemIterator lvit( categoriesLV );
00259     for ( ; lvit.current() ; ++lvit )
00260       groups << lvit.current()->text( 0 );
00261   }
00262 
00263   QListViewItemIterator lvit( categoriesLV );
00264   for ( QStringList::const_iterator it = groups.begin() ; it != groups.end() && lvit.current(); ++it, ++lvit ) {
00265     CategoryListViewItem* item = static_cast<CategoryListViewItem*>(lvit.current() );
00266     KConfigGroup cfg( config, *it );
00267     item->save( cfg );
00268   }
00269   config->sync();
00270   Kleo::KeyFilterManager::instance()->reload();
00271 }
00272 
00273 
00274 void AppearanceConfigWidget::slotForegroundClicked() {
00275   CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
00276   Q_ASSERT( item );
00277   if( !item )
00278     return;
00279   QColor fg = item->foregroundColor();
00280   int result = KColorDialog::getColor( fg );
00281   if ( result == KColorDialog::Accepted ) {
00282     item->setForegroundColor( fg );
00283     item->repaint();
00284     emit changed();
00285   }
00286 }
00287 
00288 void AppearanceConfigWidget::slotBackgroundClicked() {
00289   CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
00290   Q_ASSERT( item );
00291   if( !item )
00292     return;
00293   QColor bg = item->backgroundColor();
00294   int result = KColorDialog::getColor( bg );
00295   if ( result == KColorDialog::Accepted ) {
00296     item->setBackgroundColor( bg );
00297     item->repaint();
00298     emit changed();
00299   }
00300 }
00301 
00302 void AppearanceConfigWidget::slotFontClicked() {
00303   CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
00304   Q_ASSERT( item );
00305   if( !item )
00306     return;
00307   QFont font = item->font();
00308   int result = KFontDialog::getFont( font );
00309   if ( result == KFontDialog::Accepted ) {
00310     item->setFont( font );
00311     item->repaint();
00312     emit changed();
00313   }
00314 }
00315 
00316 void AppearanceConfigWidget::defaults()
00317 {
00318   // This simply means "default look for every category"
00319   QListViewItemIterator lvit( categoriesLV );
00320   for ( ; lvit.current() ; ++lvit ) {
00321     CategoryListViewItem* item = static_cast<CategoryListViewItem *>( lvit.current() );
00322     item->setDefaultAppearance();
00323     item->repaint();
00324   }
00325   emit changed();
00326 }
00327 
00328 void AppearanceConfigWidget::slotItalicClicked()
00329 {
00330   CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
00331   if ( item ) {
00332     item->toggleItalic();
00333     item->repaint();
00334     emit changed();
00335   }
00336 }
00337 
00338 void AppearanceConfigWidget::slotBoldClicked()
00339 {
00340   CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
00341   if ( item ) {
00342     item->toggleBold();
00343     item->repaint();
00344     emit changed();
00345   }
00346 }
00347 
00348 void AppearanceConfigWidget::slotStrikeoutClicked()
00349 {
00350   CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
00351   if ( item ) {
00352     item->toggleStrikeout();
00353     item->repaint();
00354     emit changed();
00355   }
00356 }
00357 
00358 #include "appearanceconfigwidget.moc"
KDE Logo
This file is part of the documentation for certmanager Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:17:51 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003