full rework of the output order
This commit is contained in:
@@ -8,8 +8,6 @@
|
||||
|
||||
CheckListViewWidget::CheckListViewWidget(QWidget *parent, Check check) : QWidget(parent), check(check) {
|
||||
mw = (MainWindow*) parent;
|
||||
|
||||
std::cout << "I was created with check with date " << check.get_date() << std::endl;
|
||||
QHBoxLayout *layout = new QHBoxLayout;
|
||||
|
||||
QLabel *date_label = new QLabel(QString::fromStdString(check.get_date()));
|
||||
|
||||
17
widgets/outputcolumn.cpp
Normal file
17
widgets/outputcolumn.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "outputcolumn.h"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
|
||||
OutputColumn::OutputColumn() { }
|
||||
|
||||
OutputColumn::OutputColumn(QString text, ColumnType type)
|
||||
: text(text), type(type) { }
|
||||
|
||||
void OutputColumn::set_column_type(ColumnType type) { this->type = type; }
|
||||
ColumnType OutputColumn::get_column_type() { return type; }
|
||||
|
||||
void OutputColumn::set_text(const QString &text) { this->text = text; }
|
||||
QString OutputColumn::get_text() { return text; }
|
||||
|
||||
|
||||
Q_DECLARE_METATYPE(OutputColumn)
|
||||
23
widgets/outputcolumn.h
Normal file
23
widgets/outputcolumn.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef OUTPUTCOLUMN_H
|
||||
#define OUTPUTCOLUMN_H
|
||||
|
||||
#include "output/output_options.h"
|
||||
#include <QLineEdit>
|
||||
#include <QStandardItem>
|
||||
|
||||
class OutputColumn
|
||||
{
|
||||
QString text;
|
||||
ColumnType type;
|
||||
public:
|
||||
OutputColumn();
|
||||
OutputColumn(QString text, ColumnType type);
|
||||
|
||||
void set_column_type(ColumnType type);
|
||||
ColumnType get_column_type();
|
||||
|
||||
void set_text(const QString &text);
|
||||
QString get_text();
|
||||
};
|
||||
|
||||
#endif // OUTPUTCOLUMN_H
|
||||
151
widgets/outputcolumnmodel.cpp
Normal file
151
widgets/outputcolumnmodel.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
#include "outputcolumnmodel.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <outputcolumn.h>
|
||||
#include <QJsonObject>
|
||||
#include <QMimeData>
|
||||
#include <QIODevice>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
37
widgets/outputcolumnmodel.h
Normal file
37
widgets/outputcolumnmodel.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef OUTPUTCOLUMNMODEL_H
|
||||
#define OUTPUTCOLUMNMODEL_H
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QAbstractListModel>
|
||||
#include <outputcolumn.h>
|
||||
|
||||
class OutputColumnModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
static constexpr const char* MimeType = "application/output.column.model";
|
||||
std::vector<OutputColumn> *columns;
|
||||
|
||||
public:
|
||||
OutputColumnModel(std::vector<OutputColumn> *columns, QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation,
|
||||
int role = Qt::DisplayRole) const override;
|
||||
|
||||
Qt::ItemFlags flags (const QModelIndex& index) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value,
|
||||
int role = Qt::EditRole) override;
|
||||
|
||||
Qt::DropActions supportedDropActions() const override;
|
||||
|
||||
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 *, 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);
|
||||
};
|
||||
|
||||
#endif // OUTPUTCOLUMNMODEL_H
|
||||
Reference in New Issue
Block a user