• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.12
  • kdepim
  • kalarm
eventlistview.cpp
Go to the documentation of this file.
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>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "kalarm.h"
22 #include "eventlistview.moc"
23 
24 #include "find.h"
25 #ifndef USE_AKONADI
26 #include "eventlistmodel.h"
27 #include "templatelistfiltermodel.h"
28 #endif
29 
30 #include <kglobalsettings.h>
31 #include <klocale.h>
32 #include <kdebug.h>
33 
34 #include <QHeaderView>
35 #include <QMouseEvent>
36 #include <QToolTip>
37 #include <QApplication>
38 
39 
40 EventListView::EventListView(QWidget* parent)
41  : QTreeView(parent),
42  mFind(0),
43  mEditOnSingleClick(false)
44 {
45  setRootIsDecorated(false); // don't show expander icons for child-less items
46  setSortingEnabled(true);
47  setAllColumnsShowFocus(true);
48  setSelectionMode(ExtendedSelection);
49  setSelectionBehavior(SelectRows);
50  setTextElideMode(Qt::ElideRight);
51  // Set default WhatsThis text to be displayed when no actual item is clicked on
52  setWhatsThis(i18nc("@info:whatsthis", "List of scheduled alarms"));
53 }
54 
55 /******************************************************************************
56 * Return the event referred to by an index.
57 */
58 #ifdef USE_AKONADI
59 KAEvent EventListView::event(const QModelIndex& index) const
60 {
61  return itemModel()->event(index);
62 }
63 
64 KAEvent EventListView::event(int row) const
65 {
66  return itemModel()->event(itemModel()->index(row, 0));
67 }
68 #else
69 KAEvent* EventListView::event(const QModelIndex& index) const
70 {
71  return eventFilterModel()->event(index);
72 }
73 
74 KAEvent* EventListView::event(int row) const
75 {
76  return eventFilterModel()->event(row);
77 }
78 #endif
79 
80 /******************************************************************************
81 * Select one event and make it the current item.
82 */
83 #ifdef USE_AKONADI
84 void EventListView::select(Akonadi::Item::Id eventId)
85 {
86  select(itemModel()->eventIndex(eventId));
87 }
88 #else
89 void EventListView::select(const QString& eventId, bool scrollToEvent)
90 {
91  select(eventFilterModel()->eventIndex(eventId), scrollToEvent);
92 }
93 #endif
94 
95 void EventListView::select(const QModelIndex& index, bool scrollToIndex)
96 {
97  selectionModel()->select(index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
98  if (scrollToIndex)
99  scrollTo(index);
100 }
101 
102 void EventListView::clearSelection()
103 {
104  selectionModel()->clearSelection();
105 }
106 
107 /******************************************************************************
108 * Return the single selected item.
109 * Reply = invalid if no items are selected, or if multiple items are selected.
110 */
111 QModelIndex EventListView::selectedIndex() const
112 {
113  QModelIndexList list = selectionModel()->selectedRows();
114  if (list.count() != 1)
115  return QModelIndex();
116  return list[0];
117 }
118 
119 /******************************************************************************
120 * Return the single selected event.
121 * Reply = null if no items are selected, or if multiple items are selected.
122 */
123 #ifdef USE_AKONADI
124 KAEvent EventListView::selectedEvent() const
125 {
126  QModelIndexList list = selectionModel()->selectedRows();
127  if (list.count() != 1)
128  return KAEvent();
129 kDebug(0)<<"SelectedEvent() count="<<list.count();
130  const ItemListModel* model = static_cast<const ItemListModel*>(list[0].model());
131  return model->event(list[0]);
132 }
133 #else
134 KAEvent* EventListView::selectedEvent() const
135 {
136  QModelIndexList list = selectionModel()->selectedRows();
137  if (list.count() != 1)
138  return 0;
139 kDebug(0)<<"SelectedEvent() count="<<list.count();
140  const QAbstractProxyModel* proxy = static_cast<const QAbstractProxyModel*>(list[0].model());
141  QModelIndex source = proxy->mapToSource(list[0]);
142  return static_cast<KAEvent*>(source.internalPointer());
143 }
144 #endif
145 
146 /******************************************************************************
147 * Return the selected events.
148 */
149 #ifdef USE_AKONADI
150 QVector<KAEvent> EventListView::selectedEvents() const
151 #else
152 KAEvent::List EventListView::selectedEvents() const
153 #endif
154 {
155 #ifdef USE_AKONADI
156  QVector<KAEvent> elist;
157 #else
158  KAEvent::List elist;
159 #endif
160  QModelIndexList ixlist = selectionModel()->selectedRows();
161  int count = ixlist.count();
162  if (count)
163  {
164 #ifdef USE_AKONADI
165  const ItemListModel* model = static_cast<const ItemListModel*>(ixlist[0].model());
166  for (int i = 0; i < count; ++i)
167  elist += model->event(ixlist[i]);
168 #else
169  const QAbstractProxyModel* proxy = static_cast<const QAbstractProxyModel*>(ixlist[0].model());
170  for (int i = 0; i < count; ++i)
171  {
172  QModelIndex source = proxy->mapToSource(ixlist[i]);
173  elist += static_cast<KAEvent*>(source.internalPointer());
174  }
175 #endif
176  }
177  return elist;
178 }
179 
180 /******************************************************************************
181 * Called when the Find action is selected.
182 * Display the non-modal Find dialog.
183 */
184 void EventListView::slotFind()
185 {
186  if (!mFind)
187  {
188  mFind = new Find(this);
189  connect(mFind, SIGNAL(active(bool)), SIGNAL(findActive(bool)));
190  }
191  mFind->display();
192 }
193 
194 /******************************************************************************
195 * Called when the Find Next or Find Prev action is selected.
196 */
197 void EventListView::findNext(bool forward)
198 {
199  if (mFind)
200  mFind->findNext(forward);
201 }
202 
203 /******************************************************************************
204 * Called when a ToolTip or WhatsThis event occurs.
205 */
206 bool EventListView::viewportEvent(QEvent* e)
207 {
208  if (e->type() == QEvent::ToolTip && isActiveWindow())
209  {
210  QHelpEvent* he = static_cast<QHelpEvent*>(e);
211  QModelIndex index = indexAt(he->pos());
212  QVariant value = model()->data(index, Qt::ToolTipRole);
213  if (qVariantCanConvert<QString>(value))
214  {
215  QString toolTip = value.toString();
216  int i = toolTip.indexOf(QLatin1Char('\n'));
217  if (i < 0)
218  {
219 #ifdef USE_AKONADI
220  ItemListModel* m = qobject_cast<ItemListModel*>(model());
221  if (!m || m->event(index).commandError() == KAEvent::CMD_NO_ERROR)
222 #else
223  EventListFilterModel* m = qobject_cast<EventListFilterModel*>(model());
224  if (!m || m->event(index)->commandError() == KAEvent::CMD_NO_ERROR)
225 #endif
226  {
227  // Single line tooltip. Only display it if the text column
228  // is truncated in the view display.
229  value = model()->data(index, Qt::FontRole);
230  QFontMetrics fm(qvariant_cast<QFont>(value).resolve(viewOptions().font));
231  int textWidth = fm.boundingRect(toolTip).width() + 1;
232  const int margin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
233  int left = columnViewportPosition(index.column()) + margin;
234  int right = left + textWidth;
235  if (left >= 0 && right <= width() - 2*frameWidth())
236  toolTip.clear(); // prevent any tooltip showing
237  }
238  }
239  QToolTip::showText(he->globalPos(), toolTip, this);
240  return true;
241  }
242  }
243  return QTreeView::viewportEvent(e);
244 }
245 
246 /******************************************************************************
247 * Called when a context menu event is requested by mouse or key.
248 */
249 void EventListView::contextMenuEvent(QContextMenuEvent* e)
250 {
251  emit contextMenuRequested(e->globalPos());
252 }
253 
254 
255 bool EventListDelegate::editorEvent(QEvent* e, QAbstractItemModel* model, const QStyleOptionViewItem&, const QModelIndex& index)
256 {
257  // Don't invoke the editor unless it's either a double click or,
258  // if KDE is in single click mode and it's a left button release
259  // with no other buttons pressed and no keyboard modifiers.
260  switch (e->type())
261  {
262  case QEvent::MouseButtonPress:
263  case QEvent::MouseMove:
264  return false;
265  case QEvent::MouseButtonDblClick:
266  break;
267  case QEvent::MouseButtonRelease:
268  {
269  if (!static_cast<EventListView*>(parent())->editOnSingleClick()
270  || !KGlobalSettings::singleClick())
271  return false;
272  QMouseEvent* me = static_cast<QMouseEvent*>(e);
273  if (me->button() != Qt::LeftButton || me->buttons()
274  || me->modifiers() != Qt::NoModifier)
275  return false;
276  break;
277  }
278  default:
279  break;
280  }
281  if (index.isValid())
282  {
283  kDebug();
284 #ifdef USE_AKONADI
285  ItemListModel* itemModel = qobject_cast<ItemListModel*>(model);
286  if (!itemModel)
287  kError() << "Invalid cast to ItemListModel*";
288  else
289  {
290  KAEvent event = itemModel->event(index);
291  edit(&event, static_cast<EventListView*>(parent()));
292  return true;
293  }
294 #else
295  QModelIndex source = static_cast<QAbstractProxyModel*>(model)->mapToSource(index);
296  KAEvent* event = static_cast<KAEvent*>(source.internalPointer());
297  edit(event, static_cast<EventListView*>(parent()));
298  return true;
299 #endif
300  }
301  return false; // indicate that the event has not been handled
302 }
303 
304 // vim: et sw=4:
EventListView::slotFind
virtual void slotFind()
Definition: eventlistview.cpp:184
EventListView::selectedEvent
KAEvent * selectedEvent() const
Definition: eventlistview.cpp:134
EventListDelegate::editorEvent
virtual bool editorEvent(QEvent *, QAbstractItemModel *, const QStyleOptionViewItem &, const QModelIndex &)
Definition: eventlistview.cpp:255
templatelistfiltermodel.h
EventListView::EventListView
EventListView(QWidget *parent=0)
Definition: eventlistview.cpp:40
EventListFilterModel
Definition: eventlistmodel.h:129
QWidget
ItemListModel::event
KAEvent event(int row) const
Definition: itemlistmodel.cpp:163
EventListView::findActive
void findActive(bool)
EventListView::selectedEvents
KAEvent::List selectedEvents() const
Definition: eventlistview.cpp:152
EventListView::contextMenuRequested
void contextMenuRequested(const QPoint &globalPos)
EventListView::event
KAEvent * event(int row) const
Definition: eventlistview.cpp:74
Find
Definition: find.h:36
find.h
QTreeView
eventlistmodel.h
EventListView::select
void select(const QString &eventId, bool scrollToEvent=false)
Definition: eventlistview.cpp:89
EventListDelegate::edit
virtual void edit(KAEvent *, EventListView *)=0
EventListView::selectedIndex
QModelIndex selectedIndex() const
Definition: eventlistview.cpp:111
kalarm.h
EventListView::clearSelection
void clearSelection()
Definition: eventlistview.cpp:102
EventListView::eventFilterModel
EventListFilterModel * eventFilterModel() const
Definition: eventlistview.h:48
Find::display
void display()
Definition: find.cpp:105
EventListView::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *)
Definition: eventlistview.cpp:249
EventListFilterModel::event
KAEvent * event(int row) const
Definition: eventlistmodel.cpp:944
EventListView::viewportEvent
virtual bool viewportEvent(QEvent *)
Definition: eventlistview.cpp:206
Find::findNext
void findNext(bool forward)
Definition: find.h:43
ItemListModel
Definition: itemlistmodel.h:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal