AdvancedSearchWidget.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "AdvancedSearchWidget.h"
00025 
00026 using UIs::Searching::  AdvancedSearchWidget;
00027 
00028 AdvancedSearchWidget::AdvancedSearchWidget (const QString &schemaFileName,
00029                                             QWidget *parent)
00030  :  QGroupBox(parent), outputDoc(), elementToString()
00031 {
00032     QFile           file (schemaFileName);
00033     QDomDocument    doc;
00034     QString         errorStr;
00035     int             errorLine;
00036     int             errorColumn;
00037 
00038     if (!doc.setContent (&file, true, &errorStr, &errorLine, &errorColumn)) {
00039         QMessageBox::warning (0, QObject::tr ("XML Schema Parser"),
00040                               QObject::tr ("Parse error at line %1, ""column %2:\n%3").arg (errorLine).arg (errorColumn).arg (errorStr));
00041     } else {
00042         CreateWidget (doc);
00043     }
00044 }
00045 
00046 AdvancedSearchWidget::~AdvancedSearchWidget ()
00047 {
00048 
00049 }
00050 
00051 void AdvancedSearchWidget::CreateWidget (const QDomDocument &schema)
00052 {
00053     QVBoxLayout *vLayout;
00054 
00055     QDomElement root = schema.documentElement ();
00056     if (root.tagName () != "schema")
00057         return ;
00058 
00059     QDomNode    elementNode = root.firstChild ();
00060 
00061     // loop through all top-level nodes
00062     while (!elementNode.isNull ()) {
00063         if (elementNode.toElement ().tagName () == "element"
00064         && elementNode.attributes ().contains ("name")) {
00065             QString groupBoxTitle = elementNode.attributes ().namedItem ("name").nodeValue ();
00066             setTitle (groupBoxTitle);
00067 
00068             // add root element
00069             outputDoc.appendChild (outputDoc.createElement (groupBoxTitle));
00070 
00071             vLayout = new QVBoxLayout (this);
00072 
00073             QDomNode    node = elementNode.firstChild ();
00074             if (node.toElement ().tagName () == "complexType") {
00075                 node = node.firstChild ();
00076                 if (node.toElement ().tagName () == "element"
00077                 && node.attributes ().contains ("type")
00078                 && node.attributes ().contains ("name")) {
00079                     QString     elementName = node.toElement ().attributes ()
00080                         .namedItem ("name").nodeValue ();
00081                     QString     elementType = node.toElement ().attributes ()
00082                         .namedItem ("type").nodeValue ();
00083 
00084                     QGridLayout *typeWidget = CreateWidgetForType (schema,
00085                                                                    elementType);
00086 
00087                     if (typeWidget == NULL)
00088                         return ;
00089                     vLayout->addLayout (typeWidget);
00090 
00091                     QDomElement newType = outputDoc.createElement(elementName);
00092                     outputDoc.documentElement ().appendChild (newType);
00093                 }
00094             }
00095         }
00096 
00097         elementNode = elementNode.nextSibling ();
00098     }
00099 }
00100 
00101 QGridLayout *AdvancedSearchWidget::CreateWidgetForType (const QDomDocument &schema,
00102                                                         const QString &typeName)
00103 {
00104     QGridLayout *gridLayout = new QGridLayout ();
00105 
00106 
00107     QDomElement root = schema.documentElement ();
00108     if (root.tagName () != "schema")
00109         return NULL;
00110 
00111     QDomNodeList    complexTypes = root.elementsByTagName ("complexType");
00112     int             typeIdx = 0;
00113 
00114     for (; typeIdx < complexTypes.size (); typeIdx++) {
00115         if (complexTypes.at (typeIdx).attributes ().contains ("name")
00116         && complexTypes.at (typeIdx).attributes ().namedItem ("name").nodeValue ()
00117             == typeName)
00118             break;
00119     }
00120 
00121     int rowCount = 0;
00122     if (typeIdx != complexTypes.size ()) {
00123 
00124         QDomNode    requiredType = complexTypes.at (typeIdx);
00125 
00126         if (requiredType.firstChild ().toElement ().tagName () == "all") {
00127             QDomNodeList    attributes = requiredType.firstChild ().childNodes ();
00128 
00129             for (int atIdx = 0; atIdx < attributes.size (); atIdx++) {
00130                 QDomNode    attribute = attributes.at (atIdx);
00131                 if (attribute.toElement ().tagName () != "attribute")
00132                     continue;
00133                 if (!attribute.attributes ().contains ("name"))
00134                     continue;
00135 
00136                 if (attribute.attributes ().namedItem ("hidden").nodeValue ()
00137                         == "true")
00138                     continue;
00139 
00140                 QString attributeName =
00141                     attribute.attributes ().namedItem ("name").nodeValue ();
00142                 QLabel      *attributeLabel = new QLabel (attributeName);
00143                 QWidget     *editWidget;
00144 
00145                 gridLayout->addWidget (attributeLabel, rowCount, 0);
00146 
00147                 if (attribute.childNodes ().isEmpty ()) {
00148                     if (attribute.attributes ().contains ("type")) {
00149                         QString attributeType =
00150                             attribute.attributes ().namedItem ("type").nodeValue ();
00151                     }
00152                     editWidget = new QLineEdit ();
00153                     QObject::connect (editWidget, SIGNAL ( editingFinished ()),
00154                         this, SLOT (elementEditingFinished ()) );
00155                 } else {
00156 
00157                     // unknown complex type
00158                     if (attribute.childNodes ().isEmpty ())
00159                         return NULL;
00160 
00161                     QDomNode    simpleType = attribute.childNodes ().item (0);
00162 
00163                     if (simpleType.toElement ().tagName () != "simpleType")
00164                         return NULL;
00165 
00166                     if (simpleType.attributes ().namedItem("base").nodeValue ()
00167                         == "string") {
00168                         QComboBox   *en = new QComboBox ();
00169                         editWidget = en;
00170 
00171                         for (unsigned int idx = 0; idx < simpleType.childNodes ().length ();
00172                              idx++) {
00173                             QDomNode    enumeration = simpleType.childNodes ()
00174                                 .item (idx);
00175 
00176                             if (enumeration.toElement ().tagName ()
00177                                     != "enumeration")
00178                                 continue;
00179                             if (enumeration.attributes ().contains ("value"))
00180                                 en->addItem (enumeration.attributes ().namedItem ("value").nodeValue ());
00181                         }
00182                     } else {
00183 
00184                         // unknown base type
00185                         return NULL;
00186                     }
00187                 }
00188 
00189                 elementToString.insert(editWidget, attributeName);
00190                 editWidget->setFixedWidth (150);
00191                 gridLayout->addWidget (editWidget, rowCount++, 1);
00192             }
00193 
00194             return gridLayout;
00195         }
00196     }
00197 
00198     return NULL;
00199 }
00200 
00201 QDomDocument const *AdvancedSearchWidget::XMLRequest () const
00202 {
00203     return &outputDoc;
00204 }
00205 
00206 
00207 void AdvancedSearchWidget::elementEditingFinished ()
00208 {
00209     QString text("");
00210     QString attributeName("");
00211 
00212     QWidget * senderWidget = qobject_cast <QWidget *> (sender ());
00213     if (senderWidget == 0) return;
00214 
00215     attributeName = elementToString.value (senderWidget);
00216 
00217     QLineEdit *lineedit = qobject_cast <QLineEdit *> (senderWidget);
00218     if (lineedit != 0)
00219     {
00220         text = lineedit->text ();
00221     }
00222     QComboBox *combobox = qobject_cast <QComboBox *> (senderWidget);
00223     if (combobox != 0)
00224     {
00225         text = combobox->currentText ();
00226     }
00227 
00228     if (attributeName != "")
00229     {
00230         QDomElement root = outputDoc.documentElement ();
00231         QDomElement mainElem = root.firstChild ().toElement ();
00232 
00233         if (text != "")
00234             mainElem.setAttribute (attributeName, text);
00235         else
00236             mainElem.removeAttribute (attributeName);
00237     }
00238 }