27 #include <qtoolbutton.h>
29 #include <qstandarditemmodel.h>
32 #include <akonadi/monitor.h>
33 #include <akonadi/tagmodel.h>
35 #include <widgets/kcheckcombobox.h>
37 using namespace Akonadi;
43 class CategorySelectWidgetPrivate :
public QObject
51 Akonadi::TagModel *tagModel;
54 KPIM::KCheckComboBox *checkCombo;
58 void selectAll(Qt::CheckState state)
const;
63 void slotSelectNone();
65 void slotTagsInserted(
const QModelIndex &parent,
int start,
int end);
66 void slotTagsRemoved(
const QModelIndex &parent,
int start,
int end);
69 void slotCheckedItemsChanged();
70 void slotCheckedItemsTimer();
88 void CategorySelectWidgetPrivate::init()
95 checkCombo =
new KPIM::KCheckComboBox;
96 checkCombo->setMinimumWidth(150);
97 checkCombo->setSqueezeText(
true);
98 connect(checkCombo, SIGNAL(checkedItemsChanged(
QStringList)),
99 SLOT(slotCheckedItemsChanged()));
102 Monitor *monitor =
new Monitor(
this);
103 monitor->setTypeMonitored(Monitor::Tags);
104 tagModel =
new Akonadi::TagModel(monitor,
this);
106 connect(tagModel, SIGNAL(rowsInserted(
QModelIndex,
int,
int)),
108 connect(tagModel, SIGNAL(rowsRemoved(
QModelIndex,
int,
int)),
113 updateTimer =
new QTimer(
this);
114 updateTimer->setSingleShot(
true);
115 updateTimer->setInterval(200);
116 connect(updateTimer, SIGNAL(timeout()), SLOT(slotCheckedItemsTimer()));
123 but->
setToolTip(i18nc(
"@action:button",
"Reset category filter"));
124 connect(but, SIGNAL(clicked(
bool)), SLOT(slotSelectAll()));
130 but->
setToolTip(i18nc(
"@action:button",
"Clear category filter"));
131 connect(but, SIGNAL(clicked(
bool)), SLOT(slotSelectNone()));
137 itemModel()->appendRow(item);
142 itemModel()->appendRow(item);
144 rowOffset = itemModel()->rowCount();
156 void CategorySelectWidgetPrivate::slotTagsRemoved(
const QModelIndex &parent,
int start,
int end)
158 itemModel()->
removeRows(start+rowOffset, end+rowOffset, parent);
162 void CategorySelectWidgetPrivate::slotTagsChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight)
164 for (
int row = topLeft.
row(); row<=bottomRight.
row(); ++row) {
169 it->
setText(tagModel->data(idx, TagModel::NameRole).toString());
170 it->
setIcon(tagModel->data(idx, Qt::DecorationRole).value<
QIcon>());
176 void CategorySelectWidgetPrivate::slotTagsInserted(
const QModelIndex &parent,
int start,
int end)
178 for (
int row = start; row<=end; ++row) {
181 kDebug() <<
"idx" << idx <<
"=" << tagModel->
data(idx, Qt::DisplayRole).
toString()
182 <<
"name" << tagModel->
data(idx, TagModel::NameRole).toString()
183 <<
"tag" << tagModel->data(idx, TagModel::TagRole)
184 <<
"id" << tagModel->data(idx, TagModel::IdRole).toInt();
187 it->
setIcon(tagModel->data(idx, Qt::DecorationRole).value<
QIcon>());
197 const Tag::Id parentId = tagModel->data(idx, TagModel::IdRole).value<Tag::Id>();
198 QModelIndexList matchList = itemModel()->match(itemModel()->index(0, 0),
FILTER_ROLE,
200 Qt::MatchExactly|Qt::MatchRecursive);
201 if (matchList.count()==1) {
203 itemModel()->itemFromIndex(parentIndex)->appendRow(it);
205 kWarning() <<
"Cannot find parent with ID" << parentId;
206 itemModel()->insertRow(row+rowOffset, it);
209 itemModel()->insertRow(row+rowOffset, it);
215 void CategorySelectWidgetPrivate::selectAll(Qt::CheckState state)
const
217 for (
int row = 0; row<itemModel()->rowCount(); ++row) {
224 void CategorySelectWidgetPrivate::slotSelectAll()
226 selectAll(Qt::Checked);
230 void CategorySelectWidgetPrivate::slotSelectNone()
232 selectAll(Qt::Unchecked);
236 void CategorySelectWidgetPrivate::slotCheckedItemsChanged()
238 updateTimer->start();
242 void CategorySelectWidgetPrivate::slotCheckedItemsTimer()
247 for (
int row = 0; row<itemModel()->rowCount(); ++row) {
249 Qt::CheckState rowState =
static_cast<Qt::CheckState
>(it->
data(Qt::CheckStateRole).
toInt());
250 if (rowState!=Qt::Checked) {
257 checkCombo->setAlwaysShowDefaultText(
true);
258 checkCombo->setDefaultText(i18n(
"(All)"));
260 checkCombo->setAlwaysShowDefaultText(
false);
261 checkCombo->setDefaultText(i18n(
"(None)"));
264 const QStringList checkedList = checkCombo->checkedItems();
266 checkCombo->setToolTip(i18n(
"<qt>Category filter: %1", checkedList.
join(i18n(
", "))));
268 checkCombo->setToolTip(
QString());
271 emit q->filterChanged(filterTags());
279 for (
int row = 0; row<itemModel()->rowCount(); ++row) {
284 if (
id!=0) filter.
append(
id);
302 d_ptr(new CategorySelectWidgetPrivate(this))
318 return d->filterTags();
322 #include "categoryselectwidget.moc"
void setIcon(const QIcon &icon)
virtual bool removeRows(int row, int count, const QModelIndex &parent)
QString join(const QString &separator) const
void addSpacing(int size)
Qt::CheckState checkState() const
virtual void setData(const QVariant &value, int role)
void append(const T &value)
int toInt(bool *ok) const
void setText(const QString &text)
QVariant data(int role) const
QFuture< void > filter(Sequence &sequence, FilterFunction filterFunction)
void setCheckState(Qt::CheckState state)
virtual QVariant data(int role) const
void setSpacing(int spacing)