comparison widgets/SearchWidget.cpp @ 7:a5175615d153

add easaier tab widgets, style and pass the layer characteristics in the main window (remove from panestack)
author lbajardsilogic
date Fri, 11 May 2007 14:11:19 +0000
parents
children 46af1af183ac
comparison
equal deleted inserted replaced
6:028faa201f0e 7:a5175615d153
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /* Sound Access
4 EASAIER client application.
5 Silogic 2007. Laure Bajard.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version. See the file
11 COPYING included with this distribution for more information.
12 */
13
14 #include "SearchWidget.h"
15
16 #include <QGroupBox>
17 #include <QLabel>
18 #include <QLineEdit>
19 #include <QComboBox>
20 #include <QSpinBox>
21 #include <QPushButton>
22 #include <QScrollArea>
23
24 #include <iostream>
25
26 #include "sv/main/MainWindow.h"
27
28 SearchWidget::SearchWidget() : QWidget(),
29 m_curThemeWidget(0),
30 m_curBoxLayout(0),
31 m_curBoxRow(0)
32 {
33 QVBoxLayout *mainLayout = new QVBoxLayout;
34 mainLayout->setSpacing(15);
35
36 m_buttonLayout = new QHBoxLayout;
37
38 m_themesLayout = new QStackedLayout;
39
40 QWidget *stackedWidget = new QWidget;
41 stackedWidget->setLayout(m_themesLayout);
42
43 QScrollArea * scrollArea = new QScrollArea;
44 scrollArea->setWidget(stackedWidget);
45 scrollArea->setWidgetResizable(true);
46
47 QHBoxLayout *searchButtonLayout = new QHBoxLayout;
48 m_searchButton = new QPushButton(tr("Search"));
49 searchButtonLayout->addStretch(1);
50 searchButtonLayout->addWidget(m_searchButton);
51
52 mainLayout->addLayout(m_buttonLayout);
53 mainLayout->addWidget(scrollArea);
54 mainLayout->addLayout(searchButtonLayout);
55
56 setLayout(mainLayout);
57
58 connect(m_searchButton, SIGNAL(clicked()), MainWindow::instance(), SLOT(queryDatabase()));
59 }
60
61 SearchWidget::~SearchWidget()
62 {
63 reset();
64 }
65
66 void SearchWidget::reset()
67 {
68 QLayoutItem *child;
69
70 while ((child = m_buttonLayout->takeAt(0)) != 0) {
71 delete child->widget();
72 }
73
74 while ((child = m_themesLayout->takeAt(0)) != 0) {
75 delete child->widget();
76 }
77
78 m_themes.clear();
79 }
80
81 void SearchWidget::displayQuerymodel(QueryModel* queryModel)
82 {
83 QueryThemeModel* curTheme;
84
85 std::map<QString , QueryThemeModel* > themes = queryModel->getThemes();
86
87 std::map<QString , QueryThemeModel* >::iterator iter;
88
89 m_buttonLayout->addStretch(1);
90
91 for (iter = themes.begin(); iter != themes.end(); iter++)
92 {
93 //add a new widget to StackedLayout for each theme
94 curTheme = iter->second;
95 addTheme(curTheme->getName(), curTheme->getLabel());
96
97 PropertyContainer::PropertyList properList = curTheme->getProperties();
98 PropertyContainer::PropertyList::iterator propertyIter;
99
100 QString groupName = "";
101
102 for (propertyIter = properList.begin(); propertyIter < properList.end(); propertyIter++)
103 {
104 //add new fields for each of the theme property (that can be grouped)
105 QString name = *propertyIter;
106
107 if (groupName != curTheme->getPropertyGroup(name))
108 {//newfieldgroup
109 endFieldGroup();
110 groupName = curTheme->getPropertyGroup(name);
111 addFieldGroup(groupName, curTheme->getPropertyGroupLabel(name));
112 }
113 addField(name, curTheme);
114 }
115 endFieldGroup();
116 }
117
118 m_buttonLayout->addStretch(1);
119
120 QString themeName = getActiveTheme();
121 m_searchButton->setObjectName(themeName);
122
123 }
124
125 void SearchWidget::addTheme(const QString &name, const QString &label)
126 {
127 QPushButton * themeButton = new QPushButton(label);
128 themeButton->setObjectName(name);
129 m_buttonLayout->addWidget(themeButton);
130
131 m_curThemeWidget = new AdvancedToolBox();
132 m_curThemeWidget->setObjectName(name);
133
134 m_themes[QString(name)] = m_themesLayout->addWidget(m_curThemeWidget);
135
136 connect(themeButton, SIGNAL(clicked()), this, SLOT(activeTheme()));
137 }
138
139 void SearchWidget::addFieldGroup(const QString &name, const QString &label)
140 {
141 QWidget *groupBox = new QWidget();
142 groupBox->setObjectName(name);
143
144 m_curBoxLayout = new QGridLayout();
145 m_curBoxLayout->setObjectName(name);
146
147 groupBox->setLayout(m_curBoxLayout);
148 m_curThemeWidget->addItem(label,groupBox);
149
150 m_curBoxRow = 0;
151 }
152
153 void SearchWidget::endFieldGroup()
154 {
155 m_curBoxLayout = 0;
156 m_curBoxRow = 0;
157 }
158
159 void SearchWidget::addField(const QString &name, QueryThemeModel* curTheme)
160 {
161 QWidget* field = 0;
162
163 QLabel * metadata = new QLabel(curTheme->getPropertyLabel(name));
164 metadata->setObjectName(name);
165
166 m_curBoxLayout->addWidget(metadata, m_curBoxRow, 0);
167
168 PropertyContainer::PropertyType type = curTheme->getPropertyType(name);
169
170 switch (type) //draw a different widget according to the datatype
171 {
172 case PropertyContainer::StringProperty:
173 {
174 QLineEdit * lineEdit = new QLineEdit();
175
176 connect(lineEdit, SIGNAL(textChanged(const QString &)), curTheme, SLOT(setProperty(QString)));
177
178 field = lineEdit;
179
180 break;
181 }
182 case PropertyContainer::ValueProperty:
183 {
184 QComboBox* box = new QComboBox();
185 box->addItems(curTheme->getPropertyRange(name));
186
187 connect(box, SIGNAL(currentIndexChanged(int)), curTheme, SLOT(setProperty(int)));
188
189 field = box;
190
191 break;
192 }
193 case PropertyContainer::RangeProperty:
194 {
195 QSpinBox* box = new QSpinBox();
196
197 int min;
198 int max;
199
200 int value = curTheme->getPropertyRangeAndValue(name, &min, &max);
201
202 if (min != 0)
203 box->setMinimum(min);
204
205 if (max != 0)
206 box->setMaximum(max);
207
208 box->setValue(value);
209
210 connect(box, SIGNAL(valueChanged(int)), curTheme, SLOT(setProperty(int)));
211
212 field = box;
213
214 break;
215 }
216
217 default: break;
218
219 }
220
221 if (field)
222 {
223 field->setObjectName(name);
224 m_curBoxLayout->addWidget(field, m_curBoxRow, 1);
225 }
226
227 QString unit = curTheme->getPropertyUnit(name);
228
229 if (unit != "")
230 {
231 m_curBoxLayout->addWidget(new QLabel(unit), m_curBoxRow, 2);
232 }
233
234 m_curBoxRow++;
235
236 QString comment = curTheme->getPropertyComment(name);
237
238 if (comment != "")
239 {
240 m_curBoxLayout->addWidget(new QLabel(comment), m_curBoxRow, 1);
241 m_curBoxRow++;
242 }
243 }
244
245 void SearchWidget::activeTheme()
246 {
247 QString name = sender()->objectName();
248
249 m_searchButton->setObjectName(name);
250
251 std::map<QString, int>::iterator iter = m_themes.find(name);
252
253 if (iter != m_themes.end())
254 m_themesLayout->setCurrentIndex(iter->second);
255 }
256
257 QString SearchWidget::getActiveTheme()
258 {
259 int index = m_themesLayout->currentIndex();
260
261 std::map<QString, int>::iterator iter;
262
263 for (iter = m_themes.begin(); iter != m_themes.end(); iter++)
264 {
265 if (iter->second == index)
266 return iter->first;
267 }
268
269 return "";
270 }