Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon editors: Support copying and moving rows #489

Merged
merged 4 commits into from Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/librepcb/common/common.pro
Expand Up @@ -77,6 +77,7 @@ SOURCES += \
geometry/cmd/cmdtextedit.cpp \
geometry/hole.cpp \
geometry/path.cpp \
geometry/pathmodel.cpp \
geometry/polygon.cpp \
geometry/stroketext.cpp \
geometry/text.cpp \
Expand Down Expand Up @@ -195,6 +196,7 @@ HEADERS += \
geometry/cmd/cmdtextedit.h \
geometry/hole.h \
geometry/path.h \
geometry/pathmodel.h \
geometry/polygon.h \
geometry/stroketext.h \
geometry/text.h \
Expand Down
2 changes: 1 addition & 1 deletion libs/librepcb/common/dialogs/polygonpropertiesdialog.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>270</width>
<width>400</width>
<height>394</height>
</rect>
</property>
Expand Down
283 changes: 283 additions & 0 deletions libs/librepcb/common/geometry/pathmodel.cpp
@@ -0,0 +1,283 @@
/*
* LibrePCB - Professional EDA for everyone!
* Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
* https://librepcb.org/
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*******************************************************************************
* Includes
******************************************************************************/
#include "pathmodel.h"

#include <QtCore>

