add more spacing
[personal-kdebase.git] / workspace / kwin / kcmkwin / kwinrules / ruleslist.cpp
blob028ad185f7976997651b3d4cf5d40b8da981a30e
1 /*
2 * Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "ruleslist.h"
21 #include <klistwidget.h>
22 #include <kpushbutton.h>
23 #include <assert.h>
24 #include <kdebug.h>
25 #include <kconfig.h>
27 #include "ruleswidget.h"
29 namespace KWin
32 KCMRulesList::KCMRulesList( QWidget* parent)
33 : QWidget( parent)
35 setupUi( this );
36 // connect both current/selected, so that current==selected (stupid QListBox :( )
37 connect( rules_listbox, SIGNAL(itemChanged(QListWidgetItem*)),
38 SLOT(activeChanged()));
39 connect( rules_listbox, SIGNAL(itemSelectionChanged()),
40 SLOT( activeChanged()));
41 connect( new_button, SIGNAL( clicked()),
42 SLOT( newClicked()));
43 connect( modify_button, SIGNAL( clicked()),
44 SLOT( modifyClicked()));
45 connect( delete_button, SIGNAL( clicked()),
46 SLOT( deleteClicked()));
47 connect( moveup_button, SIGNAL( clicked()),
48 SLOT( moveupClicked()));
49 connect( movedown_button, SIGNAL( clicked()),
50 SLOT( movedownClicked()));
51 connect( rules_listbox, SIGNAL(itemDoubleClicked(QListWidgetItem*) ),
52 SLOT( modifyClicked()));
53 load();
56 KCMRulesList::~KCMRulesList()
58 for( QVector< Rules* >::Iterator it = rules.begin();
59 it != rules.end();
60 ++it )
61 delete *it;
62 rules.clear();
65 void KCMRulesList::activeChanged()
67 QListWidgetItem *item = rules_listbox->currentItem();
68 int itemRow = rules_listbox->row(item);
70 if( item != NULL )
71 item->setSelected( true ); // make current==selected
72 modify_button->setEnabled( item != NULL );
73 delete_button->setEnabled( item != NULL );
74 moveup_button->setEnabled( item != NULL && itemRow > 0 );
75 movedown_button->setEnabled( item != NULL && itemRow < (rules_listbox->count()-1) );
78 void KCMRulesList::newClicked()
80 RulesDialog dlg(this);
81 Rules* rule = dlg.edit( NULL, 0, false );
82 if( rule == NULL )
83 return;
84 int pos = rules_listbox->currentRow() + 1;
85 rules_listbox->insertItem( pos , rule->description );
86 rules_listbox->item(pos)->setSelected( true );
87 rules.insert( rules.begin() + pos, rule );
88 emit changed( true );
91 void KCMRulesList::modifyClicked()
93 int pos = rules_listbox->currentRow();
94 if ( pos == -1 )
95 return;
96 RulesDialog dlg(this);
97 Rules* rule = dlg.edit( rules[ pos ], 0, false );
98 if( rule == rules[ pos ] )
99 return;
100 delete rules[ pos ];
101 rules[ pos ] = rule;
102 rules_listbox->item(pos)->setText( rule->description );
103 emit changed( true );
106 void KCMRulesList::deleteClicked()
108 int pos = rules_listbox->currentRow();
109 assert( pos != -1 );
110 delete rules_listbox->takeItem( pos );
111 rules.erase( rules.begin() + pos );
112 emit changed( true );
115 void KCMRulesList::moveupClicked()
117 int pos = rules_listbox->currentRow();
118 assert( pos != -1 );
119 if( pos > 0 )
121 QString txt = rules_listbox->item(pos)->text();
122 delete rules_listbox->takeItem( pos );
123 rules_listbox->insertItem( pos - 1 , txt );
124 rules_listbox->item(pos-1)->setSelected( true );
125 Rules* rule = rules[ pos ];
126 rules[ pos ] = rules[ pos - 1 ];
127 rules[ pos - 1 ] = rule;
129 emit changed( true );
132 void KCMRulesList::movedownClicked()
134 int pos = rules_listbox->currentRow();
135 assert( pos != -1 );
136 if( pos < int( rules_listbox->count()) - 1 )
138 QString txt = rules_listbox->item(pos)->text();
139 delete rules_listbox->takeItem( pos );
140 rules_listbox->insertItem( pos + 1 , txt);
141 rules_listbox->item(pos+1)->setSelected( true );
142 Rules* rule = rules[ pos ];
143 rules[ pos ] = rules[ pos + 1 ];
144 rules[ pos + 1 ] = rule;
146 emit changed( true );
149 void KCMRulesList::load()
151 rules_listbox->clear();
152 for( QVector< Rules* >::Iterator it = rules.begin();
153 it != rules.end();
154 ++it )
155 delete *it;
156 rules.clear();
157 KConfig _cfg( "kwinrulesrc" );
158 KConfigGroup cfg(&_cfg, "General" );
159 int count = cfg.readEntry( "count",0 );
160 rules.reserve( count );
161 for( int i = 1;
162 i <= count;
163 ++i )
165 cfg = KConfigGroup(&_cfg,QString::number( i ));
166 Rules* rule = new Rules( cfg );
167 rules.append( rule );
168 rules_listbox->addItem( rule->description );
170 if( rules.count() > 0 )
171 rules_listbox->setCurrentItem( rules_listbox->item( 0 ));
172 else
173 rules_listbox->setCurrentItem( NULL );
174 activeChanged();
177 void KCMRulesList::save()
179 KConfig cfg( QLatin1String("kwinrulesrc") );
180 QStringList groups = cfg.groupList();
181 for( QStringList::ConstIterator it = groups.constBegin();
182 it != groups.constEnd();
183 ++it )
184 cfg.deleteGroup( *it );
185 cfg.group("General").writeEntry( "count", rules.count());
186 int i = 1;
187 for( QVector< Rules* >::ConstIterator it = rules.constBegin();
188 it != rules.constEnd();
189 ++it )
191 KConfigGroup cg( &cfg, QString::number( i ));
192 (*it)->write( cg );
193 ++i;
197 void KCMRulesList::defaults()
199 load();
202 } // namespace
204 #include "ruleslist.moc"