shitty table WIP
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "checkqueuetablemodel.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <QMimeData>
|
||||
#include <QIODevice>
|
||||
|
||||
CheckQueueTableModel::CheckQueueTableModel(std::vector<Check> *checks, QObject *parent)
|
||||
: checks(checks), QAbstractTableModel{parent}
|
||||
@@ -10,8 +11,10 @@ int CheckQueueTableModel::rowCount(const QModelIndex &parent) const { return che
|
||||
int CheckQueueTableModel::columnCount(const QModelIndex &parent) const { return 3; }
|
||||
|
||||
QVariant CheckQueueTableModel::data(const QModelIndex &index, int role) const {
|
||||
if (!index.isValid() || index.row() >= checks->size())
|
||||
return QVariant();
|
||||
if (role != Qt::DisplayRole) return QVariant();
|
||||
Check& c = (*checks).at(index.row());
|
||||
Check& c = checks->at(index.row());
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
return QVariant::fromValue(QString::fromStdString(c.get_date()));
|
||||
@@ -33,24 +36,16 @@ bool CheckQueueTableModel::setData(const QModelIndex &index, const QVariant &val
|
||||
return false;
|
||||
unsigned int row = index.row();
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
case 0: {
|
||||
checks->at(row).set_date(value.value<std::string>());
|
||||
break;
|
||||
case 1:
|
||||
} case 1:
|
||||
checks->at(row).set_total(value.value<double>());
|
||||
break;
|
||||
case 2:
|
||||
// delete Button
|
||||
break;
|
||||
}
|
||||
|
||||
//for presentation purposes only: build and emit a joined string
|
||||
// QString result = "dick";
|
||||
// for (int row = 0; row < checks->size(); row++) {
|
||||
// for (int col= 0; col < 3; col++)
|
||||
// result += m_gridData[row][col] + ' ';
|
||||
// }
|
||||
// emit editCompleted(result);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -74,21 +69,100 @@ QVariant CheckQueueTableModel::headerData(int section, Qt::Orientation orientati
|
||||
}
|
||||
|
||||
Qt::ItemFlags CheckQueueTableModel::flags(const QModelIndex &index) const {
|
||||
Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
|
||||
auto flags = QAbstractItemModel::flags(index);
|
||||
|
||||
if (index.isValid())
|
||||
return Qt::ItemIsDragEnabled | defaultFlags;
|
||||
flags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
|
||||
else
|
||||
return Qt::ItemIsDropEnabled | defaultFlags;
|
||||
// return Qt::ItemIsSelectable | QAbstractTableModel::flags(index);
|
||||
flags |= Qt::ItemIsDropEnabled;
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
bool CheckQueueTableModel::insertRow(int row, int count, const QModelIndex &parent) {
|
||||
beginInsertRows(QModelIndex(), row, row+count-1);
|
||||
Qt::DropActions CheckQueueTableModel::supportedDropActions() const {
|
||||
return Qt::DropActions() | Qt::MoveAction;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
checks->emplace(checks->begin() + row, Check());
|
||||
bool CheckQueueTableModel::insertRows(int position, int rows, const QModelIndex &index) {
|
||||
beginInsertRows(QModelIndex(), position, position+rows-1);
|
||||
|
||||
for (int i = 0; i < rows; ++i)
|
||||
checks->emplace(checks->begin() + position, Check());
|
||||
|
||||
endInsertRows();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CheckQueueTableModel::removeRows(int position, int rows, const QModelIndex &index) {
|
||||
beginRemoveRows(QModelIndex(), position, position+rows-1);
|
||||
|
||||
for (int row = 0; row < rows; ++row)
|
||||
checks->erase(std::next(checks->begin(), position));
|
||||
|
||||
endRemoveRows();
|
||||
return true;
|
||||
}
|
||||
|
||||
QStringList CheckQueueTableModel::mimeTypes() const {
|
||||
QStringList types;
|
||||
types << CheckQueueTableModel::MimeType;
|
||||
return types;
|
||||
}
|
||||
|
||||
bool CheckQueueTableModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &) {
|
||||
if (action != Qt::MoveAction || !data->hasFormat(CheckQueueTableModel::MimeType))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
QMimeData* CheckQueueTableModel::mimeData(const QModelIndexList &indexes) const {
|
||||
QMimeData* mimeData = new QMimeData;
|
||||
QByteArray encodedData;
|
||||
|
||||
QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
||||
|
||||
for (const QModelIndex &i : indexes) {
|
||||
if (i.isValid() && i.column() == 0) {
|
||||
QString date = data(i, Qt::DisplayRole).toString();
|
||||
double total = data(index(i.row(), i.column() + 1), Qt::DisplayRole).toDouble();
|
||||
stream << date << total;
|
||||
}
|
||||
}
|
||||
mimeData->setData(CheckQueueTableModel::MimeType, encodedData);
|
||||
return mimeData;
|
||||
}
|
||||
|
||||
bool CheckQueueTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
|
||||
if (!canDropMimeData(data, action, row, column, parent))
|
||||
return false;
|
||||
if (action == Qt::IgnoreAction)
|
||||
return true;
|
||||
else if (action != Qt::MoveAction)
|
||||
return false;
|
||||
QByteArray encodedData = data->data(CheckQueueTableModel::MimeType);
|
||||
QDataStream stream(&encodedData, QIODevice::ReadOnly);
|
||||
std::vector<Check> newItems;
|
||||
int rows = 0;
|
||||
|
||||
while (!stream.atEnd()) {
|
||||
QString date;
|
||||
double total;
|
||||
stream >> date >> total;
|
||||
Check c;
|
||||
c.set_date(date.toStdString());
|
||||
c.set_total(total);
|
||||
newItems.push_back(c);
|
||||
++rows;
|
||||
}
|
||||
|
||||
insertRows(row, rows, QModelIndex());
|
||||
for (Check &c : newItems) {
|
||||
QModelIndex date_index = index(row, 0, QModelIndex());
|
||||
QModelIndex total_index = index(row, 1, QModelIndex());
|
||||
setData(date_index, QVariant::fromValue(c.get_date()), Qt::EditRole);
|
||||
setData(total_index, QVariant::fromValue(c.get_total()), Qt::EditRole);
|
||||
row++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
class CheckQueueTableModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
static constexpr const char* MimeType = "application/check.queue.model";
|
||||
public:
|
||||
explicit CheckQueueTableModel(std::vector<Check> *checks, QObject *parent = nullptr);
|
||||
|
||||
@@ -19,8 +20,16 @@ public:
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
Qt::DropActions supportedDropActions() const override;
|
||||
|
||||
bool insertRow(int row, int count, const QModelIndex &parent = QModelIndex());
|
||||
// bool insertRow(int row, int count, const QModelIndex &parent = QModelIndex());
|
||||
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
|
||||
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
|
||||
|
||||
QStringList mimeTypes() const override;
|
||||
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &);
|
||||
QMimeData* mimeData(const QModelIndexList &indexes) const override;
|
||||
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
|
||||
private:
|
||||
std::vector<Check> *checks;
|
||||
signals:
|
||||
|
||||
47
widgets/checkqueuetableview.cpp
Normal file
47
widgets/checkqueuetableview.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "checkqueuetableview.h"
|
||||
|
||||
CheckQueueTableView::CheckQueueTableView(QWidget *parent)
|
||||
: QTableView(parent), m_dropRow(0) {
|
||||
setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
setDragEnabled(true);
|
||||
setAcceptDrops(true);
|
||||
setDragDropMode(QAbstractItemView::DragDrop);
|
||||
setDefaultDropAction(Qt::MoveAction);
|
||||
setDragDropOverwriteMode(false);
|
||||
setDropIndicatorShown(true);
|
||||
}
|
||||
|
||||
int CheckQueueTableView::selectedRow() const {
|
||||
QItemSelectionModel *selection = selectionModel();
|
||||
return selection->hasSelection() ? selection->selectedRows().front().row() : -1;
|
||||
}
|
||||
|
||||
void CheckQueueTableView::reset() {
|
||||
QTableView::reset();
|
||||
|
||||
QObject::connect(model(), &QAbstractTableModel::rowsInserted, this, [this](const QModelIndex &parent, int first, int last) {
|
||||
Q_UNUSED(parent)
|
||||
Q_UNUSED(last)
|
||||
m_dropRow = first;
|
||||
});
|
||||
}
|
||||
|
||||
void CheckQueueTableView::dropEvent(QDropEvent *e) {
|
||||
if (e->source() != this || e->dropAction() != Qt::MoveAction)
|
||||
return;
|
||||
|
||||
int dragRow = selectedRow();
|
||||
|
||||
QTableView::dropEvent(e); // m_dropRow is set by inserted row
|
||||
|
||||
if (m_dropRow > dragRow)
|
||||
--m_dropRow;
|
||||
|
||||
QMetaObject::invokeMethod(this,
|
||||
std::bind(&CheckQueueTableView::selectRow, this, m_dropRow),
|
||||
Qt::QueuedConnection); // Postpones selection
|
||||
}
|
||||
|
||||
|
||||
|
||||
19
widgets/checkqueuetableview.h
Normal file
19
widgets/checkqueuetableview.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef CHECKQUEUETABLEVIEW_H
|
||||
#define CHECKQUEUETABLEVIEW_H
|
||||
|
||||
#include <QTableView>
|
||||
#include <QDropEvent>
|
||||
|
||||
class CheckQueueTableView : public QTableView
|
||||
{
|
||||
Q_OBJECT
|
||||
int m_dropRow;
|
||||
public:
|
||||
CheckQueueTableView(QWidget *parent = nullptr);
|
||||
Q_INVOKABLE int selectedRow() const;
|
||||
void reset();
|
||||
void dropEvent(QDropEvent *e);
|
||||
|
||||
};
|
||||
|
||||
#endif // CHECKQUEUETABLEVIEW_H
|
||||
@@ -77,12 +77,6 @@ bool OutputColumnModel::removeRows(int position, int rows, const QModelIndex &pa
|
||||
return true;
|
||||
}
|
||||
|
||||
QStringList OutputColumnModel::mimeTypes() const {
|
||||
QStringList types;
|
||||
types << OutputColumnModel::MimeType;
|
||||
return types;
|
||||
}
|
||||
|
||||
bool OutputColumnModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &) {
|
||||
if ( action != Qt::MoveAction || !data->hasFormat(OutputColumnModel::MimeType))
|
||||
return false;
|
||||
|
||||
@@ -28,7 +28,7 @@ public:
|
||||
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
|
||||
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
|
||||
|
||||
QStringList mimeTypes() const override;
|
||||
// QStringList mimeTypes() const override;
|
||||
bool canDropMimeData(const QMimeData *, Qt::DropAction, int, int, const QModelIndex&);
|
||||
QMimeData* mimeData(const QModelIndexList &indexes) const;
|
||||
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
|
||||
|
||||
Reference in New Issue
Block a user