/*******************************************************************************
* Namespace
******************************************************************************/
namespace librepcb {

/*******************************************************************************
* Constructors / Destructor
******************************************************************************/

PathModel::PathModel(QObject* parent) noexcept
: QAbstractTableModel(parent), mPath(), mNewVertex() {
}

PathModel::~PathModel() noexcept {
}

/*******************************************************************************
* Setters
******************************************************************************/

void PathModel::setPath(const Path& path) noexcept {
emit beginResetModel();
mPath = path;
emit endResetModel();
}

/*******************************************************************************
* Slots
******************************************************************************/

void PathModel::addItem(const QVariant& editData) noexcept {
Q_UNUSED(editData);
beginInsertRows(QModelIndex(), mPath.getVertices().count(),
mPath.getVertices().count());
mPath.addVertex(mNewVertex);
endInsertRows();
}

void PathModel::copyItem(const QVariant& editData) noexcept {
int index = editData.toInt();
if ((index >= 0) && (index < mPath.getVertices().count())) {
beginInsertRows(QModelIndex(), index, index);
mPath.insertVertex(index, mPath.getVertices().value(index));
endInsertRows();
} else {
qWarning() << "Invalid index in PathModel::copyItem()";
}
}

void PathModel::removeItem(const QVariant& editData) noexcept {
int index = editData.toInt();
if ((index >= 0) && (index < mPath.getVertices().count())) {
beginRemoveRows(QModelIndex(), index, index);
mPath.getVertices().remove(index);
endRemoveRows();
} else {
qWarning() << "Invalid index in PathModel::removeItem()";
}
}

void PathModel::moveItemUp(const QVariant& editData) noexcept {
int index = editData.toInt();
if ((index >= 1) && (index < mPath.getVertices().count())) {
beginMoveRows(QModelIndex(), index, index, QModelIndex(), index - 1);
mPath.getVertices().insert(index - 1, mPath.getVertices().takeAt(index));
endMoveRows();
}
}

void PathModel::moveItemDown(const QVariant& editData) noexcept {
int index = editData.toInt();
if ((index >= 0) && (index < mPath.getVertices().count() - 1)) {
// Note: destination index "+2" looks strange, but is correct. See Qt docs
// for explanation.
beginMoveRows(QModelIndex(), index, index, QModelIndex(), index + 2);
mPath.getVertices().insert(index + 1, mPath.getVertices().takeAt(index));
endMoveRows();
}
}

/*******************************************************************************
* Inherited from QAbstractItemModel
******************************************************************************/

int PathModel::rowCount(const QModelIndex& parent) const {
if (!parent.isValid()) {
return mPath.getVertices().count() + 1;
}
return 0;
}

int PathModel::columnCount(const QModelIndex& parent) const {
if (!parent.isValid()) {
return _COLUMN_COUNT;
}
return 0;
}

QVariant PathModel::data(const QModelIndex& index, int role) const {
if (!index.isValid()) {
return QVariant();
}

switch (index.column()) {
case COLUMN_X: {
Length val =
mPath.getVertices().value(index.row(), mNewVertex).getPos().getX();
switch (role) {
case Qt::DisplayRole:
return val.toMmString() + " mm";
case Qt::EditRole:
return val.toMm();
default:
return QVariant();
}
}
case COLUMN_Y: {
Length val =
mPath.getVertices().value(index.row(), mNewVertex).getPos().getY();
switch (role) {
case Qt::DisplayRole:
return val.toMmString() + " mm";
case Qt::EditRole:
return val.toMm();
default:
return QVariant();
}
}
case COLUMN_ANGLE: {
Angle val = mPath.getVertices().value(index.row(), mNewVertex).getAngle();
switch (role) {
case Qt::DisplayRole:
return val.toDegString() + "°";
case Qt::EditRole:
return val.toDeg();
default:
return QVariant();
}
}
case COLUMN_ACTIONS: {
switch (role) {
case Qt::EditRole:
return index.row();
default:
return QVariant();
}
}
default:
return QVariant();
}

return QVariant();
}

QVariant PathModel::headerData(int section, Qt::Orientation orientation,
int role) const {
if (orientation == Qt::Horizontal) {
if (role == Qt::DisplayRole) {
switch (section) {
case COLUMN_X:
return tr("Pos. X");
case COLUMN_Y:
return tr("Pos. Y");
case COLUMN_ANGLE:
return tr("Angle");
case COLUMN_ACTIONS:
return tr("Actions");
default:
return QVariant();
}
}
} else if (orientation == Qt::Vertical) {
if (role == Qt::DisplayRole) {
if (section < mPath.getVertices().count()) {
return QString::number(section + 1);
} else {
return tr("New:");
}
} else if ((role == Qt::ToolTipRole) &&
(section == mPath.getVertices().count())) {
return tr("Add a new vertex");
} else if (role == Qt::TextAlignmentRole) {
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
} else if (role == Qt::FontRole) {
// Actually we don't show UUIDs in the vertical header, thus monospace
// font is not needed. However, it seems that the table rows are less
// high if the font is set to monospace, so the tables are more compact.
QFont f = QAbstractTableModel::headerData(section, orientation, role)
.value<QFont>();
f.setStyleHint(QFont::Monospace);
f.setFamily("Monospace");
return f;
}
}
return QVariant();
}

Qt::ItemFlags PathModel::flags(const QModelIndex& index) const {
Qt::ItemFlags f = QAbstractTableModel::flags(index);
if (index.isValid() && (index.column() != COLUMN_ACTIONS)) {
f |= Qt::ItemIsEditable;
}
return f;
}

bool PathModel::setData(const QModelIndex& index, const QVariant& value,
int role) {
try {
Vertex* vertex = nullptr;
if ((index.row() >= 0) && (index.row() < mPath.getVertices().count())) {
vertex = &mPath.getVertices()[index.row()];
}
if ((index.column() == COLUMN_X) && role == Qt::EditRole) {
QString valueStr = value.toString().trimmed();
if (vertex) {
Point pos = vertex->getPos();
pos.setX(Length::fromMm(valueStr));
vertex->setPos(pos);
} else {
Point pos = mNewVertex.getPos();
pos.setX(Length::fromMm(valueStr));
mNewVertex.setPos(pos);
}
} else if ((index.column() == COLUMN_Y) && role == Qt::EditRole) {
QString valueStr = value.toString().trimmed();
if (vertex) {
Point pos = vertex->getPos();
pos.setY(Length::fromMm(valueStr));
vertex->setPos(pos);
} else {
Point pos = mNewVertex.getPos();
pos.setY(Length::fromMm(valueStr));
mNewVertex.setPos(pos);
}
} else if ((index.column() == COLUMN_ANGLE) && role == Qt::EditRole) {
QString valueStr = value.toString().trimmed();
if (vertex) {
vertex->setAngle(Angle::fromDeg(valueStr));
} else {
mNewVertex.setAngle(Angle::fromDeg(valueStr));
}
} else {
return false;
}
emit dataChanged(index, index);
return true;
} catch (const Exception& e) {
QMessageBox::critical(0, tr("Error"), e.getMsg());
}
return false;
}

/*******************************************************************************
* End of File
******************************************************************************/

} // namespace librepcb