diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/SearchWidget.cpp	Fri May 11 14:11:19 2007 +0000
@@ -0,0 +1,270 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*	Sound Access	
+		EASAIER client application.	
+		Silogic 2007. Laure Bajard. 
+	
+	This program is free software; you can redistribute it and/or    
+	modify it under the terms of the GNU General Public License as    
+	published by the Free Software Foundation; either version 2 of the    
+	License, or (at your option) any later version.  See the file    
+	COPYING included with this distribution for more information.
+*/
+
+#include "SearchWidget.h"
+
+#include <QGroupBox>
+#include <QLabel>
+#include <QLineEdit>
+#include <QComboBox>
+#include <QSpinBox>
+#include <QPushButton>
+#include <QScrollArea>
+
+#include <iostream>
+
+#include "sv/main/MainWindow.h"
+
+SearchWidget::SearchWidget() : QWidget(),
+	m_curThemeWidget(0),
+	m_curBoxLayout(0),
+	m_curBoxRow(0)
+{
+	QVBoxLayout *mainLayout = new QVBoxLayout;
+	mainLayout->setSpacing(15);
+
+	m_buttonLayout = new QHBoxLayout;
+
+	m_themesLayout = new QStackedLayout;
+
+	QWidget *stackedWidget = new QWidget;
+	stackedWidget->setLayout(m_themesLayout);
+
+	QScrollArea * scrollArea = new QScrollArea;
+	scrollArea->setWidget(stackedWidget);
+	scrollArea->setWidgetResizable(true);
+
+	QHBoxLayout *searchButtonLayout = new QHBoxLayout;
+	m_searchButton = new QPushButton(tr("Search"));
+	searchButtonLayout->addStretch(1);
+	searchButtonLayout->addWidget(m_searchButton);
+
+	mainLayout->addLayout(m_buttonLayout);
+	mainLayout->addWidget(scrollArea);
+	mainLayout->addLayout(searchButtonLayout);
+
+	setLayout(mainLayout);
+
+	connect(m_searchButton, SIGNAL(clicked()), MainWindow::instance(), SLOT(queryDatabase()));
+}
+
+SearchWidget::~SearchWidget()
+{
+	reset();
+}
+
+void SearchWidget::reset()
+{
+	QLayoutItem *child;
+
+	while ((child = m_buttonLayout->takeAt(0)) != 0) {
+		delete child->widget();
+	}
+
+	while ((child = m_themesLayout->takeAt(0)) != 0) {
+		delete child->widget();
+	}
+
+	m_themes.clear();	
+}
+
+void SearchWidget::displayQuerymodel(QueryModel* queryModel)
+{
+	QueryThemeModel* curTheme;
+
+	std::map<QString , QueryThemeModel* > themes = queryModel->getThemes();
+
+	std::map<QString , QueryThemeModel* >::iterator iter;
+
+	m_buttonLayout->addStretch(1);
+
+	for (iter = themes.begin(); iter != themes.end(); iter++)
+	{
+		//add a new widget to StackedLayout for each theme
+		curTheme = iter->second;
+		addTheme(curTheme->getName(), curTheme->getLabel());
+
+		PropertyContainer::PropertyList properList = curTheme->getProperties();
+		PropertyContainer::PropertyList::iterator propertyIter;
+
+		QString groupName = "";
+
+		for (propertyIter = properList.begin(); propertyIter < properList.end(); propertyIter++)
+		{
+			//add new fields for each of the theme property (that can be grouped)
+			QString name = *propertyIter;
+
+			if (groupName != curTheme->getPropertyGroup(name))
+			{//newfieldgroup
+				endFieldGroup();
+				groupName = curTheme->getPropertyGroup(name);
+				addFieldGroup(groupName, curTheme->getPropertyGroupLabel(name));
+			}
+			addField(name, curTheme);
+		}
+		endFieldGroup();
+	}
+	
+	m_buttonLayout->addStretch(1);
+
+	QString themeName = getActiveTheme();
+	m_searchButton->setObjectName(themeName);
+
+}
+
+void SearchWidget::addTheme(const QString &name, const QString &label)
+{
+	QPushButton * themeButton = new QPushButton(label);
+	themeButton->setObjectName(name);
+	m_buttonLayout->addWidget(themeButton);
+
+	m_curThemeWidget = new AdvancedToolBox();
+	m_curThemeWidget->setObjectName(name);
+
+	m_themes[QString(name)] = m_themesLayout->addWidget(m_curThemeWidget);
+
+	connect(themeButton, SIGNAL(clicked()), this, SLOT(activeTheme()));
+}
+
+void SearchWidget::addFieldGroup(const QString &name, const QString &label)
+{
+	QWidget *groupBox = new QWidget();
+	groupBox->setObjectName(name);
+	
+	m_curBoxLayout = new QGridLayout();
+	m_curBoxLayout->setObjectName(name);
+
+	groupBox->setLayout(m_curBoxLayout);
+	m_curThemeWidget->addItem(label,groupBox);
+	
+	m_curBoxRow = 0;
+}
+
+void SearchWidget::endFieldGroup()
+{
+	m_curBoxLayout = 0;
+	m_curBoxRow = 0;
+}
+
+void SearchWidget::addField(const QString &name, QueryThemeModel* curTheme)
+{
+	QWidget* field = 0;
+
+	QLabel * metadata = new QLabel(curTheme->getPropertyLabel(name));
+	metadata->setObjectName(name);
+
+	m_curBoxLayout->addWidget(metadata, m_curBoxRow, 0);
+
+	PropertyContainer::PropertyType type = curTheme->getPropertyType(name);
+
+	switch (type)  //draw a different widget according to the datatype
+	{
+	case PropertyContainer::StringProperty:
+	{
+		QLineEdit * lineEdit = new QLineEdit();
+		
+		connect(lineEdit, SIGNAL(textChanged(const QString &)), curTheme, SLOT(setProperty(QString)));
+
+		field = lineEdit;
+
+		break;
+	}
+	case PropertyContainer::ValueProperty:
+	{
+		QComboBox* box = new QComboBox();
+		box->addItems(curTheme->getPropertyRange(name));
+
+		connect(box, SIGNAL(currentIndexChanged(int)), curTheme, SLOT(setProperty(int)));
+
+		field = box;
+
+		break;
+	}
+	case PropertyContainer::RangeProperty:
+	{
+		QSpinBox* box = new QSpinBox();
+		
+		int min;
+		int max;
+
+		int value = curTheme->getPropertyRangeAndValue(name, &min, &max);
+
+		if (min != 0)
+			box->setMinimum(min);
+
+		if (max != 0)
+			box->setMaximum(max);
+
+		box->setValue(value);
+
+		connect(box, SIGNAL(valueChanged(int)), curTheme, SLOT(setProperty(int)));
+
+		field = box;
+		
+		break;
+	}
+
+	default: break;
+
+	}
+	
+	if (field)
+	{
+		field->setObjectName(name);
+		m_curBoxLayout->addWidget(field, m_curBoxRow, 1);
+	}
+
+	QString unit = curTheme->getPropertyUnit(name);
+
+	if (unit != "")
+	{
+		m_curBoxLayout->addWidget(new QLabel(unit), m_curBoxRow, 2);
+	}
+
+	m_curBoxRow++;
+
+	QString comment = curTheme->getPropertyComment(name);
+
+	if (comment != "")
+	{
+		m_curBoxLayout->addWidget(new QLabel(comment), m_curBoxRow, 1);
+		m_curBoxRow++;
+	}
+}
+
+void SearchWidget::activeTheme()
+{
+	QString name = sender()->objectName();
+
+	m_searchButton->setObjectName(name);
+
+	std::map<QString, int>::iterator iter = m_themes.find(name);
+	
+	if (iter != m_themes.end())
+		m_themesLayout->setCurrentIndex(iter->second);
+}
+
+QString SearchWidget::getActiveTheme()
+{
+	int index = m_themesLayout->currentIndex();
+
+	std::map<QString, int>::iterator iter;
+
+	for (iter = m_themes.begin(); iter != m_themes.end(); iter++)
+	{
+		if (iter->second == index)
+			return iter->first;
+	}
+
+	return "";
+}