Skip to content

Commit

Permalink
Merge pull request #980 from FashionFreedom/issue-979-add-group-sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
csett86 committed Jun 20, 2023
2 parents 6319c3d + 6e5fa6b commit 49f53ef
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 43 deletions.
124 changes: 85 additions & 39 deletions src/app/seamly2d/dialogs/groups_widget.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/***************************************************************************
** @file groups_widget.cpp
** @author Douglas S Caskey
** @date Mar 1, 2023
** @date 11 Jun, 2023
**
** @copyright
** Copyright (C) 2017 - 2023 Seamly, LLC
Expand Down Expand Up @@ -73,6 +73,7 @@
#include "../vgeometry/vcubicbezierpath.h"
#include "../vgeometry/vpointf.h"
#include "../vpatterndb/vcontainer.h"
#include "../vwidgets/group_tablewidgetitem.h"
#include "../vwidgets/vabstractmainwindow.h"
#include "../vmisc/vabstractapplication.h"
#include "../vmisc/vsettings.h"
Expand Down Expand Up @@ -114,6 +115,7 @@ GroupsWidget::GroupsWidget(VContainer *data, VAbstractPattern *doc, QWidget *par
ui->groups_Splitter->restoreState(settings.value("splitterSizes").toByteArray());

fillTable(m_doc->getGroups());
ui->groups_TableWidget->sortItems(settings.value("groupSort", 4).toInt(), Qt::AscendingOrder);

connect(m_doc, &VAbstractPattern::patternHasGroups, this, &GroupsWidget::draftBlockHasGroups);

Expand All @@ -138,6 +140,7 @@ GroupsWidget::GroupsWidget(VContainer *data, VAbstractPattern *doc, QWidget *par
connect(ui->groupItems_ListWidget, &QListWidget::customContextMenuRequested, this, &GroupsWidget::groupItemContextMenu);
connect(ui->groupItems_ListWidget, &QListWidget::itemDoubleClicked, this, &GroupsWidget::itemDoubleClicked);

connect(ui->groups_TableWidget->horizontalHeader(), &QHeaderView::sectionClicked, this, &GroupsWidget::headerClicked);
connect(ui->groups_Splitter, &QSplitter::splitterMoved, this, &GroupsWidget::splitterMoved);
}

Expand Down Expand Up @@ -382,7 +385,7 @@ void GroupsWidget::editGroup()
ui->groups_TableWidget->blockSignals(false);
return;
}
QTableWidgetItem *item = ui->groups_TableWidget->item(row,1);

const quint32 groupId = ui->groups_TableWidget->item(row, 0)->data(Qt::UserRole).toUInt();
const bool locked = m_doc->getGroupLock(groupId);
QString oldGroupName = m_doc->getGroupName(groupId);
Expand Down Expand Up @@ -434,14 +437,22 @@ void GroupsWidget::editGroup()
}

const QString groupColor = dialog->getColor();
QPixmap pixmap = VAbstractTool::createColorIcon(32, 12, groupColor);

const QString groupLineType = dialog->getLineType();
const QString groupLineWeight = dialog->getLineWeight();

item = ui->groups_TableWidget->item(row,3);
QTableWidgetItem *item = ui->groups_TableWidget->item(row, 3);
item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
item->setSizeHint(QSize(20, 20));
QPixmap pixmap(20, 20);
pixmap.fill(QColor(groupColor));
item->setIcon(QIcon(pixmap));
item->setFlags(item->flags() &= ~(Qt::ItemIsEditable)); // set the item non-editable (view only), and non-selectable
item->setToolTip(tr("Group color"));
ui->groups_TableWidget->setItem(row, 3, item);

item = ui->groups_TableWidget->item(row, 4);
item->setText(groupName);

m_doc->setGroupName(groupId, groupName);
m_doc->setGroupColor(groupId, groupColor);
m_doc->setGroupLineType(groupId, groupLineType);
Expand All @@ -455,6 +466,9 @@ void GroupsWidget::editGroup()
//---------------------------------------------------------------------------------------------------------------------
void GroupsWidget::groupContextMenu(const QPoint &pos)
{
ui->groups_TableWidget->setSortingEnabled(false);
ui->groups_TableWidget->blockSignals(true);

QTableWidgetItem *item = ui->groups_TableWidget->itemAt(pos);
if (!item)
{
Expand Down Expand Up @@ -526,10 +540,19 @@ void GroupsWidget::groupContextMenu(const QPoint &pos)

const QString groupColor = dialog->getColor();
const QString groupLineType = dialog->getLineType();
const QString groupLineWeight = dialog->getLineWeight();
QPixmap pixmap = VAbstractTool::createColorIcon(32, 12, groupColor);
const QString groupLineWeight = dialog->getLineWeight();

item = ui->groups_TableWidget->item(row, 3);
item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
item->setSizeHint(QSize(20, 20));
QPixmap pixmap(20, 20);
pixmap.fill(QColor(groupColor));
item->setIcon(QIcon(pixmap));
item->setFlags(item->flags() &= ~(Qt::ItemIsEditable)); // set the item non-editable (view only), and non-selectable
item->setToolTip(tr("Group color"));
ui->groups_TableWidget->setItem(row, 3, item);

item = ui->groups_TableWidget->item(row, 4);
item->setText(groupName);
m_doc->setGroupName(groupId, groupName);
m_doc->setGroupColor(groupId, groupColor);
Expand All @@ -542,6 +565,10 @@ void GroupsWidget::groupContextMenu(const QPoint &pos)
connect(command, &DelGroup::updateGroups, this, &GroupsWidget::updateGroups);
qApp->getUndoStack()->push(command);
}

ui->groups_TableWidget->setSortingEnabled(true);
ui->groups_TableWidget->blockSignals(false);

}

//---------------------------------------------------------------------------------------------------------------------
Expand All @@ -561,8 +588,10 @@ void GroupsWidget::fillTable(const QMap<quint32, GroupAttributes> &groups)
{
ui->groups_TableWidget->blockSignals(true);
ui->groups_TableWidget->clear();
ui->groups_TableWidget->setColumnCount(4);
ui->groups_TableWidget->setColumnCount(5);
ui->groups_TableWidget->setRowCount(groups.size());
ui->groups_TableWidget->setSortingEnabled(false);

qint32 currentRow = -1;
auto i = groups.constBegin();
while (i != groups.constEnd())
Expand All @@ -571,8 +600,9 @@ void GroupsWidget::fillTable(const QMap<quint32, GroupAttributes> &groups)
const GroupAttributes data = i.value();

// Add visibility item
QTableWidgetItem *item = new QTableWidgetItem();
GroupTableWidgetItem *item = new GroupTableWidgetItem(m_doc);
item->setTextAlignment(Qt::AlignHCenter);
item->setSizeHint(QSize(20, 20));

setGroupVisibility(item, i.key(), data.visible);

Expand All @@ -582,49 +612,55 @@ void GroupsWidget::fillTable(const QMap<quint32, GroupAttributes> &groups)
ui->groups_TableWidget->setItem(currentRow, 0, item);

// Add locked item
item = new QTableWidgetItem();
item = new GroupTableWidgetItem(m_doc);
item->setTextAlignment(Qt::AlignHCenter);

if (data.locked)
{
item->setIcon(QIcon("://icon/32x32/lock_on.png"));
}
else
{
item->setIcon(QIcon("://icon/32x32/lock_off.png"));
}

item->setSizeHint(QSize(20, 20));
item->setIcon(data.locked ? QIcon("://icon/32x32/lock_on.png") : QIcon("://icon/32x32/lock_off.png"));
item->setData(Qt::UserRole, i.key());
item->setFlags(item->flags() &= ~(Qt::ItemIsEditable)); // set the item non-editable (view only), and non-selectable
item->setToolTip(tr("Show which groups in the list are locked"));
ui->groups_TableWidget->setItem(currentRow, 1, item);

// Add Edit Item
if (!m_doc->isGroupEmpty(i.key()))
{
item = new QTableWidgetItem();
item->setTextAlignment(Qt::AlignHCenter);
item->setIcon(QIcon("://icon/32x32/history.png"));
item->setData(Qt::UserRole, i.key());
item->setFlags(item->flags() &= ~(Qt::ItemIsEditable)); // set the item non-editable (view only), and non-selectable
item->setToolTip(tr("Show which groups contain objects"));
ui->groups_TableWidget->setItem(currentRow, 2, item);
}

// Add group name item
QPixmap pixmap = VAbstractTool::createColorIcon(32,12,data.color);
// Add contain objects Item
item = new GroupTableWidgetItem(m_doc);
item->setTextAlignment(Qt::AlignHCenter);
item->setIcon(!m_doc->isGroupEmpty(i.key()) ? QIcon("://icon/32x32/history.png") : QIcon());
item->setData(Qt::UserRole, i.key());
item->setFlags(item->flags() &= ~(Qt::ItemIsEditable)); // set the item non-editable (view only), and non-selectable
item->setToolTip(tr("Show which groups contain objects"));
ui->groups_TableWidget->setItem(currentRow, 2, item);

item = new QTableWidgetItem(data.name);
// Add color item
item = new GroupTableWidgetItem(m_doc);
item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
item->setSizeHint(QSize(20, 20));
QPixmap pixmap(20, 20);
pixmap.fill(QColor(data.color));
item->setIcon(QIcon(pixmap));
item->setToolTip(tr("Group color and name"));
item->setData(Qt::UserRole, data.color);
item->setFlags(item->flags() &= ~(Qt::ItemIsEditable)); // set the item non-editable (view only), and non-selectable
item->setToolTip(tr("Group color"));
ui->groups_TableWidget->setItem(currentRow, 3, item);

// Add group name item
QTableWidgetItem *nameItem = new QTableWidgetItem(data.name);
nameItem->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
nameItem->setToolTip(tr("Group name"));
ui->groups_TableWidget->setItem(currentRow, 4, nameItem);

++i;
}
ui->groups_TableWidget->sortItems(1, Qt::AscendingOrder);

ui->groups_TableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem(makeHeaderName(QString("Visible"))));
ui->groups_TableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem(makeHeaderName(QString("Locked"))));
ui->groups_TableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem(makeHeaderName(QString("Objects"))));
ui->groups_TableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem(makeHeaderName(QString("Color"))));
ui->groups_TableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem(tr("Name")));
ui->groups_TableWidget->horizontalHeaderItem(4)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui->groups_TableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->groups_TableWidget->resizeColumnsToContents();
ui->groups_TableWidget->resizeRowsToContents();
ui->groups_TableWidget->setSortingEnabled(true);
ui->groups_TableWidget->blockSignals(false);
}

Expand Down Expand Up @@ -675,7 +711,7 @@ QString GroupsWidget::getCurrentGroupName()
}
int row = ui->groups_TableWidget->row(item);
qCDebug(WidgetGroups, "Row = %d\n", row);
const QString groupName = ui->groups_TableWidget->item(row, 3)->text();
const QString groupName = ui->groups_TableWidget->item(row, 4)->text();
return groupName;
}

Expand Down Expand Up @@ -998,7 +1034,6 @@ void GroupsWidget::addGroupItem(const quint32 &toolId, const quint32 &objId, con
itemData.append(objId);
itemData.append(toolId);

qCDebug(WidgetGroups, "Icon Name = %s", qUtf8Printable(iconFileName));
QListWidgetItem *item = new QListWidgetItem(objName);
item->setIcon(QIcon(iconFileName));
item->setData(Qt::UserRole, QVariant::fromValue(itemData));
Expand Down Expand Up @@ -1327,3 +1362,14 @@ void GroupsWidget::splitterMoved(int pos, int index)
QSettings settings;
settings.setValue("splitterSizes", ui->groups_Splitter->saveState());
}

//---------------------------------------------------------------------------------------------------------------------
/**
* @brief headerClicked Sort state whenever header section clicked.
* @param index
*/
void GroupsWidget::headerClicked(int index)
{
QSettings settings;
settings.setValue("groupSort", index);
}
3 changes: 2 additions & 1 deletion src/app/seamly2d/dialogs/groups_widget.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/***************************************************************************
** @file groups_widget.h
** @author Douglas S Caskey
** @date Mar 1, 2023
** @date 11 Jun, 2023
**
** @copyright
** Copyright (C) 2017 - 2023 Seamly, LLC
Expand Down Expand Up @@ -118,6 +118,7 @@ private slots:
quint32 attrUInt(const QDomElement &domElement, const QString &name);
QString getObjName(quint32 id);
void splitterMoved(int pos, int index);
void headerClicked(int index);
};

#endif // GROUPS_WIDGET_H
14 changes: 11 additions & 3 deletions src/app/seamly2d/dialogs/groups_widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,19 @@
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="sortingEnabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="columnCount">
<number>5</number>
</property>
<attribute name="horizontalHeaderVisible">
<bool>false</bool>
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>16</number>
</attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>false</bool>
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
Expand All @@ -334,6 +337,11 @@
<attribute name="verticalHeaderHighlightSections">
<bool>false</bool>
</attribute>
<column/>
<column/>
<column/>
<column/>
<column/>
</widget>
</item>
</layout>
Expand Down
11 changes: 11 additions & 0 deletions src/libs/vmisc/def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ QString strippedName(const QString &fullFileName)
return QFileInfo(fullFileName).fileName();
}

/**
* @brief makeHeaderName make a 1 char tablewidgetitem header name based on a translated string.
* @param name full name of header item.
* @return 1 char name.
*/
QString makeHeaderName(const QString &name)
{
QString headerStr = QObject::tr("%1").arg(name);
return headerStr.left(1).toUpper();
}

