/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. */ #include #include #include #include #include #include #include #include "smtreeview.h" #include "smglobals.h" SmTreeView::SmTreeView(QWidget *parent) : QTreeView(parent) { header()->setSectionResizeMode(QHeaderView::ResizeToContents); setAlternatingRowColors(true); setPalette(qApp->palette()); } SmTreeView::SmTreeView(const QString &hSetting, QWidget *parent) : QTreeView(parent), mHeaderSetting(hSetting){ header()->setSectionResizeMode(QHeaderView::ResizeToContents); setAlternatingRowColors(true); setPalette(qApp->palette()); } void SmTreeView::readHeaderConfig(){ QSettings s; QByteArray headerPos = s.value(mHeaderSetting).toByteArray(); if(!headerPos.isEmpty()){ header()->restoreState(headerPos); } QHeaderView *hv = header(); QHash headerActions; foreach(QAction *a, mHeaderGroup->actions()){ headerActions.insert(a->data().toInt(), a); } for(int i = 0; i < hv->count(); ++i){ // don't crash if header count is smaller than saved... if(i >= mHeaderGroup->actions().count()){ continue; } if(!hv->isSectionHidden(i)){ headerActions.value(i)->setChecked(true); } } } void SmTreeView::writeHeaderConfig(){ QSettings s; s.setValue(mHeaderSetting, header()->saveState()); } void SmTreeView::toggleHeader(QObject *action){ QAction *a = qobject_cast(action); int logicalIndex = a->data().toInt(); QHeaderView *hv = header(); hv->setSectionHidden(logicalIndex, !a->isChecked()); } void SmTreeView::contextMenuEvent(QContextMenuEvent *e){ if(actions().isEmpty()){ return; } QMenu contextMenu(this); foreach(QAction *a, actions()){ contextMenu.addAction(a); } contextMenu.exec(e->globalPos()); }