File size: 4,144 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/****************************************************************************
 *   Copyright (c) 2021 Wanderer Fan <wandererfan@gmail.com>                *
 *                                                                          *
 *   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 <QListWidgetItem>
# include <QList>
# include <QPushButton>

#include <Base/Console.h> // for FC_LOG_LEVEL_INIT

#include "DlgPageChooser.h"
#include "ui_DlgPageChooser.h"


FC_LOG_LEVEL_INIT("Gui", true, true)    //NOLINT

using namespace TechDrawGui;

/* TRANSLATOR Gui::DlgPageChooser */

DlgPageChooser::DlgPageChooser(
        const std::vector<std::string>& labels,
        const std::vector<std::string>& 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, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(ui->bbButtons, &QDialogButtonBox::rejected, this, &QDialog::reject);
    connect(ui->lwPages, &QListWidget::itemSelectionChanged, this, &DlgPageChooser::slotChangedSelection);
    auto acceptButton = ui->bbButtons->button(QDialogButtonBox::Ok);
    acceptButton->setEnabled(false);
}

/**
 *  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::slotChangedSelection()
{
    auto acceptButton = ui->bbButtons->button(QDialogButtonBox::Ok);
    if (ui->lwPages->selectedItems().empty()) {
        acceptButton->setEnabled(false);
        return;
    }

    acceptButton->setEnabled(true);
}

void DlgPageChooser::fillList(std::vector<std::string> labels, std::vector<std::string> names)
{
    size_t labelCount = labels.size();
    size_t i = 0;
    for (; i < labelCount; i++) {
        auto qLabel = QString::fromStdString(labels[i]);
        auto qName = QString::fromStdString(names[i]);
        auto qText = QStringLiteral("%1 (%2)").arg(qLabel, qName);
        auto* item = new QListWidgetItem(qText, ui->lwPages);
        item->setData(Qt::UserRole, qName);
    }
}

std::string DlgPageChooser::getSelection() const
{
    QList<QListWidgetItem*> sels = ui->lwPages->selectedItems();
    if (!sels.empty()) {
        QListWidgetItem* item = sels.front();
        return item->data(Qt::UserRole).toByteArray().constData();
    }
    return {};
}


void DlgPageChooser::accept() {
    if (ui->lwPages->selectedItems().empty()) {
        Base::Console().message("Page Chooser: no page was selected\n");
    }
    QDialog::accept();
}

void DlgPageChooser::reject() {
    QDialog::reject();
}

//NOLINTNEXTLINE
#include "moc_DlgPageChooser.cpp"