//---------------------------------------------------------------------------------------------------------------------
QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath)
{
Expand Down
3 changes: 3 additions & 0 deletions src/libs/vmisc/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ Q_REQUIRED_RESULT QMarginsF UnitConvertor(const QMarginsF &margins, const Unit &
void InitLanguages(QComboBox *combobox);
Q_REQUIRED_RESULT QStringList SupportedLocales();

QString makeHeaderName(const QString &name);
Q_REQUIRED_RESULT QString strippedName(const QString &fullFileName);
Q_REQUIRED_RESULT QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath);
Q_REQUIRED_RESULT QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath);
Expand Down Expand Up @@ -645,4 +646,6 @@ inline QList<T> convertToList(const C<T> &set)
{
return QList<T>(set.begin(), set.end());
}


#endif // DEF_H
64 changes: 64 additions & 0 deletions src/libs/vwidgets/group_tablewidgetitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/***************************************************************************
** @file group_tablewidgetitem.cpp
** @author Douglas S Caskey
** @date 11 Jun, 2023
**
** @brief
** @copyright
** This source code is part of the Seamly2D project, a pattern making
** program to create and model patterns of clothing.
** Copyright (C) 2017-2023 Seamly2D project
** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
**
** Seamly2D 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.
**
** Seamly2D 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 Seamly2D. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/

#include "group_tablewidgetitem.h"

#include "../ifc/xml/vabstractpattern.h"

GroupTableWidgetItem::GroupTableWidgetItem(VAbstractPattern *doc)
: QTableWidgetItem()
, m_doc(doc)
{}

//---------------------------------------------------------------------------------------------------------------------
bool GroupTableWidgetItem::operator<(const QTableWidgetItem &other) const
{
if (other.column() == 0)
{
bool thisVisible = m_doc->getGroupVisibility(this->data(Qt::UserRole).toUInt());
bool otherVisible = m_doc->getGroupVisibility(other.data(Qt::UserRole).toUInt());
return int(thisVisible) < int(otherVisible);
}
else if (other.column() == 1)
{
bool thisLocked = m_doc->getGroupLock(this->data(Qt::UserRole).toUInt());
bool otherLocked = m_doc->getGroupLock(other.data(Qt::UserRole).toUInt());
return int(thisLocked) < int(otherLocked);
}
else if (other.column() == 2)
{
bool thisEmpty = m_doc->isGroupEmpty(this->data(Qt::UserRole).toUInt());
bool otherEmpty = m_doc->isGroupEmpty(other.data(Qt::UserRole).toUInt());
return int(!thisEmpty) < int(!otherEmpty);
}
else if (other.column() == 3)
{
return this->data(Qt::UserRole).toString() < other.data(Qt::UserRole).toString();
}

return false;
}

0 comments on commit 49f53ef

Please sign in to comment.