54 #include "ui_pieces_widget.h"
55 #include "../ifc/xml/vabstractpattern.h"
56 #include "../vpatterndb/vcontainer.h"
57 #include "../vmisc/vabstractapplication.h"
58 #include "../vtools/tools/pattern_piece_tool.h"
59 #include "../vtools/undocommands/togglepieceinlayout.h"
60 #include "../vtools/undocommands/toggle_piecelock.h"
61 #include "../vtools/undocommands/set_piece_color.h"
62 #include "../vwidgets/piece_tablewidgetitem.h"
63 #include "../vwidgets/vmaingraphicsscene.h"
65 #include <QColorDialog>
68 #include <QTableWidget>
80 ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
98 ui->tableWidget->setSortingEnabled(
false);
103 if (pieces->contains(
id))
105 for (
int row = 0; row <
ui->tableWidget->rowCount(); ++row)
107 QTableWidgetItem *item =
ui->tableWidget->item(row, 0);
110 if (item && item->data(Qt::UserRole).toUInt() ==
id)
113 VPiece piece = pieces->value(
id);
115 inLayout ? item->setIcon(QIcon(
"://icon/32x32/checkmark.png")) : item->setIcon(QIcon());
117 QTableWidgetItem *lockItem =
ui->tableWidget->item(row, 1);
118 const bool locked = piece.
isLocked();
119 locked ? lockItem->setIcon(QIcon(
"://icon/32x32/lock_on.png"))
120 : lockItem->setIcon(QIcon(
"://icon/32x32/lock_off.png"));
122 QTableWidgetItem *colorItem =
ui->tableWidget->item(row, 2);
123 QPixmap pixmap(20, 20);
124 pixmap.fill(QColor(piece.
getColor()));
125 colorItem->setIcon(QIcon(pixmap));
127 QTableWidgetItem *nameItem =
ui->tableWidget->item(row, 3);
128 nameItem->setText(piece.
GetName());
133 ui->tableWidget->selectRow(selectedRow);
134 ui->tableWidget->setSortingEnabled(
true);
147 const int rowCount =
ui->tableWidget->rowCount();
148 for (
int row = 0; row < rowCount; ++row)
150 QTableWidgetItem *item =
ui->tableWidget->item(row, 0);
152 if (item->data(Qt::UserRole).toUInt() ==
id)
154 ui->tableWidget->setCurrentItem(item);
163 QTableWidgetItem *item =
ui->tableWidget->item(row, 0);
166 const quint32
id = item->data(Qt::UserRole).toUInt();
171 const bool inLayout = !allPieces->value(
id).isInLayout();
175 qApp->getUndoStack()->push(command);
177 else if (column == 1)
179 const bool lock = !allPieces->value(
id).isLocked();
183 qApp->getUndoStack()->push(command);
189 else if (column == 3)
193 ui->tableWidget->clearSelection();
199 QTableWidgetItem *item =
ui->tableWidget->item(row, 0);
202 const quint32
id = item->data(Qt::UserRole).toUInt();
204 if (allPieces->value(
id).isLocked())
206 QApplication::beep();
207 ui->tableWidget->clearSelection();
213 const QColor color = QColorDialog::getColor(Qt::white,
this,
"Select Color", QColorDialog::DontUseNativeDialog);
219 qApp->getUndoStack()->push(command);
223 else if (column == 3)
229 ui->tableWidget->clearSelection();
235 ui->tableWidget->blockSignals(
true);
237 const int selectedRow =
ui->tableWidget->currentRow();
238 ui->tableWidget->clearContents();
239 ui->tableWidget->setColumnCount(4);
240 ui->tableWidget->setRowCount(pieces->size());
241 ui->tableWidget->setSortingEnabled(
false);
243 qint32 currentRow = -1;
244 auto i = pieces->constBegin();
245 while (i != pieces->constEnd())
248 const VPiece piece = i.value();
252 item->setTextAlignment(Qt::AlignHCenter);
253 item->setSizeHint(QSize(20, 20));
254 item->setIcon(piece.
isInLayout() ? QIcon(
"://icon/32x32/checkmark.png") : QIcon());
255 item->setData(Qt::UserRole, i.key());
256 item->setFlags(item->flags() &= ~(Qt::ItemIsEditable));
257 item->setToolTip(tr(
"Toggle inclusion of pattern piece in layout"));
259 ui->tableWidget->setItem(currentRow, 0, item);
263 item->setTextAlignment(Qt::AlignHCenter);
264 item->setSizeHint(QSize(20, 20));
265 item->setIcon(piece.
isLocked() ? QIcon(
"://icon/32x32/lock_on.png") : QIcon(
"://icon/32x32/lock_off.png"));
266 item->setData(Qt::UserRole, i.key());
267 item->setFlags(item->flags() &= ~(Qt::ItemIsEditable));
268 item->setToolTip(
"Toggle lock on pattern piece");
269 ui->tableWidget->setItem(currentRow, 1, item);
273 item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
274 item->setSizeHint(QSize(20, 20));
275 QPixmap pixmap(20, 20);
276 pixmap.fill(QColor(piece.
getColor()));
277 item->setIcon(QIcon(pixmap));
278 item->setFlags(item->flags() &= ~(Qt::ItemIsEditable));
279 item->setToolTip(
"Double click opens color selector");
280 ui->tableWidget->setItem(currentRow, 2, item);
283 QString name = piece.
GetName();
286 name = tr(
"Unnamed");
288 QTableWidgetItem *nameItem =
new QTableWidgetItem(name);
289 nameItem->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
290 nameItem->setFlags(nameItem->flags() &= ~(Qt::ItemIsEditable));
291 nameItem->setToolTip(
"Double click opens pattern piece properties dialog");
292 ui->tableWidget->setItem(currentRow, 3, nameItem);
297 ui->tableWidget->setHorizontalHeaderItem(0,
new QTableWidgetItem(
"I"));
298 ui->tableWidget->setHorizontalHeaderItem(1,
new QTableWidgetItem(
"L"));
299 ui->tableWidget->setHorizontalHeaderItem(2,
new QTableWidgetItem(
"C"));
300 ui->tableWidget->setHorizontalHeaderItem(3,
new QTableWidgetItem(tr(
"Name")));
301 ui->tableWidget->horizontalHeaderItem(3)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
302 ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
303 ui->tableWidget->resizeColumnsToContents();
304 ui->tableWidget->resizeRowsToContents();
305 ui->tableWidget->setCurrentCell(selectedRow, 0);
306 ui->tableWidget->setSortingEnabled(
true);
307 ui->tableWidget->blockSignals(
false);
314 if (allPieces->count() == 0)
319 for (
int row = 0; row<
ui->tableWidget->rowCount(); ++row)
321 QTableWidgetItem *item =
ui->tableWidget->item(row, 0);
322 const quint32
id = item->data(Qt::UserRole).toUInt();
323 if (allPieces->contains(
id))
325 if (!(inLayout == allPieces->value(
id).isInLayout()))
329 qApp->getUndoStack()->push(command);
338 ui->tableWidget->blockSignals(
true);
339 ui->tableWidget->horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder);
341 if (allPieces->count() == 0)
346 for (
int row = 0; row<
ui->tableWidget->rowCount(); ++row)
348 QTableWidgetItem *item =
ui->tableWidget->item(row, 1);
349 const quint32
id = item->data(Qt::UserRole).toUInt();
350 if (allPieces->contains(
id))
352 if (!(lock == allPieces->value(
id).isLocked()))
356 qApp->getUndoStack()->push(command);
364 ui->tableWidget->blockSignals(
false);
370 ui->tableWidget->horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder);
371 ui->tableWidget->setSortingEnabled(
false);
372 ui->tableWidget->blockSignals(
true);
375 QScopedPointer<QMenu> menu(
new QMenu(
ui->tableWidget));
376 QAction *selectAll = menu->addAction(tr(
"Include all pieces"));
377 QAction *selectNone = menu->addAction(tr(
"Exclude all pieces"));
378 QAction *invertSelection = menu->addAction(tr(
"Invert included pieces"));
380 QAction *separator =
new QAction(
this);
381 separator->setSeparator(
true);
382 menu->addAction(separator);
384 QAction *lockAll = menu->addAction(tr(
"Lock all pieces"));
385 QAction *unlockAll = menu->addAction(tr(
"Unlock all pieces"));
386 QAction *invertLocked = menu->addAction(tr(
"Invert locked pieces"));
389 if (allPieces->count() == 0)
394 int selectedPieces = 0;
395 int lockedPieces = 0;
397 auto piece = allPieces->constBegin();
398 while (piece != allPieces->constEnd())
400 if(piece.value().isInLayout())
404 if(piece.value().isLocked())
411 if (selectedPieces == 0)
413 selectNone->setDisabled(
true);
415 else if (selectedPieces == allPieces->size())
417 selectAll->setDisabled(
true);
420 if (lockedPieces == 0)
422 unlockAll->setDisabled(
true);
424 else if (lockedPieces == allPieces->size())
426 lockAll->setDisabled(
true);
429 QAction *selectedAction = menu->exec(
ui->tableWidget->viewport()->mapToGlobal(pos));
431 if (selectedAction == selectAll)
433 qApp->getUndoStack()->beginMacro(tr(
"Include all pieces"));
435 qApp->getUndoStack()->endMacro();
437 else if (selectedAction == selectNone)
439 qApp->getUndoStack()->beginMacro(tr(
"Exclude all pieces"));
441 qApp->getUndoStack()->endMacro();
443 else if (selectedAction == invertSelection)
445 qApp->getUndoStack()->beginMacro(tr(
"Invert included pieces"));
447 for (
int row = 0; row <
ui->tableWidget->rowCount(); ++row)
449 QTableWidgetItem *item =
ui->tableWidget->item(row, 0);
450 const quint32
id = item->data(Qt::UserRole).toUInt();
451 if (allPieces->contains(
id))
453 const bool inLayout = !allPieces->value(
id).isInLayout();
457 qApp->getUndoStack()->push(command);
461 qApp->getUndoStack()->endMacro();
464 else if (selectedAction == lockAll)
466 qApp->getUndoStack()->beginMacro(tr(
"Lock all pieces"));
468 qApp->getUndoStack()->endMacro();
470 else if (selectedAction == unlockAll)
472 qApp->getUndoStack()->beginMacro(tr(
"Unlock all pieces"));
474 qApp->getUndoStack()->endMacro();
476 else if (selectedAction == invertLocked)
478 qApp->getUndoStack()->beginMacro(tr(
"Invert locked pieces"));
480 for (
int row = 0; row <
ui->tableWidget->rowCount(); ++row)
482 QTableWidgetItem *item =
ui->tableWidget->item(row, 1);
483 const quint32
id = item->data(Qt::UserRole).toUInt();
484 if (allPieces->contains(
id))
486 const bool lock = !allPieces->value(
id).isLocked();
490 qApp->getUndoStack()->push(command);
498 qApp->getUndoStack()->endMacro();
500 ui->tableWidget->setSortingEnabled(
true);
501 ui->tableWidget->blockSignals(
false);
void updateList(quint32 m_id)
void updateList(quint32 m_id)
void updateList(quint32 m_id)
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
The VContainer class container of all variables.
const QHash< quint32, VPiece > * DataPieces() const
The VMainGraphicsScene class main scene.
void pieceLockedChanged(quint32 id, bool lock)