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 00051 load(); 00052 } 00053 00054 KCMRulesList::~KCMRulesList() 00055 { 00056 for( QValueVector< Rules* >::Iterator it = rules.begin(); 00057 it != rules.end(); 00058 ++it ) 00059 delete *it; 00060 rules.clear(); 00061 } 00062 00063 void KCMRulesList::activeChanged( QListBoxItem* item ) 00064 { 00065 if( item != NULL ) 00066 rules_listbox->setSelected( item, true ); // make current==selected 00067 modify_button->setEnabled( item != NULL ); 00068 delete_button->setEnabled( item != NULL ); 00069 moveup_button->setEnabled( item != NULL && item->prev() != NULL ); 00070 movedown_button->setEnabled( item != NULL && item->next() != NULL ); 00071 } 00072 00073 void KCMRulesList::newClicked() 00074 { 00075 RulesDialog dlg; 00076 Rules* rule = dlg.edit( NULL ); 00077 if( rule == NULL ) 00078 return; 00079 int pos = rules_listbox->currentItem() + 1; 00080 rules_listbox->insertItem( rule->description, pos ); 00081 rules_listbox->setSelected( pos, true ); 00082 rules.insert( rules.begin() + pos, rule ); 00083 emit changed( true ); 00084 } 00085 00086 void KCMRulesList::modifyClicked() 00087 { 00088 int pos = rules_listbox->currentItem(); 00089 assert( pos != -1 ); 00090 RulesDialog dlg; 00091 Rules* rule = dlg.edit( rules[ pos ] ); 00092 if( rule == rules[ pos ] ) 00093 return; 00094 delete rules[ pos ]; 00095 rules[ pos ] = rule; 00096 rules_listbox->changeItem( rule->description, pos ); 00097 emit changed( true ); 00098 } 00099 00100 void KCMRulesList::deleteClicked() 00101 { 00102 int pos = rules_listbox->currentItem(); 00103 assert( pos != -1 ); 00104 rules_listbox->removeItem( pos ); 00105 rules.erase( rules.begin() + pos ); 00106 emit changed( true ); 00107 } 00108 00109 void KCMRulesList::moveupClicked() 00110 { 00111 int pos = rules_listbox->currentItem(); 00112 assert( pos != -1 ); 00113 if( pos > 0 ) 00114 { 00115 QString txt = rules_listbox->text( pos ); 00116 rules_listbox->removeItem( pos ); 00117 rules_listbox->insertItem( txt, pos - 1 ); 00118 rules_listbox->setSelected( pos - 1, true ); 00119 Rules* rule = rules[ pos ]; 00120 rules[ pos ] = rules[ pos - 1 ]; 00121 rules[ pos - 1 ] = rule; 00122 } 00123 emit changed( true ); 00124 } 00125 00126 void KCMRulesList::movedownClicked() 00127 { 00128 int pos = rules_listbox->currentItem(); 00129 assert( pos != -1 ); 00130 if( pos < int( rules_listbox->count()) - 1 ) 00131 { 00132 QString txt = rules_listbox->text( pos ); 00133 rules_listbox->removeItem( pos ); 00134 rules_listbox->insertItem( txt, pos + 1 ); 00135 rules_listbox->setSelected( pos + 1, true ); 00136 Rules* rule = rules[ pos ]; 00137 rules[ pos ] = rules[ pos + 1 ]; 00138 rules[ pos + 1 ] = rule; 00139 } 00140 emit changed( true ); 00141 } 00142 00143 void KCMRulesList::load() 00144 { 00145 rules_listbox->clear(); 00146 for( QValueVector< Rules* >::Iterator it = rules.begin(); 00147 it != rules.end(); 00148 ++it ) 00149 delete *it; 00150 rules.clear(); 00151 KConfig cfg( "kwinrulesrc", true ); 00152 cfg.setGroup( "General" ); 00153 int count = cfg.readNumEntry( "count" ); 00154 rules.reserve( count ); 00155 for( int i = 1; 00156 i <= count; 00157 ++i ) 00158 { 00159 cfg.setGroup( QString::number( i )); 00160 Rules* rule = new Rules( cfg ); 00161 rules.append( rule ); 00162 rules_listbox->insertItem( rule->description ); 00163 } 00164 if( rules.count() > 0 ) 00165 rules_listbox->setSelected( 0, true ); 00166 else 00167 activeChanged( NULL ); 00168 } 00169 00170 void KCMRulesList::save() 00171 { 00172 KConfig cfg( "kwinrulesrc" ); 00173 cfg.setGroup( "General" ); 00174 cfg.writeEntry( "count", rules.count()); 00175 int i = 1; 00176 for( QValueVector< Rules* >::ConstIterator it = rules.begin(); 00177 it != rules.end(); 00178 ++it ) 00179 { 00180 cfg.setGroup( QString::number( i )); 00181 (*it)->write( cfg ); 00182 ++i; 00183 } 00184 } 00185 00186 void KCMRulesList::defaults() 00187 { 00188 load(); 00189 } 00190 00191 } // namespace 00192 00193 #include "ruleslist.moc"
KDE Logo
This file is part of the documentation for kwin Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 13 21:47:06 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003