2
This file is part of libkdepim.
4
Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org>
5
Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6
Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
8
This library is free software; you can redistribute it and/or
9
modify it under the terms of the GNU Library General Public
10
License as published by the Free Software Foundation; either
11
version 2 of the License, or (at your option) any later version.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Library General Public License for more details.
18
You should have received a copy of the GNU Library General Public License
19
along with this library; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
Boston, MA 02110-1301, USA.
24
#include "categoryeditdialog.h"
26
#include "categoryhierarchyreader.h"
27
#include "ui_categoryeditdialog_base.h"
29
#include <calendarsupport/categoryconfig.h>
33
#include <QHeaderView>
35
#include <QStringList>
37
using namespace IncidenceEditorNG;
38
using namespace CalendarSupport;
40
CategoryEditDialog::CategoryEditDialog( CategoryConfig *categoryConfig,
42
: KDialog( parent ), mCategoryConfig( categoryConfig )
44
setCaption( i18n( "Edit Categories" ) );
45
setButtons( Ok /*| Apply*/ | Cancel | Help );
46
mWidgets = new Ui::CategoryEditDialog_base();
47
QWidget *widget = new QWidget( this );
48
widget->setObjectName( "CategoryEdit" );
49
mWidgets->setupUi( widget );
51
mWidgets->mCategories->header()->hide();
52
mWidgets->mButtonAdd->setIcon( KIcon( "list-add" ) );
53
mWidgets->mButtonAddSubcategory->setIcon( KIcon( "list-add" ) );
54
mWidgets->mButtonRemove->setIcon( KIcon( "list-remove" ) );
56
#ifndef KDEPIM_MOBILE_UI
57
mWidgets->mCategories->setDragDropMode( QAbstractItemView::InternalMove );
60
// unfortunately, kde-core-devel will not allow this code in KDialog
61
// because these button's functionality cannot be easily generalized.
62
setButtonToolTip( Ok, i18n( "Apply changes and close" ) );
63
setButtonWhatsThis( Ok, i18n( "When clicking <b>Ok</b>, "
64
"the settings will be handed over to the "
65
"program and the dialog will be closed." ) );
66
setButtonToolTip( Cancel, i18n( "Cancel changes and close" ) );
67
setButtonWhatsThis( Cancel, i18n( "When clicking <b>Cancel</b>, "
68
"the settings will be discarded and the "
69
"dialog will be closed." ) );
71
setButtonWhatsThis( Help, i18n( "When clicking <b>Help</b>, "
72
"a separate KHelpCenter window will open "
73
"providing more information about the settings." ) );
75
setMainWidget( widget );
79
mWidgets->mCategories->setFocus();
81
connect( mWidgets->mCategories, SIGNAL(currentItemChanged(QTreeWidgetItem *,QTreeWidgetItem *)),
82
SLOT(editItem(QTreeWidgetItem *)) );
83
connect( mWidgets->mCategories, SIGNAL(itemSelectionChanged()),
84
SLOT(slotSelectionChanged()) );
85
connect( mWidgets->mCategories, SIGNAL(itemCollapsed(QTreeWidgetItem *)),
86
SLOT(expandIfToplevel(QTreeWidgetItem *)) );
87
connect( mWidgets->mEdit, SIGNAL(textChanged(const QString &)),
88
this, SLOT(slotTextChanged(const QString &)) );
89
connect( mWidgets->mButtonAdd, SIGNAL(clicked()),
91
connect( mWidgets->mButtonAddSubcategory, SIGNAL(clicked()),
92
this, SLOT(addSubcategory()) );
93
connect( mWidgets->mButtonRemove, SIGNAL(clicked()),
94
this, SLOT(remove()) );
95
connect( this, SIGNAL(okClicked()), this, SLOT(slotOk()) );
96
connect( this, SIGNAL(cancelClicked()), this, SLOT(slotCancel()) );
97
//connect( this, SIGNAL(applyClicked()), this, SLOT(slotApply()) );
100
CategoryEditDialog::~CategoryEditDialog()
105
void CategoryEditDialog::fillList()
107
IncidenceEditorNG::CategoryHierarchyReaderQTreeWidget(
108
mWidgets->mCategories ).read( mCategoryConfig->customCategories() );
110
mWidgets->mButtonRemove->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
111
mWidgets->mButtonAddSubcategory->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
114
void CategoryEditDialog::slotTextChanged( const QString &text )
116
QTreeWidgetItem *item = mWidgets->mCategories->currentItem();
118
item->setText( 0, text );
122
void CategoryEditDialog::slotSelectionChanged()
124
QTreeWidgetItemIterator it( mWidgets->mCategories, QTreeWidgetItemIterator::Selected );
125
mWidgets->mButtonRemove->setEnabled( *it );
128
void CategoryEditDialog::add()
130
if ( !mWidgets->mEdit->text().isEmpty() ) {
131
QTreeWidgetItem *newItem =
132
new QTreeWidgetItem( mWidgets->mCategories,
133
QStringList( i18n( "New category" ) ) );
134
newItem->setExpanded( true );
136
mWidgets->mCategories->setCurrentItem( newItem );
137
mWidgets->mCategories->clearSelection();
138
newItem->setSelected( true );
139
mWidgets->mCategories->scrollToItem( newItem );
140
mWidgets->mButtonRemove->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
141
mWidgets->mButtonAddSubcategory->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
142
mWidgets->mEdit->setFocus();
146
void CategoryEditDialog::addSubcategory()
148
if ( !mWidgets->mEdit->text().isEmpty() ) {
149
QTreeWidgetItem *newItem =
150
new QTreeWidgetItem( mWidgets->mCategories->currentItem(),
151
QStringList( i18n( "New subcategory" ) ) );
152
newItem->setExpanded( true );
154
mWidgets->mCategories->setCurrentItem( newItem );
155
mWidgets->mCategories->clearSelection();
156
newItem->setSelected( true );
157
mWidgets->mCategories->scrollToItem( newItem );
158
mWidgets->mEdit->setFocus();
162
void CategoryEditDialog::remove()
164
QList<QTreeWidgetItem*> to_remove = mWidgets->mCategories->selectedItems();
165
while ( !to_remove.isEmpty() ) {
166
deleteItem( to_remove.takeFirst(), to_remove );
169
mWidgets->mButtonRemove->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
170
mWidgets->mButtonAddSubcategory->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
171
if ( mWidgets->mCategories->currentItem() ) {
172
mWidgets->mCategories->currentItem()->setSelected( true );
176
void CategoryEditDialog::deleteItem( QTreeWidgetItem *item, QList<QTreeWidgetItem *> &to_remove )
182
for ( int i = item->childCount() - 1; i >= 0; i-- ) {
183
QTreeWidgetItem *child = item->child( i );
184
to_remove.removeAll( child );
185
deleteItem( child, to_remove );
190
void CategoryEditDialog::slotOk()
196
void CategoryEditDialog::slotApply()
201
QTreeWidgetItemIterator it( mWidgets->mCategories );
203
path = mWidgets->mCategories->pathByItem( *it++ );
204
path.replaceInStrings(
205
CategoryConfig::categorySeparator,
206
QString( "\\" ) + CategoryConfig::categorySeparator );
207
l.append( path.join( CategoryConfig::categorySeparator ) );
209
mCategoryConfig->setCustomCategories( l );
210
mCategoryConfig->writeConfig();
212
emit categoryConfigChanged();
215
void CategoryEditDialog::slotCancel()
220
void CategoryEditDialog::editItem( QTreeWidgetItem *item )
223
mWidgets->mEdit->setText( item->text( 0 ) );
227
void CategoryEditDialog::reload()
232
void CategoryEditDialog::show()
234
QTreeWidgetItem *first = 0;
235
if ( mWidgets->mCategories->topLevelItemCount() ) {
236
first = mWidgets->mCategories->topLevelItem( 0 );
237
mWidgets->mCategories->setCurrentItem( first );
239
mWidgets->mCategories->clearSelection();
241
first->setSelected( true );
247
void CategoryEditDialog::expandIfToplevel( QTreeWidgetItem *item )
249
if ( !item->parent() ) {
250
item->setExpanded( true );
254
#include "categoryeditdialog.moc"