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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user