Guest User

vas.cpp

a guest
Oct 12th, 2010
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. #include <QtSql>
  2. #include "vas.h"
  3. #include "ui_vas.h"
  4. #include "storage.h"
  5. #include "model.h" // Kopiert von hier: http://www.java2s.com/Code/Cpp/Qt/DragDropListModel.htm
  6.  
  7. Vas::Vas(QWidget *parent) :
  8.     QDialog(parent),
  9.     ui(new Ui::Vas)
  10. {
  11.     ui->setupUi(this);
  12. }
  13.  
  14. Vas::~Vas()
  15. {
  16.     delete ui;
  17. }
  18.  
  19. void Vas::on_vaClose_clicked()
  20. {
  21.     close();
  22. }
  23.  
  24. void Vas::on_vaNewVa_clicked()
  25. {
  26.     QSqlDatabase db = QSqlDatabase::database();
  27.     QSqlQuery query;
  28.     QSqlRecord record;
  29.  
  30.     query.prepare("INSERT INTO vas (name)"
  31.                   "VALUES(:name)");
  32.     query.bindValue(":name", ui->vaName->text());
  33.     query.exec();
  34.     Storage lager;
  35.     lager.setVaID(query.lastInsertId().toInt());
  36.     query.prepare("SELECT * FROM locations");
  37.     query.exec();
  38.     record = query.record();
  39.     while (query.next()) {
  40.         ui->vaSelectLocation->addItem(query.value(record.indexOf("name")).toString());
  41.     }
  42.     ui->vaBarsOnVa2->setAcceptDrops(true);
  43.     ui->stackedWidget->setCurrentIndex(1);
  44.  
  45. }
  46.  
  47. void Vas::on_vaSelectLocation_currentIndexChanged(QString )
  48. {
  49.     QSqlDatabase db = QSqlDatabase::database();
  50.     QSqlQuery query;
  51.     QSqlRecord record;
  52.     QStringList items;
  53.     query.prepare("SELECT bars.name FROM bars WHERE locid = :id OR (locid = (SELECT b.locid FROM (locations AS a INNER JOIN locationdetails as b ON a.id = b.pid) WHERE b.pid = :pid AND b.pid <> 1))");
  54.     query.bindValue(":id", ui->vaSelectLocation->currentIndex()+1);
  55.     query.bindValue(":pid", ui->vaSelectLocation->currentIndex()+1);
  56.     query.exec();
  57.     record = query.record();
  58.     while (query.next()) {
  59.         items.append(query.value(record.indexOf("name")).toString());
  60.     }
  61.  
  62.     DragDropListModel *tableModel = new DragDropListModel(items, this);
  63.     ui->vaBarsOnLocation->setModel(tableModel);
  64.     ui->vaBarsOnLocation->setSelectionMode(QAbstractItemView::SingleSelection);
  65.     ui->vaBarsOnLocation->setDragEnabled(true);
  66.     ui->vaBarsOnLocation->setAcceptDrops(true);
  67.     ui->vaBarsOnLocation->setDropIndicatorShown(true);
  68.     ui->vaBarsOnLocation->resizeColumnsToContents();
  69.     ui->vaBarsOnLocation->show();
  70. }
  71.  
  72. DragDropListModel::DragDropListModel(const QStringList &strings,
  73.                                      QObject *parent)
  74.     : QStringListModel(strings, parent)
  75. {
  76. }
  77.  
  78.  
  79. bool DragDropListModel::dropMimeData(const QMimeData *data,
  80.     Qt::DropAction action, int row, int column, const QModelIndex &parent)
  81. {
  82.     if (action == Qt::IgnoreAction)
  83.         return true;
  84.  
  85.     if (!data->hasFormat("application/vnd.text.list"))
  86.         return false;
  87.  
  88.     if (column > 0)
  89.         return false;
  90.     int beginRow;
  91.  
  92.     if (row != -1)
  93.         beginRow = row;
  94.     else if (parent.isValid())
  95.         beginRow = parent.row();
  96.     else
  97.         beginRow = rowCount(QModelIndex());
  98.     QByteArray encodedData = data->data("application/vnd.text.list");
  99.     QDataStream stream(&encodedData, QIODevice::ReadOnly);
  100.     QStringList newItems;
  101.     int rows = 0;
  102.  
  103.     while (!stream.atEnd()) {
  104.         QString text;
  105.         stream >> text;
  106.         newItems << text;
  107.         ++rows;
  108.     }
  109.     insertRows(beginRow, rows, QModelIndex());
  110.     foreach (QString text, newItems) {
  111.         QModelIndex idx = index(beginRow, 0, QModelIndex());
  112.         setData(idx, text);
  113.         beginRow++;
  114.     }
  115.  
  116.     return true;
  117. }
  118. Qt::ItemFlags DragDropListModel::flags(const QModelIndex &index) const
  119. {
  120.     Qt::ItemFlags defaultFlags = QStringListModel::flags(index);
  121.  
  122.     if (index.isValid())
  123.         return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
  124.     else
  125.         return Qt::ItemIsDropEnabled | defaultFlags;
  126. }
  127. QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes) const
  128. {
  129.     QMimeData *mimeData = new QMimeData();
  130.     QByteArray encodedData;
  131.  
  132.     QDataStream stream(&encodedData, QIODevice::WriteOnly);
  133.  
  134.     foreach (QModelIndex index, indexes) {
  135.         if (index.isValid()) {
  136.             QString text = data(index, Qt::DisplayRole).toString();
  137.             stream << text;
  138.         }
  139.     }
  140.  
  141.     mimeData->setData("application/vnd.text.list", encodedData);
  142.     return mimeData;
  143. }
  144. QStringList DragDropListModel::mimeTypes() const
  145. {
  146.     QStringList types;
  147.     types << "application/vnd.text.list";
  148.     return types;
  149. }
  150. Qt::DropActions DragDropListModel::supportedDropActions() const
  151. {
  152.     //return Qt::CopyAction | Qt::MoveAction;
  153.     return Qt::MoveAction;
  154. }
  155.  
  156. void Vas::dragEnterEvent(QDragEnterEvent *event)
  157. {
  158.     if (event->mimeData()->hasFormat("application/vnd.text.list"))
  159.         event->acceptProposedAction();
  160. }
  161.  
  162. void Vas::dropEvent(QDropEvent *event)
  163. {
  164.     QStringList bars;
  165.     event->setDropAction(Qt::MoveAction);
  166.     bars.append(event->mimeData()->text());
  167.     DragDropListModel *barModel = new DragDropListModel(bars, this);
  168.     ui->vaBarsOnVa2->setModel(barModel);
  169.     ui->vaBarsOnVa2->show();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment