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

ktimetracker

  • sources
  • kde-4.12
  • kdepim
  • ktimetracker
treeviewheadercontextmenu.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 by Mathias Soeken <msoeken@tzi.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the
16  * Free Software Foundation, Inc.
17  * 51 Franklin Street, Fifth Floor
18  * Boston, MA 02110-1301 USA.
19  *
20  */
21 
22 #include "treeviewheadercontextmenu.h"
23 
24 #include <QAction>
25 #include <QTreeView>
26 #include <QHeaderView>
27 
28 #include <KMenu>
29 
30 #include <KDebug>
31 #include <KLocale>
32 
33 TreeViewHeaderContextMenu::TreeViewHeaderContextMenu( QObject *parent, QTreeView *widget, int style, QVector<int> excludedColumns )
34  : QObject( parent ),
35  mWidget( widget ),
36  mContextMenu( 0 ),
37  mStyle( style ),
38  mExcludedColumns( excludedColumns )
39 {
40  kDebug(5970) << "Entering function";
41  if (mWidget)
42  {
43  mWidget->header()->setContextMenuPolicy( Qt::CustomContextMenu );
44  connect( mWidget->header(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotCustomContextMenuRequested(QPoint)) );
45 
46  mContextMenu = new KMenu( mWidget );
47  mContextMenu->addTitle( i18n("Columns") );
48  connect( mContextMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotTriggered(QAction*)) );
49  connect( mContextMenu, SIGNAL(aboutToShow()), this, SLOT(slotAboutToShow()) );
50  updateActions();
51  }
52  kDebug(5970) << "Leaving function";
53 }
54 
55 TreeViewHeaderContextMenu::~TreeViewHeaderContextMenu()
56 {
57  kDebug(5970) << "Entering function";
58  qDeleteAll( mActions );
59 }
60 
61 void TreeViewHeaderContextMenu::slotCustomContextMenuRequested( const QPoint& pos )
62 {
63  kDebug(5970) << "Entering function";
64  if (mWidget && mContextMenu)
65  {
66  mContextMenu->exec( mWidget->mapToGlobal(pos) );
67  }
68 }
69 
70 void TreeViewHeaderContextMenu::updateActions()
71 {
72  kDebug(5970) << "Entering function";
73  if (mWidget)
74  {
75  QAction *action;
76  foreach (action, mActions)
77  {
78  mContextMenu->removeAction( action );
79  }
80 
81  mActionColumnMapping.clear();
82  qDeleteAll( mActions );
83  mActions.clear();
84 
85  for (int c = 0; c < mWidget->model()->columnCount(); ++c)
86  {
87  if (mExcludedColumns.contains( c )) continue;
88 
89  QAction* action = new QAction( this );
90  updateAction( action, c );
91  mActions.append( action );
92 
93  mContextMenu->addAction( action );
94  mActionColumnMapping[action] = c;
95  }
96  }
97 }
98 
99 void TreeViewHeaderContextMenu::slotTriggered( QAction *action )
100 {
101  kDebug(5970) << "Entering function";
102  if (mWidget && action)
103  {
104  int column = mActionColumnMapping[action];
105  bool hidden = mWidget->isColumnHidden(column);
106  mWidget->setColumnHidden( column, !hidden );
107  updateAction( action, column );
108  emit columnToggled( column );
109  }
110 }
111 
112 void TreeViewHeaderContextMenu::slotAboutToShow()
113 {
114  kDebug(5970) << "Entering function";
115  QAction *action;
116  foreach (action, mActions)
117  {
118  updateAction( action, mActionColumnMapping[action] );
119  }
120 }
121 
122 void TreeViewHeaderContextMenu::updateAction( QAction *action, int column )
123 {
124  kDebug(5970) << "Entering function";
125  QString text = mWidget->model()->headerData(column, Qt::Horizontal).toString();
126  switch (mStyle)
127  {
128  case AlwaysCheckBox:
129  action->setCheckable( true );
130  action->setChecked( !mWidget->isColumnHidden(column) );
131  action->setText( text );
132  break;
133  case CheckBoxOnChecked:
134  action->setCheckable( !mWidget->isColumnHidden(column) );
135  action->setChecked( !mWidget->isColumnHidden(column) );
136  action->setText( text );
137  break;
138  case ShowHideText:
139  action->setCheckable( false );
140  action->setChecked( false );
141  action->setText( (mWidget->isColumnHidden(column) ? i18n("Show") : i18n("Hide")) + ' ' + text );
142  break;
143  }
144 }
145 
146 #include "treeviewheadercontextmenu.moc"
TreeViewHeaderContextMenu::mExcludedColumns
QVector< int > mExcludedColumns
Definition: treeviewheadercontextmenu.h:76
TreeViewHeaderContextMenu::slotAboutToShow
void slotAboutToShow()
Definition: treeviewheadercontextmenu.cpp:112
TreeViewHeaderContextMenu::updateActions
void updateActions()
Definition: treeviewheadercontextmenu.cpp:70
TreeViewHeaderContextMenu::columnToggled
void columnToggled(int)
TreeViewHeaderContextMenu::CheckBoxOnChecked
Definition: treeviewheadercontextmenu.h:53
TreeViewHeaderContextMenu::mWidget
QTreeView * mWidget
Definition: treeviewheadercontextmenu.h:71
TreeViewHeaderContextMenu::TreeViewHeaderContextMenu
TreeViewHeaderContextMenu(QObject *parent, QTreeView *widget, int style=AlwaysCheckBox, QVector< int > excludedColumns=QVector< int >())
Definition: treeviewheadercontextmenu.cpp:33
TreeViewHeaderContextMenu::mContextMenu
KMenu * mContextMenu
Definition: treeviewheadercontextmenu.h:73
TreeViewHeaderContextMenu::mActions
QVector< QAction * > mActions
Definition: treeviewheadercontextmenu.h:72
QObject
TreeViewHeaderContextMenu::ShowHideText
Definition: treeviewheadercontextmenu.h:53
TreeViewHeaderContextMenu::mStyle
int mStyle
Definition: treeviewheadercontextmenu.h:74
TreeViewHeaderContextMenu::AlwaysCheckBox
Definition: treeviewheadercontextmenu.h:53
TreeViewHeaderContextMenu::~TreeViewHeaderContextMenu
~TreeViewHeaderContextMenu()
Definition: treeviewheadercontextmenu.cpp:55
TreeViewHeaderContextMenu::mActionColumnMapping
QHash< QAction *, int > mActionColumnMapping
Definition: treeviewheadercontextmenu.h:75
TreeViewHeaderContextMenu::slotTriggered
void slotTriggered(QAction *)
Definition: treeviewheadercontextmenu.cpp:99
treeviewheadercontextmenu.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktimetracker

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

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