New Appointment -> New Task
[kdepim.git] / ktimetracker / treeviewheadercontextmenu.cpp
blobca501e3f39eb5d49547fa628691d0fbe7d2eee8d
1 /*
2 * Copyright (C) 2007 by Mathias Soeken <msoeken@tzi.de>
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.
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.
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.
22 #include "treeviewheadercontextmenu.h"
24 #include <QAction>
25 #include <QTreeView>
26 #include <QHeaderView>
28 #include <KMenu>
30 #include <KDebug>
31 #include <KLocale>
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 )
40 kDebug(5970) << "Entering function";
41 if (mWidget)
43 mWidget->header()->setContextMenuPolicy( Qt::CustomContextMenu );
44 connect( mWidget->header(), SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(slotCustomContextMenuRequested(const QPoint&)) );
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();
52 kDebug(5970) << "Leaving function";
55 TreeViewHeaderContextMenu::~TreeViewHeaderContextMenu()
57 kDebug(5970) << "Entering function";
58 qDeleteAll( mActions );
61 void TreeViewHeaderContextMenu::slotCustomContextMenuRequested( const QPoint& pos )
63 kDebug(5970) << "Entering function";
64 if (mWidget && mContextMenu)
66 mContextMenu->exec( mWidget->mapToGlobal(pos) );
70 void TreeViewHeaderContextMenu::updateActions()
72 kDebug(5970) << "Entering function";
73 if (mWidget)
75 QAction *action;
76 foreach (action, mActions)
78 mContextMenu->removeAction( action );
81 mActionColumnMapping.clear();
82 qDeleteAll( mActions );
83 mActions.clear();
85 for (int c = 0; c < mWidget->model()->columnCount(); ++c)
87 if (mExcludedColumns.contains( c )) continue;
89 QAction* action = new QAction( this );
90 updateAction( action, c );
91 mActions.append( action );
93 mContextMenu->addAction( action );
94 mActionColumnMapping[action] = c;
99 void TreeViewHeaderContextMenu::slotTriggered( QAction *action )
101 kDebug(5970) << "Entering function";
102 if (mWidget && action)
104 int column = mActionColumnMapping[action];
105 bool hidden = mWidget->isColumnHidden(column);
106 mWidget->setColumnHidden( column, !hidden );
107 updateAction( action, column );
108 emit columnToggled( column );
112 void TreeViewHeaderContextMenu::slotAboutToShow()
114 kDebug(5970) << "Entering function";
115 QAction *action;
116 foreach (action, mActions)
118 updateAction( action, mActionColumnMapping[action] );
122 void TreeViewHeaderContextMenu::updateAction( QAction *action, int column )
124 kDebug(5970) << "Entering function";
125 QString text = mWidget->model()->headerData(column, Qt::Horizontal).toString();
126 switch (mStyle)
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;
146 #include "treeviewheadercontextmenu.moc"