- Fixed KineticScrolling due to Qt 4.6.1 changes.
[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 <QAbstractKineticScroller>
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_(NULL),
40         grid_(NULL),
41         treeWidget_(treewidget),
42         scroller_(NULL)
43         checkBoxes_()
44 {
45         QScrollArea *scrollArea = new QScrollArea(this);
46
47 #ifdef Q_WS_MAEMO_5
48         /// @todo Kineticscrolling (property "FingerScrollable" does not work in Qt 4.6, only in 4.5)   
49         scroller_ = scrollArea->property("kineticScroller").value<QAbstractKineticScroller*>();
50
51         if (!scroller_)                                                               
52                 qFatal("This example only works with QMaemo5Style as the default style for all QAbstractScrollAreas"); 
53 #endif
54
55         scrollArea->setWidgetResizable(true);
56         //scrollArea->setProperty("FingerScrollable", false);
57         QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
58         grid_ = new QGridLayout;
59         QCheckBox *cbox = NULL;
60
61
62
63         // Create scrollable checkbox dialog to allow proper viewing on Maemo:
64         verticalBox->addWidget(scrollArea);
65         // A "temporary" widget for containing QScrollArea stuff
66         QWidget* scrollAreaWidgetContents = new QWidget(); 
67         scrollAreaWidgetContents->setLayout(grid_);
68         scrollArea->setWidget(scrollAreaWidgetContents);
69         setLayout(verticalBox);
70         
71         if (treeWidget_ != NULL) {
72                 QTreeWidgetItem *item = treeWidget_->headerItem();
73
74                 for (unsigned i = 0; i < item->columnCount(); ++i) {
75                         cbox = new QCheckBox(item->text(i));
76                         grid_->addWidget(cbox, i, 0);
77                         treeWidget_->isColumnHidden(i) ? cbox->setCheckState(Qt::Unchecked) : cbox->setCheckState(Qt::Checked);
78                         checkBoxes_.push_back(cbox);
79                         cbox = NULL;
80                 }
81         }
82         
83         dialogButtons_ = new QDialogButtonBox(this);
84         dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
85                                            | QDialogButtonBox::Cancel); 
86
87         verticalBox->addWidget(dialogButtons_);
88         
89         connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
90                                         this, SLOT(on_buttonClicked(QAbstractButton*)));
91 }
92
93
94 ColumnSelectorDialog::~ColumnSelectorDialog()
95 {
96 }
97
98
99 void ColumnSelectorDialog::on_buttonClicked(QAbstractButton *button)
100 {
101         switch (dialogButtons_->buttonRole ( button ) ) {
102         case QDialogButtonBox::AcceptRole :
103                 ApplySettings();
104                 done(QDialog::Accepted);
105                 break;
106         case QDialogButtonBox::ApplyRole :
107                 ApplySettings();
108                 done(QDialog::Accepted);
109                 break;
110         case QDialogButtonBox::RejectRole :
111                 done(QDialog::Rejected);
112                 break;
113         default:
114                 return;
115         }
116 }
117
118
119 void ColumnSelectorDialog::ApplySettings()
120 {
121         if (treeWidget_ != NULL) {
122                 QTreeWidgetItem *item = treeWidget_->headerItem();
123                 QCheckBox *cbox = NULL;
124                 
125                 for (unsigned i = 0; i < checkBoxes_.size(); ++i) {
126                         cbox = checkBoxes_.at(i);
127                         cbox->isChecked() ? treeWidget_->showColumn(i) : treeWidget_->hideColumn(i);
128                         cbox = NULL;
129                 }
130         }
131 }
132