|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Multiselection dialog |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <hbaction.h> |
|
19 #include <hblabel.h> |
|
20 #include <hbpushbutton.h> |
|
21 #include <hgwidgets/hgwidgets.h> |
|
22 #include <QGraphicsLinearLayout> |
|
23 #include "hgselectiondialog.h" |
|
24 #include "trace.h" |
|
25 |
|
26 HgSelectionDialog::HgSelectionDialog( |
|
27 const QString &title, const QString &primaryText, |
|
28 HgWidget *content, QGraphicsItem *parent) : |
|
29 HbDialog(parent), |
|
30 mHgWidget(content), |
|
31 mSelectAll(new HbPushButton("---")), |
|
32 mCountLabel(new HbLabel("--/--")) |
|
33 { |
|
34 FUNC_LOG; |
|
35 |
|
36 setDismissPolicy(HbDialog::NoDismiss); |
|
37 setTimeout(HbDialog::NoTimeout); |
|
38 setHeadingWidget(new HbLabel(title, this)); |
|
39 setPrimaryAction(new HbAction(primaryText, this)); |
|
40 primaryAction()->setDisabled(true); |
|
41 setSecondaryAction(new HbAction("Cancel", this)); |
|
42 |
|
43 connect(content->selectionModel(), |
|
44 SIGNAL(selectionChanged(QItemSelection, QItemSelection)), |
|
45 SLOT(updateItems())); |
|
46 connect(content->model(), |
|
47 SIGNAL(rowsInserted(QModelIndex, int, int)), |
|
48 SLOT(updateItems())); |
|
49 connect(content->model(), |
|
50 SIGNAL(rowsRemoved(QModelIndex, int, int)), |
|
51 SLOT(updateItems())); |
|
52 |
|
53 HbWidget *contentWidget = new HbWidget; |
|
54 HANDLE_ERROR_NULL(contentWidget); |
|
55 if (contentWidget) { |
|
56 QGraphicsLinearLayout *contentLayout = new QGraphicsLinearLayout(Qt::Vertical); |
|
57 HANDLE_ERROR_NULL(contentLayout); |
|
58 if (contentLayout) { |
|
59 QGraphicsLinearLayout *topLayout = new QGraphicsLinearLayout(Qt::Horizontal); |
|
60 HANDLE_ERROR_NULL(topLayout); |
|
61 if (topLayout) { |
|
62 HANDLE_ERROR_NULL(mSelectAll); |
|
63 if (mSelectAll) { |
|
64 topLayout->addItem(mSelectAll); |
|
65 connect(mSelectAll, SIGNAL(clicked()), SLOT(selectAll())); |
|
66 } |
|
67 if (mCountLabel) { |
|
68 mCountLabel->setAlignment(Qt::AlignRight); |
|
69 topLayout->addItem(mCountLabel); |
|
70 } |
|
71 |
|
72 contentLayout->addItem(topLayout); |
|
73 } |
|
74 |
|
75 contentLayout->addItem(mHgWidget); |
|
76 contentWidget->setLayout(contentLayout); |
|
77 } |
|
78 setContentWidget(contentWidget); |
|
79 } |
|
80 |
|
81 updateItems(); |
|
82 } |
|
83 |
|
84 HgSelectionDialog::~HgSelectionDialog() |
|
85 { |
|
86 FUNC_LOG; |
|
87 } |
|
88 |
|
89 void HgSelectionDialog::updateItems() |
|
90 { |
|
91 FUNC_LOG; |
|
92 |
|
93 if (mHgWidget) { |
|
94 QItemSelectionModel *selectionModel = mHgWidget->selectionModel(); |
|
95 QAbstractItemModel *model = mHgWidget->model(); |
|
96 if (selectionModel && model) { |
|
97 primaryAction()->setEnabled(selectionModel->hasSelection()); |
|
98 |
|
99 int selectedCount = selectionModel->selectedIndexes().count(); |
|
100 int itemCount = model->rowCount(); |
|
101 |
|
102 if (selectedCount == itemCount) { |
|
103 mSelectAll->setText("Unselect all"); |
|
104 mSelectAll->setChecked(false); |
|
105 } |
|
106 else { |
|
107 mSelectAll->setText("Select all"); |
|
108 mSelectAll->setChecked(false); |
|
109 } |
|
110 |
|
111 mCountLabel->setPlainText(tr("%1/%2").arg(selectedCount).arg(itemCount)); |
|
112 } |
|
113 } |
|
114 } |
|
115 |
|
116 void HgSelectionDialog::selectAll() |
|
117 { |
|
118 FUNC_LOG; |
|
119 |
|
120 if (mHgWidget) { |
|
121 QItemSelectionModel *selectionModel = mHgWidget->selectionModel(); |
|
122 QAbstractItemModel *model = mHgWidget->model(); |
|
123 if (selectionModel && model) { |
|
124 int selectedCount = selectionModel->selectedIndexes().count(); |
|
125 int itemCount = model->rowCount(); |
|
126 |
|
127 if (selectedCount == itemCount) { |
|
128 mHgWidget->clearSelection(); |
|
129 } |
|
130 else { |
|
131 mHgWidget->selectAll(); |
|
132 } |
|
133 } |
|
134 } |
|
135 } |