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