leocad/qt/lc_qeditgroupsdialog.cpp

168 lines
4.8 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_qeditgroupsdialog.h"
#include "ui_lc_qeditgroupsdialog.h"
#include "lc_application.h"
#include "project.h"
#include "lc_model.h"
2013-08-09 06:57:18 +02:00
#include "piece.h"
#include "group.h"
2013-08-16 03:25:51 +02:00
#include "lc_basewindow.h"
2013-08-09 06:57:18 +02:00
2016-08-01 05:44:15 +02:00
lcQEditGroupsDialog::lcQEditGroupsDialog(QWidget* Parent, const QMap<lcPiece*, lcGroup*>& PieceParents, const QMap<lcGroup*, lcGroup*>& GroupParents)
2016-08-11 00:29:59 +02:00
: QDialog(Parent), mPieceParents(PieceParents), mGroupParents(GroupParents)
2013-08-09 06:57:18 +02:00
{
2014-11-10 01:06:11 +01:00
mLastItemClicked = NULL;
2016-08-11 00:29:59 +02:00
ui = new Ui::lcQEditGroupsDialog;
2013-08-09 06:57:18 +02:00
ui->setupUi(this);
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, SLOT(onItemClicked(QTreeWidgetItem *,int)));
connect(ui->treeWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem *,int)), this, SLOT(onItemDoubleClicked(QTreeWidgetItem *,int)));
2013-08-09 06:57:18 +02:00
QPushButton *newGroup = ui->buttonBox->addButton(tr("New Group"), QDialogButtonBox::ActionRole);
connect(newGroup, SIGNAL(clicked()), this, SLOT(on_newGroup_clicked()));
2014-11-10 01:06:11 +01:00
AddChildren(ui->treeWidget->invisibleRootItem(), NULL);
ui->treeWidget->expandAll();
2013-08-09 06:57:18 +02:00
}
lcQEditGroupsDialog::~lcQEditGroupsDialog()
{
delete ui;
}
void lcQEditGroupsDialog::accept()
{
2014-11-10 01:06:11 +01:00
UpdateParents(ui->treeWidget->invisibleRootItem(), NULL);
2013-08-09 06:57:18 +02:00
QDialog::accept();
}
2014-11-10 01:06:11 +01:00
void lcQEditGroupsDialog::reject()
{
2016-08-01 05:44:15 +02:00
for (int GroupIdx = 0; GroupIdx < mNewGroups.size(); GroupIdx++)
lcGetActiveModel()->RemoveGroup(mNewGroups[GroupIdx]);
2014-11-10 01:06:11 +01:00
QDialog::reject();
}
2013-08-09 06:57:18 +02:00
void lcQEditGroupsDialog::on_newGroup_clicked()
{
2014-11-10 01:06:11 +01:00
QTreeWidgetItem* CurrentItem = ui->treeWidget->currentItem();
2013-08-09 06:57:18 +02:00
2014-11-10 01:06:11 +01:00
if (CurrentItem && CurrentItem->data(0, PieceRole).value<uintptr_t>())
CurrentItem = CurrentItem->parent();
2013-08-09 06:57:18 +02:00
2014-11-10 01:06:11 +01:00
if (!CurrentItem)
CurrentItem = ui->treeWidget->invisibleRootItem();
2013-08-09 06:57:18 +02:00
2014-11-10 01:06:11 +01:00
lcGroup* ParentGroup = (lcGroup*)CurrentItem->data(0, GroupRole).value<uintptr_t>();
2013-08-09 06:57:18 +02:00
2015-10-21 17:03:45 +02:00
lcGroup* NewGroup = lcGetActiveModel()->AddGroup(tr("Group #"), ParentGroup);
2016-08-01 05:44:15 +02:00
mGroupParents[NewGroup] = ParentGroup;
mNewGroups.append(NewGroup);
2014-11-10 01:06:11 +01:00
2015-10-21 17:03:45 +02:00
QTreeWidgetItem* GroupItem = new QTreeWidgetItem(CurrentItem, QStringList(NewGroup->mName));
2014-11-10 01:06:11 +01:00
GroupItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable);
GroupItem->setData(0, GroupRole, qVariantFromValue<uintptr_t>((uintptr_t)NewGroup));
2013-08-09 06:57:18 +02:00
}
void lcQEditGroupsDialog::onItemClicked(QTreeWidgetItem *item, int column)
{
2016-02-17 00:11:52 +01:00
Q_UNUSED(column);
if (item->flags() & Qt::ItemIsEditable)
{
2014-11-10 01:06:11 +01:00
mClickTimer.stop();
2014-11-10 01:06:11 +01:00
if (mLastItemClicked != item)
{
2014-11-10 01:06:11 +01:00
mLastItemClicked = item;
mEditableDoubleClicked = false;
}
else
{
2014-11-10 01:06:11 +01:00
mClickTimer.start(QApplication::doubleClickInterval() + 50, this);
}
}
}
void lcQEditGroupsDialog::onItemDoubleClicked(QTreeWidgetItem *item, int column)
{
2016-02-17 00:11:52 +01:00
Q_UNUSED(column);
if (item->flags() & Qt::ItemIsEditable)
{
2014-11-10 01:06:11 +01:00
mEditableDoubleClicked = true;
}
}
void lcQEditGroupsDialog::timerEvent(QTimerEvent *event)
{
2016-02-17 00:11:52 +01:00
Q_UNUSED(event);
2014-11-10 01:06:11 +01:00
mClickTimer.stop();
if (!mEditableDoubleClicked)
{
2014-11-10 01:06:11 +01:00
ui->treeWidget->editItem(mLastItemClicked);
}
2014-11-10 01:06:11 +01:00
mEditableDoubleClicked = false;
}
2014-11-10 01:06:11 +01:00
void lcQEditGroupsDialog::UpdateParents(QTreeWidgetItem* ParentItem, lcGroup* ParentGroup)
2013-08-09 06:57:18 +02:00
{
2014-11-10 01:06:11 +01:00
for (int ChildIdx = 0; ChildIdx < ParentItem->childCount(); ChildIdx++)
2013-08-09 06:57:18 +02:00
{
2014-11-10 01:06:11 +01:00
QTreeWidgetItem* ChildItem = ParentItem->child(ChildIdx);
2013-08-09 06:57:18 +02:00
2014-11-10 01:06:11 +01:00
lcPiece* Piece = (lcPiece*)ChildItem->data(0, PieceRole).value<uintptr_t>();
2013-08-09 06:57:18 +02:00
2014-11-10 01:06:11 +01:00
if (Piece)
2013-08-09 06:57:18 +02:00
{
2016-08-01 05:44:15 +02:00
mPieceParents[Piece] = ParentGroup;
2013-08-09 06:57:18 +02:00
}
else
{
2014-11-10 01:06:11 +01:00
lcGroup* Group = (lcGroup*)ChildItem->data(0, GroupRole).value<uintptr_t>();
2013-08-09 06:57:18 +02:00
2014-11-10 01:06:11 +01:00
// todo: validate unique group name
2015-10-21 17:03:45 +02:00
Group->mName = ChildItem->text(0);
2016-08-01 05:44:15 +02:00
mGroupParents[Group] = ParentGroup;
2014-05-25 03:45:19 +02:00
2014-11-10 01:06:11 +01:00
UpdateParents(ChildItem, Group);
2013-08-09 06:57:18 +02:00
}
}
}
2014-11-10 01:06:11 +01:00
void lcQEditGroupsDialog::AddChildren(QTreeWidgetItem* ParentItem, lcGroup* ParentGroup)
2013-08-09 06:57:18 +02:00
{
2016-08-01 05:44:15 +02:00
for (QMap<lcGroup*, lcGroup*>::const_iterator it = mGroupParents.constBegin(); it != mGroupParents.constEnd(); it++)
2013-08-09 06:57:18 +02:00
{
2014-11-10 01:06:11 +01:00
lcGroup* Group = it.key();
lcGroup* Parent = it.value();
2014-05-25 03:45:19 +02:00
2014-11-10 01:06:11 +01:00
if (Parent != ParentGroup)
2013-08-09 06:57:18 +02:00
continue;
2015-10-21 17:03:45 +02:00
QTreeWidgetItem* GroupItem = new QTreeWidgetItem(ParentItem, QStringList(Group->mName));
2014-11-10 01:06:11 +01:00
GroupItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable);
GroupItem->setData(0, GroupRole, qVariantFromValue<uintptr_t>((uintptr_t)Group));
2013-08-09 06:57:18 +02:00
2014-11-10 01:06:11 +01:00
AddChildren(GroupItem, Group);
2013-08-09 06:57:18 +02:00
}
2016-08-01 05:44:15 +02:00
for (QMap<lcPiece*, lcGroup*>::const_iterator it = mPieceParents.constBegin(); it != mPieceParents.constEnd(); it++)
2013-08-09 06:57:18 +02:00
{
2014-11-10 01:06:11 +01:00
lcPiece* Piece = it.key();
lcGroup* Parent = it.value();
2014-11-10 01:06:11 +01:00
if (Parent != ParentGroup)
2013-08-09 06:57:18 +02:00
continue;
2014-11-10 01:06:11 +01:00
QTreeWidgetItem* PieceItem = new QTreeWidgetItem(ParentItem, QStringList(Piece->GetName()));
PieceItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
PieceItem->setData(0, PieceRole, qVariantFromValue<uintptr_t>((uintptr_t)Piece));
2013-08-09 06:57:18 +02:00
}
}