Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtSql>
- #include "vas.h"
- #include "ui_vas.h"
- #include "storage.h"
- #include "model.h" // Kopiert von hier: http://www.java2s.com/Code/Cpp/Qt/DragDropListModel.htm
- Vas::Vas(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Vas)
- {
- ui->setupUi(this);
- }
- Vas::~Vas()
- {
- delete ui;
- }
- void Vas::on_vaClose_clicked()
- {
- close();
- }
- void Vas::on_vaNewVa_clicked()
- {
- QSqlDatabase db = QSqlDatabase::database();
- QSqlQuery query;
- QSqlRecord record;
- query.prepare("INSERT INTO vas (name)"
- "VALUES(:name)");
- query.bindValue(":name", ui->vaName->text());
- query.exec();
- Storage lager;
- lager.setVaID(query.lastInsertId().toInt());
- query.prepare("SELECT * FROM locations");
- query.exec();
- record = query.record();
- while (query.next()) {
- ui->vaSelectLocation->addItem(query.value(record.indexOf("name")).toString());
- }
- ui->vaBarsOnVa2->setAcceptDrops(true);
- ui->stackedWidget->setCurrentIndex(1);
- }
- void Vas::on_vaSelectLocation_currentIndexChanged(QString )
- {
- QSqlDatabase db = QSqlDatabase::database();
- QSqlQuery query;
- QSqlRecord record;
- QStringList items;
- 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))");
- query.bindValue(":id", ui->vaSelectLocation->currentIndex()+1);
- query.bindValue(":pid", ui->vaSelectLocation->currentIndex()+1);
- query.exec();
- record = query.record();
- while (query.next()) {
- items.append(query.value(record.indexOf("name")).toString());
- }
- DragDropListModel *tableModel = new DragDropListModel(items, this);
- ui->vaBarsOnLocation->setModel(tableModel);
- ui->vaBarsOnLocation->setSelectionMode(QAbstractItemView::SingleSelection);
- ui->vaBarsOnLocation->setDragEnabled(true);
- ui->vaBarsOnLocation->setAcceptDrops(true);
- ui->vaBarsOnLocation->setDropIndicatorShown(true);
- ui->vaBarsOnLocation->resizeColumnsToContents();
- ui->vaBarsOnLocation->show();
- }
- DragDropListModel::DragDropListModel(const QStringList &strings,
- QObject *parent)
- : QStringListModel(strings, parent)
- {
- }
- bool DragDropListModel::dropMimeData(const QMimeData *data,
- Qt::DropAction action, int row, int column, const QModelIndex &parent)
- {
- if (action == Qt::IgnoreAction)
- return true;
- if (!data->hasFormat("application/vnd.text.list"))
- return false;
- if (column > 0)
- return false;
- int beginRow;
- if (row != -1)
- beginRow = row;
- else if (parent.isValid())
- beginRow = parent.row();
- else
- beginRow = rowCount(QModelIndex());
- QByteArray encodedData = data->data("application/vnd.text.list");
- QDataStream stream(&encodedData, QIODevice::ReadOnly);
- QStringList newItems;
- int rows = 0;
- while (!stream.atEnd()) {
- QString text;
- stream >> text;
- newItems << text;
- ++rows;
- }
- insertRows(beginRow, rows, QModelIndex());
- foreach (QString text, newItems) {
- QModelIndex idx = index(beginRow, 0, QModelIndex());
- setData(idx, text);
- beginRow++;
- }
- return true;
- }
- Qt::ItemFlags DragDropListModel::flags(const QModelIndex &index) const
- {
- Qt::ItemFlags defaultFlags = QStringListModel::flags(index);
- if (index.isValid())
- return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
- else
- return Qt::ItemIsDropEnabled | defaultFlags;
- }
- QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes) const
- {
- QMimeData *mimeData = new QMimeData();
- QByteArray encodedData;
- QDataStream stream(&encodedData, QIODevice::WriteOnly);
- foreach (QModelIndex index, indexes) {
- if (index.isValid()) {
- QString text = data(index, Qt::DisplayRole).toString();
- stream << text;
- }
- }
- mimeData->setData("application/vnd.text.list", encodedData);
- return mimeData;
- }
- QStringList DragDropListModel::mimeTypes() const
- {
- QStringList types;
- types << "application/vnd.text.list";
- return types;
- }
- Qt::DropActions DragDropListModel::supportedDropActions() const
- {
- //return Qt::CopyAction | Qt::MoveAction;
- return Qt::MoveAction;
- }
- void Vas::dragEnterEvent(QDragEnterEvent *event)
- {
- if (event->mimeData()->hasFormat("application/vnd.text.list"))
- event->acceptProposedAction();
- }
- void Vas::dropEvent(QDropEvent *event)
- {
- QStringList bars;
- event->setDropAction(Qt::MoveAction);
- bars.append(event->mimeData()->text());
- DragDropListModel *barModel = new DragDropListModel(bars, this);
- ui->vaBarsOnVa2->setModel(barModel);
- ui->vaBarsOnVa2->show();
- }
Advertisement
Add Comment
Please, Sign In to add comment