153199a8ec58b4e2c27e68f61b130cf0e74596e7
[qtrapids] / src / client / ColumnSelectorDialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QGridLayout>
22 #include <QTreeWidgetItem>
23 #include <QCheckBox>
24 #include <QDialogButtonBox>
25 #include <QAbstractButton>
26
27 #include "ColumnSelectorDialog.h"
28
29
30 ColumnSelectorDialog::ColumnSelectorDialog(QTreeWidget *treewidget, QWidget* parent, Qt::WindowFlags f) :
31         QDialog(parent, f), // Superclass construct
32         dialogButtons_(0),
33         grid_(0),
34         treeWidget_(treewidget),
35         checkBoxes_()
36 {
37         QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
38         grid_ = new QGridLayout;
39         QCheckBox *cbox = NULL;
40
41         setLayout(verticalBox);
42         verticalBox->addLayout(grid_);
43
44         if (treeWidget_ != NULL) {
45                 QTreeWidgetItem *item = treeWidget_->headerItem();
46
47                 for (unsigned i = 0; i < item->columnCount(); ++i) {
48                         cbox = new QCheckBox(item->text(i));
49                         grid_->addWidget(cbox, i, 0);
50                         treeWidget_->isColumnHidden(i) ? cbox->setCheckState(Qt::Unchecked) : cbox->setCheckState(Qt::Checked);
51                         checkBoxes_.push_back(cbox);
52                         cbox = NULL;
53                 }
54         }
55         
56         dialogButtons_ = new QDialogButtonBox(this);
57         dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
58                                            | QDialogButtonBox::Cancel); 
59
60         verticalBox->addWidget(dialogButtons_);
61         
62         connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
63                                         this, SLOT(on_buttonClicked(QAbstractButton*)));
64 }
65
66
67 ColumnSelectorDialog::~ColumnSelectorDialog()
68 {
69 }
70
71
72 void ColumnSelectorDialog::on_buttonClicked(QAbstractButton *button)
73 {
74         switch (dialogButtons_->buttonRole ( button ) ) {
75         case QDialogButtonBox::AcceptRole :
76                 ApplySettings();
77                 done(QDialog::Accepted);
78                 break;
79         case QDialogButtonBox::ApplyRole :
80                 ApplySettings();
81                 done(QDialog::Accepted);
82                 break;
83         case QDialogButtonBox::RejectRole :
84                 done(QDialog::Rejected);
85                 break;
86         default:
87                 return;
88         }
89 }
90
91
92 void ColumnSelectorDialog::ApplySettings()
93 {
94         if (treeWidget_ != NULL) {
95                 QTreeWidgetItem *item = treeWidget_->headerItem();
96                 QCheckBox *cbox = NULL;
97                 
98                 for (unsigned i = 0; i < checkBoxes_.size(); ++i) {
99                         cbox = checkBoxes_.at(i);
100                         cbox->isChecked() ? treeWidget_->showColumn(i) : treeWidget_->hideColumn(i);
101                         cbox = NULL;
102                 }
103         }
104 }
105