150 lines
4.6 KiB
C++
150 lines
4.6 KiB
C++
#include "outputcolumnmodel.h"
|
|
|
|
#include <QObject>
|
|
#include <QMimeData>
|
|
#include <QIODevice>
|
|
#include <outputcolumn.h>
|
|
|
|
OutputColumnModel::OutputColumnModel(std::vector<OutputColumn> *columns, QObject *parent)
|
|
: columns(columns), QAbstractListModel{parent} { }
|
|
|
|
int OutputColumnModel::rowCount(const QModelIndex &parent) const {
|
|
return columns->size();
|
|
}
|
|
|
|
QVariant OutputColumnModel::data(const QModelIndex &index, int role) const {
|
|
if (!index.isValid() || index.row() >= columns->size())
|
|
return QVariant();
|
|
|
|
if (role == Qt::DisplayRole)
|
|
return QVariant::fromValue(columns->at(index.row()).get_text());
|
|
else if (role == 0x0101)
|
|
return QVariant::fromValue(columns->at(index.row()).get_column_type());
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
QVariant OutputColumnModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
if (role != Qt::DisplayRole)
|
|
return QVariant();
|
|
|
|
if (orientation == Qt::Horizontal)
|
|
return QObject::tr("Column type alias");
|
|
else
|
|
return QStringLiteral("Position %1").arg(section);
|
|
}
|
|
|
|
bool OutputColumnModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
|
if (index.isValid()) {
|
|
OutputColumn &column = columns->at(index.row());
|
|
if (role == 0x102) {
|
|
OutputColumn data = value.value<OutputColumn>();
|
|
column.set_text(data.get_text());
|
|
column.set_column_type(data.get_column_type());
|
|
} else {
|
|
QString data = value.value<QString>();
|
|
column.set_text(data);
|
|
}
|
|
|
|
(*columns)[index.row()] = column;
|
|
emit dataChanged(index, index, {role});
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Qt::DropActions OutputColumnModel::supportedDropActions() const {
|
|
return Qt::MoveAction;
|
|
}
|
|
|
|
bool OutputColumnModel::insertRows(int position, int rows, const QModelIndex &index) {
|
|
beginInsertRows(QModelIndex(), position, position+rows-1);
|
|
|
|
for (int row = 0; row < rows; ++row)
|
|
columns->emplace(columns->begin() + position, OutputColumn("Чё зыришь глаза пузыришь?", ColumnType::date));
|
|
|
|
endInsertRows();
|
|
return true;
|
|
}
|
|
|
|
bool OutputColumnModel::removeRows(int position, int rows, const QModelIndex &parent) {
|
|
beginRemoveRows(QModelIndex(), position, position+rows-1);
|
|
|
|
for (int row = 0; row < rows; ++row)
|
|
columns->erase(std::next(columns->begin(), position));
|
|
|
|
endRemoveRows();
|
|
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;
|
|
return true;
|
|
}
|
|
|
|
Qt::ItemFlags OutputColumnModel::flags (const QModelIndex & index) const {
|
|
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
|
|
|
|
if (index.isValid())
|
|
return Qt::ItemIsDragEnabled | Qt::ItemIsEditable | defaultFlags;
|
|
else
|
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
|
}
|
|
|
|
QMimeData* OutputColumnModel::mimeData(const QModelIndexList &indexes) const {
|
|
QMimeData* mimeData = new QMimeData;
|
|
QByteArray encodedData;
|
|
|
|
QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
|
|
|
for (const QModelIndex &index : indexes) {
|
|
if (index.isValid()) {
|
|
QString text = data(index, Qt::DisplayRole).toString();
|
|
ColumnType type = ColumnType(data(index, 0x0101).toInt());
|
|
stream << text << type;
|
|
}
|
|
}
|
|
mimeData->setData(OutputColumnModel::MimeType, encodedData);
|
|
return mimeData;
|
|
}
|
|
|
|
bool OutputColumnModel::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(OutputColumnModel::MimeType);
|
|
QDataStream stream(&encodedData, QIODevice::ReadOnly);
|
|
std::vector<OutputColumn> newItems;
|
|
int rows = 0;
|
|
|
|
while (!stream.atEnd()) {
|
|
QString text;
|
|
ColumnType type;
|
|
stream >> text >> type;
|
|
newItems.push_back(OutputColumn(text, type));
|
|
++rows;
|
|
}
|
|
|
|
insertRows(row, rows, QModelIndex());
|
|
for (const OutputColumn &column : newItems) {
|
|
QModelIndex idx = index(row, 0, QModelIndex());
|
|
setData(idx, QVariant::fromValue(column), 0x102);
|
|
row++;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|