1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/*
This file is part of libkdepim.
Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org>
Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "categoryeditdialog.h"
#include "ui_categoryeditdialog_base.h"
#include "categoryhierarchyreader.h"
#include "kpimprefs.h"
#include <KLocale>
#include <QHeaderView>
#include <QList>
#include <QStringList>
using namespace KPIM;
CategoryEditDialog::CategoryEditDialog( KPimPrefs *prefs, QWidget *parent )
: KDialog( parent ), mPrefs( prefs )
{
setCaption( i18n( "Edit Categories" ) );
setButtons( Ok | Apply | Cancel | Help );
mWidgets = new Ui::CategoryEditDialog_base();
QWidget *widget = new QWidget( this );
widget->setObjectName( QLatin1String( "CategoryEdit" ) );
mWidgets->setupUi( widget );
mWidgets->mCategories->header()->hide();
mWidgets->mButtonAdd->setIcon( KIcon( QLatin1String( "list-add" ) ) );
mWidgets->mButtonAddSubcategory->setIcon( KIcon( QLatin1String( "list-add" ) ) );
mWidgets->mButtonRemove->setIcon( KIcon( QLatin1String( "list-remove" ) ) );
// unfortunately, kde-core-devel will not allow this code in KDialog
// because these button's functionality cannot be easily generalized.
//TODO(KDE4.3): uncomment when strings are unfrozen
//setButtonToolTip( Ok, i18n( "Apply changes and close" ) );
//setButtonWhatsThis( Ok, i18n( "When clicking <b>Ok</b>, "
// "the settings will be handed over to the "
// "program and the dialog will be closed." ) );
//setButtonToolTip( Cancel, i18n( "Cancel changes and close" ) );
//setButtonWhatsThis( Cancel, i18n( "When clicking <b>Cancel</b>, "
// "the settings will be discarded and the "
// "dialog will be closed." ) );
//setButtonWhatsThis( Help, i18n( "When clicking <b>Help</b>, "
// "a separate KHelpCenter window will open "
// "providing more information about the settings." ) );
setMainWidget( widget );
fillList();
mWidgets->mCategories->setFocus();
connect( mWidgets->mCategories, SIGNAL(currentItemChanged(QTreeWidgetItem *,QTreeWidgetItem *)),
SLOT(editItem(QTreeWidgetItem *)) );
connect( mWidgets->mCategories, SIGNAL(itemSelectionChanged()),
SLOT(slotSelectionChanged()) );
connect( mWidgets->mCategories, SIGNAL(itemCollapsed(QTreeWidgetItem *)),
SLOT(expandIfToplevel(QTreeWidgetItem *)) );
connect( mWidgets->mEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(slotTextChanged(const QString &)) );
connect( mWidgets->mButtonAdd, SIGNAL(clicked()),
this, SLOT(add()) );
connect( mWidgets->mButtonAddSubcategory, SIGNAL(clicked()),
this, SLOT(addSubcategory()) );
connect( mWidgets->mButtonRemove, SIGNAL(clicked()),
this, SLOT(remove()) );
connect( this, SIGNAL(okClicked()), this, SLOT(slotOk()) );
connect( this, SIGNAL(cancelClicked()), this, SLOT(slotCancel()) );
connect( this, SIGNAL(applyClicked()), this, SLOT(slotApply()) );
}
CategoryEditDialog::~CategoryEditDialog()
{
delete mWidgets;
}
void CategoryEditDialog::fillList()
{
CategoryHierarchyReaderQTreeWidget( mWidgets->mCategories ).read( mPrefs->mCustomCategories );
mWidgets->mButtonRemove->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
mWidgets->mButtonAddSubcategory->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
}
void CategoryEditDialog::slotTextChanged( const QString &text )
{
QTreeWidgetItem *item = mWidgets->mCategories->currentItem();
if ( item ) {
item->setText( 0, text );
}
}
void CategoryEditDialog::slotSelectionChanged()
{
QTreeWidgetItemIterator it( mWidgets->mCategories, QTreeWidgetItemIterator::Selected );
mWidgets->mButtonRemove->setEnabled( *it );
}
void CategoryEditDialog::add()
{
if ( !mWidgets->mEdit->text().isEmpty() ) {
QTreeWidgetItem *newItem =
new QTreeWidgetItem( mWidgets->mCategories,
QStringList( i18n( "New category" ) ) );
newItem->setExpanded( true );
mWidgets->mCategories->setCurrentItem( newItem );
mWidgets->mCategories->clearSelection();
newItem->setSelected( true );
mWidgets->mCategories->scrollToItem( newItem );
mWidgets->mButtonRemove->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
mWidgets->mButtonAddSubcategory->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
mWidgets->mEdit->setFocus();
}
}
void CategoryEditDialog::addSubcategory()
{
if ( !mWidgets->mEdit->text().isEmpty() ) {
QTreeWidgetItem *newItem =
new QTreeWidgetItem( mWidgets->mCategories->currentItem(),
QStringList( i18n( "New category" ) ) );
newItem->setExpanded( true );
mWidgets->mCategories->setCurrentItem( newItem );
mWidgets->mCategories->clearSelection();
newItem->setSelected( true );
mWidgets->mCategories->scrollToItem( newItem );
mWidgets->mEdit->setFocus();
}
}
void CategoryEditDialog::remove()
{
QList<QTreeWidgetItem*> to_remove = mWidgets->mCategories->selectedItems();
while ( !to_remove.isEmpty() ) {
deleteItem( to_remove.takeFirst(), to_remove );
}
mWidgets->mButtonRemove->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
mWidgets->mButtonAddSubcategory->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
if ( mWidgets->mCategories->currentItem() ) {
mWidgets->mCategories->currentItem()->setSelected( true );
}
}
void CategoryEditDialog::deleteItem( QTreeWidgetItem *item, QList<QTreeWidgetItem *> &to_remove )
{
if ( !item ) {
return;
}
for ( int i = item->childCount() - 1; i >= 0; i-- ) {
QTreeWidgetItem *child = item->child( i );
to_remove.removeAll( child );
deleteItem( child, to_remove );
}
delete item;
}
void CategoryEditDialog::slotOk()
{
slotApply();
accept();
}
void CategoryEditDialog::slotApply()
{
mPrefs->mCustomCategories.clear();
QStringList path;
QTreeWidgetItemIterator it( mWidgets->mCategories );
while ( *it ) {
path = mWidgets->mCategories->pathByItem( *it++ );
path.replaceInStrings(
KPimPrefs::categorySeparator, QLatin1Char( '\\' ) + KPimPrefs::categorySeparator );
mPrefs->mCustomCategories.append( path.join( KPimPrefs::categorySeparator ) );
}
mPrefs->writeConfig();
emit categoryConfigChanged();
}
void CategoryEditDialog::slotCancel()
{
reload();
}
void CategoryEditDialog::editItem( QTreeWidgetItem *item )
{
if ( item ) {
mWidgets->mEdit->setText( item->text( 0 ) );
}
}
void CategoryEditDialog::reload()
{
fillList();
}
void CategoryEditDialog::show()
{
QTreeWidgetItem *first = 0;
if ( mWidgets->mCategories->topLevelItemCount() ) {
first = mWidgets->mCategories->topLevelItem( 0 );
mWidgets->mCategories->setCurrentItem( first );
}
mWidgets->mCategories->clearSelection();
if ( first ) {
first->setSelected( true );
editItem( first );
}
KDialog::show();
}
void CategoryEditDialog::expandIfToplevel( QTreeWidgetItem *item )
{
if ( !item->parent() ) {
item->setExpanded( true );
}
}
#include "categoryeditdialog.moc"
|