~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to incidenceeditor-ng/categoryeditdialog.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of libkdepim.
 
3
 
 
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>
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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.
 
22
*/
 
23
 
 
24
#include "categoryeditdialog.h"
 
25
 
 
26
#include "categoryhierarchyreader.h"
 
27
#include "ui_categoryeditdialog_base.h"
 
28
 
 
29
#include <calendarsupport/categoryconfig.h>
 
30
 
 
31
#include <KLocale>
 
32
 
 
33
#include <QHeaderView>
 
34
#include <QList>
 
35
#include <QStringList>
 
36
 
 
37
using namespace IncidenceEditorNG;
 
38
using namespace CalendarSupport;
 
39
 
 
40
CategoryEditDialog::CategoryEditDialog( CategoryConfig *categoryConfig,
 
41
                                        QWidget *parent )
 
42
  : KDialog( parent ), mCategoryConfig( categoryConfig )
 
43
{
 
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 );
 
50
 
 
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" ) );
 
55
 
 
56
#ifndef KDEPIM_MOBILE_UI
 
57
  mWidgets->mCategories->setDragDropMode( QAbstractItemView::InternalMove );
 
58
#endif
 
59
 
 
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." ) );
 
70
 
 
71
  setButtonWhatsThis( Help, i18n( "When clicking <b>Help</b>, "
 
72
                                  "a separate KHelpCenter window will open "
 
73
                                  "providing more information about the settings." ) );
 
74
 
 
75
  setMainWidget( widget );
 
76
 
 
77
  fillList();
 
78
 
 
79
  mWidgets->mCategories->setFocus();
 
80
 
 
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()),
 
90
           this, SLOT(add()) );
 
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()) );
 
98
}
 
99
 
 
100
CategoryEditDialog::~CategoryEditDialog()
 
101
{
 
102
  delete mWidgets;
 
103
}
 
104
 
 
105
void CategoryEditDialog::fillList()
 
106
{
 
107
  IncidenceEditorNG::CategoryHierarchyReaderQTreeWidget(
 
108
    mWidgets->mCategories ).read( mCategoryConfig->customCategories() );
 
109
 
 
110
  mWidgets->mButtonRemove->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
 
111
  mWidgets->mButtonAddSubcategory->setEnabled( mWidgets->mCategories->topLevelItemCount() > 0 );
 
112
}
 
113
 
 
114
void CategoryEditDialog::slotTextChanged( const QString &text )
 
115
{
 
116
  QTreeWidgetItem *item = mWidgets->mCategories->currentItem();
 
117
  if ( item ) {
 
118
    item->setText( 0, text );
 
119
  }
 
120
}
 
121
 
 
122
void CategoryEditDialog::slotSelectionChanged()
 
123
{
 
124
  QTreeWidgetItemIterator it( mWidgets->mCategories, QTreeWidgetItemIterator::Selected );
 
125
  mWidgets->mButtonRemove->setEnabled( *it );
 
126
}
 
127
 
 
128
void CategoryEditDialog::add()
 
129
{
 
130
  if ( !mWidgets->mEdit->text().isEmpty() ) {
 
131
    QTreeWidgetItem *newItem =
 
132
      new QTreeWidgetItem( mWidgets->mCategories,
 
133
                           QStringList( i18n( "New category" ) ) );
 
134
    newItem->setExpanded( true );
 
135
 
 
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();
 
143
  }
 
144
}
 
145
 
 
146
void CategoryEditDialog::addSubcategory()
 
147
{
 
148
  if ( !mWidgets->mEdit->text().isEmpty() ) {
 
149
    QTreeWidgetItem *newItem =
 
150
      new QTreeWidgetItem( mWidgets->mCategories->currentItem(),
 
151
                           QStringList( i18n( "New subcategory" ) ) );
 
152
    newItem->setExpanded( true );
 
153
 
 
154
    mWidgets->mCategories->setCurrentItem( newItem );
 
155
    mWidgets->mCategories->clearSelection();
 
156
    newItem->setSelected( true );
 
157
    mWidgets->mCategories->scrollToItem( newItem );
 
158
    mWidgets->mEdit->setFocus();
 
159
  }
 
160
}
 
161
 
 
162
void CategoryEditDialog::remove()
 
163
{
 
164
  QList<QTreeWidgetItem*> to_remove = mWidgets->mCategories->selectedItems();
 
165
  while ( !to_remove.isEmpty() ) {
 
166
    deleteItem( to_remove.takeFirst(), to_remove );
 
167
  }
 
168
 
 
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 );
 
173
  }
 
174
}
 
175
 
 
176
void CategoryEditDialog::deleteItem( QTreeWidgetItem *item, QList<QTreeWidgetItem *> &to_remove )
 
177
{
 
178
  if ( !item ) {
 
179
    return;
 
180
  }
 
181
 
 
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 );
 
186
  }
 
187
  delete item;
 
188
}
 
189
 
 
190
void CategoryEditDialog::slotOk()
 
191
{
 
192
  slotApply();
 
193
  accept();
 
194
}
 
195
 
 
196
void CategoryEditDialog::slotApply()
 
197
{
 
198
  QStringList l;
 
199
 
 
200
  QStringList path;
 
201
  QTreeWidgetItemIterator it( mWidgets->mCategories );
 
202
  while ( *it ) {
 
203
    path = mWidgets->mCategories->pathByItem( *it++ );
 
204
    path.replaceInStrings(
 
205
      CategoryConfig::categorySeparator,
 
206
      QString( "\\" ) + CategoryConfig::categorySeparator );
 
207
    l.append( path.join( CategoryConfig::categorySeparator ) );
 
208
  }
 
209
  mCategoryConfig->setCustomCategories( l );
 
210
  mCategoryConfig->writeConfig();
 
211
 
 
212
  emit categoryConfigChanged();
 
213
}
 
214
 
 
215
void CategoryEditDialog::slotCancel()
 
216
{
 
217
  reload();
 
218
}
 
219
 
 
220
void CategoryEditDialog::editItem( QTreeWidgetItem *item )
 
221
{
 
222
  if ( item ) {
 
223
    mWidgets->mEdit->setText( item->text( 0 ) );
 
224
  }
 
225
}
 
226
 
 
227
void CategoryEditDialog::reload()
 
228
{
 
229
  fillList();
 
230
}
 
231
 
 
232
void CategoryEditDialog::show()
 
233
{
 
234
  QTreeWidgetItem *first = 0;
 
235
  if ( mWidgets->mCategories->topLevelItemCount() ) {
 
236
    first = mWidgets->mCategories->topLevelItem( 0 );
 
237
    mWidgets->mCategories->setCurrentItem( first );
 
238
  }
 
239
  mWidgets->mCategories->clearSelection();
 
240
  if ( first ) {
 
241
    first->setSelected( true );
 
242
    editItem( first );
 
243
  }
 
244
  KDialog::show();
 
245
}
 
246
 
 
247
void CategoryEditDialog::expandIfToplevel( QTreeWidgetItem *item )
 
248
{
 
249
  if ( !item->parent() ) {
 
250
    item->setExpanded( true );
 
251
  }
 
252
}
 
253
 
 
254
#include "categoryeditdialog.moc"