diff --git a/qt/lc_qmodellistdialog.cpp b/qt/lc_qmodellistdialog.cpp index 7453df1a..f04015cd 100644 --- a/qt/lc_qmodellistdialog.cpp +++ b/qt/lc_qmodellistdialog.cpp @@ -33,12 +33,30 @@ int lcQModelListDialog::GetActiveModelIndex() const void lcQModelListDialog::UpdateButtons() { - int CurrentModel = ui->ModelList->currentRow(); - int NumModels = ui->ModelList->count(); + int ModelCount = ui->ModelList->count(); - ui->DeleteModel->setEnabled(NumModels > 1); - ui->MoveUp->setEnabled(NumModels > 1 && CurrentModel > 0); - ui->MoveDown->setEnabled(NumModels > 1 && CurrentModel < NumModels - 1); + 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); } void lcQModelListDialog::accept() @@ -76,25 +94,35 @@ void lcQModelListDialog::on_DeleteModel_clicked() { if (ui->ModelList->count() == 1) { - QMessageBox::information(this, tr("Error"), tr("The model cannot be empty.")); + QMessageBox::information(this, tr("Delete Submodel"), tr("The model cannot be empty.")); return; } QList SelectedItems = ui->ModelList->selectedItems(); if (SelectedItems.isEmpty()) + { + QMessageBox::information(this, tr("Delete Submodel"), tr("No submodel selected.")); 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())); - QString Prompt = tr("Are you sure you want to delete the submodel '%1'?").arg(SelectedItems[0]->text()); if (QMessageBox::question(this, tr("Delete Submodel"), Prompt, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) return; - QListWidgetItem* SelectedItem = SelectedItems.first(); + for (QListWidgetItem* SelectedItem : SelectedItems) + { + if (mActiveModelItem == SelectedItem) + mActiveModelItem = nullptr; - if (mActiveModelItem == SelectedItem) - mActiveModelItem = nullptr; + delete SelectedItem; + } - delete SelectedItem; UpdateButtons(); } @@ -103,78 +131,119 @@ void lcQModelListDialog::on_RenameModel_clicked() QList SelectedItems = ui->ModelList->selectedItems(); if (SelectedItems.isEmpty()) + { + QMessageBox::information(this, tr("Rename Submodel"), tr("No submodel selected.")); return; + } - QStringList ModelNames; + for (QListWidgetItem* CurrentItem : SelectedItems) + { + QStringList ModelNames; - for (int ItemIdx = 0; ItemIdx < ui->ModelList->count(); ItemIdx++) - ModelNames.append(ui->ModelList->item(ItemIdx)->text()); + for (int ItemIdx = 0; ItemIdx < ui->ModelList->count(); ItemIdx++) + ModelNames.append(ui->ModelList->item(ItemIdx)->text()); - QString Name = lcGetActiveProject()->GetNewModelName(this, tr("Rename Submodel"), SelectedItems[0]->text(), ModelNames); + QString Name = lcGetActiveProject()->GetNewModelName(this, tr("Rename Submodel"), CurrentItem->text(), ModelNames); - if (!Name.isEmpty()) - SelectedItems[0]->setText(Name); + if (!Name.isEmpty()) + CurrentItem->setText(Name); + } } void lcQModelListDialog::on_ExportModel_clicked() { - QListWidgetItem* CurrentItem = ui->ModelList->currentItem(); + QList SelectedItems = ui->ModelList->selectedItems(); - if (!CurrentItem) - return; - - lcModel* Model = (lcModel*)CurrentItem->data(Qt::UserRole).value(); - - if (!Model) + if (SelectedItems.isEmpty()) { - QMessageBox::information(this, tr("LeoCAD"), tr("Nothing to export.")); + QMessageBox::information(this, tr("Export Submodel"), tr("No submodel selected.")); return; } - QString SaveFileName = QFileInfo(QDir(lcGetProfileString(LC_PROFILE_PROJECTS_PATH)), CurrentItem->text()).absoluteFilePath(); + if (SelectedItems.size() == 1) + { + QListWidgetItem* CurrentItem = SelectedItems[0]; + lcModel* Model = (lcModel*)CurrentItem->data(Qt::UserRole).value(); - SaveFileName = QFileDialog::getSaveFileName(this, tr("Save Model"), SaveFileName, tr("Supported Files (*.ldr *.dat);;All Files (*.*)")); + if (!Model) + { + QMessageBox::information(this, tr("LeoCAD"), tr("Nothing to export.")); + return; + } - if (SaveFileName.isEmpty()) - return; + QString SaveFileName = QFileInfo(QDir(lcGetProfileString(LC_PROFILE_PROJECTS_PATH)), CurrentItem->text()).absoluteFilePath(); - lcGetActiveProject()->ExportModel(SaveFileName, Model); + SaveFileName = QFileDialog::getSaveFileName(this, tr("Save Model"), SaveFileName, tr("Supported Files (*.ldr *.dat);;All Files (*.*)")); - lcSetProfileString(LC_PROFILE_PROJECTS_PATH, QFileInfo(SaveFileName).absolutePath()); + if (SaveFileName.isEmpty()) + return; + + lcGetActiveProject()->ExportModel(SaveFileName, Model); + + lcSetProfileString(LC_PROFILE_PROJECTS_PATH, QFileInfo(SaveFileName).absolutePath()); + } + else + { + QString Folder = QFileDialog::getExistingDirectory(this, tr("Select Parts Library Folder"), lcGetProfileString(LC_PROFILE_PROJECTS_PATH)); + + for (QListWidgetItem* CurrentItem : SelectedItems) + { + lcModel* Model = (lcModel*)CurrentItem->data(Qt::UserRole).value(); + + if (Model) + { + QString SaveFileName = QFileInfo(QDir(Folder), CurrentItem->text()).absoluteFilePath(); + lcGetActiveProject()->ExportModel(SaveFileName, Model); + } + } + + lcSetProfileString(LC_PROFILE_PROJECTS_PATH, Folder); + } } void lcQModelListDialog::on_MoveUp_clicked() { - QList SelectedItems = ui->ModelList->selectedItems(); + bool Blocked = ui->ModelList->blockSignals(true); - if (SelectedItems.isEmpty()) - return; + for (int Row = 1; Row < ui->ModelList->count(); Row++) + { + QListWidgetItem* Item = ui->ModelList->item(Row); - QListWidgetItem* Item = SelectedItems[0]; - int Row = ui->ModelList->row(Item); + if (!Item->isSelected()) + continue; - if (Row == 0) - return; + if (ui->ModelList->item(Row - 1)->isSelected()) + continue; - ui->ModelList->takeItem(Row); - ui->ModelList->insertItem(Row - 1, Item); - ui->ModelList->setCurrentItem(Item); + ui->ModelList->takeItem(Row); + ui->ModelList->insertItem(Row - 1, Item); + Item->setSelected(true); + } + + ui->ModelList->blockSignals(Blocked); UpdateButtons(); } void lcQModelListDialog::on_MoveDown_clicked() { - QList SelectedItems = ui->ModelList->selectedItems(); + bool Blocked = ui->ModelList->blockSignals(true); - if (SelectedItems.isEmpty()) - return; + for (int Row = ui->ModelList->count() - 2; Row >= 0; Row--) + { + QListWidgetItem* Item = ui->ModelList->item(Row); - QListWidgetItem* Item = SelectedItems[0]; - int Row = ui->ModelList->row(Item); + if (!Item->isSelected()) + continue; - ui->ModelList->takeItem(Row); - ui->ModelList->insertItem(Row + 1, Item); - ui->ModelList->setCurrentItem(Item); + if (ui->ModelList->item(Row + 1)->isSelected()) + continue; + + ui->ModelList->takeItem(Row); + ui->ModelList->insertItem(Row + 1, Item); + Item->setSelected(true); + } + + ui->ModelList->blockSignals(Blocked); UpdateButtons(); } @@ -190,9 +259,7 @@ void lcQModelListDialog::on_ModelList_itemDoubleClicked(QListWidgetItem* Item) accept(); } -void lcQModelListDialog::on_ModelList_currentRowChanged(int CurrentRow) +void lcQModelListDialog::on_ModelList_itemSelectionChanged() { - Q_UNUSED(CurrentRow); - UpdateButtons(); } diff --git a/qt/lc_qmodellistdialog.h b/qt/lc_qmodellistdialog.h index b7df21ba..18967321 100644 --- a/qt/lc_qmodellistdialog.h +++ b/qt/lc_qmodellistdialog.h @@ -28,7 +28,7 @@ public slots: void on_MoveDown_clicked(); void on_SetActiveModel_clicked(); void on_ModelList_itemDoubleClicked(QListWidgetItem* Item); - void on_ModelList_currentRowChanged(int CurrentRow); + void on_ModelList_itemSelectionChanged(); private: QListWidgetItem* mActiveModelItem; diff --git a/qt/lc_qmodellistdialog.ui b/qt/lc_qmodellistdialog.ui index 6180b6bf..b9fb3a77 100644 --- a/qt/lc_qmodellistdialog.ui +++ b/qt/lc_qmodellistdialog.ui @@ -17,7 +17,11 @@ - + + + QAbstractItemView::ExtendedSelection + +