examples/xmlpatterns/trafficinfo/stationdialog.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "stationdialog.h"
       
    43 #include "ui_stationdialog.h"
       
    44 
       
    45 #include <QtCore/QAbstractListModel>
       
    46 
       
    47 class StationModel : public QAbstractListModel
       
    48 {
       
    49     public:
       
    50         enum Role
       
    51         {
       
    52             StationIdRole = Qt::UserRole + 1,
       
    53             StationNameRole
       
    54         };
       
    55 
       
    56         StationModel(QObject *parent = 0)
       
    57             : QAbstractListModel(parent)
       
    58         {
       
    59         }
       
    60 
       
    61         void setStations(const StationInformation::List &list)
       
    62         {
       
    63             m_stations = list;
       
    64             layoutChanged();
       
    65         }
       
    66 
       
    67         virtual int rowCount(const QModelIndex &parent = QModelIndex()) const
       
    68         {
       
    69             if (!parent.isValid())
       
    70                 return m_stations.count();
       
    71             else
       
    72                 return 0;
       
    73         }
       
    74 
       
    75         virtual int columnCount(const QModelIndex &parent = QModelIndex()) const
       
    76         {
       
    77             if (!parent.isValid())
       
    78                 return 1;
       
    79             else
       
    80                 return 0;
       
    81         }
       
    82 
       
    83         virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
       
    84         {
       
    85             if (!index.isValid())
       
    86                 return QVariant();
       
    87 
       
    88             if (index.column() > 1 || index.row() >= m_stations.count())
       
    89                 return QVariant();
       
    90 
       
    91             const StationInformation info = m_stations.at(index.row());
       
    92             if (role == Qt::DisplayRole || role == StationNameRole)
       
    93                 return info.name();
       
    94             else if (role == StationIdRole)
       
    95                 return info.id();
       
    96 
       
    97             return QVariant();
       
    98         }
       
    99 
       
   100     private:
       
   101         StationInformation::List m_stations;
       
   102 };
       
   103 
       
   104 StationDialog::StationDialog(const QString &name, const QStringList &lineNumbers, QWidget *parent)
       
   105     : QDialog(parent)
       
   106 {
       
   107     m_ui.setupUi(this);
       
   108 
       
   109     connect(m_ui.m_searchButton, SIGNAL(clicked()), this, SLOT(searchStations()));
       
   110 
       
   111     m_ui.m_searchButton->setDefault(true);
       
   112     m_ui.m_input->setText(name);
       
   113 
       
   114     m_model = new StationModel(this);
       
   115     m_ui.m_view->setModel(m_model);
       
   116 
       
   117     for (int i = 0; i < lineNumbers.count(); ++i) {
       
   118         if (i == 0)
       
   119             m_ui.m_line1->setText(lineNumbers.at(i));
       
   120         else if (i == 1)
       
   121             m_ui.m_line2->setText(lineNumbers.at(i));
       
   122         else if (i == 2)
       
   123             m_ui.m_line3->setText(lineNumbers.at(i));
       
   124         else if (i == 3)
       
   125             m_ui.m_line4->setText(lineNumbers.at(i));
       
   126     }
       
   127 
       
   128     QMetaObject::invokeMethod(this, SLOT(searchStations()), Qt::QueuedConnection);
       
   129 }
       
   130 
       
   131 StationInformation StationDialog::selectedStation() const
       
   132 {
       
   133     const QModelIndex index = m_ui.m_view->currentIndex();
       
   134 
       
   135     if (!index.isValid())
       
   136         return StationInformation();
       
   137 
       
   138     return StationInformation(index.data(StationModel::StationIdRole).toString(),
       
   139                               index.data(StationModel::StationNameRole).toString());
       
   140 }
       
   141 
       
   142 QStringList StationDialog::lineNumbers() const
       
   143 {
       
   144     QStringList lines;
       
   145 
       
   146     if (!m_ui.m_line1->text().simplified().isEmpty())
       
   147         lines.append(m_ui.m_line1->text().simplified());
       
   148     if (!m_ui.m_line2->text().simplified().isEmpty())
       
   149         lines.append(m_ui.m_line2->text().simplified());
       
   150     if (!m_ui.m_line3->text().simplified().isEmpty())
       
   151         lines.append(m_ui.m_line3->text().simplified());
       
   152     if (!m_ui.m_line4->text().simplified().isEmpty())
       
   153         lines.append(m_ui.m_line4->text().simplified());
       
   154 
       
   155     return lines;
       
   156 }
       
   157 
       
   158 void StationDialog::searchStations()
       
   159 {
       
   160     m_model->setStations(StationQuery::query(m_ui.m_input->text()));
       
   161     m_ui.m_view->keyboardSearch(m_ui.m_input->text());
       
   162 }