File size: 6,478 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2023 David Carter <dcarter@david.carter.ca> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include <QMenu>
#include <QMessageBox>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/MainWindow.h>
#include <Gui/Tools.h>
#include <Mod/Material/App/Exceptions.h>
#include <Mod/Material/App/Materials.h>
#include "Array2D.h"
#include "ArrayDelegate.h"
#include "ArrayModel.h"
#include "ui_Array2D.h"
using namespace MatGui;
/* TRANSLATOR MatGui::Array2D */
Array2D::Array2D(const QString& propertyName,
const std::shared_ptr<Materials::Material>& material,
QWidget* parent)
: QDialog(parent)
, ui(new Ui_Array2D)
, _material(material)
{
ui->setupUi(this);
if (material->hasPhysicalProperty(propertyName)) {
_property = material->getPhysicalProperty(propertyName);
}
else if (material->hasAppearanceProperty(propertyName)) {
_property = material->getAppearanceProperty(propertyName);
}
else {
Base::Console().log("Property '%s' not found\n", propertyName.toStdString().c_str());
_property = nullptr;
}
if (_property) {
_value =
std::static_pointer_cast<Materials::Array2D>(_property->getMaterialValue());
setWindowTitle(_property->getDisplayName());
}
else {
_value = nullptr;
}
setupArray();
ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->tableView, &QWidget::customContextMenuRequested, this, &Array2D::onContextMenu);
_deleteAction.setText(tr("Delete Row"));
_deleteAction.setShortcut(Gui::QtTools::deleteKeySequence());
connect(&_deleteAction, &QAction::triggered, this, &Array2D::onDelete);
ui->tableView->addAction(&_deleteAction);
connect(ui->standardButtons, &QDialogButtonBox::accepted, this, &Array2D::accept);
connect(ui->standardButtons, &QDialogButtonBox::rejected, this, &Array2D::reject);
}
void Array2D::setColumnWidths(QTableView* table)
{
int length = _property->columns();
for (int i = 0; i < length; i++) {
table->setColumnWidth(i, 100);
}
}
void Array2D::setColumnDelegates(QTableView* table)
{
int length = _property->columns();
for (int i = 0; i < length; i++) {
const Materials::MaterialProperty& column = _property->getColumn(i);
table->setItemDelegateForColumn(
i,
new ArrayDelegate(column.getType(), column.getUnits(), this));
}
}
void Array2D::setupArray()
{
if (_property == nullptr) {
return;
}
auto table = ui->tableView;
auto model = new Array2DModel(_property, _value, this);
table->setModel(model);
// table->setEditTriggers(QAbstractItemView::AllEditTriggers);
table->setSelectionMode(QAbstractItemView::SingleSelection);
setColumnWidths(table);
setColumnDelegates(table);
connect(model, &QAbstractItemModel::dataChanged, this, &Array2D::onDataChanged);
}
void Array2D::onDataChanged(const QModelIndex& topLeft,
const QModelIndex& bottomRight,
const QVector<int>& roles)
{
Q_UNUSED(topLeft)
Q_UNUSED(bottomRight)
Q_UNUSED(roles)
_material->setEditStateAlter();
}
void Array2D::onContextMenu(const QPoint& pos)
{
QMenu contextMenu(tr("Context Menu"), this);
contextMenu.addAction(&_deleteAction);
contextMenu.exec(ui->tableView->mapToGlobal(pos));
}
bool Array2D::newRow(const QModelIndex& index)
{
auto model = static_cast<Array2DModel*>(ui->tableView->model());
return model->newRow(index);
}
void Array2D::onDelete(bool checked)
{
Q_UNUSED(checked)
QItemSelectionModel* selectionModel = ui->tableView->selectionModel();
if (!selectionModel->hasSelection() || newRow(selectionModel->currentIndex())) {
return;
}
int res = confirmDelete();
if (res == QMessageBox::Cancel) {
return;
}
}
int Array2D::confirmDelete()
{
QMessageBox box(this);
box.setIcon(QMessageBox::Question);
box.setWindowTitle(QObject::tr("Confirm Delete"));
QString prompt = QObject::tr("Delete the row?");
box.setText(prompt);
box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
box.setDefaultButton(QMessageBox::Cancel);
box.setEscapeButton(QMessageBox::Cancel);
int res = QMessageBox::Cancel;
box.adjustSize(); // Silence warnings from Qt on Windows
switch (box.exec()) {
case QMessageBox::Ok:
deleteSelected();
res = QMessageBox::Ok;
break;
}
return res;
}
void Array2D::deleteSelected()
{
auto model = static_cast<Array2DModel*>(ui->tableView->model());
QItemSelectionModel* selectionModel = ui->tableView->selectionModel();
auto index = selectionModel->currentIndex();
model->deleteRow(index);
}
void Array2D::accept()
{
QDialog::accept();
}
void Array2D::reject()
{
QDialog::reject();
}
#include "moc_Array2D.cpp"
|