SVN_SILENT made messages (.desktop file)
[kdepim.git] / incidenceeditor-ng / categorydialog.cpp
blobcc4dd45efa462f73ba1a94a3fefff3d73714f380
1 /*
2 Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org>
3 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
5 Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
6 Author: SĂ©rgio Martins <sergio.martins@kdab.com>
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 "categorydialog.h"
25 #include "categoryhierarchyreader.h"
26 #include "ui_categorydialog_base.h"
28 #include <calendarsupport/kcalprefs.h>
29 #include <calendarsupport/categoryconfig.h>
31 #include <QIcon>
32 #include <KConfigGroup>
33 #include <QDialogButtonBox>
34 #include <QPushButton>
35 #include <QVBoxLayout>
37 using namespace IncidenceEditorNG;
38 using namespace CalendarSupport;
40 class CategoryWidgetBase : public QWidget, public Ui::CategoryDialog_base
42 public:
43 CategoryWidgetBase(QWidget *parent) : QWidget(parent)
45 setupUi(this);
49 CategoryWidget::CategoryWidget(CategoryConfig *cc, QWidget *parent)
50 : QWidget(parent), mCategoryConfig(cc)
52 QHBoxLayout *topL = new QHBoxLayout(this);
53 topL->setMargin(0);
54 mWidgets = new CategoryWidgetBase(this);
55 topL->addWidget(mWidgets);
57 mWidgets->mButtonAdd->setIcon(QIcon::fromTheme("list-add"));
58 mWidgets->mButtonRemove->setIcon(QIcon::fromTheme("list-remove"));
59 mWidgets->mLineEdit->setPlaceholderText(i18n("Click to add a new category"));
61 connect(mWidgets->mLineEdit, SIGNAL(textChanged(QString)),
62 SLOT(handleTextChanged(QString)));
64 mWidgets->mButtonAdd->setEnabled(false);
65 mWidgets->mButtonRemove->setEnabled(false);
66 mWidgets->mColorCombo->setEnabled(false);
68 connect(mWidgets->mCategories, SIGNAL(itemSelectionChanged()),
69 SLOT(handleSelectionChanged()));
71 connect(mWidgets->mButtonAdd, SIGNAL(clicked()),
72 SLOT(addCategory()));
74 connect(mWidgets->mButtonRemove, SIGNAL(clicked()),
75 SLOT(removeCategory()));
77 connect(mWidgets->mColorCombo, SIGNAL(activated(QColor)),
78 SLOT(handleColorChanged(QColor)));
82 CategoryWidget::~CategoryWidget()
86 AutoCheckTreeWidget *CategoryWidget::listView() const
88 return mWidgets->mCategories;
91 void CategoryWidget::hideButton()
95 void CategoryWidget::setCategories(const QStringList &categoryList)
97 mWidgets->mCategories->clear();
98 mCategoryList.clear();
100 QStringList::ConstIterator it;
101 QStringList cats = mCategoryConfig->customCategories();
102 for (it = categoryList.begin(); it != categoryList.end(); ++it) {
103 if (!cats.contains(*it)) {
104 cats.append(*it);
107 mCategoryConfig->setCustomCategories(cats);
108 CategoryHierarchyReaderQTreeWidget(mWidgets->mCategories).read(cats);
111 void CategoryWidget::setSelected(const QStringList &selList)
113 clear();
114 QStringList::ConstIterator it;
116 const bool remAutoCheckChildren = mWidgets->mCategories->autoCheckChildren();
117 mWidgets->mCategories->setAutoCheckChildren(false);
118 for (it = selList.begin(); it != selList.end(); ++it) {
119 QStringList path = CategoryHierarchyReader::path(*it);
120 QTreeWidgetItem *item = mWidgets->mCategories->itemByPath(path);
121 if (item) {
122 item->setCheckState(0, Qt::Checked);
125 mWidgets->mCategories->setAutoCheckChildren(remAutoCheckChildren);
128 static QStringList getSelectedCategories(AutoCheckTreeWidget *categoriesView)
130 QStringList categories;
132 QTreeWidgetItemIterator it(categoriesView, QTreeWidgetItemIterator::Checked);
133 while (*it) {
134 QStringList path = categoriesView->pathByItem(*it++);
135 if (path.count()) {
136 path.replaceInStrings(CategoryConfig::categorySeparator, QString("\\") +
137 CategoryConfig::categorySeparator);
138 categories.append(path.join(CategoryConfig::categorySeparator));
142 return categories;
145 void CategoryWidget::clear()
147 const bool remAutoCheckChildren = mWidgets->mCategories->autoCheckChildren();
148 mWidgets->mCategories->setAutoCheckChildren(false);
150 QTreeWidgetItemIterator it(mWidgets->mCategories);
151 while (*it) {
152 (*it++)->setCheckState(0, Qt::Unchecked);
155 mWidgets->mCategories->setAutoCheckChildren(remAutoCheckChildren);
158 void CategoryWidget::setAutoselectChildren(bool autoselectChildren)
160 mWidgets->mCategories->setAutoCheckChildren(autoselectChildren);
163 void CategoryWidget::hideHeader()
165 mWidgets->mCategories->header()->hide();
168 QStringList CategoryWidget::selectedCategories(QString &categoriesStr)
170 mCategoryList = getSelectedCategories(listView());
171 categoriesStr = mCategoryList.join(", ");
172 return mCategoryList;
175 QStringList CategoryWidget::selectedCategories() const
177 return mCategoryList;
180 void CategoryWidget::setCategoryList(const QStringList &categories)
182 mCategoryList = categories;
185 void CategoryWidget::addCategory()
187 QTreeWidgetItem *newItem = new QTreeWidgetItem(listView(),
188 QStringList(mWidgets->mLineEdit->text()));
189 listView()->scrollToItem(newItem);
190 listView()->clearSelection();
191 newItem->setSelected(true);
194 void CategoryWidget::removeCategory()
196 // Multi-select not supported, only one selected
197 QTreeWidgetItem *itemToDelete = listView()->selectedItems().first();
198 delete itemToDelete;
201 void CategoryWidget::handleTextChanged(const QString &newText)
203 mWidgets->mButtonAdd->setEnabled(!newText.isEmpty());
206 void CategoryWidget::handleSelectionChanged()
208 const bool hasSelection = !listView()->selectedItems().isEmpty();
209 mWidgets->mButtonRemove->setEnabled(hasSelection);
210 mWidgets->mColorCombo->setEnabled(hasSelection);
212 if (hasSelection) {
213 const QTreeWidgetItem *item = listView()->selectedItems().first();
214 const QColor &color = KCalPrefs::instance()->categoryColor(item->text(0));
215 if (color.isValid()) {
216 mWidgets->mColorCombo->setColor(color);
217 // update is needed. bug in KColorCombo?
218 mWidgets->mColorCombo->update();
223 void CategoryWidget::handleColorChanged(const QColor &newColor)
225 if (!listView()->selectedItems().isEmpty()) {
226 const QTreeWidgetItem *item = listView()->selectedItems().first();
227 const QString category = item->text(0);
228 if (newColor.isValid()) {
229 KCalPrefs::instance()->setCategoryColor(category, newColor);
234 CategoryDialog::CategoryDialog(CategoryConfig *cc, QWidget *parent)
235 : QDialog(parent), d(0)
237 setWindowTitle(i18n("Select Categories"));
238 QVBoxLayout *mainLayout = new QVBoxLayout;
239 setLayout(mainLayout);
240 QDialogButtonBox *buttonBox = 0;
242 #ifdef KDEPIM_MOBILE_UI
243 // HACK: This is for maemo, which hides the button if there is only a cancel
244 // button.
245 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
246 #else
247 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Apply);
248 #endif
250 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
251 okButton->setDefault(true);
252 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
253 connect(buttonBox, &QDialogButtonBox::accepted, this, &CategoryDialog::accept);
254 connect(buttonBox, &QDialogButtonBox::rejected, this, &CategoryDialog::reject);
256 QWidget *page = new QWidget;
257 mainLayout->addWidget(page);
258 mainLayout->addWidget(buttonBox);
259 QVBoxLayout *lay = new QVBoxLayout(page);
260 lay->setMargin(0);
262 mWidgets = new CategoryWidget(cc, this);
263 mCategoryConfig = cc;
264 mWidgets->setObjectName("CategorySelection");
265 mWidgets->hideHeader();
266 lay->addWidget(mWidgets);
268 mWidgets->setCategories();
269 mWidgets->listView()->setFocus();
271 connect(okButton, &QPushButton::clicked, this, &CategoryDialog::slotOk);
272 connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &CategoryDialog::slotApply);
275 CategoryDialog::~CategoryDialog()
277 delete mWidgets;
280 QStringList CategoryDialog::selectedCategories() const
282 return mWidgets->selectedCategories();
285 void CategoryDialog::slotApply()
287 QStringList l;
289 QStringList path;
290 QTreeWidgetItemIterator it(mWidgets->listView());
291 while (*it) {
292 path = mWidgets->listView()->pathByItem(*it++);
293 path.replaceInStrings(
294 CategoryConfig::categorySeparator,
295 QString("\\") + CategoryConfig::categorySeparator);
296 l.append(path.join(CategoryConfig::categorySeparator));
298 mCategoryConfig->setCustomCategories(l);
299 mCategoryConfig->writeConfig();
301 QString categoriesStr;
302 QStringList categories = mWidgets->selectedCategories(categoriesStr);
303 emit categoriesSelected(categories);
304 emit categoriesSelected(categoriesStr);
307 void CategoryDialog::slotOk()
309 slotApply();
310 accept();
313 void CategoryDialog::updateCategoryConfig()
315 QString tmp;
316 QStringList selected = mWidgets->selectedCategories(tmp);
317 mWidgets->setCategories();
318 mWidgets->setSelected(selected);
321 void CategoryDialog::setAutoselectChildren(bool autoselectChildren)
323 mWidgets->setAutoselectChildren(autoselectChildren);
326 void CategoryDialog::setCategoryList(const QStringList &categories)
328 mWidgets->setCategoryList(categories);
331 void CategoryDialog::setSelected(const QStringList &selList)
333 mWidgets->setSelected(selList);