examples/publish-subscribe/subscriberdialog.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include "subscriberdialog.h"
       
    42 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
    43 #include "ui_subscriberdialog_hor.h"
       
    44 #else
       
    45 #include "ui_subscriberdialog.h"
       
    46 #endif
       
    47 
       
    48 #include <qvaluespacesubscriber.h>
       
    49 
       
    50 #include <QTableWidget>
       
    51 #include <QListWidget>
       
    52 #include <QDesktopWidget>
       
    53 
       
    54 #ifdef QTM_EXAMPLES_SMALL_SCREEN
       
    55 #include <QPushButton>
       
    56 #include <QSizePolicy>
       
    57 #endif
       
    58 
       
    59 #include <QDebug>
       
    60 
       
    61 SubscriberDialog::SubscriberDialog(QWidget *parent) :
       
    62         QDialog(parent),
       
    63         ui(new Ui::SubscriberDialog),
       
    64         subscriber(0),
       
    65         tableWidget(0), listWidget(0)
       
    66 {
       
    67     ui->setupUi(this);
       
    68 
       
    69 #ifdef QTM_EXAMPLES_SMALL_SCREEN
       
    70     QPushButton *switchButton =
       
    71         ui->buttonBox->addButton(tr("Switch"), QDialogButtonBox::ActionRole);
       
    72     connect(switchButton, SIGNAL(clicked()), this, SIGNAL(switchRequested()));
       
    73 #endif
       
    74 
       
    75 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
    76     tableWidget = ui->tableWidget;
       
    77     QStringList headerLabels;
       
    78     headerLabels << tr("Key") << tr("Value") << tr("Type");
       
    79     tableWidget->setColumnCount(3);
       
    80     tableWidget->setHorizontalHeaderLabels(headerLabels);
       
    81     tableWidget->horizontalHeader()->setStretchLastSection(true);
       
    82     tableWidget->verticalHeader()->setVisible(false);
       
    83     tableWidget->setColumnWidth(0, 200);
       
    84     tableWidget->setColumnWidth(1, 400);
       
    85 #else
       
    86     QDesktopWidget desktopWidget;
       
    87     if (desktopWidget.availableGeometry().width() < 400) {
       
    88         // Screen is too small to fit a table widget without scrolling, use a list widget instead.
       
    89         listWidget = new QListWidget;
       
    90         listWidget->setAlternatingRowColors(true);
       
    91         ui->verticalLayout->insertWidget(2, listWidget);
       
    92     } else {
       
    93         tableWidget = new QTableWidget;
       
    94         QStringList headerLabels;
       
    95         headerLabels << tr("Key") << tr("Value") << tr("Type");
       
    96         tableWidget->setColumnCount(3);
       
    97         tableWidget->setHorizontalHeaderLabels(headerLabels);
       
    98         tableWidget->horizontalHeader()->setStretchLastSection(true);
       
    99         tableWidget->verticalHeader()->setVisible(false);
       
   100 
       
   101         ui->verticalLayout->insertWidget(2, tableWidget);
       
   102     }
       
   103 #endif
       
   104     connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(changeSubscriberPath()));
       
   105     changeSubscriberPath();
       
   106 
       
   107     // if the default path does not exist reset it to /
       
   108     QVariant value = subscriber->value();
       
   109     if (!subscriber->value().isValid() && subscriber->subPaths().isEmpty()) {
       
   110         ui->basePath->setText(QLatin1String("/"));
       
   111         changeSubscriberPath();
       
   112     }
       
   113 }
       
   114 
       
   115 SubscriberDialog::~SubscriberDialog()
       
   116 {
       
   117     delete ui;
       
   118 }
       
   119 
       
   120 void SubscriberDialog::changeEvent(QEvent *e)
       
   121 {
       
   122     QDialog::changeEvent(e);
       
   123     switch (e->type()) {
       
   124     case QEvent::LanguageChange:
       
   125         ui->retranslateUi(this);
       
   126         break;
       
   127     default:
       
   128         break;
       
   129     }
       
   130 }
       
   131 
       
   132 //! [0]
       
   133 void SubscriberDialog::changeSubscriberPath()
       
   134 {
       
   135     if (listWidget)
       
   136         listWidget->clear();
       
   137     else if (tableWidget)
       
   138         tableWidget->clearContents();
       
   139 
       
   140     if (!subscriber)
       
   141         subscriber = new QValueSpaceSubscriber(ui->basePath->text(), this);
       
   142     else
       
   143         subscriber->setPath(ui->basePath->text());
       
   144 
       
   145     connect(subscriber, SIGNAL(contentsChanged()), this, SLOT(subscriberChanged()));
       
   146 
       
   147     subscriberChanged();
       
   148 }
       
   149 //! [0]
       
   150 
       
   151 //! [1]
       
   152 void SubscriberDialog::subscriberChanged()
       
   153 {
       
   154     QStringList subPaths = subscriber->subPaths();
       
   155 
       
   156     if (listWidget) {
       
   157         listWidget->clear();
       
   158     } else if (tableWidget) {
       
   159         tableWidget->clearContents();
       
   160         tableWidget->setRowCount(subPaths.count());
       
   161     }
       
   162 
       
   163     for (int i = 0; i < subPaths.count(); ++i) {
       
   164         QVariant v = subscriber->value(subPaths.at(i));
       
   165 
       
   166         if (listWidget) {
       
   167             const QString itemTemplate("%1 (%2)\n%3");
       
   168 
       
   169             QListWidgetItem *item =
       
   170                 new QListWidgetItem(itemTemplate.arg(subPaths.at(i), v.typeName(), v.toString()));
       
   171             item->setFlags(item->flags() & ~Qt::ItemIsEditable);
       
   172             listWidget->addItem(item);
       
   173         } else if (tableWidget) {
       
   174             QTableWidgetItem *pathItem = new QTableWidgetItem(subPaths.at(i));
       
   175             pathItem->setFlags(pathItem->flags() & ~Qt::ItemIsEditable);
       
   176             QTableWidgetItem *valueItem = new QTableWidgetItem(v.toString());
       
   177             valueItem->setFlags(valueItem->flags() & ~Qt::ItemIsEditable);
       
   178             QTableWidgetItem *typeItem = new QTableWidgetItem(v.typeName());
       
   179             typeItem->setFlags(typeItem->flags() & ~Qt::ItemIsEditable);
       
   180 
       
   181             tableWidget->setItem(i, 0, pathItem);
       
   182             tableWidget->setItem(i, 1, valueItem);
       
   183             tableWidget->setItem(i, 2, typeItem);
       
   184         }
       
   185     }
       
   186 }
       
   187 //! [1]