Added Q_FUNC_INFO macros to some functions.
[qtrapids] / src / client / ColumnSelectorDialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Ixonos Plc   *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; version 2 of the License.               *
7  *                                                                         *
8  *   This program is distributed in the hope that it will be useful,       *
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
11  *   GNU General Public License for more details.                          *
12  *                                                                         *
13  *   You should have received a copy of the GNU General Public License     *
14  *   along with this program; if not, write to the                         *
15  *   Free Software Foundation, Inc.,                                       *
16  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17  ***************************************************************************/
18
19
20 #include <QScrollArea>
21 #include <QGroupBox>
22 #include <QGridLayout>
23 #include <QTreeWidgetItem>
24 #include <QCheckBox>
25 #include <QDialogButtonBox>
26 #include <QAbstractButton>
27
28 #ifdef Q_WS_MAEMO_5
29         #include <QAbstractKineticScroller>
30 #endif
31
32 #include "ColumnSelectorDialog.h"
33
34
35 ColumnSelectorDialog::ColumnSelectorDialog(QTreeWidget *treewidget, QWidget* parent, Qt::WindowFlags f) :
36         QDialog(parent, f), // Superclass construct
37         dialogButtons_(NULL),
38         grid_(NULL),
39         treeWidget_(treewidget),
40         scroller_(NULL),
41         checkBoxes_()
42 {
43         QScrollArea *scrollArea = new QScrollArea(this);
44
45 #ifdef Q_WS_MAEMO_5
46         /// @todo Kineticscrolling (property "FingerScrollable" does not work in Qt 4.6, only in 4.5)   
47         scroller_ = scrollArea->property("kineticScroller").value<QAbstractKineticScroller*>();
48
49         if (!scroller_)                                                               
50                 qFatal("This example only works with QMaemo5Style as the default style for all QAbstractScrollAreas"); 
51 #endif
52
53         scrollArea->setWidgetResizable(true);
54         //scrollArea->setProperty("FingerScrollable", false);
55         QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom);
56         grid_ = new QGridLayout;
57         QCheckBox *cbox = NULL;
58
59
60
61         // Create scrollable checkbox dialog to allow proper viewing on Maemo:
62         verticalBox->addWidget(scrollArea);
63         // A "temporary" widget for containing QScrollArea stuff
64         QWidget* scrollAreaWidgetContents = new QWidget(); 
65         scrollAreaWidgetContents->setLayout(grid_);
66         scrollArea->setWidget(scrollAreaWidgetContents);
67         setLayout(verticalBox);
68         
69         if (treeWidget_ != NULL) {
70                 QTreeWidgetItem *item = treeWidget_->headerItem();
71
72                 for (unsigned i = 0; i < item->columnCount(); ++i) {
73                         cbox = new QCheckBox(item->text(i));
74                         grid_->addWidget(cbox, i, 0);
75                         treeWidget_->isColumnHidden(i) ? cbox->setCheckState(Qt::Unchecked) : cbox->setCheckState(Qt::Checked);
76                         checkBoxes_.push_back(cbox);
77                         cbox = NULL;
78                 }
79         }
80         
81         dialogButtons_ = new QDialogButtonBox(this);
82         dialogButtons_->setStandardButtons(QDialogButtonBox::Ok
83                                            | QDialogButtonBox::Cancel); 
84
85         verticalBox->addWidget(dialogButtons_);
86         
87         connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)),
88                                         this, SLOT(on_buttonClicked(QAbstractButton*)));
89 }
90
91
92 ColumnSelectorDialog::~ColumnSelectorDialog()
93 {
94 }
95
96
97 void ColumnSelectorDialog::on_buttonClicked(QAbstractButton *button)
98 {
99         switch (dialogButtons_->buttonRole ( button ) ) {
100         case QDialogButtonBox::AcceptRole :
101                 ApplySettings();
102                 done(QDialog::Accepted);
103                 break;
104         case QDialogButtonBox::ApplyRole :
105                 ApplySettings();
106                 done(QDialog::Accepted);
107                 break;
108         case QDialogButtonBox::RejectRole :
109                 done(QDialog::Rejected);
110                 break;
111         default:
112                 return;
113         }
114 }
115
116
117 void ColumnSelectorDialog::ApplySettings()
118 {
119         if (treeWidget_ != NULL) {
120                 QTreeWidgetItem *item = treeWidget_->headerItem();
121                 QCheckBox *cbox = NULL;
122                 
123                 for (unsigned i = 0; i < checkBoxes_.size(); ++i) {
124                         cbox = checkBoxes_.at(i);
125                         cbox->isChecked() ? treeWidget_->showColumn(i) : treeWidget_->hideColumn(i);
126                         cbox = NULL;
127                 }
128         }
129 }
130