- Scrollbar added to column selector
[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 <QScrollArea>
22 #include <QGroupBox>
23 #include <QGridLayout>
24 #include <QTreeWidgetItem>
25 #include <QCheckBox>
26 #include <QDialogButtonBox>
27 #include <QAbstractButton>
28
29 #include "ColumnSelectorDialog.h"
30
31
32 ColumnSelectorDialog::ColumnSelectorDialog(QTreeWidget *treewidget, QWidget* parent, Qt::WindowFlags f) :
33         QDialog(parent, f), // Superclass construct
34         dialogButtons_(0),
35         grid_(0),
36         treeWidget_(treewidget),
37         checkBoxes_()
38 {
39         QScrollArea *scrollArea = new QScrollArea(this);
40         scrollArea->setWidgetResizable(true);
41         //scrollArea->setProperty("FingerScrollable", false);
42         QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
43         grid_ = new QGridLayout;
44         QCheckBox *cbox = NULL;
45
46 /// @TODO Kineticscrolling (Fingerscrollable does not work in Qt 4.6, only in 4.5)      
47 #ifdef Q_WS_HILDON
48    //Specific hildon/Maemo5 code here 
49 #endif
50
51         // Create scrollable checkbox dialog to allow proper viewing on Maemo:
52         verticalBox->addWidget(scrollArea);
53         // A "temporary" widget for containing QScrollArea stuff
54         QWidget* scrollAreaWidgetContents = new QWidget(); 
55         scrollAreaWidgetContents->setLayout(grid_);
56         scrollArea->setWidget(scrollAreaWidgetContents);
57         setLayout(verticalBox);
58         
59         if (treeWidget_ != NULL) {
60                 QTreeWidgetItem *item = treeWidget_->headerItem();
61
62                 for (unsigned i = 0; i < item->columnCount(); ++i) {
63                         cbox = new QCheckBox(item->text(i));
64                         grid_->addWidget(cbox, i, 0);
65                         treeWidget_->isColumnHidden(i) ? cbox->setCheckState(Qt::Unchecked) : cbox->setCheckState(Qt::Checked);
66                         checkBoxes_.push_back(cbox);
67                         cbox = NULL;
68                 }
69         }
70         
71         dialogButtons_ = new QDialogButtonBox(this);
72         dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
73                                            | QDialogButtonBox::Cancel); 
74
75         verticalBox->addWidget(dialogButtons_);
76         
77         connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
78                                         this, SLOT(on_buttonClicked(QAbstractButton*)));
79 }
80
81
82 ColumnSelectorDialog::~ColumnSelectorDialog()
83 {
84 }
85
86
87 void ColumnSelectorDialog::on_buttonClicked(QAbstractButton *button)
88 {
89         switch (dialogButtons_->buttonRole ( button ) ) {
90         case QDialogButtonBox::AcceptRole :
91                 ApplySettings();
92                 done(QDialog::Accepted);
93                 break;
94         case QDialogButtonBox::ApplyRole :
95                 ApplySettings();
96                 done(QDialog::Accepted);
97                 break;
98         case QDialogButtonBox::RejectRole :
99                 done(QDialog::Rejected);
100                 break;
101         default:
102                 return;
103         }
104 }
105
106
107 void ColumnSelectorDialog::ApplySettings()
108 {
109         if (treeWidget_ != NULL) {
110                 QTreeWidgetItem *item = treeWidget_->headerItem();
111                 QCheckBox *cbox = NULL;
112                 
113                 for (unsigned i = 0; i < checkBoxes_.size(); ++i) {
114                         cbox = checkBoxes_.at(i);
115                         cbox->isChecked() ? treeWidget_->showColumn(i) : treeWidget_->hideColumn(i);
116                         cbox = NULL;
117                 }
118         }
119 }
120