examples/draganddrop/dropsite/dropsitewindow.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 <QtGui>
       
    43 
       
    44 #include "droparea.h"
       
    45 #include "dropsitewindow.h"
       
    46 
       
    47 //! [constructor part1]
       
    48 DropSiteWindow::DropSiteWindow()
       
    49 {
       
    50     abstractLabel = new QLabel(tr("This example accepts drags from other "
       
    51                                   "applications and displays the MIME types "
       
    52                                   "provided by the drag object."));
       
    53     abstractLabel->setWordWrap(true);
       
    54     abstractLabel->adjustSize();
       
    55 //! [constructor part1]
       
    56 
       
    57 //! [constructor part2]
       
    58     dropArea = new DropArea;
       
    59     connect(dropArea, SIGNAL(changed(const QMimeData *)),
       
    60             this, SLOT(updateFormatsTable(const QMimeData *)));
       
    61 //! [constructor part2]
       
    62 
       
    63 //! [constructor part3]
       
    64     QStringList labels;
       
    65     labels << tr("Format") << tr("Content");
       
    66 
       
    67     formatsTable = new QTableWidget;
       
    68     formatsTable->setColumnCount(2);
       
    69     formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
       
    70     formatsTable->setHorizontalHeaderLabels(labels);
       
    71     formatsTable->horizontalHeader()->setStretchLastSection(true);
       
    72 //! [constructor part3]
       
    73 
       
    74 //! [constructor part4]
       
    75     clearButton = new QPushButton(tr("Clear"));
       
    76     quitButton = new QPushButton(tr("Quit"));
       
    77 
       
    78     buttonBox = new QDialogButtonBox;
       
    79     buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
       
    80     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
       
    81 
       
    82     connect(quitButton, SIGNAL(pressed()), this, SLOT(close()));
       
    83     connect(clearButton, SIGNAL(pressed()), dropArea, SLOT(clear()));
       
    84 //! [constructor part4]
       
    85 
       
    86 //! [constructor part5]
       
    87     QVBoxLayout *mainLayout = new QVBoxLayout;
       
    88     mainLayout->addWidget(abstractLabel);
       
    89     mainLayout->addWidget(dropArea);
       
    90     mainLayout->addWidget(formatsTable);
       
    91     mainLayout->addWidget(buttonBox);
       
    92     setLayout(mainLayout);
       
    93 
       
    94     setWindowTitle(tr("Drop Site"));
       
    95     setMinimumSize(350, 500);
       
    96 }
       
    97 //! [constructor part5]
       
    98 
       
    99 //! [updateFormatsTable() part1]
       
   100 void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
       
   101 {
       
   102     formatsTable->setRowCount(0);
       
   103     if (!mimeData)
       
   104         return;
       
   105 //! [updateFormatsTable() part1]
       
   106 
       
   107 //! [updateFormatsTable() part2]        
       
   108     foreach (QString format, mimeData->formats()) {
       
   109         QTableWidgetItem *formatItem = new QTableWidgetItem(format);
       
   110         formatItem->setFlags(Qt::ItemIsEnabled);
       
   111         formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft);
       
   112 //! [updateFormatsTable() part2]
       
   113 
       
   114 //! [updateFormatsTable() part3]
       
   115         QString text;
       
   116         if (format == "text/plain") {
       
   117             text = mimeData->text().simplified();
       
   118         } else if (format == "text/html") {
       
   119             text = mimeData->html().simplified();
       
   120         } else if (format == "text/uri-list") {
       
   121             QList<QUrl> urlList = mimeData->urls();
       
   122             for (int i = 0; i < urlList.size() && i < 32; ++i)
       
   123                 text.append(urlList[i].toString() + " ");
       
   124         } else {
       
   125             QByteArray data = mimeData->data(format);
       
   126             for (int i = 0; i < data.size() && i < 32; ++i) {
       
   127                 QString hex = QString("%1").arg(uchar(data[i]), 2, 16,
       
   128                                                 QChar('0'))
       
   129                                            .toUpper();
       
   130                 text.append(hex + " ");
       
   131             }
       
   132         }
       
   133 //! [updateFormatsTable() part3]   
       
   134 
       
   135 //! [updateFormatsTable() part4]
       
   136         int row = formatsTable->rowCount();
       
   137         formatsTable->insertRow(row);
       
   138         formatsTable->setItem(row, 0, new QTableWidgetItem(format));
       
   139         formatsTable->setItem(row, 1, new QTableWidgetItem(text));
       
   140     }
       
   141     
       
   142     formatsTable->resizeColumnToContents(0);
       
   143 }
       
   144 //! [updateFormatsTable() part4]