From 5f109b5ee32f0bf20ef9e6ac0b60fa24b6871747 Mon Sep 17 00:00:00 2001 From: Wanderer Fan Date: Thu, 30 Dec 2021 12:25:24 -0500 Subject: [PATCH] [TD]Allow selection of Page when adding View --- src/Mod/TechDraw/Gui/CMakeLists.txt | 4 + src/Mod/TechDraw/Gui/DlgPageChooser.cpp | 103 ++++++++++++++++++++++++ src/Mod/TechDraw/Gui/DlgPageChooser.h | 57 +++++++++++++ src/Mod/TechDraw/Gui/DlgPageChooser.ui | 93 +++++++++++++++++++++ src/Mod/TechDraw/Gui/DrawGuiUtil.cpp | 31 ++++++- 5 files changed, 284 insertions(+), 4 deletions(-) create mode 100644 src/Mod/TechDraw/Gui/DlgPageChooser.cpp create mode 100644 src/Mod/TechDraw/Gui/DlgPageChooser.h create mode 100644 src/Mod/TechDraw/Gui/DlgPageChooser.ui diff --git a/src/Mod/TechDraw/Gui/CMakeLists.txt b/src/Mod/TechDraw/Gui/CMakeLists.txt index 1cffb31d8f..f0c7d73fba 100644 --- a/src/Mod/TechDraw/Gui/CMakeLists.txt +++ b/src/Mod/TechDraw/Gui/CMakeLists.txt @@ -54,6 +54,7 @@ else() endif() set(TechDrawGui_UIC_SRCS + DlgPageChooser.ui DlgPrefsTechDrawAdvanced.ui DlgPrefsTechDrawAnnotation.ui DlgPrefsTechDrawColors.ui @@ -117,6 +118,9 @@ SET(TechDrawGui_SRCS TaskProjGroup.ui TaskProjGroup.cpp TaskProjGroup.h + DlgPageChooser.ui + DlgPageChooser.cpp + DlgPageChooser.h DlgPrefsTechDrawGeneral.ui DlgPrefsTechDrawGeneralImp.cpp DlgPrefsTechDrawGeneralImp.h diff --git a/src/Mod/TechDraw/Gui/DlgPageChooser.cpp b/src/Mod/TechDraw/Gui/DlgPageChooser.cpp new file mode 100644 index 0000000000..b8373e1f86 --- /dev/null +++ b/src/Mod/TechDraw/Gui/DlgPageChooser.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** + * Copyright (c) 2021 Wanderer Fan * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ****************************************************************************/ +#include "PreCompiled.h" +#ifndef _PreComp_ +# include +# include +# include +# include +# include +#endif + +#include +#include +#include "DlgPageChooser.h" +#include "ui_DlgPageChooser.h" + +FC_LOG_LEVEL_INIT("Gui",true,true) + +using namespace TechDrawGui; + +/* TRANSLATOR Gui::DlgPageChooser */ + +DlgPageChooser::DlgPageChooser( + const std::vector labels, + const std::vector names, + QWidget* parent, Qt::WindowFlags fl) + : QDialog(parent, fl), ui(new Ui_DlgPageChooser) +{ + ui->setupUi(this); + ui->lwPages->setSortingEnabled(true); + + fillList(labels, names); + + connect(ui->bbButtons, SIGNAL(accepted()), this, SLOT(accept())); + connect(ui->bbButtons, SIGNAL(rejected()), this, SLOT(reject())); +} + +/** + * Destroys the object and frees any allocated resources + */ +DlgPageChooser::~DlgPageChooser() +{ + // no need to delete child widgets, Qt does it all for us + delete ui; +} + +void DlgPageChooser::fillList(std::vector labels, std::vector names) +{ + QListWidgetItem* item; + QString qLabel; + QString qName; + QString qText; + int labelCount = labels.size(); + int i = 0; + for(; i < labelCount; i++) { + qLabel = Base::Tools::fromStdString(labels[i]); + qName = Base::Tools::fromStdString(names[i]); + qText = QString::fromUtf8("%1 (%2)").arg(qLabel).arg(qName); + item = new QListWidgetItem(qText, ui->lwPages); + item->setData(Qt::UserRole, qName); + } +} + +std::string DlgPageChooser::getSelection() const +{ + std::string result; + QList sels = ui->lwPages->selectedItems(); + if (!sels.empty()) { + QListWidgetItem* item = sels.front(); + result = item->data(Qt::UserRole).toByteArray().constData(); + } + return result; +} + + +void DlgPageChooser::accept() { + QDialog::accept(); +} + +void DlgPageChooser::reject() { + QDialog::reject(); +} + +#include "moc_DlgPageChooser.cpp" diff --git a/src/Mod/TechDraw/Gui/DlgPageChooser.h b/src/Mod/TechDraw/Gui/DlgPageChooser.h new file mode 100644 index 0000000000..d786276bd8 --- /dev/null +++ b/src/Mod/TechDraw/Gui/DlgPageChooser.h @@ -0,0 +1,57 @@ +/**************************************************************************** + * Copyright (c) 2021 Wanderer Fan * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ****************************************************************************/ +#ifndef GUI_DLGPAGECHOOSER_H +#define GUI_DLGPAGECHOOSER_H + +#include + +namespace TechDrawGui { + +class Ui_DlgPageChooser; +class TechDrawGuiExport DlgPageChooser : public QDialog +{ + Q_OBJECT + +public: + DlgPageChooser(const std::vector labels, + const std::vector names, + QWidget* parent = 0, Qt::WindowFlags fl = Qt::WindowFlags()); + ~DlgPageChooser(); + + std::string getSelection() const; + void accept(); + void reject(); + +private Q_SLOTS: + +private: + void fillList(std::vector labels, std::vector names); + +private: + Ui_DlgPageChooser* ui; +}; + +} // namespace Gui + + +#endif // GUI_DLGPAGECHOOSER_H + diff --git a/src/Mod/TechDraw/Gui/DlgPageChooser.ui b/src/Mod/TechDraw/Gui/DlgPageChooser.ui new file mode 100644 index 0000000000..c10bdf4304 --- /dev/null +++ b/src/Mod/TechDraw/Gui/DlgPageChooser.ui @@ -0,0 +1,93 @@ + + + TechDrawGui::DlgPageChooser + + + Qt::WindowModal + + + + 0 + 0 + 360 + 280 + + + + Page Chooser + + + + + + true + + + + + + FreeCAD could not determine which Page to use. Please select a Page. + + + true + + + + + + + Select a Page that should be used + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + false + + + + + + + + + bbButtons + accepted() + TechDrawGui::DlgPageChooser + accept() + + + 179 + 228 + + + 179 + 139 + + + + + bbButtons + rejected() + TechDrawGui::DlgPageChooser + reject() + + + 179 + 228 + + + 179 + 139 + + + + + diff --git a/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp index 1fa0ce681e..3ff5097b44 100644 --- a/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp +++ b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp @@ -80,6 +80,7 @@ #include "QGVPage.h" #include "MDIViewPage.h" #include "ViewProviderPage.h" +#include "DlgPageChooser.h" #include "DrawGuiUtil.h" using namespace TechDrawGui; @@ -104,6 +105,8 @@ void DrawGuiUtil::loadArrowBox(QComboBox* qcb) TechDraw::DrawPage* DrawGuiUtil::findPage(Gui::Command* cmd) { TechDraw::DrawPage* page = nullptr; + std::vector names; + std::vector labels; //check Selection for a page std::vector selPages = cmd->getSelection(). @@ -127,8 +130,18 @@ TechDraw::DrawPage* DrawGuiUtil::findPage(Gui::Command* cmd) page = qp->getDrawPage(); } else { // no active page - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Which page?"), - QObject::tr("Can not determine correct page.")); + for(auto obj: selPages) { + std::string name = obj->getNameInDocument(); + names.push_back(name); + std::string label = obj->Label.getValue(); + labels.push_back(label); + } + DlgPageChooser dlg(labels, names, Gui::getMainWindow()); + if(dlg.exec()==QDialog::Accepted) { + std::string selName = dlg.getSelection(); + App::Document* doc = cmd->getDocument(); + page = static_cast(doc->getObject(selName.c_str())); + } } } else { //only 1 page in document - use it @@ -136,8 +149,18 @@ TechDraw::DrawPage* DrawGuiUtil::findPage(Gui::Command* cmd) } } else if (selPages.size() > 1) { //multiple pages in selection - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), - QObject::tr("Select only 1 page.")); + for(auto obj: selPages) { + std::string name = obj->getNameInDocument(); + names.push_back(name); + std::string label = obj->Label.getValue(); + labels.push_back(label); + } + DlgPageChooser dlg(labels, names, Gui::getMainWindow()); + if(dlg.exec()==QDialog::Accepted) { + std::string selName = dlg.getSelection(); + App::Document* doc = cmd->getDocument(); + page = static_cast(doc->getObject(selName.c_str())); + } } else { //exactly 1 page in selection, use it page = static_cast(selPages.front());