Merge branch 'master' of git://anongit.kde.org/kdepim
[kdepim.git] / kalarm / eventlistview.cpp
blob637d210b05a1b97bee5115b171e1325186a81e4c
1 /*
2 * eventlistview.cpp - base class for widget showing list of alarms
3 * Program: kalarm
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.
21 #include "kalarm.h"
22 #include "eventlistview.h"
24 #include "find.h"
26 #include <kglobalsettings.h>
27 #include <KLocalizedString>
29 #include <QMouseEvent>
30 #include <QToolTip>
31 #include <QApplication>
32 #include "kalarm_debug.h"
35 EventListView::EventListView(QWidget* parent)
36 : QTreeView(parent),
37 mFind(Q_NULLPTR),
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);
74 if (scrollToIndex)
75 scrollTo(index);
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)
91 return QModelIndex();
92 return list[0];
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)
103 return KAEvent();
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();
117 if (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]);
123 return elist;
126 /******************************************************************************
127 * Called when the Find action is selected.
128 * Display the non-modal Find dialog.
130 void EventListView::slotFind()
132 if (!mFind)
134 mFind = new Find(this);
135 connect(mFind, &Find::active, this, &EventListView::findActive);
137 mFind->display();
140 /******************************************************************************
141 * Called when the Find Next or Find Prev action is selected.
143 void EventListView::findNext(bool forward)
145 if (mFind)
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'));
163 if (i < 0)
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);
181 return true;
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.
201 switch (e->type())
203 case QEvent::MouseButtonPress:
204 case QEvent::MouseMove:
205 return false;
206 case QEvent::MouseButtonDblClick:
207 break;
208 case QEvent::MouseButtonRelease:
210 if (!static_cast<EventListView*>(parent())->editOnSingleClick()
211 || !KGlobalSettings::singleClick())
212 return false;
213 QMouseEvent* me = static_cast<QMouseEvent*>(e);
214 if (me->button() != Qt::LeftButton || me->buttons()
215 || me->modifiers() != Qt::NoModifier)
216 return false;
217 break;
219 default:
220 break;
222 if (index.isValid())
224 qCDebug(KALARM_LOG);
225 ItemListModel* itemModel = qobject_cast<ItemListModel*>(model);
226 if (!itemModel)
227 qCCritical(KALARM_LOG) << "Invalid cast to ItemListModel*";
228 else
230 KAEvent event = itemModel->event(index);
231 edit(&event, static_cast<EventListView*>(parent()));
232 return true;
235 return false; // indicate that the event has not been handled
238 // vim: et sw=4: