leocad/common/lc_modellistdialog.cpp

346 lines
9 KiB
C++
Raw Normal View History

2017-02-13 03:05:20 +01:00
#include "lc_global.h"
2023-04-30 05:10:50 +02:00
#include "lc_modellistdialog.h"
#include "ui_lc_modellistdialog.h"
2014-12-13 00:42:09 +01:00
#include "project.h"
#include "lc_profile.h"
#include "lc_model.h"
2014-12-13 00:42:09 +01:00
enum class lcModelListRole
{
ExistingModel = Qt::UserRole,
DuplicateModel
};
2024-06-17 02:43:02 +02:00
lcModelListDialog::lcModelListDialog(QWidget* Parent, const std::vector<std::unique_ptr<lcModel>>& Models)
2023-04-30 05:10:50 +02:00
: QDialog(Parent), ui(new Ui::lcModelListDialog)
2014-12-13 00:42:09 +01:00
{
mActiveModelItem = nullptr;
2014-12-13 00:42:09 +01:00
ui->setupUi(this);
2024-06-17 02:43:02 +02:00
lcModel* ActiveModel = lcGetActiveProject()->GetActiveModel();
int ActiveModelIndex = -1;
for (const std::unique_ptr<lcModel>& Model : Models)
2014-12-13 00:42:09 +01:00
{
QListWidgetItem* Item = new QListWidgetItem(Model->GetProperties().mFileName);
2024-06-17 02:43:02 +02:00
Item->setData(static_cast<int>(lcModelListRole::ExistingModel), QVariant::fromValue<uintptr_t>((uintptr_t)Model.get()));
2014-12-13 00:42:09 +01:00
ui->ModelList->addItem(Item);
2024-06-17 02:43:02 +02:00
if (Model.get() == ActiveModel )
ActiveModelIndex = ui->ModelList->count() - 1;
2014-12-13 00:42:09 +01:00
}
2024-06-17 02:43:02 +02:00
if (ActiveModelIndex != -1)
ui->ModelList->setCurrentRow(ActiveModelIndex);
QSettings Settings;
ui->SetActiveModel->setChecked(Settings.value("Settings/ModelListSetActive", true).toBool());
UpdateButtons();
2014-12-13 00:42:09 +01:00
}
2023-04-30 05:10:50 +02:00
lcModelListDialog::~lcModelListDialog()
2014-12-13 00:42:09 +01:00
{
delete ui;
}
2023-04-30 05:10:50 +02:00
int lcModelListDialog::GetActiveModelIndex() const
{
return ui->ModelList->row(mActiveModelItem);
}
2023-04-30 05:10:50 +02:00
std::vector<lcModelListDialogEntry> lcModelListDialog::GetResults() const
{
std::vector<lcModelListDialogEntry> Models;
Models.reserve(ui->ModelList->count());
for (int ItemIdx = 0; ItemIdx < ui->ModelList->count(); ItemIdx++)
{
QListWidgetItem* Item = ui->ModelList->item(ItemIdx);
lcModelListDialogEntry Entry;
Entry.Name = Item->text();
Entry.ExistingModel = reinterpret_cast<lcModel*>(Item->data(static_cast<int>(lcModelListRole::ExistingModel)).value<uintptr_t>());
Entry.DuplicateSource = reinterpret_cast<lcModel*>(Item->data(static_cast<int>(lcModelListRole::DuplicateModel)).value<uintptr_t>());
Models.emplace_back(Entry);
}
return Models;
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::UpdateButtons()
{
int ModelCount = ui->ModelList->count();
ui->DeleteModel->setEnabled(ModelCount > 1);
ui->SetActiveModel->setEnabled(ui->ModelList->currentItem() != nullptr);
bool MoveUp = false;
bool MoveDown = false;
for (int Row = 0; Row < ui->ModelList->count(); Row++)
{
QListWidgetItem* Item = ui->ModelList->item(Row);
if (!Item->isSelected())
continue;
if (Row > 0 && !ui->ModelList->item(Row - 1)->isSelected())
MoveUp = true;
if (Row < ModelCount - 1 && !ui->ModelList->item(Row + 1)->isSelected())
MoveDown = true;
}
ui->MoveUp->setEnabled(MoveUp);
ui->MoveDown->setEnabled(MoveDown);
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::accept()
2014-12-13 00:42:09 +01:00
{
if (ui->SetActiveModel->isChecked())
mActiveModelItem = ui->ModelList->currentItem();
QSettings Settings;
Settings.setValue("Settings/ModelListSetActive", ui->SetActiveModel->isChecked());
2014-12-13 00:42:09 +01:00
QDialog::accept();
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_NewModel_clicked()
2014-12-13 00:42:09 +01:00
{
QStringList ModelNames;
2014-12-13 00:42:09 +01:00
for (int ItemIdx = 0; ItemIdx < ui->ModelList->count(); ItemIdx++)
ModelNames.append(ui->ModelList->item(ItemIdx)->text());
2014-12-13 00:42:09 +01:00
QString Name = lcGetActiveProject()->GetNewModelName(this, tr("New Submodel"), QString(), ModelNames);
2014-12-13 00:42:09 +01:00
if (Name.isEmpty())
return;
QListWidgetItem* Item = new QListWidgetItem(Name);
ui->ModelList->addItem(Item);
UpdateButtons();
2014-12-13 00:42:09 +01:00
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_DeleteModel_clicked()
2014-12-13 00:42:09 +01:00
{
if (ui->ModelList->count() == 1)
{
QMessageBox::information(this, tr("Delete Submodel"), tr("The model cannot be empty."));
2014-12-13 00:42:09 +01:00
return;
}
QList<QListWidgetItem*> SelectedItems = ui->ModelList->selectedItems();
if (SelectedItems.isEmpty())
{
QMessageBox::information(this, tr("Delete Submodel"), tr("No submodel selected."));
2014-12-13 00:42:09 +01:00
return;
}
QString Prompt;
if (SelectedItems.size() == 1)
Prompt = tr("Are you sure you want to delete the submodel '%1'?").arg(SelectedItems[0]->text());
else
Prompt = tr("Are you sure you want to delete %1 submodels?").arg(QString::number(SelectedItems.size()));
2014-12-13 00:42:09 +01:00
if (QMessageBox::question(this, tr("Delete Submodel"), Prompt, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
2014-12-13 00:42:09 +01:00
return;
for (QListWidgetItem* SelectedItem : SelectedItems)
{
if (mActiveModelItem == SelectedItem)
mActiveModelItem = nullptr;
delete SelectedItem;
}
UpdateButtons();
2014-12-13 00:42:09 +01:00
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_RenameModel_clicked()
2014-12-13 00:42:09 +01:00
{
QList<QListWidgetItem*> SelectedItems = ui->ModelList->selectedItems();
if (SelectedItems.isEmpty())
{
QMessageBox::information(this, tr("Rename Submodel"), tr("No submodel selected."));
2014-12-13 00:42:09 +01:00
return;
}
2014-12-13 00:42:09 +01:00
for (QListWidgetItem* CurrentItem : SelectedItems)
{
QStringList ModelNames;
2014-12-13 00:42:09 +01:00
for (int ItemIdx = 0; ItemIdx < ui->ModelList->count(); ItemIdx++)
ModelNames.append(ui->ModelList->item(ItemIdx)->text());
2014-12-13 00:42:09 +01:00
QString Name = lcGetActiveProject()->GetNewModelName(this, tr("Rename Submodel"), CurrentItem->text(), ModelNames);
2014-12-13 00:42:09 +01:00
if (!Name.isEmpty())
CurrentItem->setText(Name);
}
2014-12-13 00:42:09 +01:00
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_ExportModel_clicked()
{
QList<QListWidgetItem*> SelectedItems = ui->ModelList->selectedItems();
if (SelectedItems.isEmpty())
{
QMessageBox::information(this, tr("Export Submodel"), tr("No submodel selected."));
return;
}
if (SelectedItems.size() == 1)
{
QListWidgetItem* CurrentItem = SelectedItems[0];
lcModel* Model = (lcModel*)CurrentItem->data(static_cast<int>(lcModelListRole::ExistingModel)).value<uintptr_t>();
if (!Model)
{
Model = (lcModel*)CurrentItem->data(static_cast<int>(lcModelListRole::DuplicateModel)).value<uintptr_t>();
if (!Model)
{
QMessageBox::information(this, tr("LeoCAD"), tr("Nothing to export."));
return;
}
}
QString SaveFileName = QFileInfo(QDir(lcGetProfileString(LC_PROFILE_PROJECTS_PATH)), CurrentItem->text()).absoluteFilePath();
SaveFileName = QFileDialog::getSaveFileName(this, tr("Export Model"), SaveFileName, tr("Supported Files (*.ldr *.dat);;All Files (*.*)"));
if (SaveFileName.isEmpty())
return;
lcGetActiveProject()->ExportModel(SaveFileName, Model);
lcSetProfileString(LC_PROFILE_PROJECTS_PATH, QFileInfo(SaveFileName).absolutePath());
}
else
{
QString Folder = QFileDialog::getExistingDirectory(this, tr("Select Export Folder"), lcGetProfileString(LC_PROFILE_PROJECTS_PATH));
for (QListWidgetItem* CurrentItem : SelectedItems)
{
lcModel* Model = (lcModel*)CurrentItem->data(static_cast<int>(lcModelListRole::ExistingModel)).value<uintptr_t>();
if (!Model)
Model = (lcModel*)CurrentItem->data(static_cast<int>(lcModelListRole::DuplicateModel)).value<uintptr_t>();
if (Model)
{
QString SaveFileName = QFileInfo(QDir(Folder), CurrentItem->text()).absoluteFilePath();
lcGetActiveProject()->ExportModel(SaveFileName, Model);
}
}
lcSetProfileString(LC_PROFILE_PROJECTS_PATH, Folder);
}
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_DuplicateModel_clicked()
{
QList<QListWidgetItem*> SelectedItems = ui->ModelList->selectedItems();
if (SelectedItems.isEmpty())
{
QMessageBox::information(this, tr("Duplicate Submodel"), tr("No submodel selected."));
return;
}
for (QListWidgetItem* CurrentItem : SelectedItems)
{
QStringList ModelNames;
for (int ItemIdx = 0; ItemIdx < ui->ModelList->count(); ItemIdx++)
ModelNames.append(ui->ModelList->item(ItemIdx)->text());
QString Name = CurrentItem->text();
do
{
Name = tr("Copy of ") + Name;
} while (ModelNames.contains(Name, Qt::CaseInsensitive));
Name = lcGetActiveProject()->GetNewModelName(this, tr("Duplicate Submodel"), Name, ModelNames);
if (Name.isEmpty())
continue;
QListWidgetItem* Item = new QListWidgetItem(Name);
uintptr_t ExistingModel = CurrentItem->data(static_cast<int>(lcModelListRole::ExistingModel)).value<uintptr_t>();
if (!ExistingModel)
ExistingModel = CurrentItem->data(static_cast<int>(lcModelListRole::DuplicateModel)).value<uintptr_t>();
Item->setData(static_cast<int>(lcModelListRole::DuplicateModel), QVariant::fromValue<uintptr_t>(ExistingModel));
ui->ModelList->addItem(Item);
}
UpdateButtons();
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_MoveUp_clicked()
2014-12-13 00:42:09 +01:00
{
bool Blocked = ui->ModelList->blockSignals(true);
2014-12-13 00:42:09 +01:00
for (int Row = 1; Row < ui->ModelList->count(); Row++)
{
QListWidgetItem* Item = ui->ModelList->item(Row);
2014-12-13 00:42:09 +01:00
if (!Item->isSelected())
continue;
2014-12-13 00:42:09 +01:00
if (ui->ModelList->item(Row - 1)->isSelected())
continue;
ui->ModelList->takeItem(Row);
ui->ModelList->insertItem(Row - 1, Item);
Item->setSelected(true);
}
2014-12-13 00:42:09 +01:00
ui->ModelList->blockSignals(Blocked);
UpdateButtons();
2014-12-13 00:42:09 +01:00
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_MoveDown_clicked()
2014-12-13 00:42:09 +01:00
{
bool Blocked = ui->ModelList->blockSignals(true);
2014-12-13 00:42:09 +01:00
for (int Row = ui->ModelList->count() - 2; Row >= 0; Row--)
{
QListWidgetItem* Item = ui->ModelList->item(Row);
if (!Item->isSelected())
continue;
2014-12-13 00:42:09 +01:00
if (ui->ModelList->item(Row + 1)->isSelected())
continue;
2014-12-13 00:42:09 +01:00
ui->ModelList->takeItem(Row);
ui->ModelList->insertItem(Row + 1, Item);
Item->setSelected(true);
}
ui->ModelList->blockSignals(Blocked);
UpdateButtons();
2014-12-13 00:42:09 +01:00
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_ModelList_itemDoubleClicked(QListWidgetItem* Item)
{
mActiveModelItem = Item;
2016-02-17 00:11:52 +01:00
accept();
}
2023-04-30 05:10:50 +02:00
void lcModelListDialog::on_ModelList_itemSelectionChanged()
{
UpdateButtons();
}