mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-25 23:54:22 +01:00
36b1cb08ff
A target typed in by hand was split into its fields on " / ", which broke as soon as a field itself contained a slash: an installation named "ASP/01" gave the box two lines with half a name in each, and nothing to tell a separator apart from the name. Only a line break now breaks the text into fields (RealTerminal::cableTarget()), and the Target cell shows the fields the very same way, one line each (modelRealTerminalData::targetText()), so the cell and the box under the cable always read the same text. The Target cell is edited line by line to go with that: it takes a multi line editor where Return writes a new line, Ctrl+Return hands the cell back to the table and Escape throws the text away (TerminalStripModelDelegate), and the rows of the table are as tall as the lines their cell shows, so the fields below the first one are never cut off -- a cell without a target keeps the row it has always had. The text copied to the clipboard keeps one line per row, the fields of such a cell joined with " / " again, or a spreadsheet would take them for several rows. tst_terminalcableauto reads the three fields of a target of the drawing back one line each, checks a "/" typed inside a field stays part of it -- one line holding it, slashes and all -- checks the editor of the cell is the multi line one, hands its text back untouched, gives the cell back on Ctrl+Return while Return keeps writing a new line, and checks the rows grow with the lines the cell holds and shrink back afterwards. tst_cabletarget paints a strip whose cable ends on a field containing a slash: the box is no deeper than with one plain field.
156 lines
4.7 KiB
C++
156 lines
4.7 KiB
C++
/*
|
|
Copyright 2006-2026 The QElectroTech Team
|
|
This file is part of QElectroTech.
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#ifndef TERMINALSTRIPMODEL_H
|
|
#define TERMINALSTRIPMODEL_H
|
|
|
|
#include <QAbstractTableModel>
|
|
#include <QObject>
|
|
#include <QPointer>
|
|
#include <QStyledItemDelegate>
|
|
#include <QHash>
|
|
#include <QColor>
|
|
|
|
#include "../terminalstrip.h"
|
|
#include "modelTerminalData.h"
|
|
|
|
//Code to use QColor as key for QHash
|
|
inline size_t qHash(const QColor &key, size_t seed = 0) {
|
|
return qHash(key.rgba(), seed);
|
|
}
|
|
|
|
//needed to use QPointer<Element> as key of QHash
|
|
inline uint qHash(const QPointer<Element> &key, uint seed) {
|
|
if (key)
|
|
return qHash(key->uuid(), seed);
|
|
else
|
|
return qHash(QUuid(), seed);
|
|
}
|
|
|
|
class TerminalStrip;
|
|
|
|
class TerminalStripModel : public QAbstractTableModel
|
|
{
|
|
public:
|
|
enum Column {
|
|
Pos = 0,
|
|
Level = 1,
|
|
Level0 = 2,
|
|
Level1 = 3,
|
|
Level2 = 4,
|
|
Level3 = 5,
|
|
Label = 6,
|
|
Conductor = 7,
|
|
XRef = 8,
|
|
Cable = 9,
|
|
CableWire = 10,
|
|
CableTarget = 11,
|
|
Type = 12,
|
|
Function = 13,
|
|
Led = 14,
|
|
Shield = 15,
|
|
Color = 16,
|
|
Section = 17,
|
|
Width = 18,
|
|
Separator = 19,
|
|
Invalid = 99
|
|
};
|
|
|
|
static int levelForColumn(TerminalStripModel::Column column);
|
|
static TerminalStripModel::Column columnTypeForIndex(const QModelIndex &index);
|
|
|
|
///Role of the Width column : the label of the terminal chosen in the catalog.
|
|
///setData() with this role takes a QVariantMap {"mm": width, "entry": label}
|
|
static constexpr int CatalogEntryRole = Qt::UserRole + 1;
|
|
|
|
Q_OBJECT
|
|
public:
|
|
TerminalStripModel(TerminalStrip *terminal_strip, QObject *parent = nullptr);
|
|
void setTerminalStrip(TerminalStrip *terminal_strip);
|
|
|
|
virtual int rowCount (const QModelIndex &parent = QModelIndex()) const override;
|
|
virtual int columnCount (const QModelIndex &parent = QModelIndex()) const override;
|
|
virtual QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
|
virtual bool setData (const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
|
virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
|
virtual Qt::ItemFlags flags (const QModelIndex &index) const override;
|
|
QVector<modelRealTerminalData> modifiedmodelRealTerminalData() const;
|
|
|
|
QVector<modelPhysicalTerminalData> modelPhysicalTerminalDataForIndex(QModelIndexList index_list) const;
|
|
QVector<modelRealTerminalData> modelRealTerminalDataForIndex(QModelIndexList index_list) const;
|
|
modelRealTerminalData modelRealTerminalDataForIndex(const QModelIndex &index) const;
|
|
|
|
void buildBridgePixmap(const QSize &pixmap_size);
|
|
|
|
void reload();
|
|
|
|
private:
|
|
void fillPhysicalTerminalData();
|
|
modelRealTerminalData dataAtRow(int row) const;
|
|
void replaceDataAtRow(modelRealTerminalData data, int row);
|
|
modelPhysicalTerminalData physicalDataAtIndex(int index) const;
|
|
modelRealTerminalData realDataAtIndex(int index) const;
|
|
QPixmap bridgePixmapFor(const QModelIndex &index) const;
|
|
|
|
private:
|
|
QPointer<TerminalStrip> m_terminal_strip;
|
|
//Modified cells of each real terminal (spare terminals have no element)
|
|
QHash<const RealTerminal *, QVector<bool>> m_modified_cell;
|
|
QVector<modelPhysicalTerminalData> m_physical_data;
|
|
struct BridgePixmap
|
|
{
|
|
QPixmap top_,
|
|
middle_,
|
|
bottom_,
|
|
none_;
|
|
};
|
|
|
|
QHash<QColor, BridgePixmap> m_bridges_pixmaps;
|
|
|
|
};
|
|
|
|
class TerminalStripModelDelegate : public QStyledItemDelegate
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
TerminalStripModelDelegate(QObject *parent = Q_NULLPTR);
|
|
|
|
QWidget *createEditor(
|
|
QWidget *parent,
|
|
const QStyleOptionViewItem &option,
|
|
const QModelIndex &index) const override;
|
|
void setEditorData(
|
|
QWidget *editor,
|
|
const QModelIndex &index) const override;
|
|
void setModelData(
|
|
QWidget *editor,
|
|
QAbstractItemModel *model,
|
|
const QModelIndex &index) const override;
|
|
bool eventFilter(QObject *object, QEvent *event) override;
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
|
|
|
void setManufacturer(const QString &manufacturer);
|
|
|
|
private:
|
|
///Manufacturer of the terminals proposed in the Width column, empty = all
|
|
QString m_manufacturer;
|
|
};
|
|
|
|
#endif // TERMINALSTRIPMODEL_H
|