kwin Library API Documentation

ruleslist.cpp

00001 /*
00002  * Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program 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
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #include "ruleslist.h"
00020 
00021 #include <klistbox.h>
00022 #include <kpushbutton.h>
00023 #include <assert.h>
00024 #include <kdebug.h>
00025 #include <kconfig.h>
00026 
00027 #include "ruleswidget.h"
00028 
00029 namespace KWinInternal
00030 {
00031 
00032 KCMRulesList::KCMRulesList( QWidget* parent, const char* name )
00033 : KCMRulesListBase( parent, name )
00034     {
00035     // connect both current/selected, so that current==selected (stupid QListBox :( )
00036     connect( rules_listbox, SIGNAL( currentChanged( QListBoxItem* )),
00037         SLOT( activeChanged( QListBoxItem*)));
00038     connect( rules_listbox, SIGNAL( selectionChanged( QListBoxItem* )),
00039         SLOT( activeChanged( QListBoxItem*)));
00040     connect( new_button, SIGNAL( clicked()),
00041         SLOT( newClicked()));
00042     connect( modify_button, SIGNAL( clicked()),
00043         SLOT( modifyClicked()));
00044     connect( delete_button, SIGNAL( clicked()),
00045         SLOT( deleteClicked()));
00046     connect( moveup_button, SIGNAL( clicked()),
00047         SLOT( moveupClicked()));
00048     connect( movedown_button, SIGNAL( clicked()),
00049         SLOT( movedownClicked()));
00050     connect( rules_listbox, SIGNAL( doubleClicked ( QListBoxItem * ) ),
00051             SLOT( modifyClicked()));
00052     load();
00053     }
00054 
00055 KCMRulesList::~KCMRulesList()
00056     {
00057     for( QValueVector< Rules* >::Iterator it = rules.begin();
00058          it != rules.end();
00059          ++it )
00060         delete *it;
00061     rules.clear();
00062     }
00063 
00064 void KCMRulesList::activeChanged( QListBoxItem* item )
00065     {
00066     if( item != NULL )
00067         rules_listbox->setSelected( item, true ); // make current==selected
00068     modify_button->setEnabled( item != NULL );
00069     delete_button->setEnabled( item != NULL );
00070     moveup_button->setEnabled( item != NULL && item->prev() != NULL );
00071     movedown_button->setEnabled( item != NULL && item->next() != NULL );
00072     }
00073 
00074 void KCMRulesList::newClicked()
00075     {
00076     RulesDialog dlg;
00077     Rules* rule = dlg.edit( NULL, 0 );
00078     if( rule == NULL )
00079         return;
00080     int pos = rules_listbox->currentItem() + 1;
00081     rules_listbox->insertItem( rule->description, pos );
00082     rules_listbox->setSelected( pos, true );
00083     rules.insert( rules.begin() + pos, rule );
00084     emit changed( true );
00085     }
00086 
00087 void KCMRulesList::modifyClicked()
00088     {
00089     int pos = rules_listbox->currentItem();
00090     if ( pos == -1 )
00091         return;
00092     RulesDialog dlg;
00093     Rules* rule = dlg.edit( rules[ pos ], 0 );
00094     if( rule == rules[ pos ] )
00095         return;
00096     delete rules[ pos ];
00097     rules[ pos ] = rule;
00098     rules_listbox->changeItem( rule->description, pos );
00099     emit changed( true );
00100     }
00101 
00102 void KCMRulesList::deleteClicked()
00103     {
00104     int pos = rules_listbox->currentItem();
00105     assert( pos != -1 );
00106     rules_listbox->removeItem( pos );
00107     rules.erase( rules.begin() + pos );
00108     emit changed( true );
00109     }
00110 
00111 void KCMRulesList::moveupClicked()
00112     {
00113     int pos = rules_listbox->currentItem();
00114     assert( pos != -1 );
00115     if( pos > 0 )
00116         {
00117         QString txt = rules_listbox->text( pos );
00118         rules_listbox->removeItem( pos );
00119         rules_listbox->insertItem( txt, pos - 1 );
00120         rules_listbox->setSelected( pos - 1, true );
00121         Rules* rule = rules[ pos ];
00122         rules[ pos ] = rules[ pos - 1 ];
00123         rules[ pos - 1 ] = rule;
00124         }
00125     emit changed( true );
00126     }
00127 
00128 void KCMRulesList::movedownClicked()
00129     {
00130     int pos = rules_listbox->currentItem();
00131     assert( pos != -1 );
00132     if( pos < int( rules_listbox->count()) - 1 )
00133         {
00134         QString txt = rules_listbox->text( pos );
00135         rules_listbox->removeItem( pos );
00136         rules_listbox->insertItem( txt, pos + 1 );
00137         rules_listbox->setSelected( pos + 1, true );
00138         Rules* rule = rules[ pos ];
00139         rules[ pos ] = rules[ pos + 1 ];
00140         rules[ pos + 1 ] = rule;
00141         }
00142     emit changed( true );
00143     }
00144 
00145 void KCMRulesList::load()
00146     {
00147     rules_listbox->clear();
00148     for( QValueVector< Rules* >::Iterator it = rules.begin();
00149          it != rules.end();
00150          ++it )
00151         delete *it;
00152     rules.clear();
00153     KConfig cfg( "kwinrulesrc", true );
00154     cfg.setGroup( "General" );
00155     int count = cfg.readNumEntry( "count" );
00156     rules.reserve( count );
00157     for( int i = 1;
00158          i <= count;
00159          ++i )
00160         {
00161         cfg.setGroup( QString::number( i ));
00162         Rules* rule = new Rules( cfg );
00163         rules.append( rule );
00164         rules_listbox->insertItem( rule->description );
00165         }
00166     if( rules.count() > 0 )
00167         rules_listbox->setSelected( 0, true );
00168     else
00169         activeChanged( NULL );
00170     }
00171 
00172 void KCMRulesList::save()
00173     {
00174     KConfig cfg( "kwinrulesrc" );
00175     cfg.setGroup( "General" );
00176     cfg.writeEntry( "count", rules.count());
00177     int i = 1;
00178     for( QValueVector< Rules* >::ConstIterator it = rules.begin();
00179          it != rules.end();
00180          ++it )
00181         {
00182         cfg.setGroup( QString::number( i ));
00183         (*it)->write( cfg );
00184         ++i;
00185         }
00186     }
00187 
00188 void KCMRulesList::defaults()
00189     {
00190     load();
00191     }
00192 
00193 } // namespace
00194 
00195 #include "ruleslist.moc"
KDE Logo
This file is part of the documentation for kwin Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Sep 30 18:40:43 2005 by doxygen 1.4.3 written by Dimitri van Heesch, © 1997-2003