From 75bc3cfaf862e46e04a7e09caba5482a0fc2fb4b Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sun, 15 Dec 2019 19:01:55 -0800 Subject: [PATCH] Added Palette Edit Dialog. --- common/lc_partpalettedialog.cpp | 151 ++++++++++++++++++++++++++++++ common/lc_partpalettedialog.h | 31 ++++++ common/lc_partpalettedialog.ui | 131 ++++++++++++++++++++++++++ common/lc_partselectionwidget.cpp | 51 ++++++++-- common/lc_partselectionwidget.h | 5 +- leocad.pro | 9 +- 6 files changed, 363 insertions(+), 15 deletions(-) create mode 100644 common/lc_partpalettedialog.cpp create mode 100644 common/lc_partpalettedialog.h create mode 100644 common/lc_partpalettedialog.ui diff --git a/common/lc_partpalettedialog.cpp b/common/lc_partpalettedialog.cpp new file mode 100644 index 00000000..283a5686 --- /dev/null +++ b/common/lc_partpalettedialog.cpp @@ -0,0 +1,151 @@ +#include "lc_global.h" +#include "lc_partpalettedialog.h" +#include "ui_lc_partpalettedialog.h" +#include "lc_partselectionwidget.h" + +lcPartPaletteDialog::lcPartPaletteDialog(QWidget* Parent, std::vector& PartPalettes) + : QDialog(Parent), ui(new Ui::lcPartPaletteDialog), mPartPalettes(PartPalettes) +{ + ui->setupUi(this); + + for (const lcPartPalette& Palette : PartPalettes) + { + QListWidgetItem* Item = new QListWidgetItem(Palette.Name); + Item->setData(Qt::UserRole, qVariantFromValue(reinterpret_cast(&Palette))); + ui->PaletteList->addItem(Item); + } + ui->PaletteList->setCurrentRow(0); + UpdateButtons(); +} + +lcPartPaletteDialog::~lcPartPaletteDialog() +{ + delete ui; +} + +void lcPartPaletteDialog::UpdateButtons() +{ + int CurrentPalette = ui->PaletteList->currentRow(); + int PaletteCount = ui->PaletteList->count(); + + ui->DeleteButton->setEnabled(PaletteCount > 1); + ui->MoveUpButton->setEnabled(PaletteCount > 1 && CurrentPalette > 0); + ui->MoveDownButton->setEnabled(PaletteCount > 1 && CurrentPalette < PaletteCount - 1); +} + +void lcPartPaletteDialog::accept() +{ + std::vector PartPalettes; + PartPalettes.reserve(ui->PaletteList->count()); + + for (int ItemIdx = 0; ItemIdx < ui->PaletteList->count(); ItemIdx++) + { + QListWidgetItem* Item = ui->PaletteList->item(ItemIdx); + lcPartPalette* OldPalette = reinterpret_cast(Item->data(Qt::UserRole).value()); + + lcPartPalette Palette; + Palette.Name = Item->text(); + if (OldPalette) + Palette.Parts = std::move(OldPalette->Parts); + + PartPalettes.emplace_back(std::move(Palette)); + } + + mPartPalettes = std::move(PartPalettes); + + QDialog::accept(); +} + +void lcPartPaletteDialog::on_NewButton_clicked() +{ + bool Ok = false; + + QString Name = QInputDialog::getText(this, tr("New Part Palette"), tr("Palette Name:"), QLineEdit::Normal, QString(), &Ok); + + if (!Ok || Name.isEmpty()) + return; + + QListWidgetItem* Item = new QListWidgetItem(Name); + Item->setData(Qt::UserRole, qVariantFromValue(0)); + ui->PaletteList->addItem(Item); + ui->PaletteList->setCurrentRow(ui->PaletteList->count() - 1); + UpdateButtons(); +} + +void lcPartPaletteDialog::on_DeleteButton_clicked() +{ + QList SelectedItems = ui->PaletteList->selectedItems(); + + if (SelectedItems.isEmpty()) + return; + + QString Prompt = tr("Are you sure you want to delete the palette '%1'?").arg(SelectedItems[0]->text()); + if (QMessageBox::question(this, tr("Delete Part Palette"), Prompt, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) + return; + + QListWidgetItem* SelectedItem = SelectedItems.first(); + + delete SelectedItem; + UpdateButtons(); +} + +void lcPartPaletteDialog::on_RenameButton_clicked() +{ + QList SelectedItems = ui->PaletteList->selectedItems(); + + if (SelectedItems.isEmpty()) + return; + + bool Ok = false; + + QString Name = SelectedItems[0]->text(); + Name = QInputDialog::getText(this, tr("Rename Part Palette"), tr("Palette Name:"), QLineEdit::Normal, Name, &Ok); + + if (!Ok || Name.isEmpty()) + return; + + if (!Name.isEmpty()) + SelectedItems[0]->setText(Name); +} + +void lcPartPaletteDialog::on_MoveUpButton_clicked() +{ + QList SelectedItems = ui->PaletteList->selectedItems(); + + if (SelectedItems.isEmpty()) + return; + + QListWidgetItem* Item = SelectedItems[0]; + int Row = ui->PaletteList->row(Item); + + if (Row == 0) + return; + + ui->PaletteList->takeItem(Row); + ui->PaletteList->insertItem(Row - 1, Item); + ui->PaletteList->setCurrentItem(Item); + UpdateButtons(); +} + +void lcPartPaletteDialog::on_MoveDownButton_clicked() +{ + QList SelectedItems = ui->PaletteList->selectedItems(); + + if (SelectedItems.isEmpty()) + return; + + QListWidgetItem* Item = SelectedItems[0]; + int Row = ui->PaletteList->row(Item); + + ui->PaletteList->takeItem(Row); + ui->PaletteList->insertItem(Row + 1, Item); + ui->PaletteList->setCurrentItem(Item); + UpdateButtons(); +} + +void lcPartPaletteDialog::on_PaletteList_currentRowChanged(int CurrentRow) +{ + Q_UNUSED(CurrentRow); + + UpdateButtons(); +} diff --git a/common/lc_partpalettedialog.h b/common/lc_partpalettedialog.h new file mode 100644 index 00000000..e5cd58f3 --- /dev/null +++ b/common/lc_partpalettedialog.h @@ -0,0 +1,31 @@ +#pragma once + +namespace Ui { +class lcPartPaletteDialog; +} + +struct lcPartPalette; + +class lcPartPaletteDialog : public QDialog +{ + Q_OBJECT + +public: + lcPartPaletteDialog(QWidget* Parent, std::vector& PartPalettes); + ~lcPartPaletteDialog(); + +protected slots: + void accept(); + void on_NewButton_clicked(); + void on_DeleteButton_clicked(); + void on_RenameButton_clicked(); + void on_MoveUpButton_clicked(); + void on_MoveDownButton_clicked(); + void on_PaletteList_currentRowChanged(int CurrentRow); + +private: + void UpdateButtons(); + + Ui::lcPartPaletteDialog* ui; + std::vector& mPartPalettes; +}; diff --git a/common/lc_partpalettedialog.ui b/common/lc_partpalettedialog.ui new file mode 100644 index 00000000..097b9e18 --- /dev/null +++ b/common/lc_partpalettedialog.ui @@ -0,0 +1,131 @@ + + + lcPartPaletteDialog + + + + 0 + 0 + 400 + 300 + + + + Part Palettes + + + + + + + + + + + + + New... + + + + + + + Delete... + + + + + + + Rename... + + + + + + + Move Up + + + + + + + Move Down + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + PaletteList + NewButton + DeleteButton + RenameButton + MoveUpButton + MoveDownButton + + + + + buttonBox + accepted() + lcPartPaletteDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + lcPartPaletteDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/common/lc_partselectionwidget.cpp b/common/lc_partselectionwidget.cpp index 84067d79..dc13983e 100644 --- a/common/lc_partselectionwidget.cpp +++ b/common/lc_partselectionwidget.cpp @@ -1,5 +1,6 @@ #include "lc_global.h" #include "lc_partselectionwidget.h" +#include "lc_partpalettedialog.h" #include "lc_profile.h" #include "lc_application.h" #include "lc_mainwindow.h" @@ -844,7 +845,7 @@ void lcPartSelectionWidget::OptionsMenuAboutToShow() QMenu* Menu = (QMenu*)sender(); Menu->clear(); - Menu->addAction("Configure Palettes...", this, SLOT(ConfigurePartPalettes())); + Menu->addAction("Edit Palettes...", this, SLOT(EditPartPalettes())); Menu->addSeparator(); lcPartSelectionListModel* ListModel = mPartsWidget->GetListModel(); @@ -908,9 +909,15 @@ void lcPartSelectionWidget::OptionsMenuAboutToShow() } } -void lcPartSelectionWidget::ConfigurePartPalettes() +void lcPartSelectionWidget::EditPartPalettes() { + lcPartPaletteDialog Dialog(this, mPartPalettes); + if (Dialog.exec() != QDialog::Accepted) + return; + + SavePartPalettes(); + UpdateCategories(); } void lcPartSelectionWidget::Redraw() @@ -1045,25 +1052,46 @@ void lcPartSelectionWidget::RemoveFromPalette() void lcPartSelectionWidget::UpdateCategories() { - int CurrentIndex = mCategoriesWidget->indexOfTopLevelItem(mCategoriesWidget->currentItem()); + QTreeWidgetItem* CurrentItem = mCategoriesWidget->currentItem(); + lcPartCategoryType CurrentType = lcPartCategoryType::Count; + int CurrentIndex = -1; + + if (CurrentItem) + { + CurrentType = static_cast(CurrentItem->data(0, static_cast(lcPartCategoryRole::Type)).toInt()); + CurrentIndex = CurrentItem->data(0, static_cast(lcPartCategoryRole::Index)).toInt(); + CurrentItem = nullptr; + } mCategoriesWidget->clear(); QTreeWidgetItem* AllPartsCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(tr("All Parts"))); AllPartsCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::AllParts)); + if (CurrentType == lcPartCategoryType::AllParts && CurrentIndex == 0) + CurrentItem = AllPartsCategoryItem; + QTreeWidgetItem* CurrentModelCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(tr("Parts In Use"))); CurrentModelCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::PartsInUse)); + if (CurrentType == lcPartCategoryType::PartsInUse && CurrentIndex == 0) + CurrentItem = AllPartsCategoryItem; + QTreeWidgetItem* SubmodelsCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(tr("Submodels"))); SubmodelsCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::Submodels)); - for (int SetIdx = 0; SetIdx < static_cast(mPartPalettes.size()); SetIdx++) + if (CurrentType == lcPartCategoryType::Submodels && CurrentIndex == 0) + CurrentItem = AllPartsCategoryItem; + + for (int PaletteIdx = 0; PaletteIdx < static_cast(mPartPalettes.size()); PaletteIdx++) { - const lcPartPalette& Set = mPartPalettes[SetIdx]; - QTreeWidgetItem* SetCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(Set.Name)); - SetCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::Palette)); - SetCategoryItem->setData(0, static_cast(lcPartCategoryRole::Index), SetIdx); + const lcPartPalette& Set = mPartPalettes[PaletteIdx]; + QTreeWidgetItem* PaletteCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(Set.Name)); + PaletteCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::Palette)); + PaletteCategoryItem->setData(0, static_cast(lcPartCategoryRole::Index), PaletteIdx); + + if (CurrentType == lcPartCategoryType::Palette && CurrentIndex == PaletteIdx) + CurrentItem = PaletteCategoryItem; } for (int CategoryIdx = 0; CategoryIdx < gCategories.GetSize(); CategoryIdx++) @@ -1071,10 +1099,13 @@ void lcPartSelectionWidget::UpdateCategories() QTreeWidgetItem* CategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(gCategories[CategoryIdx].Name)); CategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::Category)); CategoryItem->setData(0, static_cast(lcPartCategoryRole::Index), CategoryIdx); + + if (CurrentType == lcPartCategoryType::Category && CurrentIndex == CategoryIdx) + CurrentItem = CategoryItem; } - if (CurrentIndex != -1) - mCategoriesWidget->setCurrentItem(mCategoriesWidget->topLevelItem(CurrentIndex)); + if (CurrentItem) + mCategoriesWidget->setCurrentItem(CurrentItem); } void lcPartSelectionWidget::UpdateModels() diff --git a/common/lc_partselectionwidget.h b/common/lc_partselectionwidget.h index 9728972e..b5d890f6 100644 --- a/common/lc_partselectionwidget.h +++ b/common/lc_partselectionwidget.h @@ -12,7 +12,8 @@ enum class lcPartCategoryType PartsInUse, Submodels, Palette, - Category + Category, + Count }; enum class lcPartCategoryRole @@ -225,7 +226,7 @@ protected slots: void CategoryChanged(QTreeWidgetItem* Current, QTreeWidgetItem* Previous); void PartChanged(const QModelIndex& Current, const QModelIndex& Previous); void OptionsMenuAboutToShow(); - void ConfigurePartPalettes(); + void EditPartPalettes(); protected: void LoadPartPalettes(); diff --git a/leocad.pro b/leocad.pro index 8ff50a8a..32c6b151 100644 --- a/leocad.pro +++ b/leocad.pro @@ -213,7 +213,8 @@ SOURCES += common/view.cpp \ common/lc_partselectionwidget.cpp \ common/lc_timelinewidget.cpp \ qt/lc_renderdialog.cpp \ - qt/lc_setsdatabasedialog.cpp + qt/lc_setsdatabasedialog.cpp \ + common/lc_partpalettedialog.cpp HEADERS += \ common/view.h \ common/texfont.h \ @@ -277,7 +278,8 @@ HEADERS += \ common/lc_partselectionwidget.h \ common/lc_timelinewidget.h \ qt/lc_renderdialog.h \ - qt/lc_setsdatabasedialog.h + qt/lc_setsdatabasedialog.h \ + common/lc_partpalettedialog.h FORMS += \ qt/lc_qarraydialog.ui \ qt/lc_qgroupdialog.ui \ @@ -294,7 +296,8 @@ FORMS += \ qt/lc_qfinddialog.ui \ qt/lc_qmodellistdialog.ui \ qt/lc_renderdialog.ui \ - qt/lc_setsdatabasedialog.ui + qt/lc_setsdatabasedialog.ui \ + common/lc_partpalettedialog.ui OTHER_FILES += RESOURCES += leocad.qrc