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

kalarm

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

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