2 * eventlistview.cpp - base class for widget showing list of alarms
4 * Copyright © 2007-2013 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "eventlistview.h"
26 #include <kglobalsettings.h>
27 #include <KLocalizedString>
29 #include <QMouseEvent>
31 #include <QApplication>
32 #include "kalarm_debug.h"
35 EventListView::EventListView(QWidget
* parent
)
38 mEditOnSingleClick(false)
40 setRootIsDecorated(false); // don't show expander icons for child-less items
41 setSortingEnabled(true);
42 setAllColumnsShowFocus(true);
43 setSelectionMode(ExtendedSelection
);
44 setSelectionBehavior(SelectRows
);
45 setTextElideMode(Qt::ElideRight
);
46 // Set default WhatsThis text to be displayed when no actual item is clicked on
47 setWhatsThis(i18nc("@info:whatsthis", "List of scheduled alarms"));
50 /******************************************************************************
51 * Return the event referred to by an index.
53 KAEvent
EventListView::event(const QModelIndex
& index
) const
55 return itemModel()->event(index
);
58 KAEvent
EventListView::event(int row
) const
60 return itemModel()->event(itemModel()->index(row
, 0));
63 /******************************************************************************
64 * Select one event and make it the current item.
66 void EventListView::select(Akonadi::Item::Id eventId
)
68 select(itemModel()->eventIndex(eventId
));
71 void EventListView::select(const QModelIndex
& index
, bool scrollToIndex
)
73 selectionModel()->select(index
, QItemSelectionModel::SelectCurrent
| QItemSelectionModel::Rows
);
78 void EventListView::clearSelection()
80 selectionModel()->clearSelection();
83 /******************************************************************************
84 * Return the single selected item.
85 * Reply = invalid if no items are selected, or if multiple items are selected.
87 QModelIndex
EventListView::selectedIndex() const
89 QModelIndexList list
= selectionModel()->selectedRows();
90 if (list
.count() != 1)
95 /******************************************************************************
96 * Return the single selected event.
97 * Reply = null if no items are selected, or if multiple items are selected.
99 KAEvent
EventListView::selectedEvent() const
101 QModelIndexList list
= selectionModel()->selectedRows();
102 if (list
.count() != 1)
104 qCDebug(KALARM_LOG
)<<"SelectedEvent() count="<<list
.count();
105 const ItemListModel
* model
= static_cast<const ItemListModel
*>(list
[0].model());
106 return model
->event(list
[0]);
109 /******************************************************************************
110 * Return the selected events.
112 QVector
<KAEvent
> EventListView::selectedEvents() const
114 QVector
<KAEvent
> elist
;
115 QModelIndexList ixlist
= selectionModel()->selectedRows();
116 int count
= ixlist
.count();
119 const ItemListModel
* model
= static_cast<const ItemListModel
*>(ixlist
[0].model());
120 for (int i
= 0; i
< count
; ++i
)
121 elist
+= model
->event(ixlist
[i
]);
126 /******************************************************************************
127 * Called when the Find action is selected.
128 * Display the non-modal Find dialog.
130 void EventListView::slotFind()
134 mFind
= new Find(this);
135 connect(mFind
, &Find::active
, this, &EventListView::findActive
);
140 /******************************************************************************
141 * Called when the Find Next or Find Prev action is selected.
143 void EventListView::findNext(bool forward
)
146 mFind
->findNext(forward
);
149 /******************************************************************************
150 * Called when a ToolTip or WhatsThis event occurs.
152 bool EventListView::viewportEvent(QEvent
* e
)
154 if (e
->type() == QEvent::ToolTip
&& isActiveWindow())
156 QHelpEvent
* he
= static_cast<QHelpEvent
*>(e
);
157 QModelIndex index
= indexAt(he
->pos());
158 QVariant value
= model()->data(index
, Qt::ToolTipRole
);
159 if (qVariantCanConvert
<QString
>(value
))
161 QString toolTip
= value
.toString();
162 int i
= toolTip
.indexOf(QLatin1Char('\n'));
165 ItemListModel
* m
= qobject_cast
<ItemListModel
*>(model());
166 if (!m
|| m
->event(index
).commandError() == KAEvent::CMD_NO_ERROR
)
168 // Single line tooltip. Only display it if the text column
169 // is truncated in the view display.
170 value
= model()->data(index
, Qt::FontRole
);
171 QFontMetrics
fm(qvariant_cast
<QFont
>(value
).resolve(viewOptions().font
));
172 int textWidth
= fm
.boundingRect(toolTip
).width() + 1;
173 const int margin
= QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin
) + 1;
174 int left
= columnViewportPosition(index
.column()) + margin
;
175 int right
= left
+ textWidth
;
176 if (left
>= 0 && right
<= width() - 2*frameWidth())
177 toolTip
.clear(); // prevent any tooltip showing
180 QToolTip::showText(he
->globalPos(), toolTip
, this);
184 return QTreeView::viewportEvent(e
);
187 /******************************************************************************
188 * Called when a context menu event is requested by mouse or key.
190 void EventListView::contextMenuEvent(QContextMenuEvent
* e
)
192 Q_EMIT
contextMenuRequested(e
->globalPos());
196 bool EventListDelegate::editorEvent(QEvent
* e
, QAbstractItemModel
* model
, const QStyleOptionViewItem
&, const QModelIndex
& index
)
198 // Don't invoke the editor unless it's either a double click or,
199 // if KDE is in single click mode and it's a left button release
200 // with no other buttons pressed and no keyboard modifiers.
203 case QEvent::MouseButtonPress
:
204 case QEvent::MouseMove
:
206 case QEvent::MouseButtonDblClick
:
208 case QEvent::MouseButtonRelease
:
210 if (!static_cast<EventListView
*>(parent())->editOnSingleClick()
211 || !KGlobalSettings::singleClick())
213 QMouseEvent
* me
= static_cast<QMouseEvent
*>(e
);
214 if (me
->button() != Qt::LeftButton
|| me
->buttons()
215 || me
->modifiers() != Qt::NoModifier
)
225 ItemListModel
* itemModel
= qobject_cast
<ItemListModel
*>(model
);
227 qCCritical(KALARM_LOG
) << "Invalid cast to ItemListModel*";
230 KAEvent event
= itemModel
->event(index
);
231 edit(&event
, static_cast<EventListView
*>(parent()));
235 return false; // indicate that the event has not been handled