customizableheaderview.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
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
00077
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);
00089
00090
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
00099 if (! (activated_action == 0)) {
00100 isOkay = 1;
00101
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 }