doc/src/snippets/qtreeview-dnd/dragdropmodel.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     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 documentation 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 /****************************************************************************
       
    43 **
       
    44 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
    45 ** All rights reserved.
       
    46 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
    47 **
       
    48 ** This file is part of an example program for Qt.
       
    49 ** EDITIONS: NOLIMITS
       
    50 **
       
    51 ****************************************************************************/
       
    52 
       
    53 #include <QtGui>
       
    54 
       
    55 #include "dragdropmodel.h"
       
    56 
       
    57 DragDropModel::DragDropModel(const QStringList &strings, QObject *parent)
       
    58     : TreeModel(strings, parent)
       
    59 {
       
    60 }
       
    61 
       
    62 bool DragDropModel::dropMimeData(const QMimeData *data,
       
    63     Qt::DropAction action, int row, int column, const QModelIndex &parent)
       
    64 {
       
    65     if (action == Qt::IgnoreAction)
       
    66         return true;
       
    67 
       
    68     if (!data->hasFormat("text/plain"))
       
    69         return false;
       
    70 
       
    71     int beginRow;
       
    72 
       
    73     if (row != -1)
       
    74         beginRow = row;
       
    75     else if (parent.isValid())
       
    76         beginRow = 0;
       
    77     else
       
    78         beginRow = rowCount(QModelIndex());
       
    79 
       
    80     QByteArray encodedData = data->data("text/plain");
       
    81     QDataStream stream(&encodedData, QIODevice::ReadOnly);
       
    82     QHash<qint64, QMap<int,QHash<int,QString> > > newItems;
       
    83 
       
    84     while (!stream.atEnd()) {
       
    85         qint64 id;
       
    86         int row;
       
    87         int column;
       
    88         QString text;
       
    89         stream >> id >> row >> column >> text;
       
    90         newItems[id][row][column] = text;
       
    91     }
       
    92     int rows = newItems.count();
       
    93 
       
    94     insertRows(beginRow, rows, parent);
       
    95     QMap<int,QHash<int,QString> > childItems;
       
    96     foreach (childItems, newItems.values()) {
       
    97         QHash<int,QString> rowItems;
       
    98         foreach (rowItems, childItems.values()) {
       
    99             foreach (int column, rowItems.keys()) {
       
   100                 QModelIndex idx = index(beginRow, column, parent);
       
   101                 setData(idx, rowItems[column]);
       
   102             }
       
   103             ++beginRow;
       
   104         }
       
   105     }
       
   106 
       
   107     return true;
       
   108 }
       
   109 
       
   110 Qt::ItemFlags DragDropModel::flags(const QModelIndex &index) const
       
   111 {
       
   112     Qt::ItemFlags defaultFlags = TreeModel::flags(index);
       
   113 
       
   114     if (index.isValid())
       
   115         return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
       
   116     else
       
   117         return Qt::ItemIsDropEnabled | defaultFlags;
       
   118 }
       
   119 
       
   120 QMimeData *DragDropModel::mimeData(const QModelIndexList &indexes) const
       
   121 {
       
   122     QMimeData *mimeData = new QMimeData();
       
   123     QByteArray encodedData;
       
   124 
       
   125     QDataStream stream(&encodedData, QIODevice::WriteOnly);
       
   126 
       
   127     foreach (QModelIndex index, indexes) {
       
   128         if (index.isValid()) {
       
   129             QString text = data(index, Qt::DisplayRole).toString();
       
   130             stream << index.internalId() << index.row() << index.column() << text;
       
   131         }
       
   132     }
       
   133 
       
   134     mimeData->setData("text/plain", encodedData);
       
   135     return mimeData;
       
   136 }
       
   137 
       
   138 QStringList DragDropModel::mimeTypes() const
       
   139 {
       
   140     QStringList types;
       
   141     types << "text/plain";
       
   142     return types;
       
   143 }
       
   144 
       
   145 Qt::DropActions DragDropModel::supportedDropActions() const
       
   146 {
       
   147     return Qt::CopyAction | Qt::MoveAction;
       
   148 }