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>
27 #include "ruleswidget.h"
32 KCMRulesList::KCMRulesList( QWidget
* parent
)
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()),
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()));
56 KCMRulesList::~KCMRulesList()
58 for( QVector
< Rules
* >::Iterator it
= rules
.begin();
65 void KCMRulesList::activeChanged()
67 QListWidgetItem
*item
= rules_listbox
->currentItem();
68 int itemRow
= rules_listbox
->row(item
);
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 );
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
);
91 void KCMRulesList::modifyClicked()
93 int pos
= rules_listbox
->currentRow();
96 RulesDialog
dlg(this);
97 Rules
* rule
= dlg
.edit( rules
[ pos
], 0, false );
98 if( rule
== rules
[ pos
] )
102 rules_listbox
->item(pos
)->setText( rule
->description
);
103 emit
changed( true );
106 void KCMRulesList::deleteClicked()
108 int pos
= rules_listbox
->currentRow();
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();
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();
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();
157 KConfig
_cfg( "kwinrulesrc" );
158 KConfigGroup
cfg(&_cfg
, "General" );
159 int count
= cfg
.readEntry( "count",0 );
160 rules
.reserve( count
);
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 ));
173 rules_listbox
->setCurrentItem( NULL
);
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();
184 cfg
.deleteGroup( *it
);
185 cfg
.group("General").writeEntry( "count", rules
.count());
187 for( QVector
< Rules
* >::ConstIterator it
= rules
.constBegin();
188 it
!= rules
.constEnd();
191 KConfigGroup
cg( &cfg
, QString::number( i
));
197 void KCMRulesList::defaults()
204 #include "ruleslist.moc"