FreeCAD
1/****************************************************************************
2* Copyright (c) 2021 Wanderer Fan <wandererfan@gmail.com> *
3* *
4* This file is part of the FreeCAD CAx development system. *
5* *
6* This library is free software; you can redistribute it and/or *
7* modify it under the terms of the GNU Library General Public *
8* License as published by the Free Software Foundation; either *
9* version 2 of the License, or (at your option) any later version. *
10* *
11* This library is distributed in the hope that it will be useful, *
12* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14* GNU Library General Public License for more details. *
15* *
16* You should have received a copy of the GNU Library General Public *
17* License along with this library; see the file COPYING.LIB. If not, *
18* write to the Free Software Foundation, Inc., 59 Temple Place, *
19* Suite 330, Boston, MA 02111-1307, USA *
20* *
21****************************************************************************/
22
23#include "PreCompiled.h"24#ifndef _PreComp_25# include <QListWidgetItem>26# include <QList>27#endif28
29#include <Base/Console.h> // for FC_LOG_LEVEL_INIT30#include <Base/Tools.h>31
32#include "DlgPageChooser.h"33#include "ui_DlgPageChooser.h"34
35
36FC_LOG_LEVEL_INIT("Gui", true, true)37
38using namespace TechDrawGui;39
40/* TRANSLATOR Gui::DlgPageChooser */
41
42DlgPageChooser::DlgPageChooser(43const std::vector<std::string> labels,44const std::vector<std::string> names,45QWidget* parent, Qt::WindowFlags fl)46: QDialog(parent, fl), ui(new Ui_DlgPageChooser)47{
48ui->setupUi(this);49ui->lwPages->setSortingEnabled(true);50
51fillList(labels, names);52
53connect(ui->bbButtons, &QDialogButtonBox::accepted, this, &QDialog::accept);54connect(ui->bbButtons, &QDialogButtonBox::rejected, this, &QDialog::reject);55}
56
57/**
58* Destroys the object and frees any allocated resources
59*/
60DlgPageChooser::~DlgPageChooser()61{
62// no need to delete child widgets, Qt does it all for us63delete ui;64}
65
66void DlgPageChooser::fillList(std::vector<std::string> labels, std::vector<std::string> names)67{
68QListWidgetItem* item;69QString qLabel;70QString qName;71QString qText;72int labelCount = labels.size();73int i = 0;74for (; i < labelCount; i++) {75qLabel = Base::Tools::fromStdString(labels[i]);76qName = Base::Tools::fromStdString(names[i]);77qText = QString::fromUtf8("%1 (%2)").arg(qLabel, qName);78item = new QListWidgetItem(qText, ui->lwPages);79item->setData(Qt::UserRole, qName);80}81}
82
83std::string DlgPageChooser::getSelection() const84{
85QList<QListWidgetItem*> sels = ui->lwPages->selectedItems();86if (!sels.empty()) {87QListWidgetItem* item = sels.front();88return item->data(Qt::UserRole).toByteArray().constData();89}90return std::string();91}
92
93
94void DlgPageChooser::accept() {95QDialog::accept();96}
97
98void DlgPageChooser::reject() {99QDialog::reject();100}
101
102#include "moc_DlgPageChooser.cpp"103