customizableheaderview.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008-2010  Lukas Sommer < SommerLuk at gmail dot com >
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public License as
00006     published by the Free Software Foundation; either version 2 of
00007     the License or (at your option) version 3 or any later version
00008     accepted by the membership of KDE e.V. (or its successor approved
00009     by the membership of KDE e.V.), which shall act as a proxy
00010     defined in Section 14 of version 3 of the license.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "customizableheaderview.h"
00022 #include <QMenu>
00023 #include <QPointer>
00024 #include <QContextMenuEvent>
00025 #include <QAction>
00026 
00027 CustomizableHeaderView::CustomizableHeaderView(Qt::Orientation orientation, QWidget * parent)
00028   : QHeaderView(orientation, parent)
00029 {
00030 }
00031 
00032 CustomizableHeaderView::~CustomizableHeaderView()
00033 {
00034 }
00035 
00036 #ifdef KDE4_SUPPORT
00037 
00038 #include <KMenu>
00039 
00040 QString CustomizableHeaderView::contextMenuTitle() const
00041 
00042 {
00043   return m_contextMenuTitle;
00044 }
00045 
00046 void CustomizableHeaderView::setContextMenuTitle(const QString & title)
00047 {
00048   m_contextMenuTitle = title;      // update property storage
00049 }
00050 
00051 void CustomizableHeaderView::resetContextMenuTitle()
00052 {
00053   setContextMenuTitle("");
00054 }
00055 
00056 #endif
00057 
00058 void CustomizableHeaderView::contextMenuEvent(QContextMenuEvent * e)
00059 {
00060   int sectionCount;
00061   int i;
00062   QPointer<QAction> temp_action;
00063   QPointer<QAction> activated_action;
00064   bool isOkay;
00065   int index;
00066 
00067 #ifdef KDE4_SUPPORT
00068   KMenu m_contextMenu(this);
00069   if (contextMenuTitle().isEmpty()) {
00070     m_contextMenu.addTitle(contextMenuTitle());
00071   };
00072 #else
00073   QMenu m_contextMenu(this);
00074 #endif
00075 
00076   // The menu is contructed each time again, because the numer of columns/rows in the model
00077   // could have changed since the last call.
00078 
00079   if (orientation() == Qt::Horizontal) {
00080     sectionCount = model()->columnCount();
00081   } else {
00082     sectionCount = model()->rowCount();
00083   };
00084   for (i=0; i < sectionCount; ++i) {
00085     temp_action = new QAction(model()->headerData(i,
00086                                                     orientation(),
00087                                                     Qt::DisplayRole).toString(),
00088                                &m_contextMenu);  // because m_contextMenu is parent of the
00089                                                   // actions, all of them will be deleted
00090                                                   // automatically when this function stops.
00091     temp_action->setCheckable(true);
00092     temp_action->setChecked(! isSectionHidden(i));
00093     temp_action->setData(i);
00094     m_contextMenu.addAction(temp_action);
00095   };
00096   activated_action = 0;
00097   activated_action = m_contextMenu.exec(e->globalPos());
00098   // process QAction after closing menu
00099   if (! (activated_action == 0)) {
00100       isOkay = 1;  // following the doc for QVariant, the return type of QAction::data(),
00101       // isOkay must be non-null for that an value is set.
00102       index = activated_action->data().toInt(&isOkay);
00103       if (isOkay) {
00104         if (isSectionHidden(index)) {
00105           emit sectionAboutToBeShown(index, orientation());
00106           setSectionHidden(index, false);
00107           emit sectionShown(index, orientation());
00108         } else {
00109           emit sectionAboutToBeHidden(index, orientation());
00110           setSectionHidden(index, true);
00111           emit sectionHidden(index, orientation());
00112         };
00113       };
00114   };
00115 }

doxygen