2004-06-21 16:25:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-12-21 06:45:09 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-06-21 16:25:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ruleslist.h"
|
|
|
|
|
2007-02-12 19:39:15 +00:00
|
|
|
#include <klistwidget.h>
|
2004-06-21 16:25:24 +00:00
|
|
|
#include <kpushbutton.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kconfig.h>
|
|
|
|
|
|
|
|
#include "ruleswidget.h"
|
|
|
|
|
|
|
|
namespace KWinInternal
|
|
|
|
{
|
|
|
|
|
2006-11-03 16:35:38 +00:00
|
|
|
KCMRulesList::KCMRulesList( QWidget* parent)
|
|
|
|
: KCMRulesListBase( parent)
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
|
|
|
// connect both current/selected, so that current==selected (stupid QListBox :( )
|
2007-02-12 19:39:15 +00:00
|
|
|
connect( rules_listbox, SIGNAL( currentChanged( QListWidgetItem* )),
|
|
|
|
SLOT( activeChanged( QListWidgetItem*)));
|
|
|
|
connect( rules_listbox, SIGNAL( selectionChanged( QListWidgetItem* )),
|
|
|
|
SLOT( activeChanged( QListWidgetItem*)));
|
2004-06-21 16:25:24 +00:00
|
|
|
connect( new_button, SIGNAL( clicked()),
|
|
|
|
SLOT( newClicked()));
|
|
|
|
connect( modify_button, SIGNAL( clicked()),
|
|
|
|
SLOT( modifyClicked()));
|
|
|
|
connect( delete_button, SIGNAL( clicked()),
|
|
|
|
SLOT( deleteClicked()));
|
|
|
|
connect( moveup_button, SIGNAL( clicked()),
|
|
|
|
SLOT( moveupClicked()));
|
|
|
|
connect( movedown_button, SIGNAL( clicked()),
|
|
|
|
SLOT( movedownClicked()));
|
2007-02-12 19:39:15 +00:00
|
|
|
connect( rules_listbox, SIGNAL( doubleClicked ( QListWidgetItem * ) ),
|
2004-08-30 14:04:49 +00:00
|
|
|
SLOT( modifyClicked()));
|
2004-06-21 16:25:24 +00:00
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
KCMRulesList::~KCMRulesList()
|
|
|
|
{
|
2005-09-25 14:53:55 +00:00
|
|
|
for( QVector< Rules* >::Iterator it = rules.begin();
|
2004-06-21 16:25:24 +00:00
|
|
|
it != rules.end();
|
|
|
|
++it )
|
|
|
|
delete *it;
|
|
|
|
rules.clear();
|
|
|
|
}
|
|
|
|
|
2007-02-12 19:39:15 +00:00
|
|
|
void KCMRulesList::activeChanged( QListWidgetItem* item )
|
2004-06-21 16:25:24 +00:00
|
|
|
{
|
2007-02-12 19:39:15 +00:00
|
|
|
int itemRow = rules_listbox->row(item);
|
|
|
|
|
2004-06-21 16:25:24 +00:00
|
|
|
if( item != NULL )
|
2007-02-12 19:39:15 +00:00
|
|
|
item->setSelected( true ); // make current==selected
|
2004-06-21 16:25:24 +00:00
|
|
|
modify_button->setEnabled( item != NULL );
|
|
|
|
delete_button->setEnabled( item != NULL );
|
2007-02-12 19:39:15 +00:00
|
|
|
moveup_button->setEnabled( item != NULL && itemRow > 0 );
|
|
|
|
movedown_button->setEnabled( item != NULL && itemRow < (rules_listbox->count()-1) );
|
2004-06-21 16:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::newClicked()
|
|
|
|
{
|
|
|
|
RulesDialog dlg;
|
2005-08-19 13:46:45 +00:00
|
|
|
Rules* rule = dlg.edit( NULL, 0, false );
|
2004-06-21 16:25:24 +00:00
|
|
|
if( rule == NULL )
|
|
|
|
return;
|
2007-02-12 19:39:15 +00:00
|
|
|
int pos = rules_listbox->currentRow() + 1;
|
|
|
|
rules_listbox->insertItem( pos , rule->description );
|
|
|
|
rules_listbox->item(pos)->setSelected( true );
|
2004-06-21 16:25:24 +00:00
|
|
|
rules.insert( rules.begin() + pos, rule );
|
|
|
|
emit changed( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::modifyClicked()
|
|
|
|
{
|
2007-02-12 19:39:15 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2004-08-30 14:04:49 +00:00
|
|
|
if ( pos == -1 )
|
|
|
|
return;
|
2004-06-21 16:25:24 +00:00
|
|
|
RulesDialog dlg;
|
2005-08-19 13:46:45 +00:00
|
|
|
Rules* rule = dlg.edit( rules[ pos ], 0, false );
|
2004-06-21 16:25:24 +00:00
|
|
|
if( rule == rules[ pos ] )
|
|
|
|
return;
|
|
|
|
delete rules[ pos ];
|
|
|
|
rules[ pos ] = rule;
|
2007-02-12 19:39:15 +00:00
|
|
|
rules_listbox->item(pos)->setText( rule->description );
|
2004-06-21 16:25:24 +00:00
|
|
|
emit changed( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::deleteClicked()
|
|
|
|
{
|
2007-02-12 19:39:15 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2004-06-21 16:25:24 +00:00
|
|
|
assert( pos != -1 );
|
2007-02-12 19:39:15 +00:00
|
|
|
delete rules_listbox->takeItem( pos );
|
2004-06-21 16:25:24 +00:00
|
|
|
rules.erase( rules.begin() + pos );
|
|
|
|
emit changed( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::moveupClicked()
|
|
|
|
{
|
2007-02-12 19:39:15 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2004-06-21 16:25:24 +00:00
|
|
|
assert( pos != -1 );
|
|
|
|
if( pos > 0 )
|
|
|
|
{
|
2007-02-12 19:39:15 +00:00
|
|
|
QString txt = rules_listbox->item(pos)->text();
|
|
|
|
delete rules_listbox->takeItem( pos );
|
|
|
|
rules_listbox->insertItem( pos - 1 , txt );
|
|
|
|
rules_listbox->item(pos-1)->setSelected( true );
|
2004-06-21 16:25:24 +00:00
|
|
|
Rules* rule = rules[ pos ];
|
|
|
|
rules[ pos ] = rules[ pos - 1 ];
|
|
|
|
rules[ pos - 1 ] = rule;
|
|
|
|
}
|
|
|
|
emit changed( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::movedownClicked()
|
|
|
|
{
|
2007-02-12 19:39:15 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2004-06-21 16:25:24 +00:00
|
|
|
assert( pos != -1 );
|
|
|
|
if( pos < int( rules_listbox->count()) - 1 )
|
|
|
|
{
|
2007-02-12 19:39:15 +00:00
|
|
|
QString txt = rules_listbox->item(pos)->text();
|
|
|
|
delete rules_listbox->takeItem( pos );
|
|
|
|
rules_listbox->insertItem( pos + 1 , txt);
|
|
|
|
rules_listbox->item(pos+1)->setSelected( true );
|
2004-06-21 16:25:24 +00:00
|
|
|
Rules* rule = rules[ pos ];
|
|
|
|
rules[ pos ] = rules[ pos + 1 ];
|
|
|
|
rules[ pos + 1 ] = rule;
|
|
|
|
}
|
|
|
|
emit changed( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::load()
|
|
|
|
{
|
|
|
|
rules_listbox->clear();
|
2005-09-25 14:53:55 +00:00
|
|
|
for( QVector< Rules* >::Iterator it = rules.begin();
|
2004-06-21 16:25:24 +00:00
|
|
|
it != rules.end();
|
|
|
|
++it )
|
|
|
|
delete *it;
|
|
|
|
rules.clear();
|
|
|
|
KConfig cfg( "kwinrulesrc", true );
|
|
|
|
cfg.setGroup( "General" );
|
2006-01-21 19:36:31 +00:00
|
|
|
int count = cfg.readEntry( "count",0 );
|
2004-06-21 16:25:24 +00:00
|
|
|
rules.reserve( count );
|
|
|
|
for( int i = 1;
|
|
|
|
i <= count;
|
|
|
|
++i )
|
|
|
|
{
|
|
|
|
cfg.setGroup( QString::number( i ));
|
|
|
|
Rules* rule = new Rules( cfg );
|
|
|
|
rules.append( rule );
|
2007-02-12 19:39:15 +00:00
|
|
|
rules_listbox->addItem( rule->description );
|
2004-06-21 16:25:24 +00:00
|
|
|
}
|
|
|
|
if( rules.count() > 0 )
|
2007-02-12 19:39:15 +00:00
|
|
|
rules_listbox->item(0)->setSelected( true );
|
2004-06-21 16:25:24 +00:00
|
|
|
else
|
|
|
|
activeChanged( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::save()
|
|
|
|
{
|
2006-07-04 08:26:08 +00:00
|
|
|
KConfig cfg( QLatin1String("kwinrulesrc") );
|
2006-02-06 20:44:10 +00:00
|
|
|
QStringList groups = cfg.groupList();
|
|
|
|
for( QStringList::ConstIterator it = groups.begin();
|
|
|
|
it != groups.end();
|
|
|
|
++it )
|
|
|
|
cfg.deleteGroup( *it );
|
2004-06-21 16:25:24 +00:00
|
|
|
cfg.setGroup( "General" );
|
|
|
|
cfg.writeEntry( "count", rules.count());
|
|
|
|
int i = 1;
|
2005-09-25 14:53:55 +00:00
|
|
|
for( QVector< Rules* >::ConstIterator it = rules.begin();
|
2004-06-21 16:25:24 +00:00
|
|
|
it != rules.end();
|
|
|
|
++it )
|
|
|
|
{
|
|
|
|
cfg.setGroup( QString::number( i ));
|
|
|
|
(*it)->write( cfg );
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::defaults()
|
|
|
|
{
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#include "ruleslist.moc"
|