leocad/qt/lc_qeditgroupsdialog.cpp

164 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
2014-11-10 01:06:11 +01:00
lcQEditGroupsDialog::lcQEditGroupsDialog(QWidget* Parent, void* Data)
: QDialog(Parent), ui(new Ui::lcQEditGroupsDialog)
2013-08-09 06:57:18 +02:00
{
2014-11-10 01:06:11 +01:00
mLastItemClicked = NULL;
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
mOptions = (lcEditGroupsDialogOptions*)Data;
2013-08-09 06:57:18 +02:00
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()
{
for (int GroupIdx = 0; GroupIdx < mOptions->NewGroups.size(); GroupIdx++)
lcGetActiveModel()->RemoveGroup(mOptions->NewGroups[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
lcGroup* NewGroup = lcGetActiveModel()->AddGroup("Group #", ParentGroup);
2014-11-10 01:06:11 +01:00
mOptions->GroupParents[NewGroup] = ParentGroup;
mOptions->NewGroups.append(NewGroup);
QTreeWidgetItem* GroupItem = new QTreeWidgetItem(CurrentItem, QStringList(NewGroup->m_strName));
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)
{
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)
{
if (item->flags() & Qt::ItemIsEditable)
{
2014-11-10 01:06:11 +01:00
mEditableDoubleClicked = true;
}
}
void lcQEditGroupsDialog::timerEvent(QTimerEvent *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
{
2014-11-10 01:06:11 +01:00
mOptions->PieceParents[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
strncpy(Group->m_strName, ChildItem->text(0).toLatin1(), sizeof(Group->m_strName)); // todo: qstring
Group->m_strName[sizeof(Group->m_strName) - 1] = 0;
2014-11-10 01:06:11 +01:00
mOptions->GroupParents[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
{
2014-11-10 01:06:11 +01:00
for (QMap<lcGroup*, lcGroup*>::const_iterator it = mOptions->GroupParents.constBegin(); it != mOptions->GroupParents.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;
2014-11-10 01:06:11 +01:00
QTreeWidgetItem* GroupItem = new QTreeWidgetItem(ParentItem, QStringList(Group->m_strName));
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
}
2014-11-10 01:06:11 +01:00
for (QMap<lcPiece*, lcGroup*>::const_iterator it = mOptions->PieceParents.constBegin(); it != mOptions->PieceParents.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
}
}